Cast

Cast - 25

版本

  • 名称: Cast (GitHub)

  • : main

  • since_version: 25

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的操作符已在版本 25 及之后可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

更详细地说,如果目标类型不是float 8类型,数值类型之间的转换应遵循以下规则。

  • 从浮点数转换为

    • 浮点数:如果OOR(超出范围),则为+/-无穷大。

    • 定点数:如果OOR,则为未定义。

    • 布尔值:+/- 0.0 为 False;其他都为 True。

  • 从定点数转换为

    • 浮点数:如果OOR,则为+/-无穷大。(在无符号整数的情况下为+无穷大)

    • 定点数:当OOR时,丢弃高位并重新解释(对于有符号类型,根据二进制补码表示)。例如,200 (int16) -> -56 (int8)。

    • 布尔值:零为 False;非零为 True。

  • 从布尔值转换为

    • 浮点数:{1.0, 0.0}

    • 定点数:{1, 0}

    • 布尔值:无变化。

引入了Float 8类型(E4M3FN、E4M3FNUZ、E5M2、E5M2FNUZ)以加速深度模型的训练。默认情况下,浮点数x的转换遵循以下规则。[x]表示四舍五入到目标尾数宽度的值。

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

Inf

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

-Inf

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

[x] > FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

[x] < -FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

else

RNE

RNE

RNE

RNE

如果参数'saturate'设置为False,则行为会改变。规则变为

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

-NaN

-NaN

NaN

-NaN

NaN

Inf

NaN

NaN

Inf

NaN

-Inf

-NaN

NaN

-Inf

NaN

[x] > FLT_MAX

NaN

NaN

Inf

NaN

[x] < -FLT_MAX

NaN

NaN

-Inf

NaN

else

RNE

RNE

RNE

RNE

引入FLOAT8E8M0类型是为了支持微缩放 (MX) 格式。转换为FLOAT8E8M0时,可以使用round_modesaturate属性指定舍入行为。当前的CUDA行为是向上舍入并饱和。将负值转换为FLOAT8E8M0会产生未定义的行为。下表描述了在两种最常见情况下,特殊值转换为FLOAT8E8M0的转换行为。

x

饱和 + 向上取整

非饱和 + 最近取整

0

0

NaN

-0

未指定

未指定

NaN

NaN

NaN

Inf

E8M0_MAX

NaN

x > E8M0_MAX

E8M0_MAX

NaN

x < E8M0_MIN

E8M0_MIN

NaN

x < 0

未指定

未指定

属性

  • round_mode - STRING (默认值为 'up')

    转换为float8e8m0的舍入模式。它仅适用于转换为float8e8m0,默认值为upup:向远离零的方向取最近值舍入,down:向零的方向取最近值舍入,nearest:取最近值舍入,平局向上舍入。

  • saturate - 整型 (默认为 '1')

    此参数定义当输入值超出目标类型范围时转换的行为。它仅适用于float 8转换(float8e4m3fn、float8e4m3fnuz、float8e5m2、float8e5m2fnuz、float8e8m0)。默认值为true。所有情况都已在操作符描述中插入的表格中详细描述。

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(float8e8m0), tensor(int16), tensor(int2), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint2), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(float8e8m0), tensor(int16), tensor(int2), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint2), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 24

版本

  • 名称: Cast (GitHub)

  • : main

  • 起始版本24

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的操作符已可用于版本 24 及以上

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

更详细地说,如果目标类型不是float 8类型,数值类型之间的转换应遵循以下规则。

  • 从浮点数转换为

    • 浮点数:如果OOR(超出范围),则为+/-无穷大。

    • 定点数:如果OOR,则为未定义。

    • 布尔值:+/- 0.0 为 False;其他都为 True。

  • 从定点数转换为

    • 浮点数:如果OOR,则为+/-无穷大。(在无符号整数的情况下为+无穷大)

    • 定点数:当OOR时,丢弃高位并重新解释(对于有符号类型,根据二进制补码表示)。例如,200 (int16) -> -56 (int8)。

    • 布尔值:零为 False;非零为 True。

  • 从布尔值转换为

    • 浮点数:{1.0, 0.0}

    • 定点数:{1, 0}

    • 布尔值:无变化。

引入了Float 8类型(E4M3FN、E4M3FNUZ、E5M2、E5M2FNUZ)以加速深度模型的训练。默认情况下,浮点数x的转换遵循以下规则。[x]表示四舍五入到目标尾数宽度的值。

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

Inf

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

-Inf

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

[x] > FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

[x] < -FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

else

RNE

RNE

RNE

RNE

如果参数'saturate'设置为False,则行为会改变。规则变为

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

-NaN

-NaN

NaN

-NaN

NaN

Inf

NaN

NaN

Inf

NaN

-Inf

-NaN

NaN

-Inf

NaN

[x] > FLT_MAX

NaN

NaN

Inf

NaN

[x] < -FLT_MAX

NaN

NaN

-Inf

NaN

else

RNE

RNE

RNE

RNE

引入FLOAT8E8M0类型是为了支持微缩放 (MX) 格式。转换为FLOAT8E8M0时,可以使用round_modesaturate属性指定舍入行为。当前的CUDA行为是向上舍入并饱和。将负值转换为FLOAT8E8M0会产生未定义的行为。下表描述了在两种最常见情况下,特殊值转换为FLOAT8E8M0的转换行为。

x

饱和 + 向上取整

非饱和 + 最近取整

0

0

NaN

-0

未指定

未指定

NaN

NaN

NaN

Inf

E8M0_MAX

NaN

x > E8M0_MAX

E8M0_MAX

NaN

x < E8M0_MIN

E8M0_MIN

NaN

x < 0

未指定

未指定

属性

  • round_mode - STRING (默认值为 'up')

    转换为float8e8m0的舍入模式。它仅适用于转换为float8e8m0,默认值为upup:向远离零的方向取最近值舍入,down:向零的方向取最近值舍入,nearest:取最近值舍入,平局向上舍入。

  • saturate - 整型 (默认为 '1')

    此参数定义当输入值超出目标类型范围时转换的行为。它仅适用于float 8转换(float8e4m3fn、float8e4m3fnuz、float8e5m2、float8e5m2fnuz、float8e8m0)。默认值为true。所有情况都已在操作符描述中插入的表格中详细描述。

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(float8e8m0), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(float8e8m0), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 23

版本

  • 名称: Cast (GitHub)

  • : main

  • 起始版本23

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的操作符已可用于版本 23 及以上

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

更详细地说,如果目标类型不是float 8类型,数值类型之间的转换应遵循以下规则。

  • 从浮点数转换为

    • 浮点数:如果OOR(超出范围),则为+/-无穷大。

    • 定点数:如果OOR,则为未定义。

    • 布尔值:+/- 0.0 为 False;其他都为 True。

  • 从定点数转换为

    • 浮点数:如果OOR,则为+/-无穷大。(在无符号整数的情况下为+无穷大)

    • 定点数:当OOR时,丢弃高位并重新解释(对于有符号类型,根据二进制补码表示)。例如,200 (int16) -> -56 (int8)。

    • 布尔值:零为 False;非零为 True。

  • 从布尔值转换为

    • 浮点数:{1.0, 0.0}

    • 定点数:{1, 0}

    • 布尔值:无变化。

引入浮点8类型是为了加速深度模型的训练。默认情况下,浮点数x的转换遵循以下规则。[x]表示四舍五入到目标尾数宽度的值。

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

Inf

FLT_MAX

NaN

FLT_MAX

NaN

-Inf

-FLT_MAX

NaN

-FLT_MAX

NaN

[x] > FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

[x] < -FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

else

RNE

RNE

RNE

RNE

如果参数'saturate'设置为False,则行为会改变。规则变为

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

-NaN

-NaN

NaN

-NaN

NaN

Inf

NaN

NaN

Inf

NaN

-Inf

-NaN

NaN

-Inf

NaN

[x] > FLT_MAX

NaN

NaN

Inf

NaN

[x] < -FLT_MAX

NaN

NaN

-Inf

NaN

else

RNE

RNE

RNE

RNE

属性

  • saturate - 整型 (默认为 '1')

    此参数定义当输入值超出目标类型范围时转换的行为。它仅适用于float 8转换(float8e4m3fn、float8e4m3fnuz、float8e5m2、float8e5m2fnuz)。默认值为true。所有情况都已在操作符描述中插入的两个表格中详细描述。

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 21

版本

  • 名称: Cast (GitHub)

  • : main

  • since_version: 21

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的操作符自 版本 21 起可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

更详细地说,如果目标类型不是float 8类型,数值类型之间的转换应遵循以下规则。

  • 从浮点数转换为

    • 浮点数:如果OOR(超出范围),则为+/-无穷大。

    • 定点数:如果OOR,则为未定义。

    • 布尔值:+/- 0.0 为 False;其他都为 True。

  • 从定点数转换为

    • 浮点数:如果OOR,则为+/-无穷大。(在无符号整数的情况下为+无穷大)

    • 定点数:当OOR时,丢弃高位并重新解释(对于有符号类型,根据二进制补码表示)。例如,200 (int16) -> -56 (int8)。

    • 布尔值:零为 False;非零为 True。

  • 从布尔值转换为

    • 浮点数:{1.0, 0.0}

    • 定点数:{1, 0}

    • 布尔值:无变化。

引入浮点8类型是为了加速深度模型的训练。默认情况下,浮点数x的转换遵循以下规则。[x]表示四舍五入到目标尾数宽度的值。

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

Inf

FLT_MAX

NaN

FLT_MAX

NaN

-Inf

-FLT_MAX

NaN

-FLT_MAX

NaN

[x] > FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

[x] < -FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

else

RNE

RNE

RNE

RNE

如果参数'saturate'设置为False,则行为会改变。规则变为

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

-NaN

-NaN

NaN

-NaN

NaN

Inf

NaN

NaN

Inf

NaN

-Inf

-NaN

NaN

-Inf

NaN

[x] > FLT_MAX

NaN

NaN

Inf

NaN

[x] < -FLT_MAX

NaN

NaN

-Inf

NaN

else

RNE

RNE

RNE

RNE

属性

  • saturate - 整型 (默认为 '1')

    此参数定义当输入值超出目标类型范围时转换的行为。它仅适用于float 8转换(float8e4m3fn、float8e4m3fnuz、float8e5m2、float8e5m2fnuz)。默认值为true。所有情况都已在操作符描述中插入的两个表格中详细描述。

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 19

版本

  • 名称: Cast (GitHub)

  • : main

  • since_version: 19

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的操作符自 版本 19 起可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

更详细地说,如果目标类型不是float 8类型,数值类型之间的转换应遵循以下规则。

  • 从浮点数转换为

    • 浮点数:如果OOR(超出范围),则为+/-无穷大。

    • 定点数:如果OOR,则为未定义。

    • 布尔值:+/- 0.0 为 False;其他都为 True。

  • 从定点数转换为

    • 浮点数:如果OOR,则为+/-无穷大。(在无符号整数的情况下为+无穷大)

    • 定点数:当OOR时,丢弃高位并重新解释(对于有符号类型,根据二进制补码表示)。例如,200 (int16) -> -56 (int8)。

    • 布尔值:零为 False;非零为 True。

  • 从布尔值转换为

    • 浮点数:{1.0, 0.0}

    • 定点数:{1, 0}

    • 布尔值:无变化。

引入浮点8类型是为了加速深度模型的训练。默认情况下,浮点数x的转换遵循以下规则。[x]表示四舍五入到目标尾数宽度的值。

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

Inf

FLT_MAX

NaN

FLT_MAX

NaN

-Inf

-FLT_MAX

NaN

-FLT_MAX

NaN

[x] > FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

FLT_MAX

[x] < -FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

-FLT_MAX

else

RNE

RNE

RNE

RNE

如果参数'saturate'设置为False,则行为会改变。规则变为

x

E4M3FN

E4M3FNUZ

E5M2

E5M2FNUZ

0

0

0

0

0

-0

-0

0

-0

0

NaN

NaN

NaN

NaN

NaN

-NaN

-NaN

NaN

-NaN

NaN

Inf

NaN

NaN

Inf

NaN

-Inf

-NaN

NaN

-Inf

NaN

[x] > FLT_MAX

NaN

NaN

Inf

NaN

[x] < -FLT_MAX

NaN

NaN

-Inf

NaN

else

RNE

RNE

RNE

RNE

属性

  • saturate - 整型 (默认为 '1')

    此参数定义当输入值超出目标类型范围时转换的行为。它仅适用于float 8转换(float8e4m3fn、float8e4m3fnuz、float8e5m2、float8e5m2fnuz)。默认值为true。所有情况都已在操作符描述中插入的两个表格中详细描述。

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 13

版本

  • 名称: Cast (GitHub)

  • : main

  • 起始版本: 13

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的运算符自 版本 13 起可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

更详细地说,数值类型之间的转换应遵循这些规则

  • 从浮点数转换为

    • 浮点数:如果OOR(超出范围),则为+/-无穷大。

    • 定点数:如果OOR,则为未定义。

    • 布尔值:+/- 0.0 为 False;其他都为 True。

  • 从定点数转换为

    • 浮点数:如果OOR,则为+/-无穷大。(在无符号整数的情况下为+无穷大)

    • 定点数:当OOR时,丢弃高位并重新解释(对于有符号类型,根据二进制补码表示)。例如,200 (int16) -> -56 (int8)。

    • 布尔值:零为 False;非零为 True。

  • 从布尔值转换为

    • 浮点数:{1.0, 0.0}

    • 定点数:{1, 0}

    • 布尔值:无变化。

属性

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 9

版本

  • 名称: Cast (GitHub)

  • : main

  • 起始版本: 9

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的运算符自 版本 9 起可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。

支持将平面(例如,“3.14”和“1000”)和科学数字表示(例如,“1e-5”和“1E8”)的字符串张量转换为浮点类型。例如,将字符串“100.5”转换为整数可能会得到100。有一些字符串字面量保留给特殊的浮点值;“+INF”(和“INF”)、“-INF”和“NaN”分别表示正无穷大、负无穷大和非数字。任何与“+INF”大小写不敏感地完全匹配的字符串都将被映射到正无穷大。同样,此大小写不敏感规则适用于“INF”和“NaN”。将数字张量转换为字符串张量时,将使用平面浮点表示(例如“314.15926”)。转换非数字字面字符串(例如“Hello World!”)是未定义行为。将表示浮点算术值(例如“2.718”)的字符串转换为INT是未定义行为。

数值类型之间的转换总是允许的。用户必须注意由于两种类型之间的范围差异导致的精度损失和值变化。例如,64位浮点数3.1415926459可能会四舍五入为32位浮点数3.141592。同样,将整数36转换为布尔值可能会产生1,因为我们截断了无法存储在目标类型中的位。

属性

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从复杂类型转换。

  • T2 在 ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为复杂类型。

Cast - 6

版本

  • 名称: Cast (GitHub)

  • : main

  • 起始版本: 6

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: True

此版本的操作符自版本 6 起可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。注意:尚不支持转换为和从字符串转换。

属性

  • to - INT (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从字符串和复杂类型转换。

  • T2 在 ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为字符串和复杂类型。

Cast - 1

版本

  • 名称: Cast (GitHub)

  • : main

  • 起始版本: 1

  • 函数: False

  • 支持级别: SupportType.COMMON

  • 形状推断: False

此版本的运算符自 版本 1 起可用。

摘要

该操作符将给定输入张量的元素转换为“to”参数指定的数据类型,并返回一个具有相同大小且已转换类型的输出张量。“to”参数必须是TensorProto消息中“DataType”枚举字段中指定的数据类型之一。注意:尚不支持转换为和从字符串转换。

属性

  • to - STRING (必需)

    输入张量元素要转换的数据类型。严格来说,必须是TensorProto中DataType枚举中的一种类型。

输入

  • input (异构) - T1

    要转换的输入张量。

输出

  • output (异构) - T2

    输出张量,其形状与输入相同,类型由“to”参数指定。

类型约束

  • T1 在 ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输入类型。不支持从字符串和复杂类型转换。

  • T2 在 ( tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8) )

    限制输出类型。不支持转换为字符串和复杂类型。