torch.func, like JAX, has restrictions around
what can be transformed. In general, JAX’s limitations are that transforms
only work with pure functions: that is, functions where the output is completely
determined by the input and that do not involve side effects (like mutation).
We have a similar guarantee: our transforms work well with pure functions.
However, we do support certain in-place operations. On one hand, writing code
compatible with function transforms may involve changing how you write PyTorch
code, on the other hand, you may find that our transforms let you express things
that were previously difficult to express in PyTorch.
General limitations
All torch.func transforms share a limitation in that a function should not
assign to global variables. Instead, all outputs to a function must be returned
from the function. This restriction comes from how torch.func is implemented:
each transform wraps Tensor inputs in special torch.func Tensor subclasses
that facilitate the transform.
If you are trying to use a torch.autograd API like torch.autograd.grad
or torch.autograd.backward inside of a function being transformed by
vmap() or one of torch.func’s AD transforms (vjp(), jvp(),
jacrev(), jacfwd()), the transform may not be able to transform over it.
If it is unable to do so, you’ll receive an error message.
This is a fundamental design limitation in how PyTorch’s AD support is implemented
and the reason why we designed the torch.func library. Please instead use the torch.func
equivalents of the torch.autograd APIs:
torch.autograd.grad, Tensor.backward -> torch.func.vjp or torch.func.grad
torch.autograd.functional.jvp -> torch.func.jvp
torch.autograd.functional.jacobian -> torch.func.jacrev or torch.func.jacfwd
torch.autograd.functional.hessian ->
vmap limitations
vmap(func) is a transform that returns a function that maps func over
some new dimension of each input Tensor. The mental model for vmap is that it is
like running a for-loop: for pure functions (i.e. in the absence of side
effects), vmap(f)(x) is equivalent to:
torch.stack([f(x_i)forx_iinx.unbind(0)])
Mutation: Arbitrary mutation of Python data structures
In the presence of side effects, vmap() no longer acts like it is running
a for-loop. For example, the following function:
You might be here due to receiving an error about vmap-incompatible in-place
operations. vmap() will raise an error if it encounters an unsupported PyTorch
in-place operation and it will succeed otherwise. Unsupported operations
are those that would cause a Tensor with more elements to be written to a
Tensor with fewer elements. Here’s an example of how this can occur:
deff(x,y):x.add_(y)returnxx=torch.randn
Mutation: out= PyTorch Operations
vmap() doesn’t support the out= keyword argument in PyTorch operations.
It will error out gracefully if it encounters that in your code.
This is not a fundamental limitation; we could theoretically support this in the
future but we have chosen not to for now.
Data-dependent Python control flow
We don’t yet support vmap over data-dependent control flow. Data-dependent
control flow is when the condition of an if-statement, while-loop, or
for-loop is a Tensor that is being vmap’ed over. For example, the
following will raise an error message:
defrelu(x):ifx>0:returnxreturn0x=torch.randn(3
Data-dependent operations (.item())
We do not (and will not) support vmap over a user-defined function that calls
.item() on a Tensor. For example, the following will raise an error message:
deff(x):returnx.item()x=torch.randn(3)vmap(f)(x)
Please try to rewrite your code to not use calls.
Dynamic shape operations (nonzero and friends)
vmap(f) requires that f applied to every “example” in your input
returns a Tensor with the same shape. Operations such as torch.nonzero,
torch.is_nonzero are not supported and will error as a result.
To see why, consider the following example:
xs=torch.tensor([[0,1,2],[0,0
Randomness
The user’s intention when calling a random operation can be unclear. Specifically, some users may want
the random behavior to be the same across batches while others may want it to differ across batches.
To address this, vmap takes a randomness flag.
The flag can only be passed to vmap and can take on 3 values, “error,” “different,” or “same,” defaulting
to error. Under “error” mode, any call to a random function will produce an error asking the user to use
one of the other two flags based on their use case.
Under “different” randomness, elements in a batch produce different random values. For instance,
defadd_noise(x):y=torch.randn(())# y will be different across the batchreturnx+yx=torch.ones(3)result=vmap(add_noise,randomness="different")(x)# we get 3 different values
Under “same” randomness, elements in a batch produce same random values. For instance,
defadd_noise(x):y=torch.randn(())# y will be the same across the batchreturnx+yx=torch.ones(3)result=vmap(add_noise,randomness="same")(x)
torch.func.hessian
torch
.
randn
(
3
,
1
)
lst
=
[
0
,
1
,
2
,
3
]
result
=
vmap
(
f
,
in_dims
=
(
0
,
None
))(
x
,
lst
)
will print “hello!” once and pop only one element from lst.
vmap() executes f a single time, so all side effects only happen once.
This is a consequence of how vmap is implemented. torch.func has a special,
internal BatchedTensor class. vmap(f)(*inputs) takes all Tensor inputs,
turns them into BatchedTensors, and calls f(*batched_tensor_inputs).
BatchedTensor overrides the PyTorch API to produce batched (i.e. vectorized)
behavior for each PyTorch operator.
(
1
)
y
=
torch
.
randn
(
3
,
1
)
# When vmapped over, looks like it has shape [1]
# Raises an error because `x` has fewer elements than `y`.
vmap
(
f
,
in_dims
=
(
None
,
0
))(
x
,
y
)
x is a Tensor with one element, y is a Tensor with three elements.
x+y has three elements (due to broadcasting), but attempting to write
three elements back into x, which only has one element, raises an error
due to attempting to write three elements into a Tensor with a single element.
There is no problem if the Tensor being written to is batched under
vmap() (i.e. it is being vmapped over).
deff(x,y):x.add_(y)returnxx=torch.randn(3,1)y=torch.randn(3,1)expected=x+y# Does not raise an error because x is being vmapped over.vmap(f,in_dims=(0,0))(x,y)asserttorch.allclose(x,expected)
One common fix for this is to replace calls to factory functions with
their “new_*” equivalent. For example:
defdiag_embed(vec):assertvec.dim()==1result=torch.zeros(vec.shape[0],vec.shape[0])result.diagonal().copy_(vec)returnresultvecs=torch.tensor([[0.,1,2],[3.,4,5]])# RuntimeError: vmap: inplace arithmetic(self, *extra_args) is not possible ...vmap(diag_embed)(vecs)
Inside of vmap(), result is a Tensor of shape [3, 3].
However, although vec looks like it has shape [3], vec actually has
underlying shape [2, 3].
It is not possible to copy vec into result.diagonal(), which has
shape [3], because it has too many elements.
Replacing torch.zeros() with Tensor.new_zeros() makes it so that
result has an underlying Tensor of shape [2, 3, 3], so it is now possible
to copy vec, which has underlying shape [2, 3], into result.diagonal().
)
vmap
(
relu
)(
x
)
However, any control flow that is not dependent on the values in vmap’ed
tensors will work:
JAX supports transforming over
data-dependent control flow
using special control flow operators (e.g. jax.lax.cond, jax.lax.while_loop).
We’re investigating adding equivalents of those to PyTorch.
.item()
You may also encounter an error message about using .item() but you might
not have used it. In those cases, it is possible that PyTorch internally is
calling .item() – please file an issue on GitHub and we’ll fix
PyTorch internals.
,
3
]])
vmap
(
torch
.
nonzero
)(
xs
)
torch.nonzero(xs[0]) returns a Tensor of shape 2;
but torch.nonzero(xs[1]) returns a Tensor of shape 1.
We are unable to construct a single Tensor as an output;
the output would need to be a ragged Tensor (and PyTorch does not yet have
the concept of a ragged Tensor).