Shortcuts

torch.mutation

user_input_mutation

Note

Tags: torch.mutation

Support Level: SUPPORTED

Original source code:

# 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()

Result:

ExportedProgram:
    class GraphModule(torch.nn.Module):
        def forward(self, x: "f32[3, 2]"):
             # File: /opt/conda/envs/py_3.8/lib/python3.8/site-packages/torch/_export/db/examples/user_input_mutation.py:18 in forward, code: x.mul_(2)
            mul: "f32[3, 2]" = torch.ops.aten.mul.Tensor(x, 2);  x = None

             # File: /opt/conda/envs/py_3.8/lib/python3.8/site-packages/torch/_export/db/examples/user_input_mutation.py:19 in forward, code: return x.cos()
            cos: "f32[3, 2]" = torch.ops.aten.cos.default(mul)
            return (mul, 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_INPUT_MUTATION: 6>, arg=TensorArgument(name='mul'), target='x'), OutputSpec(kind=<OutputKind.USER_OUTPUT: 1>, arg=TensorArgument(name='cos'), target=None)])
Range constraints: {}

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources