|
| 1 | +import copy |
| 2 | +import functools |
| 3 | +import logging |
| 4 | +import traceback |
| 5 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | + |
| 9 | +@functools.lru_cache() |
| 10 | +def patch_tf2onnx(): |
| 11 | + """Patches `tf2onnx` to ensure compatibility with numpy>=2.0.0.""" |
| 12 | + |
| 13 | + from onnx import AttributeProto |
| 14 | + from onnx import TensorProto |
| 15 | + |
| 16 | + from keras.src.utils.module_utils import tf2onnx |
| 17 | + |
| 18 | + logger = logging.getLogger(tf2onnx.__name__) |
| 19 | + |
| 20 | + def patched_rewrite_constant_fold(g, ops): |
| 21 | + """ |
| 22 | + We call tensorflow transform with constant folding but in some cases |
| 23 | + tensorflow does fold all constants. Since there are a bunch of ops in |
| 24 | + onnx that use attributes where tensorflow has dynamic inputs, we badly |
| 25 | + want constant folding to work. For cases where tensorflow missed |
| 26 | + something, make another pass over the graph and fix want we care about. |
| 27 | + """ |
| 28 | + func_map = { |
| 29 | + "Add": np.add, |
| 30 | + "GreaterEqual": np.greater_equal, |
| 31 | + "Cast": np.asarray, |
| 32 | + "ConcatV2": np.concatenate, |
| 33 | + "Less": np.less, |
| 34 | + "ListDiff": np.setdiff1d, |
| 35 | + "Mul": np.multiply, |
| 36 | + "Pack": np.stack, |
| 37 | + "Range": np.arange, |
| 38 | + "Sqrt": np.sqrt, |
| 39 | + "Sub": np.subtract, |
| 40 | + } |
| 41 | + ops = list(ops) |
| 42 | + |
| 43 | + keep_looking = True |
| 44 | + while keep_looking: |
| 45 | + keep_looking = False |
| 46 | + for idx, op in enumerate(ops): |
| 47 | + func = func_map.get(op.type) |
| 48 | + if func is None: |
| 49 | + continue |
| 50 | + if set(op.output) & set(g.outputs): |
| 51 | + continue |
| 52 | + try: |
| 53 | + inputs = [] |
| 54 | + for node in op.inputs: |
| 55 | + if not node.is_const(): |
| 56 | + break |
| 57 | + inputs.append(node.get_tensor_value(as_list=False)) |
| 58 | + |
| 59 | + logger.debug( |
| 60 | + "op name %s, %s, %s", |
| 61 | + op.name, |
| 62 | + len(op.input), |
| 63 | + len(inputs), |
| 64 | + ) |
| 65 | + if inputs and len(op.input) == len(inputs): |
| 66 | + logger.info( |
| 67 | + "folding node type=%s, name=%s" % (op.type, op.name) |
| 68 | + ) |
| 69 | + if op.type == "Cast": |
| 70 | + dst = op.get_attr_int("to") |
| 71 | + np_type = tf2onnx.utils.map_onnx_to_numpy_type(dst) |
| 72 | + val = np.asarray(*inputs, dtype=np_type) |
| 73 | + elif op.type == "ConcatV2": |
| 74 | + axis = inputs[-1] |
| 75 | + values = inputs[:-1] |
| 76 | + val = func(tuple(values), axis) |
| 77 | + elif op.type == "ListDiff": |
| 78 | + out_type = op.get_attr_int("out_idx") |
| 79 | + np_type = tf2onnx.utils.map_onnx_to_numpy_type( |
| 80 | + out_type |
| 81 | + ) |
| 82 | + val = func(*inputs) |
| 83 | + val = val.astype(np_type) |
| 84 | + elif op.type in ["Pack"]: |
| 85 | + # handle ops that need input array and axis |
| 86 | + axis = op.get_attr_int("axis") |
| 87 | + val = func(inputs, axis=axis) |
| 88 | + elif op.type == "Range": |
| 89 | + dtype = op.get_attr_int("Tidx") |
| 90 | + np_type = tf2onnx.utils.map_onnx_to_numpy_type( |
| 91 | + dtype |
| 92 | + ) |
| 93 | + val = func(*inputs, dtype=np_type) |
| 94 | + else: |
| 95 | + val = func(*inputs) |
| 96 | + |
| 97 | + new_node_name = tf2onnx.utils.make_name(op.name) |
| 98 | + new_output_name = new_node_name |
| 99 | + old_output_name = op.output[0] |
| 100 | + old_node_name = op.name |
| 101 | + logger.debug( |
| 102 | + "create const node [%s] replacing [%s]", |
| 103 | + new_node_name, |
| 104 | + old_node_name, |
| 105 | + ) |
| 106 | + ops[idx] = g.make_const(new_node_name, val) |
| 107 | + |
| 108 | + logger.debug( |
| 109 | + "replace old output [%s] with new output [%s]", |
| 110 | + old_output_name, |
| 111 | + new_output_name, |
| 112 | + ) |
| 113 | + # need to re-write the consumers input name to use the |
| 114 | + # const name |
| 115 | + consumers = g.find_output_consumers(old_output_name) |
| 116 | + if consumers: |
| 117 | + for consumer in consumers: |
| 118 | + g.replace_input( |
| 119 | + consumer, old_output_name, new_output_name |
| 120 | + ) |
| 121 | + |
| 122 | + # keep looking until there is nothing we can fold. |
| 123 | + # We keep the graph in topological order so if we |
| 124 | + # folded, the result might help a following op. |
| 125 | + keep_looking = True |
| 126 | + except Exception as ex: |
| 127 | + tb = traceback.format_exc() |
| 128 | + logger.info("exception: %s, details: %s", ex, tb) |
| 129 | + # ignore errors |
| 130 | + |
| 131 | + return ops |
| 132 | + |
| 133 | + def patched_get_value_attr(self, external_tensor_storage=None): |
| 134 | + """ |
| 135 | + Return onnx attr for value property of node. |
| 136 | + Attr is modified to point to external tensor data stored in |
| 137 | + external_tensor_storage, if included. |
| 138 | + """ |
| 139 | + a = self._attr["value"] |
| 140 | + if ( |
| 141 | + external_tensor_storage is not None |
| 142 | + and self in external_tensor_storage.node_to_modified_value_attr |
| 143 | + ): |
| 144 | + return external_tensor_storage.node_to_modified_value_attr[self] |
| 145 | + if external_tensor_storage is None or a.type != AttributeProto.TENSOR: |
| 146 | + return a |
| 147 | + |
| 148 | + def prod(x): |
| 149 | + if hasattr(np, "product"): |
| 150 | + return np.product(x) |
| 151 | + else: |
| 152 | + return np.prod(x) |
| 153 | + |
| 154 | + if ( |
| 155 | + prod(a.t.dims) |
| 156 | + > external_tensor_storage.external_tensor_size_threshold |
| 157 | + ): |
| 158 | + a = copy.deepcopy(a) |
| 159 | + tensor_name = ( |
| 160 | + self.name.strip() |
| 161 | + + "_" |
| 162 | + + str(external_tensor_storage.name_counter) |
| 163 | + ) |
| 164 | + for c in '~"#%&*:<>?/\\{|}': |
| 165 | + tensor_name = tensor_name.replace(c, "_") |
| 166 | + external_tensor_storage.name_counter += 1 |
| 167 | + external_tensor_storage.name_to_tensor_data[tensor_name] = ( |
| 168 | + a.t.raw_data |
| 169 | + ) |
| 170 | + external_tensor_storage.node_to_modified_value_attr[self] = a |
| 171 | + a.t.raw_data = b"" |
| 172 | + a.t.ClearField("raw_data") |
| 173 | + location = a.t.external_data.add() |
| 174 | + location.key = "location" |
| 175 | + location.value = tensor_name |
| 176 | + a.t.data_location = TensorProto.EXTERNAL |
| 177 | + return a |
| 178 | + |
| 179 | + tf2onnx.tfonnx.rewrite_constant_fold = patched_rewrite_constant_fold |
| 180 | + tf2onnx.graph.Node.get_value_attr = patched_get_value_attr |
0 commit comments