ISARef Multi-ISA & Silicon Reference
45,419 ISA items · 105,907 timings · 913 microarchs · 15,555 chips · 105 sources
Arch:
Tier:

Tensix: Format Conversion (Packers)

T1 tenstorrent guide guide
Tenstorrent WormholeB0 Tensix — Format Conversion (Packers)
SourceTenstorrent Official Tensix ISA Documentation (Wormhole & Blackhole) (tenstorrent_tt_isa)
Reliability TierT1 — Official Specification
ISA / Silicon ScopeTenstorrent WormholeB0
Tenstorrent Wormhole B0 & Blackhole A0
License & VersionApache-2.0
main
Upstreamhttps://github.com/tenstorrent/tt-isa-documentation
ExtensionsTensix, WormholeB0

Summary

Two data format conversions happen during the pipeline of each packer: an early conversion just after reading datums from Dst, and a late conversion just before writing datums to L1. To avoid double rounding, software should ensure that at least one conversion is an identity conversion, or ensure that both conversions are truncating. In general, the early format conversion should be used to make types narrower using…

Reference Notes

# Format Conversion

Two data format conversions happen during the pipeline of each packer: an early conversion just after reading datums from `Dst`, and a late conversion just before writing datums to `L1`. To avoid double rounding, software should ensure that at least one conversion is an identity conversion, or ensure that both conversions are truncating. In general, the early format conversion should be used to make types narrower using rounding, and the late format conversion should be used to make types wider. To make types narrower using truncation, it is sometimes the case that either conversion could be used, in which case the early conversion should be preferred.

## Early format conversion

If fetching datums from `Dst` (as is usually the case), the early format conversion will take datums in one of the five formats supported by `Dst` and convert them to one of the supported intermediate floating-point or integer formats:

||From FP32 (e8m23)|From BF16 (e8m7)|From FP16 (e5m10)|From INT32 (e0m31)|From INT16 (e0m15)|
|:-:|---|---|---|---|---|
|**To FP32 (e8m23)**|Identity|No (keep as BF16 then use late conversion, or go via TF32; on Blackhole, an FP32 intermediate is `UnsupportedFunctionality`)|No (keep as FP16 then use late conversion)|Bitcast|No|
|**To TF32 (e8m10)**|Rounding|Rounding (though more efficient to keep as BF16 then use late conversion)|No (keep as FP16 then use late conversion)|Bitcast to FP32 then rounding|No|
|**To BF16 (e8m7)**|Rounding or truncation|Rounding or identity|No (keep as FP16 then use late conversion)|Bitcast of top 16 bits|No|
|**To E8M6**|Rounding|Rounding|No|No|No|
|**To FP16 (e5m10)**|Rounding or truncation to TF32 then exponent narrowing on Blackhole; on Wormhole, no (keep as FP32 then use late conversion, or round to TF32 then use late conversion)|Exponent narrowing on Blackhole; on Wormhole, no (keep as BF16 then use late conversion)|Rounding or identity|No|No|
|**To E5M7**|No|No|Truncation|No|No|
|**To E5M6**|No|No|Rounding|No|No|
|**To FP8 (e5m2)**|No (keep as FP32 then use late conversion)|No (keep as BF16 then use late conversion)|Truncation|No|No|
|**To INT32 (e0m31)**|Bitcast|No|No|Identity|No|
|**To INT16 (e0m15)**|Top 16 bits|No|No|Top 16 bits|Identity|
|**To INT8 (e0m7)**|Sign bit and low 7 bits of mantissa|Sign bit only, other bits zero|Sign bit only, other bits zero|Shift then round then saturate (-127 to +127), or sign bit and low 7 bits of magnitude|No|
|**To UINT8**|Low 8 bits of mantissa|No|No|Shift then round then saturate (0 to 255), or low 8 bits of magnitude|No|

Where rounding is supported, it can either be deterministic round-to-nearest with ties away from zero, or stochastic (though due to a hardware bug, stochastic rounding has a slight bias towards increasing the magnitude rather than being 50:50, and can even sometimes increase the magnitude of values which do not require rounding). Stochastic rounding (any of the `ALU_ROUNDING_MODE_*_srnd_en` fields set) is `UnsupportedFunctionality`.

Handling of denormals and NaNs in the early format conversion depends on the nature of the early format conversion:
* **Identity / bitcast:** Denormals preserved. NaN preserved.
* **Rounding:** Denormals flushed to zero. Minus zero converted to positive zero. If the exponent is 8 bits wide, NaN becomes infinity (this is a potentially surprising behavior). When rounding from FP32 or BF16 to BF16 or E8M6, the flush happens _after_ rounding: the bit pattern is rounded (which can carry from the mantissa into the exponent), and then the result is flushed to positive zero if its exponent is zero. With deterministic rounding, this is:

  ```c
  uint32_t RoundExp8(uint32_t x, unsigned ManBits) { // x is FP32, or BF16 in the high 16 bits; ManBits is 7 (BF16) or 6 (E8M6)
    uint32_t Sign = x & 0x80000000;
    unsigned Drop = 23 - ManBits;
    uint32_t Mag = (x & 0x7fffffff) + (1u << (Drop - 1)); // Round to nearest, ties away from zero; can carry into the exponent
    Mag &= ~((1u << Drop) - 1);
    if (Mag >= 0x7f800000) return Sign | 0x7f800000;      // Exponent 255: infinity, including from NaN
    if (Mag < 0x00800000) return 0;                       // Exponent 0: positive zero, including from denormals and minus zero
    return Sign | Mag;                                    // The result is in the high 1 + 8 + ManBits bits
  }
  ```

  Consequently, FP32 denormals with a mantissa of at least `0x7F8000` become the smallest BF16 normal of the same sign (`0x0080` or `0x8080`), whereas all other FP32 denormals become positive zero; and to E8M6, FP32 denormals with a mantissa of at least `0x7F0000` and BF16 denormals with a mantissa of `0x7F` become the smallest E8M6 normal of the same sign, whereas all other denormals become positive zero.
* **Truncation:** Denormals can become zero, if that is the outcome of truncating mantissa bits. NaN can become infinity, if that is the outcome of truncating mantissa bits.
* **Exponent narrowing (Blackhole only):** The datum has an 8-bit exponent and a 10-bit mantissa: from FP32, the TF32 result of the rounding (which flushes denormals and minus zero to positive zero and turns NaN into infinity) or of the truncation (which keeps them); from BF16, the BF16 datum with three zero bits appended to its mantissa. Its exponent is then narrowed to 5 bits without rounding:

```c
uint16_t NarrowToFP16(uint19_t x) { // Sign, 8-bit exponent, 10-bit mantissa
  uint1_t Sign = x >> 18;
  int32_t Exp = ((x >> 10) & 0xff) - 112;
  uint10_t Man = x & 0x3ff;
  if (Exp < 0) return Sign << 15;              // Zero, keeping the sign
  if (Exp > 31) return (Sign << 15) | 0x7fff;  // Saturate, including infinity and NaN
  return (Sign << 15) | (Exp << 10) | Man;
}
```

  An input in range <code>2<sup>-15</sup> ≤ |x| &lt; 2<sup>-14</sup></code> thus keeps its mantissa bits under an exponent field of zero, losing its implicit leading one, as the late conversion from FP32 to FP16 does.

-----

If Packer 0 is fetching datums from L1 rather than from `Dst`, then the early format conversion does not happen, and the datums from L1 need to occupy a whole number of bytes:
||From 32b in L1|From 16b in L1|From 8b in L1|
|:-:|---|---|---|
|**To FP32 (e8m23)**|Identity|No|No|
|**To TF32 (e8m10)**|No|No|No|
|**To BF16 (e8m7)**|No|Identity|Sign and mantissa from 8b, exponent zero|
|**To E8M6**|No|No|No|
|**To FP16 (e5m10)**|No|Identity|No|
|**To E5M7**|No|No|Sign and mantissa from 8b, exponent zero|
|**To E5M6**|No|No|No|
|**To FP8 (e5m2)**|No|No|Identity|
|**To INT32**|Identity|Shifted left by 16 bits; bottom bits filled with zero|Shifted left by 24 bits; bottom bits filled with zero|
|**To INT16**|Uses top 16 bits; bottom 16 discarded|Identity|Shifted left by 8 bits; bottom bits filled with zero|
|**To INT8<br/>or UINT8**|No|Uses top 8 bits; bottom 8 discarded|Identity|

## Late format conversion

The late format conversion takes one of the supported intermediate formats, and converts it to a format supported by L1:

<table><tr><th/><th>From FP32</th><th>From TF32 or BF16 or E8M6 or FP16 or E5M7 or E5M6 or FP8</th><th>From INT32</th><th>From INT16</th><th>From INT8 or UINT8</th></tr>
<tr><th>To FP32 or BF16</th><td colspan="2">Truncate if there is narrowing of mantissa</td><td colspan="3">No</td></tr>
<tr><th>To TF32</th><td>No (use early conversion)</td><td>Yes (all bits preserved; no rounding or truncation or saturation ever required)</td><td colspan="3">No</td></tr>
<tr><th>To FP16<br/>or FP8</th><td colspan="2">Saturate if there is narrowing of exponent, then truncate if there is narrowing of mantissa</td><td colspan="3">No</td></tr>
<tr><th>To BFP8</th><td colspan="2">Converted to BF16 (as per first row), then round to BFP8 with one shared 8b exponent per 16 datums</td><td colspan="3">No</td></tr>
<tr><th>To BFP4<br/>or BFP2</th><td colspan="2">Converted to BFP8 (as per the above row), then truncate to BFP4 or BFP2</td><td colspan="3">No</td></tr>
<tr><th>To BFP8a</th><td colspan="2">Converted to E5M7 (saturate if there is narrowing of exponent, then truncate if there is narrowing of mantissa), then round to BFP8a with one shared 5b exponent per 16 datums</td><td colspan="3">No</td></tr>
<tr><th>To BFP4a<br/>or BFP2a</th><td colspan="2">Converted to BFP8a (as per the above row), then truncate to BFP4a or BFP2a</td><td colspan="3">No</td></tr>
<tr><th>To INT32</th><td colspan="2">No</td><td>Identity</td><td>No</td><td>No</td></tr>
<tr><th>To INT16</th><td colspan="2">No</td><td>No</td><td>Identity</td><td>No</td></tr>
<tr><th>To INT8<br/>or UINT8</th><td colspan="2">No</td><td>No</td><td>No</td><td>Identity or bitcast</td></tr>
</table>

Where rounding happens in BFP conversions, it can either be deterministic round-to-nearest with ties away from zero, or stochastic (though due to a hardware bug, stochastic rounding has a slight bias towards increasing the magnitude rather than being 50:50, and can even sometimes increase the magnitude of values which do not require rounding). Stochastic rounding (any of the `ALU_ROUNDING_MODE_*_srnd_en` fields set) is `UnsupportedFunctionality`. Stochastic rounding in this conversion (`ALU_ROUNDING_MODE_Packer_srnd_en`) can also turn a datum into zero: where deterministic rounding clamps a magnitude of 128 to 127 (see `Mag == 128` below), stochastic rounding can instead carry out of the magnitude, so a datum whose exponent equals the shared exponent and whose mantissa is `0x7E` or `0x7F` can become +0.

With deterministic rounding, each group of 16 datums is converted from BF16 to BFP8, and then possibly truncated to BFP4 or BFP2, using the following logic:

```c
void BF16ToBFP8(const uint16_t Datums[16], uint8_t* SharedExp, uint8_t BFP8[16]) {
  uint8_t MaxExp = 0;
  for (unsigned i = 0; i < 16; ++i) {
    uint8_t Exp = (Datums[i] >> 7) & 0xff;
    if (Exp > MaxExp) MaxExp = Exp; // NB: Exp == 255 is not special
  }
  *SharedExp = MaxExp;
  for (unsigned i = 0; i < 16; ++i) {
    uint8_t Sign = Datums[i] >> 15;
    uint8_t