已合并
【fix】set_tol_and_add_fallback #40178
【fix】set_tol_and_add_fallback #40178
已合并
Lu_G创建于 7月6日
2 个文件变更+33-0
Mbenchmarks/torchbench/npu_support.py+16-0
@@ -564,6 +564,14 @@ def _patch_model_21():
564 # The current operator suffers from severe performance degradation.564 # The current operator suffers from severe performance degradation.
565 # This patch will be removed after the issue is fixed in the future.565 # This patch will be removed after the issue is fixed in the future.
566 patch_remove_decomposition(["aten._softmax"])566 patch_remove_decomposition(["aten._softmax"])
567+ try:
568+ from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir import (
569+ config as anir_config,
570+ )
571+ 
572+ anir_config.force_fallback_kernel_names["mlir_fused_add_lt_neg_where_16"] = True
573+ except ImportError:
574+ log.warning("import config failed for hf_T5_base patch")
atomgit-bot
atomgit-botatomgit-bot7月6日

🟡 Medium Priority

register_patch 装饰器通过字典赋值 _patch_table[model_name] = fn 来注册补丁,后注册的会覆盖先注册的。

_patch_model_3 修复了 T5Attention.forward 中两次连续 _to_copy 导致的精度问题(将 scores += position_bias_masked 替换为 scores = scores.float() + position_bias_masked),是一个关键的精度修复。_patch_model_21 仅包含 patch_remove_decomposition(["aten._softmax"]) 和 force fallback 内核设置,不包含 _patch_model_3 的逻辑。

后果:对 hf_T5_base 模型,_patch_model_3 不再执行,T5Attention.forward 不会被替换,可能导致该模型的精度检查失败。

触发条件:在 NPU 上运行 hf_T5_base 模型的 benchmark 精度检查时。

建议:需要在 _patch_model_21 中集成 _patch_model_3 的逻辑,或者修改 register_patch 机制使其支持同一模型注册多个补丁。最直接的修复:在 _patch_model_21 函数开头添加 _patch_model_3 中的 T5Attention.forward 替换逻辑。或者,如果确认 _patch_model_3 的修复对 hf_T5_base 已不再需要(例如已被其他机制覆盖),应在代码注释中明确说明原因。

likedislike
567 575 
568 576 
569@register_patch("hf_T5_large")577@register_patch("hf_T5_large")
@@ -571,6 +579,14 @@ def _patch_model_22():
571 # The current operator suffers from severe performance degradation.579 # The current operator suffers from severe performance degradation.
572 # This patch will be removed after the issue is fixed in the future.580 # This patch will be removed after the issue is fixed in the future.
573 patch_remove_decomposition(["aten._softmax"])581 patch_remove_decomposition(["aten._softmax"])
582+ try:
583+ from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir import (
584+ config as anir_config,
585+ )
586+ 
587+ anir_config.force_fallback_kernel_names["mlir_fused_add_lt_neg_where_16"] = True
588+ except ImportError:
589+ log.warning("import config failed for hf_T5_large patch")
574 590 
575 591 
576@register_patch("pytorch_unet")592@register_patch("pytorch_unet")
Mbenchmarks/torchbench/torchbench.py+17-0
@@ -45,9 +45,15 @@ from torch._dynamo.utils import clone_inputs
45 45 
46 46 
47try:47try:
48+ import torch_npu
48 from npu_support import _patch_environment_variables49 from npu_support import _patch_environment_variables
49except ImportError:50except ImportError:
50 pass51 pass
52+try:
53+ from torch_npu.npu._backends import get_soc_version
54+ is_ascend950 = get_soc_version() >= 260 # Ascend950 = 260
55+except (ImportError, AttributeError):
56+ is_ascend950 = False
51from benchmark.userbenchmark.dynamo.dynamobench.torchbench import (57from benchmark.userbenchmark.dynamo.dynamobench.torchbench import (
52 DONT_CHANGE_BATCH_SIZE,58 DONT_CHANGE_BATCH_SIZE,
53 FORCE_AMP_FOR_FP16_BF16_MODELS,59 FORCE_AMP_FOR_FP16_BF16_MODELS,
@@ -120,6 +126,15 @@ NPU_REQUIRE_HIGHER_TOLERANCE = {
120 "phlippe_resnet",126 "phlippe_resnet",
121}127}
122 128 
129+NPU_REQUIRE_HIGHER_TOLERANCE_ON_A5 = {
130+ "densenet121",
131+ "resnet50",
132+ "resnet18",
133+ "resnext50_32x4d",
134+ "resnet152",
135+}
136+ 
137+ 
123 138 
124NPU_REQUIRE_EVEN_HIGHER_TOLERANCE = {139NPU_REQUIRE_EVEN_HIGHER_TOLERANCE = {
125 "shufflenet_v2_x1_0",140 "shufflenet_v2_x1_0",
@@ -438,6 +453,8 @@ class TorchBenchmarkRunner(BenchmarkRunner):
438 tolerance = 1e-2453 tolerance = 1e-2
439 elif name in NPU_REQUIRE_EVEN_HIGHER_TOLERANCE:454 elif name in NPU_REQUIRE_EVEN_HIGHER_TOLERANCE:
440 tolerance = 2e-2455 tolerance = 2e-2
456+ elif is_ascend950 and name in NPU_REQUIRE_HIGHER_TOLERANCE_ON_A5:
457+ tolerance = 2e-3
441 return tolerance, cosine458 return tolerance, cosine
442 459 
443 def get_learning_rate(self, is_training, current_device, name):460 def get_learning_rate(self, is_training, current_device, name):