Skip to content
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
5 changes: 1 addition & 4 deletions coremltools/optimize/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,6 @@ def _update_tensor_range(
"""
tensor_min = np.min(np.array(tensor_value).flatten())
tensor_max = np.max(np.array(tensor_value).flatten())
activation_stats_dict[tensor_name]["rmin"] = tensor_min
activation_stats_dict[tensor_name]["rmax"] = tensor_max
if tensor_name in activation_stats_dict:
activation_stats_dict[tensor_name]["rmin"] = min(
tensor_min, activation_stats_dict[tensor_name]["rmin"]
Expand All @@ -877,8 +875,7 @@ def _update_tensor_range(
tensor_max, activation_stats_dict[tensor_name]["rmax"]
)
else:
activation_stats_dict[tensor_name]["rmin"] = tensor_min
activation_stats_dict[tensor_name]["rmax"] = tensor_max
activation_stats_dict[tensor_name] = {"rmin": tensor_min, "rmax": tensor_max}


def _combine_lists_with_common_elements(data: List[List[str]]) -> List[List[str]]:
Expand Down
5 changes: 1 addition & 4 deletions coremltools/optimize/coreml/experimental/_model_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ def predict_intermediate_outputs(
def record_intermediate_output(output_value, output_name, activation_stats_dict):
tensor_min = np.min(output_value.flatten())
tensor_max = np.max(output_value.flatten())
activation_stats_dict[output_name]["rmin"] = tensor_min
activation_stats_dict[output_name]["rmax"] = tensor_max
if output_name in activation_stats_dict:
activation_stats_dict[output_name]["rmin"] = min(
tensor_min, activation_stats_dict[output_name]["rmin"]
Expand All @@ -278,8 +276,7 @@ def record_intermediate_output(output_value, output_name, activation_stats_dict)
tensor_max, activation_stats_dict[output_name]["rmax"]
)
else:
activation_stats_dict[output_name]["rmin"] = tensor_min
activation_stats_dict[output_name]["rmax"] = tensor_max
activation_stats_dict[output_name] = {"rmin": tensor_min, "rmax": tensor_max}

def step(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ def _update_tensor_range(
) -> None:
tensor_min = np.min(np.array(tensor_value).flatten())
tensor_max = np.max(np.array(tensor_value).flatten())
activation_stats_dict[tensor_name]["rmin"] = tensor_min
activation_stats_dict[tensor_name]["rmax"] = tensor_max
if tensor_name in activation_stats_dict:
activation_stats_dict[tensor_name]["rmin"] = min(
tensor_min, activation_stats_dict[tensor_name]["rmin"]
Expand All @@ -111,8 +109,7 @@ def _update_tensor_range(
tensor_max, activation_stats_dict[tensor_name]["rmax"]
)
else:
activation_stats_dict[tensor_name]["rmin"] = tensor_min
activation_stats_dict[tensor_name]["rmax"] = tensor_max
activation_stats_dict[tensor_name] = {"rmin": tensor_min, "rmax": tensor_max}


def _combine_lists_with_common_elements(data: List[List[str]]) -> List[List[str]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import coremltools as ct
from coremltools.optimize.coreml.experimental._post_training_quantization import (
_get_activation_calibration_stats,
_update_tensor_range,
)
from coremltools.optimize.coreml.experimental._model_debugger import ModelDebugger
from coremltools.test.optimize.coreml.test_passes import TestCompressionPasses
import coremltools.optimize as cto

Expand Down Expand Up @@ -76,6 +78,25 @@ def forward(self, img): # convert + flatten


class TestGetActivationStats(TestActivationQuantization):
def test_update_tensor_range_accumulates_running_min_max(self):
stats = {}

_update_tensor_range("my_tensor", np.array([0.0, 5.0, 10.0]), stats)
_update_tensor_range("my_tensor", np.array([2.0, 3.0, 5.0]), stats)
_update_tensor_range("my_tensor", np.array([-1.0, 7.0, 15.0]), stats)

assert stats["my_tensor"]["rmin"] == -1.0
assert stats["my_tensor"]["rmax"] == 15.0

def test_record_intermediate_output_accumulates_running_min_max(self):
stats = {}

ModelDebugger.record_intermediate_output(np.array([0.0, 5.0, 10.0]), "my_tensor", stats)
ModelDebugger.record_intermediate_output(np.array([2.0, 3.0, 5.0]), "my_tensor", stats)
ModelDebugger.record_intermediate_output(np.array([-1.0, 7.0, 15.0]), "my_tensor", stats)

assert stats["my_tensor"]["rmin"] == -1.0
assert stats["my_tensor"]["rmax"] == 15.0

def test_activation_quantization(self):
"""
Expand Down
17 changes: 17 additions & 0 deletions coremltools/test/optimize/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
from coremltools.optimize import _utils as optimize_utils


class TestUpdateTensorRange:
@pytest.mark.parametrize(
"stats",
[
{},
{"my_tensor": {"rmin": float("inf"), "rmax": float("-inf")}},
],
)
def test_accumulates_running_min_max(self, stats):
optimize_utils._update_tensor_range("my_tensor", np.array([0.0, 5.0, 10.0]), stats)
optimize_utils._update_tensor_range("my_tensor", np.array([2.0, 3.0, 5.0]), stats)
optimize_utils._update_tensor_range("my_tensor", np.array([-1.0, 7.0, 15.0]), stats)

assert stats["my_tensor"]["rmin"] == -1.0
assert stats["my_tensor"]["rmax"] == 15.0


class TestComputeQuantizationParams:
@pytest.mark.parametrize(
"quant_mode, rank, block_size",
Expand Down