Skip to content

Conversation

namgyu-youn
Copy link
Contributor

@namgyu-youn namgyu-youn commented Jun 6, 2025

What does this PR do?

Type of change: Refactor

Overview: Based on PyTorch - docs, torch.norm() will be removed in the foreseeable version. Therefore, this update replaces torch.norm() with torch.linalg.vector_norm()

Usage

# Old (current) version
torch.norm()

# New (upcoming) version
torch.linalg.vector_norm()

Testing

pytest and the comment

Before your PR is "Ready for review"

  • Make sure you read and follow Contributor guidelines
  • Is this change backward compatible?: Yes
  • Did you write any new necessary tests?: Yes
  • Did you add or update any necessary documentation?: No
  • Did you update Changelog?: No

Additional Information

torch.norm is deprecated and may be removed in future PyTorch releases
torch.norm is deprecated and may be removed in future PyTorch releases
@namgyu-youn
Copy link
Contributor Author

cc. @kevalmorabia97 @meenchen

@kevalmorabia97 kevalmorabia97 self-requested a review June 16, 2025 21:12
@kevalmorabia97
Copy link
Collaborator

kevalmorabia97 commented Jun 16, 2025

Thanks @namgyu-youn for contributing the fix. Can you also apply similar changes to usage of <tensor>.norm() throughout the repository?

Please also share testing details - whether you have run unit and GPU tests? (pytest tests/unit and pytest tests/gpu)

@namgyu-youn namgyu-youn changed the title Update L1-norm logic for foreseeable PyTorch update [refactor] Update L1-norm logic for upcoming PyTorch update Jun 17, 2025
@namgyu-youn
Copy link
Contributor Author

namgyu-youn commented Jun 17, 2025

Thanks @namgyu-youn for contributing the fix. Can you also apply similar changes to usage of <tensor>.norm() throughout the repository?

@kevalmorabia97; Sure, and it seems there is no other Ln-norm logic in modelopt/. Let me know if I am missing something.

Please also share testing details - whether you have run unit and GPU tests? (pytest tests/unit and pytest tests/gpu)

The result of pytest is the following:
image

Also, local test code is the following:

import torch

# Create test tensors
torch.manual_seed(42)
X = torch.randn(10, 10)
X_hat, A_reg = torch.randn(10, 10), torch.randn(5, 5)

# Old Version (torch.norm)
print("=== torch.norm ===")
relative_error = torch.dist(X, X_hat, p=torch.inf) / torch.norm(X, p=torch.inf)
norm_result = torch.norm(A_reg, p=torch.inf)

print("Relative error:", relative_error.item())
print("A_reg infinity norm:", norm_result.item())
print()

# New Version (torch.linalg.vector_norm)
print("=== torch.linalg.vector_norm ===")
relative_error = torch.dist(X, X_hat, p=torch.inf) / torch.linalg.vector_norm(X, ord=torch.inf)
norm_result = torch.linalg.vector_norm(A_reg, ord=torch.inf)

print("Relative error:", relative_error.item())
print("A_reg infinity norm:", norm_result.item())

And the result is:

=== torch.norm ===
Relative error: 1.512144684791565
A_reg infinity norm: 1.9775909185409546

=== torch.linalg.vector_norm ===
Relative error: 1.512144684791565
A_reg infinity norm: 1.9775909185409546

@kevalmorabia97
Copy link
Collaborator

@namgyu-youn
Copy link
Contributor Author

Please see https://github.com/search?q=repo%3ANVIDIA%2FTensorRT-Model-Optimizer+.norm%28&type=code

Got it, really thanks for your leading.

@namgyu-youn namgyu-youn changed the title [refactor] Update L1-norm logic for upcoming PyTorch update [refactor] Update Ln-norm logic for upcoming PyTorch update Jun 18, 2025
@kevalmorabia97
Copy link
Collaborator

Thanks for the changes. Let me take this in our internal repo where we run a more extensive CI test which is yet to be migrated to Github.

assert isinstance(out, nn.Linear)
hp_hidden_dim.register_importance(lambda: out._parameters["weight"].detach().norm(dim=0))
hp_hidden_dim.register_importance(
lambda: torch.linalg.norm(out._parameters["weight"].detach(), dim=0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use vector_norm here as well?

Copy link
Contributor Author

@namgyu-youn namgyu-youn Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case computes vector norm because dim is int. Please check above comment; let me know if there is anything wrong in internal CI

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets use vector_norm explicitly to avoid any ambiguity and consistency with rest of the changes

…or-norm

`torch.linalg.norm` supports various calculations based on `dim` parameter:
- If dim is an int, the vector norm will be computed.
- If dim is a 2-tuple, the matrix norm will be computed.
- If dim=None and ord=None, A will be flattened to 1D and the 2-norm of the resulting vector will be computed.
- If dim=None and ord!=None, A must be 1D or 2D.

Therefore, vector norm is not computed when `dim` is tuple. (Nit: `torch.linalg.vector_norm` is more explicit for vector norms.)
@kevalmorabia97
Copy link
Collaborator

There is one more torch.norm change missing at

attn_head_importance = self._activations.view(
self.get_hparam("num_heads_per_group").max * self.get_hparam("num_query_groups").max,
self.config.kv_channels,
).norm(p=2, dim=1)

Can you please address that as well?

- `torch.norm` is deprecated in favor of `torch.linalg.norm` and `torch.linalg.vector_norm`.
@namgyu-youn
Copy link
Contributor Author

There is one more torch.norm change missing at

attn_head_importance = self._activations.view(
self.get_hparam("num_heads_per_group").max * self.get_hparam("num_query_groups").max,
self.config.kv_channels,
).norm(p=2, dim=1)

Can you please address that as well?

@kevalmorabia97 ; Thanks for your leading. Could you take a look at this PR?

@namgyu-youn
Copy link
Contributor Author

namgyu-youn commented Jun 28, 2025

Also, could you comment about #146 - Comment? Reviewing source codes in modelopt/torch/nas and modelopt/torch/prune was the start for a deeper (new feature) contribution.

But before the start, your (and other maintainers) advice must be valuable background for the directionality. I hope the ticket is still open.

kevalmorabia97 added a commit that referenced this pull request Jul 14, 2025
@kevalmorabia97
Copy link
Collaborator

Your PR commit is merged. Thank you for contributing

Tala-mahhmmoodi pushed a commit to Tala-mahhmmoodi/TensorRT-Model-Optimizer that referenced this pull request Aug 5, 2025
commit 7a27f2a
Author: Keval Morabia <[email protected]>
Date:   Mon Jul 14 23:19:04 2025 +0530

    Update Ln-norm logic for upcoming PyTorch update (NVIDIA#206)

    Co-authored-by: namgyu-youn <[email protected]>

commit 8e3bfb5
Author: Keval Morabia <[email protected]>
Date:   Mon Jul 14 23:17:58 2025 +0530

    Fix NF4 scale padding (NVIDIA#183)

    Co-authored-by: ishan-modi <[email protected]>

commit cafa7f6
Author: Keval Morabia <[email protected]>
Date:   Mon Jul 14 23:08:26 2025 +0530

    Update for 0.33.0 release

commit 33a45be
Author: omrialmog <[email protected]>
Date:   Fri Jun 27 08:31:31 2025 -0700

    Update README.md news NVFP4 Blog (NVIDIA#223)

commit 5b4dc03
Author: Keval Morabia <[email protected]>
Date:   Wed Jun 18 04:46:36 2025 +0530

    Add Github CI action to build and publish docs (NVIDIA#219)

commit de20a6a
Author: Keval Morabia <[email protected]>
Date:   Tue Jun 17 03:27:57 2025 +0530

    Enable cpu unit tests in Github CI (NVIDIA#210)

commit 11b3eb6
Author: Keval Morabia <[email protected]>
Date:   Wed Jun 11 16:14:16 2025 -0700

    Add tox.ini and fix code_quality workflow

commit 6fd7a64
Author: Keval Morabia <[email protected]>
Date:   Wed Jun 11 15:33:45 2025 -0700

    Add SECURITY.md file

commit d6e32e9
Author: Keval Morabia <[email protected]>
Date:   Tue Jun 10 15:56:35 2025 -0700

    Add code quality checks for pull requests

commit 5a2bf34
Author: Keval Morabia <[email protected]>
Date:   Thu Jun 5 13:50:50 2025 -0700

    Fix installation files
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.

2 participants