Skip to content

[PyTorch] Add save_original_input in Linear/GroupedLinear to save memory #1865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

hxbai
Copy link
Contributor

@hxbai hxbai commented Jun 11, 2025

Description

Add save_original_input in Linear/GroupedLinear. If it is on, the modules save the original inputs and only do rowwise quant in forward and colwise quant in backward (for blockwise and mxfp8 recipe). With this, in some scenarios, we can save memory usage at the expense of separated quantizations.

In the current implementation, the Linear/GroupedLinear always saves the cast input tensors for backward.
But this may increase the memory usage if this input tensor is used by multiple modules, such as:

  • The output tensor of fused attn is saved by its self and used by the following proj_linear. We want the proj_linear to save the original input to save memory usage.
  • For multi-latent attention in DeepSeek models, the output of input_layernorm is used by q_down_linear and kv_down_linear. We want the two linears to save the original input.

Besides, in Megatron-LM, we added an output-discarding recomputation that trickily releases some saved tensors and restores them by recomputation. This relies on that the saved tensor is the original output tensor.

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:

  • transformer_engine/pytorch/module/linear.py
  • transformer_engine/pytorch/module/grouped_linear.py

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

hxbai and others added 5 commits June 11, 2025 02:33
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
BestJuly added a commit to BestJuly/TransformerEngine that referenced this pull request Jun 16, 2025
… memory NVIDIA#1865

Squashed commit of the following:

commit 218046d
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 01:25:48 2025 -0700

    refine the docstring

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit 097d900
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 00:43:35 2025 -0700

    minor fix

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit 90ade56
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 00:37:09 2025 -0700

    fix input_quantizer usage in Linear bwd

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit aa4a01f
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Wed Jun 11 03:06:44 2025 +0000

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

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

commit 406356f
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Tue Jun 10 18:58:01 2025 -0700

    save original input

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
hxbai added a commit to hxbai/TransformerEngine that referenced this pull request Jun 18, 2025
… memory NVIDIA#1865

Squashed commit of the following:

commit 218046d
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 01:25:48 2025 -0700

    refine the docstring

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit 097d900
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 00:43:35 2025 -0700

    minor fix

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit 90ade56
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 00:37:09 2025 -0700

    fix input_quantizer usage in Linear bwd

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit aa4a01f
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Wed Jun 11 03:06:44 2025 +0000

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

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

commit 406356f
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Tue Jun 10 18:58:01 2025 -0700

    save original input

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
hxbai added a commit to BestJuly/TransformerEngine that referenced this pull request Jun 18, 2025
… memory NVIDIA#1865

Squashed commit of the following:

commit 218046d
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 01:25:48 2025 -0700

    refine the docstring

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit 097d900
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 00:43:35 2025 -0700

    minor fix

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit 90ade56
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Wed Jun 11 00:37:09 2025 -0700

    fix input_quantizer usage in Linear bwd

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>

commit aa4a01f
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Wed Jun 11 03:06:44 2025 +0000

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

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

commit 406356f
Author: Hongxiao Bai <hongxiaob@nvidia.com>
Date:   Tue Jun 10 18:58:01 2025 -0700

    save original input

    Signed-off-by: Hongxiao Bai <hongxiaob@nvidia.com>
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