已合并
【fix】add_models_patch #32864
Lu_G创建于 4月1日
【fix】add_models_patch #32864
已合并
共 10 个文件变更+100-23
| @@ -1066,6 +1066,11 @@ def parse_args(args=None): | |||
| 1066 | action="store_true", | 1066 | action="store_true", |
| 1067 | help="Use a fresh triton cachedir when running each model, to force cold-start compile.", | 1067 | help="Use a fresh triton cachedir when running each model, to force cold-start compile.", |
| 1068 | ) | 1068 | ) |
| 1069 | + parser.add_argument( | ||
| 1070 | + "--disable-aclgraph", | ||
| 1071 | + action="store_true", | ||
| 1072 | + help="Disables aclgraph for NPU Inductor", | ||
| 1073 | + ) | ||
| 1069 | parser.add_argument( | 1074 | parser.add_argument( |
| 1070 | "--disable-split-reductions", | 1075 | "--disable-split-reductions", |
| 1071 | action="store_true", | 1076 | action="store_true", |
| @@ -1176,9 +1181,6 @@ def main(runner, original_dir=None): | |||
| 1176 | if args.baseline: | 1181 | if args.baseline: |
| 1177 | args.baseline = os.path.abspath(args.baseline) | 1182 | args.baseline = os.path.abspath(args.baseline) |
| 1178 | 1183 | ||
| 1179 | - if is_npu_available and args.only: | ||
| 1180 | - patch_model(args.only) | ||
| 1181 | - | ||
| 1182 | args.use_distributed = (args.ddp) and args.only | 1184 | args.use_distributed = (args.ddp) and args.only |
| 1183 | if args.multiprocess: | 1185 | if args.multiprocess: |
| 1184 | # NB: Do NOT query device count before CUDA initialization; we're | 1186 | # NB: Do NOT query device count before CUDA initialization; we're |
| @@ -1200,6 +1202,18 @@ def main(runner, original_dir=None): | |||
| 1200 | def run(runner, args, original_dir=None): | 1202 | def run(runner, args, original_dir=None): |
| 1201 | # Pass the parsed args object to benchmark runner object | 1203 | # Pass the parsed args object to benchmark runner object |
| 1202 | runner.args = args | 1204 | runner.args = args |
| 1205 | + experiment = null_experiment | ||
| 1206 | + global current_name, current_device, current_batch_size, output_filename | ||
| 1207 | + optimize_ctx = contextlib.nullcontext() | ||
| 1208 | + | ||
| 1209 | + if args.backend: | ||
| 1210 | + optimize_ctx = configure_compile_options(args) | ||
| 1211 | + experiment = speedup_experiment | ||
| 1212 | + if args.accuracy: | ||
| 1213 | + output_filename = f"accuracy_{args.backend}.csv" | ||
| 1214 | + else: | ||
| 1215 | + output_filename = f"speedup_{args.backend}.csv" | ||
| 1216 | + | ||
| 1203 | if args.ddp: | 1217 | if args.ddp: |
| 1204 | # but just to measure impact on singlenode of performing graph-breaks. | 1218 | # but just to measure impact on singlenode of performing graph-breaks. |
| 1205 | # Left it as a follow up to keep this PR isolated. | 1219 | # Left it as a follow up to keep this PR isolated. |
| @@ -1309,18 +1323,6 @@ def run(runner, args, original_dir=None): | |||
| 1309 | if args.no_skip: | 1323 | if args.no_skip: |
| 1310 | runner.skip_models.clear() | 1324 | runner.skip_models.clear() |
| 1311 | 1325 | ||
| 1312 | - experiment = null_experiment | ||
| 1313 | - global current_name, current_device, current_batch_size, output_filename | ||
| 1314 | - optimize_ctx = contextlib.nullcontext() | ||
| 1315 | - | ||
| 1316 | - if args.backend: | ||
| 1317 | - optimize_ctx = configure_compile_options(args) | ||
| 1318 | - experiment = speedup_experiment | ||
| 1319 | - if args.accuracy: | ||
| 1320 | - output_filename = f"accuracy_{args.backend}.csv" | ||
| 1321 | - else: | ||
| 1322 | - output_filename = f"speedup_{args.backend}.csv" | ||
| 1323 | - | ||
| 1324 | runner.setup_amp() | 1326 | runner.setup_amp() |
| 1325 | 1327 | ||
| 1326 | if args.output: | 1328 | if args.output: |
| @@ -1351,6 +1353,9 @@ def run(runner, args, original_dir=None): | |||
| 1351 | if os.environ.get("USE_ACLOP", "0").upper() in ["1", "ON"]: | 1353 | if os.environ.get("USE_ACLOP", "0").upper() in ["1", "ON"]: |
| 1352 | torch_npu.npu.set_compile_mode(jit_compile=True) | 1354 | torch_npu.npu.set_compile_mode(jit_compile=True) |
| 1353 | 1355 | ||
| 1356 | + if is_npu_available: | ||
| 1357 | + patch_model(args.only) | ||
| 1358 | + | ||
| 1354 | model_name = args.only | 1359 | model_name = args.only |
| 1355 | for device in args.devices: | 1360 | for device in args.devices: |
| 1356 | batch_size = args.batch_size | 1361 | batch_size = args.batch_size |
| @@ -1506,12 +1511,15 @@ def configure_compile_options(args): | |||
| 1506 | NPU_MLIR_NO_ACLGRAPH = set() | 1511 | NPU_MLIR_NO_ACLGRAPH = set() |
| 1507 | npu_backend = args.npu_backend | 1512 | npu_backend = args.npu_backend |
| 1508 | # mode Config | 1513 | # mode Config |
| 1509 | - mode = None | 1514 | + if not args.disable_aclgraph: |
| 1515 | + mode = "max-autotune" | ||
| 1516 | + else: | ||
| 1517 | + mode = None | ||
| 1510 | if args.only is not None: | 1518 | if args.only is not None: |
| 1511 | - if npu_backend == "dvm" and args.only not in NPU_DVM_NO_ACLGRAPH: | 1519 | + if npu_backend == "dvm" and args.only in NPU_DVM_NO_ACLGRAPH: |
| 1512 | - mode = "max-autotune" | 1520 | + mode = None |
| 1513 | - elif npu_backend == "mlir" and args.only not in NPU_MLIR_NO_ACLGRAPH: | 1521 | + elif npu_backend == "mlir" and args.only in NPU_MLIR_NO_ACLGRAPH: |
| 1514 | - mode = "max-autotune" | 1522 | + mode = None |
| 1515 | # Backend Config | 1523 | # Backend Config |
| 1516 | backend = None | 1524 | backend = None |
| 1517 | if args.backend: | 1525 | if args.backend: |
| @@ -1530,8 +1538,7 @@ def configure_compile_options(args): | |||
| 1530 | "fullgraph": args.nopython, | 1538 | "fullgraph": args.nopython, |
| 1531 | "dynamic": dynamic, | 1539 | "dynamic": dynamic, |
| 1532 | } | 1540 | } |
| 1533 | - if mode is not None: | 1541 | + compile_kwargs["mode"] = mode |
| 1534 | - compile_kwargs["mode"] = mode | ||
| 1535 | if backend == "inductor" and hasattr(args, 'npu_backend'): | 1542 | if backend == "inductor" and hasattr(args, 'npu_backend'): |
| 1536 | if npu_backend == "default": | 1543 | if npu_backend == "default": |
| 1537 | npu_backend = get_npu_backend(args) | 1544 | npu_backend = get_npu_backend(args) |
| @@ -304,6 +304,7 @@ def _patch_model_9(): | |||
| 304 | def _patch_model_10(): | 304 | def _patch_model_10(): |
| 305 | try: | 305 | try: |
| 306 | from torch_npu.contrib import transfer_to_npu | 306 | from torch_npu.contrib import transfer_to_npu |
| 307 | + import torch_npu._inductor | ||
| 307 | except ImportError: | 308 | except ImportError: |
| 308 | log.warning("NPU_FlAG is False!") | 309 | log.warning("NPU_FlAG is False!") |
| 309 | return | 310 | return |
| @@ -578,13 +579,79 @@ def _patch_model_19(): | |||
| 578 | return hidden_state | 579 | return hidden_state |
| 579 | 580 | ||
| 580 | projected_embeddings = self.projection( | 581 | projected_embeddings = self.projection( |
| 581 | - hidden_state[torch.arange(hidden_state.shape[0]), text.argmax(dim=-1)] | 582 | + hidden_state[torch.arange(hidden_state.shape[0], device="npu"), text.argmax(dim=-1)] |
| 582 | ) | 583 | ) |
| 583 | return projected_embeddings | 584 | return projected_embeddings |
| 584 | 585 | ||
| 585 | CLIPTextEncoder.forward = new_forward | 586 | CLIPTextEncoder.forward = new_forward |
| 586 | 587 | ||
| 587 | 588 | ||
| 589 | +def patch_remove_ops_from_generate_list(op_names=None): | ||
| 590 | + try: | ||
| 591 | + import torch | ||
| 592 | + from torch_npu._inductor.ascend_npu_ir.ascend_npu_ir import config as anir_config | ||
| 593 | + | ||
| 594 | + if not op_names: | ||
| 595 | + print("[patch] No op names provided, nothing to do.") | ||
| 596 | + return | ||
| 597 | + | ||
| 598 | + for name in op_names: | ||
| 599 | + parts = name.split(".") | ||
| 600 | + op = torch.ops | ||
| 601 | + for p in parts: | ||
| 602 | + op = getattr(op, p) | ||
| 603 | + | ||
| 604 | + if op in anir_config.GENERATE_LIST: | ||
| 605 | + anir_config.GENERATE_LIST.remove(op) | ||
| 606 | + print(f"[patch] Successfully removed {name} from GENERATE_LIST.") | ||
| 607 | + else: | ||
| 608 | + print(f"[patch] {name} not found in GENERATE_LIST (maybe already removed).") | ||
| 609 | + | ||
| 610 | + except Exception as e: | ||
| 611 | + print(f"[patch] Failed to modify GENERATE_LIST: {e}") | ||
| 612 | + | ||
| 613 | + | ||
| 614 | + | ||
| 615 | +def _patch_model_20(): | ||
| 616 | + import numpy as np | ||
| 617 | + try: | ||
| 618 | + from torchbenchmark.models.speech_transformer.speech_transformer.transformer.attention import MultiHeadAttention, ScaledDotProductAttention | ||
| 619 | + except ImportError: | ||
| 620 | + log.warning("import torchvision fail or could not get MultiHeadAttention or ScaledDotProductAttention from module " | ||
| 621 | + "torchbenchmark.models.speech_transformer.transformer.attention") | ||
| 622 | + return | ||
| 623 | + | ||
| 624 | + def new_init(self, n_head, d_model, d_k, d_v, dropout=0.1): | ||
| 625 | + super(MultiHeadAttention, self).__init__() | ||
| 626 | + | ||
| 627 | + self.n_head = n_head | ||
| 628 | + self.d_k = d_k | ||
| 629 | + self.d_v = d_v | ||
| 630 | + | ||
| 631 | + self.w_qs = nn.Linear(d_model, n_head * d_k) | ||
| 632 | + self.w_ks = nn.Linear(d_model, n_head * d_k) | ||
| 633 | + self.w_vs = nn.Linear(d_model, n_head * d_v) | ||
| 634 | + nn.init.normal_(self.w_qs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k))) | ||
| 635 | + nn.init.normal_(self.w_ks.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_k))) | ||
| 636 | + nn.init.normal_(self.w_vs.weight, mean=0, std=np.sqrt(2.0 / (d_model + d_v))) | ||
| 637 | + | ||
| 638 | + # fix two different devices npu, cpu | ||
| 639 | + self.temperature = d_k ** 0.5 | ||
| 640 | + self.attention = ScaledDotProductAttention(temperature=self.temperature, | ||
| 641 | + attn_dropout=dropout) | ||
| 642 | + self.layer_norm = nn.LayerNorm(d_model) | ||
| 643 | + | ||
| 644 | + self.fc = nn.Linear(n_head * d_v, d_model) | ||
| 645 | + nn.init.xavier_normal_(self.fc.weight) | ||
| 646 | + | ||
| 647 | + self.dropout = nn.Dropout(dropout) | ||
| 648 | + | ||
| 649 | + MultiHeadAttention.__init__ = new_init | ||
| 650 | + | ||
| 651 | + patch_remove_ops_from_generate_list(["aten.cat", "aten.full"]) | ||
| 652 | + | ||
| 653 | + | ||
| 654 | + | ||
| 588 | def patch_model(model_name): | 655 | def patch_model(model_name): |
| 589 | if model_name not in _patch_table.keys(): | 656 | if model_name not in _patch_table.keys(): |
| 590 | return | 657 | return |
| @@ -6,6 +6,7 @@ hf_Albert | |||
| 6 | hf_Bart | 6 | hf_Bart |
| 7 | hf_Bert | 7 | hf_Bert |
| 8 | hf_DistilBert | 8 | hf_DistilBert |
| 9 | +hf_GPT2_large | ||
| 9 | hf_T5_base | 10 | hf_T5_base |
| 10 | hf_T5_large | 11 | hf_T5_large |
| 11 | LearningToPaint | 12 | LearningToPaint |
| @@ -22,7 +23,9 @@ resnet50 | |||
| 22 | resnext50_32x4d | 23 | resnext50_32x4d |
| 23 | shufflenet_v2_x1_0 | 24 | shufflenet_v2_x1_0 |
| 24 | soft_actor_critic | 25 | soft_actor_critic |
| 26 | +speech_transformer | ||
| 25 | squeezenet1_1 | 27 | squeezenet1_1 |
| 28 | +timm_regnet | ||
| 26 | timm_resnest | 29 | timm_resnest |
| 27 | timm_vision_transformer | 30 | timm_vision_transformer |
| 28 | timm_vovnet | 31 | timm_vovnet |