-
Notifications
You must be signed in to change notification settings - Fork 607
Adding model stats to aot_arm_compiler #5816
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import random | ||
import tempfile | ||
import unittest | ||
|
||
import torch | ||
from executorch.backends.arm.util.arm_model_evaluator import GenericModelEvaluator | ||
|
||
random.seed(0) | ||
|
||
# Create an input that is hard to compress | ||
COMPRESSION_RATIO_TEST = bytearray(random.getrandbits(8) for _ in range(1000000)) | ||
|
||
|
||
def mocked_model_1(input: torch.Tensor) -> torch.Tensor: | ||
return torch.tensor([1.0, 2.0, 3.0, 4.0]) | ||
|
||
|
||
def mocked_model_2(input: torch.Tensor) -> torch.Tensor: | ||
return torch.tensor([1.0, 2.0, 3.0, 3.0]) | ||
|
||
|
||
class TestGenericModelEvaluator(unittest.TestCase): | ||
"""Tests the GenericModelEvaluator class.""" | ||
|
||
def test_get_model_error(self): | ||
example_input = torch.tensor([[1.0, 2.0, 3.0, 4.0]]) | ||
evaluator = GenericModelEvaluator( | ||
"dummy_model", | ||
mocked_model_1, | ||
mocked_model_2, | ||
example_input, | ||
"tmp/output_tag0.tosa", | ||
) | ||
max_error, max_absolute_error, max_percentage_error, mae = ( | ||
evaluator.get_model_error() | ||
) | ||
|
||
self.assertEqual(max_error, 1.0) | ||
self.assertEqual(max_absolute_error, 1.0) | ||
self.assertEqual(max_percentage_error, 25.0) | ||
self.assertEqual(mae, 0.25) | ||
|
||
def test_get_compression_ratio(self): | ||
with tempfile.NamedTemporaryFile(delete=True) as temp_bin: | ||
temp_bin.write(COMPRESSION_RATIO_TEST) | ||
|
||
# As the size of the file is quite small we need to call flush() | ||
temp_bin.flush() | ||
temp_bin_name = temp_bin.name | ||
|
||
example_input = torch.tensor([[1.0, 2.0, 3.0, 4.0]]) | ||
evaluator = GenericModelEvaluator( | ||
"dummy_model", | ||
mocked_model_1, | ||
mocked_model_2, | ||
example_input, | ||
temp_bin_name, | ||
) | ||
|
||
ratio = evaluator.get_compression_ratio() | ||
self.assertAlmostEqual(ratio, 1.0, places=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import os | ||
import tempfile | ||
import zipfile | ||
from typing import Optional, Tuple, Union | ||
|
||
import torch | ||
|
||
|
||
class GenericModelEvaluator: | ||
def __init__( | ||
self, | ||
model_name: str, | ||
fp32_model: torch.nn.Module, | ||
int8_model: torch.nn.Module, | ||
example_input: Tuple[torch.Tensor], | ||
tosa_output_path: Optional[str], | ||
) -> None: | ||
self.model_name = model_name | ||
|
||
self.fp32_model = fp32_model | ||
self.int8_model = int8_model | ||
self.example_input = example_input | ||
|
||
if tosa_output_path: | ||
self.tosa_output_path = tosa_output_path | ||
else: | ||
self.tosa_output_path = None | ||
|
||
def get_model_error(self) -> Union[float, float, float, float]: | ||
""" | ||
Returns the following metrics between the outputs of the FP32 and INT8 model: | ||
- Maximum error | ||
- Maximum absolute error | ||
- Maximum percentage error | ||
- Mean absolute error | ||
""" | ||
fp32_output = self.fp32_model(*self.example_input) | ||
int8_output = self.int8_model(*self.example_input) | ||
|
||
difference = fp32_output - int8_output | ||
percentage_error = torch.div(difference, fp32_output) * 100 | ||
|
||
max_error = torch.max(difference).item() | ||
max_absolute_error = torch.max(torch.abs(difference)).item() | ||
max_percentage_error = torch.max(percentage_error).item() | ||
mean_absolute_error = torch.mean(torch.abs(difference).float()).item() | ||
|
||
return max_error, max_absolute_error, max_percentage_error, mean_absolute_error | ||
|
||
def get_compression_ratio(self) -> float: | ||
"""Compute the compression ratio of the outputted TOSA flatbuffer.""" | ||
with tempfile.NamedTemporaryFile(delete=True, suffix=".zip") as temp_zip: | ||
with zipfile.ZipFile( | ||
temp_zip.name, "w", compression=zipfile.ZIP_DEFLATED | ||
) as f: | ||
f.write(self.tosa_output_path) | ||
|
||
compression_ratio = os.path.getsize( | ||
self.tosa_output_path | ||
) / os.path.getsize(temp_zip.name) | ||
|
||
return compression_ratio | ||
|
||
def evaluate(self) -> dict[any]: | ||
max_error, max_absolute_error, max_percent_error, mean_absolute_error = ( | ||
self.get_model_error() | ||
) | ||
output_metrics = { | ||
"name": self.model_name, | ||
"metrics": { | ||
"max_error": max_error, | ||
"max_absolute_error": max_absolute_error, | ||
"max_percentage_error": max_percent_error, | ||
"mean_absolute_error": mean_absolute_error, | ||
}, | ||
} | ||
|
||
if self.tosa_output_path: | ||
output_metrics["metrics"][ | ||
"compression_ratio" | ||
] = self.get_compression_ratio() | ||
|
||
return output_metrics |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
torch.rand() fp32 dtype -> save it as bytesio?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_get_compression_ratio
tests a file on a filesystem, so not sure if bytesio works here (I understand it's for writing in memory). Unless I'm missing something?I can look into using torch.rand() instead.