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

Generate report for troubleshooting the quantized model #1090

Merged
merged 50 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
22675ca
Init commit of xquant
May 27, 2024
58bfb76
remove commented out code
May 27, 2024
86a2ff2
add dummy methods if fw packages were not found
May 27, 2024
9b5b730
add histograms collection
May 29, 2024
87b0f1d
Fix comments in pytorch facade
May 29, 2024
90f19f4
Remove core config comments
Jun 3, 2024
6c8f10d
simplify xquantconfig
Jun 3, 2024
8bfa93d
rename core report generator
Jun 3, 2024
c0365a4
add similarity metrics
Jun 3, 2024
124c9d5
Add dataset utils
Jun 3, 2024
f17baee
Add model folding module
Jun 3, 2024
d978834
add tensorboard utils
Jun 4, 2024
c5c3f61
remove logger and use python logging
Jun 4, 2024
2afa2b2
Move default similarity metrics names to SimilarityMetrics
Jun 4, 2024
a1a18e2
split similarity into module with similairty functions and similarity…
Jun 5, 2024
7592a3f
add tests for more networks
Jun 5, 2024
122f1bd
Add tests to workflows
Jun 5, 2024
3f45e7a
add temp logger
Jun 5, 2024
332d96e
finalize logger
Jun 6, 2024
401c9d6
add comments
Jun 6, 2024
794a561
rename wrapped_dataset to prepare_dataset
Jun 6, 2024
3c67bfe
rename metrics to similarity metrics
Jun 9, 2024
c38e43f
add comments
Jun 9, 2024
36136f7
move package into model_compression_toolkit
Jun 9, 2024
7dd9e3d
add keras notebook
Jun 9, 2024
e23663f
add pytorch notebook
Jun 9, 2024
01da6e9
fix imports in keras notebook
Jun 9, 2024
072034c
replace inference manner to solve issue with tf2.13
Jun 10, 2024
092f8f9
use mct logger
Jun 13, 2024
b866f7a
remove duplicated license
Jun 13, 2024
0e367ec
rename arguments in similairty functions to be more general
Jun 13, 2024
4f7dc18
avoid double access to dict in similarity calculator
Jun 13, 2024
32b112a
rename ModelAnalyzerUtils to ModelAnalyzer
Jun 13, 2024
10e00ea
add no cover for abstract methods error
Jun 13, 2024
c94559f
use tag for tensorboard artifacts
Jun 13, 2024
7c177a9
take stats collection out of tensorboard class
Jun 13, 2024
1478bf9
store model outputs in activation dict during activations extraction
Jun 13, 2024
15e56e1
add keras model analyzer unit tests
Jun 13, 2024
ddd178b
add similarity functions unit tests
Jun 15, 2024
d6d8265
add tests to tensorboard
Jun 16, 2024
660b201
add colab links to notebooks
Jun 16, 2024
648fb23
remove outputs from keras notebook
Jun 16, 2024
eb5e3f6
fix install sections in notebooks
Jun 16, 2024
0bc2e61
remove sys part from notebooks
Jun 16, 2024
7a0830c
fix bad import in pytorch notebook
Jun 16, 2024
c8d764c
fix PR comments
Jun 16, 2024
517f8f9
use different images in notebooks with red marks for clarity
Jun 16, 2024
f186664
limit numpy to version < 2.0 that was just released
Jun 16, 2024
8e35f8b
Revert "limit numpy to version < 2.0 that was just released"
Jun 17, 2024
9272a97
Merge remote-tracking branch 'origin/main' into xquant
Jun 17, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/run_keras_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ jobs:
run: |
python -m unittest discover tests/keras_tests/pruning_tests -v
python -m unittest discover tests/keras_tests/non_parallel_tests -v
for script in tests/keras_tests/exporter_tests tests/keras_tests/feature_networks_tests tests/keras_tests/graph_tests tests/keras_tests/layer_tests; do python -m unittest discover $script -v & pids+=($!); done; for pid in ${pids[@]}; do wait $pid || exit 1; done
for script in tests/xquant_tests/keras_tests tests/keras_tests/exporter_tests tests/keras_tests/feature_networks_tests tests/keras_tests/graph_tests tests/keras_tests/layer_tests; do python -m unittest discover $script -v & pids+=($!); done; for pid in ${pids[@]}; do wait $pid || exit 1; done

4 changes: 3 additions & 1 deletion .github/workflows/run_pytorch_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
pip install -r requirements.txt
pip install torch==${{ inputs.torch-version }} torchvision onnx onnxruntime
- name: Run unittests
run: python -m unittest discover tests/pytorch_tests -v
run: |
python -m unittest discover tests/xquant_tests/pytorch_tests -v
python -m unittest discover tests/pytorch_tests -v



Expand Down
19 changes: 19 additions & 0 deletions model_compression_toolkit/xquant/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from model_compression_toolkit.xquant.common.xquant_config import XQuantConfig
from model_compression_toolkit.xquant.keras.facade_xquant_report import xquant_report_keras_experimental
from model_compression_toolkit.xquant.pytorch.facade_xquant_report import xquant_report_pytorch_experimental

15 changes: 15 additions & 0 deletions model_compression_toolkit/xquant/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#
38 changes: 38 additions & 0 deletions model_compression_toolkit/xquant/common/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# Default similarity metric names:
CS_SIMILARITY_METRIC_NAME = 'cs'
SQNR_SIMILARITY_METRIC_NAME = 'sqnr'
MSE_SIMILARITY_METRIC_NAME = 'mse'

# Report components names:
OUTPUT_SIMILARITY_METRICS_REPR = 'output_similarity_metrics_repr'
OUTPUT_SIMILARITY_METRICS_VAL = 'output_similarity_metrics_val'
INTERMEDIATE_SIMILARITY_METRICS_REPR = 'intermediate_similarity_metrics_repr'
INTERMEDIATE_SIMILARITY_METRICS_VAL = 'intermediate_similarity_metrics_val'

# Graph attribute names:
XQUANT_REPR = 'xquant_repr'
XQUANT_VAL = 'xquant_val'

# Report file name:
REPORT_FILENAME = 'quant_report.json'

# Tag to use in tensorboard for the graph we plot:
TENSORBOARD_DEFAULT_TAG = 'xquant'

# When extracting the activations of a model we hold the output using a dedicated key:
MODEL_OUTPUT_KEY = 'model_output_key'
83 changes: 83 additions & 0 deletions model_compression_toolkit/xquant/common/core_report_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from tqdm import tqdm
from typing import Callable, Any, Dict

from model_compression_toolkit.core.common.model_collector import ModelCollector
from model_compression_toolkit.xquant import XQuantConfig
from model_compression_toolkit.xquant.common.constants import OUTPUT_SIMILARITY_METRICS_REPR, OUTPUT_SIMILARITY_METRICS_VAL, INTERMEDIATE_SIMILARITY_METRICS_REPR, \
INTERMEDIATE_SIMILARITY_METRICS_VAL
from model_compression_toolkit.xquant.common.framework_report_utils import FrameworkReportUtils


def core_report_generator(float_model: Any,
quantized_model: Any,
repr_dataset: Callable,
validation_dataset: Callable,
fw_report_utils: FrameworkReportUtils,
xquant_config: XQuantConfig) -> Dict[str, Any]:
"""
Generate report in tensorboard with a graph of the quantized model and similarity metrics that
have been measured when comparing to the float model (or any other two models).
The report also contains histograms that are collected on the baseline model (usually, the float
model).

Args:
float_model (Any): The original floating-point model.
quantized_model (Any): The model after quantization.
repr_dataset (Callable): Representative dataset used for similarity metrics computation.
validation_dataset (Callable): Validation dataset used for similarity metrics computation.
fw_report_utils (FrameworkReportUtils): Utilities for generating framework-specific reports.
xquant_config (XQuantConfig): Configuration settings for explainable quantization.

Returns:
Dict[str, Any]: A dictionary containing the collected similarity metrics and report data.
"""
# Collect histograms on the float model.
float_graph = fw_report_utils.model_folding_utils.create_float_folded_graph(float_model, repr_dataset)
mi = ModelCollector(float_graph, fw_report_utils.fw_impl, fw_report_utils.fw_info)
for _data in tqdm(repr_dataset(), desc="Collecting Histograms"):
mi.infer(_data)

# Collect histograms and add them to Tensorboard.
fw_report_utils.tb_utils.add_histograms_to_tensorboard(graph=float_graph)

# Compute similarity metrics on representative dataset and validation set.
repr_similarity = fw_report_utils.similarity_calculator.compute_similarity_metrics(float_model=float_model,
quantized_model=quantized_model,
dataset=repr_dataset,
custom_similarity_metrics=xquant_config.custom_similarity_metrics)
val_similarity = fw_report_utils.similarity_calculator.compute_similarity_metrics(float_model=float_model,
quantized_model=quantized_model,
dataset=validation_dataset,
custom_similarity_metrics=xquant_config.custom_similarity_metrics,
is_validation=True)
similarity_metrics = {
OUTPUT_SIMILARITY_METRICS_REPR: repr_similarity[0],
OUTPUT_SIMILARITY_METRICS_VAL: val_similarity[0],
INTERMEDIATE_SIMILARITY_METRICS_REPR: repr_similarity[1],
INTERMEDIATE_SIMILARITY_METRICS_VAL: val_similarity[1]
}

# Add a graph of the quantized model with the similarity metrics to TensorBoard for visualization.
fw_report_utils.tb_utils.add_graph_to_tensorboard(quantized_model,
similarity_metrics,
repr_dataset)

# Save data to a json file.
fw_report_utils.dump_report_to_json(report_dir=xquant_config.report_dir,
collected_data=similarity_metrics)

return similarity_metrics
43 changes: 43 additions & 0 deletions model_compression_toolkit/xquant/common/dataset_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from typing import Callable

from model_compression_toolkit.logger import Logger


class DatasetUtils:
"""
Class with helpful methods for handling different kinds of datasets from the user.
"""

@staticmethod
def prepare_dataset(dataset: Callable, is_validation: bool, device: str = None):
"""
Prepare the dataset so calling it will return only inputs for the model (like in the case
of the representative dataset). For example, when the validation dataset is used, the labels
should be removed.

Args:
dataset: Dataset to prepare.
is_validation: Whether it's validation dataset or not.
device: Device to transfer the data to.

Returns:
Generator to use for retrieving the dataset inputs.
"""

Logger.critical("This method should be implemented by the framework-specific DatasetUtils.") # pragma: no cover

89 changes: 89 additions & 0 deletions model_compression_toolkit/xquant/common/framework_report_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

import json
import os

from model_compression_toolkit.core.common.framework_implementation import FrameworkImplementation
from model_compression_toolkit.core.common.framework_info import FrameworkInfo
from typing import Any, Dict

from model_compression_toolkit.xquant.common.constants import REPORT_FILENAME
from model_compression_toolkit.xquant.common.dataset_utils import DatasetUtils
from model_compression_toolkit.xquant.common.model_folding_utils import ModelFoldingUtils
from model_compression_toolkit.xquant.common.similarity_calculator import SimilarityCalculator
from model_compression_toolkit.xquant.common.tensorboard_utils import TensorboardUtils
from model_compression_toolkit.logger import Logger


class FrameworkReportUtils:
"""
Class with various utility components required for generating the report in a specific framework.
"""

def __init__(self,
fw_info: FrameworkInfo,
fw_impl: FrameworkImplementation,
similarity_calculator: SimilarityCalculator,
dataset_utils: DatasetUtils,
model_folding_utils: ModelFoldingUtils,
tb_utils: TensorboardUtils):
"""
Initializes the FrameworkReportUtils class with various utility components required for generating the report.

Args:
fw_info (FrameworkInfo): Information about the framework being used.
fw_impl (FrameworkImplementation): The implemented functions of the framework.
similarity_calculator (SimilarityCalculator): A utility for calculating similarity metrics.
dataset_utils (DatasetUtils): Utilities for handling datasets.
model_folding_utils (ModelFoldingUtils): Utilities for model folding operations.
tb_utils (TensorboardUtils): Utilities for TensorBoard operations.
"""
self.fw_info = fw_info
self.fw_impl = fw_impl
self.similarity_calculator = similarity_calculator
self.dataset_utils = dataset_utils
self.model_folding_utils = model_folding_utils
self.tb_utils = tb_utils

def create_report_directory(self, dir_path: str):
"""
Create a directory for saving reports.

Args:
dir_path (str): The path to the directory to create.

"""
if not os.path.exists(dir_path):
os.makedirs(dir_path, exist_ok=True)
Logger.info(f"Directory created at: {dir_path}")

def dump_report_to_json(self,
report_dir: str,
collected_data: Dict[str, Any]):
"""
Dump the collected data (similarity, etc.) into a JSON file.

Args:
report_dir (str): Directory where the report will be saved.
collected_data (Dict[str, Any]): Data collected during report generation.

"""
report_file_name = os.path.join(report_dir, REPORT_FILENAME)
report_file_name = os.path.abspath(report_file_name)
Logger.info(f"Dumping report data to: {report_file_name}")

with open(report_file_name, 'w') as f:
json.dump(collected_data, f, indent=4)
Loading
Loading