Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

The problem caused by the parameter dim of torch.norm #89431

Open
triumph-wangyuyang opened this issue Nov 21, 2022 · 2 comments
Open

The problem caused by the parameter dim of torch.norm #89431

triumph-wangyuyang opened this issue Nov 21, 2022 · 2 comments
Labels
module: edge cases Adversarial inputs unlikely to occur in practice module: error checking Bugs related to incorrect/lacking error checking triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@triumph-wangyuyang
Copy link

馃悰 Describe the bug

pytorch1.12.1

code 1 : The test code dim=False, but the thrown exception describes dim as a list.

    import torch
    input = torch.rand([5, 5, 5], dtype=torch.float64)
    out = torch.norm(input, p='fro', dim=False, keepdim=False, out=None, dtype=None)
    print(out)

result:    TypeError: frobenius_norm(): argument 'dim' (position 2) must be tuple of ints, not list


code 2 : The test code dim=False, the exception thrown is correct.

    import torch
    input = torch.rand([5, 5, 5], dtype=torch.float64)
    out = torch.norm(input, p='fro', dim="False", keepdim=False, out=None, dtype=None)
    print(out)

result: TypeError: frobenius_norm(): argument 'dim' (position 2) must be tuple of ints, not str

pytorch1.8.0

code 1 : In the pytorch1.8 version, dim is still set to False, and the program can run successfully, because the program automatically treats False as a value of 0..

      import torch
      input = torch.rand([5, 5, 5], dtype=torch.float64)
      out = torch.norm(input, p='fro', dim=False, keepdim=False, out=None, dtype=None)
      print(out)

result:    tensor([[1.3522, 1.1940, 0.7555, 1.3410, 1.3593],
              [1.0926, 1.0986, 1.3429, 1.0170, 1.1915],
              [1.1807, 1.4252, 1.5389, 1.2257, 1.5626],
              [1.5249, 0.9933, 1.2927, 1.7106, 1.6482],
              [1.1870, 1.0370, 1.6912, 1.0616, 1.0801]], dtype=torch.float64)

Question 1: Why is False treated as a list in the first test code?
Question 2: Why can鈥檛 False be treated as a value of 0 on a higher version, because when compiled according to the bottom layer of python, False will be set to 0, so why does the program fail?

Versions

pytorch: 1.12.1
pytorch: 1.8.0
Python version: 3.8
CUDA/cuDNN version: cuDNN 11.3
GPU models and configuration: RTX3060
Operating System锛歐indows

@samdow
Copy link
Contributor

samdow commented Nov 21, 2022

Hi, in general we use github issues only for bug reports or feature requests. Please use the forum to ask questions: https://discuss.pytorch.org/

This is being called an list instead of a bool because of the transform that norm does before calling the C++ function. Specifically it does
_dim = [i for i in range(ndim)]
and passes that to the function, seen in the source for norm

With these older versions, there may have been unintended behavior that we allowed. The user behavior when passing False here is ambiguous (did they really mean False or did they mean for it to be treated as an int). If they did intend for it to be treated as an int, they can cast it themselves

@samdow samdow added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Nov 21, 2022
@ngimel ngimel added module: error checking Bugs related to incorrect/lacking error checking module: edge cases Adversarial inputs unlikely to occur in practice labels Nov 21, 2022
@triumph-wangyuyang
Copy link
Author

Hi, in general we use github issues only for bug reports or feature requests. Please use the forum to ask questions: https://discuss.pytorch.org/

This is being called an list instead of a bool because of the transform that norm does before calling the C++ function. Specifically it does _dim = [i for i in range(ndim)] and passes that to the function, seen in the source for norm

With these older versions, there may have been unintended behavior that we allowed. The user behavior when passing False here is ambiguous (did they really mean False or did they mean for it to be treated as an int). If they did intend for it to be treated as an int, they can cast it themselves

Thank you for your reply, but the same code has different performances in different versions, which is quite confusing, especially this kind of underlying problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: edge cases Adversarial inputs unlikely to occur in practice module: error checking Bugs related to incorrect/lacking error checking triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

3 participants