Skip to content

[PyTorch] Support split Linear parameters with torch.compile - #3623

Open
pggPL wants to merge 32 commits into
NVIDIA:mainfrom
pggPL:split_parameters_linear
Open

pggPL wants to merge 32 commits into
NVIDIA:mainfrom
pggPL:split_parameters_linear

Conversation

@pggPL

@pggPL pggPL commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Support torch.compile(fullgraph=True) for
te.Linear(parameters_split=...), preserving separate Parameters,
state-dict entries and gradients.

Split weights and biases cross the custom-op boundary as tensor lists.
Contiguous parts occupying adjacent regions of the same storage are consumed
through a checked view without a concatenation copy. Other layouts fall back
to torch.cat. Storage is checked on each execution.

Eager Linear and LayerNormLinear assemble split operands inside forward
and save the resulting tensors for backward. LayerNormLinear compile support
is outside this PR's scope. Disjoint parameters still require concatenation
in both compiled forward and backward.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Add tensor-list inputs to the custom-op adapter and combined metadata
    handling to Linear's fake implementations.
  • Save original compiled parameter parts through autograd and split full
    gradients into views outside the backward op.
  • Assemble eager split operands inside Linear and LayerNormLinear forward,
    retaining them for backward.
  • Extend existing Linear compile tests with split parameters, quantization
    recipes, selective gradients, weight caching, storage changes and cat counts.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

pggPL added 15 commits October 5, 2026 11:42
…ters

Pass split Linear parameters through the custom-op boundary, reconstruct checked views at execution time and split full gradients outside backward. Preserve concatenation for disjoint storage and returned biases, handle promoted dtypes, and compute bias gradients when weights are frozen.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Replace ConcatenatedTensor with DeferredCat(parts), expose metadata through to_spec and keep materialization inside the custom op. Preserve deferred backward operands for non-FP8 training, reject quantized parts, and make dimension checks consume explicit metadata.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Keep singleton operands unwrapped and represent split parameters with immutable ParameterParts through the ConcatInput alias. Centralize eager and compiled input preparation, let the custom-op adapter restore saved parts, and select the execution path before concatenating parameters. Name backward weights by their role and update the existing MLA caller.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Restore compile_unsupported_reason, setup_saved_tensors(ctx), the original backward operand names, and the existing adapter contract. Remove the unrelated MLA changes. Retain the parameter-parts alias and helper, split-parameter correctness fixes, and focused regression coverage.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Use None when Linear has no deferred parameter parts, including eager
forward, saved backward arguments and compile fallbacks. This avoids
allocating empty weight and bias lists on the ordinary eager path.

Transport Optional[List[Tensor]] through Tensor[]? custom-op inputs,
preserving None without allocating replacement lists. Keep split-gradient
handling outside the backward op and let the adapter supply absent list
gradients when needed.

Cover ordinary and optional lists, empty lists and None in the adapter
test. Validate the full compile suite (384 passed, 62 skipped, one existing
XPASS), 55 focused cases, 30 numerical Linear cases, pre-commit and Python
L0 lint.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Use the same checked view or concatenation in eager and compiled Linear.
Pass individual parts as explicit autograd inputs and save the materialized
operands for eager backward, which returns views of the full gradients.
Preserve the single-parameter and external-bias paths.

Derive the view strides directly from contiguous parts to avoid symbolic
stride-helper overhead. Extend the existing storage and cat-count tests.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator Author

TE CI — selected tests

Pipeline ID: 77100152 · Commit: ddb3eaae745e9edb78cbb5fa092d2151877f55c7

Selected CI jobs and pytest filters are listed below.

Frameworks: PyTorch

Mode: PR Python, tests and QA with prebuilt native libraries; no TE rebuild.
Native image digest: sha256:46ede145e9eac17945e528111d8f074d501d83d9c4ef67db651cd127f3dc3dec

Selected jobs: 1

  • L0_pytorch_unittest--H100_1GPU

Not run (manual/skipped): 14

Show jobs not run
  • L0_pytorch_debug_unittest--B200_1GPU
  • L0_pytorch_debug_unittest--H100_1GPU
  • L0_pytorch_lint
  • L0_pytorch_unittest--A100_1GPU
  • L0_pytorch_unittest--B200_1GPU
  • L1_pytorch_distributed_unittest--B200_8GPU
  • L1_pytorch_distributed_unittest--H100_4GPU
  • L1_pytorch_mcore_fsdp_integration--B200_8GPU
  • L1_pytorch_mcore_integration--B200_1GPU
  • L1_pytorch_mcore_integration--H100_1GPU
  • L1_pytorch_onnx_unittest--B200_1GPU
  • L1_pytorch_onnx_unittest--H100_1GPU
  • L3_pytorch_FA_versions_test--B200_8GPU
  • L3_pytorch_FA_versions_test--H100_4GPU

Results are reported as individual commit statuses on this PR.

@pggPL

pggPL commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator Author

TE CI — selected tests

Pipeline ID: 77114224 · Commit: ddb3eaae745e9edb78cbb5fa092d2151877f55c7

Selected CI jobs and pytest filters are listed below.

Frameworks: PyTorch

Mode: PR Python, tests and QA with prebuilt native libraries; no TE rebuild.
Native image digest: sha256:46ede145e9eac17945e528111d8f074d501d83d9c4ef67db651cd127f3dc3dec

Pytest include: tests/pytorch/test_torch_compile.py, tests/pytorch/test_sanity.py

Job results cover this pytest subset. Non-pytest QA commands are unchanged.

Selected jobs: 1

  • L0_pytorch_unittest--B200_1GPU

Not run (manual/skipped): 14

Show jobs not run
  • L0_pytorch_debug_unittest--B200_1GPU
  • L0_pytorch_debug_unittest--H100_1GPU
  • L0_pytorch_lint
  • L0_pytorch_unittest--A100_1GPU
  • L0_pytorch_unittest--H100_1GPU
  • L1_pytorch_distributed_unittest--B200_8GPU
  • L1_pytorch_distributed_unittest--H100_4GPU
  • L1_pytorch_mcore_fsdp_integration--B200_8GPU
  • L1_pytorch_mcore_integration--B200_1GPU
  • L1_pytorch_mcore_integration--H100_1GPU
  • L1_pytorch_onnx_unittest--B200_1GPU
  • L1_pytorch_onnx_unittest--H100_1GPU
  • L3_pytorch_FA_versions_test--B200_8GPU
  • L3_pytorch_FA_versions_test--H100_4GPU

Results are reported as individual commit statuses on this PR.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

TE CI — selected tests

Pipeline ID: 77123759 · Commit: f5a13d2515cd40a83e21cb364dfe8907adff1178

Selected CI jobs and pytest filters are listed below.

Frameworks: PyTorch

Mode: PR Python, tests and QA with prebuilt native libraries; no TE rebuild.
Native image digest: sha256:46ede145e9eac17945e528111d8f074d501d83d9c4ef67db651cd127f3dc3dec

Pytest exclude: tests/pytorch/test_fused_rope.py, tests/pytorch/test_permutation.py, tests/pytorch/test_cross_entropy.py, tests/pytorch/test_mhc.py, tests/pytorch/test_fused_router.py, tests/pytorch/test_fused_router_perf.py

Job results cover this pytest subset. Non-pytest QA commands are unchanged.

Selected jobs: 7

  • L0_pytorch_debug_unittest--B200_1GPU
  • L0_pytorch_debug_unittest--H100_1GPU
  • L0_pytorch_lint
  • L0_pytorch_unittest--A100_1GPU
  • L0_pytorch_unittest--B200_1GPU
  • L0_pytorch_unittest--H100_1GPU
  • L0_pytorch_unittest--L40_1GPU

Not run (manual/skipped): 9

Show jobs not run
  • L1_pytorch_distributed_unittest--B200_8GPU
  • L1_pytorch_distributed_unittest--H100_4GPU
  • L1_pytorch_mcore_fsdp_integration--B200_8GPU
  • L1_pytorch_mcore_integration--B200_1GPU
  • L1_pytorch_mcore_integration--H100_1GPU
  • L1_pytorch_onnx_unittest--B200_1GPU
  • L1_pytorch_onnx_unittest--H100_1GPU
  • L3_pytorch_FA_versions_test--B200_8GPU
  • L3_pytorch_FA_versions_test--H100_4GPU

Results are reported as individual commit statuses on this PR.

Merge main 9b7e959.
Preserve the existing seven-file split Linear change without conflicts.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
pggPL added a commit to pggPL/TransformerEngine that referenced this pull request Oct 28, 2026
Restore the tree from 917773b. The split-parameter support belongs in PR NVIDIA#3623.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
pggPL added 9 commits October 28, 2026 15:20
Share Linear's checked view/cat helper for split weights and fused biases.
Pass the parts as explicit autograd inputs, save full operands for backward
and return views of the full parameter gradients.

Keep backward computation and saved-tensor restoration unchanged. Compute
split shapes only when configuring weight quantization, and extend the
existing cat-count regression to eager LayerNormLinear.

Remove standalone storage-helper and tensor-list adapter tests at the
user's request.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Replace five split-parameter tests with one parameterized integration test.
Cover gradients, inference, recipes, bias modes, storage and parameter updates,
saved-version checks and concatenation counts in a shared execution flow.
Use ordinary torch.cat in the reference to verify deferred gradient routing.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Rely on concat_input to select nonempty ordinary tensor lists. Preserve
storage, layout, offset and bounds checks before reconstructing a view.

Extend the shared integration test with LayerNormLinear recipes and dtype/
split variants, plus mixed parameter dtypes under autocast. Keep dtype
promotion in fake concatenation to match torch.cat metadata.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Remove the deferred-concatenation switch from Linear and LayerNormLinear
parameter getters. Keep bias routing in the getters and explicitly
concatenate deferred operands in the ONNX inference paths.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Rename concat_input to prepare_parameter and drop the tensors suffix
from the Linear and LayerNormLinear getters, which can return split lists.
Use the same forward operand names in LayerNormLinear as in Linear.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Use the presence of parameter_split_sizes to select gradient splitting
in eager Linear and LayerNormLinear backward instead of counting the
fixed positional autograd inputs.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Restore parameter lists from named input-list lengths already tracked by
the custom-op adapter. Remove the duplicate lengths and the saved weight
alias flag from Linear backward arguments. Resolve a missing plain split
weight in the existing backward workspace reconstruction path.

Extend the split integration test with workspace caching, reconstruction
and high-precision/dequantized backward overrides.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Move selected split scenarios into the Linear compile and LayerNormLinear
sanity tests, and add a split case to the existing microbatch cache test.
Share parameter-update assertions and remove the standalone split test.
Append complete scenarios without multiplying the existing parameter axes.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
pggPL added 3 commits October 28, 2026 19:27
Move the parameter-update helper from the shared test utilities into
test_torch_compile.py and name it _check_module_outputs_and_grads.
Restore the shared utility module to its upstream contents.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Run split and unsplit scenarios through the same local output and gradient
checker. Keep CUDA graph warmup and exact unsplit comparisons while
removing the duplicated test body and its early return.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Fall back to cat when the first split is empty, since its trailing
strides need not describe the concatenated tensor. Restore the existing
empty bias tensor for LayerNormLinear without bias.

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL pggPL changed the title [PyTorch] Avoid concatenation copies for compiled split Linear parameters [PyTorch] Support split Linear parameters with torch.compile Oct 29, 2026
@pggPL
pggPL marked this pull request as ready for review October 29, 2026 11:41
@pggPL
pggPL requested a review from ksivaman as a code owner October 29, 2026 11:41
@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds split-parameter support for Linear under torch.compile(fullgraph=True).

  • Compiled Linear keeps split parameters separate through forward and backward.
  • Eager Linear returns gradients to each original parameter part.
  • Eager LayerNormLinear sends gradients back to each split parameter.

No new actionable issues were found. The earlier unnumbered test suggestion was manually resolved by pggPL without an explanatory reply. The current tests compare split LayerNormLinear outputs and gradients with shared and separate storage.

Review was static; tests were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  P[Separate parameters] --> B[Forward receives tensor lists]
  B --> C{Parts occupy adjacent shared storage?}
  C -->|Yes| V[Build checked view]
  C -->|No| T[Concatenate parts]
  V --> F[Run Linear]
  T --> F
  F --> S[Save original parts for compiled backward]
  S --> G[Compute full gradients]
  G --> D[Split gradients outside custom operation]
  D --> R[Return gradients to separate parameters]
Loading

Reviews (4) · Last reviewed commit: "Merge main into split_parameters_linear" · Reviewed by Greptile

…torage

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L0

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL

pggPL commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L0

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant