-
Notifications
You must be signed in to change notification settings - Fork 646
[Backend Tester] Add tensor error statistic reporting #12809
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
Open
GregoryComer
wants to merge
5
commits into
gh/GregoryComer/88/head
Choose a base branch
from
gh/GregoryComer/91/head
base: gh/GregoryComer/88/head
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−10
Open
Changes from all commits
Commits
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,99 @@ | ||
from dataclasses import dataclass | ||
|
||
import torch | ||
from torch.ao.ns.fx.utils import compute_sqnr | ||
|
||
|
||
@dataclass | ||
class TensorStatistics: | ||
"""Contains summary statistics for a tensor.""" | ||
|
||
shape: torch.Size | ||
""" The shape of the tensor. """ | ||
|
||
numel: int | ||
""" The number of elements in the tensor. """ | ||
|
||
median: float | ||
""" The median of the tensor. """ | ||
|
||
mean: float | ||
""" The mean of the tensor. """ | ||
|
||
max: torch.types.Number | ||
""" The maximum element of the tensor. """ | ||
|
||
min: torch.types.Number | ||
""" The minimum element of the tensor. """ | ||
|
||
@classmethod | ||
def from_tensor(cls, tensor: torch.Tensor) -> "TensorStatistics": | ||
"""Creates a TensorStatistics object from a tensor.""" | ||
flattened = torch.flatten(tensor) | ||
return cls( | ||
shape=tensor.shape, | ||
numel=tensor.numel(), | ||
median=torch.quantile(flattened, q=0.5).item(), | ||
mean=flattened.mean().item(), | ||
max=flattened.max().item(), | ||
min=flattened.min().item(), | ||
) | ||
|
||
|
||
@dataclass | ||
class ErrorStatistics: | ||
"""Contains statistics derived from the difference of two tensors.""" | ||
|
||
reference_stats: TensorStatistics | ||
""" Statistics for the reference tensor. """ | ||
|
||
actual_stats: TensorStatistics | ||
""" Statistics for the actual tensor. """ | ||
|
||
error_l2_norm: float | None | ||
""" The L2 norm of the error between the actual and reference tensor. """ | ||
|
||
error_mae: float | None | ||
""" The mean absolute error between the actual and reference tensor. """ | ||
|
||
error_max: float | None | ||
""" The maximum absolute elementwise error between the actual and reference tensor. """ | ||
|
||
error_msd: float | None | ||
""" The mean signed deviation between the actual and reference tensor. """ | ||
|
||
sqnr: float | None | ||
""" The signal-to-quantization-noise ratio between the actual and reference tensor. """ | ||
|
||
@classmethod | ||
def from_tensors( | ||
cls, actual: torch.Tensor, reference: torch.Tensor | ||
) -> "ErrorStatistics": | ||
"""Creates an ErrorStatistics object from two tensors.""" | ||
actual = actual.to(torch.float64) | ||
reference = reference.to(torch.float64) | ||
|
||
if actual.shape != reference.shape: | ||
return cls( | ||
reference_stats=TensorStatistics.from_tensor(reference), | ||
actual_stats=TensorStatistics.from_tensor(actual), | ||
error_l2_norm=None, | ||
error_mae=None, | ||
error_max=None, | ||
error_msd=None, | ||
sqnr=None, | ||
) | ||
|
||
error = actual - reference | ||
flat_error = torch.flatten(error) | ||
|
||
return cls( | ||
reference_stats=TensorStatistics.from_tensor(reference), | ||
actual_stats=TensorStatistics.from_tensor(actual), | ||
error_l2_norm=torch.linalg.norm(flat_error).item(), | ||
error_mae=torch.mean(torch.abs(flat_error)).item(), | ||
error_max=torch.max(torch.abs(flat_error)).item(), | ||
error_msd=torch.mean(flat_error).item(), | ||
# Torch sqnr implementation requires float32 due to decorator logic | ||
sqnr=compute_sqnr(actual.to(torch.float), reference.to(torch.float)).item(), | ||
) |
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
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,65 @@ | ||
import unittest | ||
|
||
import torch | ||
from executorch.backends.test.harness.error_statistics import ErrorStatistics | ||
|
||
|
||
class ErrorStatisticsTests(unittest.TestCase): | ||
def test_error_stats_simple(self): | ||
tensor1 = torch.tensor([1, 2, 3, 4]) | ||
tensor2 = torch.tensor([2, 2, 2, 5]) | ||
|
||
error_stats = ErrorStatistics.from_tensors(tensor1, tensor2) | ||
|
||
# Check actual tensor statistics | ||
self.assertEqual(error_stats.actual_stats.shape, torch.Size([4])) | ||
self.assertEqual(error_stats.actual_stats.numel, 4) | ||
self.assertEqual(error_stats.actual_stats.median, 2.5) | ||
self.assertEqual(error_stats.actual_stats.mean, 2.5) | ||
self.assertEqual(error_stats.actual_stats.max, 4) | ||
self.assertEqual(error_stats.actual_stats.min, 1) | ||
|
||
# Check reference tensor statistics | ||
self.assertEqual(error_stats.reference_stats.shape, torch.Size([4])) | ||
self.assertEqual(error_stats.reference_stats.numel, 4) | ||
self.assertEqual(error_stats.reference_stats.median, 2.0) | ||
self.assertEqual(error_stats.reference_stats.mean, 2.75) | ||
self.assertEqual(error_stats.reference_stats.max, 5) | ||
self.assertEqual(error_stats.reference_stats.min, 2) | ||
|
||
# Check error statistics | ||
self.assertAlmostEqual(error_stats.error_l2_norm, 1.732, places=3) | ||
self.assertEqual(error_stats.error_mae, 0.75) | ||
self.assertEqual(error_stats.error_max, 1.0) | ||
self.assertEqual(error_stats.error_msd, -0.25) | ||
self.assertAlmostEqual(error_stats.sqnr, 10.0, places=3) | ||
|
||
def test_error_stats_different_shapes(self): | ||
# Create tensors with different shapes | ||
tensor1 = torch.tensor([1, 2, 3, 4]) | ||
tensor2 = torch.tensor([[2, 3], [4, 5]]) | ||
|
||
error_stats = ErrorStatistics.from_tensors(tensor1, tensor2) | ||
|
||
# Check actual tensor statistics | ||
self.assertEqual(error_stats.actual_stats.shape, torch.Size([4])) | ||
self.assertEqual(error_stats.actual_stats.numel, 4) | ||
self.assertEqual(error_stats.actual_stats.median, 2.5) | ||
self.assertEqual(error_stats.actual_stats.mean, 2.5) | ||
self.assertEqual(error_stats.actual_stats.max, 4) | ||
self.assertEqual(error_stats.actual_stats.min, 1) | ||
|
||
# Check reference tensor statistics | ||
self.assertEqual(error_stats.reference_stats.shape, torch.Size([2, 2])) | ||
self.assertEqual(error_stats.reference_stats.numel, 4) | ||
self.assertEqual(error_stats.reference_stats.median, 3.5) | ||
self.assertEqual(error_stats.reference_stats.mean, 3.5) | ||
self.assertEqual(error_stats.reference_stats.max, 5) | ||
self.assertEqual(error_stats.reference_stats.min, 2) | ||
|
||
# Check that all error values are None when shapes differ | ||
self.assertIsNone(error_stats.error_l2_norm) | ||
self.assertIsNone(error_stats.error_mae) | ||
self.assertIsNone(error_stats.error_max) | ||
self.assertIsNone(error_stats.error_msd) | ||
self.assertIsNone(error_stats.sqnr) |
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
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.
I'm not completely happy with the callback approach for exposing this, but I don't really have a better idea, since the tester relies on a builder-style pattern where it returns self to allow chaining. I'm open to suggestions.
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.
just update the tester method?
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.
Can you clarify what you're thinking? Are you meaning update the tester run_method_and_compare outputs to directly return the error stats and then update all of the callers to not use it in a chained fashion? Or something else?
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.
Modify the existing method, keeping the outside behavior but also add
def get_comparison_stats(self)
method on that stage or something?Or if you want to pass a callback for flexibility that's also fine.