|
42 | 42 | # Conditionally import paddlefleet modules |
43 | 43 | if is_paddlefleet_available(): |
44 | 44 | from paddlefleet.models.gpt import GPTModel |
| 45 | + from paddlefleet.transformer.moe.moe_expert import SonicMoEExpert |
45 | 46 | from paddlefleet.transformer.moe.moe_layer import MoELayer |
46 | 47 | from paddlefleet.transformer.moe.moe_router import StandardMoERouter |
47 | 48 | else: |
48 | 49 |
|
49 | 50 | class GPTModel: |
50 | 51 | pass |
51 | 52 |
|
| 53 | + class SonicMoEExpert: |
| 54 | + pass |
| 55 | + |
52 | 56 | class MoELayer: |
53 | 57 | pass |
54 | 58 |
|
@@ -80,6 +84,7 @@ class StandardMoERouter: |
80 | 84 | "MoEGateSpGradSyncCallBack", |
81 | 85 | "SPGradSyncCallback", |
82 | 86 | "EMAStateAssemblerCallback", |
| 87 | + "SonicMoELayoutSwitchCallback", |
83 | 88 | ] |
84 | 89 |
|
85 | 90 |
|
@@ -715,6 +720,9 @@ def on_step_begin(self, args, state, control, **kwargs): |
715 | 720 | """ |
716 | 721 | Quantize expert weights to FP8 before each training step |
717 | 722 | """ |
| 723 | + if args.using_sonic_moe: |
| 724 | + # sonicmoe cannot support offline quant now. |
| 725 | + return |
718 | 726 | model = kwargs["model"] |
719 | 727 | optimizer = kwargs["optimizer"] |
720 | 728 | global skip_count |
@@ -754,6 +762,9 @@ def on_optimizer_begin(self, args, state, control, **kwargs): |
754 | 762 | """ |
755 | 763 | Reload weights before optimizer step |
756 | 764 | """ |
| 765 | + if args.using_sonic_moe: |
| 766 | + # sonicmoe cannot support offline quant now. |
| 767 | + return |
757 | 768 | model = kwargs["model"] |
758 | 769 | optimizer = kwargs["optimizer"] |
759 | 770 | global skip_count |
@@ -932,6 +943,32 @@ def on_step_end(self, args, state, control, **kwargs): |
932 | 943 | logger.info(f"[EMAStateAssembler] Assembling EMA state took {duration:.3f} seconds.") |
933 | 944 |
|
934 | 945 |
|
| 946 | +class SonicMoELayoutSwitchCallback(TrainerCallback): |
| 947 | + def _apply_to_sonic_moe_experts(self, model, fn_name): |
| 948 | + def apply_layout_switch(layer): |
| 949 | + if isinstance(layer, SonicMoEExpert): |
| 950 | + getattr(layer, fn_name)() |
| 951 | + |
| 952 | + model.apply(apply_layout_switch) |
| 953 | + |
| 954 | + def _prepare_sonic_moe_fp8_weights(self, model): |
| 955 | + def prepare_fp8_weights(layer): |
| 956 | + if isinstance(layer, SonicMoEExpert): |
| 957 | + layer.convert_weights_to_sonic_layout() |
| 958 | + layer.quant_weight() |
| 959 | + |
| 960 | + model.apply(prepare_fp8_weights) |
| 961 | + |
| 962 | + def on_step_begin(self, args, state, control, **kwargs): |
| 963 | + if args.using_sonic_moe: |
| 964 | + self._prepare_sonic_moe_fp8_weights(kwargs["model"]) |
| 965 | + # kwargs["optimizer"].clear_param_storage("moe_expert") |
| 966 | + |
| 967 | + def on_optimizer_begin(self, args, state, control, **kwargs): |
| 968 | + if args.using_sonic_moe: |
| 969 | + self._apply_to_sonic_moe_experts(kwargs["model"], "convert_weights_to_grouped_layout") |
| 970 | + |
| 971 | + |
935 | 972 | class InterleaveGateUpCallback(TrainerCallback): |
936 | 973 | def __init__(self, model, resume_from_checkpoint=None, output_dir=None): |
937 | 974 | self.model = model |
|
0 commit comments