-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathtcu_unit.cpp
More file actions
1482 lines (1379 loc) · 56.7 KB
/
tcu_unit.cpp
File metadata and controls
1482 lines (1379 loc) · 56.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2019-2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <VX_types.h>
#include "tcu_unit.h"
#include "tensor_cfg.h"
#include <rvfloats.h>
#include "core.h"
#include "scheduler.h"
#include "local_mem.h"
#include "processor_impl.h"
#include "mem/memory.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <limits>
#include <type_traits>
using namespace vortex;
namespace vt = vortex::tensor;
using cfg = vt::wmma_config_t<VX_CFG_NUM_THREADS>;
using wg_cfg = vt::wgmma_config_t<VX_CFG_NUM_THREADS, vt::fp32, vt::fp32>;
inline uint64_t nan_box(uint32_t value) {
return value | 0xffffffff00000000;
}
static inline uint8_t unpack_u8(uint32_t word, uint32_t idx) {
return (word >> (idx * 8)) & 0xffu;
}
// FMA<It, Ot>: fused multiply-add returning an Ot-typed accumulator (bit-packed in uint32).
// Widens narrow inputs/accumulator to fp32, performs mul+add, rounds once to Ot.
template <typename It, typename Ot>
struct FMA {
using itype = typename It::dtype;
using otype = typename Ot::dtype;
static uint32_t eval(itype a, itype b, uint32_t c) {
otype fa = static_cast<otype>(a);
otype fb = static_cast<otype>(b);
otype fc = bit_cast<otype>(c);
return bit_cast<uint32_t>(fa * fb + fc);
}
};
// -- fp16 inputs --
template <> struct FMA<vt::fp16, vt::fp32> {
static uint32_t eval(uint16_t a, uint16_t b, uint32_t c) {
auto fa = rv_htof_s(a, 0, nullptr);
auto fb = rv_htof_s(b, 0, nullptr);
return rv_fadd_s(rv_fmul_s(fa, fb, 0, nullptr), c, 0, nullptr);
}
};
template <> struct FMA<vt::fp16, vt::fp16> {
static uint32_t eval(uint16_t a, uint16_t b, uint32_t c) {
auto fa = rv_htof_s(a, 0, nullptr);
auto fb = rv_htof_s(b, 0, nullptr);
auto fc = rv_htof_s(uint16_t(c), 0, nullptr);
return rv_ftoh_s(rv_fmadd_s(fa, fb, fc, 0, nullptr), 0, nullptr);
}
};
// -- bf16 inputs --
template <> struct FMA<vt::bf16, vt::fp32> {
static uint32_t eval(uint16_t a, uint16_t b, uint32_t c) {
auto fa = rv_btof_s(a, 0, nullptr);
auto fb = rv_btof_s(b, 0, nullptr);
return rv_fadd_s(rv_fmul_s(fa, fb, 0, nullptr), c, 0, nullptr);
}
};
template <> struct FMA<vt::bf16, vt::bf16> {
static uint32_t eval(uint16_t a, uint16_t b, uint32_t c) {
auto fa = rv_btof_s(a, 0, nullptr);
auto fb = rv_btof_s(b, 0, nullptr);
auto fc = rv_btof_s(uint16_t(c), 0, nullptr);
return rv_ftob_s(rv_fmadd_s(fa, fb, fc, 0, nullptr), 0, nullptr);
}
};
// -- fp8 inputs --
template <> struct FMA<vt::fp8, vt::fp32> {
static uint32_t eval(uint8_t a, uint8_t b, uint32_t c) {
auto fa = rv_e4m3tof_s(a, 0, nullptr);
auto fb = rv_e4m3tof_s(b, 0, nullptr);
return rv_fadd_s(rv_fmul_s(fa, fb, 0, nullptr), c, 0, nullptr);
}
};
template <> struct FMA<vt::fp8, vt::fp8> {
static uint32_t eval(uint8_t a, uint8_t b, uint32_t c) {
auto fa = rv_e4m3tof_s(a, 0, nullptr);
auto fb = rv_e4m3tof_s(b, 0, nullptr);
auto fc = rv_e4m3tof_s(uint8_t(c), 0, nullptr);
return rv_ftoe4m3_s(rv_fmadd_s(fa, fb, fc, 0, nullptr), 0, nullptr);
}
};
// -- bf8 inputs --
template <> struct FMA<vt::bf8, vt::fp32> {
static uint32_t eval(uint8_t a, uint8_t b, uint32_t c) {
auto fa = rv_e5m2tof_s(a, 0, nullptr);
auto fb = rv_e5m2tof_s(b, 0, nullptr);
return rv_fadd_s(rv_fmul_s(fa, fb, 0, nullptr), c, 0, nullptr);
}
};
template <> struct FMA<vt::bf8, vt::bf8> {
static uint32_t eval(uint8_t a, uint8_t b, uint32_t c) {
auto fa = rv_e5m2tof_s(a, 0, nullptr);
auto fb = rv_e5m2tof_s(b, 0, nullptr);
auto fc = rv_e5m2tof_s(uint8_t(c), 0, nullptr);
return rv_ftoe5m2_s(rv_fmadd_s(fa, fb, fc, 0, nullptr), 0, nullptr);
}
};
// -- tf32 inputs --
template <> struct FMA<vt::tf32, vt::fp32> {
static uint32_t eval(uint32_t a, uint32_t b, uint32_t c) {
auto fa = rv_tf32tof_s(a, 0, nullptr);
auto fb = rv_tf32tof_s(b, 0, nullptr);
return rv_fadd_s(rv_fmul_s(fa, fb, 0, nullptr), c, 0, nullptr);
}
};
template <> struct FMA<vt::tf32, vt::tf32> {
static uint32_t eval(uint32_t a, uint32_t b, uint32_t c) {
auto fa = rv_tf32tof_s(a, 0, nullptr);
auto fb = rv_tf32tof_s(b, 0, nullptr);
auto fc = rv_tf32tof_s(c, 0, nullptr);
return rv_ftotf32_s(rv_fmadd_s(fa, fb, fc, 0, nullptr), 0, nullptr);
}
};
// Generic FEDP: universal rule keyed on output width.
// * Wide Ot (fp32): accumulate Σ(a_k*b_k) in fp32, add c_val last.
// * Narrow Ot (fp16/bf16/fp8/bf8/…): chain FMA<It,Ot> so the accumulator is
// rounded to Ot each step.
template <typename It, typename Ot>
struct FEDP {
using itype = typename It::dtype;
static uint32_t eval(const reg_data_t *a_row, const reg_data_t *b_col, uint32_t c_val) {
constexpr uint32_t i_ratio = sizeof(uint32_t) / sizeof(itype);
static_assert(i_ratio * sizeof(itype) == sizeof(uint32_t), "FEDP: tcK * i_ratio must be <= 32");
if constexpr (std::is_same_v<Ot, vt::fp32>) {
uint32_t acc = 0;
for (uint32_t z = 0; z < cfg::tcK; ++z) {
auto a = reinterpret_cast<const itype *>(&a_row[z].u32);
auto b = reinterpret_cast<const itype *>(&b_col[z].u32);
uint32_t prod = 0;
for (uint32_t i = 0; i < i_ratio; ++i) {
prod = FMA<It, vt::fp32>::eval(a[i], b[i], prod);
}
acc = rv_fadd_s(prod, acc, 0, nullptr);
}
return rv_fadd_s(c_val, acc, 0, nullptr);
} else {
uint32_t acc = c_val;
for (uint32_t z = 0; z < cfg::tcK; ++z) {
auto a = reinterpret_cast<const itype *>(&a_row[z].u32);
auto b = reinterpret_cast<const itype *>(&b_col[z].u32);
for (uint32_t i = 0; i < i_ratio; ++i) {
acc = FMA<It, Ot>::eval(a[i], b[i], acc);
}
}
return acc;
}
}
};
template <>
struct FEDP<vt::int4, vt::int32>{
static uint32_t eval(const reg_data_t *a_row, const reg_data_t *b_col, uint32_t c_val) {
auto acc = bit_cast<int32_t>(c_val);
for (uint32_t z = 0; z < cfg::tcK; ++z) {
auto a = a_row[z].u32;
auto b = b_col[z].u32;
for (uint32_t i = 0; i < 8; ++i) { // 8 * 4 bits = 32 bits
int32_t a_val = (a >> (i * 4)) & 0xF;
int32_t b_val = (b >> (i * 4)) & 0xF;
if (a_val & 0x8) {
a_val |= 0xFFFFFFF0;
}
if (b_val & 0x8) {
b_val |= 0xFFFFFFF0;
}
acc += a_val * b_val;
}
}
return bit_cast<uint32_t>(acc);
}
};
template <>
struct FEDP<vt::uint4, vt::int32>{
static uint32_t eval(const reg_data_t *a_row, const reg_data_t *b_col, uint32_t c_val) {
auto acc = bit_cast<int32_t>(c_val);
for (uint32_t z = 0; z < cfg::tcK; ++z) {
auto a = a_row[z].u32;
auto b = b_col[z].u32;
for (uint32_t i = 0; i < 8; ++i) { // 8 * 4 bits = 32 bits
int32_t a_val = (a >> (i * 4)) & 0xF;
int32_t b_val = (b >> (i * 4)) & 0xF;
acc += a_val * b_val;
}
}
return bit_cast<uint32_t>(acc);
}
};
using PFN_FEDP = uint32_t (*)(const reg_data_t*, const reg_data_t*, uint32_t);
static PFN_FEDP select_FEDP(uint32_t IT, uint32_t OT) {
switch (OT) {
case vt::fp32::id:
switch (IT) {
case vt::fp16::id:
return FEDP<vt::fp16, vt::fp32>::eval;
case vt::bf16::id:
return FEDP<vt::bf16, vt::fp32>::eval;
case vt::fp8::id:
return FEDP<vt::fp8, vt::fp32>::eval;
case vt::bf8::id:
return FEDP<vt::bf8, vt::fp32>::eval;
case vt::tf32::id:
return FEDP<vt::tf32, vt::fp32>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
case vt::fp16::id:
switch (IT) {
case vt::fp16::id:
return FEDP<vt::fp16, vt::fp16>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
case vt::bf16::id:
switch (IT) {
case vt::bf16::id:
return FEDP<vt::bf16, vt::bf16>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
case vt::fp8::id:
switch (IT) {
case vt::fp8::id:
return FEDP<vt::fp8, vt::fp8>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
case vt::bf8::id:
switch (IT) {
case vt::bf8::id:
return FEDP<vt::bf8, vt::bf8>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
case vt::tf32::id:
switch (IT) {
case vt::tf32::id:
return FEDP<vt::tf32, vt::tf32>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
case vt::int32::id:
switch (IT) {
case vt::int8::id:
return FEDP<vt::int8, vt::int32>::eval;
case vt::uint8::id:
return FEDP<vt::uint8, vt::int32>::eval;
case vt::int4::id:
return FEDP<vt::int4, vt::int32>::eval;
case vt::uint4::id:
return FEDP<vt::uint4, vt::int32>::eval;
default:
std::cout << "Error: unsupported mma format: " << IT << " -> " << OT << "!" << std::endl;
std::abort();
}
break;
default:
std::cout << "Error: unsupported output type: " << OT << "!" << std::endl;
std::abort();
}
}
// Format-agnostic sparse gather: for each bword, iterate over its elem_count packed
// elements, collect those flagged by lo_mask/hi_mask respectively.
static inline uint32_t gather_sparse(uint32_t bword0, uint32_t bword1,
uint32_t lo_mask, uint32_t hi_mask,
uint32_t elem_bits) {
uint32_t elem_count = 32 / elem_bits;
uint32_t elem_mask = (elem_bits < 32) ? ((1u << elem_bits) - 1u) : ~0u;
assert((uint32_t)(__builtin_popcount(lo_mask) + __builtin_popcount(hi_mask)) == elem_count &&
"gather_sparse: total selected elements must equal elem_count");
uint32_t out = 0, k = 0;
for (uint32_t i = 0; i < elem_count; ++i) {
if (lo_mask & (1u << i))
out |= ((bword0 >> (i * elem_bits)) & elem_mask) << (k++ * elem_bits);
}
for (uint32_t i = 0; i < elem_count; ++i) {
if (hi_mask & (1u << i))
out |= ((bword1 >> (i * elem_bits)) & elem_mask) << (k++ * elem_bits);
}
return out;
}
static inline uint32_t meta_num_cols(uint32_t fmt_s) {
return vt::sparse_meta_num_cols(fmt_s, VX_CFG_NUM_THREADS);
}
static inline uint32_t meta_row_width(uint32_t elem_bits) {
// Each K-step uses (32/elem_bits) meta bits per half (lo and hi), 2 halves per row.
return cfg::tcK * 2 * (32 / elem_bits);
}
class TcuUnit::Impl {
public:
struct lmem_desc_t {
uint64_t base = 0;
uint32_t ldm = 0;
bool col_major = false;
};
Impl(TcuUnit* simobject, Core* core)
: simobject_(simobject)
, core_(core)
, sparse_meta_(VX_CFG_NUM_WARPS, std::vector<uint32_t>(kMetaBanks * kMaxMetaCols, 0))
, perf_stats_()
{
exec_done_.fill(false);
wgmma_planned_warps_.fill(0);
in_wgmma_.fill(false);
}
~Impl() {}
void reset() {
perf_stats_ = PerfStats();
for (auto& sparse_meta : sparse_meta_) {
std::fill(sparse_meta.begin(), sparse_meta.end(), 0);
}
exec_done_.fill(false);
wgmma_planned_warps_.fill(0);
in_wgmma_.fill(false);
cta_owner_a_.fill(-1);
cta_owner_b_ = -1;
cur_block_ = 0;
}
void tick() {
#ifdef VX_CFG_TCU_WGMMA_ENABLE
// Q-warp lock-step probe.
// Pass 1 — identify active WGMMA blocks and prime each one's plan() on
// first uop. WMMA / META_STORE blocks are unaffected (no Q-coupling).
uint32_t wgmma_active = 0;
for (uint32_t b = 0; b < VX_CFG_NUM_TCU_BLOCKS; ++b) {
auto& input = simobject_->Inputs.at(b);
if (input.empty()) continue;
auto trace = input.peek();
if (!tcu_is_wgmma(std::get<TcuType>(trace->op_type))) continue;
wgmma_active |= (1u << b);
uint32_t wid = trace->wid;
uint64_t wid_bit = (uint64_t(1) << wid);
if (wgmma_planned_warps_.at(b) & wid_bit) continue;
auto& instr = *trace->instr_ptr;
auto tpuArgs = std::get<IntrTcuArgs>(instr.get_args());
if (!(tpuArgs.step_m == 0 && tpuArgs.step_n == 0 && tpuArgs.step_k == 0)) {
// Non-first uop arrived without a prior plan: first uop already drained.
// Mark planned and continue (descriptors persist in lmem_desc_[wid]).
wgmma_planned_warps_.at(b) |= wid_bit;
continue;
}
auto& rs1_data = trace->src_data[0];
auto& rs2_data = trace->src_data[1];
uint32_t a_desc = rs1_data.empty() ? 0 : rs1_data.at(0).u32;
uint32_t b_desc = rs2_data.empty() ? 0 : rs2_data.at(0).u32;
// CTA-overlap fence — defer this block's WGMMA if any other block
// is mid-flight with a different CTA. The shared B buffer assumes
// single-CTA occupancy across all blocks.
int32_t new_cta = (int32_t)core_->scheduler().warp(wid).cta_csrs.cta_id;
bool block_other_cta_inflight = false;
for (uint32_t k = 0; k < VX_CFG_NUM_TCU_BLOCKS; ++k) {
if (k == b) continue;
if (in_wgmma_.at(k) && cta_owner_a_.at(k) != new_cta) {
block_other_cta_inflight = true;
break;
}
}
if (block_other_cta_inflight) {
wgmma_active &= ~(1u << b);
continue;
}
// Drop the shared B buffer only when no other block is mid-WGMMA —
// otherwise we'd evict their resident bytes mid-flight.
bool any_in_wgmma = false;
for (auto v : in_wgmma_) any_in_wgmma = any_in_wgmma || v;
auto& tbuf = simobject_->tbuf();
if (!any_in_wgmma) {
tbuf->invalidate_b();
cta_owner_b_ = -1;
}
// Only drop the per-block A buffer when no warp is currently in flight.
if (!in_wgmma_.at(b)) {
tbuf->invalidate_a(b);
}
this->plan_wgmma_lines(b, wid, a_desc, b_desc, tpuArgs,
std::get<TcuType>(trace->op_type) == TcuType::WGMMA_SP);
if (tbuf->ready_a(b) && tbuf->ready_b()) {
++perf_stats_.tbuf_cache_hits;
}
in_wgmma_.at(b) = true;
wgmma_planned_warps_.at(b) |= wid_bit;
cta_owner_a_.at(b) = new_cta;
if (cta_owner_b_ == -1) cta_owner_b_ = new_cta;
}
// Pass 2 — all active WGMMA blocks must have A/B operands resident
// before any of them advances.
if (wgmma_active != 0) {
uint32_t ready_mask = 0;
auto& tbuf = simobject_->tbuf();
for (uint32_t b = 0; b < VX_CFG_NUM_TCU_BLOCKS; ++b) {
if (!((wgmma_active >> b) & 1u)) continue;
auto trace = simobject_->Inputs.at(b).peek();
auto tpuArgs = std::get<IntrTcuArgs>(trace->instr_ptr->get_args());
bool a_ok = !tpuArgs.is_a_smem || tbuf->ready_a(b);
bool b_ok = tbuf->ready_b();
if (a_ok && b_ok) ready_mask |= (1u << b);
}
if (ready_mask != wgmma_active) {
++perf_stats_.tbuf_stalls;
return; // hold all blocks; per-block dispatch deferred to next tick
}
}
#endif
for (uint32_t b = 0; b < VX_CFG_NUM_TCU_BLOCKS; ++b) {
auto& input = simobject_->Inputs.at(b);
if (input.empty())
continue;
auto trace = input.peek();
auto tcu_type = std::get<TcuType>(trace->op_type);
#ifdef VX_CFG_TCU_WGMMA_ENABLE
// CTA-overlap fence deferred this block — skip until pass 1 plans it.
if (tcu_is_wgmma(tcu_type) &&
!(wgmma_planned_warps_.at(b) & (uint64_t(1) << trace->wid)))
continue;
#endif
// Execute once per trace; results persist across backpressure retries
// via exec_done_[b].
if (!exec_done_.at(b)) {
auto& instr = *trace->instr_ptr;
auto tpuArgs = std::get<IntrTcuArgs>(instr.get_args());
uint32_t wid = trace->wid;
uint32_t num_threads = VX_CFG_NUM_THREADS;
auto& rs1_data = trace->src_data[0];
auto& rs2_data = trace->src_data[1];
auto& rs3_data = trace->src_data[2];
trace->dst_data.assign(num_threads, reg_data_t{});
auto& rd_data = trace->dst_data;
switch (tcu_type) {
case TcuType::WMMA:
case TcuType::WMMA_SP:
this->wmma(wid, tpuArgs.fmt_s, tpuArgs.fmt_d,
tpuArgs.step_m, tpuArgs.step_n, tpuArgs.step_k,
rs1_data, rs2_data, rs3_data, rd_data,
tcu_is_sparse(tcu_type));
break;
#ifdef VX_CFG_TCU_WGMMA_ENABLE
case TcuType::WGMMA:
case TcuType::WGMMA_SP: {
uint32_t a_desc = rs1_data.empty() ? 0 : rs1_data.at(0).u32;
uint32_t b_desc = rs2_data.empty() ? 0 : rs2_data.at(0).u32;
cur_block_ = b;
// CTA lockstep invariant: no block may execute a WGMMA uop for a
// different cta_id while another block is mid-WGMMA.
{
int32_t this_cta = (int32_t)core_->scheduler().warp(wid).cta_csrs.cta_id;
for (uint32_t k = 0; k < VX_CFG_NUM_TCU_BLOCKS; ++k) {
if (k == b) continue;
if (in_wgmma_.at(k) && cta_owner_a_.at(k) != this_cta) {
std::cerr << "*** TCU CTA lockstep violation: block " << b
<< " executing WGMMA cta_id=" << this_cta
<< " while block " << k << " holds cta_id="
<< cta_owner_a_.at(k) << std::endl;
std::abort();
}
}
}
this->wgmma(wid, tpuArgs.fmt_s, tpuArgs.fmt_d,
tpuArgs.step_m, tpuArgs.step_n, tpuArgs.step_k,
a_desc, b_desc, rs1_data, rs3_data, rd_data,
tcu_is_sparse(tcu_type),
tpuArgs.cd_nregs, tpuArgs.is_a_smem);
} break;
#endif
case TcuType::META_STORE:
this->meta_store(wid, tpuArgs.fmt_s, tpuArgs.fmt_d,
tpuArgs.meta_kind, rs1_data);
break;
#ifdef VX_CFG_TCU_SPARSE_ENABLE
case TcuType::TCU_LD: {
// rs1 is a full-width address (.u64); use u64 to avoid truncation on XLEN=64.
uint64_t base_addr = rs1_data.empty() ? 0 : rs1_data.at(0).u64;
this->tcu_ld(wid, tpuArgs.fmt_s, tpuArgs.fmt_d, base_addr);
} break;
#endif
default:
std::abort();
}
exec_done_.at(b) = true;
}
int delay = 0;
switch (tcu_type) {
case TcuType::WMMA:
case TcuType::WMMA_SP:
case TcuType::WGMMA:
case TcuType::WGMMA_SP:
delay = 4;
break;
case TcuType::META_STORE:
delay = 1;
break;
#ifdef VX_CFG_TCU_SPARSE_ENABLE
case TcuType::TCU_LD:
delay = 4;
break;
#endif
default:
std::abort();
}
if (simobject_->Outputs.at(b).try_send(trace, 2 + delay)) {
exec_done_.at(b) = false;
#ifdef VX_CFG_TCU_WGMMA_ENABLE
// Clear this warp's plan bit on its last uop so the next WGMMA
// re-decodes descriptors. Block stays in_wgmma_ until all warps drain.
if (tcu_is_wgmma(tcu_type) && trace->instr_ptr->get_fu_unlock()) {
uint64_t wid_bit = (uint64_t(1) << trace->wid);
wgmma_planned_warps_.at(b) &= ~wid_bit;
if (wgmma_planned_warps_.at(b) == 0) {
in_wgmma_.at(b) = false;
cta_owner_a_.at(b) = -1;
}
}
#endif
DT(3, simobject_->name() << " execute: op=" << tcu_type << ", " << *trace);
input.pop();
}
}
}
// Plan all line addresses required for the current WGMMA's A, B and
// sparse-metadata tiles into the per-role caches inside TcuTbuf.
// Lines already resident or in-flight are skipped (additive plan).
void plan_wgmma_lines(uint32_t b, uint32_t wid,
uint32_t a_desc, uint32_t b_desc,
const IntrTcuArgs& args, bool is_sparse) {
uint32_t fmt_s = args.fmt_s;
bool is_a_smem = args.is_a_smem;
uint32_t e_bits = elem_bits(fmt_s);
if (e_bits < 8) return;
uint32_t e_bytes = e_bits / 8;
// NRC: cd_nregs 0/1/2 → 8/16/32; xtileN = NRC * NT / xtileM.
uint32_t nrc = (args.cd_nregs == 0) ? 8 : (args.cd_nregs == 1) ? 16 : 32;
uint32_t xtile_n = (nrc * VX_CFG_NUM_THREADS) / wg_cfg::xtileM;
lmem_desc_t sd_a{}, sd_b{};
if (is_a_smem) {
sd_a = {uint64_t(VX_MEM_LMEM_BASE_ADDR) + (a_desc & 0xFFFF), (a_desc >> 16) / e_bytes, false};
lmem_desc_[wid][0] = sd_a;
}
sd_b = {uint64_t(VX_MEM_LMEM_BASE_ADDR) + (b_desc & 0xFFFF), (b_desc >> 16) / e_bytes, false};
lmem_desc_[wid][1] = sd_b;
// tileK = xtileK × ratio (ratio = 32/e_bits); sparse compresses K on A only.
uint32_t ratio = 32 / e_bits;
uint32_t tile_k = uint32_t(wg_cfg::xtileK) * ratio;
uint32_t a_k = is_sparse ? (tile_k / 2) : tile_k;
// ldm==0 → block-major layout; ldm!=0 → row-major (stride in elements).
uint32_t k_blk_dim = cfg::tcK * ratio;
uint32_t a_blk_elems = cfg::tcM * k_blk_dim;
uint32_t b_blk_elems = k_blk_dim * cfg::tcN;
uint32_t n_steps = xtile_n / cfg::tcN;
auto& tbuf = simobject_->tbuf();
// Plan A lines (SS mode only): xtileM rows × a_k columns.
if (is_a_smem) {
bool a_block_major = (sd_a.ldm == 0);
std::vector<uint64_t> a_lines;
a_lines.reserve(uint32_t(wg_cfg::xtileM) * a_k);
for (uint32_t r = 0; r < wg_cfg::xtileM; ++r) {
for (uint32_t c = 0; c < a_k; ++c) {
uint64_t elem_off;
if (a_block_major) {
uint32_t m_blk = r / cfg::tcM;
uint32_t i_in = r % cfg::tcM;
uint32_t k_blk = c / k_blk_dim;
uint32_t k_in = c % k_blk_dim;
elem_off = (k_blk * wg_cfg::m_steps + m_blk) * a_blk_elems
+ i_in * k_blk_dim + k_in;
} else {
elem_off = uint64_t(r) * sd_a.ldm + c;
}
uint64_t addr = sd_a.base + elem_off * e_bytes;
a_lines.push_back(addr & ~uint64_t(VX_CFG_MEM_BLOCK_SIZE - 1));
}
}
tbuf->plan_a(b, a_lines);
// Sparse metadata is preloaded into sparse_meta_ via TCU_LD;
// no metadata lines are planned through tbuf here.
}
// Plan B lines: always dense in K, tileK rows × xtileN columns.
// ldm == 0 → block-major; ldm != 0 → K-major (smem[n*ldm + k]).
bool b_block_major = (sd_b.ldm == 0);
std::vector<uint64_t> b_lines;
b_lines.reserve(tile_k * xtile_n);
for (uint32_t r = 0; r < tile_k; ++r) {
for (uint32_t c = 0; c < xtile_n; ++c) {
uint64_t elem_off;
if (b_block_major) {
uint32_t k_blk = r / k_blk_dim;
uint32_t r_in = r % k_blk_dim;
uint32_t n_blk = c / cfg::tcN;
uint32_t n_in = c % cfg::tcN;
// Within-block layout: N outer, K inner.
elem_off = (k_blk * n_steps + n_blk) * b_blk_elems
+ n_in * k_blk_dim + r_in;
} else {
elem_off = uint64_t(c) * sd_b.ldm + r;
}
uint64_t addr = sd_b.base + elem_off * e_bytes;
b_lines.push_back(addr & ~uint64_t(VX_CFG_MEM_BLOCK_SIZE - 1));
}
}
tbuf->plan_b(b_lines);
}
void meta_store(uint32_t wid,
uint32_t fmt_s,
uint32_t col_idx,
uint32_t meta_kind,
const std::vector<reg_data_t>& rs1_data) {
uint32_t num_cols = meta_num_cols(fmt_s);
if (meta_kind == TCU_META_KIND_SPARSE_WG) {
// WGMMA sparse: thread mapping src_idx = col_in_group * kMetaBanks + bank.
// Banks are selected by {step_m, step_k_half} so m=1 → bank (cfg::k_steps/2).
constexpr uint32_t wg_cols_per_load = VX_CFG_NUM_THREADS / kMetaBanks;
uint32_t group = col_idx;
uint32_t col_begin = group * wg_cols_per_load;
uint32_t col_end = std::min(col_begin + wg_cols_per_load, num_cols);
for (uint32_t col = col_begin; col < col_end; ++col) {
uint32_t col_in_group = col - col_begin;
for (uint32_t bank = 0; bank < kMetaBanks; ++bank) {
uint32_t src_idx = col_in_group * kMetaBanks + bank;
sparse_meta_.at(wid).at(bank * kMaxMetaCols + col) =
rs1_data.at(src_idx).u32;
}
}
return;
}
uint32_t total_stores = vt::sparse_meta_total_store_uops(fmt_s, cfg::stores_per_col, VX_CFG_NUM_THREADS, cfg::meta_cols_per_load);
if (col_idx >= total_stores) {
// Flat per-thread store (fallback)
for (uint32_t t = 0; t < rs1_data.size(); ++t) {
sparse_meta_.at(wid).at(t) = rs1_data.at(t).u32;
}
return;
}
if constexpr (cfg::stores_per_col > 1) {
// col_idx enumerates (col, store_in_col) pairs;
// each store covers banks_per_store consecutive banks of one column.
uint32_t col = col_idx / cfg::stores_per_col;
uint32_t store_in_col = col_idx % cfg::stores_per_col;
if (col >= num_cols) return;
uint32_t bank_base = store_in_col * cfg::banks_per_store;
for (uint32_t t = 0; t < cfg::banks_per_store; ++t) {
uint32_t bank = bank_base + t;
if (bank >= kMetaBanks) break;
sparse_meta_.at(wid).at(bank * kMaxMetaCols + col) = rs1_data.at(t).u32;
}
return;
}
// NT >= per_warp_depth: col_idx enumerates column groups; each group covers
// meta_cols_per_load columns across all banks.
uint32_t group = col_idx;
uint32_t col_begin = group * cfg::meta_cols_per_load;
uint32_t col_end = std::min(col_begin + cfg::meta_cols_per_load, num_cols);
for (uint32_t col = col_begin; col < col_end; ++col) {
uint32_t col_in_group = col - col_begin;
uint32_t thread_offset = col_in_group * kMetaBanks;
for (uint32_t bank = 0; bank < kMetaBanks; ++bank) {
uint32_t src_idx = thread_offset + bank;
sparse_meta_.at(wid).at(bank * kMaxMetaCols + col) =
rs1_data.at(src_idx).u32;
}
}
}
#ifdef VX_CFG_TCU_SPARSE_ENABLE
// TCU_LD — warp-level sparse-metadata load.
// Emits per-lane reads (shared or device memory) and fills sparse_meta_[wid].
// slot_idx selects the column-group (0 for the first load, 1 for the second).
void tcu_ld(uint32_t wid,
uint32_t fmt_s,
uint32_t slot_idx,
uint64_t base_addr) {
(void)fmt_s;
auto& lmem = *core_->local_mem();
auto* memsim = core_->processor()->memsim();
// Map lane T to its (bank, col) metadata cell using the host pack layout:
// flat_store = slot*cols_per_load + T/BPS
// col = flat_store / stores_per_col
// bank = (flat_store % stores_per_col)*BPS + T%BPS
// This formula covers NT < kMetaBanks (stores_per_col > 1) as well as NT >= kMetaBanks.
constexpr uint32_t PWD = kMetaBanks;
constexpr uint32_t BPS = cfg::banks_per_store;
constexpr uint32_t SPC = cfg::stores_per_col;
constexpr uint32_t CPL = cfg::meta_cols_per_load;
for (uint32_t T = 0; T < VX_CFG_NUM_THREADS; ++T) {
uint32_t store_in_load = T / BPS;
uint32_t thread_in_store = T % BPS;
uint32_t flat_store = slot_idx * CPL + store_in_load;
uint32_t col = flat_store / SPC;
uint32_t store_in_col = flat_store % SPC;
uint32_t bank = store_in_col * BPS + thread_in_store;
if (bank >= PWD || col >= kMaxMetaCols)
continue;
// base_addr is pre-advanced per slot by the caller; read lane T at base_addr + T*4.
uint64_t word_idx = T;
uint64_t addr = base_addr + word_idx * 4;
// Route to shared memory or device memory based on address type.
uint32_t word = (get_addr_type(addr) == AddrType::Shared)
? lmem.read_word(addr)
: memsim->read_word(addr);
sparse_meta_.at(wid).at(bank * kMaxMetaCols + col) = word;
// Trace: META_SRAM write (CSV: wid,bank,col,addr,value).
if (const char* p = std::getenv("VORTEX_TCU_TRACE")) {
if (p[0] == '1') {
fprintf(stderr, "META_TRC,%u,%u,%u,0x%lx,0x%08x\n",
wid, bank, col, (unsigned long)addr, word);
}
}
}
}
#endif
void wmma(uint32_t wid,
uint32_t fmt_s,
uint32_t fmt_d,
uint32_t step_m,
uint32_t step_n,
uint32_t step_k,
const std::vector<reg_data_t>& rs1_data,
const std::vector<reg_data_t>& rs2_data,
const std::vector<reg_data_t>& rs3_data,
std::vector<reg_data_t>& rd_data,
bool is_sparse) {
if (is_sparse) {
if (!vt::sparse_format_supported(fmt_s)) {
std::cout << "Error: WMMA_SP unsupported input format: "
<< vt::fmt_string(fmt_s) << " (id=" << fmt_s
<< "). Supported formats: i8, u8, fp8, bf8, fp16, bf16, i4, u4." << std::endl;
std::abort();
}
if ((VX_CFG_NUM_THREADS % cfg::b_block_size_sp) != 0) {
std::cout << "Error: VX_CFG_NUM_THREADS must be divisible by sparse B block size" << std::endl;
std::abort();
}
}
uint32_t a_off = (step_m % cfg::a_sub_blocks) * cfg::a_block_size;
uint32_t b_off = is_sparse
? (step_n % cfg::b_sub_blocks_sp) * cfg::b_block_size_sp
: (step_n % cfg::b_sub_blocks) * cfg::b_block_size;
// Prepare A tile [tcM][tcK]
reg_data_t a_tile[cfg::tcM * cfg::tcK];
for (uint32_t i = 0; i < cfg::tcM; ++i) {
for (uint32_t z = 0; z < cfg::tcK; ++z) {
a_tile[i * cfg::tcK + z] = rs1_data.at(a_off + i * cfg::tcK + z);
}
}
// Prepare B tile [tcM][tcN][tcK]
reg_data_t b_tile[cfg::tcM * cfg::tcN * cfg::tcK];
if (is_sparse) {
constexpr uint32_t kCompression = 2;
uint32_t ebits = elem_bits(fmt_s);
uint32_t meta_bits = 32 / ebits;
uint32_t bank = step_m * (cfg::k_steps / 2) + step_k;
auto meta_bit = [&](uint32_t bit_idx) {
return (sparse_meta_.at(wid).at(bank * kMaxMetaCols + bit_idx / 32) >> (bit_idx % 32)) & 1u;
};
for (uint32_t i = 0; i < cfg::tcM; ++i) {
uint32_t row_base = i * meta_row_width(ebits);
for (uint32_t j = 0; j < cfg::tcN; ++j) {
uint32_t j_sp = cfg::sym_sparse ? (j % (cfg::tcN / 2)) : j;
for (uint32_t z = 0; z < cfg::tcK; ++z) {
uint32_t b_idx = b_off + j_sp * cfg::tcK * kCompression + z * kCompression;
uint32_t lo = 0, hi = 0;
for (uint32_t b = 0; b < meta_bits; ++b) {
lo |= meta_bit(row_base + meta_bits * z + b) << b;
hi |= meta_bit(row_base + meta_bits * (cfg::tcK + z) + b) << b;
}
b_tile[(i * cfg::tcN + j) * cfg::tcK + z].u32 =
gather_sparse(rs2_data.at(b_idx).u32, rs2_data.at(b_idx + 1).u32, lo, hi, ebits);
}
}
}
} else {
for (uint32_t i = 0; i < cfg::tcM; ++i) {
for (uint32_t j = 0; j < cfg::tcN; ++j) {
for (uint32_t z = 0; z < cfg::tcK; ++z) {
b_tile[(i * cfg::tcN + j) * cfg::tcK + z] = rs2_data.at(b_off + j * cfg::tcK + z);
}
}
}
}
fedp_tile(wid, step_m, step_n, step_k, fmt_s, fmt_d,
a_tile, b_tile, rs3_data, rd_data);
}
void wgmma(uint32_t wid,
uint32_t fmt_s,
uint32_t fmt_d,
uint32_t step_m,
uint32_t step_n,
uint32_t step_k,
uint32_t a_desc,
uint32_t b_desc,
const std::vector<reg_data_t>& rs1_data,
const std::vector<reg_data_t>& rs3_data,
std::vector<reg_data_t>& rd_data,
bool is_sparse,
uint32_t cd_nregs,
uint32_t is_a_smem) {
__unused(cd_nregs);
if (is_sparse && !vt::sparse_format_supported(fmt_s)) {
std::cout << "Error: WGMMA_SP unsupported input format: "
<< vt::fmt_string(fmt_s) << " (id=" << fmt_s << ")" << std::endl;
std::abort();
}
uint32_t ratio = elem_ratio(fmt_s);
uint32_t k_words = cfg::tcK;
uint32_t e_bytes = elem_bits(fmt_s) / 8;
// Decode smem descriptors (B always from smem, A optionally).
lmem_desc_t sd_a, sd_b;
if (step_k == 0 && step_m == 0 && step_n == 0) {
if (is_a_smem) {
sd_a = {uint64_t(VX_MEM_LMEM_BASE_ADDR) + (a_desc & 0xFFFF), (a_desc >> 16) / e_bytes, false};
lmem_desc_[wid][0] = sd_a;
}
sd_b = {uint64_t(VX_MEM_LMEM_BASE_ADDR) + (b_desc & 0xFFFF), (b_desc >> 16) / e_bytes, false};
lmem_desc_[wid][1] = sd_b;
} else {
sd_a = lmem_desc_[wid][0];
sd_b = lmem_desc_[wid][1];
}
// load_lmem_word distinguishes A from B by descriptor base.
cur_a_desc_base_ = is_a_smem ? sd_a.base : ~uint64_t(0);
// NRC: cd_nregs 0/1/2 → 8/16/32; xtileN = NRC * NT / xtileM.
{
uint32_t nrc = (cd_nregs == 0) ? 8 : (cd_nregs == 1) ? 16 : 32;
cur_xtile_n_ = (nrc * VX_CFG_NUM_THREADS) / wg_cfg::xtileM;
}
// Prepare A tile [tcM][tcK]
reg_data_t a_tile[cfg::tcM * cfg::tcK];
for (uint32_t i = 0; i < cfg::tcM; ++i) {
uint32_t a_row_idx = step_m * cfg::tcM + i;
for (uint32_t z = 0; z < k_words; ++z) {
if (is_a_smem) {
uint32_t k_elem = (step_k * k_words + z) * ratio;
a_tile[i * cfg::tcK + z].u32 = load_lmem_word(sd_a, a_row_idx, k_elem, fmt_s, false);
} else {
a_tile[i * cfg::tcK + z] = rs1_data.at(i * cfg::tcK + z);
}
}
}
// Prepare B tile [tcM][tcN][tcK]
reg_data_t b_tile[cfg::tcM * cfg::tcN * cfg::tcK];
if (is_sparse) {
uint32_t ebits = elem_bits(fmt_s);
uint32_t rtl_i_ratio = 32 / ebits;
uint32_t meta_row_w = k_words * 2 * rtl_i_ratio;
// Bank encoding {step_m, step_k_half}: m=1 → bank (cfg::k_steps/2).
uint32_t wg_bank = step_m * (cfg::k_steps / 2) + step_k;
// Metadata is preloaded into sparse_meta_ via TCU_LD before dispatch.
auto meta_bit_wg = [&](uint32_t bit_idx) -> uint32_t {
uint32_t word_idx = wg_bank * kMaxMetaCols + bit_idx / 32;
uint32_t word_val = sparse_meta_.at(wid).at(word_idx);
// Trace: META_RD (CSV: wid,step_m,step_k,bank,value).
if (const char* p = std::getenv("VORTEX_TCU_TRACE")) {
if (p[0] == '1' && (bit_idx % 32) == 0) {
fprintf(stderr, "META_RD,%u,%u,%u,%u,0x%08x\n",
wid, step_m, step_k, wg_bank, word_val);
}
}
return (word_val >> (bit_idx % 32)) & 1u;
};
for (uint32_t i = 0; i < cfg::tcM; ++i) {
uint32_t row_base = i * meta_row_w;
for (uint32_t j = 0; j < cfg::tcN; ++j) {
uint32_t b_col_idx = step_n * cfg::tcN + j;
for (uint32_t z = 0; z < k_words; ++z) {
uint32_t lo = 0, hi = 0;
for (uint32_t b = 0; b < rtl_i_ratio; ++b) {
lo |= meta_bit_wg(row_base + rtl_i_ratio * z + b) << b;
hi |= meta_bit_wg(row_base + rtl_i_ratio * (k_words + z) + b) << b;
}
constexpr uint32_t kCompression = 2;
uint32_t k_elem_b0 = (step_k * k_words + z) * ratio * kCompression;
uint32_t bword0 = load_lmem_word(sd_b, k_elem_b0, b_col_idx, fmt_s, true);
uint32_t bword1 = load_lmem_word(sd_b, k_elem_b0 + ratio, b_col_idx, fmt_s, true);
uint32_t gathered = gather_sparse(bword0, bword1, lo, hi, ebits);
b_tile[(i * cfg::tcN + j) * cfg::tcK + z].u32 = gathered;
// Trace: B-gather (CSV: wid,step_m,step_n,i,lane,bword0,bword1,lo,hi,gathered).
if (const char* p = std::getenv("VORTEX_TCU_TRACE")) {
if (p[0] == '1') {
fprintf(stderr, "GATHER,%u,%u,%u,%u,%u,0x%08x,0x%08x,%u,%u,0x%08x\n",
wid, step_m, step_n, i, j*cfg::tcK+z,
bword0, bword1, lo, hi, gathered);
}
}
}
}
}
} else {
for (uint32_t i = 0; i < cfg::tcM; ++i) {
for (uint32_t j = 0; j < cfg::tcN; ++j) {
uint32_t b_col_idx = step_n * cfg::tcN + j;
for (uint32_t z = 0; z < k_words; ++z) {
uint32_t k_elem = (step_k * k_words + z) * ratio;
b_tile[(i * cfg::tcN + j) * cfg::tcK + z].u32 =
load_lmem_word(sd_b, k_elem, b_col_idx, fmt_s, true);
}
}
}
}
fedp_tile(wid, step_m, step_n, step_k, fmt_s, fmt_d,
a_tile, b_tile, rs3_data, rd_data);
__unused(b_desc);
}
const PerfStats& perf_stats() const {
// lmem_reads: total MemReq traffic from TcuTbuf (abuf + bbuf).
perf_stats_.lmem_reads = simobject_->tbuf()->reads();
return perf_stats_;
}
private:
uint32_t elem_bits(uint32_t fmt_s) const {
switch (fmt_s) {
case vt::fp32::id:
case vt::tf32::id:
case vt::int32::id:
return 32;
case vt::fp16::id:
case vt::bf16::id: