Skip to content

vmap: temporarily disable support for random functions - #42617

Closed
zou3519 wants to merge 3 commits into
gh/zou3519/281/basefrom
gh/zou3519/281/head
Closed

zou3519 wants to merge 3 commits into
gh/zou3519/281/basefrom
gh/zou3519/281/head

Conversation

@zou3519

@zou3519 zou3519 commented Aug 5, 2020 •

Copy link
Copy Markdown
Contributor

Stack from ghstack:

While we figure out the random plan, I want to initially disable
support for random operations. This is because there is an ambiguity in
what randomness means. For example,

tensor = torch.zeros(B0, 1)
vmap(lambda t: t.normal_())(tensor)

in the above example, should tensor[0] and tensor[1] be equal (i.e.,
use the same random seed), or should they be different?

The mechanism for disabling random support is as follows:

  • We add a new dispatch key called VmapMode
  • Whenever we're inside vmap, we enable VmapMode for all tensors.
    This is done via at::VmapMode::increment_nesting and
    at::VmapMode::decrement_nesting.
  • DispatchKey::VmapMode's fallback kernel is the fallthrough kernel.
  • We register kernels that raise errors for all random functions on
    DispatchKey::VmapMode. This way, whenever someone calls a random
    function on any tensor (not just BatchedTensors) inside of a vmap block,
    an error gets thrown.

Test Plan:

  • pytest test/test_vmap.py -v -k "Operators"

Differential Revision: D22954840

While we figure out the random plan, I want to initially disable
support for random operations. This is because there is an ambiguity in
what randomness means. For example,

```
tensor = torch.zeros(B0, 1)
vmap(lambda t: t.normal_())(tensor)
```

in the above example, should tensor[0] and tensor[1] be equal (i.e.,
use the same random seed), or should they be different?

The mechanism for disabling random support is as follows:
- We add a new dispatch key called VmapMode
- Whenever we're inside vmap, we enable VmapMode for all tensors.
This is done via at::VmapMode::increment_nesting and
at::VmapMode::decrement_nesting.
- DispatchKey::VmapMode's fallback kernel is the fallthrough kernel.
- We register kernels that raise errors for all random functions on
DispatchKey::VmapMode. This way, whenever someone calls a random
function on any tensor (not just BatchedTensors) inside of a vmap block,
an error gets thrown.

Test Plan:
- pytest test/test_vmap.py -v -k "Operators"

[ghstack-poisoned]
zou3519 added a commit that referenced this pull request Aug 5, 2020
While we figure out the random plan, I want to initially disable
support for random operations. This is because there is an ambiguity in
what randomness means. For example,

```
tensor = torch.zeros(B0, 1)
vmap(lambda t: t.normal_())(tensor)
```

in the above example, should tensor[0] and tensor[1] be equal (i.e.,
use the same random seed), or should they be different?

The mechanism for disabling random support is as follows:
- We add a new dispatch key called VmapMode
- Whenever we're inside vmap, we enable VmapMode for all tensors.
This is done via at::VmapMode::increment_nesting and
at::VmapMode::decrement_nesting.
- DispatchKey::VmapMode's fallback kernel is the fallthrough kernel.
- We register kernels that raise errors for all random functions on
DispatchKey::VmapMode. This way, whenever someone calls a random
function on any tensor (not just BatchedTensors) inside of a vmap block,
an error gets thrown.

Test Plan:
- pytest test/test_vmap.py -v -k "Operators"

ghstack-source-id: f24a470
Pull Request resolved: #42617
@zou3519
zou3519 requested review from cpuhrsch and ezyang August 5, 2020 19:54
@dr-ci

dr-ci Bot commented Aug 5, 2020 •

Copy link
Copy Markdown

💊 CI failures summary and remediations

As of commit adbd0a8 (more details on the Dr. CI page):


✅ None of the CI failures appear to be your fault 💚



🚧 1 fixed upstream failure:

These were probably caused by upstream breakages that were already fixed.

Please rebase on the viable/strict branch (expand for instructions)

Since your merge base is older than viable/strict, run these commands:

git fetch https://github.com/pytorch/pytorch viable/strict
git rebase FETCH_HEAD

Check out the recency history of this "viable master" tracking branch.


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 13 times.

// CppFunction::makeNamedNotSupported() to avoid listing out the types of everything.
// However, registering e.g. CppFunction::makeNamedNotSupported() as an implementation
// only works for operators that support boxing.
#define TENSOROPTIONS c10::optional<c10::ScalarType>, c10::optional<c10::Layout>, c10::optional<c10::Device>, c10::optional<bool>

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.

cc @smessmer @bhosmer @gchanan Downside of exploded tensor options

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 really wanted to do an e.g. decltype(native::randn) here to avoid listing out the types. However there were two problems with that:

  1. we've got a lot of overloads
  2. native::randn takes const TensorOptions&, so a decltype wouldn't give the correct thing

Comment thread aten/src/ATen/VmapMode.h
// we are currently inside. That number is known as the `vmap level`.
// VmapMode is used in the implementation of the Python `torch.vmap` API.
//
// NOTE: this is NOT the c++ api for torch.vmap. That doesn't exist yet.

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.

If this isn't intended to be public API, put it in a private namespace like impl

// NB: I'd really like to register a special kernel like
// CppFunction::makeNamedNotSupported() to avoid listing out the types of everything.
// However, registering e.g. CppFunction::makeNamedNotSupported() as an implementation
// only works for operators that support boxing.

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 seems like an oversight; there's no reason this shouldn't work. File us a bug?

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.

Bug filed to #42818

While we figure out the random plan, I want to initially disable
support for random operations. This is because there is an ambiguity in
what randomness means. For example,

```
tensor = torch.zeros(B0, 1)
vmap(lambda t: t.normal_())(tensor)
```

in the above example, should tensor[0] and tensor[1] be equal (i.e.,
use the same random seed), or should they be different?

The mechanism for disabling random support is as follows:
- We add a new dispatch key called VmapMode
- Whenever we're inside vmap, we enable VmapMode for all tensors.
This is done via at::VmapMode::increment_nesting and
at::VmapMode::decrement_nesting.
- DispatchKey::VmapMode's fallback kernel is the fallthrough kernel.
- We register kernels that raise errors for all random functions on
DispatchKey::VmapMode. This way, whenever someone calls a random
function on any tensor (not just BatchedTensors) inside of a vmap block,
an error gets thrown.

Test Plan:
- pytest test/test_vmap.py -v -k "Operators"

Differential Revision: [D22954840](https://our.internmc.facebook.com/intern/diff/D22954840)

[ghstack-poisoned]
While we figure out the random plan, I want to initially disable
support for random operations. This is because there is an ambiguity in
what randomness means. For example,

```
tensor = torch.zeros(B0, 1)
vmap(lambda t: t.normal_())(tensor)
```

in the above example, should tensor[0] and tensor[1] be equal (i.e.,
use the same random seed), or should they be different?

The mechanism for disabling random support is as follows:
- We add a new dispatch key called VmapMode
- Whenever we're inside vmap, we enable VmapMode for all tensors.
This is done via at::VmapMode::increment_nesting and
at::VmapMode::decrement_nesting.
- DispatchKey::VmapMode's fallback kernel is the fallthrough kernel.
- We register kernels that raise errors for all random functions on
DispatchKey::VmapMode. This way, whenever someone calls a random
function on any tensor (not just BatchedTensors) inside of a vmap block,
an error gets thrown.

Test Plan:
- pytest test/test_vmap.py -v -k "Operators"

Differential Revision: [D22954840](https://our.internmc.facebook.com/intern/diff/D22954840)

[ghstack-poisoned]
zou3519 added a commit that referenced this pull request Aug 10, 2020
While we figure out the random plan, I want to initially disable
support for random operations. This is because there is an ambiguity in
what randomness means. For example,

```
tensor = torch.zeros(B0, 1)
vmap(lambda t: t.normal_())(tensor)
```

in the above example, should tensor[0] and tensor[1] be equal (i.e.,
use the same random seed), or should they be different?

The mechanism for disabling random support is as follows:
- We add a new dispatch key called VmapMode
- Whenever we're inside vmap, we enable VmapMode for all tensors.
This is done via at::VmapMode::increment_nesting and
at::VmapMode::decrement_nesting.
- DispatchKey::VmapMode's fallback kernel is the fallthrough kernel.
- We register kernels that raise errors for all random functions on
DispatchKey::VmapMode. This way, whenever someone calls a random
function on any tensor (not just BatchedTensors) inside of a vmap block,
an error gets thrown.

Test Plan:
- pytest test/test_vmap.py -v -k "Operators"

ghstack-source-id: 31f4aa8
Pull Request resolved: #42617
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@zou3519 merged this pull request in e8f4b04.

@facebook-github-bot
facebook-github-bot deleted the gh/zou3519/281/head branch August 15, 2020 14:17
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.

4 participants