ai-edge-quantizer-nightly 0.1.0.dev20250515__py3-none-any.whl → 0.1.0.dev20250516__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.
@@ -34,9 +34,10 @@ def duplicate_buffer(
34
34
  f' Tensor {tensor_name} is not constant.'
35
35
  )
36
36
 
37
- duplicated_buffer_id = transformation_utils.add_new_constant_buffer(
37
+ duplicated_buffer_id = transformation_utils.get_constant_buffer(
38
38
  data=buffer_data,
39
39
  buffers=transformation_input.buffers,
40
+ force_duplicate_buffer=True,
40
41
  )
41
42
  tensor.buffer = duplicated_buffer_id
42
43
 
@@ -41,6 +41,7 @@ def duplicate_tensor(
41
41
  tensor_shape=tensor.shape,
42
42
  subgraph=subgraph,
43
43
  buffers=transformation_input.buffers,
44
+ force_duplicate_buffer=True,
44
45
  )
45
46
  # Update the tensor name to avoid name collision in case when tensor is
46
47
  # duplicated mulitple times.
@@ -87,21 +87,32 @@ def add_op_code(
87
87
  return len(model_op_codes) - 1
88
88
 
89
89
 
90
- def add_new_constant_buffer(
90
+ def get_constant_buffer(
91
91
  data: np.ndarray,
92
92
  buffers: list[schema_py_generated.BufferT],
93
+ force_duplicate_buffer: bool = False,
93
94
  ) -> int:
94
- """Add a new constant buffer to the model.
95
+ """Get the index of the constant buffer that contains the given data.
96
+
97
+ creating new buffer if provided data is not found in buffers list.
95
98
 
96
99
  Args:
97
100
  data: The data of the new tensor.
98
101
  buffers: The buffers of the model.
102
+ force_duplicate_buffer: Whether to add a new buffer even if the same buffer
103
+ already exists.
99
104
 
100
105
  Returns:
101
106
  The index of the new buffer in the model.
102
107
  """
108
+ new_data = np.frombuffer(data.tobytes(), dtype=np.uint8).flatten()
109
+ # TODO: b/417811116 - we should make this more efficient.
110
+ if not force_duplicate_buffer:
111
+ for index, buffer in enumerate(buffers):
112
+ if np.array_equal(buffer.data, new_data):
113
+ return index
103
114
  new_buffer = schema_py_generated.BufferT()
104
- new_buffer.data = np.frombuffer(data.tobytes(), dtype=np.uint8).flatten()
115
+ new_buffer.data = new_data
105
116
  new_buffer.offset = 0
106
117
  new_buffer.size = 0
107
118
  new_buffer_id = len(buffers)
@@ -117,6 +128,7 @@ def add_new_constant_tensor(
117
128
  subgraph: schema_py_generated.SubGraphT,
118
129
  buffers: list[schema_py_generated.BufferT],
119
130
  tensor_shape: Optional[list[int]] = None,
131
+ force_duplicate_buffer: bool = False,
120
132
  ) -> int:
121
133
  """Add a new constant tensor to the model.
122
134
 
@@ -128,11 +140,13 @@ def add_new_constant_tensor(
128
140
  buffers: The buffers of the model.
129
141
  tensor_shape: The shape of the new tensor. If not provided, the shape of the
130
142
  data will be used.
143
+ force_duplicate_buffer: Whether to add a new buffer even if the same buffer
144
+ already exists.
131
145
 
132
146
  Returns:
133
147
  The index of the new tensor in the subgraph.
134
148
  """
135
- new_buffer_id = add_new_constant_buffer(data, buffers)
149
+ new_buffer_id = get_constant_buffer(data, buffers, force_duplicate_buffer)
136
150
 
137
151
  new_tensor = schema_py_generated.TensorT()
138
152
  if tensor_shape is None:
@@ -111,7 +111,7 @@ class TransformationUtilsTest(parameterized.TestCase):
111
111
  def test_add_new_constant_buffer(self, data):
112
112
  """Tests if the constant buffer is added to the model."""
113
113
  prev_num_buffers = len(self.model.buffers) - 1
114
- new_buffer_idx = transformation_utils.add_new_constant_buffer(
114
+ new_buffer_idx = transformation_utils.get_constant_buffer(
115
115
  data=data,
116
116
  buffers=self.model.buffers,
117
117
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ai-edge-quantizer-nightly
3
- Version: 0.1.0.dev20250515
3
+ Version: 0.1.0.dev20250516
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
@@ -46,9 +46,9 @@ ai_edge_quantizer/algorithms/utils/common_utils_test.py,sha256=zqapGEfYhjQWe9cNG
46
46
  ai_edge_quantizer/transformations/__init__.py,sha256=lpq1g2ayg3lCPLy79t2VicYcnGKw64FfYIj1V7J-4m8,676
47
47
  ai_edge_quantizer/transformations/dequant_insert.py,sha256=sL1LHFVzBDSd9jgrzlHz38LWU0bwmVX7iBkaNcui0ts,3566
48
48
  ai_edge_quantizer/transformations/dequant_insert_test.py,sha256=NJ18PnG71_AvUPz3Cr_TmG6URMeBfa7IiDDyddfTkKQ,10830
49
- ai_edge_quantizer/transformations/duplicate_buffer.py,sha256=sEod0EtmcHX0VDqBCI4BYCX9CSRyDtx2vmjtOentFiY,1743
49
+ ai_edge_quantizer/transformations/duplicate_buffer.py,sha256=TvTHbm24IiICNkWOlvR2UpJKMU-88puNFycDYc0_ehQ,1774
50
50
  ai_edge_quantizer/transformations/duplicate_buffer_test.py,sha256=YYWl3Q5WF60s8T8pLzzA8TCSxz-i7dqc03dJt1LtMw4,3880
51
- ai_edge_quantizer/transformations/duplicate_tensor.py,sha256=HF1uuKFm5kFF6X0XUpdYlLPoikSRd7pIPK1oxN7TuHY,2455
51
+ ai_edge_quantizer/transformations/duplicate_tensor.py,sha256=WKhf2LIAL0MnZe88b6942A37lvHXe1cFjUDqE5VNmvU,2490
52
52
  ai_edge_quantizer/transformations/duplicate_tensor_test.py,sha256=s-RqSxNBMfVJyCunXz2eb7-KA6UiBmbOmL7phLslENQ,5056
53
53
  ai_edge_quantizer/transformations/emulated_subchannel.py,sha256=HVaRxoC8PCAvy3xeMv3OIymukUy_yW1zK0xN8Ann6I4,13602
54
54
  ai_edge_quantizer/transformations/emulated_subchannel_test.py,sha256=gZP6u9NdPXl7s19qB_Un8evou9ZZV6I9Gy0E1rdobHM,7722
@@ -58,8 +58,8 @@ ai_edge_quantizer/transformations/quant_insert.py,sha256=jn6HsJaV-sqBiFPY-Aqbd64
58
58
  ai_edge_quantizer/transformations/quant_insert_test.py,sha256=X9ptPDvJCFkR5tejKnD1SlHFGPazQTW-wNNMV9MEAuw,10107
59
59
  ai_edge_quantizer/transformations/quantize_tensor.py,sha256=kjaNrw9mnrn0t8u0vey9S_uPz3iVUicwy4rluxVqV3E,7617
60
60
  ai_edge_quantizer/transformations/quantize_tensor_test.py,sha256=mHLO3_MRt36A8-ZN8ADn5tBBJlqjTWa7ZUN8Mmu5Rcw,9116
61
- ai_edge_quantizer/transformations/transformation_utils.py,sha256=Hc1jrY3cEUooiTu9qOh4jxyZp58vrokKxzTmzx6V70c,5853
62
- ai_edge_quantizer/transformations/transformation_utils_test.py,sha256=E90O4PYSjzGdHhaNvm3ii0Xom3cyFfcqQyYjOhYzG-c,8702
61
+ ai_edge_quantizer/transformations/transformation_utils.py,sha256=mYD6gHxSO6JmjC9fSWl0pLh6N_tCW70yTIxHYdmnV3A,6483
62
+ ai_edge_quantizer/transformations/transformation_utils_test.py,sha256=MWgq29t7rvxRQIfi4ny9IoODFCTcbpjnIwoCL40zDKk,8698
63
63
  ai_edge_quantizer/utils/__init__.py,sha256=lpq1g2ayg3lCPLy79t2VicYcnGKw64FfYIj1V7J-4m8,676
64
64
  ai_edge_quantizer/utils/calibration_utils.py,sha256=1Fj9MIO6aLZIRgyd4axvZN4S_O64nB_-Miu1WP664js,2536
65
65
  ai_edge_quantizer/utils/calibration_utils_test.py,sha256=Z-AcdTieesWFKyKBb08ZXm4Mgu6cvJ4bg2-MJ7hLD10,2856
@@ -70,8 +70,8 @@ ai_edge_quantizer/utils/tfl_interpreter_utils.py,sha256=WoewyiZpaua80oP0tpgyrw5W
70
70
  ai_edge_quantizer/utils/tfl_interpreter_utils_test.py,sha256=6fjkM-rycZ95L4yfvlr0TN6RlrhfPzxNUYrZaYO_F0A,12013
71
71
  ai_edge_quantizer/utils/validation_utils.py,sha256=oYw33Sg547AqtGw-choPUJmp9SAKkV46J_ddqSsum2Q,3950
72
72
  ai_edge_quantizer/utils/validation_utils_test.py,sha256=V_qNDikPD4OPB-siOLQCWNVWTAu87h2IgNYt7teFd-o,2934
73
- ai_edge_quantizer_nightly-0.1.0.dev20250515.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
74
- ai_edge_quantizer_nightly-0.1.0.dev20250515.dist-info/METADATA,sha256=Rwa9ls9ryiTwntWB8-SCfO_uYjWMj3bqPTjEhIiQMyo,1528
75
- ai_edge_quantizer_nightly-0.1.0.dev20250515.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
76
- ai_edge_quantizer_nightly-0.1.0.dev20250515.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
77
- ai_edge_quantizer_nightly-0.1.0.dev20250515.dist-info/RECORD,,
73
+ ai_edge_quantizer_nightly-0.1.0.dev20250516.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
74
+ ai_edge_quantizer_nightly-0.1.0.dev20250516.dist-info/METADATA,sha256=iP17vZdvF02q2glxd2bcRcQLcwle6B9KLQcsH4rFrog,1528
75
+ ai_edge_quantizer_nightly-0.1.0.dev20250516.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
76
+ ai_edge_quantizer_nightly-0.1.0.dev20250516.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
77
+ ai_edge_quantizer_nightly-0.1.0.dev20250516.dist-info/RECORD,,