MCPcopy Create free account
hub / github.com/tensorflow/tfjs / _quantize_entry

Function _quantize_entry

tfjs-converter/python/tensorflowjs/write_weights.py:153–192  ·  view source on GitHub ↗

Quantizes the weights in the entry, returning a new entry. The weights are quantized by linearly re-scaling the values between the minimum and maximum value, and representing them with the number of bits provided by the `quantization_dtype`. In order to guarantee that 0 is perfectly repres

(entry, quantization_dtype)

Source from the content-addressed store, hash-verified

151
152
153def _quantize_entry(entry, quantization_dtype):
154 """Quantizes the weights in the entry, returning a new entry.
155
156 The weights are quantized by linearly re-scaling the values between the
157 minimum and maximum value, and representing them with the number of bits
158 provided by the `quantization_dtype`.
159
160 In order to guarantee that 0 is perfectly represented by one of the quanzitzed
161 values, the range is "nudged" in the same manner as in TF-Lite.
162
163 Args:
164 entry: A weight entries to quantize.
165 quantization_dtype: An numpy dtype to quantize weights to.
166 Only np.uint8, np.uint16, and np.float16 are supported.
167
168 Returns:
169 A new entry containing the quantized data and additional quantization info,
170 for example:
171 original_entry = {
172 'name': 'weight1',
173 'data': np.array([0, -0.1, 1.2], 'float32')
174 }
175 quantized_entry = {
176 'name': 'weight1',
177 'data': np.array([20, 0, 255], 'uint8')
178 'quantization': {'min': -0.10196078817, 'scale': 0.00509803940852,
179 'dtype': 'uint8', 'original_dtype': 'float32'}
180 }
181 """
182 data = entry['data']
183 # Only float32 tensors are quantized.
184 if data.dtype != 'float32':
185 return entry
186 quantized_data, metadata = quantization.quantize_weights(
187 data, quantization_dtype)
188 metadata.update({'original_dtype': data.dtype.name})
189 quantized_entry = entry.copy()
190 quantized_entry['data'] = quantized_data
191 quantized_entry['quantization'] = metadata
192 return quantized_entry
193
194
195def _serialize_string_array(data):

Callers 1

write_weightsFunction · 0.85

Calls 1

copyMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…