|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | + |
| 4 | +""" |
| 5 | +tf2onnx.tflite_rewriters.tfl_scan_output_rewriter - Identify a common slice/concat pattern in tflite subgraphs |
| 6 | +Effectively replace A = A[:i] + [B] + A[i+1:] with A[i] = B |
| 7 | +""" |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from tf2onnx.graph_matcher import OpTypePattern, GraphMatcher |
| 11 | + |
| 12 | + |
| 13 | +# pylint: disable=missing-docstring |
| 14 | + |
| 15 | +def rewrite_slice_concat_to_scatter(g, ops): |
| 16 | + pattern0 = \ |
| 17 | + OpTypePattern('TFL_CONCATENATION', name='concat', inputs=[ |
| 18 | + OpTypePattern('TFL_SLICE', name='begin_slice'), |
| 19 | + OpTypePattern('*', name='middle'), |
| 20 | + OpTypePattern('TFL_SLICE', name='end_slice') |
| 21 | + ]) |
| 22 | + |
| 23 | + matcher = GraphMatcher(pattern0, allow_reorder=False) |
| 24 | + match_results = list(matcher.match_ops(ops)) |
| 25 | + if match_results: |
| 26 | + for match in match_results: |
| 27 | + concat = match.get_op("concat") |
| 28 | + begin_slice = match.get_op("begin_slice") |
| 29 | + middle = match.get_op("middle") |
| 30 | + end_slice = match.get_op("end_slice") |
| 31 | + middle_shape = g.get_shape(middle.output[0]) |
| 32 | + |
| 33 | + # Both slices must be slicing the same tensor |
| 34 | + if begin_slice.input[0] != end_slice.input[0]: |
| 35 | + continue |
| 36 | + original_tensor = begin_slice.input[0] |
| 37 | + if concat.get_attr_int("axis") != 0: |
| 38 | + continue |
| 39 | + # The inserted slice must have length 1 (to be a single index) |
| 40 | + if middle_shape is None or len(middle_shape) == 0 or middle_shape[0] != 1: |
| 41 | + continue |
| 42 | + rank = len(middle_shape) |
| 43 | + scan_output = middle.output[0] |
| 44 | + if not begin_slice.inputs[1].is_const() or not end_slice.inputs[2].is_const(): |
| 45 | + continue |
| 46 | + # The first slice must start from the beginning (0) for all dims |
| 47 | + if not all(v == 0 for v in begin_slice.inputs[1].get_tensor_value()): |
| 48 | + continue |
| 49 | + # The second slice must slice to the end (-1) for all dims |
| 50 | + if not all(v == -1 for v in end_slice.inputs[2].get_tensor_value()): |
| 51 | + continue |
| 52 | + # The other slice dims are assembled by concatenation if rank > 1 |
| 53 | + if rank > 1: |
| 54 | + begin_concat = begin_slice.inputs[2] |
| 55 | + end_concat = end_slice.inputs[1] |
| 56 | + if not begin_concat.type == "TFL_CONCATENATION": |
| 57 | + continue |
| 58 | + if not end_concat.type == "TFL_CONCATENATION": |
| 59 | + continue |
| 60 | + # Except for dim 0, slice from beginning to end |
| 61 | + if not all(get_uniform_const_val(inp) == -1 for inp in begin_concat.inputs[1:]): |
| 62 | + continue |
| 63 | + if not all(get_uniform_const_val(inp) == 0 for inp in end_concat.inputs[1:]): |
| 64 | + continue |
| 65 | + begin_idx = begin_concat.inputs[0] |
| 66 | + end_idx = end_concat.inputs[0] |
| 67 | + else: |
| 68 | + begin_idx = begin_slice.inputs[2] |
| 69 | + end_idx = end_slice.inputs[1] |
| 70 | + # For dim 0, slice to i for first part and from i+1 for second |
| 71 | + if not node_is_one_plus_node(begin_idx, end_idx): |
| 72 | + continue |
| 73 | + out1, _ = get_out_and_offset(begin_idx) |
| 74 | + graph_inps = [n.output[0] for n in g.inputs] |
| 75 | + # To be a scan output, i must be a graph input |
| 76 | + if out1 not in graph_inps: |
| 77 | + continue |
| 78 | + # The array being sliced must be a graph input |
| 79 | + if original_tensor not in graph_inps: |
| 80 | + continue |
| 81 | + # The input/output index of i |
| 82 | + idx = graph_inps.index(out1) |
| 83 | + # The input/output index of the array |
| 84 | + scan_output_idx = graph_inps.index(original_tensor) |
| 85 | + # For a scan output, i must be assigned to i+1 with each iteration |
| 86 | + if not node_is_one_plus_node(g.get_node_by_output(out1), g.get_node_by_output(g.outputs[idx])): |
| 87 | + continue |
| 88 | + if len(g.find_output_consumers(concat.output[0])) > 1: |
| 89 | + continue |
| 90 | + |
| 91 | + if g.opset < 10 and len(g.find_output_consumers(concat.output[0])) <= 1: |
| 92 | + # If opset is < 10, conversion of the subgraph will fail unless we remove the slice nodes |
| 93 | + # We add a tmp node to replace them. |
| 94 | + shape = g.get_shape(concat.output[0]) |
| 95 | + dtype = g.get_dtype(concat.output[0]) |
| 96 | + tmp_node = g.make_node("TMP_SCAN_OUTPUT", [original_tensor, scan_output], |
| 97 | + shapes=[shape], dtypes=[dtype]) |
| 98 | + g.replace_all_inputs(concat.output[0], tmp_node.output[0]) |
| 99 | + |
| 100 | + to_remove = [] |
| 101 | + out = g.outputs[scan_output_idx] |
| 102 | + node = g.get_node_by_output(out) |
| 103 | + to_remove.append(node) |
| 104 | + |
| 105 | + while len(node.input) > 0 and node != concat: |
| 106 | + out = node.input[0] |
| 107 | + node = g.get_node_by_output(out) |
| 108 | + to_remove.append(node) |
| 109 | + |
| 110 | + to_remove += [begin_slice, end_slice, concat] |
| 111 | + |
| 112 | + out = original_tensor |
| 113 | + node = g.get_node_by_output(out) |
| 114 | + to_remove.append(node) |
| 115 | + |
| 116 | + while len(node.input) > 0: |
| 117 | + out = node.input[0] |
| 118 | + node = g.get_node_by_output(out) |
| 119 | + to_remove.append(node) |
| 120 | + |
| 121 | + if not g.is_safe_to_remove_nodes(to_remove): |
| 122 | + continue |
| 123 | + |
| 124 | + g.scan_outputs.append((scan_output_idx, scan_output)) |
| 125 | + return ops |
| 126 | + |
| 127 | +def get_uniform_const_val(n): |
| 128 | + if not n.is_const(): |
| 129 | + return None |
| 130 | + v = n.get_tensor_value(as_list=False).flatten() |
| 131 | + if len(v) == 0: |
| 132 | + return None |
| 133 | + if np.all(v == v[0]): |
| 134 | + return v[0] |
| 135 | + return None |
| 136 | + |
| 137 | +def get_out_and_offset(n): |
| 138 | + if n.type in ['TFL_RESHAPE', 'TFL_IDENTITY', 'Identity']: |
| 139 | + return get_out_and_offset(n.inputs[0]) |
| 140 | + if n.type == 'TFL_ADD': |
| 141 | + v1 = get_uniform_const_val(n.inputs[0]) |
| 142 | + v2 = get_uniform_const_val(n.inputs[1]) |
| 143 | + if v1 is not None and v2 is not None: |
| 144 | + return '', v1 + v2 |
| 145 | + if v1 is not None: |
| 146 | + inp2, o2 = get_out_and_offset(n.inputs[1]) |
| 147 | + return inp2, v1 + o2 |
| 148 | + if v2 is not None: |
| 149 | + inp1, o1 = get_out_and_offset(n.inputs[0]) |
| 150 | + return inp1, v2 + o1 |
| 151 | + return n.output[0], 0 |
| 152 | + |
| 153 | +def node_is_one_plus_node(node, one_plus_node): |
| 154 | + n1, o1 = get_out_and_offset(node) |
| 155 | + n2, o2 = get_out_and_offset(one_plus_node) |
| 156 | + return n1 == n2 and o1 + 1 == o2 |
0 commit comments