捷徑

torch.mutation

user_input_mutation

注意

標籤: torch.mutation

支援等級: 已支援

原始碼

# mypy: allow-untyped-defs
import torch


class UserInputMutation(torch.nn.Module):
    """
    Directly mutate user input in forward
    """

    def forward(self, x):
        x.mul_(2)
        return x.cos()


example_args = (torch.randn(3, 2),)
tags = {"torch.mutation"}
model = UserInputMutation()


torch.export.export(model, example_args)

結果

ExportedProgram:
    class GraphModule(torch.nn.Module):
        def forward(self, x: "f32[3, 2]"):
                 mul_: "f32[3, 2]" = torch.ops.aten.mul_.Tensor(x, 2);  x = None

                 cos: "f32[3, 2]" = torch.ops.aten.cos.default(mul_);  mul_ = None
            return (cos,)

Graph signature: ExportGraphSignature(input_specs=[InputSpec(kind=<InputKind.USER_INPUT: 1>, arg=TensorArgument(name='x'), target=None, persistent=None)], output_specs=[OutputSpec(kind=<OutputKind.USER_OUTPUT: 1>, arg=TensorArgument(name='cos'), target=None)])
Range constraints: {}

文件

存取 PyTorch 的完整開發者文件

查看文件

教學

取得適用於初學者和進階開發者的深入教學

查看教學

資源

尋找開發資源並獲得問題解答

查看資源