-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Fix annotation for Optional[BroadcastingListx] #20866
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
Conversation
| out = avg_pool2d(input.pow(norm_type), kernel_size, padding=0, ceil_mode=ceil_mode) | ||
| kw, kh = _pair(kernel_size) | ||
| if stride is None: | ||
| stride = torch.jit.annotate(List[int], kernel_size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modest question: Why did we annotate this as a List[int] instead of a BroadcastingList2[int]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried :P but it turns out BroadcastingList2[int] is not a valid option in torch.jit.annotate. talked to @driazati about it, it's mainly because BroadcastingList<x> is a "hacky" workaround and we only supports x up to 5.
|
Looks reasonable I guess? I don't know enough about what annotate does to be able to properly review this. Let me know if you want me to figure out what exactly it's doing. |
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ailzhang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
I think the integration tests are still failing: These are the ones that pass |
|
For reference, this is the sort of call in the tests that leads to pytorch/test/cpp/api/integration.cpp Line 303 in 70ecddf
|
| """ | ||
| if stride is None: | ||
| stride = torch.jit.annotate(List[int], []) | ||
| stride = torch.jit.annotate(List[int], kernel_size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BroadcastingListx[T] gets removed and replaced with List[T] when the types are resolved during schema-making, so this can just be stride = kernel_size
|
Looking at #22032 I'm starting to think that perhaps the C++ functions in A brief summary of the issue is that also expects to be able to pass a single integer for This means that the error checking (and single integer argument expansion) in In that case the JIT would also not need to special case anything (assuming that is easier for the JIT, which I'm not sure about). |
|
Ideally, I suppose, we would add replication logic in a layer right
before we invoke the C++ native implementation. But if this is
difficult to do (it is at least not trivial), the easiest fix is to just
change the C++ kernels to do an expansion. If we have some helper
functions for this, it should really just be a one-liner.
Excerpts from Stefan Krah's message of 2019-06-21 02:29:29 -0700:
… Looking at #22032 I'm starting to think that perhaps the C++ functions in `ATen/native` should not expect `IntArrayRef`s of a guaranteed size.
A brief summary of the issue is that
https://github.com/pytorch/examples/blob/1de2ff9338bacaaffa123d03ce53d7522d5dcc2e/cpp/mnist/mnist.cpp#L40
also expects to be able to pass a single integer for `kernel_size`.
This means that the error checking in `DilatedMaxPool2d` that is currently classified as a workaround could be part of the published C++ API.
In that case the JIT would also not need to special case anything (assuming that is easier for the JIT, which I'm not sure about).
|
|
Yea I agree with @ezyang . Basically this PR ensured we do the expansion for users in JIT. To provide the same benefit for our cpp frontend users, I think it's preferred to be in our codegen so that we don't have to handle this in every kernel. I remember I made some progress on that part, I will try to get back to it this week. cc @yf225 to see if he has a better idea here. |
|
Actually I realized codegen is not enough if we want to provide the same convenience to cpp frontend users.
On the other hand, I think it might be reasonable to expect all inputs from JIT frontend to be properly initialized, as JIT is supposed to be statically typed (it doesn't take |
|
As a short term fix, I don't mind just going through and fixing the ATen implementations to do expansion as necessary. (It will be good to write some utility functions first so we aren't doing hella copy pasting). If we were going to solve this by codegen, I would have imagined some sort of stub function that interposes between Tensor::foo and at::native::foo that does expansions and then passes things along. But I do agree this is a bit involved, and maybe better to just do the simple thing. And yes, if we do force C++ to handle expansion, we should remove the logic from JIT and PyTorch. The thing that I am concerned about is that we had better do a good job auditing and testing all the places we need to do this, or there are going to be a lot of segfaults in our future. |
|
Thanks @ezyang ! Yea I totally agree some helper functions would be best short term solution. cc @skrah maybe you can set these up in one PR so that they're consistently used in TH porting? |
#20691 discussion.
Main problem here is if
xis marked asOptional[BroadcastingList2[int]], when it's None, JIT has no way to know how to construct aint[2]out of it. So we will have to annotate it properly in python.