Skip to content

[PyTorch] Linear op avoids saving input tensor if weight grad is not needed#1817

Merged
timmoon10 merged 7 commits intoNVIDIA:mainfrom
timmoon10:te-sequential-no-wgrad
May 29, 2025
Merged

[PyTorch] Linear op avoids saving input tensor if weight grad is not needed#1817
timmoon10 merged 7 commits intoNVIDIA:mainfrom
timmoon10:te-sequential-no-wgrad

Conversation

@timmoon10
Copy link
Copy Markdown
Collaborator

Description

The linear op caches its input tensor after the forward pass so that it can be used in the backward pass to compute the grad weight tensor. However, if the grad weight tensor is not needed, we can save some memory by not caching.

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

  • Linear op avoids saving input tensor if weight grad is not needed

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

Signed-off-by: Tim Moon <tmoon@nvidia.com>
@timmoon10 timmoon10 added the enhancement New feature or request label May 22, 2025
@timmoon10
Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@timmoon10
Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L1

@timmoon10 timmoon10 merged commit 41909dc into NVIDIA:main May 29, 2025
9 of 11 checks passed
phu0ngng pushed a commit to phu0ngng/TransformerEngine that referenced this pull request May 29, 2025
…needed (NVIDIA#1817)

* Linear op avoids saving input tensor if weight grad is not needed

Signed-off-by: Tim Moon <tmoon@nvidia.com>

* Linear op forward avoids producing quantized tensors with unnecessary usages

Signed-off-by: Tim Moon <tmoon@nvidia.com>

* Fix linter warnings

Signed-off-by: Tim Moon <tmoon@nvidia.com>

* Avoid unnecessary usages in fused linear ops

Signed-off-by: Tim Moon <tmoon@nvidia.com>

---------

Signed-off-by: Tim Moon <tmoon@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant