Skip to content

[common] Move Linear and ScaledBiasRowwiseLinear into common/linear.py - #3923

Merged
fegin merged 1 commit into
pytorch:mainfrom
jinsooihm:linear
Jul 15, 2026
Merged

fegin merged 1 commit into
pytorch:mainfrom
jinsooihm:linear

Conversation

@jinsooihm

@jinsooihm jinsooihm commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

New file torchtitan/models/common/linear.py to store variants of Linear in case some other model needs ScaledBiasRowwiseLinear from gpt_oss (some internal models).

Moved

  • Linear from nn_modules.py,
  • ScaledBiasRowwiseLinear from gpt_oss/model.py.

Repointed all import sites accordingly.

Pure move with no behavior change.

Test Plan:
pre-commit run on all changed files: passed (flake8, ufmt, pyrefly, etc.). Ran affected unit tests, all green: test_linear, test_module, test_train_spec, test_lora, test_fused_qkv, test_rope, test_quantization, test_activation_checkpoint, test_fused_swiglu, test_compile_moe, test_compile_regional_inductor.

Introduce torchtitan/models/common/linear.py as the home for configurable
linear modules. Move the Linear wrapper out of nn_modules.py and
ScaledBiasRowwiseLinear out of gpt_oss/model.py into this new module, and
repoint all import sites accordingly.

- nn_modules.py no longer defines Linear (removed from __all__).
- gpt_oss/model.py drops the now-unused torch.nn.functional import.
- common/__init__.py re-exports Linear and ScaledBiasRowwiseLinear.
- gpt_oss/__init__.py imports ScaledBiasRowwiseLinear from common.linear.

Pure move with no behavior change.

Test Plan:
pre-commit run on all changed files: passed (flake8, ufmt, pyrefly, etc.).
Ran affected unit tests, all green: test_linear, test_module, test_train_spec,
test_lora, test_fused_qkv, test_rope, test_quantization,
test_activation_checkpoint, test_fused_swiglu, test_compile_moe,
test_compile_regional_inductor.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 15, 2026
@pytorch-bot

pytorch-bot Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Jul 15, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/8gpu
  • ciflow/rl

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@fegin
fegin merged commit 659a0d2 into pytorch:main Jul 15, 2026
32 of 38 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jul 16, 2026
…ytorch#3923)

69th upstream sync (upstream/main 8c92de8 merge) moved Linear +
ScaledBiasRowwiseLinear out of models/common/nn_modules.py into a new
models/common/linear.py (upstream pytorch#3923). Our agpt/moe import the deep
path common.nn_modules (not the package re-export), so the move breaks
them with ImportError: cannot import name Linear.

Repoint both callsites:
- agpt/__init__.py: Linear from common.linear
- moe/model.py: split -- Linear from common.linear, RMSNorm stays in
  common.nn_modules

Import smoke: agpt + moe registries import clean. Train smoke via
sync_smoke.sh pending (GPU).
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jul 16, 2026
- upstream-sync.md: 69th sync entry (9 commits, merge 8c92de8) with the
  pytorch#3923 Linear-move breaking-change analysis + fix + sync_smoke VERDICT ok
- journal.md: 2026-07-16 session (SFT epoch 1.0 complete, 69th sync, disk,
  compile root-cause)
- refresh recently-updated docs table
mreso added a commit to mreso/torchtitan that referenced this pull request Jul 17, 2026
Absorbs pytorch#3923 ([common] Move Linear and ScaledBiasRowwiseLinear into
common/linear.py).

- Resolved common/__init__.py: keep our SharedExperts export from feed_forward
  and add main's `from .linear import Linear, ScaledBiasRowwiseLinear`.
- moe_replacement.py: Linear moved out of common/nn_modules into common/linear,
  so update the import (was `from ...nn_modules import Linear`).

Verified: MoE EP=2 trains (12.36 -> 7.16); experiment module + all flavors
import cleanly; lint passes.

@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.

New file torchtitan/models/common/linear.py to store variants of Linear in case some other model needs ScaledBiasRowwiseLinear from gpt_oss (some internal models).

I could understand you need ScaledBiasRowwiseLinear, so it'd better be put in common. But why would you need to move Linear? nn_modules.py has all the modules where we just put a thin wrapper over pytorch modules, so Linear clearly belongs to that category.

I would rather have something like scaled_bias_linear.py which is temporary anyway (remove after we support spmd.P bias).

cc @fegin @pianpwk

@fegin

fegin commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

But why would you need to move Linear?

This is my suggestion.

nn_modules.py has all the modules where we just put a thin wrapper over pytorch modules, so Linear clearly belongs to that category.

Yes, I considered this case. But I also feel linear modules are better put together. But I also feel that linear modules should be better put together.

I would rather have something like scaled_bias_linear.py which is temporary anyway (remove after we support spmd.P bias).

I didn't consider this :/

We can move it back to nn_module.py after it is removed. Sorry about the wrong suggestion.

@tianyu-l

Copy link
Copy Markdown
Contributor

Thanks for explaining.

My goal is still to kill this weird scaled bias linear class, and instead use spmd.P for bias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl 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.

4 participants