Skip to content

Commit 69c9ff5

Browse files
authored
Merge pull request #6505 from hpcaitech/feat/numerics-model-alignment
[test] numerical alignment: whole models through booster plugins, data-parallel and resume equivalence, acceptance report
2 parents d414d56 + c7cadd8 commit 69c9ff5

29 files changed

Lines changed: 4359 additions & 100 deletions

‎.github/scripts/ci/run_colossalai_batch.sh‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ case "${BATCH}" in
7676
tests/test_config/test_load_config.py
7777
tests/test_infer/test_async_engine/test_request_tracer.py
7878
tests/test_lazy/test_chatglm_cpu.py
79+
tests/test_numerics/test_numerics_acceptance.py
7980
tests/test_numerics/test_numerics_core.py
81+
tests/test_numerics/test_numerics_data_parallel.py
82+
tests/test_numerics/test_numerics_independent_impl.py
83+
tests/test_numerics/test_numerics_resume.py
8084
tests/test_optimizer/test_lr_scheduler.py
8185
tests/test_pipeline/test_pipeline_utils/test_t5_pipeline_utils.py
8286
tests/test_pipeline/test_pipeline_utils/test_whisper_pipeline_utils.py
@@ -166,7 +170,7 @@ case "${BATCH}" in
166170
;;
167171
5|05)
168172
LABEL="05-booster-lora-2gpu"
169-
DESCRIPTION="DDP, FSDP, LowLevelZeRO and LoRA"
173+
DESCRIPTION="DDP, FSDP, LowLevelZeRO, LoRA and the numerical alignment tool on its plugins"
170174
EXPECTED_GPUS=2
171175
DEFAULT_TIMEOUT_MIN=90
172176
NEEDS_CUDA_TOOLKIT=1
@@ -176,6 +180,7 @@ case "${BATCH}" in
176180
tests/test_booster/test_plugin/test_torch_fsdp_plugin.py
177181
tests/test_booster/test_plugin/test_low_level_zero_plugin.py
178182
tests/test_lora/test_lora.py
183+
tests/test_numerics
179184
)
180185
;;
181186
6|06)

‎.github/scripts/ci/run_cpu_unit_tests.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ TESTS=(
1414
tests/test_config/test_load_config.py
1515
tests/test_infer/test_async_engine/test_request_tracer.py
1616
tests/test_lazy/test_chatglm_cpu.py
17+
tests/test_numerics/test_numerics_acceptance.py
1718
tests/test_numerics/test_numerics_core.py
19+
tests/test_numerics/test_numerics_data_parallel.py
20+
tests/test_numerics/test_numerics_independent_impl.py
21+
tests/test_numerics/test_numerics_resume.py
1822
tests/test_optimizer/test_lr_scheduler.py
1923
tests/test_pipeline/test_pipeline_utils/test_t5_pipeline_utils.py
2024
tests/test_pipeline/test_pipeline_utils/test_whisper_pipeline_utils.py

‎tests/kit/model_zoo/registry.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
from dataclasses import dataclass
3-
from typing import Callable, List, Union
3+
from typing import Callable, List, Optional, Union
4+
5+
from tests.kit.numerics.spec import NumericsSpec
46

57
__all__ = ["ModelZooRegistry", "ModelAttribute", "model_zoo"]
68

@@ -13,10 +15,13 @@ class ModelAttribute:
1315
Args:
1416
has_control_flow (bool): Whether the model contains branching in its forward method.
1517
has_stochastic_depth_prob (bool): Whether the model contains stochastic depth probability. Often seen in the torchvision models.
18+
numerics (NumericsSpec): How to check the model against its reference implementation. Models without one are
19+
not part of whole-model alignment. See ``tests/kit/numerics/spec.py``.
1620
"""
1721

1822
has_control_flow: bool = False
1923
has_stochastic_depth_prob: bool = False
24+
numerics: Optional[NumericsSpec] = None
2025

2126

2227
class ModelZooRegistry(dict):

‎tests/kit/model_zoo/transformers/mixtral.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import transformers
44
from transformers import MixtralConfig
55

6+
from tests.kit.numerics.spec import NumericsSpec, Routers
7+
68
from ..registry import ModelAttribute, model_zoo
79

810
# ===============================
@@ -57,20 +59,31 @@ def data_gen_for_sequence_classification():
5759
# FlashAttention2. ShardFormer tests enable their optimized attention
5860
# paths explicitly, so keep this baseline self-contained.
5961
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",
6065
torch_dtype="float16",
6166
output_router_logits=True,
6267
)
6368

6469
if hasattr(config, "pad_token_id"):
6570
config.pad_token_id = config.eos_token_id
6671

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+
6780
model_zoo.register(
6881
name="transformers_mixtral",
6982
model_fn=lambda: transformers.MixtralModel(config),
7083
data_gen_fn=data_gen,
7184
output_transform_fn=output_transform_fn,
7285
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),
7487
)
7588
# model_zoo.register(
7689
# name="transformers_mixtral_for_casual_lm",

‎tests/kit/numerics/README.md‎

Lines changed: 486 additions & 10 deletions
Large diffs are not rendered by default.

‎tests/kit/numerics/__init__.py‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
1-
from .capture import cast_floating, full_precision_matmul, run_fwd_bwd
1+
from .capture import (
2+
RoutingRecorder,
3+
cast_floating,
4+
count_calls,
5+
full_precision_matmul,
6+
missing_grads,
7+
run_fwd_bwd,
8+
run_optimizer,
9+
upstream_loss,
10+
)
211
from .compare import TensorMetrics, flatten_tensors, tensor_metrics
3-
from .judge import AlignmentReport, Entry, assert_aligned, error_multiplier, judge
12+
from .judge import (
13+
AlignmentReport,
14+
Entry,
15+
assert_aligned,
16+
diagnose_routing,
17+
error_multiplier,
18+
judge,
19+
judge_equal,
20+
judge_relative_bound,
21+
machine_epsilon,
22+
relative_bound,
23+
require_gradients,
24+
)
25+
from .spec import NumericsSpec, Routers, TokenLoss
426
from .trace import Trace
527

628
__all__ = [
729
"AlignmentReport",
830
"Entry",
31+
"NumericsSpec",
32+
"Routers",
33+
"RoutingRecorder",
934
"TensorMetrics",
35+
"TokenLoss",
1036
"Trace",
1137
"assert_aligned",
1238
"cast_floating",
39+
"count_calls",
40+
"diagnose_routing",
1341
"error_multiplier",
1442
"flatten_tensors",
1543
"full_precision_matmul",
1644
"judge",
45+
"judge_equal",
46+
"judge_relative_bound",
47+
"machine_epsilon",
48+
"missing_grads",
49+
"relative_bound",
50+
"require_gradients",
1751
"run_fwd_bwd",
52+
"run_optimizer",
1853
"tensor_metrics",
54+
"upstream_loss",
1955
]

0 commit comments

Comments
 (0)