-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Make torch.norm consistent with numpy #40924
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
Make torch.norm consistent with numpy #40924
Conversation
💊 CI failures summary and remediationsAs of commit c080981 (more details on the Dr. CI page):
🕵️ 3 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages (reran 1 job to discount flakiness):
|
|
I'm seeing some issues with some of the JIT tests. Apparently, the TorchScript is calling the vector norm function when it should be calling the matrix norm. For instance, when I run the following test, (I added a print() to show the tensor being operated on) the non-JIT reference function that the test calls gives the correct matrix norm result of I'm not sure yet how to fix it. I have a hunch that my changes to |
I checked what the JIT tests were expecting, and they do expect the correct thing. When _norm_matrix is supposed to be used, the non-JIT reference function is actually calling the _norm_matrix function. The JITed function is really where the failure is. |
|
Looks like some of the CI builds can't use SVD: |
|
You can add |
Without knowing any of the context, it is totally reasonable to disable the tests when MAGMA is not available, we have plenty of tests that do this. Whether or not you should do it now: isn't this a bc breaking change? You have bigger fish to fry than whether or not it works without MAGMA :) |
|
This is BC breaking right? What's the deprecation plan (if there is one?) Can we get more detail about it in the PR summary? |
Yep, agreed. Yes, this is BC breaking. As for a deprecation plan, I'm not entirely sure what it should be, but I believe you've recommended something like this in the past:
Do you think that's a fair plan in this case, @ezyang ? Alternatively, we could do something like this (just brainstorming, maybe not a good idea):
|
6e358b1 to
155dcaa
Compare
|
It looks like the reason for the JIT issue I'm seeing is that when For that call, in the matrix norm Which is the exact same script that gets generated for the corresponding vector norm case. Since that script does not tell the compilation unit whether Evidently, nuclear and frobenius norm have this same issue, because those test cases are registered in |
d1159dc to
a959d21
Compare
|
Sparse tensor inputs to the original implementation of
My current changes broke all sparse tensor support, so at the minimum, I'll need to fix it to behave like it did before. Although I should probably throw an error if It would be nice to add support for sparse tensors in all cases (at least for everything except nuclear norm), but that should be done in a future PR. |
Yes, this plan is reasonable. You may also consider exposing the new behavior in some way (as you suggest below) so people can opt into it early. |
Make torch.norm interface consistent with numpy
1462c85 to
d977051
Compare
|
I noticed that there are no tests confirming that Here's what I get when I run it: Click to expandSo vector norms with Click to expandNevertheless, I will add tests to this PR for the cases that give correct results, since we really should have those tests. |
8a20124 to
57622f0
Compare
|
@ezyang or @mruberry , I have just two quick questions remaining for this PR:
|
Don't worry about it for this PR. If you want, raise an error in these cases.
Maybe, but we should probably keep p working to avoid gratuitously breaking BC. |
Hey Kurt, sorry it took me a couple days to jump in here. My question is: if our plan of record is to support a torch.linalg namespace with torch.linalg.norm, should we modify the existing torch.norm at all (except for it to throw a deprecation warning that users should use torch.linalg.norm instead?) If we do still want to modify torch.norm, I suppose we don't have any way to prevent silent breakages other than hoping the user sees a warning? |
Oh darn, I misunderstood the implications of Thinking from the perspective of the user, I think your suggestion makes sense. It does seem better to leave the original |
No worries, I think I misunderstood your response, too ;) Let's think about it for a day before committing to any plan of action? I don't want to rush your thinking. Yes, I'm planning on building torch.linalg for 1.7 and should have it available in a few weeks. I'll move that timeline up to unblock you, in case that's the direction you decide to go in. |
Sounds good. I'll tally up the changes I would have to make to support this plan. |
To be clear, torch.linalg.norm would be a totally new function. It could internally reuse parts of the existing norm functions if desired, but its goal is to be consistent with NumPy. |
|
@mruberry, I do think that avoiding changes to the
Also, vector norm with sparse tensors was only supported if doing a full reduction with It seems like these functionality changes should still be made, even if we don't change the |
Anything non-BC breaking sounds great, of course. Would the idea be to preserve the behavior of the user-facing |
|
Closing this for now. Will open a new PR when |
Summary: **BC-Breaking Note:** BC breaking changes in the case where keepdim=True. Before this change, when calling `torch.norm` with keepdim=True and p='fro' or p=number, leaving all other optional arguments as their default values, the keepdim argument would be ignored. Also, any time `torch.norm` was called with p='nuc', the result would have one fewer dimension than the input, and the dimensions could be out of order depending on which dimensions were being reduced. After the change, for each of these cases, the result has the same number and order of dimensions as the input. **PR Summary:** * Fix keepdim behavior * Throw descriptive errors for unsupported sparse norm args * Increase unit test coverage for these cases and for complex inputs These changes were taken from part of PR #40924. That PR is not going to be merged because it overrides `torch.norm`'s interface, which we want to avoid. But these improvements are still useful. Issue #24802 Pull Request resolved: #41956 Reviewed By: albanD Differential Revision: D22837455 Pulled By: mruberry fbshipit-source-id: 509ecabfa63b93737996f48a58c7188b005b7217
Implement matrix norm for orders +/- 1, 2, inf
This PR contains BC-breaking changes to
torch.norm,torch.functional.norm, and its underlyingatenfunctions. The deprecation plan is to add a warning totorch.functional.norm, explaining what will change. This warning will be added in a different PR (#41193), and should exist in PyTorch 1.6.0. Then, we can release thenormchanges in PyTorch 1.7.0.Issue #24802