Skip to content

[compile] Move local compile regions to the model config - #5026

Merged
felipemello1 merged 1 commit into
pytorch:mainfrom
felipemello1:compile/local-compile-model-config
Oct 2, 2026
Merged

felipemello1 merged 1 commit into
pytorch:mainfrom
felipemello1:compile/local-compile-model-config

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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.parallelize takes that list instead of LocalCompileConfig.

Main today

Trainer.Config.compile:    LocalCompileConfig ──> engine.initialize(compile_config=) ──> model.parallelize(compile_config=)
Controller.Config.compile: LocalCompileConfig ─┬─> RL trainer(local_compile_config=) ──> engine ──> model.parallelize
                                               └─> generator(local_compile_config=) ──> vLLM wrapper ──> model.parallelize
GraphTrainer.Config.compile: GraphTrainerCompileConfig ──> GraphTrainerModel.parallelize / .pipeline

every Qwen3.5 recipe: compile=_qwen35_local_compile_config()   # model-specific list, copied per recipe

Problem

Which regions to compile depends on the model, but the list lives on the job config:

  • Every Qwen3.5 recipe (RL and SFT) restates [gated_rmsnorm, loss, cos_sin_rope, offset_rmsnorm]. Those copies already drifted: none includes swiglu ([Perf][Compile] Add local compilation for SwiGLU #4931).
  • RL threads the config through the controller into the trainer and the generator.
  • The shared default lists regions most models never run (e.g. Kimi K3 has no RoPE or SwiGLU).

This PR

Llama3Model.Config.local_compile_regions = ["loss", "swiglu"]          # each model lists what it runs
Qwen35Model.Config.local_compile_regions = [..., "offset_rmsnorm"]
KimiK3Model.Config.local_compile_regions = ["loss", "gated_rmsnorm", "situglu"]

trainer / generator: model.parallelize(..., local_compile_regions=model_config.local_compile_regions)

GraphTrainer: model regions = [] (raises otherwise); passes its own config.compile to its models,
              through the same engine hook (_parallelize_compile_kwargs)

What to be aware of

  • Overriding: in a recipe, config.model.local_compile_regions = [...] (was config.compile.regions). BaseModel.Config defaults to [], so a new model compiles nothing, not even the loss, until it lists its regions.
  • Trainer and generator both read 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.
  • Still applied in parallelize, as on main. Applying it outside (at model build or in the engine) is a follow-up: GraphTrainer reads its own compile config inside parallelize/pipeline too, so both should move together. Applying at build also leaked compiled regions into unrelated tests, since binding is process-wide.
  • HF transformers backend: on main its parallelize drops 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.
  • Behavior change: Qwen3.5/3.6/3.8 recipes now also compile swiglu. Every other recipe compiles the same regions as on main (all 235 no-arg recipes built on main and on this PR).
  • Batch-invariant recipes still set [], as on main.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 2, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 2, 2026
Comment thread torchtitan/distributed/local_compile.py Outdated
"gated_rmsnorm",
"loss",
"swiglu",
"situglu",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread torchtitan/distributed/local_compile.py Outdated
# 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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this, too

Comment thread torchtitan/distributed/local_compile.py Outdated
"situglu",
"cos_sin_rope",
]
default_factory=lambda: list(DEFAULT_LOCAL_COMPILE_REGIONS)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related: maybe set ["loss"] explicit for HF

Comment thread torchtitan/training_engine.py Outdated
training=config.training,
parallelism=config.parallelism,
compile_config=compile_config,
compile_config=self._compile_config(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not provide hooks for alternative trainers in experiments

Comment thread torchtitan/protocols/model.py Outdated
Subclasses define model-specific hyperparameters.
"""

local_compile: LocalCompileConfig = field(default_factory=LocalCompileConfig)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason we can't make it local_compile_regions: list[str]?

Comment thread torchtitan/training_engine.py Outdated
training=config.training,
parallelism=config.parallelism,
compile_config=compile_config,
compile_config=self._compile_config(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing the compile config to model.parallelize, I think we should put compile application into model constructor.

Comment thread torchtitan/distributed/local_compile.py Outdated

# 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 = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just delete this and have per model compile?

Comment thread torchtitan/trainer.py
default_factory=HuggingFaceTokenizer.Config
)
dataloader: BaseDataLoader.Config = field(default_factory=BaseDataLoader.Config)
compile: LocalCompileConfig = field(default_factory=LocalCompileConfig)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will graph trainer still work?

Comment thread torchtitan/training_engine.py Outdated
Regular models expect ``LocalCompileConfig`` (compiled regions); GraphTrainer overrides this to
return ``GraphTrainerCompileConfig`` (whole-step compile).
"""
return self.model_config.local_compile

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just inline this?

@felipemello1
felipemello1 marked this pull request as draft October 2, 2026 18:56
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 2, 2026
@felipemello1
felipemello1 force-pushed the compile/local-compile-model-config branch from daba926 to 3a5df8d Compare October 2, 2026 19:27
@felipemello1 felipemello1 changed the title [compile] Move LocalCompileConfig to the model config [compile] Move local compile regions to the model config Oct 2, 2026
@felipemello1
felipemello1 force-pushed the compile/local-compile-model-config branch 4 times, most recently from c6afb3d to 59d0bb4 Compare October 2, 2026 21:32
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).
@felipemello1
felipemello1 force-pushed the compile/local-compile-model-config branch from 59d0bb4 to e707ab7 Compare October 2, 2026 21:46
@felipemello1

Copy link
Copy Markdown
Contributor Author
  1. Per-model region lists, no shared default. BaseModel.Config defaults to []:
# torchtitan/models/llama3/model.py
local_compile_regions: list[str] = field(default_factory=lambda: ["loss", "swiglu"])
  1. Plain list instead of a config class: LocalCompileConfig is deleted. Recipes write config.model.local_compile_regions = []

  2. HF backend: it lists ["loss", "swiglu"], because HF MoE layers are swapped for TorchTitan MoE, which runs SwiGLU. On main HF compiled nothing (its parallelize swallowed compile_config).

  3. @tianyu-l asked to move compile into the model constructor. That makes sense, but I hit two issues:
    a) GraphTrainer reads its own compile config inside parallelize/pipeline in 4 places, so moving only regular models out would split the two paths.
    b) Compile is a process-wide switch. Applying it in the constructor made any test that builds a model leave compiled functions behind for later tests (test_loss and test_batch_invariance failed).

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.

  1. GraphTrainer: it still works. GraphTrainer.Config.compile is its own field, and models need empty regions (otherwise it raises). Its models read the compile config in 4 places inside parallelize/pipeline (apply_compile, EP-overlap chunking, enable_autoparallel, the GraphPP runtime), so a small hook stays for now:
# 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}
  1. Behavior vs main: only the Qwen3.5/3.6/3.8 recipes change (they gain swiglu, which their copied lists had dropped), plus HF as above. All other recipes compile the same regions

@felipemello1
felipemello1 marked this pull request as ready for review October 2, 2026 21:47
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 2, 2026
# 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}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@felipemello1
felipemello1 merged commit e76e810 into pytorch:main Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants