|
77 | 77 | AutoMapping, |
78 | 78 | ColumnParallelMapping, |
79 | 79 | GatedMLPMapping, |
80 | | - MegatronParamMapping, |
| 80 | + HCAlphaMapping, |
81 | 81 | ReplicatedMapping, |
82 | 82 | ) |
83 | 83 | from megatron.bridge.models.hf_pretrained.causal_lm import PreTrainedCausalLM |
@@ -243,93 +243,6 @@ def _dsv4_use_mxfp4_export(hf_param: str, weight: torch.Tensor, source_scale: to |
243 | 243 | # --------------------------------------------------------------------------- |
244 | 244 |
|
245 | 245 |
|
246 | | -class _HCAlphaMapping(MegatronParamMapping): |
247 | | - """Map Megatron's three scalar HC alpha parameters to/from the V4 checkpoint's |
248 | | - 3-element hc_*_scale tensor. |
249 | | -
|
250 | | - V4 checkpoint : layers.N.hc_attn_scale shape [3] = [alpha_pre, alpha_post, alpha_res] |
251 | | - Megatron : three separate nn.Parameter([1]) tensors |
252 | | - """ |
253 | | - |
254 | | - def __init__(self, megatron_pre: str, megatron_post: str, megatron_res: str, hf_param: str): |
255 | | - # We register under the alpha_pre path; the others are handled inside hf_to_megatron. |
256 | | - super().__init__(megatron_param=megatron_pre, hf_param=hf_param) |
257 | | - self._megatron_post = megatron_post |
258 | | - self._megatron_res = megatron_res |
259 | | - |
260 | | - @staticmethod |
261 | | - def _resolve_single(pattern: str, captures) -> str: |
262 | | - result = pattern |
263 | | - ci = 0 |
264 | | - while "**" in result and ci < len(captures): |
265 | | - result = result.replace("**", captures[ci], 1) |
266 | | - ci += 1 |
267 | | - ci = 0 |
268 | | - while "*" in result and ci < len(captures): |
269 | | - result = result.replace("*", captures[ci], 1) |
270 | | - ci += 1 |
271 | | - return result |
272 | | - |
273 | | - def resolve(self, captures): |
274 | | - resolved_mg, resolved_hf = self._resolve_names(captures) |
275 | | - resolved_post = self._resolve_single(self._megatron_post, captures) |
276 | | - resolved_res = self._resolve_single(self._megatron_res, captures) |
277 | | - return _HCAlphaMapping( |
278 | | - megatron_pre=resolved_mg, |
279 | | - megatron_post=resolved_post, |
280 | | - megatron_res=resolved_res, |
281 | | - hf_param=resolved_hf, |
282 | | - ) |
283 | | - |
284 | | - def hf_to_megatron(self, hf_weights, megatron_module): |
285 | | - # hf_weights is hc_*_scale [3]; we write alpha_pre here (index 0). |
286 | | - # alpha_post and alpha_res are handled by their own mappings when registered. |
287 | | - target = hf_weights.to(megatron_module.alpha_pre.device) |
288 | | - return target[0:1] |
289 | | - |
290 | | - def megatron_to_hf(self, megatron_weights, megatron_module): |
291 | | - # megatron_weights is alpha_pre [1]; gather all 3 from the same module. |
292 | | - # With PP > 1, megatron_module may be None on non-owning ranks, |
293 | | - # so we broadcast alpha_post and alpha_res alongside alpha_pre. |
294 | | - post_tensor = megatron_module.alpha_post.detach() if megatron_module is not None else None |
295 | | - res_tensor = megatron_module.alpha_res.detach() if megatron_module is not None else None |
296 | | - megatron_weights = self.broadcast_from_pp_rank(megatron_weights, cache_key=str(self.hf_param)) |
297 | | - post = self.broadcast_from_pp_rank(post_tensor, cache_key=str(self.hf_param) + "_post") |
298 | | - res = self.broadcast_from_pp_rank(res_tensor, cache_key=str(self.hf_param) + "_res") |
299 | | - if megatron_weights is None: |
300 | | - return {} |
301 | | - megatron_weights = self.maybe_dequantize(megatron_weights) |
302 | | - return {self.hf_param: torch.cat([megatron_weights.float(), post.float(), res.float()])} |
303 | | - |
304 | | - |
305 | | -class _HCAlphaSecondaryMapping(MegatronParamMapping): |
306 | | - """Secondary mapping for alpha_post (index=1) or alpha_res (index=2). |
307 | | -
|
308 | | - Import: extracts element [index] from the 3-element hc_*_scale tensor. |
309 | | - Export: returns {} because the primary _HCAlphaMapping (alpha_pre) already |
310 | | - exports all three alpha values together. This mapping just suppresses the |
311 | | - "No mapping found" warning for the secondary Megatron params during export. |
312 | | - """ |
313 | | - |
314 | | - def __init__(self, megatron_param: str, hf_scale_param: str, index: int): |
315 | | - super().__init__(megatron_param=megatron_param, hf_param=hf_scale_param) |
316 | | - self._index = index |
317 | | - self.allow_hf_name_mismatch = True # export is no-op; skip hf_keys check |
318 | | - |
319 | | - def hf_to_megatron(self, hf_weights, megatron_module): |
320 | | - attr = "alpha_post" if self._index == 1 else "alpha_res" |
321 | | - target = hf_weights.to(getattr(megatron_module, attr).device) |
322 | | - return target[self._index : self._index + 1] |
323 | | - |
324 | | - def resolve(self, captures): |
325 | | - resolved_mg, resolved_hf = self._resolve_names(captures) |
326 | | - return _HCAlphaSecondaryMapping(resolved_mg, resolved_hf, self._index) |
327 | | - |
328 | | - def megatron_to_hf(self, megatron_weights, megatron_module): |
329 | | - # Already handled by the primary alpha_pre _HCAlphaMapping |
330 | | - return {} |
331 | | - |
332 | | - |
333 | 246 | class _ReplicatedOptional(ReplicatedMapping): |
334 | 247 | """ReplicatedMapping for CSA-optional weights (compressor / indexer). |
335 | 248 |
|
@@ -808,41 +721,36 @@ def mapping_registry(self) -> MegatronMappingRegistry: # noqa: C901 |
808 | 721 | ), |
809 | 722 | ] |
810 | 723 |
|
811 | | - # HC alpha scalars need custom concatenation mapping (per-layer, both attn and ffn) |
| 724 | + # HC alpha scalars: one HCAlphaMapping per alpha (index 0 exports all three; |
| 725 | + # index 1/2 import only and suppress export "no mapping found" warnings). |
812 | 726 | # These are wildcarded across all layers. |
813 | 727 | mappings += [ |
814 | | - _HCAlphaMapping( |
815 | | - megatron_pre="decoder.layers.*.self_attention_hyper_connection.alpha_pre", |
816 | | - megatron_post="decoder.layers.*.self_attention_hyper_connection.alpha_post", |
817 | | - megatron_res="decoder.layers.*.self_attention_hyper_connection.alpha_res", |
818 | | - hf_param="layers.*.hc_attn_scale", |
819 | | - ), |
820 | | - _HCAlphaMapping( |
821 | | - megatron_pre="decoder.layers.*.mlp_hyper_connection.alpha_pre", |
822 | | - megatron_post="decoder.layers.*.mlp_hyper_connection.alpha_post", |
823 | | - megatron_res="decoder.layers.*.mlp_hyper_connection.alpha_res", |
824 | | - hf_param="layers.*.hc_ffn_scale", |
| 728 | + HCAlphaMapping( |
| 729 | + "decoder.layers.*.self_attention_hyper_connection.alpha_pre", |
| 730 | + "layers.*.hc_attn_scale", |
| 731 | + 0, |
825 | 732 | ), |
826 | | - ] |
827 | | - |
828 | | - # HC alpha secondary: register alpha_post and alpha_res to suppress export warnings |
829 | | - mappings += [ |
830 | | - _HCAlphaSecondaryMapping( |
| 733 | + HCAlphaMapping( |
831 | 734 | "decoder.layers.*.self_attention_hyper_connection.alpha_post", |
832 | 735 | "layers.*.hc_attn_scale", |
833 | 736 | 1, |
834 | 737 | ), |
835 | | - _HCAlphaSecondaryMapping( |
| 738 | + HCAlphaMapping( |
836 | 739 | "decoder.layers.*.self_attention_hyper_connection.alpha_res", |
837 | 740 | "layers.*.hc_attn_scale", |
838 | 741 | 2, |
839 | 742 | ), |
840 | | - _HCAlphaSecondaryMapping( |
| 743 | + HCAlphaMapping( |
| 744 | + "decoder.layers.*.mlp_hyper_connection.alpha_pre", |
| 745 | + "layers.*.hc_ffn_scale", |
| 746 | + 0, |
| 747 | + ), |
| 748 | + HCAlphaMapping( |
841 | 749 | "decoder.layers.*.mlp_hyper_connection.alpha_post", |
842 | 750 | "layers.*.hc_ffn_scale", |
843 | 751 | 1, |
844 | 752 | ), |
845 | | - _HCAlphaSecondaryMapping( |
| 753 | + HCAlphaMapping( |
846 | 754 | "decoder.layers.*.mlp_hyper_connection.alpha_res", |
847 | 755 | "layers.*.hc_ffn_scale", |
848 | 756 | 2, |
@@ -936,34 +844,23 @@ def mapping_registry(self) -> MegatronMappingRegistry: # noqa: C901 |
936 | 844 | ), |
937 | 845 | ] |
938 | 846 |
|
939 | | - # MTP HC alpha scalars |
940 | | - mappings += [ |
941 | | - _HCAlphaMapping( |
942 | | - megatron_pre=f"{mg_pfx}.mtp_model_layer.self_attention_hyper_connection.alpha_pre", |
943 | | - megatron_post=f"{mg_pfx}.mtp_model_layer.self_attention_hyper_connection.alpha_post", |
944 | | - megatron_res=f"{mg_pfx}.mtp_model_layer.self_attention_hyper_connection.alpha_res", |
945 | | - hf_param=f"{ck_pfx}.hc_attn_scale", |
946 | | - ), |
947 | | - _HCAlphaMapping( |
948 | | - megatron_pre=f"{mg_pfx}.mtp_model_layer.mlp_hyper_connection.alpha_pre", |
949 | | - megatron_post=f"{mg_pfx}.mtp_model_layer.mlp_hyper_connection.alpha_post", |
950 | | - megatron_res=f"{mg_pfx}.mtp_model_layer.mlp_hyper_connection.alpha_res", |
951 | | - hf_param=f"{ck_pfx}.hc_ffn_scale", |
952 | | - ), |
953 | | - ] |
954 | | - |
955 | | - # MTP HC alpha secondary: suppress export warnings for post/res |
| 847 | + # MTP HC alpha scalars (one HCAlphaMapping per alpha; index 0 exports all three) |
956 | 848 | for _hc_mg_sub, _hc_hf_key in [ |
957 | 849 | ("self_attention_hyper_connection", "hc_attn_scale"), |
958 | 850 | ("mlp_hyper_connection", "hc_ffn_scale"), |
959 | 851 | ]: |
960 | 852 | mappings += [ |
961 | | - _HCAlphaSecondaryMapping( |
| 853 | + HCAlphaMapping( |
| 854 | + f"{mg_pfx}.mtp_model_layer.{_hc_mg_sub}.alpha_pre", |
| 855 | + f"{ck_pfx}.{_hc_hf_key}", |
| 856 | + 0, |
| 857 | + ), |
| 858 | + HCAlphaMapping( |
962 | 859 | f"{mg_pfx}.mtp_model_layer.{_hc_mg_sub}.alpha_post", |
963 | 860 | f"{ck_pfx}.{_hc_hf_key}", |
964 | 861 | 1, |
965 | 862 | ), |
966 | | - _HCAlphaSecondaryMapping( |
| 863 | + HCAlphaMapping( |
967 | 864 | f"{mg_pfx}.mtp_model_layer.{_hc_mg_sub}.alpha_res", |
968 | 865 | f"{ck_pfx}.{_hc_hf_key}", |
969 | 866 | 2, |
|
0 commit comments