Keep per-layer metadata valid when scaling child models
Browse files- create_child_job.py +45 -3
create_child_job.py
CHANGED
|
@@ -112,8 +112,21 @@ def best_head_count(hidden_size: int, original: int) -> int:
|
|
| 112 |
return min(choices or [1], key=lambda value: abs(value - target))
|
| 113 |
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def scale_config_object(config: Any, ratio: float, overrides: dict[str, Any] | None = None) -> dict[str, Any]:
|
| 116 |
-
"""Scale common Transformer dimensions
|
| 117 |
overrides = overrides or {}
|
| 118 |
layer_scale = max(0.18, min(1.0, ratio ** 0.40))
|
| 119 |
hidden_scale = max(0.22, min(1.0, ratio ** 0.30))
|
|
@@ -122,17 +135,42 @@ def scale_config_object(config: Any, ratio: float, overrides: dict[str, Any] | N
|
|
| 122 |
hidden_keys = ["hidden_size", "d_model", "n_embd", "model_dim"]
|
| 123 |
ff_keys = ["intermediate_size", "d_ff", "ffn_dim", "encoder_ffn_dim", "decoder_ffn_dim"]
|
| 124 |
head_keys = ["num_attention_heads", "n_head", "encoder_attention_heads", "decoder_attention_heads"]
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
original_heads: dict[str, int] = {}
|
| 127 |
for key in head_keys:
|
| 128 |
value = getattr(config, key, None)
|
| 129 |
if isinstance(value, int) and value > 0:
|
| 130 |
original_heads[key] = value
|
| 131 |
|
|
|
|
| 132 |
for key in layer_keys:
|
| 133 |
value = getattr(config, key, None)
|
| 134 |
if isinstance(value, int) and value > 1:
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
hidden_value = None
|
| 138 |
for key in hidden_keys:
|
|
@@ -169,6 +207,10 @@ def scale_config_object(config: Any, ratio: float, overrides: dict[str, Any] | N
|
|
| 169 |
if hasattr(config, key):
|
| 170 |
setattr(config, key, value)
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
return config.to_dict() if hasattr(config, "to_dict") else {}
|
| 173 |
|
| 174 |
|
|
|
|
| 112 |
return min(choices or [1], key=lambda value: abs(value - target))
|
| 113 |
|
| 114 |
|
| 115 |
+
def resize_layer_sequence(values: list[Any], new_length: int) -> list[Any]:
|
| 116 |
+
"""Resize per-layer metadata while preserving its distribution and final layer."""
|
| 117 |
+
if new_length <= 0 or not values:
|
| 118 |
+
return []
|
| 119 |
+
if len(values) == new_length:
|
| 120 |
+
return list(values)
|
| 121 |
+
if new_length == 1:
|
| 122 |
+
return [values[-1]]
|
| 123 |
+
last = len(values) - 1
|
| 124 |
+
indices = [round(index * last / (new_length - 1)) for index in range(new_length)]
|
| 125 |
+
return [values[index] for index in indices]
|
| 126 |
+
|
| 127 |
+
|
| 128 |
def scale_config_object(config: Any, ratio: float, overrides: dict[str, Any] | None = None) -> dict[str, Any]:
|
| 129 |
+
"""Scale common Transformer dimensions and keep architecture-coupled fields valid."""
|
| 130 |
overrides = overrides or {}
|
| 131 |
layer_scale = max(0.18, min(1.0, ratio ** 0.40))
|
| 132 |
hidden_scale = max(0.22, min(1.0, ratio ** 0.30))
|
|
|
|
| 135 |
hidden_keys = ["hidden_size", "d_model", "n_embd", "model_dim"]
|
| 136 |
ff_keys = ["intermediate_size", "d_ff", "ffn_dim", "encoder_ffn_dim", "decoder_ffn_dim"]
|
| 137 |
head_keys = ["num_attention_heads", "n_head", "encoder_attention_heads", "decoder_attention_heads"]
|
| 138 |
+
per_layer_keys = ["layer_types", "mlp_layer_types", "block_types", "attention_types"]
|
| 139 |
+
|
| 140 |
+
original_layer_count = getattr(config, "num_hidden_layers", None)
|
| 141 |
+
per_layer_values = {
|
| 142 |
+
key: list(value)
|
| 143 |
+
for key in per_layer_keys
|
| 144 |
+
if isinstance((value := getattr(config, key, None)), (list, tuple))
|
| 145 |
+
}
|
| 146 |
original_heads: dict[str, int] = {}
|
| 147 |
for key in head_keys:
|
| 148 |
value = getattr(config, key, None)
|
| 149 |
if isinstance(value, int) and value > 0:
|
| 150 |
original_heads[key] = value
|
| 151 |
|
| 152 |
+
scaled_layer_counts: dict[str, int] = {}
|
| 153 |
for key in layer_keys:
|
| 154 |
value = getattr(config, key, None)
|
| 155 |
if isinstance(value, int) and value > 1:
|
| 156 |
+
scaled_layer_counts[key] = max(2, int(round(value * layer_scale)))
|
| 157 |
+
for key, value in scaled_layer_counts.items():
|
| 158 |
+
setattr(config, key, value)
|
| 159 |
+
|
| 160 |
+
new_layer_count = getattr(config, "num_hidden_layers", None)
|
| 161 |
+
if isinstance(new_layer_count, int) and new_layer_count > 0:
|
| 162 |
+
for key, values in per_layer_values.items():
|
| 163 |
+
setattr(config, key, resize_layer_sequence(values, new_layer_count))
|
| 164 |
+
max_window_layers = getattr(config, "max_window_layers", None)
|
| 165 |
+
if isinstance(max_window_layers, int):
|
| 166 |
+
setattr(config, "max_window_layers", min(max_window_layers, new_layer_count))
|
| 167 |
+
# Some hybrid architectures keep additional lists not known in advance.
|
| 168 |
+
if isinstance(original_layer_count, int) and original_layer_count > 0:
|
| 169 |
+
for key, value in list(getattr(config, "__dict__", {}).items()):
|
| 170 |
+
if key in per_layer_keys:
|
| 171 |
+
continue
|
| 172 |
+
if isinstance(value, list) and len(value) == original_layer_count and ("layer" in key or "block" in key or "attention" in key):
|
| 173 |
+
setattr(config, key, resize_layer_sequence(value, new_layer_count))
|
| 174 |
|
| 175 |
hidden_value = None
|
| 176 |
for key in hidden_keys:
|
|
|
|
| 207 |
if hasattr(config, key):
|
| 208 |
setattr(config, key, value)
|
| 209 |
|
| 210 |
+
# Re-run generic validators before publishing instead of discovering errors at load time.
|
| 211 |
+
validator = getattr(config, "validate_layer_type", None)
|
| 212 |
+
if callable(validator):
|
| 213 |
+
validator()
|
| 214 |
return config.to_dict() if hasattr(config, "to_dict") else {}
|
| 215 |
|
| 216 |
|