|
| 1 | +import os |
| 2 | +from typing import Any, Dict, Tuple, List |
| 3 | + |
| 4 | +try: |
| 5 | + import yaml # type: ignore |
| 6 | +except Exception as e: |
| 7 | + yaml = None |
| 8 | + |
| 9 | + |
| 10 | +def _ensure_dict(d: Dict[str, Any], key: str) -> Dict[str, Any]: |
| 11 | + if key not in d or d.get(key) is None: |
| 12 | + d[key] = {} |
| 13 | + return d[key] |
| 14 | + |
| 15 | + |
| 16 | +def normalize_config(cfg: Dict[str, Any]) -> Dict[str, Any]: |
| 17 | + c = dict(cfg) |
| 18 | + |
| 19 | + training = _ensure_dict(c, "training") |
| 20 | + optimizer_obj = training.get("optimizer") |
| 21 | + if isinstance(optimizer_obj, str): |
| 22 | + name = optimizer_obj |
| 23 | + lr = training.pop("lr", None) |
| 24 | + training["optimizer"] = {"name": name} |
| 25 | + if lr is not None: |
| 26 | + training["optimizer"]["lr"] = lr |
| 27 | + elif isinstance(optimizer_obj, dict): |
| 28 | + if "lr" not in optimizer_obj and "lr" in training: |
| 29 | + optimizer_obj["lr"] = training.pop("lr") |
| 30 | + else: |
| 31 | + lr = training.pop("lr", None) |
| 32 | + if lr is not None: |
| 33 | + training["optimizer"] = {"name": "adamw_torch", "lr": lr} |
| 34 | + |
| 35 | + if "batch_size_per_gpu" not in training and "batch_size" in training: |
| 36 | + training["batch_size_per_gpu"] = training.pop("batch_size") |
| 37 | + |
| 38 | + data = _ensure_dict(c, "data") |
| 39 | + if "num_workers" not in data and "num_proc" in data: |
| 40 | + data["num_workers"] = data.pop("num_proc") |
| 41 | + |
| 42 | + validation = data.get("validation") |
| 43 | + if isinstance(validation, dict): |
| 44 | + if "batch_size_per_gpu" not in validation and "batch_size" in validation: |
| 45 | + validation["batch_size_per_gpu"] = validation.pop("batch_size") |
| 46 | + |
| 47 | + model = _ensure_dict(c, "model") |
| 48 | + if "tokenizer_name" not in data and "name" in model: |
| 49 | + data["tokenizer_name"] = model["name"] |
| 50 | + |
| 51 | + checkpoint = _ensure_dict(c, "checkpoint") |
| 52 | + if "output_dir" not in checkpoint and "dir" in checkpoint: |
| 53 | + checkpoint.setdefault("output_dir", checkpoint.get("dir")) |
| 54 | + |
| 55 | + return c |
| 56 | + |
| 57 | + |
| 58 | +def validate_config(cfg: Dict[str, Any]) -> Tuple[List[str], List[str]]: |
| 59 | + errors: List[str] = [] |
| 60 | + warnings: List[str] = [] |
| 61 | + |
| 62 | + def need(path: str): |
| 63 | + nonlocal errors |
| 64 | + node = cfg |
| 65 | + for k in path.split("."): |
| 66 | + if not isinstance(node, dict) or k not in node: |
| 67 | + errors.append(f"Missing required key: {path}") |
| 68 | + return None |
| 69 | + node = node[k] |
| 70 | + return node |
| 71 | + |
| 72 | + need("model.name") |
| 73 | + need("data.name") |
| 74 | + need("data.prompt_template") |
| 75 | + |
| 76 | + if need("training.batch_size_per_gpu") is not None: |
| 77 | + v = cfg["training"]["batch_size_per_gpu"] |
| 78 | + if not isinstance(v, int) or v <= 0: |
| 79 | + errors.append("training.batch_size_per_gpu must be a positive int") |
| 80 | + |
| 81 | + need("training.grad_accum_steps") |
| 82 | + need("training.max_steps") |
| 83 | + need("training.optimizer.name") |
| 84 | + need("training.optimizer.lr") |
| 85 | + |
| 86 | + need("checkpoint.save_interval") |
| 87 | + if need("checkpoint.output_dir") is None and need("checkpoint.dir") is None: |
| 88 | + warnings.append("checkpoint.output_dir is missing; will rely on SM_CHECKPOINT_DIR or checkpoint.dir if provided") |
| 89 | + |
| 90 | + model = cfg.get("model", {}) |
| 91 | + if model.get("load_in_4bit") and model.get("dtype"): |
| 92 | + warnings.append("model.load_in_4bit is set along with model.dtype; verify compatibility for the selected trainer") |
| 93 | + |
| 94 | + data = cfg.get("data", {}) |
| 95 | + if data.get("format") == "parquet" and data.get("streaming") is True: |
| 96 | + warnings.append("data.format=parquet with streaming=true may not be supported; verify dataset loader path") |
| 97 | + |
| 98 | + return errors, warnings |
| 99 | + |
| 100 | + |
| 101 | +def resolve_checkpoint_dir(cfg: Dict[str, Any], env: Dict[str, str] | None = None) -> str: |
| 102 | + e = env or os.environ |
| 103 | + sm_dir = e.get("SM_CHECKPOINT_DIR") |
| 104 | + if sm_dir: |
| 105 | + return sm_dir |
| 106 | + checkpoint = cfg.get("checkpoint", {}) |
| 107 | + if checkpoint.get("dir"): |
| 108 | + return str(checkpoint["dir"]) |
| 109 | + if checkpoint.get("output_dir"): |
| 110 | + return str(checkpoint["output_dir"]) |
| 111 | + return "./outputs" |
| 112 | + |
| 113 | + |
| 114 | +def load_config(path: str, env: Dict[str, str] | None = None) -> Tuple[Dict[str, Any], List[str], List[str]]: |
| 115 | + if yaml is None: |
| 116 | + raise RuntimeError("PyYAML is required to load config files") |
| 117 | + with open(path, "r") as f: |
| 118 | + raw = yaml.safe_load(f) or {} |
| 119 | + norm = normalize_config(raw) |
| 120 | + errors, warnings = validate_config(norm) |
| 121 | + if errors: |
| 122 | + raise ValueError("Config validation failed: " + "; ".join(errors)) |
| 123 | + _ = resolve_checkpoint_dir(norm, env) |
| 124 | + return norm, errors, warnings |
0 commit comments