-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_webgpu_native.cpp
More file actions
1923 lines (1760 loc) · 67.8 KB
/
Copy pathtest_webgpu_native.cpp
File metadata and controls
1923 lines (1760 loc) · 67.8 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 (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/webgpu/runtime/WebGPUDelegateHeader.h>
#include <executorch/backends/webgpu/runtime/WebGPUDevice.h>
#include <executorch/backends/webgpu/runtime/WebGPUGraph.h>
#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>
#include <executorch/runtime/backend/backend_options_map.h>
#include <executorch/runtime/backend/options.h>
#include <executorch/backends/vulkan/serialization/schema_generated.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <limits>
#include <memory>
#include <string>
#include <vector>
using namespace executorch::backends::webgpu;
using namespace executorch::extension;
using namespace executorch::runtime;
namespace {
// Environment-derived config; captured in main() before RUN_ALL_TESTS().
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
std::string g_update_cache_model_path;
std::string g_qlinear_dir;
std::string g_prepack_model_path, g_prepack_golden_path;
std::string g_prepack2_model_path, g_prepack2_golden_path;
std::string g_prepack_tied_model_path, g_prepack_tied_golden_path;
std::string g_sdpa_dir;
std::string g_symint_blob;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
#ifdef WGPU_BACKEND_ENABLE_PROFILING
// Capacity-overrun must throw; runs without a device or TimestampQuery.
void test_query_pool_overrun_throws() {
WebGPUQueryPool qp;
EXPECT_THROW(qp.reset(1), std::exception)
<< "reset beyond capacity did not throw";
}
// WebGPUQueryPool roundtrip: time a probe pass; assert non-zero GPU duration.
void test_query_pool_roundtrip(const WebGPUContext& ctx) {
if (!ctx.timestamp_supported) {
GTEST_SKIP() << "adapter lacks TimestampQuery feature";
}
WGPUDevice device = ctx.device;
// Probe loop iterates enough to burn a measurable, non-zero GPU duration.
const char* kProbeWGSL =
"@group(0) @binding(0) var<storage, read_write> out: array<f32>;\n"
"@compute @workgroup_size(64)\n"
"fn main(@builtin(global_invocation_id) gid: vec3<u32>) {\n"
" var acc = 0.0;\n"
" for (var i = 0u; i < 8192u; i = i + 1u) {\n"
" acc = acc + f32(i) * 1.000001;\n"
" }\n"
" out[gid.x] = acc;\n"
"}\n";
WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_desc.code = {kProbeWGSL, WGPU_STRLEN};
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_desc.chain;
WGPUShaderModule shader = wgpuDeviceCreateShaderModule(device, &shader_desc);
WGPUBindGroupLayoutEntry bgl_entry = {};
bgl_entry.binding = 0;
bgl_entry.visibility = WGPUShaderStage_Compute;
bgl_entry.buffer.type = WGPUBufferBindingType_Storage;
WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 1;
bgl_desc.entries = &bgl_entry;
WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc);
WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pl = wgpuDeviceCreatePipelineLayout(device, &pl_desc);
WGPUComputePipelineDescriptor pipe_desc = {};
pipe_desc.layout = pl;
pipe_desc.compute.module = shader;
pipe_desc.compute.entryPoint = {"main", WGPU_STRLEN};
WGPUComputePipeline pipe =
wgpuDeviceCreateComputePipeline(device, &pipe_desc);
WGPUBufferDescriptor obd = {};
obd.size = 64 * sizeof(float);
obd.usage = WGPUBufferUsage_Storage;
WGPUBuffer out_buf = wgpuDeviceCreateBuffer(device, &obd);
WGPUBindGroupEntry bg_entry = {};
bg_entry.binding = 0;
bg_entry.buffer = out_buf;
bg_entry.size = obd.size;
WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = bgl;
bg_desc.entryCount = 1;
bg_desc.entries = &bg_entry;
WGPUBindGroup bg = wgpuDeviceCreateBindGroup(device, &bg_desc);
WebGPUQueryPool qp;
qp.initialize(device, 1);
qp.reset(1);
WGPUCommandEncoder enc = wgpuDeviceCreateCommandEncoder(device, nullptr);
WGPUPassTimestampWrites tw = qp.writes_for(0);
WGPUComputePassDescriptor pass_desc = {};
pass_desc.timestampWrites = &tw;
WGPUComputePassEncoder pass =
wgpuCommandEncoderBeginComputePass(enc, &pass_desc);
wgpuComputePassEncoderSetPipeline(pass, pipe);
wgpuComputePassEncoderSetBindGroup(pass, 0, bg, 0, nullptr);
wgpuComputePassEncoderDispatchWorkgroups(pass, 1, 1, 1);
wgpuComputePassEncoderEnd(pass);
wgpuComputePassEncoderRelease(pass);
qp.record(0, "probe", {1, 1, 1}, {64, 1, 1});
qp.resolve(enc);
WGPUCommandBuffer cmd = wgpuCommandEncoderFinish(enc, nullptr);
wgpuQueueSubmit(ctx.queue, 1, &cmd);
wgpuCommandBufferRelease(cmd);
wgpuCommandEncoderRelease(enc);
qp.extract_results(ctx.instance);
wgpuBufferRelease(out_buf);
wgpuComputePipelineRelease(pipe);
wgpuPipelineLayoutRelease(pl);
wgpuBindGroupLayoutRelease(bgl);
wgpuBindGroupRelease(bg);
wgpuShaderModuleRelease(shader);
ASSERT_EQ(qp.results().size(), 1u)
<< "expected 1 duration, got " << qp.results().size();
const uint64_t dur = qp.results()[0].execution_duration_ns;
printf(" probe duration: %llu ns\n", (unsigned long long)dur);
EXPECT_NE(dur, 0u) << "probe duration is zero (expected monotonic non-zero)";
}
#endif // WGPU_BACKEND_ENABLE_PROFILING
void test_update_cache(const std::string& model_path) {
// update_cache: value [1,2,2,4] scattered into cache [1,8,2,4] at
// input_pos=0.
Module module(model_path);
auto err = module.load_forward();
ASSERT_EQ(err, Error::Ok)
<< "could not load forward method (error " << (int)err << ")";
printf("Model loaded: %s\n", model_path.c_str());
constexpr int S = 2, H = 2, D = 4, Cmax = 8;
constexpr int vnumel = S * H * D; // 16
constexpr int cnumel = Cmax * H * D; // 64
constexpr int input_pos = 0;
std::vector<float> value(vnumel);
std::vector<float> cache(cnumel);
for (int i = 0; i < vnumel; i++) {
value[i] = static_cast<float>(i) * 0.5f;
}
for (int i = 0; i < cnumel; i++) {
cache[i] = static_cast<float>(i) + 100.0f;
}
// Reference: input_pos=0 overwrites the [0,S) seq slice of the cache with
// value; the rest is preserved. Trivial scatter -- no library math involved.
std::vector<float> ref(cache);
for (int i = 0; i < vnumel; i++) {
ref[input_pos * H * D + i] = value[i];
}
auto v = make_tensor_ptr({1, S, H, D}, std::vector<float>(value));
auto c = make_tensor_ptr({1, Cmax, H, D}, std::vector<float>(cache));
auto result = module.forward({EValue(v), EValue(c)});
ASSERT_TRUE(result.ok()) << "forward failed (error " << (int)result.error()
<< ")";
const auto& outputs = result.get();
ASSERT_TRUE(!outputs.empty() && outputs[0].isTensor()) << "no tensor output";
const auto& out_tensor = outputs[0].toTensor();
ASSERT_EQ((int)out_tensor.numel(), cnumel)
<< "output numel " << (size_t)out_tensor.numel() << " != expected "
<< cnumel;
const float* out_data = out_tensor.const_data_ptr<float>();
float max_abs_err = 0.0f;
for (int i = 0; i < cnumel; i++) {
max_abs_err = std::max(max_abs_err, std::abs(out_data[i] - ref[i]));
}
printf("Max abs error: %e (checked %d elements)\n", max_abs_err, cnumel);
EXPECT_LE(max_abs_err, 1e-3f) << "max error exceeds tolerance 1e-3";
}
std::vector<float> load_golden(const std::string& path, size_t numel) {
// Load a raw little-endian fp32 golden written by the export .py (the native
// binary has no ATen/torch, so the reference is computed offline).
std::vector<float> g(numel);
FILE* f = std::fopen(path.c_str(), "rb");
if (!f) {
return {};
}
size_t n = std::fread(g.data(), sizeof(float), numel, f);
std::fclose(f);
if (n != numel) {
return {};
}
return g;
}
// Per-element dual tolerance mirroring at::allclose's combined gate: an element
// is OK if within abs (1e-4) OR within rel (1e-3) tol, so a near-zero golden
// value can't blow up the rel metric (the kernel's ~1e-8 abs error is the real
// signal at llama3 scale). Sets the reported maxima; true iff all elements
// pass.
bool sdpa_within_tol(
const float* out,
const float* golden,
int n,
float* ma,
float* mr) {
float atol = 1e-4f, rtol = 1e-3f;
// f16 KV (runtime opt-in) reads K/V at reduced precision; loosen the tol on a
// shader-f16 device to cover that rounding. Harmless for f32 KV (looser
// gate).
const WebGPUContext* kv_ctx = get_default_webgpu_context();
if (kv_ctx != nullptr && kv_ctx->shader_f16_supported) {
atol = 2e-3f;
rtol = 1e-2f;
}
float max_abs = 0.0f, max_rel = 0.0f;
bool ok = true;
for (int i = 0; i < n; i++) {
const float ae = std::abs(out[i] - golden[i]);
const float re = ae / std::max(std::abs(golden[i]), 1e-6f);
max_abs = std::max(max_abs, ae);
max_rel = std::max(max_rel, re);
if (ae > atol && re > rtol) {
ok = false;
}
}
*ma = max_abs;
*mr = max_rel;
return ok;
}
// linear_q4gsw sweep config; mirrors CONFIGS in test_quantized_linear.py.
struct Q4gswConfig {
const char* name;
int m; // rows (tokens)
int k; // in_features (reduction dim)
int n; // out_features
float tol_abs; // per-element abs gate
float tol_rel; // per-element rel gate
bool required; // dir set + .pte absent => FAIL (not skip)
bool heavy; // huge/slow: export-gated; runs only if WEBGPU_TEST_HEAVY
};
// Llama-3.2-1B linear shapes (q/o/k/v/gate/up/down + lm_head) + 4k/8k prefill.
// tol scales with K (fp32 accum depth), not M; down_proj (K=8192) is looser.
const Q4gswConfig kQ4gswConfigs[] = {
// name M K N tol_abs tol_rel req heavy
{"q_proj", 1, 2048, 2048, 1e-4f, 1e-3f, true, false},
{"kv_proj", 1, 2048, 512, 1e-4f, 1e-3f, true, false},
{"gate_proj", 1, 2048, 8192, 1e-4f, 1e-3f, true, false},
{"down_proj", 1, 8192, 2048, 1e-3f, 1e-2f, true, false}, // big-K accum
{"lm_head", 1, 2048, 128256, 1e-4f, 1e-3f, false, true},
{"q_proj_4k", 4096, 2048, 2048, 1e-4f, 1e-3f, true, false},
{"kv_proj_4k", 4096, 2048, 512, 1e-4f, 1e-3f, true, false},
{"q_proj_8k", 8192, 2048, 2048, 1e-4f, 1e-3f, false, true},
{"kv_proj_8k", 8192, 2048, 512, 1e-4f, 1e-3f, false, true},
// The M==1 configs above (q/kv/gate/down_proj) exercise the bicol 2-col
// decode GEMV (handler routes M==1 -> bicol; each reads its own per-column
// scale over 64-256 K-groups). q4gsw requires N % 8 == 0, so odd-N is not
// exportable; bicol's has1 odd-N guard is defensive (mirrors coop4
// general-N robustness).
// M>1: steel GEMM on a >=256-invocation device (K%16==0), else shmem/tiled.
{"steel", 96, 2048, 256, 1e-4f, 1e-3f, true, false}, // steel-isolating
// Same shape as "steel" run under the f16-multiply steel kernel; the f16
// rounding floor (~2.3e-4, uniform in K -- not an accumulate bug) needs a
// looser abs gate than the strict f32 1e-4. Runs whenever the device
// negotiated shader-f16 (else the f32 steel kernel; the looser gate holds).
{"steel_f16", 96, 2048, 256, 2.3e-4f, 1e-3f, true, false},
// Partial M and N steel tiles under the f16 kernel (f16 boundary masking).
{"steel_f16_edge", 70, 1024, 136, 2.3e-4f, 1e-3f, true, false},
// pwdq (packed-word dequant) backs the f16 steel path at group_size % BK ==
// 0
// (bit-exact to steel_half; the steel_f16 configs above run it at gs=32).
// These lock the gs gate at group sizes those omit: gs=64 stays on pwdq;
// gs=8 (< BK=16) falls back to the per-nibble steel_half kernel.
{"pwdq_gs64", 96, 2048, 256, 2.3e-4f, 1e-3f, true, false},
{"pwdq_gs8", 96, 2048, 256, 2.3e-4f, 1e-3f, true, false},
// f16-ACCUMULATE steel (pwdqf16acc): lossy, so a wider gate than the
// f16-multiply steel_f16 (2.3e-4). f16 accumulation error grows with K, so
// the deep-K down shape (K=8192) gets the loosest tol. Perplexity is the
// primary quality gate (see the kernel diff); this catches gross bit/index
// bugs. gs=32 (% BK == 0) selects pwdqf16acc; the sweep loads these rows
// with the enable_f16_accumulate_gemm runtime spec set.
{"pwdqf16acc", 96, 2048, 256, 2e-2f, 3e-2f, true, false},
{"pwdqf16acc_down", 128, 8192, 2048, 5e-2f, 8e-2f, true, false},
{"gate_proj_pf", 128, 2048, 8192, 1e-4f, 1e-3f, true, false}, // shmem via N
{"down_proj_pf", 128, 8192, 2048, 1e-3f, 1e-2f, true, false}, // shmem via K
{"shmem_edge", 130, 4096, 2056, 1e-4f, 1e-3f, true, false}, // partial tiles
};
// /16 ramp over the flat index; mirrors test_quantized_linear.py _ramp_input.
float q4gsw_ramp(int i) {
return static_cast<float>((i % 17) - 8) / 16.0f;
}
// Per-element abs-OR-rel tolerance helper.
bool quant_within_tol(
const float* out,
const float* golden,
int n,
float atol,
float rtol,
float* ma,
float* mr) {
float max_abs = 0.0f, max_rel = 0.0f;
bool ok = true;
for (int i = 0; i < n; i++) {
const float ae = std::abs(out[i] - golden[i]);
const float re = ae / std::max(std::abs(golden[i]), 1e-6f);
max_abs = std::max(max_abs, ae);
max_rel = std::max(max_rel, re);
if (ae > atol && re > rtol) {
ok = false;
}
}
*ma = max_abs;
*mr = max_rel;
return ok;
}
std::vector<int32_t> load_indices(const std::string& path, size_t numel) {
// Load raw little-endian int32 indices written by the export .py.
std::vector<int32_t> g(numel);
FILE* f = std::fopen(path.c_str(), "rb");
if (!f) {
return {};
}
size_t n = std::fread(g.data(), sizeof(int32_t), numel, f);
std::fclose(f);
if (n != numel) {
return {};
}
return g;
}
void test_embedding_q4gsw(
const std::string& model_path,
const std::string& indices_path,
const std::string& golden_path,
int num_indices,
int embed,
const char* label) {
// q4gsw embedding-gather vs torch golden; shapes per test_embedding_q4gsw.py.
const int out_numel = num_indices * embed;
printf(
"\n--- Test: embedding_q4gsw (%s: indices=%d, embed=%d) ---\n",
label,
num_indices,
embed);
Module module(model_path);
auto err = module.load_forward();
ASSERT_EQ(err, Error::Ok)
<< "could not load forward method (error " << (int)err << ")";
printf("Model loaded: %s\n", model_path.c_str());
std::vector<int32_t> idx32 = load_indices(indices_path, num_indices);
std::vector<float> golden = load_golden(golden_path, out_numel);
ASSERT_FALSE(idx32.empty() || golden.empty())
<< "could not load indices " << indices_path << " / golden "
<< golden_path;
// int64 at the program boundary; copy_inputs narrows to the int32 buffer.
std::vector<int64_t> idx64(idx32.begin(), idx32.end());
auto idx = make_tensor_ptr({num_indices}, std::move(idx64));
auto result = module.forward({EValue(idx)});
ASSERT_TRUE(result.ok()) << "forward failed (error " << (int)result.error()
<< ")";
const auto& outputs = result.get();
ASSERT_TRUE(!outputs.empty() && outputs[0].isTensor()) << "no tensor output";
const auto& out_tensor = outputs[0].toTensor();
ASSERT_EQ((int)out_tensor.numel(), out_numel)
<< "output numel " << (size_t)out_tensor.numel() << " != expected "
<< out_numel;
const float* out_data = out_tensor.const_data_ptr<float>();
float max_abs_err = 0.0f, max_rel_err = 0.0f;
const bool pass = quant_within_tol(
out_data,
golden.data(),
out_numel,
1e-3f,
1e-3f,
&max_abs_err,
&max_rel_err);
printf(
"Max abs error: %e Max rel error: %e (checked %d elements)\n",
max_abs_err,
max_rel_err,
out_numel);
EXPECT_TRUE(pass) << "embedding_q4gsw exceeds tolerance 1e-3 (abs AND rel)";
}
void test_rope(
const std::string& model_path,
const std::string& xq_golden_path,
const std::string& xk_golden_path,
int S,
int NH,
int NKV,
int HD,
const char* label) {
// Llama interleaved RoPE vs torch goldens; shapes/ramps per test_rope.py.
const int xq_numel = S * NH * HD;
const int xk_numel = S * NKV * HD;
const int freqs_numel = S * (HD / 2);
printf(
"\n--- Test: apply_rotary_emb (%s: S=%d,NH=%d,NKV=%d,HD=%d) ---\n",
label,
S,
NH,
NKV,
HD);
Module module(model_path);
auto err = module.load_forward();
ASSERT_EQ(err, Error::Ok)
<< "could not load forward method (error " << (int)err << ")";
printf("Model loaded: %s\n", model_path.c_str());
// ((i % mod) - off) / 16: exact in fp32, matches test_rope.py::_ramp.
auto ramp = [](int i, int mod, int off) {
return static_cast<float>((i % mod) - off) / 16.0f;
};
std::vector<float> xq(xq_numel), xk(xk_numel), fc(freqs_numel),
fs(freqs_numel);
for (int i = 0; i < xq_numel; i++) {
xq[i] = ramp(i, 17, 8);
}
for (int i = 0; i < xk_numel; i++) {
xk[i] = ramp(i, 13, 6);
}
for (int i = 0; i < freqs_numel; i++) {
fc[i] = ramp(i, 11, 5);
fs[i] = ramp(i, 7, 3);
}
auto xqt = make_tensor_ptr({1, S, NH, HD}, std::vector<float>(xq));
auto xkt = make_tensor_ptr({1, S, NKV, HD}, std::vector<float>(xk));
auto fct = make_tensor_ptr({S, HD / 2}, std::vector<float>(fc));
auto fst = make_tensor_ptr({S, HD / 2}, std::vector<float>(fs));
auto result =
module.forward({EValue(xqt), EValue(xkt), EValue(fct), EValue(fst)});
ASSERT_TRUE(result.ok()) << "forward failed (error " << (int)result.error()
<< ")";
const auto& outputs = result.get();
// Outputs in graph order [0]=xq_out, [1]=xk_out (positional; the numel check
// below guards a swap, since NH != NKV under GQA).
ASSERT_TRUE(
outputs.size() >= 2 && outputs[0].isTensor() && outputs[1].isTensor())
<< "expected 2 tensor outputs, got " << outputs.size();
const auto& xq_t = outputs[0].toTensor();
const auto& xk_t = outputs[1].toTensor();
ASSERT_TRUE(xq_t.numel() == xq_numel && xk_t.numel() == xk_numel)
<< "output shapes [" << (size_t)xq_t.numel() << ","
<< (size_t)xk_t.numel() << "] != expected [" << xq_numel << ","
<< xk_numel << "]";
const float* xq_out = xq_t.const_data_ptr<float>();
const float* xk_out = xk_t.const_data_ptr<float>();
std::vector<float> gq = load_golden(xq_golden_path, xq_numel);
std::vector<float> gk = load_golden(xk_golden_path, xk_numel);
ASSERT_FALSE(gq.empty() || gk.empty())
<< "could not load goldens " << xq_golden_path << " / " << xk_golden_path;
// Per-element abs-OR-rel on xq and xk (shared helper).
float maq = 0.0f, mrq = 0.0f, mak = 0.0f, mrk = 0.0f;
const bool pass_q =
quant_within_tol(xq_out, gq.data(), xq_numel, 1e-3f, 1e-3f, &maq, &mrq);
const bool pass_k =
quant_within_tol(xk_out, gk.data(), xk_numel, 1e-3f, 1e-3f, &mak, &mrk);
const float max_abs_err = std::max(maq, mak);
const float max_rel_err = std::max(mrq, mrk);
printf(
"Max abs error: %e Max rel error: %e (checked %d elements)\n",
max_abs_err,
max_rel_err,
xq_numel + xk_numel);
EXPECT_TRUE(pass_q && pass_k)
<< "apply_rotary_emb exceeds tolerance 1e-3 (abs AND rel)";
}
void test_prepack(
const std::string& model_path,
const std::string& golden_path,
const std::string& label = "x + const w") {
// et_vk.prepack copy vs golden; unrun copy leaves zeros. See test_prepack.py.
constexpr int n = 4;
constexpr int numel = n * n;
printf("\n--- Test: prepack (%s, %dx%d) ---\n", label.c_str(), n, n);
Module module(model_path);
auto err = module.load_forward();
ASSERT_EQ(err, Error::Ok)
<< "could not load forward method (error " << (int)err << ")";
printf("Model loaded: %s\n", model_path.c_str());
std::vector<float> golden = load_golden(golden_path, numel);
ASSERT_FALSE(golden.empty()) << "could not load golden " << golden_path;
// ((i % 13) - 6) / 16: exact in fp32, matches test_prepack.py::_inputs.
std::vector<float> x_data(numel);
for (int i = 0; i < numel; i++) {
x_data[i] = static_cast<float>((i % 13) - 6) / 16.0f;
}
auto x = make_tensor_ptr({n, n}, std::vector<float>(x_data));
auto result = module.forward({EValue(x)});
ASSERT_TRUE(result.ok()) << "forward failed (error " << (int)result.error()
<< ")";
const auto& outputs = result.get();
ASSERT_TRUE(!outputs.empty() && outputs[0].isTensor()) << "no tensor output";
const auto& out_tensor = outputs[0].toTensor();
ASSERT_EQ((int)out_tensor.numel(), numel)
<< "output numel " << (size_t)out_tensor.numel() << " != expected "
<< numel;
const float* out_data = out_tensor.const_data_ptr<float>();
float max_abs_err = 0.0f, max_rel_err = 0.0f;
// Per-element abs-OR-rel (quant_within_tol): a global rel gate spuriously
// fails near-zero outputs where rel error explodes.
const bool within = quant_within_tol(
out_data, golden.data(), numel, 1e-3f, 1e-3f, &max_abs_err, &max_rel_err);
printf(
"Max abs error: %e Max rel error: %e (checked %d elements)\n",
max_abs_err,
max_rel_err,
numel);
EXPECT_TRUE(within) << "prepack exceeds tolerance 1e-3";
}
// Reconstruct _ramp_input bit-for-bit, run the op, compare to the fp64 golden.
void test_q4gsw_config(
const Q4gswConfig& cfg,
const std::string& pte,
const std::string& golden_path) {
printf(
"\n--- Test: linear_q4gsw (%s: M=%d,K=%d,N=%d) ---\n",
cfg.name,
cfg.m,
cfg.k,
cfg.n);
Module module(pte);
// pwdqf16acc rows exercise the lossy f16-accumulate kernel, a runtime opt-in
// (default off); enable it via the backend option keyed by the registered id.
if (std::string(cfg.name).rfind("pwdqf16acc", 0) == 0) {
BackendOptions<1> opts;
opts.set_option("enable_f16_accumulate_gemm", true);
LoadBackendOptionsMap map;
ASSERT_EQ(map.set_options("VulkanBackend", opts.view()), Error::Ok);
ASSERT_EQ(module.load_forward(nullptr, nullptr, &map), Error::Ok)
<< "could not load " << pte;
} else {
ASSERT_EQ(module.load_forward(), Error::Ok) << "could not load " << pte;
}
const int in_numel = cfg.m * cfg.k;
const int out_numel = cfg.m * cfg.n;
std::vector<float> input(in_numel);
for (int i = 0; i < in_numel; i++) {
input[i] = q4gsw_ramp(i);
}
auto x = make_tensor_ptr({cfg.m, cfg.k}, std::vector<float>(input));
auto result = module.forward({EValue(x)});
ASSERT_TRUE(result.ok()) << "forward failed (error " << (int)result.error()
<< ")";
const auto& outputs = result.get();
ASSERT_TRUE(!outputs.empty() && outputs[0].isTensor()) << "no tensor output";
const auto& out_tensor = outputs[0].toTensor();
ASSERT_EQ((int)out_tensor.numel(), out_numel)
<< "output numel " << (size_t)out_tensor.numel() << " != expected "
<< out_numel;
const float* out_data = out_tensor.const_data_ptr<float>();
std::vector<float> golden = load_golden(golden_path, out_numel);
ASSERT_FALSE(golden.empty()) << "could not load golden " << golden_path;
float ma = 0.0f, mr = 0.0f;
const bool pass = quant_within_tol(
out_data, golden.data(), out_numel, cfg.tol_abs, cfg.tol_rel, &ma, &mr);
printf(
"Max abs error: %e Max rel error: %e (checked %d elements)\n",
ma,
mr,
out_numel);
EXPECT_TRUE(pass) << "linear_q4gsw " << cfg.name << " exceeds tolerance (abs "
<< cfg.tol_abs << " OR rel " << cfg.tol_rel << ")";
}
// Fused sdpa_with_kv_cache sweep config. Mirrors the Python CONFIGS table in
// test_sdpa.py exactly (name, Hq, Hkv, D, S, Cmax, input_pos).
struct SdpaConfig {
const char* name;
int hq; // query heads
int hkv; // key/value heads (GQA groups when hq != hkv)
int d; // head dim
int s; // new tokens this step
int cmax; // kv-cache capacity
int input_pos; // prior tokens already in the cache (decode)
float denom; // ramp divisor (mirrors Python); small -> large logits
bool required = false; // CI (SDPA dir set): absent .pte = FAIL, not skip
bool expect_reject = false; // load MUST fail (e.g. D%4 guard), no golden
};
const SdpaConfig kSdpaConfigs[] = {
// name Hq Hkv D S Cmax pos denom
{"gqa31_prefill", 6, 2, 8, 4, 16, 0, 16.0f}, // GQA 3:1 (original case)
{"mha_ctxodd", 4, 4, 16, 3, 8, 0, 16.0f}, // MHA; context_len=3 (odd)
{"gqa21_prefill", 8, 4, 4, 5, 16, 0, 16.0f}, // GQA 2:1; multi-token S=5
{"gqa31_decode", 6, 2, 8, 2, 16, 2, 16.0f}, // decode: 2 prior tokens
// llama3-ish GQA, D=128, S=128.
{"llama3_prefill", 24, 8, 128, 128, 256, 0, 16.0f},
// Adversarial: denom=0.5 -> peak logit ~177 (>88) overflows naive fp32 exp.
{"mha_biglogit", 4, 4, 32, 4, 16, 0, 0.5f},
// Llama 3.2 1B shape (Hq=32,Hkv=8,D=64): decode at 4k/8k ctx.
{"llama1b_decode_4k", 32, 8, 64, 1, 4096, 4095, 16.0f, /*required=*/true},
{"llama1b_decode_8k", 32, 8, 64, 1, 8192, 8191, 16.0f, /*required=*/true},
// Llama 3.2 1B shape: realistic prefill (S=128 at pos 0) + decode (S=1 at
// pos 127).
{"llama1b_prefill", 32, 8, 64, 128, 512, 0, 16.0f},
{"llama1b_decode", 32, 8, 64, 1, 512, 127, 16.0f},
// D=6 is not a multiple of 4: the head_dim%4 guard must reject it at load.
{"reject_d6",
4,
4,
6,
4,
16,
0,
16.0f,
/*required=*/false,
/*expect_reject=*/true},
// 2D-dispatch cap (>65535 wg): S=512 folds QK; S=2048 folds QK+softmax+AV
// (cap+1).
{"llama1b_prefill_512", 32, 8, 64, 512, 512, 0, 16.0f, /*required=*/true},
{"llama1b_prefill_2048",
32,
8,
64,
2048,
2048,
0,
16.0f,
/*required=*/true},
};
// Ramp denominator; mirror of test_sdpa.py::_RAMP_DENOM (keep in sync).
constexpr float kSdpaRampDenom = 16.0f;
// /denom ramp: ((i % mod) - off) / denom, exact in fp32 (power-of-two denom).
// Mirrors test_sdpa.py::_ramp.
float sdpa_ramp(int i, int mod, int off, float denom = kSdpaRampDenom) {
return static_cast<float>((i % mod) - off) / denom;
}
// Step-indexed ramp; mirrors test_sdpa.py::_ramp_t bit-for-bit. denom defaults
// to kSdpaRampDenom and must match the Python denom for bit-identity.
float sdpa_ramp_t(
int i,
int mod,
int off,
int t,
float denom = kSdpaRampDenom) {
return static_cast<float>(((i + 31 * t) % mod) - off) / denom;
}
// Multi-step replay sequences. Mirror the Python REPLAY_SEQS / Vulkan param
// sets (sdpa_test.cpp:856/867/875). Each seq_lens entry is one step replayed on
// a host-threaded KV cache (big=prefill, mid=multi-token, 1=decode).
struct SdpaSequence {
const char* name;
int hq;
int hkv;
int d;
int cmax;
std::vector<int> seq_lens;
};
const SdpaSequence kSdpaSequences[] = {
{"small", 8, 4, 4, 16, {3, 1, 1, 5, 1, 1, 2}},
{"small_d", 6, 2, 8, 16, {3, 1, 1, 5, 1, 1}},
{"llama3", 24, 8, 128, 256, {111, 1, 1, 1, 57, 1, 1}},
};
void test_sdpa_config(
const SdpaConfig& cfg,
const std::string& model_path,
const std::string& golden_path) {
// Inputs reconstruct test_sdpa.py::_det_inputs bit-for-bit (/16 exact fp32).
printf(
"\n--- Test: sdpa_with_kv_cache (%s: Hq=%d,Hkv=%d,D=%d,S=%d,Cmax=%d,pos=%d) ---\n",
cfg.name,
cfg.hq,
cfg.hkv,
cfg.d,
cfg.s,
cfg.cmax,
cfg.input_pos);
Module module(model_path);
auto err = module.load_forward();
if (cfg.expect_reject) {
// D not a multiple of 4 must be rejected at load by the head_dim guard.
ASSERT_NE(err, Error::Ok)
<< cfg.name << " loaded OK; head_dim%4 guard did not fire";
printf("PASS: %s rejected at load (error %d)\n", cfg.name, (int)err);
return;
}
ASSERT_EQ(err, Error::Ok)
<< "could not load forward method (error " << (int)err << ")";
printf("Model loaded: %s\n", model_path.c_str());
const int qn = cfg.s * cfg.hq * cfg.d;
const int kn = cfg.s * cfg.hkv * cfg.d;
const int cn = cfg.cmax * cfg.hkv * cfg.d;
const int on = cfg.s * cfg.hq * cfg.d;
std::vector<float> q(qn), k(kn), v(kn), kc(cn, 0.0f), vc(cn, 0.0f);
for (int i = 0; i < qn; i++) {
q[i] = sdpa_ramp(i, 17, 8, cfg.denom);
}
for (int i = 0; i < kn; i++) {
k[i] = sdpa_ramp(i, 13, 6, cfg.denom);
v[i] = sdpa_ramp(i, 11, 5, cfg.denom);
}
// Decode: seed cache rows [0, input_pos) with prior_k/prior_v (flat over
// input_pos*Hkv*D elements); all other rows stay zero.
const int prior_n = cfg.input_pos * cfg.hkv * cfg.d;
for (int i = 0; i < prior_n; i++) {
kc[i] = sdpa_ramp(i, 7, 3);
vc[i] = sdpa_ramp(i, 5, 2);
}
auto qt = make_tensor_ptr({1, cfg.s, cfg.hq, cfg.d}, std::vector<float>(q));
auto kt = make_tensor_ptr({1, cfg.s, cfg.hkv, cfg.d}, std::vector<float>(k));
auto vt = make_tensor_ptr({1, cfg.s, cfg.hkv, cfg.d}, std::vector<float>(v));
auto kct =
make_tensor_ptr({1, cfg.cmax, cfg.hkv, cfg.d}, std::vector<float>(kc));
auto vct =
make_tensor_ptr({1, cfg.cmax, cfg.hkv, cfg.d}, std::vector<float>(vc));
auto result = module.forward(
{EValue(qt), EValue(kt), EValue(vt), EValue(kct), EValue(vct)});
ASSERT_TRUE(result.ok()) << "forward failed (error " << (int)result.error()
<< ")";
const auto& outputs = result.get();
// Select the attention output [1,S,Hq,D] by shape; the op returns
// [k_cache, v_cache, attn_output] and a cache [1,Cmax,Hkv,D] can share numel.
int attn_idx = -1;
int attn_matches = 0;
for (size_t i = 0; i < outputs.size(); i++) {
if (!outputs[i].isTensor()) {
continue;
}
const auto& t = outputs[i].toTensor();
if (t.dim() == 4 && static_cast<int>(t.size(1)) == cfg.s &&
static_cast<int>(t.size(2)) == cfg.hq &&
static_cast<int>(t.size(3)) == cfg.d) {
attn_idx = static_cast<int>(i);
attn_matches++;
}
}
ASSERT_GE(attn_idx, 0) << "no attention output [1," << cfg.s << "," << cfg.hq
<< "," << cfg.d << "] among " << outputs.size()
<< " outputs";
ASSERT_LE(attn_matches, 1) << "ambiguous attention output: " << attn_matches
<< " tensors match shape [1," << cfg.s << ","
<< cfg.hq << "," << cfg.d << "]";
const auto& out_tensor = outputs[attn_idx].toTensor();
const float* out_data = out_tensor.const_data_ptr<float>();
std::vector<float> golden = load_golden(golden_path, on);
ASSERT_FALSE(golden.empty()) << "could not load golden " << golden_path;
float max_abs_err = 0.0f, max_rel_err = 0.0f;
const bool pass =
sdpa_within_tol(out_data, golden.data(), on, &max_abs_err, &max_rel_err);
printf(
"Max abs error: %e Max rel error: %e (checked %d elements)\n",
max_abs_err,
max_rel_err,
on);
EXPECT_TRUE(pass) << cfg.name
<< " exceeds tolerance (per-element abs 1e-4 OR rel 1e-3)";
}
// Replay one sequence: thread the op's returned (mutated) KV cache across
// steps, comparing each step's attention output to its accumulated-context
// golden.
void test_sdpa_replay(const SdpaSequence& seq, const std::string& dir) {
printf(
"\n--- Test: sdpa replay (%s: Hq=%d,Hkv=%d,D=%d,Cmax=%d, %zu steps) ---\n",
seq.name,
seq.hq,
seq.hkv,
seq.d,
seq.cmax,
seq.seq_lens.size());
const int cn = seq.cmax * seq.hkv * seq.d;
std::vector<float> kc(cn, 0.0f), vc(cn, 0.0f);
int input_pos = 0;
int k_idx = -1,
v_idx = -1; // pinned at step 0 by content (caches share numel)
for (size_t t = 0; t < seq.seq_lens.size(); t++) {
const int s = seq.seq_lens[t];
const std::string base = dir + "sdpa_" + seq.name + "_step" +
std::to_string(t) + "_S" + std::to_string(s) + "_pos" +
std::to_string(input_pos);
Module module(base + ".pte");
ASSERT_EQ(module.load_forward(), Error::Ok)
<< "could not load " << base << ".pte";
const int qn = s * seq.hq * seq.d;
const int kvn = s * seq.hkv * seq.d;
std::vector<float> q(qn), k(kvn), v(kvn);
for (int i = 0; i < qn; i++) {
q[i] = sdpa_ramp_t(i, 17, 8, static_cast<int>(t));
}
for (int i = 0; i < kvn; i++) {
k[i] = sdpa_ramp_t(i, 13, 6, static_cast<int>(t));
v[i] = sdpa_ramp_t(i, 11, 5, static_cast<int>(t));
}
auto qt = make_tensor_ptr({1, s, seq.hq, seq.d}, std::vector<float>(q));
auto kt = make_tensor_ptr({1, s, seq.hkv, seq.d}, std::vector<float>(k));
auto vt = make_tensor_ptr({1, s, seq.hkv, seq.d}, std::vector<float>(v));
auto kct =
make_tensor_ptr({1, seq.cmax, seq.hkv, seq.d}, std::vector<float>(kc));
auto vct =
make_tensor_ptr({1, seq.cmax, seq.hkv, seq.d}, std::vector<float>(vc));
auto result = module.forward(
{EValue(qt), EValue(kt), EValue(vt), EValue(kct), EValue(vct)});
ASSERT_TRUE(result.ok())
<< "forward " << base << ".pte (error " << (int)result.error() << ")";
const auto& outs = result.get();
// The op returns [k_cache, v_cache, attn_output]: attn has a unique numel;
// the two caches share numel cn, so identify them by content at step 0.
int attn_idx = -1;
std::vector<int> cache_idxs;
for (size_t i = 0; i < outs.size(); i++) {
if (!outs[i].isTensor()) {
continue;
}
const int ne = static_cast<int>(outs[i].toTensor().numel());
if (ne == qn) {
attn_idx = static_cast<int>(i);
} else if (ne == cn) {
cache_idxs.push_back(static_cast<int>(i));
}
}
ASSERT_TRUE(attn_idx >= 0 && cache_idxs.size() == 2)
<< seq.name << " step" << t << ": expected 1 attn + 2 caches";
if (t == 0) {
const float* c0 = outs[cache_idxs[0]].toTensor().const_data_ptr<float>();
const float* c1 = outs[cache_idxs[1]].toTensor().const_data_ptr<float>();
auto rows_match = [&](const float* c, const std::vector<float>& src) {
for (int i = 0; i < kvn; i++) {
if (std::abs(c[i] - src[i]) > 1e-6f) {
return false;
}
}
return true;
};
if (rows_match(c0, k) && rows_match(c1, v)) {
k_idx = cache_idxs[0];
v_idx = cache_idxs[1];
} else if (rows_match(c1, k) && rows_match(c0, v)) {
k_idx = cache_idxs[1];
v_idx = cache_idxs[0];
} else {
FAIL() << seq.name << " step0 cannot identify k/v cache by content";
}
printf(" k/v cache outputs: k_idx=%d v_idx=%d\n", k_idx, v_idx);
}
std::vector<float> golden = load_golden(base + ".golden.bin", qn);
ASSERT_FALSE(golden.empty()) << "could not load " << base << ".golden.bin";
const float* ad = outs[attn_idx].toTensor().const_data_ptr<float>();
float ma = 0.0f, mr = 0.0f;
const bool step_ok = sdpa_within_tol(ad, golden.data(), qn, &ma, &mr);
printf(
" step%zu (S=%d pos=%d ctx=%d): max abs %e rel %e\n",
t,
s,
input_pos,
input_pos + s,
ma,
mr);
EXPECT_TRUE(step_ok)
<< seq.name << " step" << t
<< " exceeds tolerance (per-element abs 1e-4 OR rel 1e-3)";
// Thread the device-written caches into the next step (K->K, V->V).
const float* kd = outs[k_idx].toTensor().const_data_ptr<float>();
const float* vd = outs[v_idx].toTensor().const_data_ptr<float>();
kc.assign(kd, kd + cn);
vc.assign(vd, vd + cn);
input_pos += s;
}
}
// Dynamic input_pos decode: ONE .pte (S=1, runtime SymInt input_pos) reused
// across decode steps. Each forward() supplies input_pos as a [1] int64 tensor;
// the backend reads it (update_symints_from_inputs) and recomputes dispatch
// state (propagate_resize) before replaying. The cache is threaded host-side
// (the Module re-copies inputs each call), so correctness hinges on the
// per-step input_pos actually being read + applied. negative=true pins
// input_pos at 0 every step (stale context_len) and asserts the run DIVERGES,
// proving the runtime input_pos + resize hook are load-bearing (no false-pass).
void test_sdpa_dynamic_decode(
const SdpaSequence& seq,
const std::string& dir,
bool negative) {
constexpr int kSteps = 6; // mirrors DYN_DECODE_STEPS in test_sdpa.py
printf(
"\n--- Test: sdpa dynamic decode%s (%s: Hq=%d,Hkv=%d,D=%d,Cmax=%d, %d steps) ---\n",
negative ? " [NEGATIVE]" : "",
seq.name,
seq.hq,
seq.hkv,
seq.d,
seq.cmax,
kSteps);
const std::string pte = dir + "sdpa_dyn_" + seq.name + ".pte";
Module module(pte);
ASSERT_EQ(module.load_forward(), Error::Ok) << "could not load " << pte;
const int cn = seq.cmax * seq.hkv * seq.d;
std::vector<float> kc(cn, 0.0f), vc(cn, 0.0f);
int k_idx = -1,
v_idx = -1; // pinned at step 0 by content (caches share numel)
bool any_mismatch = false;
for (int t = 0; t < kSteps; t++) {
const int qn = seq.hq * seq.d; // S=1
const int kvn = seq.hkv * seq.d; // S=1
std::vector<float> q(qn), k(kvn), v(kvn);
for (int i = 0; i < qn; i++) {
q[i] = sdpa_ramp_t(i, 17, 8, t);
}
for (int i = 0; i < kvn; i++) {
k[i] = sdpa_ramp_t(i, 13, 6, t);
v[i] = sdpa_ramp_t(i, 11, 5, t);
}
auto qt = make_tensor_ptr({1, 1, seq.hq, seq.d}, std::vector<float>(q));
auto kt = make_tensor_ptr({1, 1, seq.hkv, seq.d}, std::vector<float>(k));
auto vt = make_tensor_ptr({1, 1, seq.hkv, seq.d}, std::vector<float>(v));
auto kct =
make_tensor_ptr({1, seq.cmax, seq.hkv, seq.d}, std::vector<float>(kc));
auto vct =
make_tensor_ptr({1, seq.cmax, seq.hkv, seq.d}, std::vector<float>(vc));
const int64_t pos = negative ? 0 : t;
auto ipt = make_tensor_ptr({1}, std::vector<int64_t>{pos});
auto result = module.forward(
{EValue(qt),
EValue(kt),
EValue(vt),