已合并
Fixed for the public API. #16647
yuhaiyan8创建于 2024年12月4日
Fixed for the public API. #16647
已合并
yuhaiyan8创建于 2024年12月4日
refs/pull/16647/head合入到master
8 个文件变更+20-100
@@ -6,7 +6,7 @@ RUN cd /usr/local/bin \
6 && ln -s /opt/_internal/cpython-3.8.18/bin/pip3.8 pip3.8 \6 && ln -s /opt/_internal/cpython-3.8.18/bin/pip3.8 pip3.8 \
7 && ln -s /opt/_internal/cpython-3.9.18/bin/pip3.9 pip3.9 \7 && ln -s /opt/_internal/cpython-3.9.18/bin/pip3.9 pip3.9 \
8 && ln -s /opt/_internal/cpython-3.10.13/bin/pip3.10 pip3.10 \8 && ln -s /opt/_internal/cpython-3.10.13/bin/pip3.10 pip3.10 \
9- && ln -s /opt/_internal/cpython-3.11.6/bin/pip3.10 pip3.11 \9+ && ln -s /opt/_internal/cpython-3.11.6/bin/pip3.11 pip3.11 \
10 && ln -s python3.8 python310 && ln -s python3.8 python3
11 11 
12# Set pip source12# Set pip source
@@ -3,14 +3,14 @@ import numpy as np
3import torch3import torch
4import torch_npu4import torch_npu
5from torch_npu.contrib.module import MultiheadAttention5from torch_npu.contrib.module import MultiheadAttention
6-from torch_npu.contrib.module.multihead_attention import MHAConfig6+from torch_npu.contrib.module.multihead_attention import _MHAConfig
7from torch_npu.testing.testcase import TestCase, run_tests7from torch_npu.testing.testcase import TestCase, run_tests
8from torch_npu.testing.common_utils import create_common_tensor8from torch_npu.testing.common_utils import create_common_tensor
9 9 
10FORMAT_ND = 210FORMAT_ND = 2
11FORMAT_NZ = 2911FORMAT_NZ = 29
12npu_device = "npu:0"12npu_device = "npu:0"
13-MHAConfig.set_fussion()13+_MHAConfig.set_fussion()
14 14 
15 15 
16class TestMultiheadAttention(unittest.TestCase):16class TestMultiheadAttention(unittest.TestCase):
@@ -662,9 +662,6 @@
662 "torch_npu.contrib.module.drop_path.NpuDropPath.enable_droppath_ensemble": {662 "torch_npu.contrib.module.drop_path.NpuDropPath.enable_droppath_ensemble": {
663 "signature": "(model)"663 "signature": "(model)"
664 },664 },
665- "torch_npu.contrib.module.ensemble_dropout.DropOutTask": {
666- "signature": "(shape, dtype, device, p)"
667- },
668 "torch_npu.contrib.module.ensemble_dropout.NpuCachedDropout": {665 "torch_npu.contrib.module.ensemble_dropout.NpuCachedDropout": {
669 "signature": "(p, module_name=None)"666 "signature": "(p, module_name=None)"
670 },667 },
@@ -692,9 +689,6 @@
692 "torch_npu.contrib.module.ensemble_dropout.NpuPreGenDropout.enable_dropout_ensemble": {689 "torch_npu.contrib.module.ensemble_dropout.NpuPreGenDropout.enable_dropout_ensemble": {
693 "signature": "(model)"690 "signature": "(model)"
694 },691 },
695- "torch_npu.contrib.module.ensemble_dropout.PreGenDropoutTask": {
696- "signature": "(device, p)"
697- },
698 "torch_npu.contrib.module.focus.Conv": {692 "torch_npu.contrib.module.focus.Conv": {
699 "signature": "(c1, c2, k=1, s=1, p=None, g=1, act=True)"693 "signature": "(c1, c2, k=1, s=1, p=None, g=1, act=True)"
700 },694 },
@@ -725,18 +719,6 @@
725 "torch_npu.contrib.module.fusedcolorjitter.FusedColorJitter.forward": {719 "torch_npu.contrib.module.fusedcolorjitter.FusedColorJitter.forward": {
726 "signature": "(self, img)"720 "signature": "(self, img)"
727 },721 },
728- "torch_npu.contrib.module.fusedcolorjitter.FusedColorJitterApply": {
729- "signature": "(hue=0.0, saturation=1.0, contrast=0.0, brightness=0.0, is_normalized=False, force_return_array=False)"
730- },
731- "torch_npu.contrib.module.fusedcolorjitter.FusedColorJitterApply.hue_saturation_matrix": {
732- "signature": "(self, hue, saturation)"
733- },
734- "torch_npu.contrib.module.fusedcolorjitter.FusedColorJitterApply.get_random_transform_matrix": {
735- "signature": "(self, hue=0.05, saturation=0.5, contrast=0.5, brightness=0.125)"
736- },
737- "torch_npu.contrib.module.fusedcolorjitter.FusedColorJitterApply.apply_image_transform": {
738- "signature": "(self, img, transform_matrix, transform_offset)"
739- },
740 "torch_npu.contrib.module.linear_a8w8_quant.LinearA8W8Quant": {722 "torch_npu.contrib.module.linear_a8w8_quant.LinearA8W8Quant": {
741 "signature": "(in_features: int, out_features: int, *, bias: bool = True, offset: bool = False, pertoken_scale: bool = False, device=None, dtype=None, output_dtype=None) -> None"723 "signature": "(in_features: int, out_features: int, *, bias: bool = True, offset: bool = False, pertoken_scale: bool = False, device=None, dtype=None, output_dtype=None) -> None"
742 },724 },
@@ -755,12 +737,6 @@
755 "torch_npu.contrib.module.linear_weight_quant.LinearWeightQuant.forward": {737 "torch_npu.contrib.module.linear_weight_quant.LinearWeightQuant.forward": {
756 "signature": "(self, x: torch.Tensor) -> torch.Tensor"738 "signature": "(self, x: torch.Tensor) -> torch.Tensor"
757 },739 },
758- "torch_npu.contrib.module.multihead_attention.MHAConfig": {
759- "signature": "()"
760- },
761- "torch_npu.contrib.module.multihead_attention.MHAConfig.set_fussion": {
762- "signature": "()"
763- },
764 "torch_npu.contrib.module.multihead_attention.Matmul_transpose": {740 "torch_npu.contrib.module.multihead_attention.Matmul_transpose": {
765 "signature": "(tensor1, tensor2)"741 "signature": "(tensor1, tensor2)"
766 },742 },
@@ -800,12 +776,6 @@
800 "torch_npu.contrib.module.multihead_attention.MultiheadAttention.upgrade_state_dict_named": {776 "torch_npu.contrib.module.multihead_attention.MultiheadAttention.upgrade_state_dict_named": {
801 "signature": "(self, state_dict, name)"777 "signature": "(self, state_dict, name)"
802 },778 },
803- "torch_npu.contrib.module.multihead_attention.NpuLinear": {
804- "signature": "(in_features: int, out_features: int, bias: bool = True, device=None, dtype=None) -> None"
805- },
806- "torch_npu.contrib.module.multihead_attention.NpuLinear.forward": {
807- "signature": "(self, input2)"
808- },
809 "torch_npu.contrib.module.npu_modules.DropoutWithByteMask": {779 "torch_npu.contrib.module.npu_modules.DropoutWithByteMask": {
810 "signature": "(p=0.5, inplace=False, max_seed=1023)"780 "signature": "(p=0.5, inplace=False, max_seed=1023)"
811 },781 },
@@ -1793,12 +1763,6 @@
1793 "torch_npu.npu.utils.utilization": {1763 "torch_npu.npu.utils.utilization": {
1794 "signature": "(device=None)"1764 "signature": "(device=None)"
1795 },1765 },
1796- "torch_npu.onnx.native_layer_norm": {
1797- "signature": "(g, inputs, normalized_shape, weight, bias, eps)"
1798- },
1799- "torch_npu.onnx.register_aten_ops_to_onnx.native_layer_norm": {
1800- "signature": "(g, inputs, normalized_shape, weight, bias, eps)"
1801- },
1802 "torch_npu.optim.NpuFusedAdadelta": {1766 "torch_npu.optim.NpuFusedAdadelta": {
1803 "signature": "(params, lr=1.0, rho=0.9, eps=1e-06, weight_decay=0)"1767 "signature": "(params, lr=1.0, rho=0.9, eps=1e-06, weight_decay=0)"
1804 },1768 },
@@ -12,15 +12,13 @@ from torch_npu.utils._error_code import ErrCode, ops_error
12logger = logging.getLogger(__name__)12logger = logging.getLogger(__name__)
13 13 
14__all__ = [14__all__ = [
15- "DropOutTask",
16 "NpuCachedDropout",15 "NpuCachedDropout",
17 "NpuFairseqDropout",16 "NpuFairseqDropout",
18- "PreGenDropoutTask",
19 "NpuPreGenDropout"17 "NpuPreGenDropout"
20]18]
21 19 
22 20 
23-class DropOutTask:21+class _DropOutTask:
24 def __init__(self, shape, dtype, device, p):22 def __init__(self, shape, dtype, device, p):
25 self.shape = shape23 self.shape = shape
26 self.dtype = dtype24 self.dtype = dtype
@@ -66,7 +64,7 @@ class NpuCachedDropout(torch.nn.Dropout):
66 return return_obj64 return return_obj
67 key = (shape, dtype, device, self.p)65 key = (shape, dtype, device, self.p)
68 if key not in NpuCachedDropout.task_dict:66 if key not in NpuCachedDropout.task_dict:
69- dropout_task = DropOutTask(shape, dtype, device, self.p)67+ dropout_task = _DropOutTask(shape, dtype, device, self.p)
70 dropout_task.request_count += 168 dropout_task.request_count += 1
71 NpuCachedDropout.task_dict[key] = dropout_task69 NpuCachedDropout.task_dict[key] = dropout_task
72 return return_obj70 return return_obj
@@ -108,7 +106,7 @@ class NpuCachedDropout(torch.nn.Dropout):
108NpuFairseqDropout = NpuCachedDropout106NpuFairseqDropout = NpuCachedDropout
109 107 
110 108 
111-class PreGenDropoutTask:109+class _PreGenDropoutTask:
112 def __init__(self, device, p):110 def __init__(self, device, p):
113 self.device = device111 self.device = device
114 self.p = p112 self.p = p
@@ -186,7 +184,7 @@ class NpuPreGenDropout(torch.nn.Dropout):
186 184 
187 model_device = f"npu:{torch.npu.current_device()}"185 model_device = f"npu:{torch.npu.current_device()}"
188 for p in cls.prob:186 for p in cls.prob:
189- init_task = PreGenDropoutTask(model_device, p)187+ init_task = _PreGenDropoutTask(model_device, p)
190 188 
191 init_task.mask = torch_npu.npu_dropout_gen_mask([init_task.max_mb, 1024, 1024], p=p, 189 init_task.mask = torch_npu.npu_dropout_gen_mask([init_task.max_mb, 1024, 1024], p=p,
192 dtype=torch.float32, device=model_device)190 dtype=torch.float32, device=model_device)
@@ -7,12 +7,11 @@ import torch
7from torch_npu.utils._error_code import ErrCode, ops_error7from torch_npu.utils._error_code import ErrCode, ops_error
8 8 
9__all__ = [9__all__ = [
10- "FusedColorJitter",10+ "FusedColorJitter"
11- "FusedColorJitterApply"
12]11]
13 12 
14 13 
15-class FusedColorJitterApply(object):14+class _FusedColorJitterApply(object):
16 def __init__(self,15 def __init__(self,
17 hue=0.0,16 hue=0.0,
18 saturation=1.0,17 saturation=1.0,
@@ -129,7 +128,7 @@ class FusedColorJitter(torch.nn.Module):
129 self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5),128 self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5),
130 clip_first_on_zero=False)129 clip_first_on_zero=False)
131 130 
132- self.transformer = FusedColorJitterApply(brightness, contrast, saturation, hue)131+ self.transformer = _FusedColorJitterApply(brightness, contrast, saturation, hue)
133 132 
134 @torch.jit.unused133 @torch.jit.unused
135 def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):134 def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):
@@ -8,13 +8,13 @@ import torch.nn.functional as F
8from torch.nn import Parameter8from torch.nn import Parameter
9 9 
10import torch_npu10import torch_npu
11-from torch_npu.contrib.module.ensemble_dropout import NpuCachedDropout, DropOutTask11+from torch_npu.contrib.module.ensemble_dropout import NpuCachedDropout
12from torch_npu.utils._error_code import ErrCode, ops_error12from torch_npu.utils._error_code import ErrCode, ops_error
13from ..function import matmul_transpose13from ..function import matmul_transpose
14 14 
15dropout_class = NpuCachedDropout15dropout_class = NpuCachedDropout
16 16 
17-__all__ = ["MHAConfig", "Matmul_transpose", "MultiheadAttention", "NpuLinear"]17+__all__ = ["Matmul_transpose", "MultiheadAttention"]
18 18 
19 19 
20def _quant_noise(module, p, block_size):20def _quant_noise(module, p, block_size):
@@ -117,7 +117,7 @@ def _quant_noise(module, p, block_size):
117 return module117 return module
118 118 
119 119 
120-class NpuLinear(nn.Linear):120+class _NpuLinear(nn.Linear):
121 def forward(self, input2):121 def forward(self, input2):
122 input_shape = input2.size()122 input_shape = input2.size()
123 if input2.dim() == 3:123 if input2.dim() == 3:
@@ -130,7 +130,7 @@ class NpuLinear(nn.Linear):
130 raise RuntimeError('not support this dim' + ops_error(ErrCode.NOT_SUPPORT))130 raise RuntimeError('not support this dim' + ops_error(ErrCode.NOT_SUPPORT))
131 131 
132 132
133-class MHAConfig:133+class _MHAConfig:
134 use_fussion_mha = False134 use_fussion_mha = False
135 135 
136 @classmethod136 @classmethod
@@ -212,17 +212,17 @@ class MultiheadAttention(nn.Module):
212 ops_error(ErrCode.VALUE))212 ops_error(ErrCode.VALUE))
213 213 
214 self.k_proj = _quant_noise(214 self.k_proj = _quant_noise(
215- NpuLinear(self.kdim, embed_dim, bias=bias), q_noise, qn_block_size215+ _NpuLinear(self.kdim, embed_dim, bias=bias), q_noise, qn_block_size
216 )216 )
217 self.v_proj = _quant_noise(217 self.v_proj = _quant_noise(
218- NpuLinear(self.vdim, embed_dim, bias=bias), q_noise, qn_block_size218+ _NpuLinear(self.vdim, embed_dim, bias=bias), q_noise, qn_block_size
219 )219 )
220 self.q_proj = _quant_noise(220 self.q_proj = _quant_noise(
221- NpuLinear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size221+ _NpuLinear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
222 )222 )
223 223 
224 self.out_proj = _quant_noise(224 self.out_proj = _quant_noise(
225- NpuLinear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size225+ _NpuLinear(embed_dim, embed_dim, bias=bias), q_noise, qn_block_size
226 )226 )
227 227 
228 if add_bias_kv:228 if add_bias_kv:
@@ -298,7 +298,7 @@ class MultiheadAttention(nn.Module):
298 weights for each head. Implies *need_weights*. Default:298 weights for each head. Implies *need_weights*. Default:
299 return the average attention weights over all heads.299 return the average attention weights over all heads.
300 """300 """
301- if MHAConfig.use_fussion_mha:301+ if _MHAConfig.use_fussion_mha:
302 attn = self.multi_attn(query, key, value, key_padding_mask, bsz, tgt_len)302 attn = self.multi_attn(query, key, value, key_padding_mask, bsz, tgt_len)
303 return attn, None303 return attn, None
304 else:304 else:
@@ -1,8 +1,7 @@
1from .wrapper_onnx_ops import _add_onnx_ops1from .wrapper_onnx_ops import _add_onnx_ops
2from .wrapper_ops_combined import _add_ops_combined_for_onnx2from .wrapper_ops_combined import _add_ops_combined_for_onnx
3-from .register_aten_ops_to_onnx import native_layer_norm
4 3 
5-__all__ = ["native_layer_norm"]4+__all__ = []
6 5 
7 6 
8_add_onnx_ops()7_add_onnx_ops()
@@ -1,40 +0,0 @@
1-import functools
2- 
3-import torch
4- 
5-from torch.onnx._internal import registration
6-from torch.onnx import symbolic_helper
7-from torch.onnx.symbolic_opset9 import sub, mul, add, pow, sqrt, reciprocal
8- 
9- 
10-_onnx_symbolic = functools.partial(registration.onnx_symbolic, opset=11)
11- 
12-__all__ = ["native_layer_norm"]
13- 
14- 
15-@_onnx_symbolic("aten::native_layer_norm")
16-@symbolic_helper.quantized_args(True, False, False, False)
17-@symbolic_helper.parse_args("v", "is", "v", "v", "f")
18-def native_layer_norm(g, inputs, normalized_shape, weight, bias, eps):
19- axes = [-i for i in range(len(normalized_shape), 0, -1)]
20- 
21- two_cst = symbolic_helper._generate_wrapped_number(g, 2.0)
22- eps_cst = symbolic_helper._generate_wrapped_number(g, eps)
23- 
24- mean = g.op("ReduceMean", inputs, axes_i=axes)
25- numerator = sub(g, inputs, mean)
26- 
27- # variance = e((x - e(x))^2), and (x - e(x)) is the numerator in the layer_norm formula
28- variance = g.op("ReduceMean", pow(g, numerator, two_cst), axes_i=axes)
29- denominator = sqrt(g, g.op("Add", variance, eps_cst))
30- normalized = g.op("Div", numerator, denominator)
31- 
32- if not (weight is None or symbolic_helper._is_none(weight)):
33- normalized = mul(g, normalized, weight)
34- if not (bias is None or symbolic_helper._is_none(bias)):
35- normalized = add(g, normalized, bias)
36- 
37- # rdenominator := 1 / sqrt(variance + eps)
38- rdenominator = reciprocal(g, denominator)
39- 
40- return normalized, mean, rdenominator