Skip to content

Commit 9b0920b

Browse files
hanwHan Wang
authored andcommitted
tinytpu: VPU_IPAIR_ROTATE + Controller fusion path (slice 6)
Slice 6 of the generic-VPU MXU epilogue plan. Adds the integer variant of VPU_PAIR_ROTATE end-to-end so RoPE-style fused epilogues can lower without round-tripping through float. - src/VPU.bsv: new VPU_IPAIR_ROTATE enum at ordinal 85; case body mirrors VPU_PAIR_ROTATE math with Int#(32) directly (wraps on overflow; no saturation). staticAssert extended to cover both ops. - src/Controller.bsv: drain rule grows a second inline pass that fires only when vpuOpReg == VPU_IPAIR_ROTATE, walking adjacent lane pairs and overwriting the curated-subset fallthrough. Keeps the per-cell case statement compact and synthesizable. - scripts/tasm.py: _VPU table gains IPAIR_ROTATE=85; round-trip count assertion bumped to 86. - tests/test_tinytpu_backend.py: new TestMxuVpuEpilogue case fires op 46 with OP=IPAIR_ROTATE through the full sim stack and asserts the hand-derived expected tile. - tinygrad submodule bump: matching Python opcode entry. Verification: 898/898 backend tests, 151 tasm + epilogue tests.
1 parent 6b09afc commit 9b0920b

6 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎scripts/tasm.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
"MIN_U32": 82,
167167
"MAX_U32": 83,
168168
"PAIR_ROTATE": 84,
169+
"IPAIR_ROTATE": 85,
169170
}
170171
_VPU_INV = {v: k for k, v in _VPU.items()}
171172

‎src/Controller.bsv‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,25 @@ module mkController#(
470470
endcase
471471
outm[ri][ci] = v;
472472
end
473+
// Lane-pair rotation. Separate inner-loop pass because the op
474+
// works on adjacent lane pairs rather than a single (r,c) cell:
475+
// out[r][2p] = drain[r][2p] * src2[r][2p]
476+
// - drain[r][2p+1] * src2[r][2p+1]
477+
// out[r][2p + 1] = drain[r][2p] * src2[r][2p+1]
478+
// + drain[r][2p+1] * src2[r][2p]
479+
// Matches VPU.bsv VPU_IPAIR_ROTATE semantics so a future caller
480+
// can lower to either the dedicated VPU dispatch or the fused
481+
// MXU epilogue interchangeably.
482+
if (vpuOpReg == VPU_IPAIR_ROTATE)
483+
for (Integer ri = 0; ri < valueOf(rows); ri = ri + 1)
484+
for (Integer p = 0; p < valueOf(cols) / 2; p = p + 1) begin
485+
Int#(32) de = m[ri][2 * p];
486+
Int#(32) doo = m[ri][2 * p + 1];
487+
Int#(32) c = src2[ri][2 * p];
488+
Int#(32) sn = src2[ri][2 * p + 1];
489+
outm[ri][2 * p] = de * c - doo * sn;
490+
outm[ri][2 * p + 1] = de * sn + doo * c;
491+
end
473492
epilogueBuf <= outm;
474493
end else begin
475494
case (psumModeReg)

‎src/VPU.bsv‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ typedef enum { VPU_ADD, VPU_MUL, VPU_RELU, VPU_MAX, VPU_SUM_REDUCE, VPU_CMPLT, V
6464
// out[2p] = d[2p]*cos - d[2p+1]*sin
6565
// out[2p+1] = d[2p]*sin + d[2p+1]*cos
6666
// src2 packs cos in even lanes, sin in odd lanes.
67-
VPU_PAIR_ROTATE }
67+
VPU_PAIR_ROTATE,
68+
// Integer variant of VPU_PAIR_ROTATE. Same arithmetic with
69+
// Int#(32) operands; caller is responsible for fixed-point
70+
// scaling. Wraps on overflow (no saturation). Useful for
71+
// fused RoPE-style epilogues that operate on the MXU's
72+
// INT32 drain matrix without round-tripping through float.
73+
VPU_IPAIR_ROTATE }
6874
VpuOp deriving (Bits, Eq, FShow);
6975

7076
// Add two signed 8-bit values with saturation to [-128, 127].
@@ -373,7 +379,7 @@ module mkVPU(VPU_IFC#(sublanes, lanes))
373379
);
374380

375381
staticAssert(valueOf(lanes) % 2 == 0,
376-
"VPU_PAIR_ROTATE requires an even lane count");
382+
"VPU_PAIR_ROTATE / VPU_IPAIR_ROTATE require an even lane count");
377383

378384
Reg#(Vector#(sublanes, Vector#(lanes, Int#(32)))) resultReg <- mkRegU;
379385

@@ -840,6 +846,18 @@ module mkVPU(VPU_IFC#(sublanes, lanes))
840846
row[2 * p + 1] = fp2bits(tpl_1(addFP(es, oc, Rnd_Nearest_Even)));
841847
end
842848
end
849+
VPU_IPAIR_ROTATE: begin
850+
// Integer pair-rotate. Same shape as VPU_PAIR_ROTATE but
851+
// operating on Int#(32) directly — caller scales fixed-point.
852+
for (Integer p = 0; p < valueOf(lanes) / 2; p = p + 1) begin
853+
Int#(32) d_e = src1[s][2 * p];
854+
Int#(32) d_o = src1[s][2 * p + 1];
855+
Int#(32) c = src2[s][2 * p];
856+
Int#(32) sn = src2[s][2 * p + 1];
857+
row[2 * p] = d_e * c - d_o * sn;
858+
row[2 * p + 1] = d_e * sn + d_o * c;
859+
end
860+
end
843861
VPU_FMAX: begin
844862
for (Integer l = 0; l < valueOf(lanes); l = l + 1) begin
845863
let cmp = compareFP(bits2fp(src1[s][l]), bits2fp(src2[s][l]));

‎tests/test_tasm.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,9 @@ def test_vpu_ops_cover_full_range():
777777
from scripts.tasm import _VPU
778778
# Full VPU opcode range including EXP2/LOG2/SIN/COS transcendentals
779779
# and the packed-int8 quantized ops at the tail.
780-
assert len(_VPU) == 85
780+
assert len(_VPU) == 86
781781
codes = sorted(_VPU.values())
782-
assert codes == list(range(85))
782+
assert codes == list(range(86))
783783

784784

785785
def test_vpu_min_u32_roundtrip():

‎tests/test_tinytpu_backend.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7803,6 +7803,22 @@ def test_vpu_add_dst_vreg(self):
78037803
expected = [d + s for d, s in zip(self._DRAIN, self._SRC2)]
78047804
self.assertEqual(got, expected)
78057805

7806+
def test_vpu_ipair_rotate_dst_vmem(self):
7807+
# IPAIR_ROTATE: per-row pair rotation.
7808+
# out[r][2p] = d[r][2p] * s[r][2p] - d[r][2p+1] * s[r][2p+1]
7809+
# out[r][2p+1] = d[r][2p] * s[r][2p+1] + d[r][2p+1] * s[r][2p]
7810+
got = self._run("IPAIR_ROTATE", vmem_dst=True)
7811+
expected = []
7812+
for r in range(4):
7813+
d_row = self._DRAIN[r * 4:(r + 1) * 4]
7814+
s_row = self._SRC2[r * 4:(r + 1) * 4]
7815+
for p in range(2):
7816+
de, do = d_row[2 * p], d_row[2 * p + 1]
7817+
c, sn = s_row[2 * p], s_row[2 * p + 1]
7818+
expected.append(de * c - do * sn)
7819+
expected.append(de * sn + do * c)
7820+
self.assertEqual(got, expected)
7821+
78067822

78077823
if __name__ == "__main__":
78087824
unittest.main()

‎tinygrad‎

0 commit comments

Comments
 (0)