Merged
fix: restore SDC replay config validation after migration #1213
jiangna1111Add on 27 days ago
fix: restore SDC replay config validation after migration #1213
Merged
Total: 2 files changed+48-2
| @@ -11,9 +11,10 @@ from dataclasses import dataclass, fields | |||
| 11 | import pytest | 11 | import pytest |
| 12 | from torchtitan.components.checkpointer import CheckpointManager | 12 | from torchtitan.components.checkpointer import CheckpointManager |
| 13 | from torchtitan.components.optim import AdamW, Optim, OptimizersContainer | 13 | from torchtitan.components.optim import AdamW, Optim, OptimizersContainer |
| 14 | -from torchtitan.config import ConfigManager | 14 | +from torchtitan.config import ConfigManager, DebugConfig |
| 15 | from torchtitan.config import TrainingConfig as UpstreamTrainingConfig | 15 | from torchtitan.config import TrainingConfig as UpstreamTrainingConfig |
| 16 | from torchtitan.models.llama3 import model_registry | 16 | from torchtitan.models.llama3 import model_registry |
| 17 | +from torchtitan.observability.sdc_replayer import SDCReplayer | ||
| 17 | from torchtitan.trainer import Trainer | 18 | from torchtitan.trainer import Trainer |
| 18 | 19 | ||
| 19 | import torchtitan_npu.config as npu_config | 20 | import torchtitan_npu.config as npu_config |
| @@ -693,6 +694,49 @@ def test_config_manager_preserves_recipe_optimizer_type_and_fields(monkeypatch, | |||
| 693 | assert config.optim.optimizer._owner is RecipeOptimizer | 694 | assert config.optim.optimizer._owner is RecipeOptimizer |
| 694 | 695 | ||
| 695 | 696 | ||
| 697 | +@pytest.mark.parametrize("detector", ["typed", "native"]) | ||
| 698 | +def test_config_manager_rejects_sdc_detection_with_replay(monkeypatch, tmp_path, detector): | ||
| 699 | + monkeypatch.setenv("NPU_ASD_CONFIG", "enable:false") | ||
| 700 | + monkeypatch.setenv("NPU_ASD_ENABLE", "1" if detector == "native" else "0") | ||
| 701 | + source = Trainer.Config( | ||
| 702 | + model=_debug_model(), | ||
| 703 | + hf_assets_path=str(tmp_path), | ||
| 704 | + debug=DebugConfig(deterministic=True), | ||
| 705 | + sdc_replayer=SDCReplayer.Config(), | ||
| 706 | + ) | ||
| 707 | + module_name = "_torchtitan_npu_sdc_replay_registry" | ||
| 708 | + _install_config_registry(monkeypatch, module_name, lambda: source) | ||
| 709 | + args = ["--module", module_name, "--config", "test_config"] | ||
| 710 | + if detector == "typed": | ||
| 711 | + args.append("--sdc.gradient-enabled") | ||
| 712 | + | ||
| 713 | + diagnostic = "config.sdc detection" if detector == "typed" else "SDC replay requires native SDC disabled" | ||
| 714 | + with pytest.raises(ValueError, match=diagnostic): | ||
| 715 | + ConfigManager().parse_args(args) | ||
| 716 | + | ||
| 717 | + | ||
| 718 | +@pytest.mark.parametrize("replay_enabled", [False, True], ids=("detector-only", "replay-only")) | ||
| 719 | +def test_config_manager_accepts_sdc_detection_or_replay_alone(monkeypatch, tmp_path, replay_enabled): | ||
| 720 | + monkeypatch.setenv("NPU_ASD_CONFIG", "enable:false") | ||
| 721 | + monkeypatch.setenv("NPU_ASD_ENABLE", "0") | ||
| 722 | + source = Trainer.Config( | ||
| 723 | + model=_debug_model(), | ||
| 724 | + hf_assets_path=str(tmp_path), | ||
| 725 | + debug=DebugConfig(deterministic=True), | ||
| 726 | + sdc_replayer=SDCReplayer.Config() if replay_enabled else None, | ||
| 727 | + ) | ||
| 728 | + module_name = "_torchtitan_npu_sdc_independent_registry" | ||
| 729 | + _install_config_registry(monkeypatch, module_name, lambda: source) | ||
| 730 | + args = ["--module", module_name, "--config", "test_config"] | ||
| 731 | + if not replay_enabled: | ||
| 732 | + args.append("--sdc.gradient-enabled") | ||
| 733 | + | ||
| 734 | + config = ConfigManager().parse_args(args) | ||
| 735 | + | ||
| 736 | + assert (config.sdc_replayer is not None) is replay_enabled | ||
| 737 | + assert config.sdc.gradient_enabled is not replay_enabled | ||
| 738 | + | ||
| 739 | + | ||
| 696 | def test_config_manager_overrides_recipe_owned_optional_components(monkeypatch, tmp_path): | 740 | def test_config_manager_overrides_recipe_owned_optional_components(monkeypatch, tmp_path): |
| 697 | from torchtitan_npu.config import CompileConfig, ModelCompileConfig | 741 | from torchtitan_npu.config import CompileConfig, ModelCompileConfig |
| 698 | from torchtitan_npu.extensions.torchtitan.components.checkpoint import CheckpointManager as NPUCheckpointManager | 742 | from torchtitan_npu.extensions.torchtitan.components.checkpoint import CheckpointManager as NPUCheckpointManager |
| @@ -29,7 +29,7 @@ from torchtitan_npu.config.converters import TrainerConfigConverter | |||
| 29 | from torchtitan_npu.extensions.torchtitan.components.checkpoint import CheckpointManager | 29 | from torchtitan_npu.extensions.torchtitan.components.checkpoint import CheckpointManager |
| 30 | from torchtitan_npu.extensions.torchtitan.components.optimizer.ema import EMA | 30 | from torchtitan_npu.extensions.torchtitan.components.optimizer.ema import EMA |
| 31 | from torchtitan_npu.extensions.components.gradient_clipping import GradientClippingOptim # noqa: F401 | 31 | from torchtitan_npu.extensions.components.gradient_clipping import GradientClippingOptim # noqa: F401 |
| 32 | -from torchtitan_npu.extensions.components.sdc import SDC | 32 | +from torchtitan_npu.extensions.components.sdc import SDC, validate_sdc_replay_config |
| 33 | from torchtitan_npu.extensions.experiment.anticipatory_routing.config import ( | 33 | from torchtitan_npu.extensions.experiment.anticipatory_routing.config import ( |
| 34 | AnticipatoryRoutingConfig, | 34 | AnticipatoryRoutingConfig, |
| 35 | validate_anticipatory_config, | 35 | validate_anticipatory_config, |
| @@ -115,6 +115,8 @@ class TrainerEx(Trainer): | |||
| 115 | ) | 115 | ) |
| 116 | self._post_init_optimizer() | 116 | self._post_init_optimizer() |
| 117 | validate_anticipatory_config(self) | 117 | validate_anticipatory_config(self) |
| 118 | + if self.sdc_replayer is not None: | ||
| 119 | + validate_sdc_replay_config(self.sdc) | ||
| 118 | 120 | ||
| 119 | def _post_init_optimizer(self) -> None: | 121 | def _post_init_optimizer(self) -> None: |
| 120 | # Overrides may already have materialized and replaced the CLI schema. | 122 | # Overrides may already have materialized and replaced the CLI schema. |


添加config校验,防止后续升级再次漏掉这次校验