Repository navigation
Closed
Labels
actionablehigh prioritymodule: correctness (silent)issue that returns an incorrect result silentlymodule: linear algebraIssues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmulmodule: multithreadingRelated to issues that occur when running on multiple CPU threadsmodule: numerical-stabilityProblems related to numerical stability of operationsmodule: reductionstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Description
🐛 Bug
The torch.linalg.norm function silently produces incorrect results.
(It seems that this is specific to the setup used in Google Colab, on my local machine it works).
To Reproduce
import torch
def custom_norm(tensor):
return torch.sqrt((tensor**2).sum())
def test_norm(N):
A = torch.ones((N,N))
print(f"Size: {A.shape} \t:: Custom norm: {custom_norm(A)} \t:: Torch norm {torch.linalg.norm(A)}")
test_norm(10)
test_norm(100)
test_norm(1000)
test_norm(10000)
Output is
Size: torch.Size([10, 10]) :: Custom norm: 10.0 :: Torch norm 10.0
Size: torch.Size([100, 100]) :: Custom norm: 100.0 :: Torch norm 100.0
Size: torch.Size([1000, 1000]) :: Custom norm: 1000.0 :: Torch norm 1000.0
Size: torch.Size([10000, 10000]) :: Custom norm: 10000.0 :: Torch norm 4096.0
Expected behavior
The torch norm is wrong in the last line, probably due to some internal overflow (?).
Expected behavior would be to report that the capacity of the datatype is not high enough, e.g. raise some Error.
Then the user can correct it to the following, which will yield the correct output.
import torch
def custom_norm(tensor):
return torch.sqrt((tensor**2).sum())
def test_norm(N):
A = torch.ones((N,N),dtype=torch.float64)
print(f"Size: {A.shape} \t:: Custom norm: {custom_norm(A)} \t:: Torch norm {torch.linalg.norm(A)}")
test_norm(10)
test_norm(100)
test_norm(1000)
test_norm(10000)
Environment
Collecting environment information...
PyTorch version: 1.9.0+cu102
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A
OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.12.0
Libc version: glibc-2.26
Python version: 3.7.12 (default, Sep 10 2021, 00:21:48) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.104+-x86_64-with-Ubuntu-18.04-bionic
Is CUDA available: True
CUDA runtime version: 11.1.105
GPU models and configuration: GPU 0: Tesla P100-PCIE-16GB
Nvidia driver version: 460.32.03
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.0.5
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A
Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.9.0+cu102
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.10.0
[pip3] torchvision==0.10.0+cu102
[conda] Could not collect
cc @ezyang @gchanan @kadeng @msaroufim @albanD @pragupta @jianyuh @nikitaved @mruberry @walterddr @lezcano @zou3519 @bdhirsh @jbschlosser @pearu @IvanYashchuk @xwang233
Metadata
Metadata
Assignees
Labels
actionablehigh prioritymodule: correctness (silent)issue that returns an incorrect result silentlymodule: linear algebraIssues related to specialized linear algebra operations in PyTorch; includes matrix multiply matmulmodule: multithreadingRelated to issues that occur when running on multiple CPU threadsmodule: numerical-stabilityProblems related to numerical stability of operationsmodule: reductionstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
Activity
lezcano commentedon Sep 30, 2021
cc @kurtamohler who worked on torch.norm for quite some time.
I think that this is just a bug and should be solved asap. The numbers tested are not even that large and you are working with double precision, so I don't think this comes from an overflow.
kurtamohler commentedon Sep 30, 2021
Interesting, I'll look into it. It doesn't reproduce in my default environment, but I'll try to duplicate yours @feeds. Could you also post the output of
https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.pyfor the environment where you do not see this error? That might make it easier to figure out what difference is making this error show uplezcano commentedon Sep 30, 2021
Actually, it does not reproduce in my environment either. @feeds could you confirm whether you see this error in master or the nightly version of PyTorch?
kurtamohler commentedon Sep 30, 2021
@lezcano, it's in whatever Google colab uses by default (at the moment, it's pip torch==1.9.0+cu102). I can reproduce the error there
lezcano commentedon Sep 30, 2021
It might be the case that it's fixed in master? If @feeds confirms this we can close the issue.
kurtamohler commentedon Sep 30, 2021
cc @ngimel and @heitorschueroff, who have been working on norm functions more recently than me
ngimel commentedon Sep 30, 2021
Repros on master after setting num_threads to 1:
Most likely cause is as regular summation for norm goes, it stops updating values after reaching 2^24 (because fp32 mantissa), and then sqrt(2^24) is 4096.
We need cascade reductions for norm to fix this (we have cascade reductions for sum, but not for norm)
peterbell10 commentedon Sep 30, 2021
I wonder if the
SumKernelcode could be reused here as a generic map-sum, similar to how nansum is implemented. Though it would still need a separate pass to take the finalsqrt. To do it truely as one pass, we would need something like #40570.ezyang commentedon Oct 4, 2021
my kingdom for a pointwise-reductions compiler (cc @bertmaher)
ngimel commentedon Oct 4, 2021
Compiler sounds nice, but we should have good vectorized implementations for those reductions regardless.
egeboy35 commentedon Oct 21, 2026
@ngimel @lezcano
egeboy35 commentedon Oct 28, 2026
@ngimel @lezcano
Use cascade summation for CPU vector norms with ord 0, 1 and 2
Use cascade summation for CPU vector norms with ord 0, 1 and 2
Use cascade summation for CPU vector norms with ord 0, 1 and 2 (pytor…