Skip to content

Commit 17ed8d5

Browse files
authored
Resolve metric names in one place
Summary: Enabling change for the rest of the stack. `calculate_numeric_gap` resolved "MSE"/"L1"/"SNR" with an if/elif chain inline in its body, so no other caller could reuse it. Moved to `numerical_comparator` as `comparator_class_for_metric`. Required because the next diff up wraps the resolved comparator in a decorator, so it has to resolve the metric name itself. Same names, same behaviour; only the error text changes. Differential Revision: D119832728 Pull Request resolved: pytorch#23196
1 parent 0c1f7a4 commit 17ed8d5

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎devtools/inspector/_inspector.py‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@
7373
IntermediateOutputCapturer,
7474
)
7575
from executorch.devtools.inspector.numerical_comparator import (
76-
L1Comparator,
77-
MSEComparator,
76+
comparator_class_for_metric,
7877
NumericalComparatorBase,
79-
SNRComparator,
8078
)
8179
from executorch.exir import ExportedProgram
8280

@@ -1647,15 +1645,7 @@ def calculate_numeric_gap(
16471645
if comparator.inspector is None:
16481646
comparator.inspector = self
16491647
else:
1650-
metric = distance.strip().upper()
1651-
if metric == "MSE":
1652-
comparator = MSEComparator(inspector=self)
1653-
elif metric == "L1":
1654-
comparator = L1Comparator(inspector=self)
1655-
elif metric == "SNR":
1656-
comparator = SNRComparator(inspector=self)
1657-
else:
1658-
raise ValueError(f"Unsupported distance metric {distance!r}")
1648+
comparator = comparator_class_for_metric(distance)(inspector=self)
16591649

16601650
# Delegate to comparator's compare method (includes preprocessing)
16611651
df = comparator.compare(

‎devtools/inspector/numerical_comparator/__init__.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,27 @@
2525
)
2626

2727

28+
# The metric names Inspector.calculate_numeric_gap accepts as `distance`.
29+
_COMPARATOR_BY_NAME: dict[str, type[NumericalComparatorBase]] = {
30+
"L1": L1Comparator,
31+
"MSE": MSEComparator,
32+
"SNR": SNRComparator,
33+
}
34+
35+
36+
def comparator_class_for_metric(name: str) -> type[NumericalComparatorBase]:
37+
"""Resolve a built-in metric name to its comparator class."""
38+
comparator_cls = _COMPARATOR_BY_NAME.get(name.strip().upper())
39+
if comparator_cls is None:
40+
raise ValueError(
41+
f"Unsupported metric {name!r}; expected one of "
42+
f"{sorted(_COMPARATOR_BY_NAME)}"
43+
)
44+
return comparator_cls
45+
46+
2847
__all__ = [
48+
"comparator_class_for_metric",
2949
"DebugHandle",
3050
"IntermediateOutputMapping",
3151
"L1Comparator",

0 commit comments

Comments
 (0)