TCVT
Instruction Diagram
Introduction
Performs element-wise type conversion with a specified rounding mode.
Mathematical Semantics
For each element (i, j) in the valid region:
$$ \mathrm{dst}_{i,j} = \mathrm{cast}_{\mathrm{rmode}}\!\left(\mathrm{src}_{i,j}\right) $$
Where rmode is the rounding strategy (see pto::RoundMode).
C++ Built-in APIs
To use this API, include <pto/pto-inst.hpp>. The overloads are defined in include/pto/common/pto_instr.hpp, and RoundMode and SaturationMode are defined in include/pto/common/type.hpp.
template <typename TileDataD, typename TileDataS, typename... WaitEvents>
PTO_INST RecordEvent TCVT(TileDataD &dst, TileDataS &src, RoundMode mode, SaturationMode satMode, WaitEvents &... events);
template <typename TileDataD, typename TileDataS, typename... WaitEvents>
PTO_INST RecordEvent TCVT(TileDataD &dst, TileDataS &src, RoundMode mode, WaitEvents &... events);
template <typename TileDataD, typename TileDataS, typename TmpTileData, typename... WaitEvents>
PTO_INST RecordEvent TCVT(TileDataD &dst, TileDataS &src, TmpTileData &tmp, RoundMode mode,
SaturationMode satMode, WaitEvents &... events);
template <typename TileDataD, typename TileDataS, typename TmpTileData, typename... WaitEvents>
PTO_INST RecordEvent TCVT(TileDataD &dst, TileDataS &src, TmpTileData &tmp, RoundMode mode,
WaitEvents &... events);Constraints
dstandsrcmust be compatible in terms of shape/valid region, as required by the implementation.- For a given
RoundMode, the conversion(src element type) -> (dst element type)must be supported by the destination. - Implementation description:
- One form accepts an explicit
SaturationMode, and the specified saturation behavior is passed directly to the implementation. - The other form does not explicitly provide a
SaturationMode; in this case, the implementation selects the default saturation behavior defined by the destination for the specific type pair. - CPU_SIM implements all four C++ overloads listed above; its default saturation mode is
SaturationMode::OFF. - CPU_SIM's FP32-to-FP8 E4M3/E5M2 conversion implements
CAST_RINT(round-to-nearest, ties to even),CAST_ROUND(Round-to-nearest, ties away from zero),CAST_FLOOR,CAST_CEIL, andCAST_TRUNC. Consistent with the A5 dispatch,CAST_NONEandCAST_ODD, which is not supported by FP8, fall back toCAST_RINT. Rounding preserves signed zero and does not depend on the host floating-point rounding mode. - For the CPU_SIM FP8 conversions described above, the saturation mode clamps finite-value overflow and input infinity to the maximum finite value with the original sign
(E4M3: 448; E5M2: 57344). An input NaN remains NaN, but its payload is not preserved. In non-saturating mode, an E4M3 rounded result that exceeds the finite range or an input infinity produces NaN. For finite E5M2 inputs, round-to-nearest (
CAST_RINT/CAST_ROUND) produces infinity with the original sign when the absolute value is >= 61440; rounding away from zero produces infinity when the absolute value is > 57344. Rounding toward zero takes the maximum finite value with the original sign for inputs whose absolute value is > 57344; an input infinity remains infinity. Here, the away-from-zero direction refers toCAST_CEILfor positive numbers orCAST_FLOORfor negative numbers, and the toward-zero direction refers toCAST_TRUNC,CAST_FLOORfor positive numbers, orCAST_CEILfor negative numbers. E4M3's0x7f/0xffdecode to NaN, and E5M2 distinguishes infinity from NaN. The E8M0 conversion and the single-argument MXType encoding constructor remain unchanged.
- One form accepts an explicit
- Temporary tile:
- CPU_SIM: The two overloads with
tmpproduce the same results as their corresponding overloads withouttmp. CPU_SIM acceptstmpto maintain source compatibility, but does not calltmp.data()or access its storage. Cross-backend kernels must still declare and allocatetmpas required by the target NPU backend. - Atlas A2/A3 training products/Atlas A2/A3 inference products: When
SaturationMode::OFFis used forfloat -> int16,half -> int16, orhalf -> int8, the PyTorch-compatible non-saturating narrowing path uses a temporary tile; other conversions do not require tmp space. - Ascend 950PR/Ascend 950DT: The two overloads with
tmpaccept but do not use the temporary tile; the conversion behavior and default saturation behavior are consistent with the corresponding overloads withouttmp. - The Atlas A2/A3 implementation converts
tmptoint32_t *for use; therefore, the tmp tile size should be planned in bytes rather than interpreted according to the type ofTmpTileData::DType. - The following formulas give the minimum allocation size rounded up to the 32-byte vector block granularity used by the implementation. If
C = 0, no conversion requiring tmp is initiated, and the required tmp size is0. - Common parameters:
R = dst.GetValidRow().C = dst.GetValidCol().SS = TileDataS::RowStride, in units of source elements.REPEAT_MAX = 255,REPEAT_BYTE = 256,BLOCK_BYTE_SIZE = 32.
float -> int16, non-saturating (SaturationMode::OFF):- The temporary result is the
int32_ttile produced by the first-stepfloat -> int32conversion. - Since the
floatsource row is guaranteed to be 32-byte aligned by the tile constraint,SS / 8is the source repeat stride in units of 32-byte blocks. - In the aligned main region, one call processes one row, processing at most
REPEAT_MAXrepeats, with each repeat containing64elements:$$ \text{tmpHeadBytes} = 4 \times 64 \times \min\left(\left\lfloor\frac{C}{64}\right\rfloor, 255\right) $$ - In the tail region, one call processes at most
REPEAT_MAXrows and uses the source row stride. Because the vector repeat stride is in units of blocks, the spatial extent is computed in 32-byte blocks: $$ \text{tmpTailBytes} = \begin{cases} 32 \times \left((\min(R, 255) - 1) \times \frac{SS}{8} + \left\lceil\frac{C \bmod 64}{8}\right\rceil\right), & C \bmod 64 > 0 \ 0, & C \bmod 64 = 0 \end{cases} $$ - The minimum tmp size required by this path is:
$$ \text{tmpFloatToInt16Bytes} = \max(\text{tmpHeadBytes}, \text{tmpTailBytes}) $$
- For the main region, a compact upper bound for a complete repeat is
REPEAT_MAX * REPEAT_BYTE = 65280bytes; however, whenSSis large, the tail is written using the source row stride, and the required space may be larger.
- The temporary result is the
half -> int16, non-saturating (SaturationMode::OFF):- The tmp-backed path on Atlas A2/A3 first performs a saturating
half -> int32conversion, followed by a non-saturatingint32 -> int16narrowing. Therefore, finite values outside theint16range wrap around to the low 16 bits; the results of-inf,+inf, andNaNare0,-1, and0, respectively. - The implementation processes row by row, splitting each row into sub-blocks of no more than
64elements, and reuses the same temporary buffer between sub-blocks. ForC > 0, let:$$ H = \min(C, 64) $$ - The minimum tmp size required by this path is:
$$ \text{tmpHalfToInt16Bytes} = 32 \times \left\lceil\frac{H}{8}\right\rceil $$
- For any non-empty tile, the shape-independent upper bound of this path is
256bytes.
- The tmp-backed path on Atlas A2/A3 first performs a saturating
half -> int8, non-saturating (SaturationMode::OFF):- The implementation similarly processes sub-blocks of no more than
64elements and reuses the same 256-byte temporary region. In the first step, at most64int32_tvalues are written to bytes[0, 255]; after theint32 -> int16narrowing is complete, bytes[0, 127]holdint16_tvalues, and bytes[128, 255]are reused as scratch. tempMaskBuf = tempAndBuf + 64advances by64 * sizeof(int16_t) = 128bytes, so it points to the upper half of the same 256-byte temporary region, and no additional 256 bytes need to be allocated.- The minimum tmp size required by this path is:
$$ \text{tmpHalfToInt8Bytes} = \max\left(32 \times \left\lceil\frac{H}{8}\right\rceil,\ 128 + 32 \times \left\lceil\frac{H}{16}\right\rceil\right) $$
- For any non-empty tile, the shape-independent upper bound of this path is
256bytes.
- The implementation similarly processes sub-blocks of no more than
- Overall minimum value covering all tmp-backed TCVT conversions:
- Since
tmpHalfToInt8Bytes >= tmpHalfToInt16Bytes, for the same shape, the minimum tmp size that covers all TCVT conversion paths that use tmp is:$$ \text{tmpSizeAllBytes} = \max(\text{tmpFloatToInt16Bytes},\ \text{tmpHalfToInt8Bytes}) $$ - If the tile is non-empty and the half path can use a shape-independent compact upper bound, it can also be written as:
$$ \text{tmpSizeAllBytes} = \max(\text{tmpFloatToInt16Bytes},\ 256) $$
- Since
- On NPU backends, for conversions that do not require the PyTorch-compatible tmp-backed path, or when the native saturation behavior already meets the requirements,
you can continue to use the overload without
tmp.
- CPU_SIM: The two overloads with
Supported Conversions (Atlas A2/A3 Training Products/Atlas A2/A3 Inference Products and Ascend 950PR/Ascend 950DT Side-by-Side Comparison)
| Source Type | Atlas A2/A3 Training Products/Atlas A2/A3 Inference Products Target Type | Ascend 950PR/Ascend 950DT Target Type | Difference |
|---|---|---|---|
| FP32 | FP16, FP32 (rounding only), BF16, I16, I32, I64 | FP32, FP16, BF16, I16, I32, I64, FP8_E4M3, FP8_E5M2, H8 | Ascend 950PR/Ascend 950DT newly adds FP8/H8 targets |
| FP16 | FP32, I32, I16, I8, U8, S4 (int4b_t) | FP32, I32, I16, I8, U8, H8 | Atlas A2/A3 training products/Atlas A2/A3 inference products have S4; Ascend 950PR/Ascend 950DT has H8 |
| BF16 | FP32, I32 | FP32, I32, FP16, FP4_E1M2X2, FP4_E2M1X2 | Ascend 950PR/Ascend 950DT newly adds FP16/FP4 targets |
| I16 | FP16, FP32 | U8, FP16, FP32, U32, I32 | Ascend 950PR/Ascend 950DT newly adds U8/U32/I32 |
| I32 | FP32, I16, I64, FP16 (deq path) | FP32, I16, U16, I64, U8 | Atlas A2/A3 training products/Atlas A2/A3 inference products support I32 -> FP16 (half, deq); Ascend 950PR/Ascend 950DT does not |
| I64 | FP32, I32 | FP32, I32 | Same |
| U8 | FP16 | FP16, U16 | Ascend 950PR/Ascend 950DT newly adds U16 |
| I8 | FP16 | FP16, I16, I32 | Ascend 950PR/Ascend 950DT newly adds I16/I32 |
| S4 (int4b_t) | FP16 | N/A | Exclusive to Atlas A2/A3 training products/Atlas A2/A3 inference products |
| U32 | N/A | U8, U16, I16 | Source type exclusive to Ascend 950PR/Ascend 950DT |
| FP8_E4M3 | N/A | FP32 | Source type exclusive to Ascend 950PR/Ascend 950DT |
| FP8_E5M2 | N/A | FP32 | Source type exclusive to Ascend 950PR/Ascend 950DT |
| H8 | N/A | FP32 | Source type exclusive to Ascend 950PR/Ascend 950DT |
| FP4_E1M2X2 | N/A | BF16 | Source type exclusive to Ascend 950PR/Ascend 950DT |
| FP4_E2M1X2 | N/A | BF16 | Source type exclusive to Ascend 950PR/Ascend 950DT |
Note:
- Key difference: Atlas A2/A3 training products/Atlas A2/A3 inference products support I32 -> FP16 (half, deq path), while Ascend 950PR/Ascend 950DT does not support I32 -> FP16.
- FP16 -> FP8_E4M3 and FP16 -> FP8_E5M2 are not supported on Ascend 950PR/Ascend 950DT.
Examples
Automatic
#include <pto/pto-inst.hpp>
using namespace pto;
void example_auto() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, half, 16, 16>;
SrcT src;
DstT dst;
TCVT(dst, src, RoundMode::CAST_RINT);
}Manual
#include <pto/pto-inst.hpp>
using namespace pto;
void example_manual() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, half, 16, 16>;
SrcT src;
DstT dst;
TASSIGN(src, 0x1000);
TASSIGN(dst, 0x2000);
TCVT(dst, src, RoundMode::CAST_RINT);
}