forked from ggml-org/llama.cpp
-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathserver-adaptive-dm.h
More file actions
1682 lines (1523 loc) · 63.7 KB
/
Copy pathserver-adaptive-dm.h
File metadata and controls
1682 lines (1523 loc) · 63.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "common.h"
#include "log.h"
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
static constexpr int SERVER_ADAPTIVE_DM_PROFIT_POSITIONS = 128;
static constexpr int SERVER_ADAPTIVE_DM_PROFIT_DEPTHS = SERVER_ADAPTIVE_DM_PROFIT_POSITIONS + 1;
static constexpr int SERVER_ADAPTIVE_DM_PROFIT_CANDIDATES = SERVER_ADAPTIVE_DM_PROFIT_DEPTHS + 1;
static inline int server_adaptive_dm_probe_n_max(int base_n_max, float probe_fraction) {
if (base_n_max <= 0) {
return 0;
}
const int probe_n_max = std::max(2, (int) (base_n_max * probe_fraction));
return std::min(probe_n_max, base_n_max);
}
static inline float server_adaptive_dm_required_fringe_for_n_max(
int target_n_max,
int base_n_max,
float fringe_min,
float fringe_max) {
if (base_n_max <= 2 || target_n_max <= 2 || fringe_max <= fringe_min) {
return fringe_min;
}
const int bounded_n_max = std::clamp(target_n_max, 2, base_n_max);
const float t = (float)(bounded_n_max - 2) / (float)(base_n_max - 2);
return std::clamp(fringe_min + (fringe_max - fringe_min) * t, fringe_min, fringe_max);
}
static inline bool server_adaptive_dm_should_preserve_for_continuation(float prompt_similarity, float keep_fraction) {
return prompt_similarity > 0.90f && keep_fraction > 0.90f;
}
static inline bool server_adaptive_dm_uses_fringe_controller(common_speculative_dm_controller controller) {
return controller == COMMON_SPECULATIVE_DM_CONTROLLER_FRINGE;
}
static inline bool server_adaptive_dm_uses_profit_controller(common_speculative_dm_controller controller) {
return controller == COMMON_SPECULATIVE_DM_CONTROLLER_PROFIT;
}
static inline const char * server_adaptive_dm_controller_name(common_speculative_dm_controller controller) {
switch (controller) {
case COMMON_SPECULATIVE_DM_CONTROLLER_FRINGE: return "fringe";
case COMMON_SPECULATIVE_DM_CONTROLLER_PROFIT: return "profit";
}
return "unknown";
}
// Initialize EWMA with first sample, then run normal update.
// Call this instead of checking dst == 0.0f (which is a valid EWMA value).
static inline void server_adaptive_dm_ewma_init_or_update(float & dst, float sample, float alpha, int32_t samples_seen) {
if (!std::isfinite(sample)) {
return;
}
alpha = std::clamp(alpha, 0.01f, 1.0f);
if (samples_seen == 0) {
dst = sample;
} else {
dst += alpha * (sample - dst);
}
}
static inline int server_adaptive_dm_build_candidates(int base_n_max, int * out, int out_cap) {
if (out_cap <= 0) {
return 0;
}
int n = 0;
auto add_unique = [&](int candidate) {
if (candidate < 0 || candidate > base_n_max || n >= out_cap) {
return;
}
bool exists = false;
for (int i = 0; i < n; ++i) {
if (out[i] == candidate) {
exists = true;
break;
}
}
if (!exists) {
out[n++] = candidate;
}
};
add_unique(0);
const int tracked_n_max = std::min(base_n_max, SERVER_ADAPTIVE_DM_PROFIT_POSITIONS);
for (int candidate = 1; candidate <= tracked_n_max; ++candidate) {
add_unique(candidate);
}
std::sort(out, out + n);
return n;
}
static inline int server_adaptive_dm_resolve_cohort_n_max(
const int * normal_n_max,
const int * cohort_cap_n_max,
int n_slots) {
if (!normal_n_max || !cohort_cap_n_max || n_slots < 2) {
return 0;
}
int n_eligible = 0;
int n_positive = 0;
int cohort_n_max = INT_MAX;
for (int i = 0; i < n_slots; ++i) {
const int slot_cap = cohort_cap_n_max[i];
if (slot_cap <= 0) {
continue;
}
n_eligible++;
if (normal_n_max[i] > 0) {
n_positive++;
}
const int slot_limit = normal_n_max[i] > 0
? std::min(normal_n_max[i], slot_cap)
: slot_cap;
cohort_n_max = std::min(cohort_n_max, slot_limit);
}
if (n_eligible < 2 || n_positive == 0 || cohort_n_max == INT_MAX) {
return 0;
}
return std::max(0, cohort_n_max);
}
static inline int server_adaptive_dm_next_explore_depth(int current_n, int base_n_max, float probe_fraction) {
if (base_n_max <= 0) {
return 0;
}
if (current_n <= 0) {
return server_adaptive_dm_probe_n_max(base_n_max, probe_fraction);
}
int candidates[SERVER_ADAPTIVE_DM_PROFIT_CANDIDATES];
const int n_candidates = server_adaptive_dm_build_candidates(
base_n_max, candidates, SERVER_ADAPTIVE_DM_PROFIT_CANDIDATES);
for (int i = 0; i < n_candidates; ++i) {
if (candidates[i] > current_n) {
return candidates[i];
}
}
return std::min(current_n, base_n_max);
}
static inline float server_adaptive_dm_survival_expected_accept(
const float * pos_accept_ewma,
const int32_t * pos_samples,
int n_positions,
int depth,
int min_samples,
bool * ready) {
bool is_ready = true;
float expected = 0.0f;
if (depth <= 0) {
if (ready) {
*ready = true;
}
return 0.0f;
}
// pos_accept_ewma[pos] records cumulative survival P(accepted at least pos+1).
// Expected accepted tokens = sum of survival probabilities over positions.
for (int pos = 0; pos < depth; ++pos) {
if (pos >= n_positions || pos_samples[pos] < min_samples) {
is_ready = false;
break;
}
if (!std::isfinite(pos_accept_ewma[pos])) {
is_ready = false;
break;
}
expected += std::clamp(pos_accept_ewma[pos], 0.0f, 1.0f);
}
if (ready) {
*ready = is_ready;
}
return is_ready ? expected : 0.0f;
}
static inline float server_adaptive_dm_score(float expected_output_tokens, float cycle_ms) {
if (cycle_ms <= 0.0f || expected_output_tokens <= 0.0f
|| !std::isfinite(cycle_ms) || !std::isfinite(expected_output_tokens)) {
return 0.0f;
}
return expected_output_tokens * 1000.0f / cycle_ms;
}
struct server_adaptive_dm_profit_candidate {
int n = 0;
float score = 0.0f;
bool ready = false;
bool estimated = false;
};
static inline server_adaptive_dm_profit_candidate server_adaptive_dm_best_profit_candidate(
const int * candidates,
int n_candidates,
const bool * ready,
const bool * estimated,
const float * expected_accept,
const float * cycle_ms) {
server_adaptive_dm_profit_candidate best;
for (int i = 0; i < n_candidates; ++i) {
if (!ready[i]) {
continue;
}
const float score = server_adaptive_dm_score(1.0f + expected_accept[i], cycle_ms[i]);
if (!best.ready || score > best.score) {
best.ready = true;
best.n = candidates[i];
best.score = score;
best.estimated = estimated[i];
}
}
return best;
}
static inline int server_adaptive_dm_apply_profit_hysteresis(
int current_n,
int best_n,
float current_score,
float best_score,
float raise_margin,
float lower_margin,
const int * candidates,
int n_candidates) {
if (best_n > current_n) {
if (current_score > 0.0f && best_score < current_score * (1.0f + raise_margin)) {
return current_n;
}
const int max_raise_delta = current_n >= 8 ? 4 : 2;
const int raise_limit = std::min(best_n, current_n + max_raise_delta);
int next = current_n;
for (int i = 0; i < n_candidates; ++i) {
if (candidates[i] > current_n && candidates[i] <= raise_limit) {
next = candidates[i];
}
}
return next;
}
if (best_n < current_n) {
if (current_score > 0.0f && best_score < current_score * (1.0f + lower_margin)) {
return current_n;
}
// Gradual downshift: move down a couple of candidate steps, not all the way to best_n.
// Dense candidates should not make demotion materially slower than the old coarse ladder.
int next = current_n;
for (int step = 0; step < 2 && next > best_n; ++step) {
int found = -1;
for (int i = n_candidates - 1; i >= 0; --i) {
if (candidates[i] < next) {
found = candidates[i];
break;
}
}
if (found < 0) break;
next = found;
}
return next;
}
return current_n;
}
struct server_adaptive_dm_state {
static constexpr int FRINGE_WINDOW = 32;
static constexpr int FRINGE_REACH_POSITIONS = SERVER_ADAPTIVE_DM_PROFIT_POSITIONS;
static constexpr int PROFIT_POSITIONS = SERVER_ADAPTIVE_DM_PROFIT_POSITIONS;
static constexpr int PROFIT_DEPTHS = SERVER_ADAPTIVE_DM_PROFIT_DEPTHS;
static constexpr int PROFIT_CANDIDATES = SERVER_ADAPTIVE_DM_PROFIT_CANDIDATES;
struct fringe_entry {
int16_t n_accepted;
int16_t n_draft;
uint32_t epoch;
};
fringe_entry fringe_ring[FRINGE_WINDOW] = {};
int fringe_ring_idx = 0;
int fringe_ring_count = 0;
float rolling_fringe = 0.0f;
int32_t adaptive_n_max = -1;
int32_t adaptive_probe_counter = 0;
int32_t off_dwell = 0;
int32_t explore_counter = 0;
uint32_t fringe_epoch = 0;
int32_t fringe_epoch_reached[FRINGE_REACH_POSITIONS] = {};
int32_t fringe_epoch_accepted[FRINGE_REACH_POSITIONS] = {};
bool dm_adaptive = true;
float dm_fringe_min = 0.30f;
float dm_fringe_max = 0.50f;
int32_t dm_off_dwell = 8;
int32_t dm_explore_interval = 12;
int32_t dm_min_reach = 3;
int32_t dm_probe_interval = 16;
float dm_probe_fraction = 0.25f;
int32_t dm_fringe_window = 3;
common_speculative_dm_controller dm_controller = COMMON_SPECULATIVE_DM_CONTROLLER_PROFIT;
float dm_profit_min = 0.05f;
float dm_profit_raise_margin = 0.05f;
float dm_profit_lower_margin = 0.05f;
float dm_profit_ewma_alpha = 0.15f;
int32_t dm_profit_min_samples = 3;
int32_t dm_profit_warmup = 0;
int32_t dm_profit_baseline_interval = 1024;
struct profit_depth_stats {
int32_t samples = 0;
float draft_ms = 0.0f;
float verify_ms = 0.0f;
float accept_ms = 0.0f;
float cycle_ms = 0.0f;
float output_tokens = 0.0f;
float actual_draft_tokens = 0.0f;
float best_score = 0.0f;
double total_output_tokens = 0.0;
double total_cycle_ms = 0.0;
};
struct profit_config_key {
int32_t base_n_max = -1;
int32_t branch_budget = -1;
int32_t draft_topk = -1;
int32_t dflash_cross_ctx = -1;
float draft_temp = -1.0f;
float p_min = -1.0f;
int32_t target_top_k = -1;
int32_t target_has_grammar = -1;
int32_t target_grammar_lazy = -1;
float target_temp = -1.0f;
float target_top_p = -1.0f;
float target_min_p = -1.0f;
};
float profit_pos_accept_ewma[PROFIT_POSITIONS] = {};
int32_t profit_pos_samples[PROFIT_POSITIONS] = {};
profit_depth_stats profit_depth[PROFIT_DEPTHS] = {};
profit_depth_stats profit_baseline;
profit_config_key profit_key;
bool profit_has_key = false;
bool profit_pending = false;
int32_t profit_pending_requested_n_max = 0;
int32_t profit_pending_n_draft = 0;
int32_t profit_pending_n_accepted = 0;
bool profit_pending_tree = false;
uint32_t profit_epoch = 0;
float profit_current_score = 0.0f;
int32_t profit_last_recommended_n = -1;
int32_t profit_consecutive_below_profit = 0;
int32_t profit_cycles_since_baseline = 0;
bool profit_baseline_probe_pending = false;
int32_t profit_baseline_probe_resume_n = -1;
int32_t profit_off_probe_failures = 0;
int32_t profit_off_probe_next_n = -1;
int32_t profit_active_episode_n = -1;
int32_t profit_active_episode_start_samples = 0;
double profit_active_episode_start_output_tokens = 0.0;
double profit_active_episode_start_cycle_ms = 0.0;
bool profit_request_measure_depths = false;
bool profit_request_requires_fresh_switch_sample = false;
int32_t profit_request_baseline_start_samples = 0;
double profit_request_baseline_start_output_tokens = 0.0;
double profit_request_baseline_start_cycle_ms = 0.0;
int32_t profit_request_depth_start_samples[PROFIT_DEPTHS] = {};
double profit_request_depth_start_output_tokens[PROFIT_DEPTHS] = {};
double profit_request_depth_start_cycle_ms[PROFIT_DEPTHS] = {};
struct fringe_decision {
float fringe = 0.0f;
int reached = 0;
int accepted = 0;
};
void reset_fringe_epoch_reached() {
std::fill_n(fringe_epoch_reached, FRINGE_REACH_POSITIONS, 0);
std::fill_n(fringe_epoch_accepted, FRINGE_REACH_POSITIONS, 0);
}
void profit_mark_request_measurement_boundary(bool require_depth_measurements) {
profit_request_measure_depths = require_depth_measurements;
profit_request_requires_fresh_switch_sample = false;
profit_request_baseline_start_samples = profit_baseline.samples;
profit_request_baseline_start_output_tokens = profit_baseline.total_output_tokens;
profit_request_baseline_start_cycle_ms = profit_baseline.total_cycle_ms;
for (int d = 0; d < PROFIT_DEPTHS; ++d) {
profit_request_depth_start_samples[d] = profit_depth[d].samples;
profit_request_depth_start_output_tokens[d] = profit_depth[d].total_output_tokens;
profit_request_depth_start_cycle_ms[d] = profit_depth[d].total_cycle_ms;
}
}
void reset_request_state(bool preserve_active_recommendation = false) {
const int32_t previous_recommended_n = profit_last_recommended_n;
const bool previous_recommendation_profitable =
!preserve_active_recommendation &&
previous_recommended_n > 0 &&
profit_baseline_ready() &&
profit_score_for_depth(previous_recommended_n) >=
profit_score_for_depth(0) * (1.0f + dm_profit_min);
const int32_t resume_n_max =
(preserve_active_recommendation || previous_recommendation_profitable) &&
previous_recommended_n > 0
? previous_recommended_n
: -1;
const bool can_resume =
resume_n_max > 0 &&
profit_has_key &&
(profit_key.base_n_max <= 0 || resume_n_max <= profit_key.base_n_max);
std::fill_n(fringe_ring, FRINGE_WINDOW, fringe_entry{});
fringe_ring_idx = 0;
fringe_ring_count = 0;
rolling_fringe = 0.0f;
adaptive_n_max = -1;
adaptive_probe_counter = 0;
off_dwell = 0;
explore_counter = 0;
fringe_epoch++;
reset_fringe_epoch_reached();
reset_request_profit_state(false);
profit_mark_request_measurement_boundary(!can_resume);
if (can_resume) {
profit_request_requires_fresh_switch_sample = true;
adaptive_n_max = resume_n_max;
profit_last_recommended_n = resume_n_max;
profit_current_score = profit_score_for_depth(resume_n_max);
profit_begin_active_episode(resume_n_max);
}
}
void reset_request_profit_state(bool preserve_recommendation = false) {
profit_pending = false;
profit_pending_requested_n_max = 0;
profit_pending_n_draft = 0;
profit_pending_n_accepted = 0;
profit_pending_tree = false;
profit_consecutive_below_profit = 0;
profit_current_score = 0.0f;
if (!preserve_recommendation) {
profit_last_recommended_n = -1;
}
profit_cycles_since_baseline = 0;
profit_baseline_probe_pending = false;
profit_baseline_probe_resume_n = -1;
profit_off_probe_failures = 0;
profit_off_probe_next_n = -1;
profit_active_episode_n = -1;
profit_active_episode_start_samples = 0;
profit_active_episode_start_output_tokens = 0.0;
profit_active_episode_start_cycle_ms = 0.0;
}
void reset_profit_state() {
std::fill_n(profit_pos_accept_ewma, PROFIT_POSITIONS, 0.0f);
std::fill_n(profit_pos_samples, PROFIT_POSITIONS, 0);
std::fill_n(profit_depth, PROFIT_DEPTHS, profit_depth_stats{});
profit_baseline = profit_depth_stats{};
profit_key = profit_config_key{};
profit_has_key = false;
profit_epoch++;
reset_request_profit_state();
profit_mark_request_measurement_boundary(false);
adaptive_n_max = -1;
adaptive_probe_counter = 0;
explore_counter = 0;
off_dwell = 0;
}
void reset_profit_if_config_changed(
const common_params_speculative & spec,
int base_n_max,
int32_t n_past,
const common_params_sampling * sampling = nullptr) {
(void) n_past;
const profit_config_key next {
base_n_max,
spec.branch_budget,
spec.draft_topk,
spec.dflash_cross_ctx,
spec.sample_temp,
spec.p_min,
sampling ? sampling->top_k : -1,
sampling ? (common_grammar_value(sampling->grammar).empty() ? 0 : 1) : -1,
sampling ? (sampling->grammar_lazy ? 1 : 0) : -1,
sampling ? sampling->temp : -1.0f,
sampling ? sampling->top_p : -1.0f,
sampling ? sampling->min_p : -1.0f,
};
const bool changed =
!profit_has_key ||
profit_key.base_n_max != next.base_n_max ||
profit_key.branch_budget != next.branch_budget ||
profit_key.draft_topk != next.draft_topk ||
profit_key.dflash_cross_ctx != next.dflash_cross_ctx ||
profit_key.draft_temp != next.draft_temp ||
profit_key.p_min != next.p_min ||
profit_key.target_top_k != next.target_top_k ||
profit_key.target_has_grammar != next.target_has_grammar ||
profit_key.target_grammar_lazy != next.target_grammar_lazy ||
profit_key.target_temp != next.target_temp ||
profit_key.target_top_p != next.target_top_p ||
profit_key.target_min_p != next.target_min_p;
if (!changed) {
return;
}
reset_profit_state();
profit_key = next;
profit_has_key = true;
}
bool profit_baseline_ready() const {
return profit_baseline.samples >= dm_profit_min_samples && profit_baseline.cycle_ms > 0.0f;
}
bool profit_depth_ready(int depth) const {
return depth > 0 &&
depth < PROFIT_DEPTHS &&
profit_depth[depth].samples >= dm_profit_min_samples &&
profit_depth[depth].cycle_ms > 0.0f &&
profit_depth[depth].output_tokens > 0.0f;
}
int profit_depth_samples_since_request(int depth) const {
if (depth <= 0 || depth >= PROFIT_DEPTHS) {
return 0;
}
return std::max<int>(0, profit_depth[depth].samples - profit_request_depth_start_samples[depth]);
}
bool profit_depth_ready_since_request(int depth, int min_samples) const {
return depth > 0 &&
depth < PROFIT_DEPTHS &&
profit_depth[depth].samples >= profit_request_depth_start_samples[depth] + min_samples &&
profit_depth[depth].total_cycle_ms > profit_request_depth_start_cycle_ms[depth] &&
profit_depth[depth].total_output_tokens > profit_request_depth_start_output_tokens[depth];
}
float profit_score_for_depth_since_request(int depth, int min_samples) const {
if (!profit_depth_ready_since_request(depth, min_samples)) {
return 0.0f;
}
const double output_tokens =
profit_depth[depth].total_output_tokens - profit_request_depth_start_output_tokens[depth];
const double cycle_ms =
profit_depth[depth].total_cycle_ms - profit_request_depth_start_cycle_ms[depth];
if (cycle_ms <= 0.0 || output_tokens <= 0.0 ||
!std::isfinite(cycle_ms) || !std::isfinite(output_tokens)) {
return 0.0f;
}
return server_adaptive_dm_score((float) output_tokens, (float) cycle_ms);
}
bool profit_average_for_depth_since_request(
int depth,
int min_samples,
float * output_tokens,
float * cycle_ms) const {
if (!profit_depth_ready_since_request(depth, min_samples)) {
return false;
}
const int samples =
profit_depth[depth].samples - profit_request_depth_start_samples[depth];
const double total_output_tokens =
profit_depth[depth].total_output_tokens - profit_request_depth_start_output_tokens[depth];
const double total_cycle_ms =
profit_depth[depth].total_cycle_ms - profit_request_depth_start_cycle_ms[depth];
if (samples <= 0 ||
total_cycle_ms <= 0.0 ||
total_output_tokens <= 0.0 ||
!std::isfinite(total_cycle_ms) ||
!std::isfinite(total_output_tokens)) {
return false;
}
if (output_tokens) {
*output_tokens = (float) (total_output_tokens / (double) samples);
}
if (cycle_ms) {
*cycle_ms = (float) (total_cycle_ms / (double) samples);
}
return true;
}
bool profit_depth_ready_for_decision(int depth) const {
if (!profit_depth_ready(depth)) {
return false;
}
if (!profit_request_measure_depths) {
return true;
}
return profit_depth_ready_since_request(depth, dm_profit_min_samples);
}
bool profit_positive_switch_has_fresh_evidence(int depth, int current_n, int base_n_max) const {
if (!profit_request_requires_fresh_switch_sample ||
depth <= 0 ||
depth == current_n) {
return true;
}
return profit_depth_ready_since_request(depth, profit_candidate_min_samples(base_n_max));
}
int profit_initial_probe_min_samples() const {
return dm_profit_warmup > 0 ?
std::max<int>(dm_profit_min_samples, dm_profit_warmup) :
dm_profit_min_samples;
}
bool profit_initial_probe_depth_ready(int depth) const {
if (!profit_request_measure_depths) {
return depth > 0 &&
depth < PROFIT_DEPTHS &&
profit_depth[depth].samples >= profit_initial_probe_min_samples() &&
profit_depth[depth].cycle_ms > 0.0f &&
profit_depth[depth].output_tokens > 0.0f;
}
return profit_depth_ready_since_request(depth, profit_initial_probe_min_samples());
}
bool profit_has_ready_positive_depth() const {
for (int d = 1; d < PROFIT_DEPTHS; ++d) {
if (profit_depth_ready(d)) {
return true;
}
}
return false;
}
float profit_score_for_stats(const profit_depth_stats & stats) const {
if (stats.samples < dm_profit_min_samples ||
stats.total_cycle_ms <= 0.0 ||
stats.total_output_tokens <= 0.0 ||
!std::isfinite(stats.total_cycle_ms) ||
!std::isfinite(stats.total_output_tokens)) {
return 0.0f;
}
return server_adaptive_dm_score(
(float) stats.total_output_tokens,
(float) stats.total_cycle_ms);
}
float profit_score_for_depth(int depth) const {
if (depth == 0) {
if (!profit_baseline_ready()) {
return 0.0f;
}
return profit_score_for_stats(profit_baseline);
}
if (!profit_depth_ready_for_decision(depth)) {
return 0.0f;
}
if (profit_request_measure_depths) {
const float request_score = profit_score_for_depth_since_request(depth, dm_profit_min_samples);
if (request_score > 0.0f) {
return request_score;
}
return 0.0f;
}
return profit_score_for_stats(profit_depth[depth]);
}
int profit_initial_probe_depths(int base_n_max, int * out, int out_cap) const {
if (out_cap <= 0 || base_n_max <= 0) {
return 0;
}
int n = 0;
auto add_unique = [&](int value) {
if (value <= 0 || value > base_n_max || n >= out_cap) {
return;
}
for (int i = 0; i < n; ++i) {
if (out[i] == value) {
return;
}
}
out[n++] = value;
};
if (base_n_max >= 12) {
add_unique(std::max(4, server_adaptive_dm_probe_n_max(base_n_max, dm_probe_fraction)));
add_unique(std::min(8, base_n_max));
add_unique(base_n_max);
} else {
const int shallow = server_adaptive_dm_probe_n_max(base_n_max, dm_probe_fraction);
const int mid = std::max(1, base_n_max / 2);
add_unique(shallow);
add_unique(mid);
add_unique(base_n_max);
}
return n;
}
int profit_next_unready_probe_depth(int base_n_max) const {
int probes[8];
const int n_probes = profit_initial_probe_depths(base_n_max, probes, 8);
for (int i = 0; i < n_probes; ++i) {
if (!profit_initial_probe_depth_ready(probes[i])) {
return probes[i];
}
}
return -1;
}
int profit_next_lower_rescue_probe_depth(int current_n, int base_n_max) const {
if (current_n <= 1 || base_n_max < 12) {
return -1;
}
auto usable_unready = [&](int depth) {
return depth > 0 &&
depth < current_n &&
depth <= base_n_max &&
!profit_ladder_probe_depth_ready(depth);
};
const int rescue_ladder[] = {12, 10, 8, 4};
for (const int depth : rescue_ladder) {
if (usable_unready(depth)) {
return depth;
}
}
return -1;
}
int profit_first_initial_probe_depth(int base_n_max) const {
int probes[8];
const int n_probes = profit_initial_probe_depths(base_n_max, probes, 8);
return n_probes > 0 ? probes[0] : 0;
}
int profit_next_initial_probe_depth_after(int current_n, int base_n_max, bool * wrapped = nullptr) const {
int probes[8];
const int n_probes = profit_initial_probe_depths(base_n_max, probes, 8);
if (n_probes <= 0) {
if (wrapped) {
*wrapped = false;
}
return 0;
}
for (int i = 0; i < n_probes; ++i) {
if (probes[i] > current_n) {
if (wrapped) {
*wrapped = false;
}
return probes[i];
}
}
if (wrapped) {
*wrapped = true;
}
return probes[0];
}
int profit_off_probe_depths(int base_n_max, int * out, int out_cap) const {
if (out_cap <= 0 || base_n_max <= 0) {
return 0;
}
int n = 0;
auto add_unique = [&](int value) {
if (value <= 0 || value > base_n_max || n >= out_cap) {
return;
}
for (int i = 0; i < n; ++i) {
if (out[i] == value) {
return;
}
}
out[n++] = value;
};
if (base_n_max >= 12) {
const int raw_probe = server_adaptive_dm_probe_n_max(base_n_max, dm_probe_fraction);
const int shallow = std::max(4, raw_probe);
const int mid_low = std::min(8, base_n_max);
const int mid_high = std::min(12, base_n_max);
const int anchors[] = {shallow, mid_low, mid_high, base_n_max};
add_unique(shallow);
add_unique(mid_low);
add_unique(mid_high);
add_unique(base_n_max);
add_unique(raw_probe - 1);
add_unique(raw_probe);
add_unique(1);
for (int radius = 1; radius <= base_n_max && n < out_cap; ++radius) {
for (const int anchor : anchors) {
add_unique(anchor - radius);
add_unique(anchor + radius);
}
}
} else {
const int shallow = server_adaptive_dm_probe_n_max(base_n_max, dm_probe_fraction);
const int mid = std::max(1, base_n_max / 2);
const int anchors[] = {shallow, mid, base_n_max};
add_unique(shallow);
add_unique(mid);
add_unique(base_n_max);
add_unique(1);
for (int radius = 1; radius <= base_n_max && n < out_cap; ++radius) {
for (const int anchor : anchors) {
add_unique(anchor - radius);
add_unique(anchor + radius);
}
}
}
return n;
}
int profit_first_off_probe_depth(int base_n_max) const {
int probes[PROFIT_CANDIDATES];
const int n_probes = profit_off_probe_depths(base_n_max, probes, PROFIT_CANDIDATES);
return n_probes > 0 ? probes[0] : 0;
}
int profit_next_off_probe_depth_after(int current_n, int base_n_max, bool * wrapped = nullptr) const {
int probes[PROFIT_CANDIDATES];
const int n_probes = profit_off_probe_depths(base_n_max, probes, PROFIT_CANDIDATES);
if (n_probes <= 0) {
if (wrapped) {
*wrapped = false;
}
return 0;
}
for (int i = 0; i < n_probes; ++i) {
if (probes[i] == current_n) {
if (wrapped) {
*wrapped = i + 1 >= n_probes;
}
return probes[(i + 1) % n_probes];
}
}
if (wrapped) {
*wrapped = true;
}
return probes[0];
}
int profit_next_off_probe_depth(int base_n_max, int fallback_probe_n_max) const {
if (base_n_max <= 0) {
return 0;
}
if (profit_off_probe_next_n > 0) {
return std::clamp<int>(profit_off_probe_next_n, 1, base_n_max);
}
const int first = profit_first_off_probe_depth(base_n_max);
if (first > 0) {
return first;
}
return std::clamp<int>(fallback_probe_n_max, 0, base_n_max);
}
void profit_prepare_off_probe_sweep(int base_n_max) {
const int first = profit_first_off_probe_depth(base_n_max);
profit_off_probe_next_n = first > 0 ? first : -1;
}
void profit_note_off_probe_result(int probed_n, int base_n_max, bool profitable) {
if (profitable) {
profit_off_probe_failures = 0;
profit_prepare_off_probe_sweep(base_n_max);
return;
}
bool wrapped = false;
const int next = profit_next_off_probe_depth_after(probed_n, base_n_max, &wrapped);
profit_off_probe_next_n = next > 0 ? next : -1;
if (wrapped) {
profit_off_probe_failures++;
}
}
int profit_active_explore_budget(int base_n_max) const {
if (base_n_max <= 1) {
return 0;
}
const int budget = base_n_max <= 8 ? 6 : 7;
return std::min(base_n_max - 1, budget);
}
bool profit_survival_expected_accept(int depth, float * expected_accept) const {
bool ready = false;
const float expected = server_adaptive_dm_survival_expected_accept(
profit_pos_accept_ewma,
profit_pos_samples,
PROFIT_POSITIONS,
depth,
dm_profit_min_samples,
&ready);
if (ready && expected_accept) {
*expected_accept = expected;
}
return ready;
}
bool profit_active_explore_prefers_higher(int current_n, int base_n_max) const {
if (current_n <= 0 || current_n >= base_n_max) {
return false;
}
float expected_accept = 0.0f;
if (!profit_survival_expected_accept(current_n, &expected_accept)) {
return false;
}
return expected_accept >= (float) current_n * dm_fringe_max;
}
bool profit_should_skip_active_explore(int current_n, int base_n_max) const {
if (current_n <= 0 || current_n < base_n_max) {
return false;
}
if (!profit_baseline_ready() ||
!profit_active_episode_ready(current_n, base_n_max)) {
return false;
}
const float baseline_score = profit_score_for_depth(0);
const float active_score = profit_active_episode_score(current_n);
if (baseline_score <= 0.0f ||
active_score < baseline_score * (1.0f + dm_profit_min)) {
return false;
}
float expected_accept = 0.0f;
if (!profit_survival_expected_accept(current_n, &expected_accept)) {
return false;
}
return expected_accept >= (float) current_n * dm_fringe_max;
}
int profit_active_explore_depths(int current_n, int base_n_max, int * out, int out_cap) const {
if (out_cap <= 0 || base_n_max <= 0) {
return 0;
}
const int current = std::clamp<int>(current_n, 0, base_n_max);
const int budget = std::min(out_cap, profit_active_explore_budget(base_n_max));
if (budget <= 0) {
return 0;
}
int n = 0;
auto add_unique = [&](int value) {
if (value <= 0 || value > base_n_max || value == current || n >= budget) {
return;
}
for (int i = 0; i < n; ++i) {
if (out[i] == value) {
return;
}
}
out[n++] = value;
};
if (current <= 0) {
add_unique(server_adaptive_dm_probe_n_max(base_n_max, dm_probe_fraction));
add_unique(std::max(1, base_n_max / 2));
add_unique(base_n_max);
return n;
}
const bool prefer_higher = profit_active_explore_prefers_higher(current, base_n_max);
const int local_span = std::min(base_n_max, std::max(3, std::min(5, base_n_max / 3 + 1)));
for (int delta = 1; delta <= local_span && n < budget; ++delta) {
const int lower = current - delta;
const int higher = current + delta;
if (prefer_higher) {
add_unique(higher);
add_unique(lower);
} else {
add_unique(lower);
add_unique(higher);
}
}