|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-unsafe |
| 8 | + |
| 9 | +from typing import Dict |
| 10 | + |
| 11 | +import torch |
| 12 | +from executorch.backends.xnnpack.operators.node_visitor import ( |
| 13 | + NodeVisitor, |
| 14 | + register_node_visitor, |
| 15 | +) |
| 16 | +from executorch.backends.xnnpack.serialization.xnnpack_graph_schema import ( |
| 17 | + XNNGraph, |
| 18 | + XNNStaticReshape, |
| 19 | + XNode, |
| 20 | +) |
| 21 | +from executorch.backends.xnnpack.utils.utils import ( |
| 22 | + check_or_raise, |
| 23 | + get_input_node, |
| 24 | + PERM_NCHW_TO_NHWC, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@register_node_visitor |
| 29 | +class ViewCopyVisitor(NodeVisitor): |
| 30 | + target = "aten.view_copy.default" |
| 31 | + |
| 32 | + def __init__(self, *args) -> None: |
| 33 | + super().__init__(*args) |
| 34 | + |
| 35 | + def define_node( |
| 36 | + self, |
| 37 | + node: torch.fx.Node, |
| 38 | + xnn_graph: XNNGraph, |
| 39 | + vals_to_ids: Dict[torch.fx.Node, int], |
| 40 | + debug_handle: int, |
| 41 | + ) -> None: |
| 42 | + self.define_nodes_tensor_inputs_outputs(node, xnn_graph, vals_to_ids) |
| 43 | + |
| 44 | + input_node = get_input_node(node, 0) |
| 45 | + |
| 46 | + # input |
| 47 | + input_id = vals_to_ids[input_node] |
| 48 | + |
| 49 | + # output |
| 50 | + output_id = vals_to_ids[node] |
| 51 | + |
| 52 | + # input shape |
| 53 | + check_or_raise( |
| 54 | + "val" in input_node.meta, |
| 55 | + "Missing val in tensor metadata for input when serializing XNNStaticReshape", |
| 56 | + ) |
| 57 | + |
| 58 | + # output shape |
| 59 | + check_or_raise( |
| 60 | + "val" in node.meta, |
| 61 | + "Missing val in tensor metadata for input when serializing XNNStaticReshape", |
| 62 | + ) |
| 63 | + |
| 64 | + new_shape = node.args[1] |
| 65 | + check_or_raise( |
| 66 | + all(isinstance(d, int) for d in new_shape), |
| 67 | + "Symbolic reshape parameter is not supported in XNNStaticReshape", |
| 68 | + ) |
| 69 | + |
| 70 | + # PyTorch uses -1 for inferred dims, whereas XNNPACK expects 0. |
| 71 | + new_shape = tuple(d if d != -1 else 0 for d in new_shape) |
| 72 | + |
| 73 | + # Handle NCHW dim order - if this op is in NCHW order, we need to permute the |
| 74 | + # view shape correspondingly. |
| 75 | + if "XNN_NHWC_NODE" in node.meta: |
| 76 | + check_or_raise(len(new_shape) == 4, "Invalid NCHW shape") |
| 77 | + new_shape = [new_shape[PERM_NCHW_TO_NHWC[n]] for n in range(4)] |
| 78 | + |
| 79 | + num_dynamic_dims = sum(1 for d in new_shape if d == 0) |
| 80 | + |
| 81 | + check_or_raise( |
| 82 | + num_dynamic_dims <= 1, |
| 83 | + "XNNPACK reshape only supports 1 dynamic dimension.", |
| 84 | + ) |
| 85 | + |
| 86 | + ser_node = XNode( |
| 87 | + xnode_union=XNNStaticReshape( |
| 88 | + num_dims=len(new_shape), |
| 89 | + new_shape=new_shape, |
| 90 | + input_id=input_id, |
| 91 | + output_id=output_id, |
| 92 | + flags=0, |
| 93 | + ), |
| 94 | + debug_handle=debug_handle, |
| 95 | + ) |
| 96 | + xnn_graph.xnodes.append(ser_node) |
0 commit comments