Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
160 commits
Select commit Hold shift + click to select a range
08856bc
ggml-agx: add initial architecture and 6-pillar M4 Max AGX3 design spec
astaff Nov 27, 2026
59f151c
ggml-agx: Pillar 1/4/6 direct drm_asahi CDM runtime, Borealis compile…
astaff Nov 27, 2026
f1c9900
ggml-agx: Pillar 2 128B super-tile repacking, branchless GPR decode &…
astaff Nov 27, 2026
d0676d0
ggml-agx: Pillar 4/5 hybrid 40-ring SWA + 8-paged KV attention & GPU …
astaff Nov 27, 2026
0d6e472
ggml-agx: Pillar 3 48-pass Inkling 2 Mini single-CDM engine, DSpark M…
astaff Nov 27, 2026
29d6c43
ggml-agx: add numerics, repack, Borealis shaderdb, M4 Max roofline & …
astaff Nov 27, 2026
446cc41
ggml-agx: auto-flush CDM ring, 32 MiB arg ring, and zero-split offload
astaff Nov 27, 2026
5aefe37
ggml-agx: fix MUL_MAT F32/BF16/F16/NVFP4/Q8_0 precision and hoisted MMA
astaff Nov 27, 2026
08894e1
ggml-agx: fix FLASH_ATTN_EXT, MUL_MAT_ID (F32/BF16/NVFP4/Q8_0), and M…
astaff Nov 27, 2026
f30f085
ggml-agx: full RAW/WAR/WAW/scratch hazard tracking and generic op fixes
astaff Nov 27, 2026
f3c4e3c
ggml-agx: add per-node bisector, throughput profiler, and M=512 MMA r…
astaff Nov 27, 2026
3555b4e
ggml-agx: pre-templated zero-alloc G17P Ioto encoder and GGML_AGX_STA…
astaff Nov 27, 2026
1447853
ggml-agx: Pillar 2 Wave 1 (coalesced sh_y, TILE_MODE=2, TILE_K=16, K_…
astaff Nov 27, 2026
6d54ef2
ggml-agx: Pillar 3 Wave 1 (bitonic topk/argsort + 7 Inkling-2 DAG fus…
astaff Nov 27, 2026
027f422
ggml-agx: Pillar 4/5 Wave 1 (direct M=1 MoE, 32M hoisted MoE GEMM, ti…
astaff Nov 27, 2026
cf1cdb1
ggml-agx: expand Borealis shaderdb audit to 36 kernels covering Wave 1
astaff Nov 27, 2026
f2bd4a1
ggml-agx: Pillar 1 Wave 2 (zero-memset root upload + 64-slot Ioto fas…
astaff Nov 27, 2026
83bc233
ggml-agx: Pillar 2 Wave 2 (SWAR2 E2M1, Q8 u16 magic-bias, double-buff…
astaff Nov 27, 2026
17cf2e9
ggml-agx: Pillar 3 Wave 2 (GGML_OP_ARANGE 0-split DSpark + ctrl/mix/w…
astaff Nov 27, 2026
1a0a258
ggml-agx: Pillar 5 Wave 2 (56-GPR direct M=1 MoE + 4x1 TILE_K=32 1053…
astaff Nov 27, 2026
b830efd
ggml-agx: Wave 3 Pillar 1/6 zero-encode steady-state CDM replay & lea…
astaff Nov 27, 2026
b1027bf
ggml-agx: Wave 3 Pillar 2 6-op SWAR2 NVFP4 (482 GB/s) & 4x1 TILE_K=32…
astaff Nov 27, 2026
60eff1b
ggml-agx: Wave 3 Pillar 4/5 8x8 coopmat Flash-Attention, magic Q8_0 K…
astaff Nov 27, 2026
716f54c
ggml-agx: Wave 3 Pillar 3 VA-dependency wave scheduler, Q8_0 SET_ROWS…
astaff Nov 27, 2026
d7ea93e
ggml-agx: expand Wave 3 Borealis shaderdb (43 kernels) & M=8 GEMM roo…
astaff Nov 27, 2026
a574a85
ggml-agx: Wave 4 Pillar 1/6 SLC_PIN arena, AGX0_Repack probe fix, meg…
astaff Nov 27, 2026
2305e52
ggml-agx: Wave 4 Pillar 2 load-time W_attn_in coalescing, M=2..8 vec-…
astaff Nov 27, 2026
be09b32
ggml-agx: Wave 4 Pillar 4/5 fused 2-stage MoE (scale2 + SiTU + wsum),…
astaff Nov 27, 2026
a10bd87
ggml-agx: Wave 4 Pillar 3 48-pass Inkling-2 megakernel (63.79 t/s B=1…
astaff Nov 27, 2026
92cbea6
ggml-agx: expand Wave 4 Borealis shaderdb (68 kernels) & live step-1 …
astaff Nov 27, 2026
de5110f
ggml-agx: Wave 5 Pillar 0 formal TLA+ spec (AgxPersistentW5 128B USC …
astaff Nov 27, 2026
0fd7876
ggml-agx: Wave 5 Pillar 1 sh_w 64x64 prefill MMA (10.12 TFLOPS Q8_0, …
astaff Nov 27, 2026
3dc1023
ggml-agx: Wave 5 Pillar 2 matQ-pinned Flash-Attn prefill (2.22x), Row…
astaff Nov 27, 2026
a3a2de8
ggml-agx: Wave 5 Pillar 3 40-WG SENSE_BARRIER B=1 megakernel (70.4 t/…
astaff Nov 27, 2026
f083af0
ggml-agx: expand Wave 5 Borealis shaderdb audit to 95 kernels (100% 0…
astaff Nov 27, 2026
9e61b04
ggml-agx: Wave 6 6A+6B — 64N×32M MoE W-prefetch + lean s0/s1/s5/s6 NVFP4
astaff Nov 27, 2026
e570176
ggml-agx: Wave 6 6C+6C-2 — ITEM_FUSED_MOE prefill fusion + zero-divid…
astaff Nov 27, 2026
6cae349
ggml-agx: Wave 6 6D-2 — M=2..4 DSpark 40-WG SENSE_BARRIER hybrid supe…
astaff Nov 27, 2026
7d5b05b
ggml-agx: Wave 6 6E — M=2..4 vec-mat fast path + M=5..8 shared f16vec…
astaff Nov 27, 2026
71a594d
ggml-agx: expand Borealis shaderdb audit to 100 kernels (Wave 6 6D + 6E)
astaff Nov 27, 2026
f455c0f
ggml-agx: Wave 7 prefill fusions — rel=48/48 alias_tmp_bo, sconv=192,…
astaff Nov 28, 2026
43ba165
ggml-agx: Wave 7 MMA & MoE — direct-register K=64 MMA, zero-atomic mo…
astaff Nov 28, 2026
1e47d69
ggml-agx: Wave 7 decode & DSpark — 512T B=1 hybrid (72.2 t/s) + fused…
astaff Nov 28, 2026
ea0d540
ggml-agx: add Wave 8 formal TLA+ spec (AgxWave8Ceiling.tla, 10,270 st…
astaff Nov 28, 2026
d5ac410
ggml-agx: Wave 8 prefill graph & FlashAttn — 8184B SRAM, SWA skip, re…
astaff Nov 28, 2026
7f250fe
ggml-agx: Wave 8 MMA & MoE — 11.40 TFLOPS Q8_0 MMA, SLC-pinned FP16 h…
astaff Nov 28, 2026
fdf1f33
ggml-agx: Wave 8 DSpark MTP — 5-disp inject MK (0.08ms) + SLC-pinned …
astaff Nov 28, 2026
07a69d6
ggml-agx: add Wave 9 formal TLA+ spec (AgxWave9Breakthrough.tla, 154,…
astaff Nov 28, 2026
2311c54
ggml-agx: Wave 9 250K+ O(n_kv) banded RelBias + 66-GPR32 FlashAttn pr…
astaff Nov 28, 2026
1099272
ggml-agx: Wave 9 Borealis norobust + K_PAIRS=2 SH_X_PAD=2 MMA/MoE (11…
astaff Nov 28, 2026
22ac141
ggml-agx: Wave 9 MTP — P-core double-WFE (0.088ms drm_oh), 2.60ms dra…
astaff Nov 28, 2026
4e0918e
ggml-agx: Wave 10 formal ceiling model (AgxWave10Ceiling.tla, TLC 0 e…
astaff Nov 28, 2026
5ede827
ggml-agx: Wave 10 FA Br=32, 3x-GQA PASS_MODE=0/4, batched B>1, startu…
astaff Nov 28, 2026
f02263a
ggml-agx: Wave 10 MMA t0 128T (12.5 TF), MoE 128T 8KB SRAM + 8M batch…
astaff Nov 28, 2026
0ddb546
ggml-agx: Wave 10 MTP/decode banded FA (250K+), FWHT64 Q8_0 KV fix, S…
astaff Nov 28, 2026
437b57b
ggml-agx: Wave 11 DSpark large-M chunked inject (Mode 14), load-time …
astaff Nov 28, 2026
8d2b14a
ggml-agx: Wave 11 pair-transposed XOR-swizzled MMA (12.69/12.83 TF, 0…
astaff Nov 28, 2026
d9b9d83
ggml-agx: Wave 12 MoE DOWN_NOSCATTER (-31% insts) + BF16 down_bo (pp2…
astaff Nov 28, 2026
d1c3673
ggml-agx: Wave 12 inline BF16 MMA RelBias in Br=32 FA + DSpark ShortC…
astaff Nov 28, 2026
edaa9b8
ggml-agx: Wave 12 M=2..8 concurrent QKV/RelBias split, fused LM+Softc…
astaff Nov 28, 2026
4e9fa00
ggml-agx: Wave 13 massive cleanup, env collapse & formal verification…
astaff Nov 28, 2026
be8b3b9
ggml-agx: add minimal 5-13 state TLA+ bottleneck & barrier optimizer …
astaff Nov 28, 2026
da154ac
ggml-agx: Wave 14 TLA+-guided prefill & DSpark decode fusions (pp2048…
astaff Nov 28, 2026
10bfc1c
ggml-agx: Wave 15 multi-seq batch megakernel, 32-ActSG M=2..4 MoE & D…
astaff Nov 28, 2026
afa5d36
agx(wave17): zero-SENSE_BARRIER 32-ActSG HM3/HM4 deconstruction for M…
astaff Nov 28, 2026
fbb80ce
agx(wave18): 32-ActSG 2x-prefetch Q8_0_8BLK M=2..4 W_o + unbiased ada…
astaff Nov 28, 2026
9e0aa29
agx(wave19b): fuse HM4 Stage 0+1 (-36 dispatches/barriers, Batch 8x 1…
astaff Nov 28, 2026
fb3dd4b
ggml-agx + sampling: Wave 24 WB compute buffer + zero-copy fast greedy
astaff Nov 29, 2026
3d5ad66
ggml-agx: Wave 25b elide HM4 HAS_LOOP=0 k_rollback STRONG_USC (-11 ba…
astaff Nov 29, 2026
754b3f4
ggml-agx: allow unit-scale llama-quantize NVFP4 MoE layers in Inkling…
astaff Nov 29, 2026
04d8484
sampling + ggml-agx: Wave 29 fast_greedy ignore_eos support (+1.0-1.4…
astaff Nov 29, 2026
58d1490
ggml-agx: Wave 30 40-core 72/216-WG ctrl_up_mix (-1.4 to -1.7 ms/step…
astaff Nov 29, 2026
bfadccc
ggml-agx: Wave 31 direct-L1 FP16 288-WG W_o (-0.18 ms/step, Code 1k 8…
astaff Nov 29, 2026
c04fa9e
ggml-agx: Wave 32 512T 1-barrier batched depth_mix in hm3_s0 (-0.20 m…
astaff Nov 29, 2026
90aa493
ggml-agx: Wave 33 1-barrier batched depth_mix + msg_mix in hm4_s0 (Co…
astaff Nov 29, 2026
3a76ba2
ggml-agx: Wave 34 zero-realloc DSpark Markov early-stop + M=3 attn_in…
astaff Nov 29, 2026
eb45ffc
ggml-agx: add opt-in GGML_AGX_STAGE_PROFILE and Borealis GGML_AGX_DUM…
astaff Nov 29, 2026
853a6c7
ggml-agx: Wave 35 modernize M=1 onto Wave 30-34 pipeline (oQ4e 59.8->…
astaff Nov 29, 2026
42cb776
ggml-agx: Wave 36C coalesced vec4 ctrl_up_mix + d0+=64 ctrl_norm_down…
astaff Nov 29, 2026
c1ae453
ggml-agx: Wave 37 BF16 loop controllers (-113 MB/step) + BATCH_M=3 + …
astaff Nov 29, 2026
cd6788e
ggml-agx: Wave 38A Split-K GQA=3 M=1 FlashAttention (TK=4 prefetch, 1…
astaff Nov 29, 2026
26d2a27
ggml-agx: Wave 38B M=1 fused router->top8, ctrl_up_mix->norm, and MoE…
astaff Nov 29, 2026
9378c13
ggml-agx: Wave 38C PP inline F32/F16/BF16 rel_bias (48/48), coalesced…
astaff Nov 29, 2026
0c8c9e6
ggml-agx: Wave 39 M=1 FA cooperative 1x KV load, 2-phase Global loop,…
astaff Nov 29, 2026
453c460
ggml-agx: Wave 39 async CDM submit, timeline events, and <=1 GiB AGX0…
astaff Nov 29, 2026
42d9145
ggml-agx: Wave 39 occupancy-first M=1 GEMV (lean attn_in/s6, direct-F…
astaff Nov 29, 2026
48c7b2a
ggml-agx: Wave 39 SLC-pinned 18.9 MB F32 s3 weighted-accum + zero-top…
astaff Nov 29, 2026
f7c6f86
ggml-agx: Wave 40 M=5..8 fastpath (s5/s6 64-slot dedup, FA PF4_BR=24,…
astaff Nov 30, 2026
33d70eb
speculative: Wave 40 cost-calibrated dynamic draft-width planner for …
astaff Nov 30, 2026
975d61d
speculative: Wave 40 v2 CDM-replay-aware sticky regime planner for DS…
astaff Nov 30, 2026
6749668
ggml-agx: Wave 41 MAX_GROUP=4 ActSG=32 grouped M=2..8 MoE (s5/s6) + s…
astaff Nov 30, 2026
74486d0
ggml-agx: Wave 41 M=1 turbo (dedicated inline SWA rel_bias, single-di…
astaff Nov 30, 2026
89e31a7
ggml-agx: gate m8grp behind GGML_AGX_MOE_M8_GRP=1 (restores W40 bit-e…
astaff Nov 30, 2026
5ca0df8
ggml-agx: Wave 42 bloat-kill & ActSG occupancy fixes across FlashAttn…
astaff Nov 30, 2026
35cc8c5
ggml-agx: Wave 42 v4 64T full-supertile tn-major 0-barrier 32-ActSG 0…
astaff Nov 30, 2026
8e281b0
ggml-agx: Wave 42 8-slot M=1..8 target-only CDM replay cache (n_nodes…
astaff Nov 30, 2026
4198e7d
ggml-agx: Wave 43 PP & llama-server wins (sconv fusion fix, 256T WG_M…
astaff Nov 30, 2026
f7afb5a
ggml-agx: Wave 43 M=1 non-MoE fastpath (0x1D default: rtr 64x512T, qk…
astaff Nov 30, 2026
d0323db
ggml-agx: Wave 44 FA (0x1F default: SB TK=6 + short-ctx FP2 + INLINE_…
astaff Nov 30, 2026
c6fdade
ggml-agx: Wave 44 M=1 lean MoE s6 (+1.54 t/s) + barr=0 W_o A/B variants
astaff Nov 30, 2026
d2111b1
ggml-agx: Wave 44 PP fuse (strided 4D SET_ROWS Full-layer sconv/sprep…
astaff Nov 30, 2026
81d75ff
ggml-agx: Wave 45 FA (ActSG=32 m1tk6sb_w45v2 + 8-SG p2wide_w45 + 40/6…
astaff Nov 30, 2026
dfabd41
ggml-agx: Wave 45 M=1 fastpath (0x12 default: 4-pair/in-reg hm3/hm4 s…
astaff Nov 30, 2026
c721e1a
ggml-agx: Wave 45 PP (1-pass linear RMSNorm WG_M=32 ctrl_norm_down_w4…
astaff Nov 30, 2026
e9c7d09
ggml-agx: Wave 46 M=1 decode (GGML_AGX_W46_M1=0x5F: 512T attn_in, 256…
astaff Nov 30, 2026
fdf5661
ggml-agx: Wave 46 PP (hoisted BF16/F32 MMA GEMM + swiz_m=16 SLC reuse…
astaff Nov 30, 2026
588908e
ggml-agx/speculative: Wave 46 DSpark Planner v3.3 (2-tier k=0 bypass …
astaff Nov 30, 2026
ac6580c
ggml-agx/server: Wave 47 M=1 Crush (GGML_AGX_W47_M1=0x05: auto backen…
astaff Nov 30, 2026
8c02f90
ggml-agx: Wave 47 PP MoE v4 (20-ActSG TK64 flat sh_x[256] + WG-unifor…
astaff Nov 30, 2026
c136076
ggml-agx: Wave 48 PP MoE s3_w2 20-ActSG (<=88 GPR32, GGML_AGX_W48_PP=…
astaff Nov 30, 2026
616d2b4
ggml-agx: Wave 48 M=1 1024T attn_in (37 GPR32, GGML_AGX_W48_M1=0x04)
astaff Nov 30, 2026
f4b5664
ggml-agx: Wave 49 lean 1-pass PP FlashAttention (hoist Rcur, q_seq, F…
astaff Nov 30, 2026
776fab0
ggml-agx: Wave 49 MK/HOST profilers + DSpark GPU greedy argmax & plan…
astaff Nov 30, 2026
3bdabfa
ggml-agx: Wave 50 M=2..8 verify GPU optimizations (GGML_AGX_W50_M48=0…
astaff Nov 30, 2026
95d230f
ggml-agx: Wave 50 SPEC_TIMER + DSpark draft small-graph cache & host …
astaff Nov 30, 2026
97f92cc
ggml-agx: Wave 51 M=4..8 grouped MoE (64T parallel moe_group_m8_w51, …
astaff Nov 30, 2026
46b943e
ggml-agx: Wave 51 DSPARK MK - ActSG=32 NVFP4 LM Head (M<=4) + Sorted-…
astaff Nov 30, 2026
ff6777f
ggml-agx: Wave 51 M=2..8 lean ActSG=32 FlashAttention (GGML_AGX_W51_F…
astaff Nov 30, 2026
6bc942c
ggml-agx,speculative: Wave 52 deterministic MoE mode + fixed-N DSpark…
astaff Nov 30, 2026
4069daf
ggml-agx: Wave 52 M=2..8 non-MoE bloat killer (GGML_AGX_W52_NONMOE=0x0F)
astaff Nov 30, 2026
711a2f7
ggml-agx: fix async prefill AGX_Host/scratch race (32k M=1 crash & 4k…
astaff Nov 30, 2026
4761477
ggml-agx: Wave 53 DSpark MK Body & M=8 W2 Radix-Select (GGML_AGX_W53_…
astaff Nov 30, 2026
abf375d
ggml-agx: Wave 55 direct-register AGX3B f32matmad8x8 W_o (GGML_AGX_W5…
astaff Nov 30, 2026
26cb428
speculative: Wave 55 DSpark planner acceptance yield & bypass fixes (…
astaff Nov 30, 2026
cf57e10
ggml-agx: Wave 56 Direct-Reg f32matmad8x8 LM Head + HM3 Router/Top-8 …
astaff Nov 30, 2026
fc865e8
ggml-agx: Wave 58 256T 8-SG Split-K Direct-Reg f32matmad8x8 Q8_0 W_o …
astaff Nov 30, 2026
8c8f3c7
ggml-agx: Wave 60 typed AgxHmSyncLayout + AgxSlcScratchLayout with co…
astaff Nov 30, 2026
34a74af
ggml-agx: Wave 60 AgxPingPongPool<2> Async Prefill (GGML_AGX_W60_ASYN…
astaff Nov 30, 2026
776a33a
ggml-agx: Wave 60 prune ~5,600L dead code + split agx-inkling2.cpp in…
astaff Nov 30, 2026
a974399
speculative: Wave 60 per-pos acc_pos[1..7] logging ([DSPARK_POS]) & r…
astaff Nov 30, 2026
e98cc69
ggml-agx: restore agx_inkling2_{set,consume}_dspark_active_drafts in …
astaff Nov 30, 2026
fb1095e
ggml-agx: Wave 60 fix Bug 2 (dead 7th DSpark draft) by isolating va_l…
astaff Nov 30, 2026
d2dc48f
Merge branch 'w60/dspark-bug2' into agx/ggml-agx
astaff Nov 30, 2026
48403d7
Merge branch 'w60/scratch-split' into agx/ggml-agx
astaff Nov 30, 2026
d1fb966
ggml-agx: Wave 61 Lane A 16x256 tile-repacked Q8_0 attn_in Direct-Reg…
astaff Nov 30, 2026
bf63881
common/speculative: Wave 61 calibrated DSpark planner + zero-cost pro…
astaff Nov 30, 2026
ef60146
ggml-agx: Wave 61 Lane B target epilogue fusion (-168 disp/bar at M=4…
astaff Nov 30, 2026
8e309b2
ggml-agx: Wave 61 Lane A Iter 2 32x256 tile repack + 32-row/WG overla…
astaff Nov 30, 2026
120ff1a
common/speculative: W61 Iter 2 drop k=2 tier ({3,5,7}) + raise copy-p…
astaff Nov 30, 2026
7ea0d2f
ggml-agx: Wave 61 Lane B drop FUSE_CTRL_NORM regression (keep -0.38ms…
astaff Nov 30, 2026
c551d4a
Merge commit '7ea0d2f7f' into agx/ggml-agx
astaff Nov 30, 2026
20152b2
Merge commit '120ff1a5b' into agx/ggml-agx
astaff Nov 30, 2026
325fbaf
w62: add llama-agx-logit-parity teacher-forced logit parity gate tool
astaff Nov 30, 2026
abdd296
common/speculative + server: Wave 62 per-request reset + calibrated a…
astaff Nov 30, 2026
7cf3e84
w62: add MoE-aware top2|diff| tie classification and --top2-tol gate
astaff Nov 30, 2026
8deb330
w62: gate parity on t=0 max|diff|, mean|diff|, p99|diff|, and local t…
astaff Nov 30, 2026
4a0d862
common/speculative: W62 Iter 2 surgical bypass gate + positive-delta …
astaff Nov 30, 2026
c36f2a1
ggml-agx: Wave 62 Lane 3 MoE s5/s6 NVFP4 64T 2-SG/WG (-0.68ms/step 1k…
astaff Dec 1, 2026
f06003f
common/speculative: W62 Iter 3 gate k=0 bypass to dead speculation (n…
astaff Dec 1, 2026
318e2a3
Merge commit 'f06003f34' into agx/ggml-agx
astaff Dec 1, 2026
d4e6b0f
Merge commit 'c36f2a1e2' into agx/ggml-agx
astaff Dec 1, 2026
8b5e6fc
w63: add --dspark-replay teacher-forced acceptance tool & LLAMA_DSPAR…
astaff Dec 1, 2026
8f06c91
w63: set dcparams.ctx_other = ctx_tgt in --dspark-replay
astaff Dec 1, 2026
b339f01
w63: cap draft n_ubatch<=512 and n_outputs_max=n_draft+1 in --dspark-…
astaff Dec 1, 2026
df16ac6
w63: match tgt/dft n_batch=n_ubatch<=512 in --dspark-replay + trace e…
astaff Dec 1, 2026
7195ffa
w63: set n_outputs_max_per_seq = dspark_n_draft + 1 for backend sampl…
astaff Dec 1, 2026
244ea9a
llama-batch: split multi-seq speculative rollback batches into 1-seq …
astaff Dec 1, 2026
fbeb1cf
ggml-agx: support build_rs states_extra in Inkling-2 megakernel (Wave…
astaff Dec 1, 2026
9f6312b
ggml-agx: fix false-positive is_large_pp skipping megakernel on M<=8 …
astaff Dec 1, 2026
7eab29b
ggml-agx: skip unselected sampler OUTPUT branches in DSpark discover
astaff Dec 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 123 additions & 9 deletions common/sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ struct common_sampler {
// for rejection sampling; independent of the draft, or the target distribution is not preserved
std::mt19937 rng;

// Fast-path direct argmax when the sampler chain is mathematically identical to pure greedy
// (no grammar, no reasoning budget, no penalties/DRY/XTC/infill, temp <= 0, small logit_bias list e.g. ignore_eos).
bool fast_greedy = false;
bool fast_greedy_has_pos_bias = false;
std::vector<llama_logit_bias> fast_greedy_biases;
uint64_t fast_greedy_hits = 0;

void reset() {
prev.clear();

Expand Down Expand Up @@ -439,16 +446,57 @@ struct common_sampler * common_sampler_init(
params.backend_sampling = false;
}

// Determine whether the sampler chain is mathematically identical to pure greedy argmax
bool is_fast_greedy = false;
bool fg_has_pos_bias = false;
std::vector<llama_logit_bias> fg_biases;
if (params.mirostat == 0 && params.n_probs == 0 && !grmr && !rbudget && !params.samplers.empty()) {
fg_biases = params.logit_bias;
int32_t n_suppress = 0;
const llama_token * suppress = llama_vocab_get_suppress_tokens(vocab, &n_suppress);
for (int32_t i = 0; i < n_suppress; ++i) {
fg_biases.push_back({ suppress[i], -INFINITY });
}
for (const auto & b : fg_biases) {
if (b.bias > 0.0f) {
fg_has_pos_bias = true;
}
}
const bool small_bias = fg_biases.size() <= 64;
const bool no_penalty = (params.penalty_last_n == 0) ||
(params.penalty_repeat == 1.0f && params.penalty_freq == 0.0f && params.penalty_present == 0.0f);
const bool no_dry = (params.dry_multiplier == 0.0f);
const bool no_xtc = (params.xtc_probability == 0.0f);
const bool is_greedy_end =
(params.samplers.back() == COMMON_SAMPLER_TYPE_TEMPERATURE && params.temp <= 0.0f && params.dynatemp_range == 0.0f) ||
(params.samplers.back() == COMMON_SAMPLER_TYPE_TOP_K && params.top_k == 1);
bool has_special = false;
for (const auto & cnstr : params.samplers) {
if (cnstr == COMMON_SAMPLER_TYPE_INFILL || cnstr == COMMON_SAMPLER_TYPE_ADAPTIVE_P) {
has_special = true;
break;
}
}
is_fast_greedy = small_bias && no_penalty && no_dry && no_xtc && is_greedy_end && !has_special;
if (!is_fast_greedy) {
fg_biases.clear();
}
}

auto * result = new common_sampler {
/* .params = */ params,
/* .grmr = */ grmr,
/* .rbudget = */ rbudget,
/* .chain = */ chain,
/* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
/* .cur = */ {},
/* .cur_p = */ {},
/* .params = */ params,
/* .grmr = */ grmr,
/* .rbudget = */ rbudget,
/* .chain = */ chain,
/* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
/* .cur = */ {},
/* .cur_p = */ {},
// mix it, the chain and the draft are seeded from this one too
/* .rng = */ std::mt19937(llama_sampler_get_seed(chain) ^ 0x9e3779b9u),
/* .rng = */ std::mt19937(llama_sampler_get_seed(chain) ^ 0x9e3779b9u),
/* .fast_greedy = */ is_fast_greedy,
/* .fast_greedy_has_pos_bias = */ fg_has_pos_bias,
/* .fast_greedy_biases = */ std::move(fg_biases),
/* .fast_greedy_hits = */ 0,
};

return result;
Expand All @@ -458,6 +506,14 @@ void common_sampler_free(struct common_sampler * gsmpl) {
if (!gsmpl) {
return;
}
if (gsmpl->fast_greedy_hits > 0) {
const char * st = std::getenv("GGML_AGX_STATS");
if (st && st[0] && st[0] != '0') {
std::fprintf(stderr, "[common_sampler] fast_greedy_hits=%llu biases=%zu\n",
(unsigned long long) gsmpl->fast_greedy_hits,
gsmpl->fast_greedy_biases.size());
}
}

llama_sampler_free(gsmpl->grmr);
llama_sampler_free(gsmpl->rbudget);
Expand Down Expand Up @@ -532,7 +588,11 @@ struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
/* .prev = */ gsmpl->prev,
/* .cur = */ gsmpl->cur,
/* .cur_p = */ gsmpl->cur_p,
/* .rng = */ gsmpl->rng,
/* .rng = */ gsmpl->rng,
/* .fast_greedy = */ gsmpl->fast_greedy,
/* .fast_greedy_has_pos_bias = */ gsmpl->fast_greedy_has_pos_bias,
/* .fast_greedy_biases = */ gsmpl->fast_greedy_biases,
/* .fast_greedy_hits = */ 0,
};
}

Expand Down Expand Up @@ -624,6 +684,60 @@ llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_co
auto & chain = gsmpl->chain;
auto & cur_p = gsmpl->cur_p; // initialized by set_logits

// Fast-path pure greedy argmax directly over logits (skips allocating/sorting n_vocab llama_token_data structs)
if (gsmpl->fast_greedy && !grammar_should_apply(gsmpl)) {
id = llama_get_sampled_token_ith(ctx, idx);
if (id != LLAMA_TOKEN_NULL) {
return id;
}
const float * logits = llama_get_logits_ith(ctx, idx);
if (logits) {
const llama_model * model = llama_get_model(ctx);
const llama_vocab * vocab = llama_model_get_vocab(model);
const int n_vocab = llama_vocab_n_tokens(vocab);
if (n_vocab > 0) {
int best_id = 0;
float best_v = logits[0];
for (int i = 1; i < n_vocab; ++i) {
const float v = logits[i];
if (v > best_v) {
best_v = v;
best_id = i;
}
}
// Verify bias list (e.g. ignore_eos={EOG,-INF}): if no positive bias and best_id is unbiased, done!
bool need_bias_rescan = false;
if (!gsmpl->fast_greedy_biases.empty()) {
for (const auto & b : gsmpl->fast_greedy_biases) {
if (b.token == best_id) {
need_bias_rescan = true;
break;
}
}
if (!need_bias_rescan && gsmpl->fast_greedy_has_pos_bias) {
for (const auto & b : gsmpl->fast_greedy_biases) {
if (b.token >= 0 && b.token < n_vocab) {
const float bv = logits[b.token] + b.bias;
if (bv > best_v || (bv == best_v && b.token < best_id)) {
best_v = bv;
best_id = b.token;
}
}
}
}
}
if (!need_bias_rescan) {
gsmpl->fast_greedy_hits++;
gsmpl->cur.resize(1);
gsmpl->cur[0] = llama_token_data { best_id, best_v, 1.0f };
cur_p = { gsmpl->cur.data(), 1, 0, true };
return best_id;
}
// Rare case (0.1%): argmax hit a biased/EOG token -> fall through to standard chain
}
}
}

gsmpl->set_logits(ctx, idx);

// Check if a backend sampler has already sampled a token in which case we
Expand Down
Loading
Loading