Repository navigation
Expand file tree
/
Copy pathmodel.cpp
More file actions
1666 lines (1630 loc) · 91.7 KB
/
Copy pathmodel.cpp
File metadata and controls
1666 lines (1630 loc) · 91.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
#include "model.hpp"
#include "card_check.hpp"
#include "env.hpp"
#include "expert_residency.hpp"
#include "rope_gguf.hpp"
#include "store.hpp"
#if FNX_HIP
#include "hip/selftest_policy.hpp"
#include "selftest.hpp"
#endif
#include <cuda_runtime.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <algorithm>
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <type_traits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <thread>
namespace fnx {
namespace {
void ck(cudaError_t e, const char* what) {
if (e != cudaSuccess) throw std::runtime_error(std::string(what) + ": " + cudaGetErrorString(e));
}
// a card's PCI address in sysfs's spelling
std::string card_bdf(int dev) {
char bus[32];
ck(cudaDeviceGetPCIBusId(bus, sizeof bus, dev), "cudaDeviceGetPCIBusId");
try {
return sysfs_bdf(bus);
} catch (const std::exception& e) {
throw std::runtime_error("card " + std::to_string(dev) + ": " + e.what());
}
}
// the ring stores straight into the next card's memory: with translated IOMMU domains every such write faults
// (dmesg: "DMAR: ... PTE Write access is not set") and the first window ends in a ring timeout that names nothing.
// Each card's IOMMU group must be identity (boot with iommu=pt); a card whose iommu_group link is absent has no IOMMU
// in the way. Only absent: a link that cannot be read or points nowhere (iommu_groups hidden from this process) is
// refused, as a translated domain behind it would look the same
void require_identity_iommu(int dev, const std::string& dir) {
if (access(dir.c_str(), F_OK) != 0)
throw std::runtime_error("card " + std::to_string(dev) + ": no " + dir + " to read its IOMMU group from");
const std::string link = dir + "/iommu_group", path = link + "/type";
struct stat st;
if (lstat(link.c_str(), &st) != 0) {
if (errno == ENOENT) return;
throw std::runtime_error("card " + std::to_string(dev) + ": cannot read " + link + ": " + std::strerror(errno));
}
std::FILE* f = std::fopen(path.c_str(), "r");
char type[64] = {0};
if (!f || !std::fgets(type, sizeof type, f)) {
if (f) std::fclose(f);
throw std::runtime_error("card " + std::to_string(dev) + ": cannot read " + path);
}
std::fclose(f);
std::string t(type);
while (!t.empty() && (t.back() == '\n' || t.back() == ' ')) t.pop_back();
if (t != "identity")
throw std::runtime_error("card " + std::to_string(dev) + " is in an IOMMU group of type '" + t + "' (" + path +
"): the ring's writes between cards need identity domains; boot with iommu=pt");
}
// the card's BAR1 from sysfs, held to its memory (require_bar1_covers)
void check_bar1(int dev, const std::string& bdf, const std::string& dir, const cudaDeviceProp& prop) {
const std::string path = dir + "/resource";
std::FILE* f = std::fopen(path.c_str(), "r");
if (!f) throw std::runtime_error("card " + std::to_string(dev) + ": cannot open " + path + " to read its BAR1 from");
std::string text;
char buf[4096];
for (size_t n; (n = std::fread(buf, 1, sizeof buf, f)) > 0;) text.append(buf, n);
const bool bad = std::ferror(f);
std::fclose(f);
if (bad) throw std::runtime_error("card " + std::to_string(dev) + ": cannot read " + path);
require_bar1_covers(dev, bdf, prop.name, bar1_bytes(text, path), prop.totalGlobalMem, NCARD);
}
// the Makefile passes the arch the kernels were compiled for (ARCH): a default here would let a card of another one
// run them through the driver's PTX compiler unchecked
#ifndef FNX_ARCH
#error "FNX_ARCH is unset: build through the Makefile (make ARCH=70 or 86)"
#endif
// The cards are checked before anything is placed on them (src/card_check.hpp has the rules): a card that fails one
// would otherwise take the whole load first and then fail far from the cause, an sm_70 build on 24 GB cards out of
// memory, a short BAR1 as a ring that never completes. The IOMMU check comes before BAR1's, as it is the one that
// refuses a card with no sysfs directory: BAR1's would only say it cannot open the file
void check_cards() {
const bool skip_bar1 = env_flag("FNX_SKIP_BAR1_CHECK");
if (skip_bar1) std::fprintf(stderr, "fnx: FNX_SKIP_BAR1_CHECK=1: the cards' BAR1 is not checked against their memory\n");
for (int dev = 0; dev < NCARD; ++dev) {
cudaDeviceProp prop;
ck(cudaGetDeviceProperties(&prop, dev), "cudaGetDeviceProperties");
#if FNX_HIP
// the code objects in the binary: gfx1201 (R9700, RX 9070; run), gfx1200 (RX 9060) and gfx1100..1103 (RDNA3; not run on a multi-card PC): the card's gfx name, not a
// compute capability. Any other card would find no kernels
{
bool built = false;
for (const char* a : {"gfx1201", "gfx1200", "gfx1100", "gfx1101", "gfx1102", "gfx1103"})
if (std::strncmp(prop.gcnArchName, a, 7) == 0) built = true;
if (!built)
throw std::runtime_error("card " + std::to_string(dev) + " (" + prop.name + ") is " + prop.gcnArchName +
" and this build has kernels for gfx1201, gfx1200 and gfx1100..gfx1103 only");
}
(void) FNX_ARCH;
#else
require_card_arch(dev, prop.name, prop.major, prop.minor, FNX_ARCH);
#endif
#if FNX_HOSTRING
(void) skip_bar1; // no card writes into another: the identity-domain and BAR1 requirements do not apply
#else
const std::string bdf = card_bdf(dev), dir = "/sys/bus/pci/devices/" + bdf;
require_identity_iommu(dev, dir);
if (!skip_bar1) check_bar1(dev, bdf, dir, prop);
#endif
}
#if FNX_HIP
// a card Strata has not run the split on (anything but gfx1201): say so once, then check its kernels against a scalar reference before the model is loaded (hip/selftest_policy.hpp)
{
cudaDeviceProp prop;
ck(cudaGetDeviceProperties(&prop, 0), "cudaGetDeviceProperties");
const SelftestDecision d = selftest_decide(prop.gcnArchName, std::getenv("FNX_SELFTEST"), std::getenv("FNX_SELFTEST_FAIL"), [&](std::string& covered) {
for (int dev = 0; dev < NCARD; ++dev) {
std::string c;
const std::string bad = selftest_card(dev, c);
if (!bad.empty()) return "card " + std::to_string(dev) + ": " + bad;
covered += (covered.empty() ? "" : "; ") + c;
}
return std::string();
});
for (const std::string& line : d.lines) std::fprintf(stderr, "fnx: %s\n", line.c_str());
if (!d.ok) throw std::runtime_error("the kernel self-test failed on " + std::string(prop.gcnArchName).substr(0, std::string(prop.gcnArchName).find(':')) + ", the split engine does not run on this card");
}
#endif
}
float f16_to_f32(uint16_t h) {
const uint32_t s = (h >> 15) & 1, e = (h >> 10) & 31, m = h & 1023;
uint32_t u;
if (e == 0) {
if (m == 0) u = s << 31;
else {
int ee = 0;
uint32_t mm = m;
while (!(mm & 1024)) { mm <<= 1; --ee; }
u = (s << 31) | ((uint32_t) (ee + 127 - 14) << 23) | ((mm & 1023) << 13);
}
} else if (e == 31) u = (s << 31) | 0x7f800000u | (m << 13);
else u = (s << 31) | ((e + 112) << 23) | (m << 13);
float f;
std::memcpy(&f, &u, 4);
return f;
}
uint16_t f32_to_f16(float x) {
uint32_t u;
std::memcpy(&u, &x, 4);
const uint32_t s = (u >> 16) & 0x8000u;
const int e = (int) ((u >> 23) & 0xff) - 127 + 15;
uint32_t m = u & 0x7fffffu;
if (e <= 0) { // subnormal or zero in f16
if (e < -10) return (uint16_t) s;
m |= 0x800000u;
const int shift = 14 - e;
uint32_t h = m >> shift;
if ((m >> (shift - 1)) & 1u && ((m & ((1u << (shift - 1)) - 1)) || (h & 1u))) ++h;
return (uint16_t) (s | h);
}
if (e >= 31) return (uint16_t) (s | 0x7c00u);
uint32_t h = ((uint32_t) e << 10) | (m >> 13);
if ((m >> 12) & 1u && ((m & 0xfffu) || (h & 1u))) ++h; // nearest, ties to even
return (uint16_t) (s | h);
}
uint16_t f32_to_bf16(float x) {
uint32_t u;
std::memcpy(&u, &x, 4);
return (uint16_t) ((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
}
// an F32 tensor's first n values, read on the host
const float* f32_data(const Tensor& t, size_t n) {
if (t.type != T_F32) throw std::runtime_error(t.name + " is not F32");
if (t.nbytes < n * sizeof(float)) throw std::runtime_error(t.name + " holds fewer than " + std::to_string(n) + " values");
return (const float*) t.data;
}
// a tensor's range of the mapping locked, LOCKERS threads each faulting in a part: one thread faults a cold file in
// readahead-sized pieces (1.1 GB/s from the NVMe), 16 side by side reached 2.3 (tools/stage_test.cpp)
void lock_in_ram(const Tensor& t) {
constexpr int LOCKERS = 16;
constexpr uintptr_t PG = 4096;
const uintptr_t a = (uintptr_t) t.data / PG * PG, e = (uintptr_t) t.data + t.nbytes;
const uintptr_t part = ((e - a) / LOCKERS + PG - 1) / PG * PG;
std::vector<int> err(LOCKERS, 0);
std::vector<std::thread> th;
for (int i = 0; i < LOCKERS; ++i)
th.emplace_back([&, i] {
const uintptr_t lo = std::min(e, a + (uintptr_t) i * part), hi = std::min(e, lo + part);
if (hi > lo && mlock((const void*) lo, hi - lo) != 0) {
err[(size_t) i] = errno;
#if FNX_HIP
// HIP build: a user's RLIMIT_MEMLOCK is often 8 MiB and cannot be raised without root. The table then stays in the page
// cache unlocked (it is a file mapping: an evicted page is read again); the 16 threads still fault it in side by side
volatile unsigned char sink = 0;
for (uintptr_t p = lo; p < hi; p += PG) sink = sink + *(const volatile unsigned char*) p;
(void) sink;
#endif
}
});
for (auto& x : th) x.join();
#if FNX_HIP
for (int en : err)
if (en) {
std::fprintf(stderr, "fnx: mlock of %s (%zu MiB) failed: %s; the table stays unlocked in the page cache (raise RLIMIT_MEMLOCK to lock it)\n",
t.name.c_str(), (size_t) (t.nbytes >> 20), std::strerror(en));
break;
}
return;
#endif
for (int en : err)
if (en)
throw std::runtime_error("fnx: mlock of " + t.name + " (" + std::to_string(t.nbytes >> 20) + " MiB) failed: " + std::strerror(en) +
"; run with RLIMIT_MEMLOCK raised (systemd-run -p LimitMEMLOCK=infinity)");
}
// the stager's groups for load_card: group l every tensor of layer l, group NL the output head's. The head is read
// through the page cache: the draft layer (Mtp) takes its rows of output.weight from the mapping after the load
std::vector<Stager::Group> stage_groups(const Gguf& g) {
#if FNX_27B
// and group NL + 1 the draft layer, blk.NL (Flash-Next's comes from separate files, Mtp)
std::vector<Stager::Group> gs(NL + 2);
for (const auto& kv : g.tensors()) {
int l = -1, n = 0;
if (std::sscanf(kv.first.c_str(), "blk.%d.%n", &l, &n) == 1 && n > 0 && l >= 0 && l <= NL)
gs[(size_t) (l < NL ? l : NL + 1)].names.push_back(kv.first);
}
gs[NL].names = {"output_norm.weight", "output.weight"};
#else
std::vector<Stager::Group> gs(NL + 1);
for (const auto& kv : g.tensors()) {
int l = -1, n = 0;
if (std::sscanf(kv.first.c_str(), "blk.%d.%n", &l, &n) == 1 && n > 0 && l >= 0 && l < NL) gs[(size_t) l].names.push_back(kv.first);
}
gs[NL].names = {"output_hc_norm.weight", "output_hc_down.weight", "output_hc_up.weight", "output.weight"};
#endif
gs[NL].cached = true;
return gs;
}
// the column slicers take block indices that the callers count in 32-value blocks (ff0 / 32, HS / 32, HD / 32)
TypeInfo block32(const Tensor& t) {
const TypeInfo ti = type_info(t.type);
if (ti.block != 32) throw std::runtime_error(t.name + ": sliced by columns, which needs 32-value blocks, not " + std::to_string(ti.block));
return ti;
}
struct Up {
CardW& c;
Store* store = nullptr; // the lab's holder: the shared memory instead of cudaMalloc / cudaHostAlloc
std::vector<void*>* track = nullptr; // a worker placing its experts: what it allocates (Model::free_experts)
template <typename T> T* put(const void* host, size_t bytes) {
void* d = nullptr;
if (store) d = store->dev(c.dev, bytes);
else ck(cudaMalloc(&d, bytes), "cudaMalloc");
if (track) track->push_back(d);
ck(cudaMemcpy(d, host, bytes, cudaMemcpyDefault), "upload");
c.bytes += bytes;
return (T*) d;
}
// a norm as the window reads it: exactly n floats. A tensor of another width would feed garbage rows silently;
// the whole tensor must match, so extra rows fail too (the window never reads them)
float* f32(const Tensor& t, size_t n) {
if (t.type != T_F32) throw std::runtime_error(t.name + " is not F32");
if (t.nbytes != n * 4)
throw std::runtime_error(t.name + " holds " + std::to_string(t.nbytes / 4) + " floats, not the " + std::to_string(n) +
" the window reads it at");
return put<float>(t.data, t.nbytes);
}
// where a tensor's routed experts are assembled: with an ExpertCache (host_of) their mapped page-locked host copy,
// else tmp
uint8_t* expert_buf(size_t bytes, const char** host_of, std::vector<uint8_t>& tmp) {
if (!host_of) {
tmp.resize(bytes);
return tmp.data();
}
void* hp = nullptr;
const cudaError_t err = store ? (hp = store->host(c.dev, bytes), cudaSuccess) : cudaHostAlloc(&hp, bytes, cudaHostAllocMapped);
if (err != cudaSuccess)
throw std::runtime_error("place: " + std::to_string(bytes >> 20) + " MiB of page-locked host memory for experts: " +
cudaGetErrorString(err));
c.host_bytes += bytes;
return (uint8_t*) hp;
}
// routed experts (h from expert_buf: every expert's matrix, stride bytes apart): to VRAM at p, or with an
// ExpertCache each one's address in host_of (the layer's row of the card's host addresses)
void place(DW& w, const uint8_t* h, size_t bytes, size_t stride, const char** host_of) {
if (!host_of) {
w.p = put<uint8_t>(h, bytes);
return;
}
void* dp = nullptr;
if (store) dp = (void*) h; // the shared host memory has one address for the CPU and the cards
else ck(cudaHostGetDevicePointer(&dp, (void*) h, 0), "place: mapped pointer");
for (size_t e = 0; e < bytes / stride; ++e) host_of[e] = (const char*) dp + e * stride;
}
// the card's ExpertCache: a slot for each resident expert (res) in its size's pool, filled from its host copy; the
// layers' tables. With arena_score (FNX_ARENA) the largest pool's slots go coldest first and are neither allocated
// nor filled here: the engine puts them in its arena (Engine::arena_fill); until then its experts point at RAM
void expert_cache(const std::vector<std::vector<char>>& res, const std::vector<const char*>& hgu, const std::vector<const char*>& hdn,
const std::vector<double>* arena_score) {
ExpertCache& ec = c.ec;
std::vector<std::pair<size_t, size_t>> sizes; // each pool's (gate|up, down) bytes
for (int l = 0; l < NL; ++l) {
const LayerW& L = c.L[(size_t) l];
if (L.gu_e_stride % 16 || L.down_e_stride % 16) throw std::runtime_error("expert cache: expert strides not in 16-byte units");
const std::pair<size_t, size_t> s{L.gu_e_stride, L.down_e_stride};
auto it = std::find(sizes.begin(), sizes.end(), s);
if (it == sizes.end()) {
if (sizes.size() == EC_POOLS) throw std::runtime_error("expert cache: more than " + std::to_string(EC_POOLS) + " expert sizes");
sizes.push_back(s);
it = sizes.end() - 1;
}
ec.pool_of[l] = (int) (it - sizes.begin());
}
ec.n_pools = (int) sizes.size();
if (arena_score) { // the largest pool by bytes
size_t most = 0;
for (int p = 0; p < ec.n_pools; ++p) {
size_t b = 0;
for (int l = 0; l < NL; ++l) b += ec.pool_of[l] == p ? (size_t) NEXP * (sizes[(size_t) p].first + sizes[(size_t) p].second) : 0;
if (b > most) most = b, ec.arena_pool = p;
}
}
std::vector<int> owner, slot_of((size_t) NL * NEXP, -1);
for (int p = 0; p < ec.n_pools; ++p) {
ec.s0[p] = (int) owner.size();
for (int l = 0; l < NL; ++l)
if (ec.pool_of[l] == p)
for (int e = 0; e < NEXP; ++e)
if (res[(size_t) l][(size_t) e]) owner.push_back(l * NEXP + e);
if (p == ec.arena_pool)
std::stable_sort(owner.begin() + ec.s0[p], owner.end(),
[&](int a, int b) { return (*arena_score)[(size_t) a] < (*arena_score)[(size_t) b]; });
for (int s = ec.s0[p]; s < (int) owner.size(); ++s) slot_of[(size_t) owner[(size_t) s]] = s;
ec.n[p] = (int) owner.size() - ec.s0[p];
if (ec.n[p] < EXPERT_WINDOW_SLOTS)
throw std::runtime_error("expert cache: pool " + std::to_string(p) + " has " + std::to_string(ec.n[p]) + " slots, a window needs " +
std::to_string(EXPERT_WINDOW_SLOTS) + " (fewer experts in RAM)");
ec.dn_off[p] = sizes[(size_t) p].first;
ec.slot_bytes[p] = sizes[(size_t) p].first + sizes[(size_t) p].second;
if (p == ec.arena_pool) continue;
ck(cudaMalloc(&ec.pool[p], (size_t) ec.n[p] * ec.slot_bytes[p]), "expert cache: slots");
if (track) track->push_back(ec.pool[p]);
c.bytes += (size_t) ec.n[p] * ec.slot_bytes[p];
}
ec.n_slots = (int) owner.size();
ec.arena_full = arena_score && ec.n_slots == NL * NEXP;
std::vector<const char*> epg(hgu), epd(hdn);
for (int s = 0; s < ec.n_slots; ++s) {
const int i = owner[(size_t) s], p = ec.pool_of[i / NEXP];
if (p == ec.arena_pool) continue;
const LayerW& L = c.L[(size_t) (i / NEXP)];
char* at = ec.pool[p] + (size_t) (s - ec.s0[p]) * ec.slot_bytes[p];
ck(cudaMemcpy(at, hgu[(size_t) i], L.gu_e_stride, cudaMemcpyDefault), "expert cache: fill");
ck(cudaMemcpy(at + ec.dn_off[p], hdn[(size_t) i], L.down_e_stride, cudaMemcpyDefault), "expert cache: fill");
epg[(size_t) i] = at;
epd[(size_t) i] = at + ec.dn_off[p];
}
ec.ep_gu = put<const char*>(epg.data(), epg.size() * sizeof(char*));
ec.ep_dn = put<const char*>(epd.data(), epd.size() * sizeof(char*));
ec.host_gu = put<const char*>(hgu.data(), hgu.size() * sizeof(char*));
ec.host_dn = put<const char*>(hdn.data(), hdn.size() * sizeof(char*));
c.ec_host_gu = hgu;
c.ec_host_dn = hdn;
ec.slot_of = put<int>(slot_of.data(), slot_of.size() * sizeof(int));
c.ec_owner0 = owner;
c.ec_slot0 = slot_of;
ec.owner = put<int>(owner.data(), owner.size() * sizeof(int));
const std::vector<unsigned char> ref((size_t) ec.n_slots, 0);
ec.ref = put<unsigned char>(ref.data(), ref.size());
ec.hand = put<int>(ec.s0, sizeof ec.s0); // each pool's hand at its first slot
const int none[EC_POOLS] = {};
ec.lent = put<int>(none, sizeof none);
for (int l = 0; l < NL; ++l) {
c.L[(size_t) l].gu_e.ep = ec.ep_gu + (size_t) l * NEXP;
c.L[(size_t) l].down_e.ep = ec.ep_dn + (size_t) l * NEXP;
}
}
// row r of t (counting every matrix's rows, in order), which must be one of its rows: a row list made from the model's
// geometry would otherwise read the next tensor's bytes from the mapping when a GGUF's tensor is shorter than it
static const uint8_t* row_of(const Tensor& t, int r, size_t row_bytes) {
if (r < 0 || r >= t.nrows())
throw std::runtime_error(t.name + ": row " + std::to_string(r) + " asked for, it has " + std::to_string(t.nrows()));
return t.data + (size_t) r * row_bytes;
}
// rows `rows` of a 2-D F32 tensor (ne1 indexes rows)
float* f32_rows(const Tensor& t, const std::vector<int>& rows, size_t row_floats) {
if (t.type != T_F32) throw std::runtime_error(t.name + " is not F32");
if ((size_t) t.ne[0] != row_floats)
throw std::runtime_error(t.name + " is " + std::to_string(t.ne[0]) + " floats a row, not the " + std::to_string(row_floats) +
" its reader takes");
std::vector<float> h(rows.size() * (size_t) t.ne[0]);
for (size_t i = 0; i < rows.size(); ++i)
std::memcpy(&h[i * t.ne[0]], row_of(t, rows[i], (size_t) t.ne[0] * 4), (size_t) t.ne[0] * 4);
return put<float>(h.data(), h.size() * 4);
}
// Q8_0 -> bf16, the whole tensor; F32 -> bf16 likewise
uint16_t* bf16(const Tensor& t) {
const size_t n = (size_t) (t.ne[0] * t.ne[1] * t.ne[2] * t.ne[3]);
std::vector<uint16_t> h(n);
if (t.type == T_F32) {
const float* f = (const float*) t.data;
for (size_t i = 0; i < n; ++i) h[i] = f32_to_bf16(f[i]);
} else if (t.type == T_Q8_0) {
for (size_t b = 0; b < n / 32; ++b) {
const uint8_t* blk = t.data + b * 34;
uint16_t dh;
std::memcpy(&dh, blk, 2);
const float d = f16_to_f32(dh);
for (int j = 0; j < 32; ++j) h[b * 32 + j] = f32_to_bf16(d * (float) (int8_t) blk[2 + j]);
}
} else if (t.type == T_BF16) {
std::memcpy(h.data(), t.data, n * 2);
} else {
throw std::runtime_error(t.name + ": bf16 conversion supports F32, Q8_0 and BF16");
}
return put<uint16_t>(h.data(), h.size() * 2);
}
// a dense matrix for q8s_gemv: Q8_0 slices move to the split layout (other types stay as they are)
DW dense(DW w) {
if (w.type != T_Q8_0) return w;
const size_t bytes = (size_t) w.n_out * w.row_bytes, nblk = bytes / 34;
std::vector<uint8_t> h(bytes);
ck(cudaMemcpy(h.data(), w.p, bytes, cudaMemcpyDeviceToHost), "dense: download");
if (store) store->free_dev(c.dev, w.p, bytes); // the upload just before: its room is used again
else ck(cudaFree(w.p), "dense: free");
if (track) track->erase(std::remove(track->begin(), track->end(), (void*) w.p), track->end()); // not freed twice
c.bytes -= bytes;
std::vector<int8_t> q(nblk * 32);
std::vector<uint16_t> d(nblk);
for (size_t b = 0; b < nblk; ++b) {
std::memcpy(&d[b], &h[b * 34], 2);
std::memcpy(&q[b * 32], &h[b * 34 + 2], 32);
}
w.p = nullptr;
w.s.q = put<int8_t>(q.data(), q.size());
w.s.d = put<uint16_t>(d.data(), d.size() * 2);
return w;
}
// bf16 values [n] as the split int8 layout (per 32 values: d = amax / 127, ggml's rounding)
Q8S q8split_bf16(const uint16_t* v, size_t n) {
std::vector<int8_t> q(n);
std::vector<uint16_t> d(n / 32);
for (size_t b = 0; b < n / 32; ++b) {
float x[32], amax = 0.f;
for (int j = 0; j < 32; ++j) {
const uint32_t u = (uint32_t) v[b * 32 + j] << 16;
std::memcpy(&x[j], &u, 4);
amax = std::max(amax, std::fabs(x[j]));
}
const float dd = amax / 127.0f, id = dd > 0.f ? 1.0f / dd : 0.f;
for (int j = 0; j < 32; ++j) q[b * 32 + j] = (int8_t) std::lround(x[j] * id);
d[b] = f32_to_f16(dd);
}
Q8S s;
s.q = put<int8_t>(q.data(), q.size());
s.d = put<uint16_t>(d.data(), d.size() * 2);
return s;
}
// a whole Q8_0 tensor as int8 values and f16 scales
Q8S q8split(const Tensor& t) {
if (t.type != T_Q8_0) throw std::runtime_error(t.name + ": q8split needs Q8_0");
const size_t n = (size_t) (t.ne[0] * t.ne[1] * t.ne[2] * t.ne[3]);
std::vector<int8_t> q(n);
std::vector<uint16_t> d(n / 32);
for (size_t b = 0; b < n / 32; ++b) {
const uint8_t* blk = t.data + b * 34;
std::memcpy(&d[b], blk, 2);
std::memcpy(&q[b * 32], blk + 2, 32);
}
Q8S s;
s.q = put<int8_t>(q.data(), q.size());
s.d = put<uint16_t>(d.data(), d.size() * 2);
return s;
}
// whole rows of a quantized 2-D tensor (or of matrix `mat` of a 3-D one)
DW rows(const Tensor& t, const std::vector<int>& rows) {
DW w;
w.type = t.type;
w.n_in = (int) t.ne[0];
w.n_out = (int) rows.size();
w.row_bytes = t.row_bytes();
std::vector<uint8_t> h(rows.size() * w.row_bytes);
for (size_t i = 0; i < rows.size(); ++i) std::memcpy(&h[i * w.row_bytes], row_of(t, rows[i], w.row_bytes), w.row_bytes);
w.p = put<uint8_t>(h.data(), h.size());
return w;
}
// rows r1 of t1 then rows r2 of t2 (same type and row width), one matrix
DW rows2(const Tensor& t1, const std::vector<int>& r1, const Tensor& t2, const std::vector<int>& r2) {
if (t1.type != t2.type || t1.ne[0] != t2.ne[0]) throw std::runtime_error(t1.name + " and " + t2.name + " do not stack");
DW w;
w.type = t1.type;
w.n_in = (int) t1.ne[0];
w.n_out = (int) (r1.size() + r2.size());
w.row_bytes = t1.row_bytes();
std::vector<uint8_t> h((size_t) w.n_out * w.row_bytes);
size_t o = 0;
for (int r : r1) { std::memcpy(&h[o], row_of(t1, r, w.row_bytes), w.row_bytes); o += w.row_bytes; }
for (int r : r2) { std::memcpy(&h[o], row_of(t2, r, w.row_bytes), w.row_bytes); o += w.row_bytes; }
w.p = put<uint8_t>(h.data(), h.size());
return w;
}
#if FNX_27B
// rows r1 of t1, r2 of t2, then r3 of t3, one matrix
DW rows3(const Tensor& t1, const std::vector<int>& r1, const Tensor& t2, const std::vector<int>& r2, const Tensor& t3,
const std::vector<int>& r3) {
for (const Tensor* t : {&t2, &t3})
if (t1.type != t->type || t1.ne[0] != t->ne[0]) throw std::runtime_error(t1.name + " and " + t->name + " do not stack");
DW w;
w.type = t1.type;
w.n_in = (int) t1.ne[0];
w.n_out = (int) (r1.size() + r2.size() + r3.size());
w.row_bytes = t1.row_bytes();
std::vector<uint8_t> h((size_t) w.n_out * w.row_bytes);
size_t o = 0;
for (const auto& [t, rs] : {std::pair{&t1, &r1}, std::pair{&t2, &r2}, std::pair{&t3, &r3}})
for (int r : *rs) { std::memcpy(&h[o], row_of(*t, r, w.row_bytes), w.row_bytes); o += w.row_bytes; }
w.p = put<uint8_t>(h.data(), h.size());
return w;
}
#endif
// rows `rows` of t1 then of t2 as f32 (F32 rows as they are, Q8_0 rows dequantized)
float* f32_rows2(const Tensor& t1, const Tensor& t2, const std::vector<int>& rows, size_t row_floats) {
std::vector<float> h;
for (const Tensor* t : {&t1, &t2}) {
const size_t n = (size_t) t->ne[0];
if (n != row_floats)
throw std::runtime_error(t->name + " is " + std::to_string(n) + " floats a row, not the " + std::to_string(row_floats) +
" its reader takes");
for (int r : rows) {
if (t->type == T_F32) {
const float* row = (const float*) row_of(*t, r, n * 4);
h.insert(h.end(), row, row + n);
} else if (t->type == T_Q8_0 && n % 32 == 0) {
const uint8_t* row = row_of(*t, r, t->row_bytes());
for (size_t b = 0; b < n / 32; ++b) {
uint16_t dh;
std::memcpy(&dh, row + b * 34, 2);
const float d = f16_to_f32(dh);
for (int j = 0; j < 32; ++j) h.push_back(d * (float) (int8_t) row[b * 34 + 2 + j]);
}
} else {
throw std::runtime_error(t->name + " is neither F32 nor Q8_0");
}
}
}
return put<float>(h.data(), h.size() * 4);
}
// every row, keeping input blocks `blocks` (block indices along ne0) of each: a column slice
DW cols(const Tensor& t, const std::vector<int>& blocks) {
const TypeInfo ti = block32(t);
DW w;
w.type = t.type;
w.n_in = (int) blocks.size() * ti.block;
w.n_out = (int) t.ne[1];
w.row_bytes = blocks.size() * ti.bytes;
const size_t src_row = t.row_bytes();
for (int b : blocks)
if (b < 0 || (size_t) (b + 1) * ti.bytes > src_row)
throw std::runtime_error(t.name + ": block " + std::to_string(b) + " asked for, a row has " + std::to_string(src_row / ti.bytes));
std::vector<uint8_t> h((size_t) w.n_out * w.row_bytes);
for (int r = 0; r < w.n_out; ++r)
for (size_t b = 0; b < blocks.size(); ++b)
std::memcpy(&h[(size_t) r * w.row_bytes + b * ti.bytes], t.data + (size_t) r * src_row + (size_t) blocks[b] * ti.bytes, ti.bytes);
w.p = put<uint8_t>(h.data(), h.size());
return w;
}
// per expert: rows [r0, r1) (gate / up), expert-major
static void row_range(const Tensor& t, int r0, int r1) {
if (r0 < 0 || r1 <= r0 || r1 > t.ne[1])
throw std::runtime_error(t.name + ": rows [" + std::to_string(r0) + ", " + std::to_string(r1) + ") asked for, it has " +
std::to_string(t.ne[1]) + " a matrix");
}
DW expert_rows(const Tensor& t, int r0, int r1, size_t& stride) {
row_range(t, r0, r1);
DW w;
w.type = t.type;
w.n_in = (int) t.ne[0];
w.n_out = r1 - r0;
w.row_bytes = t.row_bytes();
stride = (size_t) w.n_out * w.row_bytes;
const size_t src_mat = (size_t) t.ne[1] * w.row_bytes;
std::vector<uint8_t> h((size_t) t.ne[2] * stride);
for (int64_t e = 0; e < t.ne[2]; ++e) std::memcpy(&h[e * stride], t.data + e * src_mat + (size_t) r0 * w.row_bytes, stride);
w.p = put<uint8_t>(h.data(), h.size());
return w;
}
// per expert: rows [r0, r1) of t1 then of t2 (gate; up), expert-major
DW expert_rows2(const Tensor& t1, const Tensor& t2, int r0, int r1, size_t& stride, const char** host_of = nullptr) {
if (t1.type != t2.type || t1.ne[0] != t2.ne[0] || t1.ne[2] != t2.ne[2]) throw std::runtime_error(t1.name + " and " + t2.name + " do not stack");
row_range(t1, r0, r1);
row_range(t2, r0, r1);
DW w;
w.type = t1.type;
w.n_in = (int) t1.ne[0];
w.n_out = 2 * (r1 - r0);
w.row_bytes = t1.row_bytes();
stride = (size_t) w.n_out * w.row_bytes;
const size_t half = (size_t) (r1 - r0) * w.row_bytes, src_mat = (size_t) t1.ne[1] * w.row_bytes;
std::vector<uint8_t> tmp;
const size_t bytes = (size_t) t1.ne[2] * stride;
uint8_t* h = expert_buf(bytes, host_of, tmp);
for (int64_t e = 0; e < t1.ne[2]; ++e) {
std::memcpy(&h[e * stride], t1.data + e * src_mat + (size_t) r0 * w.row_bytes, half);
std::memcpy(&h[e * stride + half], t2.data + e * src_mat + (size_t) r0 * w.row_bytes, half);
}
place(w, h, bytes, stride, host_of);
return w;
}
// f32 tensors' rows one after another
float* f32_cat(const Tensor& t1, const Tensor& t2, size_t row_floats) {
if (t1.type != T_F32 || t2.type != T_F32 || t1.ne[0] != t2.ne[0]) throw std::runtime_error(t1.name + " and " + t2.name + " do not stack");
if ((size_t) t1.ne[0] != row_floats)
throw std::runtime_error(t1.name + " is " + std::to_string(t1.ne[0]) + " floats a row, not the " + std::to_string(row_floats) +
" its reader takes");
std::vector<float> h((const float*) t1.data, (const float*) t1.data + t1.ne[0] * t1.ne[1]);
h.insert(h.end(), (const float*) t2.data, (const float*) t2.data + t2.ne[0] * t2.ne[1]);
return put<float>(h.data(), h.size() * 4);
}
// per expert: every row, input blocks [b0, b1) (down), expert-major
DW expert_cols(const Tensor& t, int b0, int b1, size_t& stride, const char** host_of = nullptr) {
const TypeInfo ti = block32(t);
DW w;
w.type = t.type;
w.n_in = (b1 - b0) * ti.block;
w.n_out = (int) t.ne[1];
w.row_bytes = (size_t) (b1 - b0) * ti.bytes;
stride = (size_t) w.n_out * w.row_bytes;
const size_t src_row = t.row_bytes(), src_mat = (size_t) t.ne[1] * src_row;
if (b0 < 0 || b1 <= b0 || (size_t) b1 * ti.bytes > src_row)
throw std::runtime_error(t.name + ": blocks [" + std::to_string(b0) + ", " + std::to_string(b1) + ") asked for, a row has " +
std::to_string(src_row / ti.bytes));
std::vector<uint8_t> tmp;
const size_t bytes = (size_t) t.ne[2] * stride;
uint8_t* h = expert_buf(bytes, host_of, tmp);
for (int64_t e = 0; e < t.ne[2]; ++e)
for (int r = 0; r < w.n_out; ++r)
std::memcpy(&h[e * stride + (size_t) r * w.row_bytes], t.data + e * src_mat + (size_t) r * src_row + (size_t) b0 * ti.bytes, w.row_bytes);
place(w, h, bytes, stride, host_of);
return w;
}
};
std::vector<int> range(int a, int b) {
std::vector<int> v;
for (int i = a; i < b; ++i) v.push_back(i);
return v;
}
#if FNX_27B
// The 27B's layers on a card, from load_card's channel lists: the recurrent layers as Flash-Next's; attention split by
// heads (K/V too: each card keeps its own heads' cache, src/s27_attn.cuh), q | gate, K and V stacked in one matrix;
// the dense FFN by FF width; then the head, and the draft layer (blk.NL, stage group NL + 1). Every matrix q8s reads
// is Q8_0: the 27B's path has no other matvec
void load_layers_27b(Stager& st, CardW& c, Up& up, const std::vector<int>& conv_rows, const std::vector<int>& zrows,
const std::vector<int>& vblocks, const std::vector<int>& qrows, const std::vector<int>& kvrows,
const std::vector<int>& woblocks) {
auto L = [&](int l, const char* s) -> const Tensor& { return st.t(l, "blk." + std::to_string(l) + "." + s); };
const std::vector<int>& vheads = c.vheads;
auto q8 = [](const DW& d, const std::string& what) {
if (!d.s.q) throw std::runtime_error("27b: " + what + " is not Q8_0 (the 27B's matvecs read Q8_0 only)");
};
// the shape the window writes and reads it at (rows are the output stride; a wrong one would land at another)
auto shape = [](const DW& d, int out, int in, const std::string& what) {
if (d.n_out != out || d.n_in != in)
throw std::runtime_error("27b: " + what + " is " + std::to_string(d.n_out) + " x " + std::to_string(d.n_in) + ", not " +
std::to_string(out) + " x " + std::to_string(in));
};
const int ffs = c.fd1 - c.fd0, qkv = c.nq * 2 * HD + 2 * c.nkv * HD;
auto attn = [&](LayerW& w, auto&& T) {
w.wq = up.dense(up.rows3(T("attn_q.weight"), qrows, T("attn_k.weight"), kvrows, T("attn_v.weight"), kvrows));
w.wo = up.dense(up.cols(T("attn_output.weight"), woblocks));
w.q_norm = up.f32(T("attn_q_norm.weight"), HD);
w.k_norm = up.f32(T("attn_k_norm.weight"), HD);
};
auto ffn = [&](LayerW& w, auto&& T) {
w.attn_norm = up.f32(T("attn_norm.weight"), N);
w.ffn_norm = up.f32(T("post_attention_norm.weight"), N);
w.ffn_gu = up.dense(up.rows2(T("ffn_gate.weight"), range(c.fd0, c.fd1), T("ffn_up.weight"), range(c.fd0, c.fd1)));
w.ffn_down = up.dense(up.cols(T("ffn_down.weight"), range(c.fd0 / 32, c.fd1 / 32)));
};
c.L.resize(NL);
for (int l = 0; l < NL; ++l) {
LayerW& w = c.L[l];
auto T = [&](const char* s) -> const Tensor& { return L(l, s); };
if (!is_attn(l)) {
w.qkvz = up.dense(up.rows2(T("attn_qkv.weight"), conv_rows, T("attn_gate.weight"), zrows));
w.ab = up.f32_rows2(T("ssm_alpha.weight"), T("ssm_beta.weight"), vheads, N);
w.ssm_out = up.dense(up.cols(T("ssm_out.weight"), vblocks));
w.conv = up.f32_rows(T("ssm_conv1d.weight"), conv_rows, 4);
std::vector<float> a, dt;
const size_t nh = (size_t) *std::max_element(vheads.begin(), vheads.end()) + 1;
const float* af = f32_data(T("ssm_a"), nh);
const float* df = f32_data(T("ssm_dt.bias"), nh);
for (int h : vheads) { a.push_back(af[h]); dt.push_back(df[h]); }
w.ssm_a = up.put<float>(a.data(), a.size() * 4);
w.dt_bias = up.put<float>(dt.data(), dt.size() * 4);
w.ssm_norm = up.f32(T("ssm_norm.weight"), HS);
q8(w.qkvz, "layer " + std::to_string(l) + "'s qkv | gate"), q8(w.ssm_out, "layer " + std::to_string(l) + "'s ssm_out");
shape(w.qkvz, c.conv_ch() + c.nv() * HS, N, "layer " + std::to_string(l) + "'s qkv | gate");
shape(w.ssm_out, N, c.nv() * HS, "layer " + std::to_string(l) + "'s ssm_out");
} else {
attn(w, T);
q8(w.wq, "layer " + std::to_string(l) + "'s q | k | v"), q8(w.wo, "layer " + std::to_string(l) + "'s attn_output");
shape(w.wq, qkv, N, "layer " + std::to_string(l) + "'s q | k | v");
shape(w.wo, N, c.nq * HD, "layer " + std::to_string(l) + "'s attn_output");
}
ffn(w, T);
q8(w.ffn_gu, "layer " + std::to_string(l) + "'s gate | up"), q8(w.ffn_down, "layer " + std::to_string(l) + "'s down");
shape(w.ffn_gu, 2 * ffs, N, "layer " + std::to_string(l) + "'s gate | up");
shape(w.ffn_down, N, ffs, "layer " + std::to_string(l) + "'s down");
st.done(l);
}
c.out_norm = up.f32(st.t(NL, "output_norm.weight"), N);
c.out = up.dense(up.rows(st.t(NL, "output.weight"), range(c.v0, c.v1)));
q8(c.out, "output.weight");
shape(c.out, c.v1 - c.v0, N, "output.weight's rows");
st.done(NL);
{
auto T = [&](const char* s) -> const Tensor& { return st.t(NL + 1, "blk." + std::to_string(NL) + "." + s); };
attn(c.mtp, T);
ffn(c.mtp, T);
c.mtp_eh = up.dense(up.rows(T("nextn.eh_proj.weight"), range(0, N))); // every card the whole of it
c.mtp_enorm = up.f32(T("nextn.enorm.weight"), N);
c.mtp_hnorm = up.f32(T("nextn.hnorm.weight"), N);
c.mtp_head_norm = up.f32(T("nextn.shared_head_norm.weight"), N);
for (const DW* d : {&c.mtp.wq, &c.mtp.wo, &c.mtp.ffn_gu, &c.mtp.ffn_down, &c.mtp_eh}) q8(*d, "a draft layer matrix");
shape(c.mtp.wq, qkv, N, "the draft layer's q | k | v");
shape(c.mtp.wo, N, c.nq * HD, "the draft layer's attn_output");
shape(c.mtp.ffn_gu, 2 * ffs, N, "the draft layer's gate | up");
shape(c.mtp.ffn_down, N, ffs, "the draft layer's down");
shape(c.mtp_eh, N, 2 * N, "the draft layer's eh_proj");
st.done(NL + 1);
}
}
#endif
// The tensors come from st: group l is layer l's, group NL the output head's (stage_groups); each group is given back
// once this card is through with it.
// host_experts: every routed expert to a host copy (an ExpertCache, or the lab's holder); store: the holder's shared memory
void load_card(Stager& st, CardW& c, const std::vector<std::vector<char>>& resident, const std::vector<double>* arena_score,
Store* store, bool host_experts) {
ck(cudaSetDevice(c.dev), "cudaSetDevice");
Up up{c, store};
auto L = [&](int l, const char* s) -> const Tensor& { return st.t(l, "blk." + std::to_string(l) + "." + s); };
// recurrent channel lists: q, k for this card's key heads; v for its value heads (s * NKH + k)
std::vector<int> qch, kch, vch, vblocks;
const std::vector<int>& vheads = c.vheads;
for (int k = c.k0; k < c.k0 + c.nk; ++k)
for (int i = 0; i < HS; ++i) { qch.push_back(k * HS + i); kch.push_back(NKH * HS + k * HS + i); }
{
std::vector<int> vmap;
std::vector<bool> seen(c.nk, false);
for (int h : vheads) {
const int kl = h % NKH - c.k0;
if (kl < 0 || kl >= c.nk) throw std::runtime_error("fnx: a value head whose key head is not on its card");
vmap.push_back(kl | (seen[kl] ? 0 : 1 << 8));
seen[kl] = true;
for (int i = 0; i < HS; ++i) vch.push_back(2 * NKH * HS + h * HS + i);
for (int b = 0; b < HS / 32; ++b) vblocks.push_back(h * HS / 32 + b);
}
for (int kl = 0; kl < c.nk; ++kl)
if (!seen[kl]) throw std::runtime_error("fnx: a key head with no value head on its card");
c.vmap = up.put<int>(vmap.data(), vmap.size() * 4);
}
std::vector<int> conv_rows = qch;
conv_rows.insert(conv_rows.end(), kch.begin(), kch.end());
conv_rows.insert(conv_rows.end(), vch.begin(), vch.end());
std::vector<int> zrows;
for (int ch : vch) zrows.push_back(ch - 2 * NKH * HS);
// attention rows: q heads (q + gate, 2 * HD rows each), KV heads; wo input blocks of the q heads
std::vector<int> qrows, kvrows, woblocks;
for (int h = c.q0; h < c.q0 + c.nq; ++h) {
for (int i = 0; i < 2 * HD; ++i) qrows.push_back(h * 2 * HD + i);
for (int b = 0; b < HD / 32; ++b) woblocks.push_back(h * HD / 32 + b);
}
for (int h = c.kv0; h < c.kv0 + c.nkv; ++h)
for (int i = 0; i < HD; ++i) kvrows.push_back(h * HD + i);
#if FNX_27B
(void) resident, (void) arena_score, (void) host_experts;
load_layers_27b(st, c, up, conv_rows, zrows, vblocks, qrows, kvrows, woblocks);
return;
#endif
// the K/V cache is split by position: every card projects q for every head and k/v for both KV heads
std::vector<int> qrows_all, kvrows_all;
for (int i = 0; i < NQH * 2 * HD; ++i) qrows_all.push_back(i);
for (int i = 0; i < NKVH * HD; ++i) kvrows_all.push_back(i);
c.L.resize(NL);
std::vector<const char*> hgu(host_experts ? (size_t) NL * NEXP : 0), hdn(hgu.size()); // the experts' host copies
for (int l = 0; l < NL; ++l) {
LayerW& w = c.L[l];
const char** hg = host_experts ? hgu.data() + (size_t) l * NEXP : nullptr;
const char** hd = host_experts ? hdn.data() + (size_t) l * NEXP : nullptr;
const char* hp[2] = {"hc_attn_", "hc_ffn_"};
for (int h = 0; h < 2; ++h) {
w.hc_norm[h] = up.f32(L(l, (std::string(hp[h]) + "norm.weight").c_str()), D);
w.hc_down[h] = up.q8split(L(l, (std::string(hp[h]) + "down.weight").c_str()));
w.hc_up[h] = up.q8split(L(l, (std::string(hp[h]) + "up.weight").c_str()));
w.hc_inj[h] = up.bf16(L(l, (std::string(hp[h]) + "inject.weight").c_str()));
}
const Tensor& gi = L(l, "ffn_gate_inp.weight");
const Tensor& gs = L(l, "ffn_gate_inp_shexp.weight");
if (gi.ne[1] != NEXP || gs.ne[1] != 1)
throw std::runtime_error("fnx: " + gi.name + " holds " + std::to_string(gi.ne[1]) + " rows and " + gs.name + " " +
std::to_string(gs.ne[1]) + ", not the " + std::to_string(NEXP) +
" gate rows and 1 shared row k_router_route reads");
w.router = up.f32_cat(gi, gs, N);
if (!is_attn(l)) {
w.qkvz = up.dense(up.rows2(L(l, "attn_qkv.weight"), conv_rows, L(l, "attn_gate.weight"), zrows));
w.ab = up.f32_rows2(L(l, "ssm_alpha.weight"), L(l, "ssm_beta.weight"), vheads, N);
w.ssm_out = up.dense(up.cols(L(l, "ssm_out.weight"), vblocks));
w.conv = up.f32_rows(L(l, "ssm_conv1d.weight"), conv_rows, 4);
std::vector<float> a, dt;
const size_t nh = (size_t) *std::max_element(vheads.begin(), vheads.end()) + 1;
const float* af = f32_data(L(l, "ssm_a"), nh);
const float* df = f32_data(L(l, "ssm_dt.bias"), nh);
for (int h : vheads) { a.push_back(af[h]); dt.push_back(df[h]); }
w.ssm_a = up.put<float>(a.data(), a.size() * 4);
w.dt_bias = up.put<float>(dt.data(), dt.size() * 4);
w.ssm_norm = up.f32(L(l, "ssm_norm.weight"), HS);
} else {
w.wq = up.dense(up.rows(L(l, "attn_q.weight"), qrows_all));
w.wk = up.dense(up.rows(L(l, "attn_k.weight"), kvrows_all));
w.wv = up.dense(up.rows(L(l, "attn_v.weight"), kvrows_all));
w.wo = up.dense(up.cols(L(l, "attn_output.weight"), woblocks));
w.q_norm = up.f32(L(l, "attn_q_norm.weight"), HD);
w.k_norm = up.f32(L(l, "attn_k_norm.weight"), HD);
const Tensor& iq = L(l, "indexer.q_proj.weight");
const Tensor& ik = L(l, "indexer.k_proj.weight");
if (iq.ne[1] != QSA_HEADS * QSA_KEY || iq.ne[0] != N || ik.ne[1] != QSA_KEY || ik.ne[0] != N)
throw std::runtime_error("fnx: " + iq.name + " is " + std::to_string(iq.ne[0]) + " by " + std::to_string(iq.ne[1]) +
" and " + ik.name + " is " + std::to_string(ik.ne[0]) + " by " + std::to_string(ik.ne[1]) +
", not the " + std::to_string(N) + " by " + std::to_string(QSA_HEADS * QSA_KEY) + " and " +
std::to_string(N) + " by " + std::to_string(QSA_KEY) + " k_idx_proj reads");
w.idx_wq = up.bf16(iq);
w.idx_wk = up.bf16(ik);
w.idx_qn = up.f32(L(l, "indexer.q_norm.weight"), QSA_KEY);
w.idx_kn = up.f32(L(l, "indexer.k_norm.weight"), QSA_KEY);
}
w.gu_e = up.expert_rows2(L(l, "ffn_gate_exps.weight"), L(l, "ffn_up_exps.weight"), c.ff0, c.ff1, w.gu_e_stride, hg);
w.down_e = up.expert_cols(L(l, "ffn_down_exps.weight"), c.ff0 / 32, c.ff1 / 32, w.down_e_stride, hd);
w.sh_gu = up.dense(up.rows2(L(l, "ffn_gate_shexp.weight"), range(c.ff0, c.ff1), L(l, "ffn_up_shexp.weight"), range(c.ff0, c.ff1)));
w.sh_down = up.dense(up.cols(L(l, "ffn_down_shexp.weight"), range(c.ff0 / 32, c.ff1 / 32)));
if (!w.sh_gu.s.q || !w.sh_down.s.q) throw std::runtime_error("fnx: the shared expert is not Q8_0");
if (l == PLE_LAYER) {
const Tensor& pk = L(l, "ple_key.weight");
const Tensor& pv = L(l, "ple_value.weight");
// dense() reads N-wide xq rows and writes D-float rows into b->ple_key, N-float into b->ple_val
if (pk.ne[0] != N || pk.ne[1] != D)
throw std::runtime_error("fnx: " + pk.name + " is " + std::to_string(pk.ne[0]) + " by " + std::to_string(pk.ne[1]) +
", not the " + std::to_string(N) + " by " + std::to_string(D) + " its dense into the ple_key rows reads");
if (pv.ne[0] != N || pv.ne[1] != N)
throw std::runtime_error("fnx: " + pv.name + " is " + std::to_string(pv.ne[0]) + " by " + std::to_string(pv.ne[1]) +
", not the " + std::to_string(N) + " by " + std::to_string(N) + " its dense into the ple_val rows reads");
c.ple_key = up.dense(up.rows(pk, range(0, (int) pk.ne[1])));
c.ple_value = up.dense(up.rows(pv, range(0, (int) pv.ne[1])));
c.ple_norm_key = up.f32(L(l, "ple_norm_key.weight"), D);
c.ple_norm_query = up.f32(L(l, "ple_norm_query.weight"), D);
c.ple_norm_conv = up.f32(L(l, "ple_norm_conv.weight"), D);
const Tensor& pc = L(l, "ple_conv1d.weight"); // k_ple_conv reads w[ch * 4 + k]: 4 a row, D of them
if (pc.ne[0] != 4 || pc.ne[1] != D)
throw std::runtime_error("fnx: " + pc.name + " is " + std::to_string(pc.ne[0]) + " by " + std::to_string(pc.ne[1]) +
", not the 4 by " + std::to_string(D) + " k_ple_conv reads");
c.ple_conv = up.f32(pc, 4 * D);
}
st.done(l);
}
c.head_norm = up.f32(st.t(NL, "output_hc_norm.weight"), D);
c.head_down = up.q8split(st.t(NL, "output_hc_down.weight"));
c.head_up = up.q8split(st.t(NL, "output_hc_up.weight"));
c.out = up.dense(up.rows(st.t(NL, "output.weight"), range(c.v0, c.v1)));
st.done(NL);
if (!resident.empty()) { // tracked, so the engine can place them again with more in RAM (Engine: VRAM short)
const size_t before = c.bytes;
Up ue{c, store, &c.expert_mem};
ue.expert_cache(resident, hgu, hdn, arena_score);
c.expert_bytes = c.bytes - before;
} else if (host_experts) c.ec_host_gu = hgu, c.ec_host_dn = hdn; // the holder: placed by a worker
}
// ---------------------------------------------------------------- the draft layer
// Strata's runtime files: dense.txt lines "name kind rows cols offset bytes" into dense.bin (kind q8_0 | bf16 | f32)
struct MtpFiles {
std::map<std::string, Tensor> t;
void* base = nullptr;
size_t size = 0;
std::vector<uint8_t> gate_b, up_b, down_b; // the experts' blocks: [512][640][2560], [512][640][2560], [512][2560][640]
uint32_t etype = T_Q4_0;
Tensor gate, up, down;
explicit MtpFiles(const std::string& dir) {
const int fd = ::open((dir + "/dense.bin").c_str(), O_RDONLY);
if (fd < 0) throw std::runtime_error("mtp: cannot open " + dir + "/dense.bin");
size = (size_t) lseek(fd, 0, SEEK_END);
base = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
::close(fd);
if (base == MAP_FAILED) throw std::runtime_error("mtp: cannot map dense.bin");
std::ifstream idx(dir + "/dense.txt");
if (!idx) throw std::runtime_error("mtp: cannot open " + dir + "/dense.txt");
std::string line;
while (std::getline(idx, line)) {
if (line.empty()) continue;
std::istringstream is(line);
std::string name, kind;
int64_t rows, cols;
uint64_t off, bytes;
if (!(is >> name >> kind >> rows >> cols >> off >> bytes)) throw std::runtime_error("mtp: dense.txt: " + line);
Tensor x;
x.name = name;
x.type = kind == "q8_0" ? T_Q8_0 : kind == "bf16" ? T_BF16 : kind == "f32" ? T_F32 : 0xffffffffu;
if (x.type == 0xffffffffu) throw std::runtime_error("mtp: dense.txt: kind " + kind);
x.ne[0] = cols;
x.ne[1] = rows;
x.n_dims = 2;
x.data = (const uint8_t*) base + off;
x.nbytes = bytes;
if (off + bytes > size || x.row_bytes() * rows != bytes) throw std::runtime_error("mtp: dense.txt: " + name + " does not fit");
t[name] = x;
}
// FNX_MTP_EXPERTS: q2_0 is Strata's experts.bin (unpacked to Q4_0 blocks), q4_0 is tools/mtp_experts.py's
// experts-q4_0.bin, made from the BF16 checkpoint; the same VRAM either way. Q8_0 drafted exactly as Q4_0 did
// for ~420 MiB more a card, which the arena's reserve does not count: not offered.
const char* ex_s = std::getenv("FNX_MTP_EXPERTS");
const std::string ex = ex_s ? ex_s : "q2_0"; // strata-split: Strata's draft-layer files (mtp/rt), no download; FNX_MTP_EXPERTS=q4_0 for sFNX's
if (ex == "q2_0") experts(dir + "/experts.bin");
else if (ex == "q4_0") experts_blocks(dir + "/experts-q4_0.bin", T_Q4_0, 18);
else throw std::runtime_error("mtp: FNX_MTP_EXPERTS=" + ex + ": q2_0 or q4_0");
std::fprintf(stderr, "fnx: draft layer experts %s\n", ex.c_str());
views();
}
~MtpFiles() { if (base) munmap(base, size); }
const Tensor& operator[](const std::string& n) const {
const auto it = t.find(n);
if (it == t.end()) throw std::runtime_error("mtp: " + n + " is missing");
return it->second;
}
// experts.bin: per expert codes (gate/up rows interleaved, 640 B each; then 2560 down rows of 160 B), then the
// fp16 scales (gate/up rows, 40 each; down rows, 10 each). Q2_0: value j of a 64-value block is code j (byte j / 4,
// bits 2 (j % 4)) minus one, times the scale. Q4_0 keeps 32 values per block: nibble = code + 7 (value i low,
// i + 16 high), the same scale for both halves of a Q2_0 block.
void experts(const std::string& path) {
constexpr int H = N, F = FF, NE = NEXP;
constexpr size_t ROW_GU = H / 4, ROW_D = F / 4, SC_GU = H / 64, SC_D = F / 64;
constexpr size_t O_D = 2ull * F * ROW_GU, O_GS = O_D + (size_t) H * ROW_D, O_DS = O_GS + 2ull * F * SC_GU * 2;
constexpr size_t BLOB = O_DS + (size_t) H * SC_D * 2;
#if !FNX_27B
static_assert(BLOB == 1382400, "Strata's Q2_0 expert blob");
#endif
std::FILE* f = std::fopen(path.c_str(), "rb");
if (!f) throw std::runtime_error("mtp: cannot open " + path);
const size_t qrow_gu = (size_t) H / 32 * 18, qrow_d = (size_t) F / 32 * 18;
gate_b.resize((size_t) NE * F * qrow_gu);
up_b.resize((size_t) NE * F * qrow_gu);
down_b.resize((size_t) NE * H * qrow_d);
etype = T_Q4_0;
std::vector<uint8_t> blob(BLOB);
// one row of n values: codes (n / 4 bytes) and n / 64 scales -> n / 32 Q4_0 blocks
auto row = [](const uint8_t* codes, const uint8_t* scales, int n, uint8_t* out) {
for (int b = 0; b < n / 64; ++b) {
uint16_t d;
std::memcpy(&d, scales + b * 2, 2);
const uint8_t* c = codes + b * 16;
auto code = [&](int j) { return (c[j / 4] >> (2 * (j % 4))) & 3; };
for (int h = 0; h < 2; ++h) {
uint8_t* o = out + (size_t) (2 * b + h) * 18;
std::memcpy(o, &d, 2);
for (int i = 0; i < 16; ++i)
o[2 + i] = (uint8_t) ((code(32 * h + i) + 7) | ((code(32 * h + 16 + i) + 7) << 4));
}
}
};
for (int e = 0; e < NE; ++e) {
if (std::fread(blob.data(), 1, BLOB, f) != BLOB) {
std::fclose(f);
throw std::runtime_error("mtp: experts.bin is truncated");
}
for (int r = 0; r < F; ++r) {