ELWADD
tenstorrent_tt_isa)Tenstorrent Wormhole B0 & Blackhole A0
main
userTensix, Matrix Unit, WormholeB0Summary
An aligned 8x16 block of SrcA is added element-wise to an aligned 8x16 block of SrcB, and the result either assigned to an aligned 8x16 block of Dst or 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.
Syntax / Encoding
TT_ELWADD(((/* bool */ FlipSrcB) << 1) +
/* bool */ FlipSrcA,
/* bool */ AddDst,
((/* bool */ BroadcastSrcBRow) << 1) +
/* bool */ BroadcastSrcBCol0,
/* u2 */ AddrMod,
/* u10 */ DstRow)
Binary Encodings (2)
| Width | Format | Bit Pattern | Mask / Match |
|---|---|---|---|
| 32 | Tensix-32b | 00101000xxxxxxxxxxxxxxxxxxxxxxxx | 0xff000000 / 0x28000000 |
| 32 | Tensix-32b (Blackhole) | 00101000xxxxxxxxxxxxxxxxxxxxxxxx | 0xff000000 / 0x28000000 |
Reference Notes
# `ELWADD` (`Dst = SrcA + SrcB` or `Dst += SrcA + SrcB`)
**Summary:** An aligned 8x16 block of `SrcA` is added element-wise to an aligned 8x16 block of `SrcB`, and the result either assigned to an aligned 8x16 block of `Dst` or 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 supported data type combinations are:
|[`Dst` data type](Dst.md#data-types)|= or +=|[`SrcA` data type](SrcASrcB.md#data-types)|+|[`SrcB` data type](SrcASrcB.md#data-types)|
|---|---|---|---|---|
|8x16 matrix of either FP32 or BF16|= or +=|8x16 matrix of either TF32 or BF16|+|8x16 matrix of either TF32 or BF16|
|8x16 matrix of either FP32 or FP16|= or +=|8x16 matrix of FP16|+|8x16 matrix of FP16|
|8x16 matrix of integer "32"|= or +=|8x16 matrix of integer "8"|+|8x16 matrix of integer "8"|
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_ELWADD(((/* bool */ FlipSrcB) << 1) +
/* bool */ FlipSrcA,
/* bool */ AddDst,
((/* bool */ BroadcastSrcBRow) << 1) +
/* bool */ BroadcastSrcBCol0,
/* u2 */ AddrMod,
/* u10 */ DstRow)
```
## Encoding

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

## 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;
if (FidelityPhase) {
UnsupportedFunctionality(); // ELWADD/SUB should not be used with FidelityPhase
}
// 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, true);
int32_t SrcBValInt = ReadSrcInt8(SrcBVal, FlushDenormals);
int32_t Result = SrcAValInt + SrcBValInt;
// Dst is INT32.
if (AddDst) 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 = SrcAValFP + SrcBValFP;
if (UseDst32b) {
// Dst is FP32, regardless of SrcAStyle.
if (AddDst) Result += ReadDstFP32(Dst32b[DstRow + i][j]);
Dst32b[DstRow + i][j] = WriteDstFP32(Result);
} else if (SrcAStyle == FP16) {
// Dst is FP16, just like SrcAStyle.
if (AddDst) Result += ReadDstFP16(Dst16b[DstRow + i][j]);
Dst16b[DstRow + i][j] = WriteDstFP16(Result);
} else {
// Dst is BF16 (SrcAStyle is either BF16 or TF32).
if (AddDst) Result += ReadDstBF16(Dst16b[DstRow + i][j]);
Dst16b[DstRow + i][j] = WriteDstBF16(Result);
}
}
}
}
// Possibly flip source ba