Deano Calver commited on
Commit
0d6ced2
·
1 Parent(s): e0c8be6

Expand live context selection and sync backend source

Browse files
data/benchmark_bundle/backend_truth_source.json ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "qwen35_27b_hf": {
3
+ "1024": {
4
+ "output_match": {
5
+ "learned_selector": true,
6
+ "shortlist_base": true
7
+ },
8
+ "profiles": {
9
+ "exact": {
10
+ "latency_ms": 485.96312350127846,
11
+ "m3_fraction": 0.0,
12
+ "resident_bytes": 38670336,
13
+ "selector_us": 0.0,
14
+ "source_records": 7,
15
+ "text": " matters for fast decoding.\n\n<think>\n",
16
+ "token_count": 8
17
+ },
18
+ "learned_selector": {
19
+ "latency_ms": 149.39251463511027,
20
+ "m3_fraction": 0.99462890625,
21
+ "resident_bytes": 99929088,
22
+ "selector_us": 24.829375597335,
23
+ "source_records": 7,
24
+ "text": " matters for fast decoding.\n\n<think>\n",
25
+ "token_count": 8
26
+ },
27
+ "shortlist_base": {
28
+ "latency_ms": 504.25705371890217,
29
+ "m3_fraction": 0.0,
30
+ "resident_bytes": 38670336,
31
+ "selector_us": 0.0,
32
+ "source_records": 7,
33
+ "text": " matters for fast decoding.\n\n<think>\n",
34
+ "token_count": 8
35
+ }
36
+ }
37
+ },
38
+ "2048": {
39
+ "output_match": {
40
+ "learned_selector": true,
41
+ "shortlist_base": true
42
+ },
43
+ "profiles": {
44
+ "exact": {
45
+ "latency_ms": 821.4117659954354,
46
+ "m3_fraction": 0.0,
47
+ "resident_bytes": 76288000,
48
+ "selector_us": 0.0,
49
+ "source_records": 7,
50
+ "text": " fast decoding.\n\n<think>\nThinking Process",
51
+ "token_count": 8
52
+ },
53
+ "learned_selector": {
54
+ "latency_ms": 236.0101520025637,
55
+ "m3_fraction": 0.9954833984375,
56
+ "resident_bytes": 189485056,
57
+ "selector_us": 24.959413796718113,
58
+ "source_records": 7,
59
+ "text": " fast decoding.\n\n<think>\nThinking Process",
60
+ "token_count": 8
61
+ },
62
+ "shortlist_base": {
63
+ "latency_ms": 626.3046781823505,
64
+ "m3_fraction": 0.0,
65
+ "resident_bytes": 75898880,
66
+ "selector_us": 0.0,
67
+ "source_records": 7,
68
+ "text": " fast decoding.\n\n<think>\nThinking Process",
69
+ "token_count": 8
70
+ }
71
+ }
72
+ }
73
+ },
74
+ "qwen35_4b_hf": {
75
+ "1024": {
76
+ "output_match": {
77
+ "learned_selector": true,
78
+ "shortlist_base": true
79
+ },
80
+ "profiles": {
81
+ "exact": {
82
+ "latency_ms": 232.18651674687862,
83
+ "m3_fraction": 0.0,
84
+ "resident_bytes": 19337216,
85
+ "selector_us": 0.0,
86
+ "source_records": 7,
87
+ "text": " matters for fast decoding.\n\nCache locality",
88
+ "token_count": 8
89
+ },
90
+ "learned_selector": {
91
+ "latency_ms": 71.47038698894903,
92
+ "m3_fraction": 0.982421875,
93
+ "resident_bytes": 50163712,
94
+ "selector_us": 25.20372302683427,
95
+ "source_records": 7,
96
+ "text": " matters for fast decoding.\n\nCache locality",
97
+ "token_count": 8
98
+ },
99
+ "shortlist_base": {
100
+ "latency_ms": 242.41085114772432,
101
+ "m3_fraction": 0.0,
102
+ "resident_bytes": 19337216,
103
+ "selector_us": 0.0,
104
+ "source_records": 7,
105
+ "text": " matters for fast decoding.\n\nCache locality",
106
+ "token_count": 8
107
+ }
108
+ }
109
+ },
110
+ "2048": {
111
+ "output_match": {
112
+ "learned_selector": true,
113
+ "shortlist_base": true
114
+ },
115
+ "profiles": {
116
+ "exact": {
117
+ "latency_ms": 400.1560862525366,
118
+ "m3_fraction": 0.0,
119
+ "resident_bytes": 38146048,
120
+ "selector_us": 0.0,
121
+ "source_records": 7,
122
+ "text": " fast decoding.Cache locality matters for fast",
123
+ "token_count": 8
124
+ },
125
+ "learned_selector": {
126
+ "latency_ms": 122.57132897502743,
127
+ "m3_fraction": 0.9710693359375,
128
+ "resident_bytes": 99615744,
129
+ "selector_us": 25.1929690234322,
130
+ "source_records": 7,
131
+ "text": " fast decoding.Cache locality matters for fast",
132
+ "token_count": 8
133
+ },
134
+ "shortlist_base": {
135
+ "latency_ms": 300.46495210262947,
136
+ "m3_fraction": 0.0,
137
+ "resident_bytes": 37134336,
138
+ "selector_us": 0.0,
139
+ "source_records": 7,
140
+ "text": " fast decoding.Cache locality matters for fast",
141
+ "token_count": 8
142
+ }
143
+ }
144
+ }
145
+ },
146
+ "qwen35_9b_hf": {
147
+ "1024": {
148
+ "output_match": {
149
+ "learned_selector": true,
150
+ "shortlist_base": true
151
+ },
152
+ "profiles": {
153
+ "exact": {
154
+ "latency_ms": 242.51539027318358,
155
+ "m3_fraction": 0.0,
156
+ "resident_bytes": 19337216,
157
+ "selector_us": 0.0,
158
+ "source_records": 7,
159
+ "text": " matters for fast decoding.Cache locality matters",
160
+ "token_count": 8
161
+ },
162
+ "learned_selector": {
163
+ "latency_ms": 74.78160472237505,
164
+ "m3_fraction": 0.98779296875,
165
+ "resident_bytes": 50642944,
166
+ "selector_us": 25.876024622562,
167
+ "source_records": 7,
168
+ "text": " matters for fast decoding.Cache locality matters",
169
+ "token_count": 8
170
+ },
171
+ "shortlist_base": {
172
+ "latency_ms": 242.637697578175,
173
+ "m3_fraction": 0.0,
174
+ "resident_bytes": 19337216,
175
+ "selector_us": 0.0,
176
+ "source_records": 7,
177
+ "text": " matters for fast decoding.Cache locality matters",
178
+ "token_count": 8
179
+ }
180
+ }
181
+ },
182
+ "2048": {
183
+ "output_match": {
184
+ "learned_selector": true,
185
+ "shortlist_base": true
186
+ },
187
+ "profiles": {
188
+ "exact": {
189
+ "latency_ms": 404.7011856455356,
190
+ "m3_fraction": 0.0,
191
+ "resident_bytes": 38146048,
192
+ "selector_us": 0.0,
193
+ "source_records": 7,
194
+ "text": " fast decoding.Cache locality matters for fast",
195
+ "token_count": 8
196
+ },
197
+ "learned_selector": {
198
+ "latency_ms": 105.71580115356483,
199
+ "m3_fraction": 0.99853515625,
200
+ "resident_bytes": 101265408,
201
+ "selector_us": 25.52354613629047,
202
+ "source_records": 7,
203
+ "text": " fast decoding.Cache locality matters for fast",
204
+ "token_count": 8
205
+ },
206
+ "shortlist_base": {
207
+ "latency_ms": 307.73281096480787,
208
+ "m3_fraction": 0.0,
209
+ "resident_bytes": 37134336,
210
+ "selector_us": 0.0,
211
+ "source_records": 7,
212
+ "text": " fast decoding.Cache locality matters for fast",
213
+ "token_count": 8
214
+ }
215
+ }
216
+ }
217
+ }
218
+ }
engines/fixture_builder.py CHANGED
@@ -9,11 +9,12 @@ from typing import Any, Mapping
9
  from engines.compare import build_summary_sentence
10
  from engines.presets import MODEL_BY_KEY, PRESET_BY_KEY
11
 
12
- FIXTURE_VERSION = "v6"
13
 
14
  REPO_ROOT = Path(__file__).resolve().parents[1]
15
  BUNDLE_ROOT = REPO_ROOT / "data" / "benchmark_bundle"
16
  COMPACT_BUNDLE_PATH = BUNDLE_ROOT / "space_benchmark_bundle.json"
 
17
 
18
  MODEL_DIR_BY_KEY = {
19
  "qwen35_4b_hf": "qwen35_4b",
@@ -85,6 +86,13 @@ def _space_bundle() -> dict[str, Any]:
85
  return json.loads(COMPACT_BUNDLE_PATH.read_text(encoding="utf-8"))
86
 
87
 
 
 
 
 
 
 
 
88
  def _clean_text(value: str) -> str:
89
  cleaned = str(value or "").replace("\r\n", "\n")
90
  cleaned = re.sub(r"(?is)<think>.*?</think>", " ", cleaned)
@@ -293,7 +301,7 @@ def _longbench_result(request: Mapping[str, Any], *, model_key: str, context_len
293
 
294
 
295
  def _backend_truth_result(request: Mapping[str, Any], *, model_key: str, context_length: int, mode: str) -> dict[str, Any]:
296
- row_bundle = _space_bundle()["backend_truth"].get(model_key, {}).get(str(context_length))
297
  if row_bundle is None:
298
  raise ValueError(f"No backend-truth benchmark row is bundled for model={model_key} at {context_length} tokens.")
299
  candidate_variant = BACKEND_MODE_TO_VARIANT[mode]
@@ -352,7 +360,8 @@ def _backend_truth_result(request: Mapping[str, Any], *, model_key: str, context
352
  "paper_subtitle": f"Exact-length serving benchmark at {context_length} prompt tokens.",
353
  "paper_summary": (
354
  f"Decode latency moves from {baseline_latency:.1f} ms/token to {candidate_latency:.1f} ms/token "
355
- f"with resident KV {baseline_kv_bytes / (1024 ** 2):.1f} MiB versus {candidate_kv_bytes / (1024 ** 2):.1f} MiB."
 
356
  ),
357
  "paper_metric_badge": (
358
  f"M3 fraction {float(candidate_stats.get('m3_fraction') or 0.0):.3f}, "
 
9
  from engines.compare import build_summary_sentence
10
  from engines.presets import MODEL_BY_KEY, PRESET_BY_KEY
11
 
12
+ FIXTURE_VERSION = "v7"
13
 
14
  REPO_ROOT = Path(__file__).resolve().parents[1]
15
  BUNDLE_ROOT = REPO_ROOT / "data" / "benchmark_bundle"
16
  COMPACT_BUNDLE_PATH = BUNDLE_ROOT / "space_benchmark_bundle.json"
17
+ BACKEND_TRUTH_BUNDLE_PATH = BUNDLE_ROOT / "backend_truth_source.json"
18
 
19
  MODEL_DIR_BY_KEY = {
20
  "qwen35_4b_hf": "qwen35_4b",
 
86
  return json.loads(COMPACT_BUNDLE_PATH.read_text(encoding="utf-8"))
87
 
88
 
89
+ @lru_cache(maxsize=None)
90
+ def _backend_truth_bundle() -> dict[str, Any]:
91
+ if BACKEND_TRUTH_BUNDLE_PATH.exists():
92
+ return json.loads(BACKEND_TRUTH_BUNDLE_PATH.read_text(encoding="utf-8"))
93
+ return _space_bundle().get("backend_truth", {})
94
+
95
+
96
  def _clean_text(value: str) -> str:
97
  cleaned = str(value or "").replace("\r\n", "\n")
98
  cleaned = re.sub(r"(?is)<think>.*?</think>", " ", cleaned)
 
301
 
302
 
303
  def _backend_truth_result(request: Mapping[str, Any], *, model_key: str, context_length: int, mode: str) -> dict[str, Any]:
304
+ row_bundle = _backend_truth_bundle().get(model_key, {}).get(str(context_length))
305
  if row_bundle is None:
306
  raise ValueError(f"No backend-truth benchmark row is bundled for model={model_key} at {context_length} tokens.")
307
  candidate_variant = BACKEND_MODE_TO_VARIANT[mode]
 
360
  "paper_subtitle": f"Exact-length serving benchmark at {context_length} prompt tokens.",
361
  "paper_summary": (
362
  f"Decode latency moves from {baseline_latency:.1f} ms/token to {candidate_latency:.1f} ms/token "
363
+ f"with resident KV {baseline_kv_bytes / (1024 ** 2):.1f} MiB versus {candidate_kv_bytes / (1024 ** 2):.1f} MiB. "
364
+ f"The recorded {exact_token_count}-token decode sample is identical across exact, shortlist, and learned rows on this benchmark."
365
  ),
366
  "paper_metric_badge": (
367
  f"M3 fraction {float(candidate_stats.get('m3_fraction') or 0.0):.3f}, "
space_app.py CHANGED
@@ -1131,6 +1131,12 @@ def _live_context_guard_copy(mode: str) -> str:
1131
  "<strong>⚡ Live mode (ZeroGPU)</strong><span>Your prompt runs on the real Space system using the selected preset's benchmark configuration. When the runtime allows it, custom live prompts and example buttons can use the 4K context lane to sanity-check that behavior stays in the same ballpark as the cached paper row.</span>"
1132
  "</div>"
1133
  )
 
 
 
 
 
 
1134
  if mode == "benchmark":
1135
  return (
1136
  "<div class='live-context-note active'>"
@@ -1170,23 +1176,19 @@ def _preferred_live_example_context(current_context_length: int) -> int:
1170
 
1171
  def _guard_live_context(prompt_mode: str, custom_prompt: str, current_context_length: int) -> tuple[Any, str]:
1172
  if _is_live_prompt_mode(prompt_mode):
 
 
 
 
1173
  has_custom_prompt = bool(custom_prompt.strip())
1174
  if has_custom_prompt:
1175
- available_choices = _available_live_context_choices()
1176
- guarded_value = int(current_context_length)
1177
- if guarded_value not in available_choices:
1178
- guarded_value = available_choices[min(len(available_choices) - 1, 1)]
1179
  return (
1180
  gr.update(choices=available_choices, value=guarded_value),
1181
  _live_context_guard_copy("custom"),
1182
  )
1183
- benchmark_choices = _available_live_benchmark_context_choices()
1184
- guarded_value = int(current_context_length)
1185
- if guarded_value not in benchmark_choices:
1186
- guarded_value = int(benchmark_choices[-1])
1187
  return (
1188
- gr.update(choices=benchmark_choices, value=guarded_value),
1189
- _live_context_guard_copy("benchmark"),
1190
  )
1191
  has_custom_prompt = bool(custom_prompt.strip())
1192
  restored_value = int(current_context_length)
 
1131
  "<strong>⚡ Live mode (ZeroGPU)</strong><span>Your prompt runs on the real Space system using the selected preset's benchmark configuration. When the runtime allows it, custom live prompts and example buttons can use the 4K context lane to sanity-check that behavior stays in the same ballpark as the cached paper row.</span>"
1132
  "</div>"
1133
  )
1134
+ if mode == "live":
1135
+ return (
1136
+ "<div class='live-context-note active'>"
1137
+ "<strong>⚡ Live mode (ZeroGPU)</strong><span>Live mode exposes the full runtime context ladder up to the current Space cap. Empty-prompt replay is still only bundled for the benchmark rows, so use a custom prompt or example button when you want to run the 4K live lane.</span>"
1138
+ "</div>"
1139
+ )
1140
  if mode == "benchmark":
1141
  return (
1142
  "<div class='live-context-note active'>"
 
1176
 
1177
  def _guard_live_context(prompt_mode: str, custom_prompt: str, current_context_length: int) -> tuple[Any, str]:
1178
  if _is_live_prompt_mode(prompt_mode):
1179
+ available_choices = _available_live_context_choices()
1180
+ guarded_value = int(current_context_length)
1181
+ if guarded_value not in available_choices:
1182
+ guarded_value = available_choices[min(len(available_choices) - 1, 1)]
1183
  has_custom_prompt = bool(custom_prompt.strip())
1184
  if has_custom_prompt:
 
 
 
 
1185
  return (
1186
  gr.update(choices=available_choices, value=guarded_value),
1187
  _live_context_guard_copy("custom"),
1188
  )
 
 
 
 
1189
  return (
1190
+ gr.update(choices=available_choices, value=guarded_value),
1191
+ _live_context_guard_copy("live"),
1192
  )
1193
  has_custom_prompt = bool(custom_prompt.strip())
1194
  restored_value = int(current_context_length)