快捷方式

torch.map

dynamic_shape_map

注意

标签:torch.dynamic-shape, torch.map

支持级别:SUPPORTED

原始源代码

# mypy: allow-untyped-defs
import torch

from functorch.experimental.control_flow import map

class DynamicShapeMap(torch.nn.Module):
    """
    functorch map() maps a function over the first tensor dimension.
    """

    def forward(self, xs, y):
        def body(x, y):
            return x + y

        return map(body, xs, y)

example_args = (torch.randn(3, 2), torch.randn(2))
tags = {"torch.dynamic-shape", "torch.map"}
model = DynamicShapeMap()


torch.export.export(model, example_args)

结果

ExportedProgram:
    class GraphModule(torch.nn.Module):
        def forward(self, xs: "f32[3, 2]", y: "f32[2]"):
                 body_graph_0 = self.body_graph_0
            map_impl = torch.ops.higher_order.map_impl(body_graph_0, [xs], [y]);  body_graph_0 = xs = y = None
            getitem: "f32[3, 2]" = map_impl[0];  map_impl = None
            return (getitem,)

        class body_graph_0(torch.nn.Module):
            def forward(self, xs: "f32[2]", y: "f32[2]"):
                         add: "f32[2]" = torch.ops.aten.add.Tensor(xs, y);  xs = y = None
                return (add,)

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

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取适用于初学者和高级开发人员的深入教程

查看教程

资源

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

查看资源