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

ELWMUL

T1 tenstorrent matrix insn
ELWMUL (Dst += SrcA * SrcB element-wise)
SourceTenstorrent Official Tensix ISA Documentation (Wormhole & Blackhole) (tenstorrent_tt_isa)
Reliability TierT1 — Official Specification
ISA / Silicon ScopeBlackholeA0 / WormholeB0
Tenstorrent Wormhole B0 & Blackhole A0
License & VersionApache-2.0
main
Upstreamhttps://github.com/tenstorrent/tt-isa-documentation
Privilegeuser
ExtensionsTensix, Matrix Unit, BlackholeA0, WormholeB0

Summary

An aligned 8x16 block of SrcA is multiplied element-wise with an aligned 8x16 block of SrcB, and the result added element-wise to an aligned 8x16 block of Dst. Broadcasting is supported on the SrcB operand; any 1x16 row can be broadcasted up to 8x16 and/or column 0 can be broadcasted to all columns. The hardware multipliers consume at most 5 bits of SrcA magnitude/mantissa and at most 7 bits of SrcB…

Syntax / Encoding

TT_ELWMUL(((/* bool */ FlipSrcB) << 1) +
            /* bool */ FlipSrcA,
            false,
          ((/* bool */ BroadcastSrcBRow) << 1) +
            /* bool */ BroadcastSrcBCol0,
            /* u2 */ AddrMod,
            /* u10 */ DstRow)

Binary Encodings (2)

WidthFormatBit PatternMask / Match
32Tensix-32b00100111xxxxxxxxxxxxxxxxxxxxxxxx0xff000000 / 0x27000000
32Tensix-32b (Blackhole)00100111xxxxxxxxxxxxxxxxxxxxxxxx0xff000000 / 0x27000000

Reference Notes

# `ELWMUL` (`Dst += SrcA * SrcB` element-wise)

**Summary:** An aligned 8x16 block of `SrcA` is multiplied element-wise with an aligned 8x16 block of `SrcB`, and the result added element-wise to an aligned 8x16 block of `Dst`. Broadcasting is supported on the `SrcB` operand; any 1x16 row can be broadcasted up to 8x16 and/or column 0 can be broadcasted to all columns. The hardware multipliers consume at most 5 bits of `SrcA` magnitude/mantissa and at most 7 bits of `SrcB` magnitude/mantissa, so software may wish to execute up to four separate fidelity phases, subject to the precision of the input data and the desired output precision. To get the effect of `Dst = SrcA * SrcB` (i.e. `=` rather than `+=`), [`ZEROACC`](ZEROACC.md) can be used prior to `ELWMUL`, as `ELWMUL` reads undefined rows as zero.

The supported data type combinations are:

|[`Dst` data type](Dst.md#data-types)|+=|[`SrcA` data type](SrcASrcB.md#data-types)|*|[`SrcB` data type](SrcASrcB.md#data-types)|
|---|---|---|---|---|
|8x16 matrix of either FP32 or BF16|+=|8x16 matrix of either TF32 (†) or BF16|*|8x16 matrix of either TF32 or BF16|
|8x16 matrix of either FP32 or FP16|+=|8x16 matrix of FP16 (†)|*|8x16 matrix of FP16|
|8x16 matrix of integer "32"|+=|8x16 matrix of integer "8" (‡)|*|8x16 matrix of integer "8"|

(†) For `SrcA`, the least significant bit of the FP16 or TF32 mantissa is ignored in multiplication operations.

(‡) For `SrcA`, the most significant two bits of the integer "8" magnitude are ignored in multiplication operations, leaving just the low eight bits of magnitude. The usable values are therefore -255 through +255.

When `Dst += SrcA * SrcB` is performed for integers, the computation is really `Dst = Saturate(Dst + SrcA * SrcB)`, where `Saturate` clamps at ±(2<sup>31</sup>-1).

**Backend execution unit:** [Matrix Unit (FPU)](MatrixUnit.md)

## Syntax

```c
TT_ELWMUL(((/* bool */ FlipSrcB) << 1) +
            /* bool */ FlipSrcA,
            false,
          ((/* bool */ BroadcastSrcBRow) << 1) +
            /* bool */ BroadcastSrcBCol0,
            /* u2 */ AddrMod,
            /* u10 */ DstRow)
```

## Encoding

![](../../../Diagrams/Out/Bits32_ELWMUL.svg)

On Blackhole, `AddrMod` is a `u3` rather than a `u2`, giving this encoding:

![](../../../Diagrams/Out/Bits32_ELWMUL_BH.svg)

## Functional model

This instruction will, if necessary, spend time waiting at the Wait Gate before being dispatched to the Matrix Unit (FPU):

```c
while (SrcA[MatrixUnit.SrcABank].AllowedClient != MatrixUnit
    || SrcB[MatrixUnit.SrcBBank].AllowedClient != MatrixUnit) {
  wait;
}
```

Once dispatched to the Matrix Unit (FPU):
```c
uint1_t StateID = ThreadConfig[CurrentThread].CFG_STATE_ID_StateID;
auto& ConfigState = Config[StateID];

// Determine the data formats.
// Note: Blackhole implied format behavior for SrcB not fully characterized
uint4_t SrcAStyle;
bool UseDst32b;
uint4_t SrcAFmt = ConfigState.ALU_FORMAT_SPEC_REG_SrcA_override ? ConfigState.ALU_FORMAT_SPEC_REG_SrcA_val : ConfigState.ALU_FORMAT_SPEC_REG0_SrcA;
uint4_t SrcBFmt = ConfigState.ALU_FORMAT_SPEC_REG_SrcB_override ? ConfigState.ALU_FORMAT_SPEC_REG_SrcB_val : ConfigState.ALU_FORMAT_SPEC_REG1_SrcB;
if ((TTArchitecture == Blackhole) && !ThreadConfig[CurrentThread].DISABLE_IMPLIED_SRCA_FMT_Base) {
  SrcAFmt = ImpliedSrcAFmt[MatrixUnit.SrcABank];
}
if ((TTArchitecture == Blackhole) && !ThreadConfig[CurrentThread].DISABLE_IMPLIED_SRCB_FMT_Base) {
  SrcBFmt = ImpliedSrcBFmt[MatrixUnit.SrcBBank];
}
if (ThreadConfig[CurrentThread].FP16A_FORCE_Enable) {
  UnsupportedFunctionality(); // No known usage, confidence in specification below is weak
  SrcAStyle = FP16;
  UseDst32b = false;
} else if (ConfigState.ALU_ACC_CTRL_INT8_math_enabled) {
  SrcAStyle = INT8;
  UseDst32b = true;
} else {
  if (SrcAFmt in {FP32, BF16, BFP8, BFP4, BFP2, INT32, INT16}) {
    SrcAStyle = BF16;
  } else if (SrcAFmt in {FP16, FP8, BFP8a, BFP4a, BFP2a, INT8}) {
    SrcAStyle = FP16;
  } else /* SrcAFmt == TF32 */ {
    SrcAStyle = TF32;
  }
  UseDst32b = ConfigState.ALU_ACC_CTRL_Fp32_enabled;
}
bool FlushDenormals = !ConfigState.ALU_ACC_CTRL_Zero_Flag_disabled_src; // Note: behavior has yet to be fully characterized
if (!FlushDenormals) {
  UnsupportedFunctionality(); // Asymmetric "keep SrcB denormals" mode that cannot represent 0.0 should not be used
}

// Determine the row range.
uint6_t SrcARow = RWCs[CurrentThread].SrcA & 0x38;
uint6_t SrcBRow = RWCs[CurrentThread].SrcB & (BroadcastSrcBRow ? 0x3f : 0x38);
DstRow += ThreadConfig[CurrentThread].DEST_TARGET_REG_CFG_MATH_Offset;
DstRow += RWCs[CurrentThread].Dst + ConfigState.DEST_REGW_BASE_Base;
DstRow &= 0x3f8;

// Determine the fidelity phase.
uint2_t FidelityPhase = RWCs[CurrentThread].FidelityPhase;
FidelityPhase += ThreadConfig[CurrentThread].FIDELITY_BASE_Phase;
FidelityPhase &= 3;

// Perform the element-wise computation.
for (unsigned i = 0; i < 8; ++i) {
  for (unsigned j = 0; j < 16; ++j) {
    uint19_t SrcAVal = TruncateSrc(SrcA[MatrixUnit.SrcABank][SrcARow + i][j], SrcAFmt);
    uint19_t SrcBVal = TruncateSrc(SrcB[MatrixUnit.SrcBBank][SrcBRow + (BroadcastSrcBRow ? 0 : i)][BroadcastSrcBCol0 ? 0 : j], SrcBFmt);
    if (SrcAStyle == INT8) {
      int32_t SrcAValInt = ReadSrcInt8(SrcAVal & (FidelityPhase & 1 ? 0x41fff : 0x4e0ff), true);
      int32_t SrcBValInt = ReadSrcInt8(SrcBVal & (FidelityPhase & 2 ? 0x40fff : 0x7f0ff), FlushDenormals);
      int32_t Result = SrcAValInt * SrcBValInt;
      // Dst is INT32.
      Result = SaturateAddInt32(Result, DstDecodeInt32(Dst32b[DstRow + i][j]));
      Dst32b[DstRow + i][j] = DstEncodeInt32(Result);
    } else {
      float SrcAValFP, SrcBValFP;
      switch (SrcAStyle) {
      case BF16: SrcAValFP = SrcDecodeBF16(SrcAVal); SrcBValFP = SrcDecodeBF16(SrcBVal); break;
      case FP16: SrcAValFP = SrcDecodeFP16(SrcAVal); SrcBValFP = SrcDecodeFP16(SrcBVal); break;
      case TF32: SrcAValFP = SrcDecodeTF32(SrcAVal); SrcBValFP = SrcDecodeTF32(SrcBVal); break;
      }
      float Result = S