Merged
fix(operation): Cast will support fp4e2m1/fp4e1m2 and will no longer support fp4e2m1x2/fp4e1m2x2. #6490
刘奕哲Add on Sep 18
fix(operation): Cast will support fp4e2m1/fp4e1m2 and will no longer support fp4e2m1x2/fp4e1m2x2. #6490
Merged
刘奕哲Add on Sep 18
Total: 7 files changed+36-20
@@ -33,8 +33,8 @@ cast(input: Tensor, dtype: DataType, mode: CastMode = CastMode.CAST_NONE,
33 33 
34| 参数名 | 输入/输出 | 说明 |34| 参数名 | 输入/输出 | 说明 |
35|------------|-----------|----------------------------------------------------------------------|35|------------|-----------|----------------------------------------------------------------------|
36-| input | 输入 | 源操作数。<br>支持的类型为:Tensor。<br>Tensor支持的数据类型为:DT_FP32,DT_FP16,DT_BF16,DT_INT8,DT_UINT8,DT_INT16,DT_INT32,DT_INT64,DT_INT4,DT_FP8E4M3,DT_FP8E5M2,DT_HF8,DT_FP4_E2M1X2,DT_FP4_E1M2X2。<br>不支持空Tensor;Shape仅支持1-4维;Shape Size不大于2147483647(即INT32_MAX)。 |36+| input | 输入 | 源操作数。<br>支持的类型为:Tensor。<br>Tensor支持的数据类型为:DT_FP32,DT_FP16,DT_BF16,DT_INT8,DT_UINT8,DT_INT16,DT_INT32,DT_INT64,DT_INT4,DT_FP8E4M3,DT_FP8E5M2,DT_HF8,DT_FP4_E2M1,DT_FP4_E1M2。<br>不支持空Tensor;Shape仅支持1-4维;Shape Size不大于2147483647(即INT32_MAX)。 |
37-| dtype | 输入 | 精度转换后的数据类型。<br>支持的数据类型为:DT_FP32,DT_FP16,DT_BF16,DT_INT8,DT_UINT8,DT_INT16,DT_INT32,DT_INT64,DT_INT4,DT_FP8E4M3,DT_FP8E5M2,DT_HF8,DT_FP4_E2M1X2,DT_FP4_E1M2X2。 |37+| dtype | 输入 | 精度转换后的数据类型。<br>支持的数据类型为:DT_FP32,DT_FP16,DT_BF16,DT_INT8,DT_UINT8,DT_INT16,DT_INT32,DT_INT64,DT_INT4,DT_FP8E4M3,DT_FP8E5M2,DT_HF8,DT_FP4_E2M1,DT_FP4_E1M2。 |
38| CastMode | 输入 | 源操作数枚举类型,用以控制精度转换处理模式,具体定义为:[CastMode](../datatype/CastMode.md)。<br>默认为CAST_NONE,常见类型之间的转换,框架会自动转换,与torch对齐,详见约束说明。 |38| CastMode | 输入 | 源操作数枚举类型,用以控制精度转换处理模式,具体定义为:[CastMode](../datatype/CastMode.md)。<br>默认为CAST_NONE,常见类型之间的转换,框架会自动转换,与torch对齐,详见约束说明。 |
39| SaturationMode | 输入 | 饱和模式枚举类型,用以控制浮点数转整数时的溢出处理方式,具体定义为:[SaturationMode](../datatype/SaturationMode.md)。<br>默认为OFF(截断模式),当设置为ON时,超出目标类型范围的数值会被截断到最大值或最小值(饱和截断),详见约束说明。 |39| SaturationMode | 输入 | 饱和模式枚举类型,用以控制浮点数转整数时的溢出处理方式,具体定义为:[SaturationMode](../datatype/SaturationMode.md)。<br>默认为OFF(截断模式),当设置为ON时,超出目标类型范围的数值会被截断到最大值或最小值(饱和截断),详见约束说明。 |
40 40 
@@ -131,10 +131,10 @@ Ascend 950PR/Ascend 950DT使用不同的CastMode体系,内部实现基于 `Rou
131| DT_FP8E4M3 | DT_FP32 | 不支持舍入模式 | - | 类型扩展 |131| DT_FP8E4M3 | DT_FP32 | 不支持舍入模式 | - | 类型扩展 |
132| DT_FP8E5M2 | DT_FP32 | 不支持舍入模式 | - | 类型扩展 |132| DT_FP8E5M2 | DT_FP32 | 不支持舍入模式 | - | 类型扩展 |
133| DT_HF8 | DT_FP32 | 不支持舍入模式 | - | 类型扩展 |133| DT_HF8 | DT_FP32 | 不支持舍入模式 | - | 类型扩展 |
134-| DT_BF16 | DT_FP4_E2M1X2 | 不支持舍入模式 | - | - |134+| DT_BF16 | DT_FP4_E2M1 | 不支持舍入模式 | - | - |
135-| DT_BF16 | DT_FP4_E1M2X2 | 不支持舍入模式 | - | - |135+| DT_BF16 | DT_FP4_E1M2 | 不支持舍入模式 | - | - |
136-| DT_FP4_E2M1X2 | DT_BF16 | 不支持舍入模式 | - | - |136+| DT_FP4_E2M1 | DT_BF16 | 不支持舍入模式 | - | - |
137-| DT_FP4_E1M2X2 | DT_BF16 | 不支持舍入模式 | - | - |137+| DT_FP4_E1M2 | DT_BF16 | 不支持舍入模式 | - | - |
138<!-- end id5 -->138<!-- end id5 -->
139 139 
140### 饱和模式设置说明140### 饱和模式设置说明
@@ -154,7 +154,7 @@ Ascend 950PR/Ascend 950DT使用不同的CastMode体系,内部实现基于 `Rou
154 154 
1552. **DT_INT4(S4)特殊说明**:打包类型,每字节包含2个元素,仅支持与DT_FP16的相互转换。1552. **DT_INT4(S4)特殊说明**:打包类型,每字节包含2个元素,仅支持与DT_FP16的相互转换。
156 156 
157-3. **DT_FP4_E2M1X2/DT_FP4_E1M2X2特殊说明**:157+3. **DT_FP4_E2M1/DT_FP4_E1M2特殊说明**:
158 <!-- npu="950" id6 -->158 <!-- npu="950" id6 -->
159 - Ascend 950PR/Ascend 950DT:支持159 - Ascend 950PR/Ascend 950DT:支持
160 <!-- end id6 -->160 <!-- end id6 -->
@@ -164,7 +164,9 @@ Ascend 950PR/Ascend 950DT使用不同的CastMode体系,内部实现基于 `Rou
164 <!-- npu="910b" id8 -->164 <!-- npu="910b" id8 -->
165 - Atlas A2 训练系列产品/Atlas A2 推理系列产品:不支持165 - Atlas A2 训练系列产品/Atlas A2 推理系列产品:不支持
166 <!-- end id8 -->166 <!-- end id8 -->
167- - 打包类型,每字节包含2个FP4元素(4-bit nibble)167+ - 使用逻辑4-bit类型,与scaled_mm一致;输入参数标注和cast目标类型均使用不带X2的枚举。
168+ - 转换前后逻辑Shape保持不变;每字节存储2个FP4元素,逻辑末轴长度必须为偶数。
169+ - 与Torch交互时,用torch.uint8承载打包数据,存储Shape为[m, n/2];参数标注为`pypto.Tensor([], pypto.DT_FP4_E1M2)`或`pypto.Tensor([], pypto.DT_FP4_E2M1)`后,kernel内看到的逻辑Shape为[m, n]。
168 - 仅支持与DT_BF16的相互转换170 - 仅支持与DT_BF16的相互转换
169 171 
1704. **DT_HF8 (hifloat8)特殊说明**:1724. **DT_HF8 (hifloat8)特殊说明**:
@@ -92,7 +92,7 @@ void CheckCastTypeSupport(DataType srcType, DataType dstType, const std::string&
92 std::unordered_map<DataType, std::unordered_set<DataType>> a5SupportedConversions = {92 std::unordered_map<DataType, std::unordered_set<DataType>> a5SupportedConversions = {
93 {DT_FP32, {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_INT64, DT_FP8E4M3, DT_FP8E5M2, DT_HF8}},93 {DT_FP32, {DT_FP16, DT_BF16, DT_INT16, DT_INT32, DT_INT64, DT_FP8E4M3, DT_FP8E5M2, DT_HF8}},
94 {DT_FP16, {DT_FP32, DT_INT32, DT_INT16, DT_INT8, DT_UINT8, DT_HF8}},94 {DT_FP16, {DT_FP32, DT_INT32, DT_INT16, DT_INT8, DT_UINT8, DT_HF8}},
95- {DT_BF16, {DT_FP32, DT_INT32, DT_FP16, DT_FP4_E1M2X2, DT_FP4_E2M1X2}},95+ {DT_BF16, {DT_FP32, DT_INT32, DT_FP16, DT_FP4_E1M2, DT_FP4_E2M1}},
96 {DT_UINT8, {DT_FP16, DT_UINT16}},96 {DT_UINT8, {DT_FP16, DT_UINT16}},
97 {DT_INT8, {DT_FP16, DT_INT16, DT_INT32}},97 {DT_INT8, {DT_FP16, DT_INT16, DT_INT32}},
98 {DT_INT16, {DT_UINT8, DT_FP16, DT_FP32, DT_UINT32, DT_INT32}},98 {DT_INT16, {DT_UINT8, DT_FP16, DT_FP32, DT_UINT32, DT_INT32}},
@@ -102,8 +102,8 @@ void CheckCastTypeSupport(DataType srcType, DataType dstType, const std::string&
102 {DT_FP8E4M3, {DT_FP32}},102 {DT_FP8E4M3, {DT_FP32}},
103 {DT_FP8E5M2, {DT_FP32}},103 {DT_FP8E5M2, {DT_FP32}},
104 {DT_HF8, {DT_FP32}},104 {DT_HF8, {DT_FP32}},
105- {DT_FP4_E1M2X2, {DT_BF16}},105+ {DT_FP4_E1M2, {DT_BF16}},
106- {DT_FP4_E2M1X2, {DT_BF16}}};106+ {DT_FP4_E2M1, {DT_BF16}}};
107 107 
108 if (a5SupportedConversions.count(srcType) == 0 || a5SupportedConversions[srcType].count(dstType) == 0) {108 if (a5SupportedConversions.count(srcType) == 0 || a5SupportedConversions[srcType].count(dstType) == 0) {
109 CHECK(VectorErrorCode::ERR_PARAM_DTYPE_UNSUPPORTED, false)109 CHECK(VectorErrorCode::ERR_PARAM_DTYPE_UNSUPPORTED, false)
@@ -86,6 +86,8 @@ def get_dtype_by_name(name: str, is_torch: bool = False, check: bool = True):
86 "complex64": [np.complex64, torch.complex64],86 "complex64": [np.complex64, torch.complex64],
87 "complex128": [np.complex128, torch.complex64],87 "complex128": [np.complex128, torch.complex64],
88 "bf16": [None, torch.bfloat16],88 "bf16": [None, torch.bfloat16],
89+ "fp4_e2m1": [np.uint8, torch.uint8],
90+ "fp4_e1m2": [np.uint8, torch.uint8],
89 "fp4_e2m1x2": [np.uint8, torch.uint8],91 "fp4_e2m1x2": [np.uint8, torch.uint8],
90 "fp4_e1m2x2": [np.uint8, torch.uint8],92 "fp4_e1m2x2": [np.uint8, torch.uint8],
91 "fp8e4m3": [93 "fp8e4m3": [
@@ -114,6 +114,10 @@ def _generate_golden_input_tensor(op: str, input_tensor: dict, config: dict, ind
114 dtype = get_dtype_by_name(input_tensor["dtype"])114 dtype = get_dtype_by_name(input_tensor["dtype"])
115 if op == "QuantMX":115 if op == "QuantMX":
116 return _generate_quantmx_input(input_tensor, config)116 return _generate_quantmx_input(input_tensor, config)
117+ if op == "Cast" and input_tensor["dtype"] in ("fp4_e2m1", "fp4_e1m2"):
118+ values = gen_uniform_data(input_tensor["shape"], min_value, max_value, np.float32)
119+ encode = _encode_e2m1_vectorized if input_tensor["dtype"] == "fp4_e2m1" else _encode_e1m2_cast
120+ return _pack_fp4_e2m1x2_low_first(encode(values))
117 if min_value != max_value:121 if min_value != max_value:
118 assert not isinstance(min_value, str) and not isinstance(max_value, str), (122 assert not isinstance(min_value, str) and not isinstance(max_value, str), (
119 "Data range must be number when the min and max are not same."123 "Data range must be number when the min and max are not same."
@@ -505,14 +509,14 @@ def gen_cast_op_golden(case_name: str, output: Path, case_index: int = None) ->
505 dst_dtype = params.get("dst_dtype", output_dtype)509 dst_dtype = params.get("dst_dtype", output_dtype)
506 src_dtype = config.get("input_tensors")[0].get("dtype")510 src_dtype = config.get("input_tensors")[0].get("dtype")
507 511 
508- if dst_dtype == "fp4_e2m1x2":512+ if dst_dtype == "fp4_e2m1":
509 return [_cast_fp4_e2m1x2_encode(inputs[0])]513 return [_cast_fp4_e2m1x2_encode(inputs[0])]
510- if dst_dtype == "fp4_e1m2x2":514+ if dst_dtype == "fp4_e1m2":
511 return [_cast_fp4_e1m2x2_encode(inputs[0])]515 return [_cast_fp4_e1m2x2_encode(inputs[0])]
512- if src_dtype == "fp4_e2m1x2":516+ if src_dtype == "fp4_e2m1":
513 x = _decode_fp4_e2m1x2(inputs[0])517 x = _decode_fp4_e2m1x2(inputs[0])
514 return [x.astype(get_dtype_by_name(dst_dtype))]518 return [x.astype(get_dtype_by_name(dst_dtype))]
515- if src_dtype == "fp4_e1m2x2":519+ if src_dtype == "fp4_e1m2":
516 x = _decode_fp4_e1m2x2(inputs[0])520 x = _decode_fp4_e1m2x2(inputs[0])
517 return [x.astype(get_dtype_by_name(dst_dtype))]521 return [x.astype(get_dtype_by_name(dst_dtype))]
518 522 
@@ -89,7 +89,8 @@ private:
89 89 
90 static bool IsFp4PackedDtype(DataType dtype)90 static bool IsFp4PackedDtype(DataType dtype)
91 {91 {
92- return dtype == DataType::DT_FP4_E2M1X2 || dtype == DataType::DT_FP4_E1M2X2;92+ return dtype == DataType::DT_FP4_E2M1 || dtype == DataType::DT_FP4_E1M2 || dtype == DataType::DT_FP4_E2M1X2 ||
93+ dtype == DataType::DT_FP4_E1M2X2;
93 }94 }
94 95 
95 static size_t GetTensorStorageElementCount(const Tensor& tensor)96 static size_t GetTensorStorageElementCount(const Tensor& tensor)
@@ -245,6 +246,8 @@ private:
245 case DataType::DT_FP8E8M0:246 case DataType::DT_FP8E8M0:
246 case DataType::DT_FP4_E2M1X2:247 case DataType::DT_FP4_E2M1X2:
247 case DataType::DT_FP4_E1M2X2:248 case DataType::DT_FP4_E1M2X2:
249+ case DataType::DT_FP4_E2M1:
250+ case DataType::DT_FP4_E1M2:
248 readGoldenCmp<uint8_t>(tensor, testCase.goldenPaths[i], i, 0);251 readGoldenCmp<uint8_t>(tensor, testCase.goldenPaths[i], i, 0);
249 return true;252 return true;
250 default:253 default:
@@ -12,9 +12,9 @@ Cast_test_9,"[128, 128]",int8,ND,"[1, 1]","[128, 128]",fp16,ND,"[128, 128]","[64
12Cast_test_10,"[16, 384]",int32,ND,"[-1, 1]","[16, 384]",fp32,ND,"[7, 169]","[215, 48]",012Cast_test_10,"[16, 384]",int32,ND,"[-1, 1]","[16, 384]",fp32,ND,"[7, 169]","[215, 48]",0
13Cast_test_11,"[16, 1, 64]",bf16,ND,"[-1, 1]","[16, 1, 64]",fp32,ND,"[5, 2, 18]","[310, 6, 16]",013Cast_test_11,"[16, 1, 64]",bf16,ND,"[-1, 1]","[16, 1, 64]",fp32,ND,"[5, 2, 18]","[310, 6, 16]",0
14Cast_test_12,"[32, 127, 128, 64]",fp16,ND,"[-100, 100]","[32, 127, 128, 64]",fp32,ND,"[2, 42, 44, 29]","[1, 28, 8, 112]",014Cast_test_12,"[32, 127, 128, 64]",fp16,ND,"[-100, 100]","[32, 127, 128, 64]",fp32,ND,"[2, 42, 44, 29]","[1, 28, 8, 112]",0
15-Cast_test_13,"[64, 128]",bf16,ND,"[-6, 6]","[64, 128]",fp4_e2m1x2,ND,"[64, 128]","[64, 128]",015+Cast_test_13,"[64, 128]",bf16,ND,"[-6, 6]","[64, 128]",fp4_e2m1,ND,"[64, 128]","[64, 128]",0
16-Cast_test_14,"[64, 128]",bf16,ND,"[-28, 28]","[64, 128]",fp4_e1m2x2,ND,"[64, 128]","[64, 128]",016+Cast_test_14,"[64, 128]",bf16,ND,"[-28, 28]","[64, 128]",fp4_e1m2,ND,"[64, 128]","[64, 128]",0
17-Cast_test_15,"[64, 128]",fp4_e2m1x2,ND,"[-6, 6]","[64, 128]",bf16,ND,"[64, 128]","[64, 128]",017+Cast_test_15,"[64, 128]",fp4_e2m1,ND,"[-6, 6]","[64, 128]",bf16,ND,"[64, 128]","[64, 128]",0
18-Cast_test_16,"[64, 128]",fp4_e1m2x2,ND,"[-28, 28]","[64, 128]",bf16,ND,"[64, 128]","[64, 128]",018+Cast_test_16,"[64, 128]",fp4_e1m2,ND,"[-28, 28]","[64, 128]",bf16,ND,"[64, 128]","[64, 128]",0
19Cast_test_17,"[64, 64]",int32,ND,"[0, 65535]","[64, 64]",uint16,ND,"[64, 64]","[64, 64]",019Cast_test_17,"[64, 64]",int32,ND,"[0, 65535]","[64, 64]",uint16,ND,"[64, 64]","[64, 64]",0
20Cast_test_18,"[64, 64]",uint32,ND,"[0, 65535]","[64, 64]",uint16,ND,"[64, 64]","[64, 64]",020Cast_test_18,"[64, 64]",uint32,ND,"[0, 65535]","[64, 64]",uint16,ND,"[64, 64]","[64, 64]",0
@@ -187,6 +187,11 @@ def cast(
187 y = pypto.cast(x, pypto.DT_INT8, satmode=pypto.SaturationMode.ON)187 y = pypto.cast(x, pypto.DT_INT8, satmode=pypto.SaturationMode.ON)
188 # Values will be clamped to [-128, 127] range188 # Values will be clamped to [-128, 127] range
189 189 
190+ On A5, BF16 converts to/from DT_FP4_E1M2 and DT_FP4_E2M1. FP4 tensors
191+ keep the logical shape (one 4-bit value per element), as in scaled_mm.
192+ At the Torch boundary, annotate the tensor with the logical FP4 dtype
193+ and pass packed uint8 storage with half as many columns.
194+ 
190 Input x: [2.0, 3.0] x.dtype: pypto.DT_FP32195 Input x: [2.0, 3.0] x.dtype: pypto.DT_FP32
191 Output y: [2.0, 3.0] y.dtype: pypto.DT_FP16196 Output y: [2.0, 3.0] y.dtype: pypto.DT_FP16
192 """197 """