[torchbench] Fix mobilenetv2 inductor freezing fail_accuracy (#145296)

Issue: https://github.com/pytorch/pytorch/issues/144891

inductor freezing effectively enables inductor conv-batchnorm fusion. This fusion increases the accuracy error.

More context about this: https://github.com/pytorch/pytorch/issues/120545
For Timm models that are run through benchmarks/dynamo/timm_models.py with TimsRunner the tolerance was increased here:
https://github.com/pytorch/pytorch/blob/main/benchmarks/dynamo/timm_models.py#L367

If to comment out  conv-batchnorm fusion as Elias suggested in Context issue, the accuracy is back.

=>
Increasing tolerace for mobilenetv2  to the same value via introducing the special configuration for tolerance for freezing only

Pull Request resolved: https://github.com/pytorch/pytorch/pull/145296
Approved by: https://github.com/eellison, https://github.com/zou3519
This commit is contained in:
IvanKobzarev
2025-01-22 03:12:07 -08:00
committed by PyTorch MergeBot
parent 0dbff7e4be
commit c2b401933f
2 changed files with 15 additions and 0 deletions

View File

@ -413,6 +413,13 @@ class TorchBenchmarkRunner(BenchmarkRunner):
cosine = self.args.cosine
# Increase the tolerance for torch allclose
if self.args.float16 or self.args.amp:
if self.args.freezing and (freezing := self._tolerance["freezing"]):
higher_fp16 = freezing.get("higher_fp16", None)
even_higher = freezing.get("even_higher", None)
if higher_fp16 and name in higher_fp16:
return 1e-2, cosine
elif even_higher and name in even_higher:
return 8 * 1e-2, cosine
if name in self._tolerance["higher_fp16"]:
return 1e-2, cosine
elif name in self._tolerance["even_higher"]:

View File

@ -54,6 +54,14 @@ tolerance:
- drq
- hf_Whisper
freezing:
# Similar logic to timm_models.py:get_tolerance_and_cosine_flag
# the conv-batchnorm fusion used under freezing may cause relatively
# large numerical difference. We need are larger tolerance.
# Check https://github.com/pytorch/pytorch/issues/120545 for context
even_higher:
- mobilenet_v2
cosine: []
require_larger_multiplier_for_smaller_tensor: