Skip to content

vmap fallback kernel - #41943

Closed
zou3519 wants to merge 11 commits into
gh/zou3519/274/basefrom
gh/zou3519/274/head
Closed

zou3519 wants to merge 11 commits into
gh/zou3519/274/basefrom
gh/zou3519/274/head

Conversation

@zou3519

@zou3519 zou3519 commented Jul 23, 2020 •

Copy link
Copy Markdown
Contributor

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 op on all of the corresponding slices to produce slices
of the outputs. The output slices then get torch.stacked 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

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]
@dr-ci

dr-ci Bot commented Jul 23, 2020 •

Copy link
Copy Markdown

💊 CI failures summary and remediations

As 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.

See how this bot performed.

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]
zou3519 added a commit that referenced this pull request Jul 23, 2020
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
Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
TORCH_INTERNAL_ASSERT(stack.size() == 1)
output_shards.push_back(stack[0].toTensor());
}
auto flat_output = at::stack(output_shards);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RIP. cc @albanD

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@zou3519
zou3519 requested review from cpuhrsch, ezyang and izdeby July 23, 2020 20:41
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]
zou3519 added a commit that referenced this pull request Jul 24, 2020
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
Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
Comment thread test/test_vmap.py
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@ezyang

ezyang commented Jul 27, 2020

Copy link
Copy Markdown
Contributor

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.

@ezyang
ezyang requested review from bhosmer and smessmer July 27, 2020 18:55
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]
zou3519 added a commit that referenced this pull request Jul 27, 2020
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]
zou3519 added a commit that referenced this pull request Jul 28, 2020
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
Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
@zou3519

zou3519 commented Jul 28, 2020

Copy link
Copy Markdown
Contributor Author

I updated the strategy to do stack push-pop

@zou3519
zou3519 requested a review from ezyang July 28, 2020 15:16
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]
zou3519 added a commit that referenced this pull request Jul 29, 2020
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmmmmm

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not true in general

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that'll get fixed in the future :) (probably one of the next three diffs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread aten/src/ATen/BatchedFallback.cpp Resolved
zou3519 added 3 commits July 31, 2020 08:49
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]
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@zou3519 merged this pull request in 2f8d5b6.

@facebook-github-bot
facebook-github-bot deleted the gh/zou3519/274/head branch August 7, 2020 14:26
MauiDesign pushed a commit to MauiDesign/PyTorchPyTorch that referenced this pull request Aug 16, 2020
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants