已合并
【fix】batcnnorm_check_fix #36322
cuiduo创建于 5月21日
【fix】batcnnorm_check_fix #36322
已合并
cuiduo创建于 5月21日
4 个文件变更+13-4
Mtest/_inductor/test_run_with_rng_state.py+1-0
@@ -9,6 +9,7 @@ import torch_npu
9import torch_npu._inductor9import torch_npu._inductor
10 10 
11 11 
12+ 
12class TestRunWithRngState(TestUtils):13class TestRunWithRngState(TestUtils):
13 def op_calc(self, current_state, like, device, dtype):14 def op_calc(self, current_state, like, device, dtype):
14 res1 = torch._prims.rng_prims.run_with_rng_state(15 res1 = torch._prims.rng_prims.run_with_rng_state(
Mtorch_npu/_inductor/codegen/wrapper.py+1-1
@@ -2,7 +2,6 @@ import os
2import copy2import copy
3from typing import Any, Callable, Optional, TYPE_CHECKING, Union3from typing import Any, Callable, Optional, TYPE_CHECKING, Union
4import hashlib4import hashlib
5-import sympy
6 5 
7import torch6import torch
8from torch._inductor import config7from torch._inductor import config
@@ -36,6 +35,7 @@ class _NPUKernelCodegenMixin:
36 from leaking into subgraphs.35 from leaking into subgraphs.
37 """36 """
38 37 
38+
39 # generate numel expr for range_tree_node39 # generate numel expr for range_tree_node
40 def generate_node_numel_expr(self, kernel_name: str, node, numel_expr):40 def generate_node_numel_expr(self, kernel_name: str, node, numel_expr):
41 expr = f"{kernel_name}_{node.name}_numel"41 expr = f"{kernel_name}_{node.name}_numel"
Mtorch_npu/contrib/module/_batchnorm_with_int32_count.py+5-3
@@ -270,9 +270,7 @@ class FastSyncBatchNorm(_BatchNorm):
270 )270 )
271 271 
272 def forward(self, input1: Tensor) -> Tensor:272 def forward(self, input1: Tensor) -> Tensor:
273- # currently NPU or GPU input1 is supported273+
274- if not input1.is_cuda and not input1.is_npu:
275- raise ValueError("SyncBatchNorm expected input1 tensor to be on NPU or GPU" + ops_error(ErrCode.VALUE))
276 274 
277 self._check_input_dim(input1)275 self._check_input_dim(input1)
278 self._check_non_zero_input_channels(input1)276 self._check_non_zero_input_channels(input1)
@@ -324,6 +322,10 @@ class FastSyncBatchNorm(_BatchNorm):
324 and torch.distributed.is_initialized()322 and torch.distributed.is_initialized()
325 )323 )
326 if need_sync:324 if need_sync:
325+ # currently NPU or GPU input1 is supported
326+ if not input1.is_cuda and not input1.is_npu:
327+ raise ValueError("SyncBatchNorm expected input1 tensor to be on NPU or GPU" + ops_error(ErrCode.VALUE))
328+ 
327 process_group = torch.distributed.group.WORLD329 process_group = torch.distributed.group.WORLD
328 if self.process_group:330 if self.process_group:
329 process_group = self.process_group331 process_group = self.process_group
Mtorch_npu/multiprocessing/reductions.py+6-0
@@ -12,6 +12,7 @@ from torch.multiprocessing.reductions import (
12 rebuild_tensor,12 rebuild_tensor,
13 storage_from_cache,13 storage_from_cache,
14 rebuild_meta_tensor,14 rebuild_meta_tensor,
15+ reduce_nested_tensor,
15)16)
16 17 
17import torch_npu18import torch_npu
@@ -95,6 +96,11 @@ def rebuild_npu_tensor(
95 96 
96 97 
97def _npu_reduce_tensor(tensor):98def _npu_reduce_tensor(tensor):
99+ from torch.nested._internal.nested_tensor import NestedTensor
100+ 
101+ # 特殊处理:嵌套张量(Nested Tensor)走专用归约
102+ if tensor.is_nested and not isinstance(tensor, NestedTensor):
103+ return reduce_nested_tensor(tensor)
98 storage = tensor._typed_storage()104 storage = tensor._typed_storage()
99 105 
100 if tensor.requires_grad and not tensor.is_leaf:106 if tensor.requires_grad and not tensor.is_leaf: