Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect im2col size allocation with INT4 filter #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion ai_edge_quantizer/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""AI Edge Quantizer API."""

from collections.abc import Iterable
import copy
import dataclasses
import json
import os
Expand Down Expand Up @@ -239,7 +240,9 @@ def calibrate(
return calib.get_model_qsvs()

def quantize(
self, calibration_result: Optional[_CalibrationResult] = None
self,
calibration_result: Optional[_CalibrationResult] = None,
tfl_scales=None,
) -> QuantizationResult:
"""Quantizes the float model.

Expand All @@ -257,6 +260,25 @@ def quantize(
if not self.get_quantization_recipe():
raise RuntimeError('Can not quantize without a quantization recipe.')
quant_params = self._get_quantization_params(calibration_result)

# import pprint
# pp = pprint.PrettyPrinter(indent=4)

# override scales
if tfl_scales is not None:
for s, params in quant_params.items():
for ts in tfl_scales:
if ts[0] == s:
for c in params.consumers:
if c.parameters is not None and isinstance(
c.parameters, qtyping.UniformQuantParams
):
print(c.parameters.scale)
print(ts[1])
c.parameters.scale[:] = abs(float(ts[1]))
print(c.parameters.scale)
print('.........')

quantized_model = self._get_quantized_model(quant_params)
self._result = QuantizationResult(
self.get_quantization_recipe(), quantized_model
Expand Down
32 changes: 32 additions & 0 deletions ai_edge_quantizer/utils/validation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ def mean_squared_difference(
Raises:
Value error if the two inputs don't have the same number of elements
"""
if np.any(np.isnan(data1)):
print(data1)
print("data1 is nan")
if np.any(np.isnan(data2)):
print("data2 is nan")
if np.any(np.isinf(data1)):
print("data1 is inf")
if np.any(np.isinf(data2)):
print("data2 is inf")
if (
np.any(np.isnan(data1))
or np.any(np.isnan(data2))
or np.any(np.isinf(data1))
or np.any(np.isinf(data2))
):
return float(1234567890)
data1, data2 = _preprocess_same_size_arrays(data1, data2)
# special handling for tensor of size 0
if data1.size == 0:
Expand Down Expand Up @@ -89,6 +105,22 @@ def median_diff_ratio(
Raises:
Value error if the two inputs don't have the same number of elements
"""
if np.any(np.isnan(data1)):
print(data1)
print("data1 is nan")
if np.any(np.isnan(data2)):
print("data2 is nan")
if np.any(np.isinf(data1)):
print("data1 is inf")
if np.any(np.isinf(data2)):
print("data2 is inf")
if (
np.any(np.isnan(data1))
or np.any(np.isnan(data2))
or np.any(np.isinf(data1))
or np.any(np.isinf(data2))
):
return float(1234567890)
data1, data2 = _preprocess_same_size_arrays(data1, data2)
# special handling for tensor of size 0
if data1.size == 0:
Expand Down
Loading