Skip to content

[Module] Replace from_nn_module with native Module subclasses - #3398

Merged
fegin merged 11 commits into
mainfrom
gh/fegin/123/head
May 26, 2026
Merged

fegin merged 11 commits into
mainfrom
gh/fegin/123/head

Conversation

@fegin

@fegin fegin commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Motivation
from_nn_module is designed to be a drop-in replacement of nn.Module. However it causes several issues including not easy to do sharding and may violate the build() constraint. So instead of continuing hacking it, this PR replaces the usage in TorchTitan with the explicit class definitions.

Summary
Replace Module.from_nn_module dynamic class generation with explicit diamond-inheritance classes in nn_modules.py, following the same pattern as Linear, RMSNorm, and Embedding.

Detail

  • Add Conv2d, GroupNorm, LayerNorm (parameterized, with typed Config fields and _init_self_parameters override delegating to reset_parameters)
  • Add GELU, SiLU, Identity (lightweight, Config with minimal or no extra fields)
  • Delete from_nn_module classmethod and _created_classes cache from module.py (~80 lines removed)
  • Update all flux callsites (layers.py, autoencoder.py) from direct construction to Config-based construction
  • Rewrite TestFromNnModule as TestNnModuleWrappers testing native classes

fegin added 2 commits May 19, 2026 11:00
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request May 19, 2026
…ses; LocalMapConfig None-per-arg

Replace Module.from_nn_module dynamic class generation with explicit diamond-inheritance classes in nn_modules.py, following the same pattern as Linear, RMSNorm, and Embedding.

- Add Conv2d, GroupNorm, LayerNorm (parameterized, with typed Config fields and _init_self_parameters override delegating to reset_parameters)
- Add GELU, SiLU, Identity (lightweight, Config with minimal or no extra fields)
- Delete from_nn_module classmethod and _created_classes cache from module.py (~80 lines removed)
- Update all flux callsites (layers.py, autoencoder.py) from direct construction to Config-based construction
- Rewrite TestFromNnModule as TestNnModuleWrappers testing native classes
- Allow None entries in LocalMapConfig.in_grad_placements and ShardingConfig.out_src_shardings for non-tensor forward args

ghstack-source-id: 2a21f21
Pull-Request: #3398
fegin added a commit that referenced this pull request May 19, 2026
…ses; LocalMapConfig None-per-arg

Replace Module.from_nn_module dynamic class generation with explicit diamond-inheritance classes in nn_modules.py, following the same pattern as Linear, RMSNorm, and Embedding.

- Add Conv2d, GroupNorm, LayerNorm (parameterized, with typed Config fields and _init_self_parameters override delegating to reset_parameters)
- Add GELU, SiLU, Identity (lightweight, Config with minimal or no extra fields)
- Delete from_nn_module classmethod and _created_classes cache from module.py (~80 lines removed)
- Update all flux callsites (layers.py, autoencoder.py) from direct construction to Config-based construction
- Rewrite TestFromNnModule as TestNnModuleWrappers testing native classes
- Allow None entries in LocalMapConfig.in_grad_placements and ShardingConfig.out_src_shardings for non-tensor forward args

ghstack-source-id: 2a21f21
Pull-Request: #3398
fegin added 2 commits May 20, 2026 14:24
[ghstack-poisoned]
[ghstack-poisoned]
@fegin fegin changed the title [NOT READY][Module] Replace from_nn_module with native Module subclasses; LocalMapConfig None-per-arg [NOT READY][Module] Replace from_nn_module with native Module subclasses May 20, 2026
[ghstack-poisoned]
@fegin
fegin marked this pull request as ready for review May 20, 2026 22:10
@fegin
fegin requested review from tianyu-l, wconstab and wwwjn as code owners May 20, 2026 22:10
@fegin fegin changed the title [NOT READY][Module] Replace from_nn_module with native Module subclasses [Module] Replace from_nn_module with native Module subclasses May 20, 2026

@tianyu-l tianyu-l left a comment

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.

one comment

Comment thread torchtitan/models/common/nn_modules.py Resolved
[ghstack-poisoned]
Comment thread review.html Outdated

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.

what's this, lol

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.

lol maybe we can also have some gitignored agent scratch space folder

@fegin fegin May 22, 2026 •

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.

wow, the agent went crazy to add too many things that I locally own. That's interesting.

fegin added 2 commits May 21, 2026 19:22
[ghstack-poisoned]
[ghstack-poisoned]
fegin added 2 commits May 22, 2026 19:39
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin changed the base branch from gh/fegin/123/base to main May 26, 2026 06:08
@fegin
fegin merged commit af33f76 into main May 26, 2026
13 of 15 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 27, 2026
…ules

Upstream PR pytorch#3398 (40th sync) replaced from_nn_module with native
Module subclasses; common/{linear,rmsnorm,embedding}.py were
deleted and consolidated into common/nn_modules.py. Update the 3
broken imports in ezpz to the new path:

- agpt/__init__.py: import Linear from nn_modules
- moe/model.py: import Linear, RMSNorm from nn_modules

API is identical (same Config dataclass, same constructor signature),
so this is a pure import-path swap. Smoke-tested import path locally.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 27, 2026
… routing

Merged 7 upstream commits (19c567f..af33f76). Documents which
ones needed ezpz replays:

- PR pytorch#3398 (Module subclass refactor): 3 import paths replayed in
  b052f29 — pure import-path swap, class API unchanged.
- PR pytorch#3146 (deterministic MoE routing): inherits transitively; this
  is the upstream fix for the _histc_xpu non-determinism blocker
  we hit on 2026-05-21. --debug.deterministic on MoE+XPU should now
  work.
- PR pytorch#3423 (MoE [7/n] 3D tensors): inherits transitively; doesn't
  touch deepseek_v3 callsites.
- PR pytorch#3105 (FSDP symm_mem): skipped — ezpz has its own apply_fsdp
  and symm_mem is an optional optimization XPU CCL likely doesn't
  support.
- PRs pytorch#3331/pytorch#3369/pytorch#3361: graph_trainer-only no-ops.

Captures two action items: smoke-test before next production push,
and re-try --debug.deterministic on MoE+XPU.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 27, 2026
… fix is incomplete

Three smokes on my own 2N alloc (12467455):

- agpt_2b clean (24.34 GiB, ~5,900 TPS, ~22% MFU) — matches the
  2026-05-22 baseline exactly. PR pytorch#3398 nn_modules import-path
  replay (b052f29) didn't perturb agpt.
- moe_2b_ep at the new LBS=2 default clean (27.09 GiB, ~3,200 TPS,
  9.4% MFU, loss 12.93→6.12) — numerically identical to the
  2026-05-22 baseline (27.08 GiB, ~3,200 TPS, 9.4% MFU, loss 6.15).
  PR pytorch#3423 (MoE [7/n] 3D tensors) inherits cleanly via the
  dispatcher path.
- moe_2b_ep with --debug.deterministic STILL FAILS with the same
  _histc_xpu error from 2026-05-21. PR pytorch#3146 was advertised to fix
  this but the merged diff only contains the aten.topk.default save
  -list addition — the promised histc → bincount swap in
  common/moe.py:262 is missing from the commit. Verified via GitHub
  API.

Adds smoke report at
docs/experiments/moe/sunspot/20260527-smoke-n2-40th-sync.md, links
from the moe experiments README, updates the 40th-sync entry in
upstream-sync.md, and the journal.

Open action item: file upstream issue on pytorch/torchtitan for
the incomplete PR pytorch#3146.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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