| @@ -9,32 +9,273 @@ | |||
| 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | +"""ClipByValueV2 算子测试规范(TTK TestSpec)。 | ||
| 12 | 13 | ||
| 13 | -import numpy as np | 14 | +kernel/GEIR = clip_by_value_v2(numpy 入/出):y = min(max(x, clip_value_min), |
| 15 | +clip_value_max)(等价 torch.clamp); | ||
| 16 | +aclnn = aclnnClamp / aclnnClampMax / aclnnClampMaxTensor / aclnnClampMin / | ||
| 17 | +aclnnClampMinTensor / aclnnClampTensor / aclnnHardtanh / | ||
| 18 | +aclnnInplaceClampMax / aclnnInplaceClampMaxTensor / | ||
| 19 | +aclnnInplaceClampMinTensor / aclnnInplaceHardtanh(torch 入/出); | ||
| 20 | +e2e = torch.clamp(torch 入/出)。 | ||
| 21 | +标量 min/max 按 ACLNN 规则与 self 计算 promote dtype 后在 CPU 经 torch.clamp 计算。 | ||
| 22 | +""" | ||
| 14 | 23 | ||
| 15 | -__golden__ = {"kernel": {"clip_by_value_v2": "clip_by_value_v2_golden"}} | 24 | +import torch |
| 25 | + | ||
| 26 | +__spec__ = { | ||
| 27 | + "clip_by_value_v2": "ClipByValueV2TestSpec", | ||
| 28 | + "aclnnClamp": "AclnnClampTestSpec", | ||
| 29 | + "aclnnClampMax": "AclnnClampMaxTestSpec", | ||
| 30 | + "aclnnClampMaxTensor": "AclnnClampMaxTensorTestSpec", | ||
| 31 | + "aclnnClampMin": "AclnnClampMinTestSpec", | ||
| 32 | + "aclnnClampMinTensor": "AclnnClampMinTensorTestSpec", | ||
| 33 | + "aclnnClampTensor": "AclnnClampTensorTestSpec", | ||
| 34 | + "aclnnHardtanh": "AclnnHardtanhTestSpec", | ||
| 35 | + "aclnnInplaceClampMax": "AclnnInplaceClampMaxTestSpec", | ||
| 36 | + "aclnnInplaceClampMaxTensor": "AclnnInplaceClampMaxTensorTestSpec", | ||
| 37 | + "aclnnInplaceClampMinTensor": "AclnnInplaceClampMinTensorTestSpec", | ||
| 38 | + "aclnnInplaceHardtanh": "AclnnInplaceHardtanhTestSpec", | ||
| 39 | + "torch.clamp": "TorchClampTestSpec", | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +_FP_REDUCE = (torch.float16, torch.bfloat16) | ||
| 16 | 43 | ||
| 17 | 44 | ||
| 18 | -def clip_by_value_v2_golden(x, clip_value_min, clip_value_max, **kwargs): | 45 | +def _promote(a, b): |
| 19 | - """ | 46 | + """CANN PromoteType 的 torch 近似。""" |
| 20 | - Golden function for clip_by_value_v2. | 47 | + return torch.promote_types(a, b) |
| 21 | - All the parameters (names and order) follow @clip_by_value_v2_def.cpp without outputs. | ||
| 22 | - All the input Tensors are numpy.ndarray. | ||
| 23 | 48 | ||
| 24 | - Args: | ||
| 25 | - **kwargs: {input,output}_{dtypes,ori_shapes,formats,ori_formats}, | ||
| 26 | - full_soc_version, short_soc_version, testcase_name | ||
| 27 | 49 | ||
| 28 | - Returns: | 50 | +def _scalar_default(dtype): |
| 29 | - Output tensor | 51 | + """GetScalarDefaultDtype:浮点标量 → float32,整型/布尔保持原样。""" |
| 30 | - """ | 52 | + if dtype.is_floating_point: |
| 31 | - is_bfloat16 = "bfloat16" in str(x.dtype) | 53 | + return torch.float32 |
| 32 | - if is_bfloat16: | 54 | + return dtype |
| 33 | - x = x.astype("float32") | 55 | + |
| 34 | - clip_value_min = clip_value_min.astype("float32") | 56 | + |
| 35 | - clip_value_max = clip_value_max.astype("float32") | 57 | +def _combine_categories(higher, lower): |
| 36 | - max_ = np.maximum(x, clip_value_min) | 58 | + """CombineCategoriesWithComplex 的简化实现(本算子域内无 complex)。""" |
| 37 | - res = np.minimum(max_, clip_value_max) | 59 | + if higher.is_floating_point: |
| 38 | - if is_bfloat16: | 60 | + return higher |
| 39 | - return res.astype("bfloat16", copy=False) | 61 | + if higher == torch.bool or lower.is_floating_point: |
| 40 | - return res | 62 | + return _promote(higher, lower) |
| 63 | + return higher | ||
| 64 | + | ||
| 65 | + | ||
| 66 | +def _cast_to_out(result, out): | ||
| 67 | + """按 out 张量的 dtype 回写结果(对齐 NPU 输出 dtype)。""" | ||
| 68 | + if out is not None and result.dtype != out.dtype: | ||
| 69 | + return result.to(out.dtype) | ||
| 70 | + return result | ||
| 71 | + | ||
| 72 | + | ||
| 73 | +def _as_bound(v, dtype): | ||
| 74 | + """标量(0 维张量/数值)统一转为 promote dtype 的 0 维张量。""" | ||
| 75 | + if torch.is_tensor(v): | ||
| 76 | + return v.to(dtype) | ||
| 77 | + return v | ||
| 78 | + | ||
| 79 | + | ||
| 80 | +def _clamp_scalar(x, clip_min, clip_max, out=None): | ||
| 81 | + """标量下界/上界 clamp:复刻 ClampPromoteType。""" | ||
| 82 | + promote = x.dtype | ||
| 83 | + if clip_min is not None and clip_max is not None: | ||
| 84 | + scalar_dtype = _promote( | ||
| 85 | + _scalar_default(_dtype_of(clip_min)), _scalar_default(_dtype_of(clip_max)) | ||
| 86 | + ) | ||
| 87 | + promote = _combine_categories(promote, scalar_dtype) | ||
| 88 | + elif clip_min is not None: | ||
| 89 | + promote = _combine_categories(promote, _scalar_default(_dtype_of(clip_min))) | ||
| 90 | + elif clip_max is not None: | ||
| 91 | + promote = _combine_categories(promote, _scalar_default(_dtype_of(clip_max))) | ||
| 92 | + | ||
| 93 | + x_p = x.to(promote) | ||
| 94 | + kwargs = {} | ||
| 95 | + if clip_min is not None: | ||
| 96 | + kwargs["min"] = _as_bound(clip_min, promote) | ||
| 97 | + if clip_max is not None: | ||
| 98 | + kwargs["max"] = _as_bound(clip_max, promote) | ||
| 99 | + return _cast_to_out(torch.clamp(x_p, **kwargs), out) | ||
| 100 | + | ||
| 101 | + | ||
| 102 | +def _clamp_tensor(x, clip_min, clip_max, out=None): | ||
| 103 | + """张量下界/上界 clamp:复刻 ClampTensorPromoteType。""" | ||
| 104 | + promote = x.dtype | ||
| 105 | + if clip_min is not None: | ||
| 106 | + promote = _promote(promote, _dtype_of(clip_min)) | ||
| 107 | + if clip_max is not None: | ||
| 108 | + promote = _promote(promote, _dtype_of(clip_max)) | ||
| 109 | + | ||
| 110 | + x_p = x.to(promote) | ||
| 111 | + kwargs = {} | ||
| 112 | + if clip_min is not None: | ||
| 113 | + kwargs["min"] = _as_bound(clip_min, promote) | ||
| 114 | + if clip_max is not None: | ||
| 115 | + kwargs["max"] = _as_bound(clip_max, promote) | ||
| 116 | + return _cast_to_out(torch.clamp(x_p, **kwargs), out) | ||
| 117 | + | ||
| 118 | + | ||
| 119 | +def _dtype_of(v): | ||
| 120 | + if torch.is_tensor(v): | ||
| 121 | + return v.dtype | ||
| 122 | + return torch.as_tensor(v).dtype | ||
| 123 | + | ||
| 124 | + | ||
| 125 | +def _is_scalar_bound(v): | ||
| 126 | + return v is None or (torch.is_tensor(v) and v.dim() == 0) or not torch.is_tensor(v) | ||
| 127 | + | ||
| 128 | + | ||
| 129 | +def _first(*values): | ||
| 130 | + """返回第一个非 None 的值(不做张量真值判断)。""" | ||
| 131 | + for v in values: | ||
| 132 | + if v is not None: | ||
| 133 | + return v | ||
| 134 | + return None | ||
| 135 | + | ||
| 136 | + | ||
| 137 | +class ThirdPartyImpl: | ||
| 138 | + """torch 三方标杆:按其 min/max 为标量或张量调用对应 clamp(cross_check 用)。""" | ||
| 139 | + | ||
| 140 | + def __call__(self, *args, **kwargs): | ||
| 141 | + x = _first(kwargs.get("self"), kwargs.get("selfRef"), args[0] if args else None) | ||
| 142 | + clip_min = _first( | ||
| 143 | + kwargs.get("clipValueMin"), kwargs.get("clip_value_min"), kwargs.get("min") | ||
| 144 | + ) | ||
| 145 | + clip_max = _first( | ||
| 146 | + kwargs.get("clipValueMax"), kwargs.get("clip_value_max"), kwargs.get("max") | ||
| 147 | + ) | ||
| 148 | + if x is None: | ||
| 149 | + raise TypeError("clip_by_value_v2 third_party: input tensor not provided") | ||
| 150 | + # lower/upper 语义以参数名(pool key)为准:clipValueMin/clip_value_min/min | ||
| 151 | + # 为下界、clipValueMax/clip_value_max/max 为上界;不做按位置参数个数推断 | ||
| 152 | + # min/max 的启发式(torch.clamp(x, v) 的第二位置参是 min,而 aclnnClampMax | ||
| 153 | + # 的唯一界是 max,位置猜测会把两者互相误判)。 | ||
| 154 | + if _is_scalar_bound(clip_min) and _is_scalar_bound(clip_max): | ||
| 155 | + return [_clamp_scalar(x, clip_min, clip_max)] | ||
| 156 | + return [_clamp_tensor(x, clip_min, clip_max)] | ||
| 157 | + | ||
| 158 | + | ||
| 159 | +class ClipByValueV2TestSpec: | ||
| 160 | + """kernel/GEIR 流程:numpy 入/出。""" | ||
| 161 | + | ||
| 162 | + @staticmethod | ||
| 163 | + def golden(x, clip_value_min, clip_value_max, **kwargs): | ||
| 164 | + """kernel/GEIR golden:numpy → torch 计算 → numpy 回转。""" | ||
| 165 | + from ttk.utilities import numpy_to_torch_tensor, torch_to_numpy_tensor | ||
| 166 | + | ||
| 167 | + x_t = numpy_to_torch_tensor(x) | ||
| 168 | + min_t = numpy_to_torch_tensor(clip_value_min) | ||
| 169 | + max_t = numpy_to_torch_tensor(clip_value_max) | ||
| 170 | + return [torch_to_numpy_tensor(torch.clamp(x_t, min=min_t, max=max_t))] | ||
| 171 | + | ||
| 172 | + third_party = {"torch": ThirdPartyImpl} | ||
| 173 | + | ||
| 174 | + | ||
| 175 | +class AclnnClampTestSpec: | ||
| 176 | + """aclnnClamp 流程:out = clamp(self, clipValueMin 标量, clipValueMax 标量)。""" | ||
| 177 | + | ||
| 178 | + def golden(self, clipValueMin, clipValueMax, out, **kwargs): | ||
| 179 | + return [_clamp_scalar(self, clipValueMin, clipValueMax, out)] | ||
| 180 | + | ||
| 181 | + third_party = {"torch": ThirdPartyImpl} | ||
| 182 | + | ||
| 183 | + | ||
| 184 | +class AclnnClampMaxTestSpec: | ||
| 185 | + """aclnnClampMax 流程:out = clamp_max(self, clipValueMax 标量)。""" | ||
| 186 | + | ||
| 187 | + def golden(self, clipValueMax, out, **kwargs): | ||
| 188 | + return [_clamp_scalar(self, None, clipValueMax, out)] | ||
| 189 | + | ||
| 190 | + third_party = {"torch": ThirdPartyImpl} | ||
| 191 | + | ||
| 192 | + | ||
| 193 | +class AclnnClampMinTestSpec: | ||
| 194 | + """aclnnClampMin 流程:out = clamp_min(self, clipValueMin 标量)。""" | ||
| 195 | + | ||
| 196 | + def golden(self, clipValueMin, out, **kwargs): | ||
| 197 | + return [_clamp_scalar(self, clipValueMin, None, out)] | ||
| 198 | + | ||
| 199 | + third_party = {"torch": ThirdPartyImpl} | ||
| 200 | + | ||
| 201 | + | ||
| 202 | +class AclnnHardtanhTestSpec: | ||
| 203 | + """aclnnHardtanh 流程:out = clamp(self, clipValueMin 标量, clipValueMax 标量)。""" | ||
| 204 | + | ||
| 205 | + def golden(self, clipValueMin, clipValueMax, out, **kwargs): | ||
| 206 | + return [_clamp_scalar(self, clipValueMin, clipValueMax, out)] | ||
| 207 | + | ||
| 208 | + third_party = {"torch": ThirdPartyImpl} | ||
| 209 | + | ||
| 210 | + | ||
| 211 | +class AclnnClampTensorTestSpec: | ||
| 212 | + """aclnnClampTensor 流程:out = clamp(self, clipValueMin 张量, clipValueMax 张量)。""" | ||
| 213 | + | ||
| 214 | + def golden(self, clipValueMin, clipValueMax, out, **kwargs): | ||
| 215 | + return [_clamp_tensor(self, clipValueMin, clipValueMax, out)] | ||
| 216 | + | ||
| 217 | + third_party = {"torch": ThirdPartyImpl} | ||
| 218 | + | ||
| 219 | + | ||
| 220 | +class AclnnClampMaxTensorTestSpec: | ||
| 221 | + """aclnnClampMaxTensor 流程:out = clamp_max(self, max 张量)。""" | ||
| 222 | + | ||
| 223 | + def golden(self, max, out, **kwargs): | ||
| 224 | + return [_clamp_tensor(self, None, max, out)] | ||
| 225 | + | ||
| 226 | + third_party = {"torch": ThirdPartyImpl} | ||
| 227 | + | ||
| 228 | + | ||
| 229 | +class AclnnClampMinTensorTestSpec: | ||
| 230 | + """aclnnClampMinTensor 流程:out = clamp_min(self, clipValueMin 张量)。""" | ||
| 231 | + | ||
| 232 | + def golden(self, clipValueMin, out, **kwargs): | ||
| 233 | + return [_clamp_tensor(self, clipValueMin, None, out)] | ||
| 234 | + | ||
| 235 | + third_party = {"torch": ThirdPartyImpl} | ||
| 236 | + | ||
| 237 | + | ||
| 238 | +class AclnnInplaceClampMaxTestSpec: | ||
| 239 | + """aclnnInplaceClampMax 流程:selfRef = clamp_max(selfRef, clipValueMax 标量)。""" | ||
| 240 | + | ||
| 241 | + def golden(selfRef, clipValueMax, **kwargs): | ||
| 242 | + return [_clamp_scalar(selfRef, None, clipValueMax, selfRef)] | ||
| 243 | + | ||
| 244 | + third_party = {"torch": ThirdPartyImpl} | ||
| 245 | + | ||
| 246 | + | ||
| 247 | +class AclnnInplaceClampMaxTensorTestSpec: | ||
| 248 | + """aclnnInplaceClampMaxTensor 流程:selfRef = clamp_max(selfRef, max 张量)。""" | ||
| 249 | + | ||
| 250 | + def golden(selfRef, max, **kwargs): | ||
| 251 | + return [_clamp_tensor(selfRef, None, max, selfRef)] | ||
| 252 | + | ||
| 253 | + third_party = {"torch": ThirdPartyImpl} | ||
| 254 | + | ||
| 255 | + | ||
| 256 | +class AclnnInplaceClampMinTensorTestSpec: | ||
| 257 | + """aclnnInplaceClampMinTensor 流程:selfRef = clamp_min(selfRef, clipValueMin 张量)。""" | ||
| 258 | + | ||
| 259 | + def golden(selfRef, clipValueMin, **kwargs): | ||
| 260 | + return [_clamp_tensor(selfRef, clipValueMin, None, selfRef)] | ||
| 261 | + | ||
| 262 | + third_party = {"torch": ThirdPartyImpl} | ||
| 263 | + | ||
| 264 | + | ||
| 265 | +class AclnnInplaceHardtanhTestSpec: | ||
| 266 | + """aclnnInplaceHardtanh 流程:selfRef = clamp(selfRef, clipValueMin 标量, clipValueMax 标量)。""" | ||
| 267 | + | ||
| 268 | + def golden(selfRef, clipValueMin, clipValueMax, **kwargs): | ||
| 269 | + return [_clamp_scalar(selfRef, clipValueMin, clipValueMax, selfRef)] | ||
| 270 | + | ||
| 271 | + third_party = {"torch": ThirdPartyImpl} | ||
| 272 | + | ||
| 273 | + | ||
| 274 | +class TorchClampTestSpec: | ||
| 275 | + """e2e 流程:torch 入/出。""" | ||
| 276 | + | ||
| 277 | + def golden(input, min, max, **kwargs): | ||
| 278 | + """参数跟随 torch.clamp:input/min/max 位置张量。""" | ||
| 279 | + return [_clamp_tensor(input, min, max)] | ||
| 280 | + | ||
| 281 | + third_party = {"torch": ThirdPartyImpl} | ||
| @@ -9,53 +9,150 @@ | |||
| 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | -import numpy | 12 | +"""Addcdiv 算子测试规范(TTK TestSpec)。 |
| 13 | + | ||
| 14 | +kernel/GEIR = addcdiv(numpy 入/出):out = input_data + value * (x1 / x2); | ||
| 15 | +fp16/bf16 抬 fp32 中间精度(匹配 PrecisionReduceFlag)后回写; | ||
| 16 | +aclnn = aclnnAddcdiv / aclnnInplaceAddcdiv(torch 入/出); | ||
| 17 | +e2e = torch.addcdiv(torch 入/出)。 | ||
| 18 | +""" | ||
| 19 | + | ||
| 13 | import torch | 20 | import torch |
| 14 | 21 | ||
| 15 | -__golden__ = { | 22 | +__spec__ = { |
| 16 | - "kernel": {"addcdiv": "addcdiv_golden"}, | 23 | + "addcdiv": "AddcdivTestSpec", |
| 17 | - "aclnn": {"aclnnAddcdiv": "aclnn_addcdiv_golden"}, | 24 | + "aclnnAddcdiv": "AclnnAddcdivTestSpec", |
| 25 | + "aclnnInplaceAddcdiv": "AclnnInplaceAddcdivTestSpec", | ||
| 26 | + "torch.addcdiv": "TorchAddcdivTestSpec", | ||
| 18 | } | 27 | } |
| 19 | 28 | ||
| 20 | - | 29 | +_FP_REDUCE = (torch.float16, torch.bfloat16) |
| 21 | -def addcdiv_golden(input_data, x1, x2, value, **kwargs): | ||
| 22 | - """ | ||
| 23 | - Kernel golden for addcdiv. | ||
| 24 | - All the parameters follow @addcdiv_def.cpp without outputs. | ||
| 25 | - All the input Tensors are numpy.ndarray. | ||
| 26 | - kwargs may contain: short_soc_version, input_ori_shapes, output_ori_shapes, | ||
| 27 | - input_formats, output_formats, input_ori_formats, output_ori_formats, | ||
| 28 | - input_dtypes, output_dtypes. | ||
| 29 | - """ | ||
| 30 | - data_type = input_data.dtype | ||
| 31 | - input_data = torch.from_numpy(input_data.astype(numpy.float32)) | ||
| 32 | - x1 = torch.from_numpy(x1.astype(numpy.float32)) | ||
| 33 | - x2 = torch.from_numpy(x2.astype(numpy.float32)) | ||
| 34 | - value = value.item() | ||
| 35 | - | ||
| 36 | - res = torch.addcdiv(input_data, x1, x2, value=value) | ||
| 37 | - res_np = res.numpy() | ||
| 38 | - res_np = res_np.astype(data_type, copy=False) | ||
| 39 | - | ||
| 40 | - return res_np | ||
| 41 | 30 | ||
| 42 | 31 | ||
| 43 | -def aclnn_addcdiv_golden(self, tensor1, tensor2, value, out, **kwargs): | 32 | +def _to_scalar(v): |
| 44 | - """ | 33 | + if isinstance(v, torch.Tensor): |
| 45 | - Aclnn golden for aclnnAddcdiv. | 34 | + return v.item() if v.numel() == 1 else v |
| 46 | - All the parameters (name & order) follow \ | 35 | + if hasattr(v, "item"): |
| 47 | - function `aclnnAddcdivGetWorkspaceSize` in @aclnn_addcdiv.h \ | 36 | + return v.item() |
| 48 | - without `workspaceSize` & `executor`. | 37 | + return v |
| 49 | - When all dtypes are natively supported by torch, \ | ||
| 50 | - the Tensors in the parameters are all torch.Tensor. \ | ||
| 51 | - Conversely, when not, the Tensors in the parameters are all numpy.ndarray. | ||
| 52 | 38 | ||
| 53 | - Args: | ||
| 54 | - kwargs: tensor_{dtypes, formats}, scalar_dtypes, short_soc_version, testcase_name | ||
| 55 | 39 | ||
| 56 | - Returns: | 40 | +def _to_fp32(t): |
| 57 | - Output tensors. | 41 | + if t.dtype in (torch.bfloat16, torch.float16): |
| 58 | - """ | 42 | + return t.to(torch.float32) |
| 59 | - import torch | 43 | + return t |
| 60 | 44 | ||
| 61 | - return torch.addcdiv(self, tensor1, tensor2, value=value) | 45 | + |
| 46 | +def _cast_to_out(result, out): | ||
| 47 | + """按 out 张量的 dtype 回写结果(对齐 NPU 输出 dtype)。""" | ||
| 48 | + if out is not None and result.dtype != out.dtype: | ||
| 49 | + return result.to(out.dtype) | ||
| 50 | + return result | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +def _third_party_inputs(args, kwargs): | ||
| 54 | + """从三方标杆调用参数中提取三个输入张量(位置优先,kwargs 兜底)。""" | ||
| 55 | + | ||
| 56 | + def _pick(idx, names): | ||
| 57 | + if len(args) > idx: | ||
| 58 | + return args[idx] | ||
| 59 | + for name in names: | ||
| 60 | + if name in kwargs: | ||
| 61 | + return kwargs[name] | ||
| 62 | + return None | ||
| 63 | + | ||
| 64 | + first = _pick(0, ("self", "selfRef", "selfT", "input_data", "input", "x1")) | ||
| 65 | + second = _pick(1, ("tensor1", "x1")) | ||
| 66 | + third = _pick(2, ("tensor2", "x2")) | ||
| 67 | + missing = [ | ||
| 68 | + n | ||
| 69 | + for n, v in (("input_data", first), ("tensor1", second), ("tensor2", third)) | ||
| 70 | + if v is None | ||
| 71 | + ] | ||
| 72 | + if missing: | ||
| 73 | + raise TypeError( | ||
| 74 | + "addcdiv third_party: input tensors %s not provided (args=%r, kwargs=%r)" | ||
| 75 | + % (missing, args, sorted(kwargs)) | ||
| 76 | + ) | ||
| 77 | + return first, second, third | ||
| 78 | + | ||
| 79 | + | ||
| 80 | +class ThirdPartyImpl: | ||
| 81 | + """torch 三方标杆(各模式共用):value 属性绑定,三个输入按位置接收。""" | ||
| 82 | + | ||
| 83 | + def __init__(self, *, value=1.0, **kwargs): | ||
| 84 | + self.value = value | ||
| 85 | + | ||
| 86 | + def __call__(self, *args, **kwargs): | ||
| 87 | + a, b, c = _third_party_inputs(args, kwargs) | ||
| 88 | + v = kwargs.get("value", self.value) | ||
| 89 | + return [torch.addcdiv(a, b, c, value=_to_scalar(v))] | ||
| 90 | + | ||
| 91 | + | ||
| 92 | +class AddcdivTestSpec: | ||
| 93 | + """kernel/GEIR 流程:numpy 入/出。""" | ||
| 94 | + | ||
| 95 | + @staticmethod | ||
| 96 | + def golden(input_data, x1, x2, value, **kwargs): | ||
| 97 | + """kernel/GEIR golden:numpy → torch 计算 → numpy 回转。""" | ||
| 98 | + from ttk.utilities import numpy_to_torch_tensor, torch_to_numpy_tensor | ||
| 99 | + | ||
| 100 | + t_data = numpy_to_torch_tensor(input_data) | ||
| 101 | + t_x1 = numpy_to_torch_tensor(x1) | ||
| 102 | + t_x2 = numpy_to_torch_tensor(x2) | ||
| 103 | + v = _to_scalar(numpy_to_torch_tensor(value)) | ||
| 104 | + | ||
| 105 | + res = torch.addcdiv(_to_fp32(t_data), _to_fp32(t_x1), _to_fp32(t_x2), value=v) | ||
| 106 | + if t_data.dtype in _FP_REDUCE: | ||
| 107 | + res = res.to(t_data.dtype) | ||
| 108 | + return [torch_to_numpy_tensor(res)] | ||
| 109 | + | ||
| 110 | + third_party = {"torch": ThirdPartyImpl} | ||
| 111 | + | ||
| 112 | + | ||
| 113 | +class AclnnAddcdivTestSpec: | ||
| 114 | + """aclnnAddcdiv 流程:out = self + value * (tensor1 / tensor2)。""" | ||
| 115 | + | ||
| 116 | + def golden(self, tensor1, tensor2, value, out, **kwargs): | ||
| 117 | + """参数跟随 aclnnAddcdiv:self、tensor1、tensor2 张量,value 标量,out 张量。""" | ||
| 118 | + v = _to_scalar(value) | ||
| 119 | + res = torch.addcdiv( | ||
| 120 | + _to_fp32(self), _to_fp32(tensor1), _to_fp32(tensor2), value=v | ||
| 121 | + ) | ||
| 122 | + if self.dtype in _FP_REDUCE: | ||
| 123 | + res = res.to(self.dtype) | ||
| 124 | + return [_cast_to_out(res, out)] | ||
| 125 | + | ||
| 126 | + third_party = {"torch": ThirdPartyImpl} | ||
| 127 | + | ||
| 128 | + | ||
| 129 | +class AclnnInplaceAddcdivTestSpec: | ||
| 130 | + """aclnnInplaceAddcdiv 流程:selfRef += value * (tensor1 / tensor2)。""" | ||
| 131 | + | ||
| 132 | + def golden(selfRef, tensor1, tensor2, value, **kwargs): | ||
| 133 | + """参数跟随 aclnnInplaceAddcdiv:selfRef、tensor1、tensor2 张量,value 标量。""" | ||
| 134 | + v = _to_scalar(value) | ||
| 135 | + res = torch.addcdiv( | ||
| 136 | + _to_fp32(selfRef), _to_fp32(tensor1), _to_fp32(tensor2), value=v | ||
| 137 | + ) | ||
| 138 | + if selfRef.dtype in _FP_REDUCE: | ||
| 139 | + res = res.to(selfRef.dtype) | ||
| 140 | + return [res] | ||
| 141 | + | ||
| 142 | + third_party = {"torch": ThirdPartyImpl} | ||
| 143 | + | ||
| 144 | + | ||
| 145 | +class TorchAddcdivTestSpec: | ||
| 146 | + """e2e 流程:torch 入/出。""" | ||
| 147 | + | ||
| 148 | + def golden(input, tensor1, tensor2, value=1.0, **kwargs): | ||
| 149 | + """参数跟随 torch.addcdiv:input/tensor1/tensor2 位置张量,value 标量。""" | ||
| 150 | + v = _to_scalar(value) | ||
| 151 | + res = torch.addcdiv( | ||
| 152 | + _to_fp32(input), _to_fp32(tensor1), _to_fp32(tensor2), value=v | ||
| 153 | + ) | ||
| 154 | + if input.dtype in _FP_REDUCE: | ||
| 155 | + res = res.to(input.dtype) | ||
| 156 | + return [res] | ||
| 157 | + | ||
| 158 | + third_party = {"torch": ThirdPartyImpl} | ||
| @@ -0,0 +1,158 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: UTF-8 -*- | ||
| 3 | +# ---------------------------------------------------------------------------- | ||
| 4 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 5 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 6 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 7 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 8 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 9 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 10 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 11 | +# ---------------------------------------------------------------------------- | ||
| 12 | +"""FloorDiv 算子测试规范(TTK TestSpec)。 | ||
| 13 | + | ||
| 14 | +kernel/GEIR = floor_div(numpy 入/出):out = floor(x1 / x2);fp16/bf16 抬 fp32 | ||
| 15 | +中间精度(匹配 PrecisionReduceFlag)后按 RINT 回写; | ||
| 16 | +aclnn = aclnnFloorDivide / aclnnFloorDivides / aclnnInplaceFloorDivide / | ||
| 17 | +aclnnInplaceFloorDivides(torch 入/出); | ||
| 18 | +e2e = torch.floor_divide(torch 入/出)。 | ||
| 19 | +""" | ||
| 20 | + | ||
| 21 | +import torch | ||
| 22 | + | ||
| 23 | +__spec__ = { | ||
| 24 | + "floor_div": "FloorDivTestSpec", | ||
| 25 | + "aclnnFloorDivide": "AclnnFloorDivideTestSpec", | ||
| 26 | + "aclnnFloorDivides": "AclnnFloorDividesTestSpec", | ||
| 27 | + "aclnnInplaceFloorDivide": "AclnnInplaceFloorDivideTestSpec", | ||
| 28 | + "aclnnInplaceFloorDivides": "AclnnInplaceFloorDividesTestSpec", | ||
| 29 | + "torch.floor_divide": "TorchFloorDivideTestSpec", | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +_FP_REDUCE = (torch.float16, torch.bfloat16) | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +def _to_tensor(v): | ||
| 36 | + """标量/0 维张量统一为张量,供 .to() 与广播。""" | ||
| 37 | + if torch.is_tensor(v): | ||
| 38 | + return v | ||
| 39 | + return torch.as_tensor(v) | ||
| 40 | + | ||
| 41 | + | ||
| 42 | +def _floor_divide(t1, t2): | ||
| 43 | + """floor(x1 / x2):bool 抬 float32(aclnn promote 规则);fp16/bf16 抬 fp32 | ||
| 44 | + 中间精度后回写(匹配 PrecisionReduceFlag)。""" | ||
| 45 | + orig_dtype = t1.dtype | ||
| 46 | + if orig_dtype == torch.bool: | ||
| 47 | + res = torch.floor_divide(t1.to(torch.float32), _to_tensor(t2).to(torch.float32)) | ||
| 48 | + return res.to(torch.bool) | ||
| 49 | + if orig_dtype in _FP_REDUCE: | ||
| 50 | + res = torch.floor_divide(t1.to(torch.float32), _to_tensor(t2).to(torch.float32)) | ||
| 51 | + return res.to(orig_dtype) | ||
| 52 | + return torch.floor_divide(t1, _to_tensor(t2)) | ||
🟡 Medium Priority 变更行: 影响面:kernel/GEIR 的 失败模式:当 x2 含 0(或出现 INT_MIN / -1)时,torch 的整数除法是未定义行为(返回 0/垃圾值,部分平台直接触发进程级异常),而 CANN 的 div 系算子对这些边界有确定语义。同仓库同族算子 修复方向:整数分支在调用 torch 前先用掩码把 x2 中的 0(以及 INT_MIN/-1 组合)替换为安全除数,计算后再按 CANN 语义覆写结果,参照 truncate_div golden 的写法;uint 与有符号类型需分别处理。 建议:参照 math/truncate_div/tests/assets/golden.py 的整数参考实现:先用 ![]() ![]() Incorrect? | |||
| 53 | + | ||
| 54 | + | ||
| 55 | +def _cast_to_out(result, out): | ||
| 56 | + """按 out 张量的 dtype 回写结果(对齐 NPU 输出 dtype)。""" | ||
| 57 | + if out is not None and result.dtype != out.dtype: | ||
| 58 | + return result.to(out.dtype) | ||
| 59 | + return result | ||
| 60 | + | ||
| 61 | + | ||
| 62 | +def _third_party_inputs( | ||
| 63 | + args, | ||
| 64 | + kwargs, | ||
| 65 | + first_names=("self", "selfRef", "selfT", "x1", "input"), | ||
| 66 | + second_names=("other", "x2"), | ||
| 67 | +): | ||
| 68 | + """从三方标杆调用参数中提取两个输入张量(位置优先,kwargs 兜底)。""" | ||
| 69 | + | ||
| 70 | + def _pick(idx, names): | ||
| 71 | + if len(args) > idx: | ||
| 72 | + return args[idx] | ||
| 73 | + for name in names: | ||
| 74 | + if name in kwargs: | ||
| 75 | + return kwargs[name] | ||
| 76 | + return None | ||
| 77 | + | ||
| 78 | + first = _pick(0, first_names) | ||
| 79 | + second = _pick(1, second_names) | ||
| 80 | + if first is None or second is None: | ||
| 81 | + raise TypeError( | ||
| 82 | + "floor_div third_party: two input tensors not provided (args=%r, kwargs=%r)" | ||
| 83 | + % (args, sorted(kwargs)) | ||
| 84 | + ) | ||
| 85 | + return first, second | ||
| 86 | + | ||
| 87 | + | ||
| 88 | +class ThirdPartyImpl: | ||
| 89 | + """torch 三方标杆(各模式共用):两个输入按位置接收(含标量 other)。""" | ||
| 90 | + | ||
| 91 | + def __call__(self, *args, **kwargs): | ||
| 92 | + a, b = _third_party_inputs(args, kwargs) | ||
| 93 | + return [_floor_divide(a, b)] | ||
| 94 | + | ||
| 95 | + | ||
| 96 | +class FloorDivTestSpec: | ||
| 97 | + """kernel/GEIR 流程:numpy 入/出。""" | ||
| 98 | + | ||
| 99 | + @staticmethod | ||
| 100 | + def golden(x1, x2, **kwargs): | ||
| 101 | + """kernel/GEIR golden:numpy → torch 计算 → numpy 回转。""" | ||
| 102 | + from ttk.utilities import numpy_to_torch_tensor, torch_to_numpy_tensor | ||
| 103 | + | ||
| 104 | + x1_torch = numpy_to_torch_tensor(x1) | ||
| 105 | + x2_torch = numpy_to_torch_tensor(x2) | ||
| 106 | + return [torch_to_numpy_tensor(_floor_divide(x1_torch, x2_torch))] | ||
| 107 | + | ||
| 108 | + third_party = {"torch": ThirdPartyImpl} | ||
| 109 | + | ||
| 110 | + | ||
| 111 | +class AclnnFloorDivideTestSpec: | ||
| 112 | + """aclnnFloorDivide 流程:out = floor_divide(self, other)。""" | ||
| 113 | + | ||
| 114 | + def golden(self, other, out, **kwargs): | ||
| 115 | + """参数跟随 aclnnFloorDivide:self、other 张量,out 张量。""" | ||
| 116 | + return [_cast_to_out(_floor_divide(self, other), out)] | ||
| 117 | + | ||
| 118 | + third_party = {"torch": ThirdPartyImpl} | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +class AclnnFloorDividesTestSpec: | ||
| 122 | + """aclnnFloorDivides 流程:out = floor_divide(self, other 标量)。""" | ||
| 123 | + | ||
| 124 | + def golden(self, other, out, **kwargs): | ||
| 125 | + """参数跟随 aclnnFloorDivides:self 张量、other 标量、out 张量。""" | ||
| 126 | + return [_cast_to_out(_floor_divide(self, other), out)] | ||
| 127 | + | ||
| 128 | + third_party = {"torch": ThirdPartyImpl} | ||
| 129 | + | ||
| 130 | + | ||
| 131 | +class AclnnInplaceFloorDivideTestSpec: | ||
| 132 | + """aclnnInplaceFloorDivide 流程:selfRef = floor_divide(selfRef, other)。""" | ||
| 133 | + | ||
| 134 | + def golden(selfRef, other, **kwargs): | ||
| 135 | + """参数跟随 aclnnInplaceFloorDivide:selfRef、other 张量。""" | ||
| 136 | + return [_floor_divide(selfRef, other)] | ||
| 137 | + | ||
| 138 | + third_party = {"torch": ThirdPartyImpl} | ||
| 139 | + | ||
| 140 | + | ||
| 141 | +class AclnnInplaceFloorDividesTestSpec: | ||
| 142 | + """aclnnInplaceFloorDivides 流程:selfRef = floor_divide(selfRef, other 标量)。""" | ||
| 143 | + | ||
| 144 | + def golden(selfRef, other, **kwargs): | ||
| 145 | + """参数跟随 aclnnInplaceFloorDivides:selfRef 张量、other 标量。""" | ||
| 146 | + return [_floor_divide(selfRef, other)] | ||
| 147 | + | ||
| 148 | + third_party = {"torch": ThirdPartyImpl} | ||
| 149 | + | ||
| 150 | + | ||
| 151 | +class TorchFloorDivideTestSpec: | ||
| 152 | + """e2e 流程:torch 入/出。""" | ||
| 153 | + | ||
| 154 | + def golden(input, other, **kwargs): | ||
| 155 | + """参数跟随 torch.floor_divide:input/other 位置张量。""" | ||
| 156 | + return [_floor_divide(input, other)] | ||
| 157 | + | ||
| 158 | + third_party = {"torch": ThirdPartyImpl} | ||
| @@ -9,78 +9,151 @@ | |||
| 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | +"""Lerp 算子测试规范(TTK TestSpec)。 | ||
| 13 | + | ||
| 14 | +kernel/GEIR = lerp(numpy 入/出):out = start + weight * (end - start); | ||
| 15 | +fp16/bf16 抬 fp32 中间精度(匹配 PrecisionReduceFlag)后回写; | ||
| 16 | +aclnn = aclnnLerp / aclnnInplaceLerp / aclnnLerps / aclnnInplaceLerps(torch 入/出); | ||
| 17 | +e2e = torch.lerp(torch 入/出)。 | ||
| 18 | +""" | ||
| 19 | + | ||
| 12 | import torch | 20 | import torch |
| 13 | 21 | ||
| 14 | -__golden__ = { | 22 | +__spec__ = { |
| 15 | - "kernel": {"lerp": "lerp_golden"}, | 23 | + "lerp": "LerpTestSpec", |
| 16 | - "aclnn": {"aclnnLerp": "aclnn_lerp_golden", "aclnnLerps": "aclnn_lerps_golden"}, | 24 | + "aclnnLerp": "AclnnLerpTestSpec", |
| 25 | + "aclnnInplaceLerp": "AclnnInplaceLerpTestSpec", | ||
| 26 | + "aclnnLerps": "AclnnLerpsTestSpec", | ||
| 27 | + "aclnnInplaceLerps": "AclnnInplaceLerpsTestSpec", | ||
| 28 | + "torch.lerp": "TorchLerpTestSpec", | ||
| 17 | } | 29 | } |
| 18 | 30 | ||
| 19 | 31 | ||
| 20 | -def lerp_golden(start, end, weight, **kwargs): | 32 | +_FP_REDUCE = (torch.float16, torch.bfloat16) |
| 21 | - """ | ||
| 22 | - Kernel golden for lerp. | ||
| 23 | - All the parameters follow @lerp_def.cpp without outputs. | ||
| 24 | - All the input Tensors are numpy.ndarray. | ||
| 25 | - kwargs may contain: short_soc_version, input_ori_shapes, output_ori_shapes, | ||
| 26 | - input_formats, output_formats, input_ori_formats, output_ori_formats, | ||
| 27 | - input_dtypes, output_dtypes. | ||
| 28 | - """ | ||
| 29 | - | ||
| 30 | - dtype = start.dtype | ||
| 31 | - | ||
| 32 | - if "bfloat16" in str(start.dtype): | ||
| 33 | - start = start.astype("float32") | ||
| 34 | - if "bfloat16" in str(end.dtype): | ||
| 35 | - end = end.astype("float32") | ||
| 36 | - if "bfloat16" in str(weight.dtype): | ||
| 37 | - weight = weight.astype("float32") | ||
| 38 | - | ||
| 39 | - start_tensor = torch.from_numpy(start).to(torch.float32) | ||
| 40 | - end_tensor = torch.from_numpy(end).to(torch.float32) | ||
| 41 | - weight_tensor = torch.from_numpy(weight).to(torch.float32) | ||
| 42 | - | ||
| 43 | - golden = torch.lerp(start_tensor, end_tensor, weight_tensor).numpy() | ||
| 44 | - return golden.astype(dtype, copy=False) | ||
| 45 | 33 | ||
| 46 | 34 | ||
| 47 | -def aclnn_lerp_golden(self, end, weight, out, **kwargs): | 35 | +def _to_fp32(v): |
| 48 | - """ | 36 | + if torch.is_tensor(v) and v.dtype in (torch.float16, torch.bfloat16): |
| 49 | - Aclnn golden for aclnnLerp. | 37 | + return v.to(torch.float32) |
| 50 | - All the parameters (name & order) follow \ | 38 | + return v |
| 51 | - function `aclnnLerpGetWorkspaceSize` in @aclnn_lerp_tensor.h \ | ||
| 52 | - without `workspaceSize` & `executor`. | ||
| 53 | - When all dtypes are natively supported by torch, \ | ||
| 54 | - the Tensors in the parameters are all torch.Tensor. \ | ||
| 55 | - Conversely, when not, the Tensors in the parameters are all numpy.ndarray. | ||
| 56 | - | ||
| 57 | - Args: | ||
| 58 | - kwargs: tensor_{dtypes, formats}, scalar_dtypes, short_soc_version, testcase_name | ||
| 59 | - | ||
| 60 | - Returns: | ||
| 61 | - Output tensors. | ||
| 62 | - """ | ||
| 63 | - import torch | ||
| 64 | - | ||
| 65 | - return torch.lerp(self, end, weight) | ||
| 66 | 39 | ||
| 67 | 40 | ||
| 68 | -def aclnn_lerps_golden(self, end, weight, out, **kwargs): | 41 | +def _lerp(start, end, weight): |
| 69 | - """ | 42 | + """torch.lerp 包装:fp16/bf16 抬 fp32 中间精度(匹配 PrecisionReduceFlag)后回写 |
| 70 | - Aclnn golden for aclnnLerps. | 43 | + start dtype;promote 模式下 weight 可能被抬到比 start 更高的精度(如 fp32→fp64 |
| 71 | - All the parameters (name & order) follow \ | 44 | + vs fp16→fp32),需统一回 start dtype。""" |
| 72 | - function `aclnnLerpsGetWorkspaceSize` in @aclnn_lerp_scalar.h \ | 45 | + orig_dtype = start.dtype |
| 73 | - without `workspaceSize` & `executor`. | 46 | + if orig_dtype in _FP_REDUCE: |
| 74 | - When all dtypes are natively supported by torch, \ | 47 | + res = torch.lerp( |
| 75 | - the Tensors in the parameters are all torch.Tensor. \ | 48 | + start.to(torch.float32), end.to(torch.float32), _to_fp32(weight) |
| 76 | - Conversely, when not, the Tensors in the parameters are all numpy.ndarray. | 49 | + ) |
| 50 | + return res.to(orig_dtype) | ||
| 51 | + if torch.is_tensor(weight) and weight.dtype != start.dtype: | ||
| 52 | + weight = weight.to(start.dtype) | ||
| 53 | + return torch.lerp(start, end, weight) | ||
🟡 Medium Priority 变更点: 建议:补充 ![]() ![]() Incorrect? | |||
| 77 | 54 | ||
| 78 | - Args: | ||
| 79 | - kwargs: tensor_{dtypes, formats}, scalar_dtypes, short_soc_version, testcase_name | ||
| 80 | 55 | ||
| 81 | - Returns: | 56 | +def _cast_to_out(result, out): |
| 82 | - Output tensors. | 57 | + """按 out 张量的 dtype 回写结果(对齐 NPU 输出 dtype)。""" |
| 83 | - """ | 58 | + if out is not None and result.dtype != out.dtype: |
| 84 | - import torch | 59 | + return result.to(out.dtype) |
| 60 | + return result | ||
| 85 | 61 | ||
| 86 | - return torch.lerp(self, end, weight) | 62 | + |
| 63 | +def _third_party_inputs(args, kwargs): | ||
| 64 | + """从三方标杆调用参数中提取三个输入(位置优先,kwargs 兜底)。""" | ||
| 65 | + | ||
| 66 | + def _pick(idx, names): | ||
| 67 | + if len(args) > idx: | ||
| 68 | + return args[idx] | ||
| 69 | + for name in names: | ||
| 70 | + if name in kwargs: | ||
| 71 | + return kwargs[name] | ||
| 72 | + return None | ||
| 73 | + | ||
| 74 | + start = _pick(0, ("self", "selfRef", "selfT", "start", "input")) | ||
| 75 | + end = _pick(1, ("end",)) | ||
| 76 | + weight = _pick(2, ("weight",)) | ||
| 77 | + missing = [ | ||
| 78 | + n for n, v in (("start", start), ("end", end), ("weight", weight)) if v is None | ||
| 79 | + ] | ||
| 80 | + if missing: | ||
| 81 | + raise TypeError( | ||
| 82 | + "lerp third_party: input tensors %s not provided (args=%r, kwargs=%r)" | ||
| 83 | + % (missing, args, sorted(kwargs)) | ||
| 84 | + ) | ||
| 85 | + return start, end, weight | ||
| 86 | + | ||
| 87 | + | ||
| 88 | +class ThirdPartyImpl: | ||
| 89 | + """torch 三方标杆(各模式共用):三个输入按位置接收(weight 可为标量)。""" | ||
| 90 | + | ||
| 91 | + def __call__(self, *args, **kwargs): | ||
| 92 | + start, end, weight = _third_party_inputs(args, kwargs) | ||
| 93 | + return [_lerp(start, end, weight)] | ||
| 94 | + | ||
| 95 | + | ||
| 96 | +class LerpTestSpec: | ||
| 97 | + """kernel/GEIR 流程:numpy 入/出。""" | ||
| 98 | + | ||
| 99 | + @staticmethod | ||
| 100 | + def golden(start, end, weight, **kwargs): | ||
| 101 | + """kernel/GEIR golden:numpy → torch 计算 → numpy 回转。""" | ||
| 102 | + from ttk.utilities import numpy_to_torch_tensor, torch_to_numpy_tensor | ||
| 103 | + | ||
| 104 | + s = numpy_to_torch_tensor(start) | ||
| 105 | + e = numpy_to_torch_tensor(end) | ||
| 106 | + w = numpy_to_torch_tensor(weight) | ||
| 107 | + return [torch_to_numpy_tensor(_lerp(s, e, w))] | ||
| 108 | + | ||
| 109 | + third_party = {"torch": ThirdPartyImpl} | ||
| 110 | + | ||
| 111 | + | ||
| 112 | +class AclnnLerpTestSpec: | ||
| 113 | + """aclnnLerp 流程:out = lerp(self, end, weight 张量)。""" | ||
| 114 | + | ||
| 115 | + def golden(self, end, weight, out, **kwargs): | ||
| 116 | + """参数跟随 aclnnLerp:self、end、weight 张量,out 张量。""" | ||
| 117 | + return [_cast_to_out(_lerp(self, end, weight), out)] | ||
| 118 | + | ||
| 119 | + third_party = {"torch": ThirdPartyImpl} | ||
| 120 | + | ||
| 121 | + | ||
| 122 | +class AclnnInplaceLerpTestSpec: | ||
| 123 | + """aclnnInplaceLerp 流程:selfRef = lerp(selfRef, end, weight 张量)。""" | ||
| 124 | + | ||
| 125 | + def golden(selfRef, end, weight, **kwargs): | ||
| 126 | + """参数跟随 aclnnInplaceLerp:selfRef、end、weight 张量。""" | ||
| 127 | + return [_lerp(selfRef, end, weight)] | ||
| 128 | + | ||
| 129 | + third_party = {"torch": ThirdPartyImpl} | ||
| 130 | + | ||
| 131 | + | ||
| 132 | +class AclnnLerpsTestSpec: | ||
| 133 | + """aclnnLerps 流程:out = lerp(self, end, weight 标量)。""" | ||
| 134 | + | ||
| 135 | + def golden(self, end, weight, out, **kwargs): | ||
| 136 | + """参数跟随 aclnnLerps:self、end 张量,weight 标量,out 张量。""" | ||
| 137 | + return [_cast_to_out(_lerp(self, end, weight), out)] | ||
| 138 | + | ||
| 139 | + third_party = {"torch": ThirdPartyImpl} | ||
| 140 | + | ||
| 141 | + | ||
| 142 | +class AclnnInplaceLerpsTestSpec: | ||
| 143 | + """aclnnInplaceLerps 流程:selfRef = lerp(selfRef, end, weight 标量)。""" | ||
| 144 | + | ||
| 145 | + def golden(selfRef, end, weight, **kwargs): | ||
| 146 | + """参数跟随 aclnnInplaceLerps:selfRef、end 张量,weight 标量。""" | ||
| 147 | + return [_lerp(selfRef, end, weight)] | ||
| 148 | + | ||
| 149 | + third_party = {"torch": ThirdPartyImpl} | ||
| 150 | + | ||
| 151 | + | ||
| 152 | +class TorchLerpTestSpec: | ||
| 153 | + """e2e 流程:torch 入/出。""" | ||
| 154 | + | ||
| 155 | + def golden(input, end, weight, **kwargs): | ||
| 156 | + """参数跟随 torch.lerp:input/end/weight 位置参数。""" | ||
| 157 | + return [_lerp(input, end, weight)] | ||
| 158 | + | ||
| 159 | + third_party = {"torch": ThirdPartyImpl} | ||
| @@ -9,53 +9,124 @@ | |||
| 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | +"""Maximum 算子测试规范(TTK TestSpec)。 | ||
| 13 | + | ||
| 14 | +kernel/GEIR = maximum(numpy 入/出):out = elementwise_max(x1, x2); | ||
| 15 | +aclnn = aclnnMaximum(self+other→out)/ aclnnMaxN(TensorList→out)(torch 入/出); | ||
| 16 | +e2e = torch.maximum(torch 入/出)。 | ||
| 17 | +""" | ||
| 18 | + | ||
| 12 | import torch | 19 | import torch |
| 13 | 20 | ||
| 14 | -__golden__ = { | 21 | +__spec__ = { |
| 15 | - "kernel": {"maximum": "maximum_golden"}, | 22 | + "maximum": "MaximumTestSpec", |
| 16 | - "aclnn": {"aclnnMaximum": "aclnn_maximum_golden"}, | 23 | + "aclnnMaximum": "AclnnMaximumTestSpec", |
| 24 | + "aclnnMaxN": "AclnnMaxNTestSpec", | ||
| 25 | + "torch.maximum": "TorchMaximumTestSpec", | ||
| 17 | } | 26 | } |
| 18 | 27 | ||
| 19 | 28 | ||
| 20 | -def maximum_golden(x1, x2, **kwargs): | 29 | +def _cast_to_out(result, out): |
| 21 | - """ | 30 | + """按 out 张量的 dtype 回写结果(对齐 NPU 输出 dtype)。""" |
| 22 | - Kernel golden for maximum. | 31 | + if out is not None and result.dtype != out.dtype: |
| 23 | - All the parameters follow @maximum_def.cpp without outputs. | 32 | + return result.to(out.dtype) |
| 24 | - All the input Tensors are numpy.ndarray. | 33 | + return result |
| 25 | - kwargs may contain: short_soc_version, input_ori_shapes, output_ori_shapes, | ||
| 26 | - input_formats, output_formats, input_ori_formats, output_ori_formats, | ||
| 27 | - input_dtypes, output_dtypes. | ||
| 28 | - """ | ||
| 29 | - dtype = x1.dtype | ||
| 30 | - if "bfloat16" in str(dtype): | ||
| 31 | - x1 = x1.astype("float32") | ||
| 32 | - x2 = x2.astype("float32") | ||
| 33 | - | ||
| 34 | - x = torch.from_numpy(x1) | ||
| 35 | - y = torch.from_numpy(x2) | ||
| 36 | - res = torch.maximum(x, y).numpy() | ||
| 37 | - | ||
| 38 | - if "bfloat16" in str(dtype): | ||
| 39 | - res = res.astype(dtype) | ||
| 40 | - return res | ||
| 41 | 34 | ||
| 42 | 35 | ||
| 43 | -def aclnn_maximum_golden(self, other, out, **kwargs): | 36 | +def _third_party_inputs( |
| 44 | - """ | 37 | + args, |
| 45 | - Aclnn golden for aclnnMaximum. | 38 | + kwargs, |
| 46 | - All the parameters (name & order) follow \ | 39 | + first_names=("self", "selfT", "x1", "input"), |
| 47 | - function `aclnnMaximumGetWorkspaceSize` in @aclnn_maximum.h \ | 40 | + second_names=("other", "x2"), |
| 48 | - without `workspaceSize` & `executor`. | 41 | +): |
| 49 | - When all dtypes are natively supported by torch, \ | 42 | + """从三方标杆调用参数中提取两个输入张量(位置优先,kwargs 兜底)。""" |
| 50 | - the Tensors in the parameters are all torch.Tensor. \ | ||
| 51 | - Conversely, when not, the Tensors in the parameters are all numpy.ndarray. | ||
| 52 | 43 | ||
| 53 | - Args: | 44 | + def _pick(idx, names): |
| 54 | - kwargs: tensor_{dtypes, formats}, scalar_dtypes, short_soc_version, testcase_name | 45 | + if len(args) > idx: |
| 46 | + return args[idx] | ||
| 47 | + for name in names: | ||
| 48 | + if name in kwargs: | ||
| 49 | + return kwargs[name] | ||
| 50 | + return None | ||
| 55 | 51 | ||
| 56 | - Returns: | 52 | + first = _pick(0, first_names) |
| 57 | - Output tensors. | 53 | + second = _pick(1, second_names) |
| 58 | - """ | 54 | + if first is None or second is None: |
| 59 | - import torch | 55 | + raise TypeError( |
| 56 | + "maximum third_party: two input tensors not provided (args=%r, kwargs=%r)" | ||
| 57 | + % (args, sorted(kwargs)) | ||
| 58 | + ) | ||
| 59 | + return first, second | ||
| 60 | 60 | ||
| 61 | - return torch.maximum(self, other) | 61 | + |
| 62 | +class ThirdPartyImpl: | ||
| 63 | + """torch 三方标杆(各模式共用):两个输入按位置接收。""" | ||
| 64 | + | ||
| 65 | + def __call__(self, *args, **kwargs): | ||
| 66 | + a, b = _third_party_inputs(args, kwargs) | ||
| 67 | + return [torch.maximum(a, b)] | ||
| 68 | + | ||
| 69 | + | ||
| 70 | +class MaxNThirdPartyImpl: | ||
| 71 | + """aclnnMaxN 三方标杆:对 TensorList 逐元素 torch.maximum 归约。""" | ||
| 72 | + | ||
| 73 | + def __call__(self, *args, **kwargs): | ||
| 74 | + tensors = kwargs.get("tensors") | ||
| 75 | + if tensors is None and args: | ||
| 76 | + tensors = args[0] | ||
| 77 | + if not tensors: | ||
| 78 | + raise ValueError("aclnnMaxN third_party: empty tensors list") | ||
| 79 | + result = tensors[0] | ||
| 80 | + for t in tensors[1:]: | ||
| 81 | + result = torch.maximum(result, t) | ||
| 82 | + return [result] | ||
| 83 | + | ||
| 84 | + | ||
| 85 | +class MaximumTestSpec: | ||
| 86 | + """kernel/GEIR 流程:numpy 入/出。""" | ||
| 87 | + | ||
| 88 | + @staticmethod | ||
| 89 | + def golden(x1, x2, **kwargs): | ||
| 90 | + """kernel/GEIR golden:numpy → torch 计算 → numpy 回转。""" | ||
| 91 | + from ttk.utilities import numpy_to_torch_tensor, torch_to_numpy_tensor | ||
| 92 | + | ||
| 93 | + x1_torch = numpy_to_torch_tensor(x1) | ||
| 94 | + x2_torch = numpy_to_torch_tensor(x2) | ||
| 95 | + return [torch_to_numpy_tensor(torch.maximum(x1_torch, x2_torch))] | ||
| 96 | + | ||
| 97 | + third_party = {"torch": ThirdPartyImpl} | ||
| 98 | + | ||
| 99 | + | ||
| 100 | +class AclnnMaximumTestSpec: | ||
| 101 | + """aclnnMaximum 流程:out = maximum(self, other)。""" | ||
| 102 | + | ||
| 103 | + def golden(self, other, out, **kwargs): | ||
| 104 | + """参数跟随 aclnnMaximum:self、other 张量,out 张量。""" | ||
| 105 | + return [_cast_to_out(torch.maximum(self, other), out)] | ||
| 106 | + | ||
| 107 | + third_party = {"torch": ThirdPartyImpl} | ||
| 108 | + | ||
| 109 | + | ||
| 110 | +class AclnnMaxNTestSpec: | ||
| 111 | + """aclnnMaxN 流程:out = maximum over tensors list。""" | ||
| 112 | + | ||
| 113 | + def golden(tensors, out, **kwargs): | ||
| 114 | + """参数跟随 aclnnMaxN:tensors 为 aclTensorList,out 张量。""" | ||
| 115 | + if not tensors: | ||
| 116 | + raise ValueError("aclnnMaxN: empty tensors list") | ||
| 117 | + result = tensors[0] | ||
| 118 | + for t in tensors[1:]: | ||
| 119 | + result = torch.maximum(result, t) | ||
| 120 | + return [_cast_to_out(result, out)] | ||
| 121 | + | ||
| 122 | + third_party = {"torch": MaxNThirdPartyImpl} | ||
| 123 | + | ||
| 124 | + | ||
| 125 | +class TorchMaximumTestSpec: | ||
| 126 | + """e2e 流程:torch 入/出。""" | ||
| 127 | + | ||
| 128 | + def golden(input, other, **kwargs): | ||
| 129 | + """参数跟随 torch.maximum:input/other 位置张量。""" | ||
| 130 | + return [torch.maximum(input, other)] | ||
| 131 | + | ||
| 132 | + third_party = {"torch": ThirdPartyImpl} | ||
| @@ -9,53 +9,124 @@ | |||
| 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 9 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 | # See LICENSE in the root of the software repository for the full text of the License. | 10 | # See LICENSE in the root of the software repository for the full text of the License. |
| 11 | # ---------------------------------------------------------------------------- | 11 | # ---------------------------------------------------------------------------- |
| 12 | +"""Minimum 算子测试规范(TTK TestSpec)。 | ||
| 13 | + | ||
| 14 | +kernel/GEIR = minimum(numpy 入/出):out = elementwise_min(x1, x2); | ||
| 15 | +aclnn = aclnnMinimum(self+other→out)/ aclnnMinN(TensorList→out)(torch 入/出); | ||
| 16 | +e2e = torch.minimum(torch 入/出)。 | ||
| 17 | +""" | ||
| 18 | + | ||
| 12 | import torch | 19 | import torch |
| 13 | 20 | ||
| 14 | -__golden__ = { | 21 | +__spec__ = { |
| 15 | - "kernel": {"minimum": "minimum_golden"}, | 22 | + "minimum": "MinimumTestSpec", |
| 16 | - "aclnn": {"aclnnMinimum": "aclnn_minimum_golden"}, | 23 | + "aclnnMinimum": "AclnnMinimumTestSpec", |
| 24 | + "aclnnMinN": "AclnnMinNTestSpec", | ||
| 25 | + "torch.minimum": "TorchMinimumTestSpec", | ||
| 17 | } | 26 | } |
| 18 | 27 | ||
| 19 | 28 | ||
| 20 | -def minimum_golden(x1, x2, **kwargs): | 29 | +def _cast_to_out(result, out): |
| 21 | - """ | 30 | + """按 out 张量的 dtype 回写结果(对齐 NPU 输出 dtype)。""" |
| 22 | - Kernel golden for minimum. | 31 | + if out is not None and result.dtype != out.dtype: |
| 23 | - All the parameters follow @minimum_def.cpp without outputs. | 32 | + return result.to(out.dtype) |
| 24 | - All the input Tensors are numpy.ndarray. | 33 | + return result |
| 25 | - kwargs may contain: short_soc_version, input_ori_shapes, output_ori_shapes, | ||
| 26 | - input_formats, output_formats, input_ori_formats, output_ori_formats, | ||
| 27 | - input_dtypes, output_dtypes. | ||
| 28 | - """ | ||
| 29 | - dtype = x1.dtype | ||
| 30 | - if "bfloat16" in str(dtype): | ||
| 31 | - x1 = x1.astype("float32") | ||
| 32 | - x2 = x2.astype("float32") | ||
| 33 | - | ||
| 34 | - x = torch.from_numpy(x1) | ||
| 35 | - y = torch.from_numpy(x2) | ||
| 36 | - res = torch.minimum(x, y).numpy() | ||
| 37 | - | ||
| 38 | - if "bfloat16" in str(dtype): | ||
| 39 | - res = res.astype(dtype) | ||
| 40 | - return res | ||
| 41 | 34 | ||
| 42 | 35 | ||
| 43 | -def aclnn_minimum_golden(self, other, out, **kwargs): | 36 | +def _third_party_inputs( |
| 44 | - """ | 37 | + args, |
| 45 | - Aclnn golden for aclnnMinimum. | 38 | + kwargs, |
| 46 | - All the parameters (name & order) follow \ | 39 | + first_names=("self", "selfT", "x1", "input"), |
| 47 | - function `aclnnMinimumGetWorkspaceSize` in @aclnn_minimum.h \ | 40 | + second_names=("other", "x2"), |
| 48 | - without `workspaceSize` & `executor`. | 41 | +): |
| 49 | - When all dtypes are natively supported by torch, \ | 42 | + """从三方标杆调用参数中提取两个输入张量(位置优先,kwargs 兜底)。""" |
| 50 | - the Tensors in the parameters are all torch.Tensor. \ | ||
| 51 | - Conversely, when not, the Tensors in the parameters are all numpy.ndarray. | ||
| 52 | 43 | ||
| 53 | - Args: | 44 | + def _pick(idx, names): |
| 54 | - kwargs: tensor_{dtypes, formats}, scalar_dtypes, short_soc_version, testcase_name | 45 | + if len(args) > idx: |
| 46 | + return args[idx] | ||
| 47 | + for name in names: | ||
| 48 | + if name in kwargs: | ||
| 49 | + return kwargs[name] | ||
| 50 | + return None | ||
| 55 | 51 | ||
| 56 | - Returns: | 52 | + first = _pick(0, first_names) |
| 57 | - Output tensors. | 53 | + second = _pick(1, second_names) |
| 58 | - """ | 54 | + if first is None or second is None: |
| 59 | - import torch | 55 | + raise TypeError( |
| 56 | + "minimum third_party: two input tensors not provided (args=%r, kwargs=%r)" | ||
| 57 | + % (args, sorted(kwargs)) | ||
| 58 | + ) | ||
| 59 | + return first, second | ||
| 60 | 60 | ||
| 61 | - return torch.minimum(self, other) | 61 | + |
| 62 | +class ThirdPartyImpl: | ||
| 63 | + """torch 三方标杆(各模式共用):两个输入按位置接收。""" | ||
| 64 | + | ||
| 65 | + def __call__(self, *args, **kwargs): | ||
| 66 | + a, b = _third_party_inputs(args, kwargs) | ||
| 67 | + return [torch.minimum(a, b)] | ||
| 68 | + | ||
| 69 | + | ||
| 70 | +class MinNThirdPartyImpl: | ||
| 71 | + """aclnnMinN 三方标杆:对 TensorList 逐元素 torch.minimum 归约。""" | ||
| 72 | + | ||
| 73 | + def __call__(self, *args, **kwargs): | ||
| 74 | + tensors = kwargs.get("tensors") | ||
| 75 | + if tensors is None and args: | ||
| 76 | + tensors = args[0] | ||
| 77 | + if not tensors: | ||
| 78 | + raise ValueError("aclnnMinN third_party: empty tensors list") | ||
| 79 | + result = tensors[0] | ||
| 80 | + for t in tensors[1:]: | ||
| 81 | + result = torch.minimum(result, t) | ||
| 82 | + return [result] | ||
| 83 | + | ||
| 84 | + | ||
| 85 | +class MinimumTestSpec: | ||
| 86 | + """kernel/GEIR 流程:numpy 入/出。""" | ||
| 87 | + | ||
| 88 | + @staticmethod | ||
| 89 | + def golden(x1, x2, **kwargs): | ||
| 90 | + """kernel/GEIR golden:numpy → torch 计算 → numpy 回转。""" | ||
| 91 | + from ttk.utilities import numpy_to_torch_tensor, torch_to_numpy_tensor | ||
| 92 | + | ||
| 93 | + x1_torch = numpy_to_torch_tensor(x1) | ||
| 94 | + x2_torch = numpy_to_torch_tensor(x2) | ||
| 95 | + return [torch_to_numpy_tensor(torch.minimum(x1_torch, x2_torch))] | ||
| 96 | + | ||
| 97 | + third_party = {"torch": ThirdPartyImpl} | ||
| 98 | + | ||
| 99 | + | ||
| 100 | +class AclnnMinimumTestSpec: | ||
| 101 | + """aclnnMinimum 流程:out = minimum(self, other)。""" | ||
| 102 | + | ||
| 103 | + def golden(self, other, out, **kwargs): | ||
| 104 | + """参数跟随 aclnnMinimum:self、other 张量,out 张量。""" | ||
| 105 | + return [_cast_to_out(torch.minimum(self, other), out)] | ||
| 106 | + | ||
| 107 | + third_party = {"torch": ThirdPartyImpl} | ||
| 108 | + | ||
| 109 | + | ||
| 110 | +class AclnnMinNTestSpec: | ||
| 111 | + """aclnnMinN 流程:out = minimum over tensors list。""" | ||
| 112 | + | ||
| 113 | + def golden(tensors, out, **kwargs): | ||
| 114 | + """参数跟随 aclnnMinN:tensors 为 aclTensorList,out 张量。""" | ||
| 115 | + if not tensors: | ||
| 116 | + raise ValueError("aclnnMinN: empty tensors list") | ||
| 117 | + result = tensors[0] | ||
| 118 | + for t in tensors[1:]: | ||
| 119 | + result = torch.minimum(result, t) | ||
| 120 | + return [_cast_to_out(result, out)] | ||
| 121 | + | ||
| 122 | + third_party = {"torch": MinNThirdPartyImpl} | ||
| 123 | + | ||
| 124 | + | ||
| 125 | +class TorchMinimumTestSpec: | ||
| 126 | + """e2e 流程:torch 入/出。""" | ||
| 127 | + | ||
| 128 | + def golden(input, other, **kwargs): | ||
| 129 | + """参数跟随 torch.minimum:input/other 位置张量。""" | ||
| 130 | + return [torch.minimum(input, other)] | ||
| 131 | + | ||
| 132 | + third_party = {"torch": ThirdPartyImpl} | ||


🟠 High Priority
变更点:clip_by_value_v2 的
ThirdPartyImpl.__call__用kwargs.get("self") or kwargs.get("selfRef") or ...、kwargs.get("clipValueMin") or kwargs.get("clip_value_min") or kwargs.get("min")这类or链来挑选参数(133–143 行)。\n\n受影响契约:or会对每个候选做真值判断,而torch.Tensor.__bool__只在张量恰好 1 个元素时可用。这个类被挂在 aclnnClamp / aclnnClampMaxTensor / aclnnClampTensor 等所有 spec 的third_party = {"torch": ThirdPartyImpl}上,cross_check 时self/selfRef/clipValueMin/clipValueMax都是多元素张量。\n\n失败模式:只要 kwargs 里带任一多元素张量(正常用例即如此),第 133 行bool(tensor)立即抛RuntimeError: Boolean value of Tensor with more than one element is ambiguous,全部 clip 算子的三方交叉校验直接崩溃;即使不抛错(单元素/全零张量被判为 falsy),也会静默跳过该候选去取下一个键,取到错误输入。\n\n修复方向:改为显式is not None判断,不再依赖张量真值。建议:把
or链改成显式 None 判断(例如内部_first(*values)辅助函数),避免对 Tensor 做真值判断。