|
3 | 3 | import transformers |
4 | 4 | from transformers import MixtralConfig |
5 | 5 |
|
| 6 | +from tests.kit.numerics.spec import NumericsSpec, Routers |
| 7 | + |
6 | 8 | from ..registry import ModelAttribute, model_zoo |
7 | 9 |
|
8 | 10 | # =============================== |
@@ -57,20 +59,31 @@ def data_gen_for_sequence_classification(): |
57 | 59 | # FlashAttention2. ShardFormer tests enable their optimized attention |
58 | 60 | # paths explicitly, so keep this baseline self-contained. |
59 | 61 | attn_implementation="eager", |
| 62 | + # The default grouped_mm experts kernel has no float64 path, which the |
| 63 | + # float64 ground truth of numerical alignment (tests/kit/numerics) needs. |
| 64 | + experts_implementation="eager", |
60 | 65 | torch_dtype="float16", |
61 | 66 | output_router_logits=True, |
62 | 67 | ) |
63 | 68 |
|
64 | 69 | if hasattr(config, "pad_token_id"): |
65 | 70 | config.pad_token_id = config.eos_token_id |
66 | 71 |
|
| 72 | + |
| 73 | +def is_router(name, module): |
| 74 | + # the gate of each sparse MoE block; its first output is the router logits |
| 75 | + return name.endswith("mlp.gate") |
| 76 | + |
| 77 | + |
| 78 | +numerics = NumericsSpec(routers=Routers(select=is_router, top_k=config.num_experts_per_tok)) |
| 79 | + |
67 | 80 | model_zoo.register( |
68 | 81 | name="transformers_mixtral", |
69 | 82 | model_fn=lambda: transformers.MixtralModel(config), |
70 | 83 | data_gen_fn=data_gen, |
71 | 84 | output_transform_fn=output_transform_fn, |
72 | 85 | loss_fn=loss_fn_for_mixtral_model, |
73 | | - model_attribute=ModelAttribute(has_control_flow=True), |
| 86 | + model_attribute=ModelAttribute(has_control_flow=True, numerics=numerics), |
74 | 87 | ) |
75 | 88 | # model_zoo.register( |
76 | 89 | # name="transformers_mixtral_for_casual_lm", |
|
0 commit comments