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
//! PERF-041: the admission predicate of `contracts/batch-admission-v1.yaml`,
//! stated once, in one place, in a module that is **not** feature-gated.
//!
//! Two reasons it lives here rather than inside a scheduler:
//!
//! 1. `api::cuda_batch_scheduler` and `api::iteration_scheduler` are both
//! declared `#[cfg(feature = "cuda")]` in `api/mod.rs`. Anything defined
//! inside them is a dark target: `cargo test -p aprender-serve --lib` (what
//! `workspace-test` runs) never compiles it. The comment above
//! `pub mod apr_q4k_scheduler` in that same file records this exact lesson
//! already — "Gating the whole module put the only cancellation-free decode
//! loop in the crate outside every CI test job."
//! 2. `batch-admission-v1.yaml` F-BATCH-002 names the filter
//! `cargo test -p aprender-serve --features cuda --lib batch_admission`, and
//! its own `test_harness_status` records that the filter matched **zero**
//! tests — a filter that selects nothing prints `test result: ok. 0 passed`
//! and is indistinguishable from a pass. This module is what that filter
//! selects. Being ungated, it is also selected without `--features cuda`.
//!
//! The soundness obligation this discharges, quoted from the contract:
//!
//! ```text
//! fast_path(batch) <=> (len(batch) == 1 and channel_empty)
//! ```
/// Is the single-request fast path admissible for this batch?
///
/// The fast path (`generate_single_request` → `generate_gpu_resident_streaming`)
/// replays a captured CUDA decode graph. The batched path (`batched_decode_step`
/// → `forward_batched_to_token_ids`) launches every kernel eagerly, because
/// `BATCHED_GRAPH` is opt-in and its own documentation says graph replay is
/// slower than eager there owing to capture overhead. Taking the batched path
/// when nothing else is pending therefore costs a lone client per-token latency
/// it does not need to pay, which is what F-BATCH-004 forbids.
///
/// The MAGNITUDE of that penalty is UNMEASURED on a correct batched path. Every
/// figure this comment used to carry was taken while batched decode emitted
/// garbage tokens to the `max_tokens` cap (aprender#2753: the online-softmax
/// rescale, the Q4_K weight-type hardcode and the missing QKV bias) and with a
/// per-layer synchronize and device-to-host copy in the decode loop
/// (aprender#2764). A ratio between a healthy path and a broken one, measured
/// through a debug path, is not a throughput comparison.
///
/// `force_batched` is the F-BATCH-004 mutation knob ("force the batched path
/// unconditionally; the c=1 case must turn RED"), exposed via
/// [`force_batched_path`] so the penalty can be *measured* rather than inferred.
/// It is false in every build that does not set the environment variable, so
/// production behaviour is unchanged.
/// Environment name of the F-BATCH-004 mutation knob.
pub const FORCE_BATCHED_ENV: &str = "APR_FORCE_BATCHED_PATH";
/// Read the mutation knob once per process.
///
/// PERF-041 measures `serialization_index(c) = wall(c) / wall(1)` where the
/// denominator is taken on the fast path and every numerator on the batched
/// path. That makes the index a product of two independent factors:
///
/// ```text
/// serialization_index(c) = path_penalty * scaling_index(c)
/// path_penalty = wall_batched(1) / wall_fast(1)
/// scaling_index = wall(c) / wall_batched(1)
/// ```
///
/// Only `scaling_index` is serialization. `wall_batched(1)` is the one term of
/// that decomposition no recorded run contains, and this knob is how it is
/// obtained: set `APR_FORCE_BATCHED_PATH=1` and re-run the c=1 band.
/// The arming rule, separated from the environment so it can be tested.
///
/// Exactly the string `"1"` arms the knob. Anything else — unset, empty, `"0"`,
/// `"true"` — leaves production behaviour alone. Deliberately strict: a knob
/// that a stray `APR_FORCE_BATCHED_PATH=0` could arm would silently make every
/// c=1 measurement on this box a measurement of the penalty path.