|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 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 | +import torch |
| 8 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 9 | +from executorch.exir.dialects.edge._ops import EdgeOpOverload |
| 10 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 11 | +from executorch.exir.passes import dead_code_elimination_pass |
| 12 | + |
| 13 | +from .utils import copy_meta |
| 14 | + |
| 15 | + |
| 16 | +def _get_ops(is_edge: bool): |
| 17 | + return ( |
| 18 | + { |
| 19 | + "full": exir_ops.edge.aten.full.default, |
| 20 | + "cast": exir_ops.edge.aten._to_copy.default, |
| 21 | + } |
| 22 | + if is_edge |
| 23 | + else { |
| 24 | + "full": torch.ops.aten.full.default, |
| 25 | + "cast": torch.ops.aten.to.dtype, |
| 26 | + } |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def _fill_value(dtype: torch.dtype): |
| 31 | + """Return a zero fill scalar and the dtype full.default infers from it. |
| 32 | +
|
| 33 | + full.default has no dtype argument here (see the note in DecomposeEmpty on |
| 34 | + why kwargs are not an option), so its output dtype is derived from the type |
| 35 | + of the python scalar: bool -> bool, int -> int64, float -> the default |
| 36 | + dtype. The caller compares the returned dtype against the dtype it actually |
| 37 | + needs and inserts a cast when they differ. |
| 38 | + """ |
| 39 | + if dtype == torch.bool: |
| 40 | + return False, torch.bool |
| 41 | + if dtype.is_floating_point or dtype.is_complex: |
| 42 | + return 0.0, torch.get_default_dtype() |
| 43 | + return 0, torch.int64 |
| 44 | + |
| 45 | + |
| 46 | +class DecomposeEmpty(ExportPass): |
| 47 | + """ |
| 48 | + Decompose empty.memory_format / empty_strided into full.default. |
| 49 | +
|
| 50 | + The values are uninitialized, so filling with zero is free. The rewrite is |
| 51 | + applied to every allocation, including a non-contiguous memory_format (e.g. |
| 52 | + channels_last) or an arbitrary stride passed to empty_strided: QNN tensors |
| 53 | + are dense and carry no strides, so the layout cannot survive delegation |
| 54 | + anyway, and nothing in the buffer is readable. Restricting the pass to |
| 55 | + contiguous allocations would instead split the delegate and push the |
| 56 | + remaining empty nodes onto CPU. |
| 57 | +
|
| 58 | + Assumes a plain CPU, non-pinned allocation -- full.default is created |
| 59 | + without device/layout/pin_memory. |
| 60 | + """ |
| 61 | + |
| 62 | + def __init__(self): |
| 63 | + super().__init__() |
| 64 | + self.targets = { |
| 65 | + torch.ops.aten.empty.memory_format, |
| 66 | + torch.ops.aten.empty_strided.default, |
| 67 | + exir_ops.edge.aten.empty.memory_format, |
| 68 | + exir_ops.edge.aten.empty_strided.default, |
| 69 | + } |
| 70 | + |
| 71 | + def call(self, graph_module: torch.fx.GraphModule): |
| 72 | + graph = graph_module.graph |
| 73 | + for node in list(graph.nodes): |
| 74 | + if node.op == "call_function" and node.target in self.targets: |
| 75 | + empty_node = node |
| 76 | + val = empty_node.meta["val"] |
| 77 | + shape = list(val.shape) |
| 78 | + |
| 79 | + is_edge = isinstance(empty_node.target, EdgeOpOverload) |
| 80 | + ops = _get_ops(is_edge) |
| 81 | + fill_value, fill_dtype = _fill_value(val.dtype) |
| 82 | + |
| 83 | + # The replacement always produces a contiguous tensor, so the |
| 84 | + # propagated FakeTensor must not keep the original strides. |
| 85 | + def cast_meta(meta, dtype=None): |
| 86 | + out = meta["val"].contiguous() |
| 87 | + meta["val"] = out if dtype is None else out.to(dtype) |
| 88 | + return meta |
| 89 | + |
| 90 | + with graph.inserting_before(empty_node): |
| 91 | + # Never pass kwargs (dtype/device) to an ATen op here: the |
| 92 | + # ATen IR requires them to be empty and prepare_pt2e |
| 93 | + # asserts on it, which this pass would hit since it runs |
| 94 | + # before the quantizer and full.default is annotated. |
| 95 | + full_node = graph.create_node( |
| 96 | + "call_function", |
| 97 | + ops["full"], |
| 98 | + (shape, fill_value), |
| 99 | + ) |
| 100 | + full_node.meta = copy_meta( |
| 101 | + empty_node.meta, |
| 102 | + lambda meta, _dtype=fill_dtype: cast_meta(meta, _dtype), |
| 103 | + ) |
| 104 | + out_node = full_node |
| 105 | + |
| 106 | + if fill_dtype != val.dtype: |
| 107 | + # to.dtype takes its dtype positionally, keeping the |
| 108 | + # ATen node free of kwargs; the edge dialect uses |
| 109 | + # _to_copy, where kwargs are expected. |
| 110 | + cast_args, cast_kwargs = ( |
| 111 | + ((full_node,), {"dtype": val.dtype}) |
| 112 | + if is_edge |
| 113 | + else ((full_node, val.dtype), {}) |
| 114 | + ) |
| 115 | + cast_node = graph.create_node( |
| 116 | + "call_function", |
| 117 | + ops["cast"], |
| 118 | + cast_args, |
| 119 | + cast_kwargs, |
| 120 | + ) |
| 121 | + cast_node.meta = copy_meta(empty_node.meta, cast_meta) |
| 122 | + out_node = cast_node |
| 123 | + |
| 124 | + for user in empty_node.users.copy(): |
| 125 | + user.replace_input_with(empty_node, out_node) |
| 126 | + |
| 127 | + dead_code_elimination_pass(graph_module) |
| 128 | + return PassResult(graph_module, True) |
0 commit comments