Skip to content

Enable Weight Preswizzling only when Swizzle fusion is available (#3232) - #3305

Closed
balasaajay wants to merge 1 commit into
NVIDIA:release_v2.18from
balasaajay:release_v2.18_mcore_fix
Closed

Enable Weight Preswizzling only when Swizzle fusion is available (#3232)#3305
balasaajay wants to merge 1 commit into
NVIDIA:release_v2.18from
balasaajay:release_v2.18_mcore_fix

Conversation

@balasaajay

Copy link
Copy Markdown
  • optimize for gemm's conditional enablement

  • avoid code repeatition

  • [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

  • address review comments

  • [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

  • remove redundant test

  • address review comment


Description

Please include a brief summary of the changes, relevant motivation and context.

Fixes # (issue)

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:

  • Change A
  • Change B

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

…DIA#3232)

* optimize for gemm's conditional enablement

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>

* avoid code repeatition

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* address review comments

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove redundant test

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>

* address review comment

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>

---------

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@balasaajay
balasaajay requested a review from ksivaman as a code owner August 3, 2026 18:43
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Aug 3, 2026
@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR centralizes weight-preswizzle eligibility and applies it dynamically across the PyTorch fused linear layers, limiting NVFP4 preswizzling according to recipe, architecture, and weight alignment.

  • Adds shared MXFP8/NVFP4 eligibility logic to TransformerEngineBaseModule.
  • Updates Linear, LayerNormLinear, LayerNormMLP, and GroupedLinear to configure weight quantizers during forward.
  • Expands cached-weight tests across NVFP4 quantization modes and aligned versus unaligned shapes.

Confidence Score: 4/5

The PR appears safe to merge, with a non-blocking device-selection issue for heterogeneous multi-GPU processes.

The new recipe and alignment gating is consistently applied across the affected layers, but architecture eligibility is read from the current CUDA device rather than the device that owns the weight.

Files Needing Attention: transformer_engine/pytorch/module/base.py

Important Files Changed

Filename Overview
transformer_engine/pytorch/module/base.py Adds the centralized preswizzle predicate; its architecture query should be tied to the weight device.
transformer_engine/pytorch/module/linear.py Moves Linear's preswizzle configuration from quantizer lookup to the forward path.
transformer_engine/pytorch/module/layernorm_linear.py Applies the shared eligibility predicate to LayerNormLinear's weight quantizer.
transformer_engine/pytorch/module/layernorm_mlp.py Independently evaluates preswizzle eligibility for the two MLP weights.
transformer_engine/pytorch/module/grouped_linear.py Evaluates shared expert eligibility once and propagates it across GroupedLinear weight quantizers.
tests/pytorch/test_weight_swizzle_in_layers.py Expands preswizzle state and numerical-equivalence coverage across recipes, NVFP4 modes, and alignments.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  W[Layer weight and quantizer] --> E[_enable_weight_preswizzle]
  E --> P{Recipe and primary-weight checks}
  P -->|MXFP8 eligible| O[Set optimize_for_gemm]
  P -->|NVFP4| A{Architecture, mode, and alignment eligible?}
  A -->|Yes| O
  A -->|No| U[Keep compact unswizzled scales]
  O --> Q[Quantize and cache weight]
  U --> Q
  Q --> G[GEMM]
Loading

Reviews (1): Last reviewed commit: "Enable Weight Preswizzling only when Swi..." | Re-trigger Greptile

return True
if isinstance(quantizer, NVFP4Quantizer):
rows, cols = weight.numel() // weight.shape[-1], weight.shape[-1]
arch_supported = get_device_compute_capability() >= (10, 0)

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.

P2 Capability check uses current device

If an NVFP4 module's weight resides on a GPU with a different compute capability from torch.cuda.current_device(), the argument-free capability check evaluates the wrong device, enabling an unsupported fused swizzle path or unnecessarily disabling the optimization for the weight's actual device.

Knowledge Base Used: PyTorch Fused Modules (transformer_engine/pytorch/module)

@vthumbe1503

Copy link
Copy Markdown
Collaborator

@balasaajay closing this PR as the PR is included as part of 2.18 branch already

@vthumbe1503 vthumbe1503 closed this Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants