ai-edge-quantizer-nightly 0.4.0.dev20251108__py3-none-any.whl → 0.4.0.dev20251110__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.
@@ -32,7 +32,7 @@ def get_validation_func(
32
32
  a validation function
33
33
 
34
34
  Raises:
35
- Value error if the function name is not supported
35
+ ValueError: if the function name is not supported
36
36
  """
37
37
  if func_name == "mse":
38
38
  return mean_squared_difference
@@ -42,6 +42,8 @@ def get_validation_func(
42
42
  return cosine_similarity
43
43
  elif func_name == "kl_divergence":
44
44
  return kl_divergence
45
+ elif func_name == "snr":
46
+ return signal_to_noise_ratio
45
47
  else:
46
48
  raise ValueError(f"Validation function {func_name} not supported")
47
49
 
@@ -62,7 +64,7 @@ def mean_squared_difference(
62
64
  a float value representing the MSD between data1 & 2
63
65
 
64
66
  Raises:
65
- Value error if the two inputs don't have the same number of elements
67
+ ValueError: if the two inputs don't have the same number of elements
66
68
  """
67
69
  data1, data2 = _preprocess_same_size_arrays(data1, data2)
68
70
  # special handling for tensor of size 0
@@ -91,7 +93,7 @@ def median_diff_ratio(
91
93
  a float value representing the median diff ratio between data1 & 2
92
94
 
93
95
  Raises:
94
- Value error if the two inputs don't have the same number of elements
96
+ ValueError: if the two inputs don't have the same number of elements
95
97
  """
96
98
  data1, data2 = _preprocess_same_size_arrays(data1, data2)
97
99
  # special handling for tensor of size 0
@@ -120,7 +122,7 @@ def cosine_similarity(
120
122
  a float value representing the cosine similarity between data1 & 2
121
123
 
122
124
  Raises:
123
- Value error if the two inputs don't have the same number of elements
125
+ ValueError: if the two inputs don't have the same number of elements
124
126
  """
125
127
  data1, data2 = _preprocess_same_size_arrays(data1, data2)
126
128
  # special handling for tensor of size 0
@@ -152,15 +154,15 @@ def kl_divergence(
152
154
 
153
155
  Args:
154
156
  data1: input data to be used for comparison (distribution Q)
155
- data2: input data to be used for comparison (distribution P),
156
- data1 & 2 must be of the same shape
157
+ data2: input data to be used for comparison (distribution P), data1 & 2 must
158
+ be of the same shape
157
159
  epsilon: small value to avoid log(0) and division by zero.
158
160
 
159
161
  Returns:
160
162
  A float value representing the KL divergence between data1 & 2.
161
163
 
162
164
  Raises:
163
- Value error if the two inputs don't have the same number of elements.
165
+ ValueError: if the two inputs don't have the same number of elements.
164
166
  """
165
167
  data1, data2 = _preprocess_same_size_arrays(data1, data2)
166
168
  # special handling for tensor of size 0
@@ -173,6 +175,40 @@ def kl_divergence(
173
175
  return float(np.sum(p * np.log((p + epsilon) / (q + epsilon))))
174
176
 
175
177
 
178
+ def signal_to_noise_ratio(
179
+ noisy_signal: np._typing.ArrayLike,
180
+ signal: np._typing.ArrayLike,
181
+ epsilon: float = 1e-9,
182
+ ) -> float:
183
+ """Calculates the signal to noise ratio between noisy_signal & signal.
184
+
185
+ SNR = P_signal / P_noise, where signal is treated as the clean signal and
186
+ noisy_signal-signal is treated as the noise samples.
187
+ P_signal = mean(signal^2)
188
+ P_noise = mean((noisy_signal-signal)^2) = mse(noisy_signal, signal)
189
+
190
+ Args:
191
+ noisy_signal: Input data to be used for comparison (e.g. noisy signal).
192
+ signal: Input data to be used for comparison (e.g. clean signal),
193
+ noisy_signal & signal must be of the same shape.
194
+ epsilon: Small value to avoid division by zero.
195
+
196
+ Returns:
197
+ A float value representing the SNR between noisy_signal & signal.
198
+
199
+ Raises:
200
+ ValueError: If the two inputs don't have the same number of elements.
201
+ """
202
+ noisy_signal, signal = _preprocess_same_size_arrays(noisy_signal, signal)
203
+ if signal.size == 0:
204
+ return float(0)
205
+
206
+ mse = mean_squared_difference(noisy_signal, signal)
207
+ signal_power = float(np.square(signal).mean())
208
+ snr = signal_power / (mse + epsilon)
209
+ return snr
210
+
211
+
176
212
  def _preprocess_same_size_arrays(
177
213
  data1: np._typing.ArrayLike, data2: np._typing.ArrayLike
178
214
  ) -> Tuple[np.ndarray, np.ndarray]:
@@ -187,7 +223,7 @@ def _preprocess_same_size_arrays(
187
223
  a tuple of the preprocessed data1 & 2
188
224
 
189
225
  Raises:
190
- Value error if the two inputs don't have the same number of elements
226
+ ValueError: if the two inputs don't have the same number of elements
191
227
  """
192
228
  data1 = np.array(data1, dtype=np.float32).flatten()
193
229
  data2 = np.array(data2, dtype=np.float32).flatten()
@@ -134,6 +134,34 @@ class ValidationUtilTest(googletest.TestCase):
134
134
  func = validation_utils.get_validation_func("kl_divergence")
135
135
  self.assertEqual(func, validation_utils.kl_divergence)
136
136
 
137
+ def test_signal_to_noise_ratio_0d(self):
138
+ data1 = []
139
+ data2 = []
140
+ result = validation_utils.signal_to_noise_ratio(data1, data2)
141
+ self.assertEqual(result, 0)
142
+
143
+ def test_signal_to_noise_ratio_identical(self):
144
+ data1 = [1, 2, 3]
145
+ data2 = [1, 2, 3]
146
+ result = validation_utils.signal_to_noise_ratio(data1, data2)
147
+ self.assertGreater(result, 1e8) # mse=0, so snr should be large
148
+
149
+ def test_signal_to_noise_ratio_with_noise(self):
150
+ data1 = [2, 3, 4]
151
+ data2 = [1, 2, 3]
152
+ result = validation_utils.signal_to_noise_ratio(data1, data2)
153
+ self.assertAlmostEqual(result, 14 / 3, places=5)
154
+
155
+ def test_signal_to_noise_ratio_simple(self):
156
+ data1 = [1, 1]
157
+ data2 = [1, 0]
158
+ result = validation_utils.signal_to_noise_ratio(data1, data2)
159
+ self.assertAlmostEqual(result, 1.0, places=5)
160
+
161
+ def test_get_validation_func_snr(self):
162
+ func = validation_utils.get_validation_func("snr")
163
+ self.assertEqual(func, validation_utils.signal_to_noise_ratio)
164
+
137
165
 
138
166
  if __name__ == "__main__":
139
167
  googletest.main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ai-edge-quantizer-nightly
3
- Version: 0.4.0.dev20251108
3
+ Version: 0.4.0.dev20251110
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
@@ -72,10 +72,10 @@ ai_edge_quantizer/utils/tfl_flatbuffer_utils.py,sha256=42OWzQsRTXq3XQYmoxlz177_d
72
72
  ai_edge_quantizer/utils/tfl_flatbuffer_utils_test.py,sha256=K1SbK8q92qYVtiVj0I0GtugsPTkpIpEKv9zakvFV_Sc,8555
73
73
  ai_edge_quantizer/utils/tfl_interpreter_utils.py,sha256=EoVjI_hplX_Rml3hfRsGmQOihexmizeJqt4SQcET9aA,14925
74
74
  ai_edge_quantizer/utils/tfl_interpreter_utils_test.py,sha256=6fjkM-rycZ95L4yfvlr0TN6RlrhfPzxNUYrZaYO_F0A,12013
75
- ai_edge_quantizer/utils/validation_utils.py,sha256=QTYyQ_HDVrFTGPIsrA240Lv8tUw1fwWp2fu9kTVISkE,6224
76
- ai_edge_quantizer/utils/validation_utils_test.py,sha256=lO51rGskhzpXePRdZMU87u_YO35_sDp9_eQ85CmupL4,4600
77
- ai_edge_quantizer_nightly-0.4.0.dev20251108.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
78
- ai_edge_quantizer_nightly-0.4.0.dev20251108.dist-info/METADATA,sha256=yUTFddw1QJHge_e5fD2irNEXLnRdCw1SSBWtB7pBPcY,1707
79
- ai_edge_quantizer_nightly-0.4.0.dev20251108.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
80
- ai_edge_quantizer_nightly-0.4.0.dev20251108.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
81
- ai_edge_quantizer_nightly-0.4.0.dev20251108.dist-info/RECORD,,
75
+ ai_edge_quantizer/utils/validation_utils.py,sha256=Mr0D6X-pTDLODFAnCX3IlqdV1OL02tlq0ZjHbqx8nzg,7439
76
+ ai_edge_quantizer/utils/validation_utils_test.py,sha256=T8K5mCWeMcihND2KS_dHvCJUU9lEdG2sD95EgPkaX3w,5584
77
+ ai_edge_quantizer_nightly-0.4.0.dev20251110.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
78
+ ai_edge_quantizer_nightly-0.4.0.dev20251110.dist-info/METADATA,sha256=se3xo08IXOZLI8qd_7g_aH_o32b84BLW223gw1nQYS4,1707
79
+ ai_edge_quantizer_nightly-0.4.0.dev20251110.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
80
+ ai_edge_quantizer_nightly-0.4.0.dev20251110.dist-info/top_level.txt,sha256=8QTfPnFXNVUhScFLaa-NWZMFWMn72M50DVPubpwWB1g,18
81
+ ai_edge_quantizer_nightly-0.4.0.dev20251110.dist-info/RECORD,,