Repository navigation
Expand file tree
/
Copy pathds4_test.c
More file actions
7480 lines (6933 loc) · 308 KB
/
Copy pathds4_test.c
File metadata and controls
7480 lines (6933 loc) · 308 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
#define DS4_SERVER_TEST
#define DS4_SERVER_TEST_NO_MAIN
#include <inttypes.h>
#include "../ds4_server.c"
#ifndef DS4_NO_GPU
#include "../ds4_gpu.h"
#include "../ds4_image.h"
#include <math.h>
bool ds4_test_dspark_cache_window_crop(void);
bool ds4_test_dspark_prefix_capture(ds4_engine *engine, const ds4_tokens *prompt);
static ds4_engine *test_engine_fast;
static ds4_engine *test_engine_quality;
static char *test_read_file(const char *path);
static const char *test_model_path(void) {
const char *model_path = getenv("DS4_TEST_MODEL");
return (model_path && model_path[0]) ? model_path : "ds4flash.gguf";
}
static bool test_env_bool(const char *name) {
const char *v = getenv(name);
return v && v[0] && strcmp(v, "0") != 0;
}
static uint32_t test_env_u32(const char *name) {
const char *v = getenv(name);
if (!v || !v[0]) return 0;
char *end = NULL;
unsigned long n = strtoul(v, &end, 10);
if (end == v) return 0;
return n > UINT32_MAX ? UINT32_MAX : (uint32_t)n;
}
static uint64_t test_env_gib(const char *name) {
const char *v = getenv(name);
if (!v || !v[0]) return 0;
char *end = NULL;
unsigned long long n = strtoull(v, &end, 10);
if (end == v || n == 0) return 0;
const uint64_t one_gib = 1024ull * 1024ull * 1024ull;
if (n > UINT64_MAX / one_gib) return UINT64_MAX;
return (uint64_t)n * one_gib;
}
static char *test_save_env(const char *name) {
const char *value = getenv(name);
if (!value) return NULL;
size_t len = strlen(value);
char *copy = malloc(len + 1);
TEST_ASSERT(copy != NULL);
if (!copy) return NULL;
memcpy(copy, value, len + 1);
return copy;
}
static void test_restore_env(const char *name, char *saved) {
if (saved) {
setenv(name, saved, 1);
free(saved);
} else {
unsetenv(name);
}
}
typedef struct {
char *cold_decode;
char *batch_selected_addr;
} test_streaming_prefill_env;
static test_streaming_prefill_env test_force_canonical_streaming_prefill(void) {
test_streaming_prefill_env saved = {
.cold_decode =
test_save_env("DS4_METAL_DISABLE_STREAMING_COLD_DECODE_PREFILL"),
.batch_selected_addr =
test_save_env("DS4_METAL_DISABLE_STREAMING_PREFILL_BATCH_SELECTED_ADDR"),
};
if (test_env_bool("DS4_TEST_SSD_STREAMING")) {
setenv("DS4_METAL_DISABLE_STREAMING_COLD_DECODE_PREFILL", "1", 1);
setenv("DS4_METAL_DISABLE_STREAMING_PREFILL_BATCH_SELECTED_ADDR", "1", 1);
}
return saved;
}
static void test_restore_canonical_streaming_prefill(
test_streaming_prefill_env saved) {
test_restore_env("DS4_METAL_DISABLE_STREAMING_COLD_DECODE_PREFILL",
saved.cold_decode);
test_restore_env("DS4_METAL_DISABLE_STREAMING_PREFILL_BATCH_SELECTED_ADDR",
saved.batch_selected_addr);
}
static ds4_backend test_model_backend(void) {
const char *backend = getenv("DS4_TEST_BACKEND");
if (backend && !strcmp(backend, "cpu")) return DS4_BACKEND_CPU;
#ifdef __APPLE__
return DS4_BACKEND_METAL;
#else
return DS4_BACKEND_CUDA;
#endif
}
static ds4_engine *test_open_engine(bool quality) {
ds4_engine *engine = NULL;
/* DS4_TEST_MTP loads the MTP head on the fast engine so the speculative
* verify regression can reuse it; draft=4 hits the multi-row verify path. */
const char *mtp = getenv("DS4_TEST_MTP");
ds4_engine_options opt = {
.model_path = test_model_path(),
.backend = test_model_backend(),
.quality = quality,
.ssd_streaming = test_env_bool("DS4_TEST_SSD_STREAMING"),
.ssd_streaming_cold = test_env_bool("DS4_TEST_SSD_STREAMING_COLD"),
.ssd_streaming_cache_experts =
test_env_u32("DS4_TEST_SSD_STREAMING_CACHE_EXPERTS"),
.ssd_streaming_cache_bytes =
test_env_gib("DS4_TEST_SSD_STREAMING_CACHE_GB"),
.ssd_streaming_preload_experts =
test_env_u32("DS4_TEST_SSD_STREAMING_PRELOAD_EXPERTS"),
.mtp_path = (mtp && mtp[0] && !quality) ? mtp : NULL,
.mtp_draft_tokens = (mtp && mtp[0] && !quality) ? 4 : 0,
.glm_mtp = test_env_bool("DS4_TEST_GLM_MTP"),
.dspark_exact_sampling = test_env_bool("DS4_TEST_MTP_EXACT"),
};
TEST_ASSERT(ds4_engine_open(&engine, &opt) == 0);
return engine;
}
static ds4_engine *test_get_engine(bool quality) {
ds4_engine **slot = quality ? &test_engine_quality : &test_engine_fast;
if (*slot) return *slot;
*slot = test_open_engine(quality);
return *slot;
}
static void test_close_engines(void) {
ds4_engine_close(test_engine_fast);
ds4_engine_close(test_engine_quality);
test_engine_fast = NULL;
test_engine_quality = NULL;
}
static void test_close_engine(bool quality) {
ds4_engine **slot = quality ? &test_engine_quality : &test_engine_fast;
ds4_engine_close(*slot);
*slot = NULL;
}
/* Qwen3.8 rewind: one token back restores the verify snapshot, anything else
* resets the recurrent state and replays the kept tokens on the next eval;
* both must land on the logits a fresh session produces for the same tokens. */
static void test_session_rewind_replay(void) {
ds4_engine *engine = test_get_engine(false);
if (!engine) return;
if (!ds4_engine_is_qwen4(engine)) {
puts("session-rewind: Qwen3.8 model required, skipped");
return;
}
ds4_session *live = NULL, *fresh = NULL;
ds4_tokens prompt = {0}, replay = {0};
char err[192] = {0};
ds4_token_score got[8], want[8];
const bool mtp = test_env_bool("DS4_TEST_GLM_MTP");
char *saved_force = test_save_env("DS4_QWEN4_SPEC_FORCE_ACCEPT");
if (mtp) setenv("DS4_QWEN4_SPEC_FORCE_ACCEPT", "1", 1);
ds4_chat_begin(engine, &prompt);
ds4_chat_append_message(engine, &prompt, "user", "Count from one to ten.");
ds4_chat_append_assistant_prefix(engine, &prompt, DS4_THINK_NONE);
TEST_ASSERT(ds4_session_create(&live, engine, 1024) == 0);
TEST_ASSERT(ds4_session_create(&fresh, engine, 1024) == 0);
if (!live || !fresh) goto cleanup;
TEST_ASSERT(ds4_session_sync(live, &prompt, err, sizeof(err)) == 0);
for (int i = 0; i < prompt.len; i++) ds4_tokens_push(&replay, prompt.v[i]);
/* Three greedy steps; prepared MTP prefixes can verify immediately,
* and forced acceptance ensures the final cycle commits a token pair. */
int last_n = 1;
for (int step = 0; step < 3; step++) {
const int first = ds4_session_argmax(live);
if (mtp) {
int acc[2] = {0, 0};
last_n = ds4_session_eval_speculative_argmax(live, first, 2, -1, acc, 2, err, sizeof(err));
TEST_ASSERT(last_n == 1 || last_n == 2);
if (last_n < 1) goto cleanup;
for (int i = 0; i < last_n; i++) ds4_tokens_push(&replay, acc[i]);
} else {
TEST_ASSERT(ds4_session_eval(live, first, err, sizeof(err)) == 0);
ds4_tokens_push(&replay, first);
}
}
if (mtp) TEST_ASSERT(last_n == 2); /* the one-token rewind below takes the snapshot path */
TEST_ASSERT(ds4_session_pos(live) == replay.len);
if (mtp) TEST_ASSERT(ds4_session_top_logprobs(live, want, 8) == 8);
/* rewind one token (snapshot path under MTP) and re-evaluate it */
const int last = replay.v[replay.len - 1];
ds4_session_rewind(live, replay.len - 1);
TEST_ASSERT(ds4_session_pos(live) == replay.len - 1);
if (mtp) {
ds4_session_snapshot rewound = {0};
TEST_ASSERT(ds4_session_save_snapshot(live, &rewound, err, sizeof(err)) == 0);
if (rewound.ptr) {
TEST_ASSERT(ds4_session_load_snapshot(fresh, &rewound, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(fresh, last, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_top_logprobs(fresh, got, 8) == 8);
TEST_ASSERT(got[0].id == want[0].id);
for (int i = 0; i < 8; i++) TEST_ASSERT(fabsf(got[i].logprob - want[i].logprob) < 2e-3f);
}
ds4_session_snapshot_free(&rewound);
}
TEST_ASSERT(ds4_session_eval(live, last, err, sizeof(err)) == 0);
/* Match the replay's prefill/decode split. A full-prefix prefill uses
* different floating-point accumulation than the final decode step. */
ds4_tokens prefix = replay;
prefix.len--;
if (!mtp) {
TEST_ASSERT(ds4_session_sync(fresh, &prefix, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(fresh, last, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_top_logprobs(fresh, want, 8) == 8);
}
/* MTP restores row zero of the verifier snapshot: re-evaluating row one
* must reproduce its original logits, including the prior decode state. */
TEST_ASSERT(ds4_session_top_logprobs(live, got, 8) == 8);
TEST_ASSERT(got[0].id == want[0].id);
for (int i = 0; i < 8; i++) TEST_ASSERT(fabsf(got[i].logprob - want[i].logprob) < 2e-3f);
/* rewind two tokens (reset + replay path) and re-evaluate both */
ds4_session_rewind(live, replay.len - 2);
TEST_ASSERT(ds4_session_pos(live) == replay.len - 2);
TEST_ASSERT(ds4_session_eval(live, replay.v[replay.len - 2], err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(live, last, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_pos(live) == replay.len);
ds4_session_free(fresh);
fresh = NULL;
TEST_ASSERT(ds4_session_create(&fresh, engine, 1024) == 0);
if (!fresh) goto cleanup;
prefix.len = replay.len - 2;
TEST_ASSERT(ds4_session_sync(fresh, &prefix, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(fresh, replay.v[replay.len - 2], err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(fresh, last, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_top_logprobs(fresh, want, 8) == 8);
TEST_ASSERT(ds4_session_top_logprobs(live, got, 8) == 8);
TEST_ASSERT(got[0].id == want[0].id);
for (int i = 0; i < 8; i++) TEST_ASSERT(fabsf(got[i].logprob - want[i].logprob) < 2e-3f);
cleanup:
ds4_tokens_free(&replay);
ds4_tokens_free(&prompt);
ds4_session_free(fresh);
ds4_session_free(live);
test_restore_env("DS4_QWEN4_SPEC_FORCE_ACCEPT", saved_force);
}
/* Issue #8 regression: the server rewinds a verified block to its start and
* re-evaluates the kept token when exact sampling crosses a tool
* sampling-mode boundary at nonzero temperature. The rewind must restore
* the pre-verify snapshot and land on the logits a session that never ran
* the verify produces for the same committed tokens; before the fix the
* rewind reset the recurrent state and the next eval replayed the whole
* kept context. Needs DS4_TEST_GLM_MTP and DS4_TEST_MTP_EXACT. */
static void test_session_rewind_resample_boundary(void) {
ds4_engine *engine = test_get_engine(false);
if (!engine) return;
if (!ds4_engine_is_qwen4(engine) || !ds4_engine_mtp_exact_sampling(engine)) {
puts("session-rewind-resample: Qwen3.8 model with DS4_TEST_MTP_EXACT required, skipped");
return;
}
ds4_session *live = NULL, *fresh = NULL;
ds4_tokens prompt = {0};
char err[192] = {0};
ds4_token_score got[8], want[8];
uint64_t rng = 12345;
ds4_chat_begin(engine, &prompt);
ds4_chat_append_message(engine, &prompt, "user", "List the primary colors, then name two fruits.");
ds4_chat_append_assistant_prefix(engine, &prompt, DS4_THINK_NONE);
TEST_ASSERT(ds4_session_create(&live, engine, 1024) == 0);
TEST_ASSERT(ds4_session_create(&fresh, engine, 1024) == 0);
if (!live || !fresh) goto cleanup;
TEST_ASSERT(ds4_session_sync(live, &prompt, err, sizeof(err)) == 0);
int acc[2] = {0, 0};
int block_start = -1;
for (int attempt = 0; attempt < 64; attempt++) {
const int tok = ds4_session_sample(live, 1.0f, 0, 1.0f, 0.0f, &rng);
block_start = ds4_session_pos(live);
const int ntok = ds4_session_eval_speculative(
live, tok, 8, ds4_token_eos(engine), 1.0f, 0, 1.0f, 0.0f, &rng,
acc, 2, err, sizeof(err));
TEST_ASSERT(ntok >= 1);
if (ntok < 1) { block_start = -1; goto cleanup; }
if (ntok == 2) break; /* an accepted two-row block to rewind */
block_start = -1;
}
TEST_ASSERT(block_start >= 0);
if (block_start < 0) goto cleanup;
/* the server resample flow: rewind to the block start, re-evaluate the
* kept token so the next sample uses the boundary's new mode */
ds4_session_rewind(live, block_start);
TEST_ASSERT(ds4_session_pos(live) == block_start);
TEST_ASSERT(ds4_session_eval(live, acc[0], err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_pos(live) == block_start + 1);
/* reference: the same committed tokens, one row at a time, no verify */
TEST_ASSERT(ds4_session_sync(fresh, &prompt, err, sizeof(err)) == 0);
const ds4_tokens *hist = ds4_session_tokens(live);
TEST_ASSERT(hist->len > prompt.len);
for (int i = prompt.len; i < hist->len; i++) {
TEST_ASSERT(ds4_session_eval(fresh, hist->v[i], err, sizeof(err)) == 0);
}
TEST_ASSERT(ds4_session_pos(fresh) == ds4_session_pos(live));
TEST_ASSERT(ds4_session_top_logprobs(live, got, 8) == 8);
TEST_ASSERT(ds4_session_top_logprobs(fresh, want, 8) == 8);
TEST_ASSERT(got[0].id == want[0].id);
for (int i = 0; i < 8; i++) TEST_ASSERT(fabsf(got[i].logprob - want[i].logprob) < 2e-3f);
cleanup:
ds4_tokens_free(&prompt);
ds4_session_free(fresh);
ds4_session_free(live);
}
/* A continued checkpoint must be loadable and immediately sampleable at a
* completed prefill chunk, including after a resumed prefill or cancellation. */
typedef struct {
ds4_session *session;
ds4_session_payload_file payload;
int pos;
bool captured;
bool cancel;
} test_qwen_prefill_checkpoint;
static void test_qwen_capture_prefill(void *ud, const char *event, int current, int total) {
test_qwen_prefill_checkpoint *p = ud;
if (strcmp(event, "prefill_chunk") || current <= 0 || current >= total || p->captured) return;
p->captured = true;
p->pos = current;
char err[192] = {0};
int rc = ds4_session_stage_payload(p->session, &p->payload, err, sizeof(err));
if (rc) fprintf(stderr, "ds4-test: prefill checkpoint at %d: %s\n", current, err);
TEST_ASSERT(rc == 0);
}
static bool test_qwen_cancel_after_checkpoint(void *ud) {
test_qwen_prefill_checkpoint *p = ud;
return p->cancel && p->captured;
}
static void test_qwen_prefill_scores_equal(ds4_session *a, ds4_session *b) {
ds4_token_score got[8], want[8];
TEST_ASSERT(ds4_session_top_logprobs(a, got, 8) == 8);
TEST_ASSERT(ds4_session_top_logprobs(b, want, 8) == 8);
for (int i = 0; i < 8; i++) {
TEST_ASSERT(got[i].id == want[i].id);
TEST_ASSERT(fabsf(got[i].logit - want[i].logit) < 1e-5f);
}
}
static void test_qwen_prefill_checkpoints(void) {
ds4_engine *engine = test_get_engine(false);
if (!engine || !ds4_engine_is_qwen4(engine)) {
puts("prefill-checkpoints: Qwen3.8 model required, skipped");
return;
}
char *saved_chunk = test_save_env("DS4_QWEN4_PREFILL_CHUNK");
setenv("DS4_QWEN4_PREFILL_CHUNK", "128", 1);
ds4_session *live = NULL, *reference = NULL, *restored = NULL;
ds4_tokens prompt = {0};
buf text = {0};
char err[192] = {0};
for (int i = 0; i < 200; i++) buf_puts(&text, "The harbor records the weather and shipping schedules. ");
ds4_encode_chat_prompt(engine, NULL, text.ptr, DS4_THINK_NONE, &prompt);
buf_free(&text);
TEST_ASSERT(prompt.len > 512);
TEST_ASSERT(ds4_session_create(&live, engine, 1024) == 0);
TEST_ASSERT(ds4_session_create(&reference, engine, 1024) == 0);
TEST_ASSERT(ds4_session_create(&restored, engine, 1024) == 0);
if (!live || !reference || !restored || prompt.len <= 512) goto cleanup;
for (int round = 0; round < 3; round++) {
test_qwen_prefill_checkpoint capture = {.session = live, .cancel = round == 2};
if (round == 2) ds4_session_invalidate(live);
ds4_tokens target = prompt;
target.len = round == 1 ? 512 : 256;
ds4_session_set_progress(live, test_qwen_capture_prefill, &capture);
ds4_session_set_cancel(live, test_qwen_cancel_after_checkpoint, &capture);
int rc = ds4_session_sync(live, &target, err, sizeof(err));
ds4_session_set_progress(live, NULL, NULL);
ds4_session_set_cancel(live, NULL, NULL);
TEST_ASSERT(rc == (capture.cancel ? DS4_SESSION_SYNC_INTERRUPTED : 0));
TEST_ASSERT(capture.captured && capture.payload.path);
TEST_ASSERT(capture.pos == (round == 1 ? 384 : 128));
if (!capture.payload.path) continue;
ds4_session_invalidate(reference);
ds4_tokens prefix = prompt;
prefix.len = capture.pos;
TEST_ASSERT(ds4_session_sync(reference, &prefix, err, sizeof(err)) == 0);
FILE *fp = fopen(capture.payload.path, "rb");
TEST_ASSERT(fp != NULL);
if (fp) {
TEST_ASSERT(ds4_session_load_payload(restored, fp, capture.payload.bytes, err, sizeof(err)) == 0);
fclose(fp);
TEST_ASSERT(ds4_session_pos(restored) == capture.pos);
/* Compare against an independent prefill, not the source's possibly
* stale logits. Also verify the serialized recurrent state by
* continuing both sessions to the same full prefix. */
test_qwen_prefill_scores_equal(restored, reference);
if (capture.cancel) test_qwen_prefill_scores_equal(live, reference);
TEST_ASSERT(ds4_session_sync(restored, &target, err, sizeof(err)) == 0);
if (capture.cancel) TEST_ASSERT(ds4_session_sync(live, &target, err, sizeof(err)) == 0);
test_qwen_prefill_scores_equal(restored, live);
}
ds4_session_payload_file_free(&capture.payload);
}
/* Rewind outside a speculative snapshot resets the recurrent graph but
* retains the transcript. A subsequent sync must replay that prefix,
* whether it appends new tokens or requests the retained prefix itself. */
for (int append = 0; append < 2; append++) {
ds4_tokens target = prompt;
target.len = 512;
TEST_ASSERT(ds4_session_sync(live, &target, err, sizeof(err)) == 0);
ds4_session_rewind(live, 128);
target.len = append ? 384 : 128;
TEST_ASSERT(ds4_session_sync(live, &target, err, sizeof(err)) == 0);
ds4_session_invalidate(reference);
TEST_ASSERT(ds4_session_sync(reference, &target, err, sizeof(err)) == 0);
test_qwen_prefill_scores_equal(live, reference);
const int token = ds4_session_argmax(reference);
TEST_ASSERT(ds4_session_eval(live, token, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(reference, token, err, sizeof(err)) == 0);
test_qwen_prefill_scores_equal(live, reference);
}
cleanup:
ds4_session_free(restored);
ds4_session_free(reference);
ds4_session_free(live);
ds4_tokens_free(&prompt);
test_restore_env("DS4_QWEN4_PREFILL_CHUNK", saved_chunk);
}
static void test_qwen_restore_reused_session(void) {
ds4_engine *engine = test_get_engine(false);
if (!engine || !ds4_engine_is_qwen4(engine) || !test_env_bool("DS4_TEST_GLM_MTP")) {
puts("qwen4-restore-reuse: Qwen3.8 with MTP required, skipped");
return;
}
char *saved_force = test_save_env("DS4_QWEN4_SPEC_FORCE_ACCEPT");
setenv("DS4_QWEN4_SPEC_FORCE_ACCEPT", "1", 1);
ds4_session *live = NULL, *reference = NULL;
ds4_tokens prompt = {0}, other = {0};
ds4_session_snapshot snapshot = {0};
char err[192] = {0};
ds4_encode_chat_prompt(engine, NULL, "Count from one to ten.", DS4_THINK_NONE, &prompt);
TEST_ASSERT(ds4_session_create(&live, engine, 1024) == 0);
TEST_ASSERT(ds4_session_create(&reference, engine, 1024) == 0);
if (!live || !reference) goto cleanup;
TEST_ASSERT(ds4_session_sync(live, &prompt, err, sizeof(err)) == 0);
for (int step = 0; step < 3; step++) {
int accepted[2];
int n = ds4_session_eval_speculative_argmax(live, ds4_session_argmax(live),
2, -1, accepted, 2, err, sizeof(err));
TEST_ASSERT(step == 0 ? n >= 1 && n <= 2 : n == 2);
if (n < 1) goto cleanup;
}
const ds4_tokens *tokens = ds4_session_tokens(live);
for (int i = 0; i < tokens->len; i++) ds4_tokens_push(&other, tokens->v[i]);
/* Same positions, different history: the old verifier snapshots must not
* survive loading this checkpoint into the already-used session. */
other.v[prompt.len / 2] = prompt.v[prompt.len / 2 + 1];
TEST_ASSERT(ds4_session_sync(reference, &other, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_save_snapshot(reference, &snapshot, err, sizeof(err)) == 0);
if (!snapshot.ptr) goto cleanup;
TEST_ASSERT(ds4_session_load_snapshot(live, &snapshot, err, sizeof(err)) == 0);
ds4_session_rewind(live, other.len - 1);
TEST_ASSERT(ds4_session_eval(live, other.v[other.len - 1], err, sizeof(err)) == 0);
ds4_tokens prefix = other;
prefix.len--;
ds4_session_invalidate(reference);
TEST_ASSERT(ds4_session_sync(reference, &prefix, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_eval(reference, other.v[other.len - 1], err, sizeof(err)) == 0);
test_qwen_prefill_scores_equal(live, reference);
/* A truncated logits read must not leave a sampleable checkpoint. */
FILE *fp = tmpfile();
TEST_ASSERT(fp != NULL);
if (fp) {
size_t bytes = 13u * sizeof(uint32_t) + (size_t)other.len * sizeof(uint32_t) + 17u;
TEST_ASSERT(bytes < snapshot.len);
TEST_ASSERT(fwrite(snapshot.ptr, 1, bytes, fp) == bytes);
rewind(fp);
TEST_ASSERT(ds4_session_load_payload(live, fp, snapshot.len, err, sizeof(err)) != 0);
TEST_ASSERT(ds4_session_argmax(live) == -1);
fclose(fp);
}
cleanup:
ds4_session_snapshot_free(&snapshot);
ds4_tokens_free(&other);
ds4_tokens_free(&prompt);
ds4_session_free(reference);
ds4_session_free(live);
test_restore_env("DS4_QWEN4_SPEC_FORCE_ACCEPT", saved_force);
}
static void test_session_snapshot_roundtrip(void) {
ds4_engine *engine = test_get_engine(false);
if (!engine) return;
ds4_session *reference = NULL;
ds4_session *restored = NULL;
ds4_session_snapshot snapshot = {0};
ds4_tokens prompt = {0};
char err[192] = {0};
ds4_token_score before[8];
ds4_token_score reference_after[8];
ds4_token_score restored_before[8];
ds4_token_score restored_after[8];
enum { GLM_MTP_SNAPSHOT_CYCLES = 16 };
int reference_accepted[GLM_MTP_SNAPSHOT_CYCLES * 2] = {0};
int reference_counts[GLM_MTP_SNAPSHOT_CYCLES] = {0};
int reference_total = 0;
const bool test_glm_mtp = test_env_bool("DS4_TEST_GLM_MTP");
#ifdef DS4_ROCM_BUILD
const float continued_logit_tolerance =
ds4_engine_is_glm53(engine) ? 1e-5f : 1e-6f;
#else
const float continued_logit_tolerance = 1e-6f;
#endif
uint32_t ctx = test_env_u32("DS4_TEST_SNAPSHOT_CTX");
if (ctx == 0) ctx = 1024;
const char *prompt_path = getenv("DS4_TEST_SNAPSHOT_PROMPT");
char *prompt_text = prompt_path && prompt_path[0] ?
test_read_file(prompt_path) : NULL;
if (prompt_path && prompt_path[0]) {
TEST_ASSERT(prompt_text != NULL);
if (!prompt_text) goto cleanup;
}
TEST_ASSERT(ds4_session_create(&reference, engine, ctx) == 0);
if (!reference) goto cleanup;
ds4_chat_begin(engine, &prompt);
ds4_chat_append_message(engine, &prompt, "user",
prompt_text ? prompt_text :
"Give one concise reason to test session restore.");
ds4_chat_append_assistant_prefix(engine, &prompt, DS4_THINK_NONE);
TEST_ASSERT(prompt.len > 0);
fprintf(stderr, "ds4-test: snapshot prompt=%d ctx=%u glm_mtp=%d\n",
prompt.len, ctx, test_glm_mtp);
TEST_ASSERT(ds4_session_sync(reference, &prompt, err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_top_logprobs(reference, before, 8) == 8);
TEST_ASSERT(ds4_session_save_snapshot(reference, &snapshot,
err, sizeof(err)) == 0);
TEST_ASSERT(snapshot.ptr != NULL && snapshot.len > 0);
if (!snapshot.ptr || snapshot.len == 0) goto cleanup;
FILE *payload = tmpfile();
TEST_ASSERT(payload != NULL);
if (!payload) goto cleanup;
TEST_ASSERT(ds4_session_save_payload(reference, payload, err, sizeof(err)) == 0);
TEST_ASSERT(ftello(payload) == (off_t)snapshot.len);
rewind(payload);
uint8_t payload_chunk[4096];
bool payload_matches = true;
for (uint64_t offset = 0; offset < snapshot.len; ) {
size_t bytes = snapshot.len - offset > sizeof(payload_chunk) ?
sizeof(payload_chunk) : (size_t)(snapshot.len - offset);
if (fread(payload_chunk, 1, bytes, payload) != bytes ||
memcmp(payload_chunk, snapshot.ptr + offset, bytes) != 0) {
fprintf(stderr, "ds4-test: snapshot payload differs at offset %llu\n",
(unsigned long long)offset);
payload_matches = false;
break;
}
offset += bytes;
}
fclose(payload);
TEST_ASSERT(payload_matches);
if (!payload_matches) goto cleanup;
if (test_glm_mtp) {
for (int cycle = 0; cycle < GLM_MTP_SNAPSHOT_CYCLES; cycle++) {
const int first = ds4_session_argmax(reference);
const int n = ds4_session_eval_speculative_argmax(
reference, first, 2, -1,
reference_accepted + reference_total, 2,
err, sizeof(err));
TEST_ASSERT(n > 0 && n <= 2);
if (n <= 0 || n > 2) goto cleanup;
reference_counts[cycle] = n;
reference_total += n;
}
} else {
TEST_ASSERT(ds4_session_eval(reference, before[0].id,
err, sizeof(err)) == 0);
}
TEST_ASSERT(ds4_session_top_logprobs(reference, reference_after, 8) == 8);
ds4_session_free(reference);
reference = NULL;
TEST_ASSERT(ds4_session_create(&restored, engine, ctx) == 0);
if (!restored) goto cleanup;
TEST_ASSERT(ds4_session_load_snapshot(restored, &snapshot,
err, sizeof(err)) == 0);
{
/* a loaded snapshot must save back byte-identical */
ds4_session_snapshot again = {0};
TEST_ASSERT(ds4_session_save_snapshot(restored, &again, err, sizeof(err)) == 0);
TEST_ASSERT(again.len == snapshot.len);
if (again.len == snapshot.len && memcmp(again.ptr, snapshot.ptr, again.len) != 0) {
size_t first = 0;
while (first < again.len && again.ptr[first] == snapshot.ptr[first]) first++;
fprintf(stderr, "ds4-test: snapshot round trip differs at byte %zu of %" PRIu64 "\n", first, again.len);
TEST_ASSERT(false);
}
ds4_session_snapshot_free(&again);
}
TEST_ASSERT(ds4_session_top_logprobs(restored, restored_before, 8) == 8);
for (int i = 0; i < 8; i++) {
if (restored_before[i].id != before[i].id ||
fabsf(restored_before[i].logit - before[i].logit) > 1e-6f) {
fprintf(stderr,
"ds4-test: snapshot before[%d] reference=(%d,%.9g) "
"restored=(%d,%.9g) delta=%.9g\n",
i, before[i].id, before[i].logit,
restored_before[i].id, restored_before[i].logit,
restored_before[i].logit - before[i].logit);
}
TEST_ASSERT(restored_before[i].id == before[i].id);
TEST_ASSERT(fabsf(restored_before[i].logit - before[i].logit) <= 1e-6f);
}
if (test_glm_mtp) {
int restored_total = 0;
int single_cycles = 0;
int double_cycles = 0;
for (int cycle = 0; cycle < GLM_MTP_SNAPSHOT_CYCLES; cycle++) {
int restored_accepted[2] = {0};
const int first = ds4_session_argmax(restored);
TEST_ASSERT(first == reference_accepted[restored_total]);
const int n = ds4_session_eval_speculative_argmax(
restored, first, 2, -1,
restored_accepted, 2, err, sizeof(err));
TEST_ASSERT(n == reference_counts[cycle]);
if (n != reference_counts[cycle]) goto cleanup;
for (int i = 0; i < n; i++) {
TEST_ASSERT(restored_accepted[i] ==
reference_accepted[restored_total + i]);
}
restored_total += n;
single_cycles += n == 1;
double_cycles += n == 2;
}
TEST_ASSERT(restored_total == reference_total);
fprintf(stderr,
"ds4-test: GLM MTP snapshot cycles=%d single=%d double=%d tokens=%d\n",
GLM_MTP_SNAPSHOT_CYCLES,
single_cycles,
double_cycles,
restored_total);
} else {
TEST_ASSERT(ds4_session_eval(restored, before[0].id,
err, sizeof(err)) == 0);
}
TEST_ASSERT(ds4_session_top_logprobs(restored, restored_after, 8) == 8);
for (int i = 0; i < 8; i++) {
if (restored_after[i].id != reference_after[i].id ||
fabsf(restored_after[i].logit - reference_after[i].logit) >
continued_logit_tolerance) {
fprintf(stderr,
"ds4-test: snapshot after[%d] reference=(%d,%.9g) "
"restored=(%d,%.9g) delta=%.9g\n",
i, reference_after[i].id, reference_after[i].logit,
restored_after[i].id, restored_after[i].logit,
restored_after[i].logit - reference_after[i].logit);
}
TEST_ASSERT(restored_after[i].id == reference_after[i].id);
TEST_ASSERT(fabsf(restored_after[i].logit -
reference_after[i].logit) <=
continued_logit_tolerance);
}
if (test_glm_mtp) {
TEST_ASSERT(ds4_session_sync(restored, &prompt,
err, sizeof(err)) == 0);
TEST_ASSERT(ds4_session_top_logprobs(restored,
restored_before, 8) == 8);
for (int i = 0; i < 8; i++) {
TEST_ASSERT(restored_before[i].id == before[i].id);
TEST_ASSERT(fabsf(restored_before[i].logit - before[i].logit) <=
1e-6f);
}
int reuse_single = 0;
int reuse_double = 0;
for (int cycle = 0; cycle < 4; cycle++) {
int cycle_accepted[2] = {0};
const int first = ds4_session_argmax(restored);
const int n = ds4_session_eval_speculative_argmax(
restored, first, 2, -1,
cycle_accepted, 2, err, sizeof(err));
TEST_ASSERT(n > 0 && n <= 2);
if (n <= 0 || n > 2) goto cleanup;
reuse_single += n == 1;
reuse_double += n == 2;
}
fprintf(stderr,
"ds4-test: GLM MTP context reuse single=%d double=%d\n",
reuse_single,
reuse_double);
}
cleanup:
free(prompt_text);
ds4_tokens_free(&prompt);
ds4_session_snapshot_free(&snapshot);
ds4_session_free(restored);
ds4_session_free(reference);
}
static uint64_t test_round_up_u64(uint64_t n, uint64_t align) {
return (n + align - 1) & ~(align - 1);
}
static uint32_t test_float_ordered_u32(float value) {
uint32_t bits;
memcpy(&bits, &value, sizeof(bits));
return (bits & 0x80000000u) != 0 ? ~bits : bits | 0x80000000u;
}
typedef struct {
size_t mismatch_count;
uint32_t max_ulp;
float max_abs;
} test_float_compare_stats;
static test_float_compare_stats test_compare_float_bits(
const float *reference,
const float *actual,
size_t count) {
test_float_compare_stats stats = {0};
for (size_t i = 0; i < count; i++) {
if (memcmp(&reference[i], &actual[i], sizeof(float)) != 0) {
stats.mismatch_count++;
}
const uint32_t ref_ordered = test_float_ordered_u32(reference[i]);
const uint32_t actual_ordered = test_float_ordered_u32(actual[i]);
const uint32_t ulp = ref_ordered > actual_ordered
? ref_ordered - actual_ordered
: actual_ordered - ref_ordered;
if (ulp > stats.max_ulp) stats.max_ulp = ulp;
const float abs_error = fabsf(reference[i] - actual[i]);
if (abs_error > stats.max_abs) stats.max_abs = abs_error;
}
return stats;
}
#if defined(__APPLE__)
static const uint32_t test_copy_f32_patterns[] = {
0x00000000u, 0x80000000u, /* signed zero */
0x7f800000u, 0xff800000u, /* infinities */
0x7fc00000u, 0x7fc12345u, 0xffc54321u, 0x7fa00001u, /* NaNs */
0x00000001u, 0x007fffffu, 0x80000001u, 0x807fffffu, /* F32 subnormals */
0x32ffffffu, 0x33000000u, 0x33000001u,
0x337fffffu, 0x33800000u, 0x33800001u, /* minimum F16 subnormal boundary */
0x387fbfffu, 0x387fc000u, 0x387fffffu,
0x38800000u, 0x38800001u, /* maximum subnormal/minimum normal boundary */
0x3f800fffu, 0x3f801000u, 0x3f801001u,
0x3f802fffu, 0x3f803000u, 0x3f803001u, /* round-to-nearest ties */
0x477fdfffu, 0x477fe000u, 0x477fefffu,
0x477ff000u, 0x477ff001u, 0x47800000u, /* maximum/overflow boundary */
0xc77fe000u, 0xbf801000u, 0x3eaaaaabu, 0xbeaaaaabu,
};
static void test_fill_copy_f32_patterns(void *dst, uint32_t n, uint32_t salt) {
uint8_t *bytes = dst;
const uint32_t n_patterns =
(uint32_t)(sizeof(test_copy_f32_patterns) / sizeof(test_copy_f32_patterns[0]));
for (uint32_t i = 0; i < n; i++) {
const uint32_t bits = test_copy_f32_patterns[(i + salt) % n_patterns];
memcpy(bytes + (uint64_t)i * sizeof(bits), &bits, sizeof(bits));
}
}
#endif
static uint16_t test_float_to_f16(float f) {
union {
float f;
uint32_t u;
} v = { .f = f };
uint32_t sign = (v.u >> 16) & 0x8000u;
int32_t exp = (int32_t)((v.u >> 23) & 0xffu) - 127 + 15;
uint32_t mant = v.u & 0x7fffffu;
if (exp <= 0) {
if (exp < -10) return (uint16_t)sign;
mant |= 0x800000u;
uint32_t shift = (uint32_t)(14 - exp);
uint32_t half_mant = mant >> shift;
if ((mant >> (shift - 1)) & 1u) half_mant++;
return (uint16_t)(sign | half_mant);
}
if (exp >= 31) return (uint16_t)(sign | 0x7c00u);
uint32_t half = sign | ((uint32_t)exp << 10) | (mant >> 13);
if (mant & 0x1000u) half++;
return (uint16_t)half;
}
static float test_f16_to_f32(uint16_t h) {
uint32_t sign = (uint32_t)(h & 0x8000u) << 16;
uint32_t exp = (h >> 10) & 0x1fu;
uint32_t mant = h & 0x03ffu;
uint32_t bits;
if (exp == 0) {
if (mant == 0) {
bits = sign;
} else {
exp = 1;
while ((mant & 0x0400u) == 0) {
mant <<= 1;
exp--;
}
mant &= 0x03ffu;
bits = sign | ((exp + 127u - 15u) << 23) | (mant << 13);
}
} else if (exp == 31) {
bits = sign | 0x7f800000u | (mant << 13);
} else {
bits = sign | ((exp + 127u - 15u) << 23) | (mant << 13);
}
float f;
memcpy(&f, &bits, sizeof(f));
return f;
}
static void test_fill_q8_0_weights(uint8_t *weights,
uint32_t in_dim,
uint32_t out_dim,
uint32_t seed) {
const uint32_t blocks = in_dim / 32u;
const uint64_t row_bytes = (uint64_t)blocks * 34u;
for (uint32_t o = 0; o < out_dim; o++) {
uint8_t *row = weights + (uint64_t)o * row_bytes;
for (uint32_t b = 0; b < blocks; b++) {
float vals[32];
float amax = 0.0f;
for (uint32_t i = 0; i < 32; i++) {
const uint32_t k = b * 32u + i;
const int v = (int)((o * 17u + k * 23u + (o ^ k) * 3u +
seed * 29u + ((o + seed) ^ k) * 5u) % 67u) - 33;
vals[i] = (float)v / 96.0f;
float av = fabsf(vals[i]);
if (av > amax) amax = av;
}
const uint16_t scale_bits = test_float_to_f16(amax / 127.0f);
const float scale = test_f16_to_f32(scale_bits);
memcpy(row + b * 34u, &scale_bits, sizeof(scale_bits));
int8_t *qs = (int8_t *)(row + b * 34u + 2u);
for (uint32_t i = 0; i < 32; i++) {
int q = scale != 0.0f ? (int)lrintf(vals[i] / scale) : 0;
if (q > 127) q = 127;
if (q < -128) q = -128;
qs[i] = (int8_t)q;
}
}
}
}
static void test_metal_f16_matvec_fast_nr0_4(void) {
/*
* This is the short regression for the long-context repetition failure.
* Decode uses one-token F16 matvecs for several DS4 projections; the fast
* nr0=4 variant must be numerically equivalent to the plain kernel.
*/
const uint32_t in_dim = 4096;
const uint32_t out_dim = 512;
const uint64_t weight_bytes = (uint64_t)in_dim * out_dim * sizeof(uint16_t);
const uint64_t weight_alloc = test_round_up_u64(weight_bytes, (uint64_t)getpagesize());
void *weights_raw = NULL;
TEST_ASSERT(posix_memalign(&weights_raw, (size_t)getpagesize(), (size_t)weight_alloc) == 0);
if (!weights_raw) return;
uint16_t *weights = weights_raw;
memset(weights, 0, (size_t)weight_alloc);
for (uint32_t o = 0; o < out_dim; o++) {
for (uint32_t i = 0; i < in_dim; i++) {
float w = (float)((int)((o * 3u + i * 5u) % 23u) - 11) / 64.0f;
weights[(uint64_t)o * in_dim + i] = test_float_to_f16(w);
}
}
ds4_gpu_tensor *x = ds4_gpu_tensor_alloc((uint64_t)in_dim * sizeof(float));
ds4_gpu_tensor *out = ds4_gpu_tensor_alloc((uint64_t)out_dim * sizeof(float));
TEST_ASSERT(x != NULL);
TEST_ASSERT(out != NULL);
if (!x || !out) {
ds4_gpu_tensor_free(x);
ds4_gpu_tensor_free(out);
free(weights_raw);
return;
}
float *x_host = malloc((size_t)in_dim * sizeof(float));
float *out_host = malloc((size_t)out_dim * sizeof(float));
TEST_ASSERT(x_host != NULL);
TEST_ASSERT(out_host != NULL);
if (!x_host || !out_host) {
free(x_host);
free(out_host);
ds4_gpu_tensor_free(x);
ds4_gpu_tensor_free(out);
free(weights_raw);
return;
}
for (uint32_t i = 0; i < in_dim; i++) {
x_host[i] = (float)((int)(i % 31u) - 15) / 32.0f;
}
TEST_ASSERT(ds4_gpu_tensor_write(x, 0, x_host, (uint64_t)in_dim * sizeof(float)) != 0);
TEST_ASSERT(ds4_gpu_set_model_map(weights_raw, weight_alloc) != 0);
ds4_gpu_set_quality(false);
TEST_ASSERT(ds4_gpu_matmul_f16_tensor(out, weights_raw, weight_alloc, 0,
in_dim, out_dim, x, 1) != 0);
TEST_ASSERT(ds4_gpu_tensor_read(out, 0, out_host, (uint64_t)out_dim * sizeof(float)) != 0);
float max_abs = 0.0f;
for (uint32_t o = 0; o < out_dim; o++) {
float ref = 0.0f;
for (uint32_t i = 0; i < in_dim; i++) {
float w = (float)((int)((o * 3u + i * 5u) % 23u) - 11) / 64.0f;
ref += w * x_host[i];
}
float err = fabsf(out_host[o] - ref);
if (err > max_abs) max_abs = err;
}
TEST_ASSERT(max_abs < 0.02f);
free(x_host);
free(out_host);
ds4_gpu_tensor_free(x);
ds4_gpu_tensor_free(out);
free(weights_raw);
}
static void test_metal_f16_prefill_matmul(void) {
const uint32_t in_dim = 128;
const uint32_t out_dim = 64;
const uint32_t n_tok = 128;
const uint64_t weight_bytes = (uint64_t)out_dim * in_dim * sizeof(uint16_t);
const uint64_t weight_alloc = test_round_up_u64(weight_bytes, (uint64_t)getpagesize());
const uint64_t x_bytes = (uint64_t)n_tok * in_dim * sizeof(float);
const uint64_t out_bytes = (uint64_t)n_tok * out_dim * sizeof(float);
void *weights_raw = NULL;
TEST_ASSERT(posix_memalign(&weights_raw, (size_t)getpagesize(), (size_t)weight_alloc) == 0);
if (!weights_raw) return;
uint16_t *weights = weights_raw;
memset(weights, 0, (size_t)weight_alloc);
for (uint32_t o = 0; o < out_dim; o++) {
for (uint32_t i = 0; i < in_dim; i++) {
const int v = (int)((o * 11u + i * 13u + (o ^ i) * 5u) % 61u) - 30;
weights[(uint64_t)o * in_dim + i] = test_float_to_f16((float)v / 96.0f);
}
}
ds4_gpu_tensor *x = ds4_gpu_tensor_alloc(x_bytes);
ds4_gpu_tensor *out = ds4_gpu_tensor_alloc(out_bytes);
TEST_ASSERT(x != NULL);
TEST_ASSERT(out != NULL);
if (!x || !out) {
ds4_gpu_tensor_free(x);
ds4_gpu_tensor_free(out);
free(weights_raw);
return;
}
float *x_host = malloc((size_t)x_bytes);
float *out_host = malloc((size_t)out_bytes);
TEST_ASSERT(x_host != NULL);
TEST_ASSERT(out_host != NULL);
if (!x_host || !out_host) {
free(x_host);
free(out_host);
ds4_gpu_tensor_free(x);
ds4_gpu_tensor_free(out);
free(weights_raw);
return;