|
| 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 | +from typing import Dict |
| 8 | + |
| 9 | +import torch |
| 10 | +from executorch.backends.xnnpack.operators.node_visitor import ( |
| 11 | + NodeVisitor, |
| 12 | + register_node_visitor, |
| 13 | +) |
| 14 | +from executorch.backends.xnnpack.serialization.xnnpack_graph_schema import ( |
| 15 | + XNNExpandDims, |
| 16 | + XNNGraph, |
| 17 | + XNode, |
| 18 | +) |
| 19 | +from executorch.backends.xnnpack.utils.utils import get_input_node |
| 20 | + |
| 21 | + |
| 22 | +def check_expand_copy_constraints(node: torch.fx.Node) -> bool: |
| 23 | + """ |
| 24 | + Checks whether the given expand_copy node is delegatable to XNNPACK. |
| 25 | + XNNPACK only allows insertion of size-1 dimensions, not expanding existing |
| 26 | + dims. |
| 27 | + """ |
| 28 | + in_shape = get_input_node(node, 0).meta["val"].shape |
| 29 | + new_shape = list(node.args[1]) |
| 30 | + |
| 31 | + assert len(new_shape) >= len( |
| 32 | + in_shape |
| 33 | + ), "Expanded shape must have rank >= input rank." |
| 34 | + |
| 35 | + # Check new leading dims (if any). They must be of size 1. |
| 36 | + new_leading_dims_count = len(new_shape) - len(in_shape) |
| 37 | + for i in range(new_leading_dims_count): |
| 38 | + if new_shape[i] != 1: |
| 39 | + return False |
| 40 | + |
| 41 | + # Check existing dims. PyTorch expand semantics don't allow for dim insertion other |
| 42 | + # than at the front, so we just need to make sure none of the dims are expanded. |
| 43 | + for i in range(len(new_shape) - new_leading_dims_count): |
| 44 | + new_shape_at_dim = new_shape[new_leading_dims_count + i] |
| 45 | + # -1 means preserve dim. |
| 46 | + if new_shape_at_dim != -1 and new_shape_at_dim != in_shape[i]: |
| 47 | + return False |
| 48 | + |
| 49 | + return True |
| 50 | + |
| 51 | + |
| 52 | +def get_inserted_dim_indices( |
| 53 | + node: torch.fx.Node, |
| 54 | +) -> list[int]: |
| 55 | + """ |
| 56 | + Returns the indices of the inserted dimensions in the expanded shape. Assumes that |
| 57 | + the node meets the conditions checked in check_expand_copy_constraints. |
| 58 | + """ |
| 59 | + in_shape = get_input_node(node, 0).meta["val"].shape |
| 60 | + new_shape = list(node.args[1]) |
| 61 | + new_dim_indices = [] |
| 62 | + |
| 63 | + assert len(new_shape) >= len( |
| 64 | + in_shape |
| 65 | + ), "Expanded shape must have rank >= input rank." |
| 66 | + |
| 67 | + # PyTorch expand semantics enforce new dim insertion only at the front. |
| 68 | + new_leading_dims_count = len(new_shape) - len(in_shape) |
| 69 | + for i in range(new_leading_dims_count): |
| 70 | + if new_shape[i] != 1: |
| 71 | + return False |
| 72 | + else: |
| 73 | + new_dim_indices.append(i) |
| 74 | + |
| 75 | + return new_dim_indices |
| 76 | + |
| 77 | + |
| 78 | +@register_node_visitor |
| 79 | +class ExpandCopyVisitor(NodeVisitor): |
| 80 | + target = "aten.expand_copy.default" |
| 81 | + |
| 82 | + def __init__(self, *args) -> None: |
| 83 | + super().__init__(*args) |
| 84 | + |
| 85 | + def define_node( |
| 86 | + self, |
| 87 | + node: torch.fx.Node, |
| 88 | + xnn_graph: XNNGraph, |
| 89 | + vals_to_ids: Dict[torch.fx.Node, int], |
| 90 | + debug_handle: int, |
| 91 | + ) -> None: |
| 92 | + self.define_nodes_tensor_inputs_outputs(node, xnn_graph, vals_to_ids) |
| 93 | + |
| 94 | + # input |
| 95 | + input_id = vals_to_ids[get_input_node(node, 0)] |
| 96 | + |
| 97 | + # output |
| 98 | + output_id = vals_to_ids[node] |
| 99 | + |
| 100 | + new_dim_indices = get_inserted_dim_indices(node) |
| 101 | + |
| 102 | + ser_node = XNode( |
| 103 | + xnode_union=XNNExpandDims( |
| 104 | + num_new_dims=len(new_dim_indices), |
| 105 | + new_dim_indices=new_dim_indices, |
| 106 | + input_id=input_id, |
| 107 | + output_id=output_id, |
| 108 | + flags=0, |
| 109 | + ), |
| 110 | + debug_handle=debug_handle, |
| 111 | + ) |
| 112 | + xnn_graph.xnodes.append(ser_node) |
0 commit comments