asc2.cast

asc2.cast(input: LocalTensor, dtype: DataType, round_mode: RoundMode = RoundMode.Default) LocalTensor
asc2.cast(input: RuntimeNumeric, dtype: DataType) PlainValue

Cast a tensor or scalar value to a different data type.

Creates a new tensor (or scalar) with the same shape but converted to the specified data type. If the input already has the target dtype, returns the input unchanged.

The supported data types are: int8, int16, int32, int64, float16, bfloat16, float32.

Parameters:
  • input – The input tensor or scalar value to cast

  • dtype – The target data type

  • round_mode – The rounding mode for precision conversion (if input is a tensor). Supported values: RoundMode.Default (automatically infer rounding mode based on source and target types), RoundMode.NoRound (no rounding, truncate toward zero), RoundMode.Rint (round to nearest, ties to even), RoundMode.Floor (round toward negative infinity), RoundMode.Ceil (round toward positive infinity), RoundMode.Round (round half away from zero), RoundMode.Trunc (truncate toward zero), RoundMode.Odd (round to nearest odd).

Returns:

A new tensor with the specified dtype (if input is a LocalTensor) PlainValue: A scalar value with the specified dtype (if input is a scalar)

Return type:

LocalTensor

Raises:

TypeError – If input is not a LocalTensor or numeric value, or dtype is not a DataType

Note

This function is also available as the .to() method on tensors: result.to(dtype).

Examples

Cast a tensor from float32 to float16:

input = asc2.copy_in(x_gm, [0], [128])
result_fp16 = asc2.cast(input, asc2.float16)

Cast with explicit rounding mode:

input = asc2.copy_in(x_gm, [0], [128])
result_int32 = asc2.cast(input, asc2.int32, round_mode=asc2.RoundMode.Floor)

Cast using the .to() method (equivalent):

input = asc2.copy_in(x_gm, [0], [128])
result_fp16 = input.to(asc2.float16)

Cast a scalar value:

scalar_fp16 = asc2.cast(3.14, asc2.float16)

Chain multiple casts for quantization:

acc = asc2.zeros_acc([64, 128], dtype=asc2.float32)
# ... accumulate matmul results ...
result_fp16 = acc.to(asc2.float16)
asc2.copy_out(result_fp16, out_gm, [0, 0])