torch.func.functional_call
- torch.func.functional_call(module, parameter_and_buffer_dicts, args=None, kwargs=None, *, tie_weights=True, strict=
Performs a functional call on the module by replacing the module parameters and buffers with the provided ones.
An example of passing multiple dictionaries
a = (
{"weight": torch.ones(1, 1)},
{"buffer": torch.zeros(1)},
) # two separate dictionaries
mod = nn.Bar(1, 1) # return self.weight @ x + self.buffer
And here is an example of applying the grad transform over the parameters of a model.
import torch
import torch.nn as nn
from torch.func import functional_call, grad
x = torch.randn(4, 3)
t = torch.randn(4, 3)
module (torch.nn.Module) – the module to call
parameters_and_buffer_dicts (Dict[str, Tensor] or tuple of Dict[str, Tensor]) – the parameters that will be used in the module call. If given a tuple of dictionaries, they must have distinct keys so that all dictionaries can be used together
args () – arguments to be passed to the module call. If not a tuple, considered a single argument.
This means that the user cannot call grad_weight.backward(). However, if they don’t need autograd tracking
outside of the transforms, this will result in less memory usage and faster speeds.
kwargs (dict) – keyword arguments to be passed to the module call
tie_weights (bool, optional) – If True, then parameters and buffers tied in the original model will be treated as tied in the reparameterized version. Therefore, if True and different values are passed for the tied parameters and buffers, it will error. If False, it will not respect the originally tied parameters and buffers unless the values passed for both weights are the same. Default: True.
strict (bool, optional) – If True, then the parameters and buffers passed in must match the parameters and buffers in the original module. Therefore, if True and there are any missing or unexpected keys, it will error. Default: False.
the result of calling module.
Any