已合并
修复 matmul 相关算子的日志规范问题 #8995
yangyang016创建于 16 天前
修复 matmul 相关算子的日志规范问题 #8995
已合并
yangyang016创建于 16 天前
5 个文件变更+88-73
@@ -85,7 +85,7 @@ static graphStatus QuantBatchMatmulV3ExecuteFunc(OpExecuteContext* host_api_ctx)
85 apiRet = CANN_OPS_OPB_SYN_EXEC_ACLNN(host_api_ctx, aclnnQuantMatmulV4, x1, x2, scale, offset, pertokenScale,85 apiRet = CANN_OPS_OPB_SYN_EXEC_ACLNN(host_api_ctx, aclnnQuantMatmulV4, x1, x2, scale, offset, pertokenScale,
86 bias, transposeX1, transposeX2, output);86 bias, transposeX1, transposeX2, output);
87 }87 }
88- OP_CHECK_IF(apiRet != GRAPH_SUCCESS, OP_LOGE("aclnnfallback quant_batch_matmul_v3", "api_ret faild:%d", apiRet),88+ OP_CHECK_IF(apiRet != GRAPH_SUCCESS, OP_LOGE("aclnnfallback quant_batch_matmul_v3", "api_ret failed:%d", apiRet),
89 return GRAPH_FAILED);89 return GRAPH_FAILED);
90 90 
91 return GRAPH_SUCCESS;91 return GRAPH_SUCCESS;
@@ -1151,7 +1151,7 @@ bool QuantBatchMatmulV3Tiling::SetMatmulTilingFromTbeTiling()
1151 mt.baseN = tbeTiling_.n_l0 * BLOCK_CUBE;1151 mt.baseN = tbeTiling_.n_l0 * BLOCK_CUBE;
1152 OP_TILING_CHECK(1152 OP_TILING_CHECK(
1153 !SetBlockDimsAndSingleCore(mt),1153 !SetBlockDimsAndSingleCore(mt),
1154- CUBE_INNER_ERR_REPORT(inputParams_.opName, "Set usedCoreNum or singleCoreM/N faild when m(%lu) and n(%lu).",1154+ CUBE_INNER_ERR_REPORT(inputParams_.opName, "Set usedCoreNum or singleCoreM/N failed when m(%lu) and n(%lu).",
1155 inputParams_.mSize, inputParams_.nSize),1155 inputParams_.mSize, inputParams_.nSize),
1156 return false);1156 return false);
1157 1157 
@@ -329,13 +329,13 @@ bool QuantBatchMatmulV3TilingBase::CheckShapeInRangeForMandtoryInputs(size_t x1S
329{329{
330 OP_TILING_CHECK(330 OP_TILING_CHECK(
331 x1ShapeLen < MIN_DIM_NUM_ND || x2ShapeLen < MIN_DIM_NUM_ND,331 x1ShapeLen < MIN_DIM_NUM_ND || x2ShapeLen < MIN_DIM_NUM_ND,
332- CUBE_INNER_ERR_REPORT(inputParams_.opName, "input x1 deminsion and x2 deminsion should be greater than 1, \332+ CUBE_INNER_ERR_REPORT(inputParams_.opName, "input x1 dimension and x2 dimension should be greater than 1, \
333-but x1 deminsion: %zu, x2 deminsion: %zu",333+but x1 dimension: %zu, x2 dimension: %zu",
334 x1ShapeLen, x2ShapeLen),334 x1ShapeLen, x2ShapeLen),
335 return false);335 return false);
336 OP_TILING_CHECK(x1ShapeLen > MAX_DIM_NUM_ND || x2ShapeLen > MAX_DIM_NUM_ND,336 OP_TILING_CHECK(x1ShapeLen > MAX_DIM_NUM_ND || x2ShapeLen > MAX_DIM_NUM_ND,
337- CUBE_INNER_ERR_REPORT(inputParams_.opName, "x1 deminsion and x2 deminsion should be less than 7, \337+ CUBE_INNER_ERR_REPORT(inputParams_.opName, "x1 dimension and x2 dimension should be less than 7, \
338-but x1 deminsion: %zu, x2 deminsion: %zu",338+but x1 dimension: %zu, x2 dimension: %zu",
339 x1ShapeLen, x2ShapeLen),339 x1ShapeLen, x2ShapeLen),
340 return false);340 return false);
341 341 
@@ -576,7 +576,7 @@ bool QuantBatchMatmulV3TilingBase::AnalyzeInputs()
576 auto x1Outer = x1Shape.GetDim(x1ShapeLen - LAST_SECOND_DIM_INDEX);576 auto x1Outer = x1Shape.GetDim(x1ShapeLen - LAST_SECOND_DIM_INDEX);
577 auto x2Inner = x2Shape.GetDim(x2ShapeLen - LAST_FIRST_DIM_INDEX);577 auto x2Inner = x2Shape.GetDim(x2ShapeLen - LAST_FIRST_DIM_INDEX);
578 auto x2Outer = x2Shape.GetDim(x2ShapeLen - LAST_SECOND_DIM_INDEX);578 auto x2Outer = x2Shape.GetDim(x2ShapeLen - LAST_SECOND_DIM_INDEX);
579- 579+ 
580 const std::vector<int64_t> dimValueOfMKN = {x1Inner, x1Outer, x2Inner, x2Outer};580 const std::vector<int64_t> dimValueOfMKN = {x1Inner, x1Outer, x2Inner, x2Outer};
581 inputParams_.mSize = static_cast<uint64_t>(inputParams_.transA ? x1Inner : x1Outer);581 inputParams_.mSize = static_cast<uint64_t>(inputParams_.transA ? x1Inner : x1Outer);
582 inputParams_.kSize = static_cast<uint64_t>(inputParams_.transA ? x1Outer : x1Inner);582 inputParams_.kSize = static_cast<uint64_t>(inputParams_.transA ? x1Outer : x1Inner);
@@ -681,7 +681,7 @@ by the %s dimension of %s [%lu], the real group_size in %s dimension can not be
681}681}
682 682 
683bool QuantBatchMatmulV3TilingBase::AnalyzeMxGroupInfo(const gert::Shape& scaleShape, const gert::Shape& pertoken,683bool QuantBatchMatmulV3TilingBase::AnalyzeMxGroupInfo(const gert::Shape& scaleShape, const gert::Shape& pertoken,
684- size_t pertokenShapeLen, size_t scaleShapeLen)684+ size_t pertokenShapeLen, size_t scaleShapeLen)
685{685{
686 const size_t mxScaleNonBatchDimNum = 3;686 const size_t mxScaleNonBatchDimNum = 3;
687 const uint64_t microScalingAlign = 2;687 const uint64_t microScalingAlign = 2;
@@ -690,19 +690,18 @@ bool QuantBatchMatmulV3TilingBase::AnalyzeMxGroupInfo(const gert::Shape& scaleSh
690 }690 }
691 size_t pertokenBatchDimNum = pertokenShapeLen - mxScaleNonBatchDimNum;691 size_t pertokenBatchDimNum = pertokenShapeLen - mxScaleNonBatchDimNum;
692 size_t scaleBatchDimNum = scaleShapeLen - mxScaleNonBatchDimNum;692 size_t scaleBatchDimNum = scaleShapeLen - mxScaleNonBatchDimNum;
693- uint64_t mxPertokenMSize =693+ uint64_t mxPertokenMSize = static_cast<uint64_t>(
694- static_cast<uint64_t>(pertoken.GetDim(pertokenBatchDimNum + (inputParams_.transA ? 1 : 0)));694+ pertoken.GetDim(pertokenBatchDimNum + (inputParams_.transA ? 1 : 0)));
695 if (!ReCalcGroupSize(inputParams_.groupSizeM, inputParams_.mSize, mxPertokenMSize, "m")) {695 if (!ReCalcGroupSize(inputParams_.groupSizeM, inputParams_.mSize, mxPertokenMSize, "m")) {
696 return false;696 return false;
697 }697 }
698- uint64_t mxPertokenKSize =698+ uint64_t mxPertokenKSize = static_cast<uint64_t>(
699- static_cast<uint64_t>(pertoken.GetDim(pertokenBatchDimNum + (inputParams_.transA ? 0 : 1))) *699+ pertoken.GetDim(pertokenBatchDimNum + (inputParams_.transA ? 0 : 1))) *
700- microScalingAlign;700+ microScalingAlign;
701 if (!ReCalcGroupSize(inputParams_.groupSizeK, inputParams_.kSize, mxPertokenKSize, "k")) {701 if (!ReCalcGroupSize(inputParams_.groupSizeK, inputParams_.kSize, mxPertokenKSize, "k")) {
702 return false;702 return false;
703 }703 }
704- uint64_t mxScaleNSize =704+ uint64_t mxScaleNSize = static_cast<uint64_t>(scaleShape.GetDim(scaleBatchDimNum + (inputParams_.transB ? 0 : 1)));
705- static_cast<uint64_t>(scaleShape.GetDim(scaleBatchDimNum + (inputParams_.transB ? 0 : 1)));
706 if (!ReCalcGroupSize(inputParams_.groupSizeN, inputParams_.nSize, mxScaleNSize, "n")) {705 if (!ReCalcGroupSize(inputParams_.groupSizeN, inputParams_.nSize, mxScaleNSize, "n")) {
707 return false;706 return false;
708 }707 }
@@ -12,20 +12,14 @@
12 12 
13import torch13import torch
14import torch_npu14import torch_npu
15-import ctypes
16-import logging
17-import numpy as np
18-import random
19 15 
20from atk.common.log import Logger16from atk.common.log import Logger
21from atk.configs.dataset_config import InputDataset17from atk.configs.dataset_config import InputDataset
22from atk.configs.results_config import TaskResult18from atk.configs.results_config import TaskResult
23from atk.tasks.api_execute import register19from atk.tasks.api_execute import register
24from atk.tasks.api_execute.base_api import BaseApi20from atk.tasks.api_execute.base_api import BaseApi
25-from atk.tasks.dataset.base_dataset import OpsDataset
26from atk.tasks.api_execute.aclnn_base_api import AclnnBaseApi21from atk.tasks.api_execute.aclnn_base_api import AclnnBaseApi
27from atk.tasks.backends.lib_interface.acl_wrapper import TensorPtr22from atk.tasks.backends.lib_interface.acl_wrapper import TensorPtr
28-from atk.tasks.backends.lib_interface.acl_wrapper import AclFormat, Int64, AclTensorList, AclIntArray, AclTensor
29 23 
30logging = Logger().get_logger()24logging = Logger().get_logger()
31 25 
@@ -44,96 +38,121 @@ class AclnnQuantMatmulV4(BaseApi):
44 38 
45 def __call__(self, input_data: InputDataset, with_output: bool = False):39 def __call__(self, input_data: InputDataset, with_output: bool = False):
46 # 值域截断点40 # 值域截断点
47- value_max = 127 if self.x1.dtype == torch.int8 else 741+ if self.bias is None and self.pertoken is None:
48- if self.bias == None and self.pertoken == None:42+ logging.info(
49- logging.info(f"用例 id: {self.task_result.case_config.id} | 场景 0: pertoken - bias - ")43+ f"Case ID: {self.task_result.case_config.id} | Scenario 0: pertoken - no, bias - no"
44+ )
50 # out = x1 @ x2 ∗ scale + offset45 # out = x1 @ x2 ∗ scale + offset
51 out = torch.matmul(self.x1, self.x2).to(self.scale.dtype) * self.scale46 out = torch.matmul(self.x1, self.x2).to(self.scale.dtype) * self.scale
52- if self.offset != None:47+ if self.offset is not None:
53 out = out.to(self.offset.dtype) + self.offset48 out = out.to(self.offset.dtype) + self.offset
54 49 
55- elif self.pertoken == None and self.bias.dtype == torch.int32:50+ elif self.pertoken is None and self.bias.dtype == torch.int32:
56- logging.info(f"用例 id: {self.task_result.case_config.id} | 场景 1: pertoken - 无 bias - INT32")51+ logging.info(
52+ f"Case ID: {self.task_result.case_config.id} | Scenario 1: pertoken - no, bias - INT32"
53+ )
57 # out = (x1 @ x2 + bias) ∗ scale + offset54 # out = (x1 @ x2 + bias) ∗ scale + offset
58- out = (torch.matmul(self.x1, self.x2).to(self.bias.dtype) + self.bias).to(self.scale.dtype) * self.scale55+ out = (torch.matmul(self.x1, self.x2).to(self.bias.dtype) + self.bias).to(
59- if self.offset != None:56+ self.scale.dtype
57+ ) * self.scale
58+ if self.offset is not None:
60 out = out.to(self.offset.dtype) + self.offset59 out = out.to(self.offset.dtype) + self.offset
61 60 
62- elif self.pertoken == None and self.bias.dtype in [torch.bfloat16, torch.float32]:61+ elif self.pertoken is None and self.bias.dtype in [
63- logging.info(f"用例 id: {self.task_result.case_config.id} | 场景 2: pertoken - 无 bias - BFLOAT16/FLOAT32")62+ torch.bfloat16,
63+ torch.float32,
64+ ]:
65+ logging.info(
66+ f"Case ID: {self.task_result.case_config.id} | Scenario 2: "
67+ "pertoken - no, bias - BFLOAT16/FLOAT32"
68+ )
64 # out = x1 @ x2 ∗ scale + bias69 # out = x1 @ x2 ∗ scale + bias
65 out = torch.matmul(self.x1, self.x2)70 out = torch.matmul(self.x1, self.x2)
66 out = out.to(self.scale.dtype) * self.scale71 out = out.to(self.scale.dtype) * self.scale
67 out = out.to(self.bias.dtype) + self.bias72 out = out.to(self.bias.dtype) + self.bias
68 73 
69- elif self.pertoken != None and self.bias == None:74+ elif self.pertoken is not None and self.bias is None:
70- logging.info(f"用例 id: {self.task_result.case_config.id} | 场景 3: pertoken - 有 bias - 无")75+ logging.info(
76+ f"Case ID: {self.task_result.case_config.id} | Scenario 3: pertoken - yes, bias - no"
77+ )
71 # out = x1 @ x2 ∗ scale ∗ pertokenScaleOptional78 # out = x1 @ x2 ∗ scale ∗ pertokenScaleOptional
72- out = torch.matmul(self.x1, self.x2).to(self.scale.dtype) * self.scale 79+ out = torch.matmul(self.x1, self.x2).to(self.scale.dtype) * self.scale
73- out = out.to(self.pertoken.dtype) * self.pertoken.unsqueeze(1) 80+ out = out.to(self.pertoken.dtype) * self.pertoken.unsqueeze(1)
74 81 
75- elif self.pertoken != None and self.bias.dtype == torch.int32:82+ elif self.pertoken is not None and self.bias.dtype == torch.int32:
76- logging.info(f"用例 id: {self.task_result.case_config.id} | 场景 4: pertoken - 有 bias - INT32")83+ logging.info(
84+ f"Case ID: {self.task_result.case_config.id} | Scenario 4: pertoken - yes, bias - INT32"
85+ )
77 # out=(x1 @ x2 + bias) ∗ scale ∗ pertokenScaleOptional86 # out=(x1 @ x2 + bias) ∗ scale ∗ pertokenScaleOptional
78- out = (torch.matmul(self.x1, self.x2) + self.bias).to(self.scale.dtype) * self.scale87+ out = (torch.matmul(self.x1, self.x2) + self.bias).to(
79- out = out.to(self.pertoken.dtype) * self.pertoken.unsqueeze(1) 88+ self.scale.dtype
89+ ) * self.scale
90+ out = out.to(self.pertoken.dtype) * self.pertoken.unsqueeze(1)
80 91 
81- elif self.pertoken != None and self.bias.dtype in [torch.bfloat16, torch.float16, torch.float32]:92+ elif self.pertoken is not None and self.bias.dtype in [
82- logging.info(f"用例 id: {self.task_result.case_config.id} | 场景 5: pertoken - 有 bias - BFLOAT16/FLOAT16/FLOAT32")93+ torch.bfloat16,
94+ torch.float16,
95+ torch.float32,
96+ ]:
97+ logging.info(
98+ f"Case ID: {self.task_result.case_config.id} | Scenario 5: "
99+ "pertoken - yes, bias - BFLOAT16/FLOAT16/FLOAT32"
100+ )
83 # out = x1 @ x2 ∗ scale ∗ pertokenScaleOptional + bias101 # out = x1 @ x2 ∗ scale ∗ pertokenScaleOptional + bias
84- out = torch.matmul(self.x1, self.x2).to(self.scale.dtype) * self.scale 102+ out = torch.matmul(self.x1, self.x2).to(self.scale.dtype) * self.scale
85- out = out.to(self.pertoken.dtype) * self.pertoken.unsqueeze(1) 103+ out = out.to(self.pertoken.dtype) * self.pertoken.unsqueeze(1)
86 out = out.to(self.bias.dtype) + self.bias104 out = out.to(self.bias.dtype) + self.bias
87 105 
88 else:106 else:
89- logging.error("输入 dtype 组合无效.")107+ logging.error("Invalid input dtype combination.")
90 raise ValueError108 raise ValueError
91 109 
92 return out.to(self.out_dtype)110 return out.to(self.out_dtype)
93 111 
94 def init_by_input_data(self, input_data: InputDataset):112 def init_by_input_data(self, input_data: InputDataset):
95- self.x1 = input_data.kwargs['x1'].clone()113+ self.x1 = input_data.kwargs["x1"].clone()
96- self.x2 = input_data.kwargs['x2'].clone()114+ self.x2 = input_data.kwargs["x2"].clone()
97- self.scale = input_data.kwargs['Scale'].clone()115+ self.scale = input_data.kwargs["Scale"].clone()
98 116 
99- if input_data.kwargs['transposeX1']:117+ if input_data.kwargs["transposeX1"]:
100 self.x1 = self.x1.transpose(-2, -1)118 self.x1 = self.x1.transpose(-2, -1)
101- 119+ 
102- if input_data.kwargs['transposeX2']:120+ if input_data.kwargs["transposeX2"]:
103 self.x2 = self.x2.transpose(-2, -1)121 self.x2 = self.x2.transpose(-2, -1)
104 122 
105- if len(input_data.kwargs['Offset'].shape) != 0:123+ if len(input_data.kwargs["Offset"].shape) != 0:
106- self.offset = input_data.kwargs['Offset'].clone()124+ self.offset = input_data.kwargs["Offset"].clone()
107 else:125 else:
108 self.offset = None126 self.offset = None
109 127 
110- if len(input_data.kwargs['pertokenScaleOptional'].shape) != 0:128+ if len(input_data.kwargs["pertokenScaleOptional"].shape) != 0:
111- self.pertoken = input_data.kwargs['pertokenScaleOptional'].clone()129+ self.pertoken = input_data.kwargs["pertokenScaleOptional"].clone()
112 else:130 else:
113 self.pertoken = None131 self.pertoken = None
114- 132+ 
115- if len(input_data.kwargs['bias'].shape) != 0:133+ if len(input_data.kwargs["bias"].shape) != 0:
116- self.bias = input_data.kwargs['bias'].clone()134+ self.bias = input_data.kwargs["bias"].clone()
117 else:135 else:
118 self.bias = None136 self.bias = None
119- 137+ 
120- self.out_dtype = input_data.kwargs['out'].dtype138+ self.out_dtype = input_data.kwargs["out"].dtype
139+ 
121 140 
122@register("execute_aclnn_quant_matmul_v4")141@register("execute_aclnn_quant_matmul_v4")
123class PyAclnnQuantMatmulV4(AclnnBaseApi):142class PyAclnnQuantMatmulV4(AclnnBaseApi):
124- def init_by_input_data(self, input_data: InputDataset): 143+ def init_by_input_data(self, input_data: InputDataset):
125 input_args = [] # 算子的入参列表144 input_args = [] # 算子的入参列表
126 output_packages = [] # 算子的出参数据包列表145 output_packages = [] # 算子的出参数据包列表
127 146 
128- x1 = input_data.kwargs['x1']147+ x1 = input_data.kwargs["x1"]
129 if x1.dtype == torch.int32:148 if x1.dtype == torch.int32:
130 x1_npu = torch_npu.npu_convert_weight_to_int4pack(x1.npu())149 x1_npu = torch_npu.npu_convert_weight_to_int4pack(x1.npu())
131- input_data.kwargs['x1'] = x1_npu150+ input_data.kwargs["x1"] = x1_npu
132 151 
133- x2 = input_data.kwargs['x2']152+ x2 = input_data.kwargs["x2"]
134 if x2.dtype == torch.int32:153 if x2.dtype == torch.int32:
135 x2_npu = torch_npu.npu_convert_weight_to_int4pack(x2.npu())154 x2_npu = torch_npu.npu_convert_weight_to_int4pack(x2.npu())
136- input_data.kwargs['x2'] = x2_npu155+ input_data.kwargs["x2"] = x2_npu
137 156 
138 input_data.kwargs.pop("isNz")157 input_data.kwargs.pop("isNz")
139 input_data.kwargs.pop("out")158 input_data.kwargs.pop("out")
@@ -141,7 +160,7 @@ class PyAclnnQuantMatmulV4(AclnnBaseApi):
141 for i, arg in enumerate(input_data.args):160 for i, arg in enumerate(input_data.args):
142 data = self.backend.convert_input_data(arg, index=i)161 data = self.backend.convert_input_data(arg, index=i)
143 input_args.extend(data)162 input_args.extend(data)
144- 163+ 
145 for name, kwarg in input_data.kwargs.items():164 for name, kwarg in input_data.kwargs.items():
146 data = self.backend.convert_input_data(kwarg, name=name)165 data = self.backend.convert_input_data(kwarg, name=name)
147 input_args.extend(data)166 input_args.extend(data)
@@ -150,15 +169,14 @@ class PyAclnnQuantMatmulV4(AclnnBaseApi):
150 output = self.backend.convert_output_data(output_data, index)169 output = self.backend.convert_output_data(output_data, index)
151 output_packages.extend(output)170 output_packages.extend(output)
152 171 
153- if len(input_data.kwargs['Offset'].shape) == 0:172+ if len(input_data.kwargs["Offset"].shape) == 0:
154 input_args[3] = TensorPtr()173 input_args[3] = TensorPtr()
155 174 
156- if len(input_data.kwargs['pertokenScaleOptional'].shape) == 0:175+ if len(input_data.kwargs["pertokenScaleOptional"].shape) == 0:
157 input_args[4] = TensorPtr()176 input_args[4] = TensorPtr()
158 177 
159- if len(input_data.kwargs['bias'].shape) == 0:178+ if len(input_data.kwargs["bias"].shape) == 0:
160 input_args[5] = TensorPtr()179 input_args[5] = TensorPtr()
161- 180+ 
162 input_args.extend(output_packages)181 input_args.extend(output_packages)
163 return input_args, output_packages182 return input_args, output_packages
164- 
@@ -120,8 +120,6 @@ bool ProcessWeightBlock(std::vector<int8_t>& sparseWeightBlock, std::vector<int8
120 auto compressedParam0 = SPARSE_MAP.find(weightPattern0);120 auto compressedParam0 = SPARSE_MAP.find(weightPattern0);
121 auto compressedParam1 = SPARSE_MAP.find(weightPattern1);121 auto compressedParam1 = SPARSE_MAP.find(weightPattern1);
122 if (compressedParam0 == SPARSE_MAP.end() || compressedParam1 == SPARSE_MAP.end()) {122 if (compressedParam0 == SPARSE_MAP.end() || compressedParam1 == SPARSE_MAP.end()) {
123- std::cout << "compressedParam0 = " << weightPattern0 << std::endl;
124- std::cout << "compressedParam1 = " << weightPattern1 << std::endl;
125 OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Data of weight not satisfy 50%% sparsity rate.");123 OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Data of weight not satisfy 50%% sparsity rate.");
126 return false;124 return false;
127 }125 }
@@ -258,4 +256,4 @@ aclnnStatus aclnnTransSparse4to2Para(const int8_t* weight, aclIntArray* shape, i
258 *sparseWeightDimsNum = sparseWShape.size();256 *sparseWeightDimsNum = sparseWShape.size();
259 *indexDimsNum = idxShape.size();257 *indexDimsNum = idxShape.size();
260 return ACLNN_SUCCESS;258 return ACLNN_SUCCESS;
261-}259+}