forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_evaluator.py
More file actions
90 lines (70 loc) · 2.68 KB
/
metrics_evaluator.py
File metadata and controls
90 lines (70 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (c) Qualcomm Innovation Center, Inc.
# 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.
from abc import ABC, abstractmethod
from typing import Any, Tuple
import torch
class MetricEvaluatorBase(ABC):
@abstractmethod
def metric_name(self) -> str:
"""
A name for this metric evaluation
Returns:
str: name of the metric evaluation
"""
...
@abstractmethod
def evaluate(
self, qnn_output: torch.Tensor, cpu_output: torch.Tensor, **kwargs
) -> Tuple[Any, bool]:
"""
This abstract method should accept both QNN and CPU outputs for a single layer.
Define your own logic to compare the results.
Args:
qnn_output (torch.Tensor): QNN intermediate output
cpu_output (torch.Tensor): CPU intermediate output
Returns:
Tuple[Any, bool]: Return 2 elements:
1) Score or anything that you would like to be printed under metrics category for svg graph or csv file.
2) A boolean that indicates whether the evaluation result is acceptable or not.
"""
...
class AtolEvaluator(MetricEvaluatorBase):
def __init__(self, threshold=1e-1):
self.threshold = threshold
def metric_name(self) -> str:
return "Atol Similarity"
def evaluate(
self, qnn_output: torch.Tensor, cpu_output: torch.Tensor
) -> Tuple[Any, bool]:
avg_atol = torch.mean(torch.abs(qnn_output - cpu_output))
valid = avg_atol < self.threshold
formatted_score = f"{avg_atol:.3f}"
return formatted_score, valid
class CosineSimilarityEvaluator(MetricEvaluatorBase):
def __init__(self, threshold=0.9):
self.threshold = threshold
def metric_name(self) -> str:
return "Cosine Similarity"
def evaluate(
self, qnn_output: torch.Tensor, cpu_output: torch.Tensor
) -> Tuple[Any, bool]:
score = torch.nn.functional.cosine_similarity(
qnn_output.flatten(), cpu_output.flatten(), dim=0
).item()
valid = score > self.threshold
formatted_score = f"{score:.3f}"
return formatted_score, valid
class MeanSquaredErrorEvaluator(MetricEvaluatorBase):
def __init__(self, threshold=0.01):
self.threshold = threshold
def metric_name(self) -> str:
return "Mean Squared Error"
def evaluate(
self, qnn_output: torch.Tensor, cpu_output: torch.Tensor
) -> Tuple[Any, bool]:
mse = torch.mean((qnn_output - cpu_output) ** 2)
valid = mse < self.threshold
return mse, valid