快捷方式

从 functorch 迁移到 torch.func

torch.func,以前称为“functorch”,是 JAX-like 可组合的 PyTorch 函数转换。

functorch 最初是一个在 pytorch/functorch 存储库中的外部库。我们的目标一直是将 functorch 直接上游到 PyTorch,并将其作为 PyTorch 的核心库提供。

作为上游的最后一步,我们决定从顶级包 (functorch) 迁移到 PyTorch 的一部分,以反映函数转换如何直接集成到 PyTorch 核心。从 PyTorch 2.0 开始,我们弃用 import functorch,并要求用户迁移到最新的 API,我们将继续维护这些 API。 import functorch 将保留一段时间以保持向后兼容性。

函数转换

以下 API 是以下 functorch API 的直接替换。它们完全向后兼容。

functorch API

PyTorch API(截至 PyTorch 2.0)

functorch.vmap

torch.vmap()torch.func.vmap()

functorch.grad

torch.func.grad()

functorch.vjp

torch.func.vjp()

functorch.jvp

torch.func.jvp()

functorch.jacrev

torch.func.jacrev()

functorch.jacfwd

torch.func.jacfwd()

functorch.hessian

torch.func.hessian()

functorch.functionalize

torch.func.functionalize()

此外,如果您使用的是 torch.autograd.functional API,请尝试使用 torch.func 等效项。 torch.func 函数转换在许多情况下更具可组合性和性能更高。

torch.autograd.functional API

torch.func API(截至 PyTorch 2.0)

torch.autograd.functional.vjp()

torch.func.grad()torch.func.vjp()

torch.autograd.functional.jvp()

torch.func.jvp()

torch.autograd.functional.jacobian()

torch.func.jacrev()torch.func.jacfwd()

torch.autograd.functional.hessian()

torch.func.hessian()

NN 模块实用程序

我们更改了 API,以便将函数转换应用于 NN 模块,使其更符合 PyTorch 的设计理念。新的 API 与之前不同,请仔细阅读本节内容。

functorch.make_functional

torch.func.functional_call()functorch.make_functionalfunctorch.make_functional_with_buffers 的替代品。但是,它不是直接替换。

如果您时间紧迫,可以使用 此 gist 中的辅助函数 来模拟 functorch.make_functional 和 functorch.make_functional_with_buffers 的行为。我们建议直接使用 torch.func.functional_call(),因为它是一个更明确且灵活的 API。

具体来说,functorch.make_functional 返回一个函数式模块和参数。函数式模块接受参数和模型的输入作为参数。 torch.func.functional_call() 允许使用新的参数、缓冲区和输入来调用现有模块的前向传递。

以下是如何使用 functorch 和 torch.func 计算模型参数梯度的示例。

# ---------------
# using functorch
# ---------------
import torch
import functorch
inputs = torch.randn(64, 3)
targets = torch.randn(64, 3)
model = torch.nn.Linear(3, 3)

fmodel, params = functorch.make_functional(model)

def compute_loss(params, inputs, targets):
    prediction = fmodel(params, inputs)
    return torch.nn.functional.mse_loss(prediction, targets)

grads = functorch.grad(compute_loss)(params, inputs, targets)

# ------------------------------------
# using torch.func (as of PyTorch 2.0)
# ------------------------------------
import torch
inputs = torch.randn(64, 3)
targets = torch.randn(64, 3)
model = torch.nn.Linear(3, 3)

params = dict(model.named_parameters())

def compute_loss(params, inputs, targets):
    prediction = torch.func.functional_call(model, params, (inputs,))
    return torch.nn.functional.mse_loss(prediction, targets)

grads = torch.func.grad(compute_loss)(params, inputs, targets)

以下是如何计算模型参数雅可比矩阵的示例。

# ---------------
# using functorch
# ---------------
import torch
import functorch
inputs = torch.randn(64, 3)
model = torch.nn.Linear(3, 3)

fmodel, params = functorch.make_functional(model)
jacobians = functorch.jacrev(fmodel)(params, inputs)

# ------------------------------------
# using torch.func (as of PyTorch 2.0)
# ------------------------------------
import torch
from torch.func import jacrev, functional_call
inputs = torch.randn(64, 3)
model = torch.nn.Linear(3, 3)

params = dict(model.named_parameters())
# jacrev computes jacobians of argnums=0 by default.
# We set it to 1 to compute jacobians of params
jacobians = jacrev(functional_call, argnums=1)(model, params, (inputs,))

请注意,为了节省内存,您应该只保留参数的单个副本。 model.named_parameters() 不会复制参数。如果您在模型训练中就地更新模型的参数,那么作为模型的 nn.Module 将拥有参数的单个副本,一切正常。

但是,如果您想将参数保存在字典中并进行非就地更新,那么参数将有两个副本:字典中的副本和model中的副本。在这种情况下,您应该将model转换为元设备,通过model.to('meta')来使其不再持有内存。

functorch.combine_state_for_ensemble

请使用torch.func.stack_module_state() 而不是 functorch.combine_state_for_ensemble torch.func.stack_module_state() 返回两个字典,一个包含堆叠的参数,另一个包含堆叠的缓冲区,这些字典可以与 torch.vmap()torch.func.functional_call() 一起用于集成。

例如,以下是如何在一个非常简单的模型上进行集成的一个示例

import torch
num_models = 5
batch_size = 64
in_features, out_features = 3, 3
models = [torch.nn.Linear(in_features, out_features) for i in range(num_models)]
data = torch.randn(batch_size, 3)

# ---------------
# using functorch
# ---------------
import functorch
fmodel, params, buffers = functorch.combine_state_for_ensemble(models)
output = functorch.vmap(fmodel, (0, 0, None))(params, buffers, data)
assert output.shape == (num_models, batch_size, out_features)

# ------------------------------------
# using torch.func (as of PyTorch 2.0)
# ------------------------------------
import copy

# Construct a version of the model with no memory by putting the Tensors on
# the meta device.
base_model = copy.deepcopy(models[0])
base_model.to('meta')

params, buffers = torch.func.stack_module_state(models)

# It is possible to vmap directly over torch.func.functional_call,
# but wrapping it in a function makes it clearer what is going on.
def call_single_model(params, buffers, data):
    return torch.func.functional_call(base_model, (params, buffers), (data,))

output = torch.vmap(call_single_model, (0, 0, None))(params, buffers, data)
assert output.shape == (num_models, batch_size, out_features)

functorch.compile

我们不再支持 functorch.compile(也称为 AOTAutograd)作为 PyTorch 中编译的前端;我们已将 AOTAutograd 集成到 PyTorch 的编译故事中。如果您是用户,请使用 torch.compile() 代替。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并获得问题的解答

查看资源