ai-edge-quantizer-nightly 0.1.0.dev20250429__py3-none-any.whl → 0.1.0.dev20250501__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,21 +51,39 @@ class TransformationInput:
51
51
  def add_op_code(
52
52
  op_code: schema_py_generated.OperatorCodeT,
53
53
  model_op_codes: list[schema_py_generated.OperatorCodeT],
54
+ custom_op_name: Optional[str] = None,
54
55
  ) -> int:
55
56
  """Add an op code into a model if it's not present.
56
57
 
57
58
  Args:
58
59
  op_code: The op code to be added.
59
60
  model_op_codes: The op codes of the model.
61
+ custom_op_name: The custom string of the op code. If None, the op code will
62
+ be added as a builtin op code.
60
63
 
61
64
  Returns:
62
65
  The index of the op code in the model.
63
66
  """
67
+ if (
68
+ op_code == schema_py_generated.BuiltinOperator.CUSTOM
69
+ and custom_op_name is None
70
+ ):
71
+ raise ValueError('Custom string is required for custom op code.')
72
+
64
73
  for i, model_op_code in enumerate(model_op_codes):
74
+ # If the model already has the op code, just return the index.
65
75
  if model_op_code.builtinCode == op_code:
66
- return i
76
+ if custom_op_name is not None:
77
+ if model_op_code.customCode == custom_op_name:
78
+ return i
79
+ else:
80
+ # Built-in op
81
+ return i
82
+
67
83
  model_op_codes.append(schema_py_generated.OperatorCodeT())
68
84
  model_op_codes[-1].builtinCode = op_code
85
+ if custom_op_name is not None:
86
+ model_op_codes[-1].customCode = custom_op_name
69
87
  return len(model_op_codes) - 1
70
88
 
71
89
 
@@ -146,7 +164,14 @@ def add_new_activation_tensor(
146
164
  The index of the new tensor in the subgraph.
147
165
  """
148
166
  new_tensor = schema_py_generated.TensorT()
149
- new_tensor.shape = shape
167
+ # If there's a dynamic shape, we need to read from the shapeSignature field
168
+ # instead of shape. Shape should contain just 1 for the dynamic dimension but
169
+ # shapeSignature should contain the true shape.
170
+ if -1 in shape:
171
+ new_tensor.shapeSignature = shape
172
+ new_tensor.shape = [1 if i == -1 else i for i in shape]
173
+ else:
174
+ new_tensor.shape = shape
150
175
  new_tensor.type = tensor_type
151
176
  new_tensor.name = tensor_name
152
177
  new_tensor.buffer = 0
@@ -41,19 +41,62 @@ class TransformationUtilsTest(parameterized.TestCase):
41
41
  testcase_name="add_new_op_code",
42
42
  op_code=schema_py_generated.BuiltinOperator.LOGISTIC,
43
43
  expected=1,
44
+ custom_op_name=None,
44
45
  ),
45
46
  dict(
46
47
  testcase_name="add_existing_op_code",
47
48
  op_code=schema_py_generated.BuiltinOperator.FULLY_CONNECTED,
48
49
  expected=0,
50
+ custom_op_name=None,
51
+ ),
52
+ dict(
53
+ testcase_name="add_new_custom_op_code",
54
+ op_code=schema_py_generated.BuiltinOperator.CUSTOM,
55
+ expected=1,
56
+ custom_op_name="random_new_custom_op",
49
57
  ),
50
58
  )
51
- def test_add_op_code(self, op_code, expected):
59
+ def test_add_op_code(self, op_code, expected, custom_op_name):
52
60
  """Tests if the op code is added to the model."""
53
61
  got = transformation_utils.add_op_code(
54
- op_code=op_code, model_op_codes=self.model.operatorCodes
62
+ op_code=op_code,
63
+ model_op_codes=self.model.operatorCodes,
64
+ custom_op_name=custom_op_name,
55
65
  )
56
66
  self.assertEqual(expected, got)
67
+ if custom_op_name is not None:
68
+ self.assertEqual(self.model.operatorCodes[got].customCode, custom_op_name)
69
+
70
+ def test_add_custom_op_code_without_op_string_raises_error(self):
71
+ with self.assertRaisesRegex(ValueError, "Custom string is required"):
72
+ transformation_utils.add_op_code(
73
+ op_code=schema_py_generated.BuiltinOperator.CUSTOM,
74
+ model_op_codes=self.model.operatorCodes,
75
+ custom_op_name=None,
76
+ )
77
+
78
+ def test_add_two_custom_op_codes(self):
79
+ custom_op_name = "random_new_custom_op"
80
+ added_index = transformation_utils.add_op_code(
81
+ op_code=schema_py_generated.BuiltinOperator.CUSTOM,
82
+ model_op_codes=self.model.operatorCodes,
83
+ custom_op_name=custom_op_name,
84
+ )
85
+ self.assertEqual(1, added_index)
86
+ self.assertEqual(
87
+ self.model.operatorCodes[added_index].customCode, custom_op_name
88
+ )
89
+
90
+ custom_op_name_2 = "random_new_custom_op_2"
91
+ added_index = transformation_utils.add_op_code(
92
+ op_code=schema_py_generated.BuiltinOperator.CUSTOM,
93
+ model_op_codes=self.model.operatorCodes,
94
+ custom_op_name=custom_op_name_2,
95
+ )
96
+ self.assertEqual(2, added_index)
97
+ self.assertEqual(
98
+ self.model.operatorCodes[added_index].customCode, custom_op_name_2
99
+ )
57
100
 
58
101
  @parameterized.named_parameters(
59
102
  dict(
@@ -189,6 +232,25 @@ class TransformationUtilsTest(parameterized.TestCase):
189
232
  self.model.subgraphs[0].tensors[-1].shape,
190
233
  )
191
234
 
235
+ def test_add_new_activation_tensor_with_dynamic_shape(self):
236
+ """Tests adding an activation tensor with dynamic shape."""
237
+ subgraph = self.model.subgraphs[0]
238
+ new_id = transformation_utils.add_new_activation_tensor(
239
+ tensor_name="test_tensor",
240
+ shape=[1, -1, -1, 1],
241
+ tensor_type=schema_py_generated.TensorType.FLOAT32,
242
+ subgraph=subgraph,
243
+ )
244
+ # Originally had 4 tensors, new tensor is added at index 4.
245
+ self.assertEqual(new_id, 4)
246
+ self.assertLen(subgraph.tensors, 5)
247
+ self.assertEqual(subgraph.tensors[-1].name, "test_tensor")
248
+ self.assertEqual(
249
+ subgraph.tensors[-1].type, schema_py_generated.TensorType.FLOAT32
250
+ )
251
+ self.assertEqual(subgraph.tensors[-1].shape, [1, 1, 1, 1])
252
+ self.assertEqual(subgraph.tensors[-1].shapeSignature, [1, -1, -1, 1])
253
+
192
254
 
193
255
  if __name__ == "__main__":
194
256
  googletest.main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ai-edge-quantizer-nightly
3
- Version: 0.1.0.dev20250429
3
+ Version: 0.1.0.dev20250501
4
4
  Summary: A quantizer for advanced developers to quantize converted AI Edge models.
5
5
  Home-page: https://github.com/google-ai-edge/ai-edge-quantizer
6
6
  Keywords: On-Device ML,AI,Google,TFLite,Quantization,LLMs,GenAI
@@ -54,8 +54,8 @@ ai_edge_quantizer/transformations/quant_insert.py,sha256=jn6HsJaV-sqBiFPY-Aqbd64
54
54
  ai_edge_quantizer/transformations/quant_insert_test.py,sha256=X9ptPDvJCFkR5tejKnD1SlHFGPazQTW-wNNMV9MEAuw,10107
55
55
  ai_edge_quantizer/transformations/quantize_tensor.py,sha256=kjaNrw9mnrn0t8u0vey9S_uPz3iVUicwy4rluxVqV3E,7617
56
56
  ai_edge_quantizer/transformations/quantize_tensor_test.py,sha256=mHLO3_MRt36A8-ZN8ADn5tBBJlqjTWa7ZUN8Mmu5Rcw,9116
57
- ai_edge_quantizer/transformations/transformation_utils.py,sha256=5w0fG6TP362elTHs-JZokl24fuK4Gv6DGyIpybQYb3g,4885
58
- ai_edge_quantizer/transformations/transformation_utils_test.py,sha256=xH64SF3UHDh84vYbt-WvmXNjM-Jg-mefES1ACO1tkqw,6269
57
+ ai_edge_quantizer/transformations/transformation_utils.py,sha256=Hc1jrY3cEUooiTu9qOh4jxyZp58vrokKxzTmzx6V70c,5853
58
+ ai_edge_quantizer/transformations/transformation_utils_test.py,sha256=E90O4PYSjzGdHhaNvm3ii0Xom3cyFfcqQyYjOhYzG-c,8702
59
59
  ai_edge_quantizer/utils/__init__.py,sha256=lpq1g2ayg3lCPLy79t2VicYcnGKw64FfYIj1V7J-4m8,676
60
60
  ai_edge_quantizer/utils/calibration_utils.py,sha256=1Fj9MIO6aLZIRgyd4axvZN4S_O64nB_-Miu1WP664js,2536
61
61
  ai_edge_quantizer/utils/calibration_utils_test.py,sha256=Z-AcdTieesWFKyKBb08ZXm4Mgu6cvJ4bg2-MJ7hLD10,2856
@@ -66,8 +66,8 @@ ai_edge_quantizer/utils/tfl_interpreter_utils.py,sha256=WoewyiZpaua80oP0tpgyrw5W
66
66
  ai_edge_quantizer/utils/tfl_interpreter_utils_test.py,sha256=6fjkM-rycZ95L4yfvlr0TN6RlrhfPzxNUYrZaYO_F0A,12013
67
67
  ai_edge_quantizer/utils/validation_utils.py,sha256=oYw33Sg547AqtGw-choPUJmp9SAKkV46J_ddqSsum2Q,3950
68
68
  ai_edge_quantizer/utils/validation_utils_test.py,sha256=V_qNDikPD4OPB-siOLQCWNVWTAu87h2IgNYt7teFd-o,2934
69
- ai_edge_quantizer_nightly-0.1.0.dev20250429.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
70
- ai_edge_quantizer_nightly-0.1.0.dev20250429.dist-info/METADATA,sha256=uL3RUBECU5YAPHCJ8tNR8CaVFIQN1Bq-Mc4now2cpJw,1527
71
- ai_edge_quantizer_nightly-0.1.0.dev20250429.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
72
- ai_edge_quantizer_nightly-0.1.0.dev20250429.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
73
- ai_edge_quantizer_nightly-0.1.0.dev20250429.dist-info/RECORD,,
69
+ ai_edge_quantizer_nightly-0.1.0.dev20250501.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
70
+ ai_edge_quantizer_nightly-0.1.0.dev20250501.dist-info/METADATA,sha256=2ZTrsH6UES2dfRsIgNmnfEnnT88MPS6giLXvLb5MSO0,1527
71
+ ai_edge_quantizer_nightly-0.1.0.dev20250501.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
72
+ ai_edge_quantizer_nightly-0.1.0.dev20250501.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
73
+ ai_edge_quantizer_nightly-0.1.0.dev20250501.dist-info/RECORD,,