Repository navigation
vmap fallback kernel - #41943
vmap fallback kernel#41943zou3519 wants to merge 11 commits into
Conversation
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` [ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit 5d4afa6 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group. This comment has been revised 19 times. |
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` ghstack-source-id: 4c7daae Pull Request resolved: #41943
| TORCH_INTERNAL_ASSERT(stack.size() == 1) | ||
| output_shards.push_back(stack[0].toTensor()); | ||
| } | ||
| auto flat_output = at::stack(output_shards); |
There was a problem hiding this comment.
The stack means that the fallback kernel won't be as performant as a user deciding to use a for-loop rather than vmap. However, the long-term state for vmap is to not rely on the fallback (and instead have batching rules defined on the operations) so I am not too worried about the performance.
Problem
Consider the following model:
def model(example):
example = F.conv(example, ...)
example = F.relu(example, ...)
example = F.linear(example, ...)
return tensor
If the user wanted to push multiple examples via a Python for-loop, they’d do the following:
examples = torch.randn(N, ...)
results = []
for example in examples.unbind(0):
results.append(model(example))
output = torch.stack(results)
Now, let’s assume that no batching rules were implemented for F.conv, F.relu, and F.linear. The vmap “for-loop” fallback would do the following, for each operation:
- unbind the examples along the batch dimension
- run the operator for each example
- stack the results
In other words, semantically, vmap(model)(examples) is equivalent to:
examples = torch.stack([F.conv(example, ...) for example in examples])
examples = torch.stack([F.relu(example, ...) for example in examples])
examples = torch.stack([F.linear(example, ...) for example in examples])
All of the torch.stack make the vmap “for-loop” fallback slower than the python for-loop, which is not very good for vmap.
There was a problem hiding this comment.
The obvious fix is to not stack: instead, use out to write samples directly into a preallocated output. But to preallocate the output, you need a way of computing what the size of the computation will be on one output, which is not so easy to do right now. One hacky way to do it would be to run on one example, then resize the output so it's large enough to fit the rest of the examples, and then run the loop. It'll be faster than stack. Then if we have working meta functions, you can use those to compute what the output size should be after the fact.
There was a problem hiding this comment.
We can't actually use the out= variant of the function because they doesn't support autograd. Because vmap occurs above autograd in the current design, vmap must desugar into operations that are compatible with autograd.
There was a problem hiding this comment.
Note that this will depend on the overall model: a short model (few ops) won't see much difference while a deeper model will be more impacted.
Also I guess that the output size vs complexity of the function itself will have a big impact here.
But in general, for functions with inplace versions (foo_()), we could do this and keep autograd. But we will need a list of such functions and register a different fallback kernel for these.
For other functions, I can't think of a better way to do it :/
There was a problem hiding this comment.
@albanD what is being asked for here is a little different. Imagine:
x1 = torch.randn(2, requires_grad=True)
x2 = torch.randn(2, requires_grad=True)
x3 = torch.randn(2, requires_grad=True)
y = torch.empty(3, 2)
x1.add(w, out=y[0])
x2.add(w, out=y[1])
x3.add(w, out=y[2])
y.backward()
It would be cool if this worked.
There was a problem hiding this comment.
Yes it would :D
As mentioned in the out= autograd proposal here, that would work just fine.
Unfortunately, detecting these valid cases and implementing this overall is not going to be done any time soon I think.
The version for the foo_() variants would work with the current code base though (even though it won't work with as many ops).
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` ghstack-source-id: 0133f27 Pull Request resolved: #41943
| def test_fallback_masked_fill(self): | ||
| # NB: One day we will implement a batching rule for index_add | ||
| # If/when we do, this test should be replaced to test the fallback | ||
| # path on another operator to avoid bitrot. |
There was a problem hiding this comment.
It would be better if you could detect if this situation occurred :) (Maybe you could use one of the apis used in test_dispatch.py, or a new API, to test that index_add doesn't have an explicit VMap key installed. Not sure it's worth the faffing about.)
There was a problem hiding this comment.
We can detect it actually -- the fallback emits a warning message and we test for the warning message in test_fallback_masked_fill. If someone adds a batching rule for masked_fill, then the test will fail and the developer will read this comment :)
|
This is really nifty stuff. I'm adding other composability peeps in case they have more opinions. I'm happy to accept to unblock but for a "normal" code review I think I would definitely prefer it if we did a different stack push-pop strategy. |
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` Differential Revision: [D22764103](https://our.internmc.facebook.com/intern/diff/D22764103) [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` ghstack-source-id: 405e264 Pull Request resolved: #41943
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` Differential Revision: [D22764103](https://our.internmc.facebook.com/intern/diff/D22764103) [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` ghstack-source-id: 39e87b6 Pull Request resolved: #41943
|
I updated the strategy to do stack push-pop |
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` Differential Revision: [D22764103](https://our.internmc.facebook.com/intern/diff/D22764103) [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` ghstack-source-id: e385c45 Pull Request resolved: #41943
|
|
||
| op.callBoxed(stack); | ||
|
|
||
| // We assume there is a single tensor return |
There was a problem hiding this comment.
This is probably not true in general
There was a problem hiding this comment.
Yeah, that'll get fixed in the future :) (probably one of the next three diffs)
There was a problem hiding this comment.
I tried implementing support for multiple returns but I ran into #42378 (there's a minimal repro in the example). Should an operator with multiple returns push all of them onto the stack?
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` Differential Revision: [D22764103](https://our.internmc.facebook.com/intern/diff/D22764103) [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` Differential Revision: [D22764103](https://our.internmc.facebook.com/intern/diff/D22764103) [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` Differential Revision: [D22764103](https://our.internmc.facebook.com/intern/diff/D22764103) [ghstack-poisoned]
If an operator doesn't have a batching rule implemented then we fallback to this implementation. The fallback only works on out-of-place operators that return only tensors with new memory. (e.g., no in-place operators, no view operations). The fallback effectively takes all of the BatchedTensors in `stack`, slices them, and runs `op` on all of the corresponding slices to produce slices of the outputs. The output slices then get `torch.stack`ed to create the final returns. The performance of the fallback is not very good because it introduces an extra copy from stacking the sliced outputs. Because of this, we prefer to write batching rules for operators whenever possible. In the future, I'd like to disable the fallback kernel for random functions until we have a better random story for vmap. I will probably add a blocklist of operators to support that. Test Plan: - `pytest test/test_vmap.py -v` ghstack-source-id: df46a5d Pull Request resolved: pytorch/pytorch#41943
Stack from ghstack:
If an operator doesn't have a batching rule implemented then we fallback
to this implementation. The fallback only works on out-of-place operators
that return only tensors with new memory. (e.g., no in-place operators,
no view operations).
The fallback effectively takes all of the BatchedTensors in
stack,slices them, and runs
opon all of the corresponding slices to produce slicesof the outputs. The output slices then get
torch.stacked to create thefinal returns.
The performance of the fallback is not very good because it introduces
an extra copy from stacking the sliced outputs. Because of this, we prefer
to write batching rules for operators whenever possible.
In the future, I'd like to disable the fallback kernel for random
functions until we have a better random story for vmap. I will probably
add a blocklist of operators to support that.
Test Plan:
pytest test/test_vmap.py -vDifferential Revision: D22764103