已合并
[fa_v3]skip cpu check, register_sharding for npu_fusion_attention_v3 #30662
王超创建于 2月9日
[fa_v3]skip cpu check, register_sharding for npu_fusion_attention_v3 #30662
已合并
共 6 个文件变更+511-21
| @@ -262,6 +262,193 @@ class TestAttentionOps(NPUDTensorTestBase): | |||
| 262 | for placement in placements: | 262 | for placement in placements: |
| 263 | test_placement_comb([placement], [placement], [placement]) | 263 | test_placement_comb([placement], [placement], [placement]) |
| 264 | 264 | ||
| 265 | + | ||
| 266 | + | ||
| 267 | + | ||
| 268 | + | ||
| 269 | + "sparse_mode,pre_tokens,next_tokens", | ||
| 270 | + [ | ||
| 271 | + (0, 128, 128), | ||
| 272 | + (1, 65536, 65536), | ||
| 273 | + (2, 65536, 0), | ||
| 274 | + (3, 65536, 0), | ||
| 275 | + (4, 128, 128) | ||
| 276 | + ] | ||
| 277 | + ) | ||
| 278 | + def test_npu_fusion_attention_v3_forward_bnsd(self, sparse_mode, pre_tokens, next_tokens): | ||
| 279 | + device_mesh = self.build_device_mesh() | ||
| 280 | + | ||
| 281 | + B, N, S, D = 4, 8, 32, 32 | ||
| 282 | + shape = (B, N, S, D) | ||
| 283 | + query = torch.randn(shape, dtype=torch.float32, device="npu") | ||
| 284 | + key = torch.randn(shape, dtype=torch.float32, device="npu") | ||
| 285 | + value = torch.randn(shape, dtype=torch.float32, device="npu") | ||
| 286 | + | ||
| 287 | + scale = 0.08838 | ||
| 288 | + | ||
| 289 | + atten_mask = get_atten_mask(shape, sparse_mode, pre_tokens, next_tokens) | ||
| 290 | + result = torch_npu.npu_fusion_attention_v3( | ||
| 291 | + query, key, value, head_num=N, input_layout="BNSD", scale=scale, sparse_mode=sparse_mode, | ||
| 292 | + atten_mask=atten_mask, pre_tockens=pre_tokens, next_tockens=next_tokens | ||
| 293 | + ) | ||
| 294 | + | ||
| 295 | + def test_placement_comb(query_placements, key_placements, value_placements, atten_mask_placements): | ||
| 296 | + dist_query = distribute_tensor(query, device_mesh, query_placements) | ||
| 297 | + dist_key = distribute_tensor(key, device_mesh, key_placements) | ||
| 298 | + dist_value = distribute_tensor(value, device_mesh, value_placements) | ||
| 299 | + dist_atten_mask = distribute_tensor( | ||
| 300 | + atten_mask, device_mesh, atten_mask_placements | ||
| 301 | + ) if atten_mask is not None else None | ||
| 302 | + dist_result = torch_npu.npu_fusion_attention_v3( | ||
| 303 | + dist_query, dist_key, dist_value, head_num=N, input_layout="BNSD", scale=scale, | ||
| 304 | + sparse_mode=sparse_mode, atten_mask=dist_atten_mask, | ||
| 305 | + pre_tockens=pre_tokens, next_tockens=next_tokens | ||
| 306 | + ) | ||
| 307 | + self.assertEqual(dist_result[0].full_tensor(), result[0]) | ||
| 308 | + self.assertEqual(dist_result[1].full_tensor(), result[1]) | ||
| 309 | + self.assertEqual(dist_result[2].full_tensor(), result[2]) | ||
| 310 | + | ||
| 311 | + placements = [Shard(0), Shard(1), Shard(2), Shard(3), Replicate()] | ||
| 312 | + for placement in placements: | ||
| 313 | + if atten_mask is None or (isinstance(placement, Shard) and atten_mask.ndim <= placement.dim): | ||
| 314 | + test_placement_comb([placement], [placement], [placement], [Replicate()]) | ||
| 315 | + else: | ||
| 316 | + test_placement_comb([placement], [placement], [placement], [placement]) | ||
| 317 | + | ||
| 318 | + | ||
| 319 | + | ||
| 320 | + | ||
| 321 | + | ||
| 322 | + "sparse_mode,pre_tokens,next_tokens", | ||
| 323 | + [ | ||
| 324 | + (0, 128, 128), | ||
| 325 | + (1, 65536, 65536), | ||
| 326 | + (2, 65536, 0), | ||
| 327 | + (3, 65536, 0), | ||
| 328 | + (4, 128, 128) | ||
| 329 | + ] | ||
| 330 | + ) | ||
| 331 | + def test_npu_fusion_attention_v3_backward_bnsd(self, sparse_mode, pre_tokens, next_tokens): | ||
| 332 | + device_mesh = self.build_device_mesh() | ||
| 333 | + | ||
| 334 | + B, N, S, D = 4, 8, 32, 32 | ||
| 335 | + shape = (B, N, S, D) | ||
| 336 | + query = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 337 | + key = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 338 | + value = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 339 | + | ||
| 340 | + scale = 0.08838 | ||
| 341 | + | ||
| 342 | + atten_mask = get_atten_mask(shape, sparse_mode, pre_tokens, next_tokens) | ||
| 343 | + result = torch_npu.npu_fusion_attention_v3( | ||
| 344 | + query, key, value, head_num=N, input_layout="BNSD", scale=scale, sparse_mode=sparse_mode, | ||
| 345 | + atten_mask=atten_mask, pre_tockens=pre_tokens, next_tockens=next_tokens | ||
| 346 | + ) | ||
| 347 | + gard_y = torch.ones_like(result[0]) | ||
| 348 | + result[0].backward(gard_y) | ||
| 349 | + | ||
| 350 | + def test_placement_comb(query_placements, key_placements, value_placements, atten_mask_placements): | ||
| 351 | + dist_query = distribute_tensor(query, device_mesh, query_placements) | ||
| 352 | + dist_key = distribute_tensor(key, device_mesh, key_placements) | ||
| 353 | + dist_value = distribute_tensor(value, device_mesh, value_placements) | ||
| 354 | + dist_atten_mask = distribute_tensor( | ||
| 355 | + atten_mask, device_mesh, atten_mask_placements | ||
| 356 | + ) if atten_mask is not None else None | ||
| 357 | + dist_result = torch_npu.npu_fusion_attention_v3( | ||
| 358 | + dist_query, dist_key, dist_value, head_num=N, input_layout="BNSD", scale=scale, | ||
| 359 | + sparse_mode=sparse_mode, atten_mask=dist_atten_mask, | ||
| 360 | + pre_tockens=pre_tokens, next_tockens=next_tokens | ||
| 361 | + ) | ||
| 362 | + dist_grad_y = distribute_tensor(gard_y, device_mesh, dist_result[0].placements) | ||
| 363 | + dist_result[0].backward(dist_grad_y) | ||
| 364 | + self.assertEqual(dist_result[0].full_tensor(), result[0]) | ||
| 365 | + self.assertEqual(dist_result[1].full_tensor(), result[1]) | ||
| 366 | + self.assertEqual(dist_result[2].full_tensor(), result[2]) | ||
| 367 | + self.assertEqual(dist_query.grad.full_tensor(), query.grad) | ||
| 368 | + self.assertEqual(dist_key.grad.full_tensor(), key.grad) | ||
| 369 | + self.assertEqual(dist_value.grad.full_tensor(), value.grad) | ||
| 370 | + | ||
| 371 | + placements = [Shard(0), Shard(1), Shard(2), Shard(3), Replicate()] | ||
| 372 | + for placement in placements: | ||
| 373 | + if atten_mask is None or (isinstance(placement, Shard) and atten_mask.ndim <= placement.dim): | ||
| 374 | + test_placement_comb([placement], [placement], [placement], [Replicate()]) | ||
| 375 | + else: | ||
| 376 | + test_placement_comb([placement], [placement], [placement], [placement]) | ||
| 377 | + | ||
| 378 | + | ||
| 379 | + | ||
| 380 | + | ||
| 381 | + def test_npu_fusion_attention_v3_bsnd(self): | ||
| 382 | + device_mesh = self.build_device_mesh() | ||
| 383 | + | ||
| 384 | + B, N, S, D = 4, 8, 32, 32 | ||
| 385 | + shape = (B, S, N, D) | ||
| 386 | + query = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 387 | + key = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 388 | + value = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 389 | + scale = 0.08838 | ||
| 390 | + | ||
| 391 | + result = torch_npu.npu_fusion_attention_v3(query, key, value, head_num=N, input_layout="BSND", scale=scale) | ||
| 392 | + gard_y = torch.ones_like(result[0]) | ||
| 393 | + result[0].backward(gard_y) | ||
| 394 | + | ||
| 395 | + def test_placement_comb(query_placements, key_placements, value_placements): | ||
| 396 | + dist_query = distribute_tensor(query, device_mesh, query_placements) | ||
| 397 | + dist_key = distribute_tensor(key, device_mesh, key_placements) | ||
| 398 | + dist_value = distribute_tensor(value, device_mesh, value_placements) | ||
| 399 | + dist_result = torch_npu.npu_fusion_attention_v3( | ||
| 400 | + dist_query, dist_key, dist_value, head_num=N, input_layout="BSND", scale=scale | ||
| 401 | + ) | ||
| 402 | + dist_grad_y = distribute_tensor(gard_y, device_mesh, dist_result[0].placements) | ||
| 403 | + dist_result[0].backward(dist_grad_y) | ||
| 404 | + self.assertEqual(dist_result[0].full_tensor(), result[0]) | ||
| 405 | + self.assertEqual(dist_result[1].full_tensor(), result[1]) | ||
| 406 | + self.assertEqual(dist_result[2].full_tensor(), result[2]) | ||
| 407 | + self.assertEqual(dist_query.grad.full_tensor(), query.grad) | ||
| 408 | + self.assertEqual(dist_key.grad.full_tensor(), key.grad) | ||
| 409 | + self.assertEqual(dist_value.grad.full_tensor(), value.grad) | ||
| 410 | + | ||
| 411 | + placements = [Shard(0), Shard(1), Shard(2), Shard(3), Replicate()] | ||
| 412 | + for placement in placements: | ||
| 413 | + test_placement_comb([placement], [placement], [placement]) | ||
| 414 | + | ||
| 415 | + | ||
| 416 | + | ||
| 417 | + | ||
| 418 | + def test_npu_fusion_attention_v3_bsh(self): | ||
| 419 | + device_mesh = self.build_device_mesh() | ||
| 420 | + | ||
| 421 | + B, N, S, D = 4, 8, 32, 32 | ||
| 422 | + shape = (B, S, N * D) | ||
| 423 | + query = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 424 | + key = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 425 | + value = torch.randn(shape, dtype=torch.float32, device="npu", requires_grad=True) | ||
| 426 | + scale = 0.08838 | ||
| 427 | + | ||
| 428 | + result = torch_npu.npu_fusion_attention_v3(query, key, value, head_num=N, input_layout="BSH", scale=scale) | ||
| 429 | + gard_y = torch.ones_like(result[0]) | ||
| 430 | + result[0].backward(gard_y) | ||
| 431 | + | ||
| 432 | + def test_placement_comb(query_placements, key_placements, value_placements): | ||
| 433 | + dist_query = distribute_tensor(query, device_mesh, query_placements) | ||
| 434 | + dist_key = distribute_tensor(key, device_mesh, key_placements) | ||
| 435 | + dist_value = distribute_tensor(value, device_mesh, value_placements) | ||
| 436 | + dist_result = torch_npu.npu_fusion_attention_v3( | ||
| 437 | + dist_query, dist_key, dist_value, head_num=N, input_layout="BSH", scale=scale | ||
| 438 | + ) | ||
| 439 | + dist_grad_y = distribute_tensor(gard_y, device_mesh, dist_result[0].placements) | ||
| 440 | + dist_result[0].backward(dist_grad_y) | ||
| 441 | + self.assertEqual(dist_result[0].full_tensor(), result[0]) | ||
| 442 | + self.assertEqual(dist_result[1].full_tensor(), result[1]) | ||
| 443 | + self.assertEqual(dist_result[2].full_tensor(), result[2]) | ||
| 444 | + self.assertEqual(dist_query.grad.full_tensor(), query.grad) | ||
| 445 | + self.assertEqual(dist_key.grad.full_tensor(), key.grad) | ||
| 446 | + self.assertEqual(dist_value.grad.full_tensor(), value.grad) | ||
| 447 | + | ||
| 448 | + placements = [Shard(0), Shard(1), Shard(2), Replicate()] | ||
| 449 | + for placement in placements: | ||
| 450 | + test_placement_comb([placement], [placement], [placement]) | ||
| 451 | + | ||
| 265 | 452 | ||
| 266 | instantiate_parametrized_tests(TestAttentionOps) | 453 | instantiate_parametrized_tests(TestAttentionOps) |
| 267 | 454 | ||
| @@ -51,6 +51,26 @@ class aot_inductor: | |||
| 51 | dump_path_py = os.environ.get("AOTI_DUMP_PATH_PY", "aoti_dump_py") | 51 | dump_path_py = os.environ.get("AOTI_DUMP_PATH_PY", "aoti_dump_py") |
| 52 | 52 | ||
| 53 | 53 | ||
| 54 | +class _npugraph_trees: | ||
| 55 | + def __init__(self): | ||
| 56 | + # skip cpu node check, eg: npu_fusion_attention_v3 | ||
| 57 | + self._disable_cpu_input_check = False | ||
| 58 | + | ||
| 59 | + | ||
| 60 | + def disable_cpu_input_check(self): | ||
| 61 | + return self._disable_cpu_input_check | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + def disable_cpu_input_check(self, value): | ||
| 65 | + self._disable_cpu_input_check = bool(value) | ||
| 66 | + # When disable_cpu_input_check is True, set slow_path_cudagraph_asserts to True to skip the CPU check. | ||
| 67 | + if value: | ||
| 68 | + torch._inductor.config.triton.slow_path_cudagraph_asserts = False | ||
| 69 | + | ||
| 70 | + | ||
| 71 | +npugraph_trees = _npugraph_trees() | ||
| 72 | + | ||
| 73 | + | ||
| 54 | traced_fx_graph_cache = os.environ.get("INDUCTOR_ASCEND_FX_GRAPH_CACHE", None) | 74 | traced_fx_graph_cache = os.environ.get("INDUCTOR_ASCEND_FX_GRAPH_CACHE", None) |
| 55 | check_accuracy = os.environ.get("INDUCTOR_ASCEND_CHECK_ACCURACY", False) | 75 | check_accuracy = os.environ.get("INDUCTOR_ASCEND_CHECK_ACCURACY", False) |
| 56 | auto_fallback = os.environ.get("INDUCTOR_ASCEND_AUTO_FALLBACK", True) | 76 | auto_fallback = os.environ.get("INDUCTOR_ASCEND_AUTO_FALLBACK", True) |
| @@ -42,3 +42,4 @@ def _add_logging_module(): | |||
| 42 | torch._logging._internal.register_log("shmem", "torch_npu.symmetric_memory") | 42 | torch._logging._internal.register_log("shmem", "torch_npu.symmetric_memory") |
| 43 | torch._logging._internal.register_log("env", "torch_npu.env") | 43 | torch._logging._internal.register_log("env", "torch_npu.env") |
| 44 | torch._logging._internal.register_log("acl", "torch_npu.acl") | 44 | torch._logging._internal.register_log("acl", "torch_npu.acl") |
| 45 | + torch._logging._internal.register_log("aclgraph", "torch_npu.aclgraph") | ||
| @@ -46,6 +46,7 @@ import threading | |||
| 46 | import traceback | 46 | import traceback |
| 47 | import warnings | 47 | import warnings |
| 48 | import weakref | 48 | import weakref |
| 49 | +import logging | ||
| 49 | from collections import defaultdict | 50 | from collections import defaultdict |
| 50 | from enum import auto, Enum | 51 | from enum import auto, Enum |
| 51 | from typing import ( | 52 | from typing import ( |
| @@ -114,7 +115,7 @@ StorageWeakRefPointer = int | |||
| 114 | StorageDataPtr = int | 115 | StorageDataPtr = int |
| 115 | NBytes = int | 116 | NBytes = int |
| 116 | S = TypeVar("S", bound="StorageWeakRefWrapper") | 117 | S = TypeVar("S", bound="StorageWeakRefWrapper") |
| 117 | -log = torch._logging.getArtifactLogger(__name__, "cudagraphs") | 118 | +log = logging.getLogger("torch_npu.aclgraph") |
| 118 | 119 | ||
| 119 | 120 | ||
| 120 | 121 | ||
| @@ -1270,7 +1271,8 @@ class NPUGraphNode: | |||
| 1270 | self.static_output_tensors = [None for _ in range(len(outputs))] | 1271 | self.static_output_tensors = [None for _ in range(len(outputs))] |
| 1271 | 1272 | ||
| 1272 | for index_, out_ in enumerate(outputs): | 1273 | for index_, out_ in enumerate(outputs): |
| 1273 | - if out_ is None or not isinstance(out_, torch.Tensor): | 1274 | + from torch_npu._inductor import config as npu_config |
| 1275 | + if out_ is None or not isinstance(out_, torch.Tensor) or (npu_config.npugraph_trees.disable_cpu_input_check and out_.is_cpu): | ||
| 1274 | self.output_storage_alias.append(UnaliasedStorage) | 1276 | self.output_storage_alias.append(UnaliasedStorage) |
| 1275 | continue | 1277 | continue |
| 1276 | 1278 | ||
| @@ -2184,11 +2186,7 @@ class NPUGraphTreeManager: | |||
| 2184 | if isinstance(self.current_node, NPUWarmupNode): | 2186 | if isinstance(self.current_node, NPUWarmupNode): |
| 2185 | raise RuntimeError("self.current_node is NPUWarmupNode object") | 2187 | raise RuntimeError("self.current_node is NPUWarmupNode object") |
| 2186 | graph_id = self.new_graph_id() | 2188 | graph_id = self.new_graph_id() |
| 2187 | - log.debug( | 2189 | + log.debug(f"Recording function {function_id.id} of graph recording id {graph_id.id}") |
| 2188 | - "Recording function %d of graph recording id %d", | ||
| 2189 | - function_id.id, | ||
| 2190 | - graph_id.id, | ||
| 2191 | - ) | ||
| 2192 | torch.npu.synchronize() | 2190 | torch.npu.synchronize() |
| 2193 | node = NPUGraphNode( | 2191 | node = NPUGraphNode( |
| 2194 | self.ids_to_funcs[function_id], | 2192 | self.ids_to_funcs[function_id], |
| @@ -2216,6 +2214,7 @@ class NPUGraphTreeManager: | |||
| 2216 | self.current_node = node | 2214 | self.current_node = node |
| 2217 | self.path_state = ExecutionState.EXECUTION | 2215 | self.path_state = ExecutionState.EXECUTION |
| 2218 | self.update_generation() | 2216 | self.update_generation() |
| 2217 | + log.debug(f"execute graph, id is {self.current_node.id}") | ||
| 2219 | return node.run(new_inputs) | 2218 | return node.run(new_inputs) |
| 2220 | 2219 | ||
| 2221 | def run_eager( | 2220 | def run_eager( |
| @@ -2225,12 +2224,9 @@ class NPUGraphTreeManager: | |||
| 2225 | # we will deallocate it | 2224 | # we will deallocate it |
| 2226 | already_warm = function_id in self.warmed_up_functions | 2225 | already_warm = function_id in self.warmed_up_functions |
| 2227 | if not already_warm: | 2226 | if not already_warm: |
| 2228 | - log.debug("Running warmup of function %d", function_id.id) | 2227 | + log.debug(f"Running warmup of function {function_id}") |
| 2229 | else: | 2228 | else: |
| 2230 | - log.debug( | 2229 | + log.debug(f"Running eager of function {function_id} because ancestor needed to warm up") |
| 2231 | - "Running eager of function %d because ancestor needed to warm up", | ||
| 2232 | - function_id.id, | ||
| 2233 | - ) | ||
| 2234 | self.warmed_up_functions.add(function_id) | 2230 | self.warmed_up_functions.add(function_id) |
| 2235 | node = NPUWarmupNode( | 2231 | node = NPUWarmupNode( |
| 2236 | self.ids_to_funcs[function_id], | 2232 | self.ids_to_funcs[function_id], |
| @@ -1,4 +1,5 @@ | |||
| 1 | import functools | 1 | import functools |
| 2 | +import logging | ||
| 2 | from collections import defaultdict | 3 | from collections import defaultdict |
| 3 | from typing import ( | 4 | from typing import ( |
| 4 | Any, | 5 | Any, |
| @@ -53,6 +54,9 @@ from torch.multiprocessing.reductions import StorageWeakRef | |||
| 53 | import torch_npu.npu.aclnn | 54 | import torch_npu.npu.aclnn |
| 54 | 55 | ||
| 55 | 56 | ||
| 57 | +log = logging.getLogger("torch_npu.aclgraph") | ||
| 58 | + | ||
| 59 | + | ||
| 56 | def npugraph_mark_step_begin(): | 60 | def npugraph_mark_step_begin(): |
| 57 | from torch_npu.npu._graph_tree import mark_step_begin | 61 | from torch_npu.npu._graph_tree import mark_step_begin |
| 58 | mark_step_begin() | 62 | mark_step_begin() |
| @@ -61,10 +65,15 @@ def npugraph_mark_step_begin(): | |||
| 61 | def check_multiple_devices_or_any_cpu_nodes( | 65 | def check_multiple_devices_or_any_cpu_nodes( |
| 62 | device_node_mapping: Dict[torch.device, torch.fx.Node] | 66 | device_node_mapping: Dict[torch.device, torch.fx.Node] |
| 63 | ) -> Optional[str]: | 67 | ) -> Optional[str]: |
| 68 | + from torch_npu._inductor import config as npu_config | ||
| 69 | + if npu_config.npugraph_trees.disable_cpu_input_check: | ||
| 70 | + device_node_mapping.pop(torch.device("cpu"), None) | ||
| 71 | + | ||
| 64 | cpu_node = device_node_mapping.get(torch.device("cpu")) | 72 | cpu_node = device_node_mapping.get(torch.device("cpu")) |
| 65 | if cpu_node: | 73 | if cpu_node: |
| 66 | msg = f"cpu device ({cpu_node.name})" | 74 | msg = f"cpu device ({cpu_node.name})" |
| 67 | stack_trace = _get_use_stack_trace(cpu_node) | 75 | stack_trace = _get_use_stack_trace(cpu_node) |
| 76 | + log.info(f"skip with cpu node, msg is {msg}, stack_trace is {stack_trace}") | ||
| 68 | if stack_trace: | 77 | if stack_trace: |
| 69 | return format_default_skip_message(f"{msg}. Found from : \n {stack_trace}") | 78 | return format_default_skip_message(f"{msg}. Found from : \n {stack_trace}") |
| 70 | return format_default_skip_message(msg) | 79 | return format_default_skip_message(msg) |
| @@ -251,7 +260,11 @@ def check_for_skip(aot_model: torch.fx.GraphModule, num_fixed) -> Optional[str]: | |||
| 251 | 260 | ||
| 252 | 261 | ||
| 253 | def get_device_index(gm) -> int: | 262 | def get_device_index(gm) -> int: |
| 254 | - device = next(iter(get_device_node_mapping(gm))) | 263 | + device_node_mapping = get_device_node_mapping(gm) |
| 264 | + from torch_npu._inductor import config as npu_config | ||
| 265 | + if npu_config.npugraph_trees.disable_cpu_input_check: | ||
| 266 | + device_node_mapping.pop(torch.device("cpu"), None) | ||
| 267 | + device = next(iter(device_node_mapping)) | ||
| 255 | if not (device.type == "npu"): | 268 | if not (device.type == "npu"): |
| 256 | raise RuntimeError("check device.type == npu fail", ) | 269 | raise RuntimeError("check device.type == npu fail", ) |
| 257 | return device.index | 270 | return device.index |


代码重复: npu_fusion_attention_v3_strategy函数与npu_fusion_attention_strategy函数(第27-139行)的代码结构几乎完全相同,存在大量重复代码。两个函数都实现了相同的分片策略逻辑,只是注册的算子不同。这种重复违反了DRY(Don't Repeat Yourself)原则,增加了维护成本和出错风险。
问题类型: 代码重复 文件路径:
torch_npu/distributed/tensor/_attention.py行号: 274 问题代码:@register_sharding(npu.npu_fusion_attention_v3.default) # pylint:disable=huawei-too-many-arguments def npu_fusion_attention_v3_strategy(query, key, value, head_num, input_layout, pse=None, padding_mask=None, atten_mask=None, scale=1.0, keep_prob=1.0, pre_tockens=2147483647, next_tockens=2147483647, inner_precise=0, prefix=None, actual_seq_qlen=None, actual_seq_kvlen=None, sparse_mode=0, gen_mask_parallel=True, sync=False, softmax_layout="", sink=None):修改建议:
此评论由代码审查工具自动生成