Repository navigation
[compile] Move local compile regions to the model config - #5026
Conversation
| "gated_rmsnorm", | ||
| "loss", | ||
| "swiglu", | ||
| "situglu", |
There was a problem hiding this comment.
re #5008 (comment):
- I forgot why it was under
common/, it probably shouldn't be - regardless, this is only used by Kimi K3, so should go to per-model list
| # Regions defined in code every model imports (models/common, components/loss.py). | ||
| # A region defined in one model's files (e.g. Qwen3.5's offset_rmsnorm) goes in that model's Config default. | ||
| DEFAULT_LOCAL_COMPILE_REGIONS = ( | ||
| "gated_rmsnorm", |
| "situglu", | ||
| "cos_sin_rope", | ||
| ] | ||
| default_factory=lambda: list(DEFAULT_LOCAL_COMPILE_REGIONS) |
There was a problem hiding this comment.
Now that we have this model specific deafult, I don't really think we should set any default default.
E.g. Kimi K3 doesn't have RoPE or swiglu, etc.
| None # noqa: this sets Config.param_init, not Module._param_init | ||
| ) | ||
| self.sharding_config = None | ||
| # HF modules ignore local compile regions; only the loss region applies. |
There was a problem hiding this comment.
related: maybe set ["loss"] explicit for HF
| training=config.training, | ||
| parallelism=config.parallelism, | ||
| compile_config=compile_config, | ||
| compile_config=self._compile_config(), |
There was a problem hiding this comment.
let's not provide hooks for alternative trainers in experiments
- from what I see https://github.com/pytorch/torchtitan/blob/main/torchtitan/experiments/graph_trainer/llama3/parallelize_autoparallel.py#L157-L164 can be moved out of
model.parallelizeinto graph trainer / its training engine?
| Subclasses define model-specific hyperparameters. | ||
| """ | ||
|
|
||
| local_compile: LocalCompileConfig = field(default_factory=LocalCompileConfig) |
There was a problem hiding this comment.
any reason we can't make it local_compile_regions: list[str]?
| training=config.training, | ||
| parallelism=config.parallelism, | ||
| compile_config=compile_config, | ||
| compile_config=self._compile_config(), |
There was a problem hiding this comment.
Instead of passing the compile config to model.parallelize, I think we should put compile application into model constructor.
|
|
||
| # Regions defined in code every model imports (models/common, components/loss.py). | ||
| # A region defined in one model's files (e.g. Qwen3.5's offset_rmsnorm) goes in that model's Config default. | ||
| DEFAULT_LOCAL_COMPILE_REGIONS = ( |
There was a problem hiding this comment.
let's just delete this and have per model compile?
| default_factory=HuggingFaceTokenizer.Config | ||
| ) | ||
| dataloader: BaseDataLoader.Config = field(default_factory=BaseDataLoader.Config) | ||
| compile: LocalCompileConfig = field(default_factory=LocalCompileConfig) |
There was a problem hiding this comment.
will graph trainer still work?
| Regular models expect ``LocalCompileConfig`` (compiled regions); GraphTrainer overrides this to | ||
| return ``GraphTrainerCompileConfig`` (whole-step compile). | ||
| """ | ||
| return self.model_config.local_compile |
There was a problem hiding this comment.
why not just inline this?
daba926 to
3a5df8d
Compare
c6afb3d to
59d0bb4
Compare
Each model's config lists the local compile regions it uses (local_compile_regions: list[str]), so recipes stop restating model-specific lists. The trainer and generator pass them to model.parallelize, as before with LocalCompileConfig. GraphTrainer keeps passing its whole-step compile config through the same engine hook (TODO: move compile out of parallelize for both).
59d0bb4 to
e707ab7
Compare
# torchtitan/models/llama3/model.py
local_compile_regions: list[str] = field(default_factory=lambda: ["loss", "swiglu"])
So I propose to leave it in compile in parallelize + hook in this PR, and I added as a TODO to move it to the model constructor. Maybe @jessicazhongeee could help me with this one if you have bandwidth and tianyu is ok with the temporary solution.
# torchtitan/experiments/graph_trainer/trainer.py, GraphTrainingEngine
def _parallelize_compile_kwargs(self) -> dict[str, Any]:
"""Return the compile kwargs ``model.parallelize``/``model.pipeline`` expect for this engine's models.
Regular models expect ``local_compile_regions`` (compiled regions); GraphTrainer overrides this to
return ``compile_config`` (whole-step compile).
"""
# TODO: apply local compile outside parallelize/pipeline (#5026 review). That needs
# GraphTrainer to stop reading its compile config there first (apply_compile,
# EP-overlap chunking, enable_autoparallel, GraphPP runtime); then delete this hook.
return {"compile_config": self.config.compile}
|
| # TODO: apply local compile outside parallelize/pipeline (#5026 review). That needs | ||
| # GraphTrainer to stop reading its compile config there first (apply_compile, | ||
| # EP-overlap chunking, enable_autoparallel, GraphPP runtime); then delete this hook. | ||
| return {"local_compile_regions": self.model_config.local_compile_regions} |
There was a problem hiding this comment.
the coupling of
"core Model <> local_compile_regions" vs. "graph trainer Model <> compile_config" seems fragile
https://github.com/pytorch/torchtitan/blob/main/torchtitan/experiments/graph_trainer/model.py#L38
sure, can be fixed later.
TLDR: each model's config lists the local compile regions it uses (
local_compile_regions: list[str]), so recipes stop restating model-specific lists.model.parallelizetakes that list instead ofLocalCompileConfig.Main today
Problem
Which regions to compile depends on the model, but the list lives on the job config:
[gated_rmsnorm, loss, cos_sin_rope, offset_rmsnorm]. Those copies already drifted: none includesswiglu([Perf][Compile] Add local compilation for SwiGLU #4931).This PR
What to be aware of
config.model.local_compile_regions = [...](wasconfig.compile.regions).BaseModel.Configdefaults to[], so a new model compiles nothing, not even the loss, until it lists its regions.config.model.local_compile_regions, so they compile the same regions, as on main. Different regions per component: follow-up when there is a need.parallelize, as on main. Applying it outside (at model build or in the engine) is a follow-up: GraphTrainer reads its own compile config insideparallelize/pipelinetoo, so both should move together. Applying at build also leaked compiled regions into unrelated tests, since binding is process-wide.parallelizedrops the compile config, so it never compiled anything. It now lists["loss", "swiglu"](SwiGLU runs in the swapped-in TorchTitan MoE), so these compile for the first time. HF GPU integration tests should run before landing.swiglu. Every other recipe compiles the same regions as on main (all 235 no-arg recipes built on main and on this PR).[], as on main.