|
76 | 76 | AutoMapping, |
77 | 77 | ColumnParallelMapping, |
78 | 78 | GatedMLPMapping, |
79 | | - MegatronParamMapping, |
| 79 | + HCAlphaMapping, |
80 | 80 | ReplicatedMapping, |
81 | 81 | ) |
82 | 82 | from megatron.bridge.models.hf_pretrained.causal_lm import PreTrainedCausalLM |
@@ -236,93 +236,6 @@ def _dsv4_use_mxfp4_export(hf_param: str, weight: torch.Tensor, source_scale: to |
236 | 236 | # --------------------------------------------------------------------------- |
237 | 237 |
|
238 | 238 |
|
239 | | -class _HCAlphaMapping(MegatronParamMapping): |
240 | | - """Map Megatron's three scalar HC alpha parameters to/from the V4 checkpoint's |
241 | | - 3-element hc_*_scale tensor. |
242 | | -
|
243 | | - V4 checkpoint : layers.N.hc_attn_scale shape [3] = [alpha_pre, alpha_post, alpha_res] |
244 | | - Megatron : three separate nn.Parameter([1]) tensors |
245 | | - """ |
246 | | - |
247 | | - def __init__(self, megatron_pre: str, megatron_post: str, megatron_res: str, hf_param: str): |
248 | | - # We register under the alpha_pre path; the others are handled inside hf_to_megatron. |
249 | | - super().__init__(megatron_param=megatron_pre, hf_param=hf_param) |
250 | | - self._megatron_post = megatron_post |
251 | | - self._megatron_res = megatron_res |
252 | | - |
253 | | - @staticmethod |
254 | | - def _resolve_single(pattern: str, captures) -> str: |
255 | | - result = pattern |
256 | | - ci = 0 |
257 | | - while "**" in result and ci < len(captures): |
258 | | - result = result.replace("**", captures[ci], 1) |
259 | | - ci += 1 |
260 | | - ci = 0 |
261 | | - while "*" in result and ci < len(captures): |
262 | | - result = result.replace("*", captures[ci], 1) |
263 | | - ci += 1 |
264 | | - return result |
265 | | - |
266 | | - def resolve(self, captures): |
267 | | - resolved_mg, resolved_hf = self._resolve_names(captures) |
268 | | - resolved_post = self._resolve_single(self._megatron_post, captures) |
269 | | - resolved_res = self._resolve_single(self._megatron_res, captures) |
270 | | - return _HCAlphaMapping( |
271 | | - megatron_pre=resolved_mg, |
272 | | - megatron_post=resolved_post, |
273 | | - megatron_res=resolved_res, |
274 | | - hf_param=resolved_hf, |
275 | | - ) |
276 | | - |
277 | | - def hf_to_megatron(self, hf_weights, megatron_module): |
278 | | - # hf_weights is hc_*_scale [3]; we write alpha_pre here (index 0). |
279 | | - # alpha_post and alpha_res are handled by their own mappings when registered. |
280 | | - target = hf_weights.to(megatron_module.alpha_pre.device) |
281 | | - return target[0:1] |
282 | | - |
283 | | - def megatron_to_hf(self, megatron_weights, megatron_module): |
284 | | - # megatron_weights is alpha_pre [1]; gather all 3 from the same module. |
285 | | - # With PP > 1, megatron_module may be None on non-owning ranks, |
286 | | - # so we broadcast alpha_post and alpha_res alongside alpha_pre. |
287 | | - post_tensor = megatron_module.alpha_post.detach() if megatron_module is not None else None |
288 | | - res_tensor = megatron_module.alpha_res.detach() if megatron_module is not None else None |
289 | | - megatron_weights = self.broadcast_from_pp_rank(megatron_weights, cache_key=str(self.hf_param)) |
290 | | - post = self.broadcast_from_pp_rank(post_tensor, cache_key=str(self.hf_param) + "_post") |
291 | | - res = self.broadcast_from_pp_rank(res_tensor, cache_key=str(self.hf_param) + "_res") |
292 | | - if megatron_weights is None: |
293 | | - return {} |
294 | | - megatron_weights = self.maybe_dequantize(megatron_weights) |
295 | | - return {self.hf_param: torch.cat([megatron_weights.float(), post.float(), res.float()])} |
296 | | - |
297 | | - |
298 | | -class _HCAlphaSecondaryMapping(MegatronParamMapping): |
299 | | - """Secondary mapping for alpha_post (index=1) or alpha_res (index=2). |
300 | | -
|
301 | | - Import: extracts element [index] from the 3-element hc_*_scale tensor. |
302 | | - Export: returns {} because the primary _HCAlphaMapping (alpha_pre) already |
303 | | - exports all three alpha values together. This mapping just suppresses the |
304 | | - "No mapping found" warning for the secondary Megatron params during export. |
305 | | - """ |
306 | | - |
307 | | - def __init__(self, megatron_param: str, hf_scale_param: str, index: int): |
308 | | - super().__init__(megatron_param=megatron_param, hf_param=hf_scale_param) |
309 | | - self._index = index |
310 | | - self.allow_hf_name_mismatch = True # export is no-op; skip hf_keys check |
311 | | - |
312 | | - def hf_to_megatron(self, hf_weights, megatron_module): |
313 | | - attr = "alpha_post" if self._index == 1 else "alpha_res" |
314 | | - target = hf_weights.to(getattr(megatron_module, attr).device) |
315 | | - return target[self._index : self._index + 1] |
316 | | - |
317 | | - def resolve(self, captures): |
318 | | - resolved_mg, resolved_hf = self._resolve_names(captures) |
319 | | - return _HCAlphaSecondaryMapping(resolved_mg, resolved_hf, self._index) |
320 | | - |
321 | | - def megatron_to_hf(self, megatron_weights, megatron_module): |
322 | | - # Already handled by the primary alpha_pre _HCAlphaMapping |
323 | | - return {} |
324 | | - |
325 | | - |
326 | 239 | class _ReplicatedOptional(ReplicatedMapping): |
327 | 240 | """ReplicatedMapping for CSA-optional weights (compressor / indexer). |
328 | 241 |
|
@@ -768,41 +681,36 @@ def mapping_registry(self) -> MegatronMappingRegistry: # noqa: C901 |
768 | 681 | ), |
769 | 682 | ] |
770 | 683 |
|
771 | | - # HC alpha scalars need custom concatenation mapping (per-layer, both attn and ffn) |
| 684 | + # HC alpha scalars: one HCAlphaMapping per alpha (index 0 exports all three; |
| 685 | + # index 1/2 import only and suppress export "no mapping found" warnings). |
772 | 686 | # These are wildcarded across all layers. |
773 | 687 | mappings += [ |
774 | | - _HCAlphaMapping( |
775 | | - megatron_pre="decoder.layers.*.self_attention_hyper_connection.alpha_pre", |
776 | | - megatron_post="decoder.layers.*.self_attention_hyper_connection.alpha_post", |
777 | | - megatron_res="decoder.layers.*.self_attention_hyper_connection.alpha_res", |
778 | | - hf_param="layers.*.hc_attn_scale", |
779 | | - ), |
780 | | - _HCAlphaMapping( |
781 | | - megatron_pre="decoder.layers.*.mlp_hyper_connection.alpha_pre", |
782 | | - megatron_post="decoder.layers.*.mlp_hyper_connection.alpha_post", |
783 | | - megatron_res="decoder.layers.*.mlp_hyper_connection.alpha_res", |
784 | | - hf_param="layers.*.hc_ffn_scale", |
| 688 | + HCAlphaMapping( |
| 689 | + "decoder.layers.*.self_attention_hyper_connection.alpha_pre", |
| 690 | + "layers.*.hc_attn_scale", |
| 691 | + 0, |
785 | 692 | ), |
786 | | - ] |
787 | | - |
788 | | - # HC alpha secondary: register alpha_post and alpha_res to suppress export warnings |
789 | | - mappings += [ |
790 | | - _HCAlphaSecondaryMapping( |
| 693 | + HCAlphaMapping( |
791 | 694 | "decoder.layers.*.self_attention_hyper_connection.alpha_post", |
792 | 695 | "layers.*.hc_attn_scale", |
793 | 696 | 1, |
794 | 697 | ), |
795 | | - _HCAlphaSecondaryMapping( |
| 698 | + HCAlphaMapping( |
796 | 699 | "decoder.layers.*.self_attention_hyper_connection.alpha_res", |
797 | 700 | "layers.*.hc_attn_scale", |
798 | 701 | 2, |
799 | 702 | ), |
800 | | - _HCAlphaSecondaryMapping( |
| 703 | + HCAlphaMapping( |
| 704 | + "decoder.layers.*.mlp_hyper_connection.alpha_pre", |
| 705 | + "layers.*.hc_ffn_scale", |
| 706 | + 0, |
| 707 | + ), |
| 708 | + HCAlphaMapping( |
801 | 709 | "decoder.layers.*.mlp_hyper_connection.alpha_post", |
802 | 710 | "layers.*.hc_ffn_scale", |
803 | 711 | 1, |
804 | 712 | ), |
805 | | - _HCAlphaSecondaryMapping( |
| 713 | + HCAlphaMapping( |
806 | 714 | "decoder.layers.*.mlp_hyper_connection.alpha_res", |
807 | 715 | "layers.*.hc_ffn_scale", |
808 | 716 | 2, |
@@ -896,34 +804,23 @@ def mapping_registry(self) -> MegatronMappingRegistry: # noqa: C901 |
896 | 804 | ), |
897 | 805 | ] |
898 | 806 |
|
899 | | - # MTP HC alpha scalars |
900 | | - mappings += [ |
901 | | - _HCAlphaMapping( |
902 | | - megatron_pre=f"{mg_pfx}.mtp_model_layer.self_attention_hyper_connection.alpha_pre", |
903 | | - megatron_post=f"{mg_pfx}.mtp_model_layer.self_attention_hyper_connection.alpha_post", |
904 | | - megatron_res=f"{mg_pfx}.mtp_model_layer.self_attention_hyper_connection.alpha_res", |
905 | | - hf_param=f"{ck_pfx}.hc_attn_scale", |
906 | | - ), |
907 | | - _HCAlphaMapping( |
908 | | - megatron_pre=f"{mg_pfx}.mtp_model_layer.mlp_hyper_connection.alpha_pre", |
909 | | - megatron_post=f"{mg_pfx}.mtp_model_layer.mlp_hyper_connection.alpha_post", |
910 | | - megatron_res=f"{mg_pfx}.mtp_model_layer.mlp_hyper_connection.alpha_res", |
911 | | - hf_param=f"{ck_pfx}.hc_ffn_scale", |
912 | | - ), |
913 | | - ] |
914 | | - |
915 | | - # MTP HC alpha secondary: suppress export warnings for post/res |
| 807 | + # MTP HC alpha scalars (one HCAlphaMapping per alpha; index 0 exports all three) |
916 | 808 | for _hc_mg_sub, _hc_hf_key in [ |
917 | 809 | ("self_attention_hyper_connection", "hc_attn_scale"), |
918 | 810 | ("mlp_hyper_connection", "hc_ffn_scale"), |
919 | 811 | ]: |
920 | 812 | mappings += [ |
921 | | - _HCAlphaSecondaryMapping( |
| 813 | + HCAlphaMapping( |
| 814 | + f"{mg_pfx}.mtp_model_layer.{_hc_mg_sub}.alpha_pre", |
| 815 | + f"{ck_pfx}.{_hc_hf_key}", |
| 816 | + 0, |
| 817 | + ), |
| 818 | + HCAlphaMapping( |
922 | 819 | f"{mg_pfx}.mtp_model_layer.{_hc_mg_sub}.alpha_post", |
923 | 820 | f"{ck_pfx}.{_hc_hf_key}", |
924 | 821 | 1, |
925 | 822 | ), |
926 | | - _HCAlphaSecondaryMapping( |
| 823 | + HCAlphaMapping( |
927 | 824 | f"{mg_pfx}.mtp_model_layer.{_hc_mg_sub}.alpha_res", |
928 | 825 | f"{ck_pfx}.{_hc_hf_key}", |
929 | 826 | 2, |
|
0 commit comments