quantuminfer 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +96 -2
  3. package/bin/quantuminfer.js +61 -0
  4. package/c27/adapters.py +161 -0
  5. package/c27/harness27.py +349 -0
  6. package/data/input.txt +40000 -0
  7. package/data/wikitext/test.txt +7249 -0
  8. package/engine/API.txt +208 -0
  9. package/engine/PLUGINS.txt +4 -0
  10. package/engine/__init__.py +82 -0
  11. package/engine/adapter.py +505 -0
  12. package/engine/blocks.py +1180 -0
  13. package/engine/c12.py +324 -0
  14. package/engine/c12cu.py +1074 -0
  15. package/engine/ctx.py +87 -0
  16. package/engine/dev.py +11 -0
  17. package/engine/ds4.py +389 -0
  18. package/engine/kernels.py +833 -0
  19. package/engine/model.py +281 -0
  20. package/engine/record.py +140 -0
  21. package/engine/server.py +183 -0
  22. package/engine/spec.py +1030 -0
  23. package/engine/wcode.py +122 -0
  24. package/p27/SPEC.txt +66 -0
  25. package/p27/STATUS.txt +91 -0
  26. package/p27/bench_glm.py +354 -0
  27. package/p27/bench_multi.py +76 -0
  28. package/p27/mkfinal.py +98 -0
  29. package/p27/modal_comp.py +173 -0
  30. package/p27/modal_dl.py +63 -0
  31. package/p27/modal_p27.py +168 -0
  32. package/p27/modal_probe.py +49 -0
  33. package/p27/p27rules.py +29 -0
  34. package/p27/pblocks.py +1242 -0
  35. package/p27/pmodel.py +402 -0
  36. package/p27/pref.py +151 -0
  37. package/p27/prof_glm.py +34 -0
  38. package/p27/quant.py +850 -0
  39. package/p27/recipes/vllm_b300_single_node_tep_b300_agentx_ep.md +353 -0
  40. package/p27/recipes/vllm_b300_single_node_tp_b300_agentx.md +351 -0
  41. package/p27/recipes/vllm_b300_single_node_tp_baseline.md +250 -0
  42. package/p27/recipes/vllm_default_gb300.md +281 -0
  43. package/p27/recipes/vllm_glm53_h200.md +301 -0
  44. package/p27/run_window1.sh +21 -0
  45. package/p27/run_window2.sh +13 -0
  46. package/p27/sweep_glm.py +64 -0
  47. package/p27/t_align.py +42 -0
  48. package/p27/t_combine.py +28 -0
  49. package/p27/t_echo.py +31 -0
  50. package/p27/t_fp8dense.py +59 -0
  51. package/p27/t_fp8speed.py +48 -0
  52. package/p27/t_glm.py +340 -0
  53. package/p27/t_glu.py +30 -0
  54. package/p27/t_gsuf.py +23 -0
  55. package/p27/t_jac.py +22 -0
  56. package/p27/t_moebm.py +30 -0
  57. package/p27/t_nccl.py +36 -0
  58. package/p27/t_online_jac.py +31 -0
  59. package/p27/t_probe.py +4 -0
  60. package/p27/t_router.py +28 -0
  61. package/p27/t_symm.py +50 -0
  62. package/p27/t_tiny.py +116 -0
  63. package/package.json +10 -4
  64. package/results/ANALYSIS.md +156 -0
  65. package/results/comp_vllm_mtp_k5_b300mtp.json +235 -0
  66. package/results/glm53_b300x8_opt4.json +624 -0
  67. package/results/res_glm_c32w3_b300.json +638 -0
  68. package/results/res_glm_final_c32_b300.json +652 -0
  69. package/results/res_glm_multi_final_b300.json +1821 -0
  70. package/results/res_glm_online_b300.json +638 -0
  71. package/results/res_glm_online_fixed_b300.json +638 -0
  72. package/results/res_glm_opt4_b300.json +624 -0
  73. package/results/res_glm_text_plain_batch_b300.json +5792 -0
  74. package/results/res_glm_text_w1_b300.json +1635 -0
  75. package/results/res_glm_text_w3_b300.json +1639 -0
  76. package/results/res_sweep_echo_b300.json +129 -0
  77. package/results/res_sweep_final_b300.json +153 -0
  78. package/results/res_sweep_glm_b300.json +153 -0
  79. package/results/res_techo.json +29 -0
  80. package/results/res_tgsuf.json +21 -0
  81. package/results/res_tjac.json +17 -0
  82. package/results/res_tonline_jac.json +13 -0
@@ -0,0 +1,349 @@
1
+ """Lane C27 benchmark loop (gpu/BENCH27_SPEC.md), derived from exp_c1_engines/harness.py.
2
+
3
+ Runs inside the engine container. Adapter API (adapters.py): gen(prompts, n, T, seed=None) -> list[list[int]],
4
+ reset_cache(), spec_counters(), tpp_from(c0, c1, n), preemptions().
5
+
6
+ Timing (stated in every row):
7
+ single : decode tok/s = (n-1) / (t(256 new) - t(1 new)); prompt cache reset before each call (cold, so the n-gram
8
+ drafters cannot remember earlier outputs); first of 3 reps discarded as warmup at that length.
9
+ single_t1: same, T=1 exact (top_p 1, top_k off), 1 rep per prompt after a warmup (chat + 8 held-out prompts).
10
+ batched : cache reset; decode tok/s = (sum(n)-B) / (t(128 new) - t(1 new)) (prefill-only run subtracted). T=1.
11
+ shared : cache reset; total tok/s = N*64 / wall (prefill included), prefix caching ON.
12
+ longctx : N distinct 8192-token wikitext windows, 128 greedy tokens; decode tok/s = N*127/(t(128)-t(1)).
13
+ ttft : fresh never-seen prompt (distinct window), cache reset, wall time of gen(ids, 1); median of reps.
14
+ Every generated stream (token ids) is kept in STREAMS and saved to an .npz by the caller.
15
+ """
16
+ import json
17
+ import os
18
+ import statistics
19
+ import subprocess
20
+ import threading
21
+ import time
22
+
23
+ SHAKESPEARE = "/root/c27/shakespeare.txt"
24
+ WIKITEXT = "/root/c27/wikitext.txt"
25
+ CHAT_Q = "Explain in three sentences why the sky is blue."
26
+ # 8 held-out chat prompts for single-user T=1 (fixed here before any measurement; see SPEC.txt)
27
+ HELDOUT = [
28
+ "Write a short poem about a lighthouse keeper who has never seen the sea in daylight.",
29
+ "What are the main differences between a process and a thread in an operating system?",
30
+ "Give me a three-day itinerary for a first visit to Kyoto.",
31
+ "Summarize the causes of the French Revolution in one paragraph.",
32
+ "How does public-key cryptography let two strangers agree on a secret?",
33
+ "Write a Python function that returns the n-th prime number, with a brief explanation.",
34
+ "Describe the water cycle to a ten-year-old.",
35
+ "What should I consider when choosing between renting and buying a home?",
36
+ ]
37
+ STREAMS = {}
38
+
39
+
40
+ def chat_ids(tok, q):
41
+ msgs = [{"role": "user", "content": q}]
42
+ try: # Qwen3.6: thinking disabled the documented way (chat template kwarg enable_thinking=False)
43
+ text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True, enable_thinking=False)
44
+ except TypeError:
45
+ text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
46
+ return tok(text, add_special_tokens=False).input_ids
47
+
48
+
49
+ _TXT = {}
50
+
51
+
52
+ def text_ids(tok, which):
53
+ if which not in _TXT:
54
+ _TXT[which] = tok(open(SHAKESPEARE if which == "shakespeare" else WIKITEXT).read()[:1200000],
55
+ add_special_tokens=False).input_ids
56
+ return _TXT[which]
57
+
58
+
59
+ def single_cases(tok, names):
60
+ out = []
61
+ for nm in names:
62
+ if nm == "code":
63
+ out.append((nm, tok("def fibonacci(n):", add_special_tokens=False).input_ids))
64
+ elif nm == "chat":
65
+ out.append((nm, chat_ids(tok, CHAT_Q)))
66
+ else:
67
+ w, d = nm.split("@")
68
+ out.append((nm, text_ids(tok, w)[:int(d)]))
69
+ return out
70
+
71
+
72
+ def rand_prompts(B, L, seed, lo=1000, hi=100000):
73
+ import torch
74
+ g = torch.Generator().manual_seed(seed)
75
+ return torch.randint(lo, hi, (B, L), generator=g).tolist()
76
+
77
+
78
+ class MemPoll:
79
+ """Samples nvidia-smi memory.used (MiB) of GPU 0 every 0.5 s: the peak covers every process of the engine."""
80
+
81
+ def __init__(self):
82
+ self.peak, self.stop = 0, False
83
+ self.t = threading.Thread(target=self.run, daemon=True)
84
+ self.t.start()
85
+
86
+ def run(self):
87
+ while not self.stop:
88
+ try:
89
+ v = int(subprocess.run("nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits -i 0",
90
+ shell=True, capture_output=True, text=True).stdout.strip().split()[0])
91
+ self.peak = max(self.peak, v)
92
+ except Exception: # noqa
93
+ pass
94
+ time.sleep(0.5)
95
+
96
+
97
+ def _t(fn):
98
+ t0 = time.perf_counter()
99
+ r = fn()
100
+ return time.perf_counter() - t0, r
101
+
102
+
103
+ def _save(R, path):
104
+ if path:
105
+ tmp = path + ".tmp"
106
+ json.dump(R, open(tmp, "w"), indent=1)
107
+ os.replace(tmp, path)
108
+
109
+
110
+ def run_all(A, tok, R, plan, out_path=None, deadline=None, mem=None):
111
+ """plan: dict section -> options. Sections: single, single_t1, batched, shared, longctx, ttft."""
112
+ for k in ("single", "batched", "shared", "longctx", "ttft", "errors"):
113
+ R.setdefault(k, [])
114
+
115
+ def late():
116
+ return deadline is not None and time.time() > deadline
117
+
118
+ def stamp(row):
119
+ row["mem_peak_mib_so_far"] = mem.peak if mem else None
120
+ return row
121
+
122
+ A.gen([tok("Hello world").input_ids], 64, 0.0)
123
+ A.gen(rand_prompts(8, 32, 7), 32, 1.0)
124
+ reps = plan.get("reps", 2)
125
+
126
+ if "single" in plan:
127
+ for name, ids in single_cases(tok, plan["single"]):
128
+ if late():
129
+ R["errors"].append(f"deadline: skipped single {name}")
130
+ continue
131
+ try:
132
+ A.reset_cache()
133
+ A.gen([ids], 16, 0.0)
134
+ runs, tpp, outs0 = [], [], None
135
+ for r in range(reps + 1):
136
+ A.reset_cache()
137
+ t1, _ = _t(lambda: A.gen([ids], 1, 0.0))
138
+ A.reset_cache()
139
+ c0 = A.spec_counters()
140
+ tN, outs = _t(lambda: A.gen([ids], 256, 0.0))
141
+ c1 = A.spec_counters()
142
+ STREAMS[f"single/{name}/greedy/r{r}"] = outs[0]
143
+ if r == 0:
144
+ outs0 = outs[0]
145
+ continue
146
+ n_new = len(outs[0])
147
+ runs.append(dict(t1=t1, t256=tN, n_new=n_new, tok_s=(n_new - 1) / max(tN - t1, 1e-9),
148
+ same_as_r0=outs[0] == outs0))
149
+ if c0 is not None and c1 is not None:
150
+ tpp.append(A.tpp_from(c0, c1, n_new))
151
+ ts = [x["tok_s"] for x in runs]
152
+ tpp = [x for x in tpp if x]
153
+ row = dict(case=name.split("@")[0], name=name, ctx=len(ids), sampling="greedy", tok_s=sum(ts) / len(ts),
154
+ tok_s_min=min(ts), tok_s_max=max(ts),
155
+ tokens_per_pass=(sum(tpp) / len(tpp)) if tpp else (1.0 if not A.spec else None),
156
+ mode=A.mode, runs=runs, first_ids=outs0[:64],
157
+ method="cache reset before each call; (n-1)/(t(256 new)-t(1 new)); greedy T=0; "
158
+ "first of %d reps discarded" % (reps + 1))
159
+ R["single"].append(stamp(row))
160
+ print("single", name, round(row["tok_s"], 1), row["tokens_per_pass"], flush=True)
161
+ except Exception as e:
162
+ import traceback
163
+ R["errors"].append(f"single {name}: {e!r} {traceback.format_exc()[-1500:]}")
164
+ print("ERR single", name, e, flush=True)
165
+ _save(R, out_path)
166
+
167
+ if "single_t1" in plan:
168
+ names = plan["single_t1"] # e.g. ["chat", "heldout"]
169
+ prompts = []
170
+ if "chat" in names:
171
+ prompts.append(("chat", chat_ids(tok, CHAT_Q)))
172
+ if "heldout" in names:
173
+ prompts += [(f"heldout{i}", chat_ids(tok, q)) for i, q in enumerate(HELDOUT)]
174
+ for name, ids in prompts:
175
+ if late():
176
+ R["errors"].append(f"deadline: skipped single_t1 {name}")
177
+ continue
178
+ try:
179
+ runs, tpp = [], []
180
+ nrep = reps + 1 if name == "chat" else 2
181
+ for r in range(nrep):
182
+ A.reset_cache()
183
+ t1, _ = _t(lambda: A.gen([ids], 1, 1.0, seed=5000 + r))
184
+ A.reset_cache()
185
+ c0 = A.spec_counters()
186
+ tN, outs = _t(lambda: A.gen([ids], 256, 1.0, seed=5000 + r))
187
+ c1 = A.spec_counters()
188
+ STREAMS[f"single_t1/{name}/r{r}"] = outs[0]
189
+ if r == 0:
190
+ continue
191
+ n_new = len(outs[0])
192
+ runs.append(dict(seed=5000 + r, t1=t1, t256=tN, n_new=n_new, tok_s=(n_new - 1) / max(tN - t1, 1e-9)))
193
+ if c0 is not None and c1 is not None:
194
+ tpp.append(A.tpp_from(c0, c1, n_new))
195
+ ts = [x["tok_s"] for x in runs]
196
+ tpp = [x for x in tpp if x]
197
+ row = dict(case=name, name=name, ctx=len(ids), sampling="T=1 exact", tok_s=sum(ts) / len(ts),
198
+ tok_s_min=min(ts), tok_s_max=max(ts),
199
+ tokens_per_pass=(sum(tpp) / len(tpp)) if tpp else (1.0 if not A.spec else None),
200
+ mode=A.mode, runs=runs,
201
+ method="T=1 top_p 1 top_k off; cache reset; (n-1)/(t(256)-t(1)); first rep discarded")
202
+ R["single"].append(stamp(row))
203
+ print("single_t1", name, round(row["tok_s"], 1), row["tokens_per_pass"], flush=True)
204
+ except Exception as e:
205
+ import traceback
206
+ R["errors"].append(f"single_t1 {name}: {e!r} {traceback.format_exc()[-1500:]}")
207
+ print("ERR single_t1", name, e, flush=True)
208
+ _save(R, out_path)
209
+
210
+ if "batched" in plan:
211
+ for B in plan["batched"]:
212
+ if late():
213
+ R["errors"].append(f"deadline: skipped batched B={B}")
214
+ continue
215
+ try:
216
+ runs = []
217
+ A.reset_cache()
218
+ A.gen(rand_prompts(B, 32, 99), 128, 1.0) # warm this shape (seed 99 not reported)
219
+ p0 = A.preemptions()
220
+ for r in range(reps):
221
+ seed = 1000 + r
222
+ P = rand_prompts(B, 32, seed)
223
+ A.reset_cache()
224
+ t1, _ = _t(lambda: A.gen(P, 1, 1.0, seed=seed * 10000))
225
+ A.reset_cache()
226
+ c0 = A.spec_counters()
227
+ tN, outs = _t(lambda: A.gen(P, 128, 1.0, seed=seed * 10000))
228
+ c1 = A.spec_counters()
229
+ STREAMS[f"batched/B{B}/s{seed}"] = outs
230
+ n_tot = sum(len(o) for o in outs)
231
+ runs.append(dict(seed=seed, t1=t1, t128=tN, n_tokens=n_tot, tok_s=(n_tot - B) / max(tN - t1, 1e-9),
232
+ max_running=getattr(A, "max_running", None),
233
+ tok_s_incl_prefill=n_tot / tN,
234
+ tokens_per_pass=A.tpp_from(c0, c1, n_tot) if (c0 and c1) else None))
235
+ p1 = A.preemptions()
236
+ ts = [x["tok_s"] for x in runs]
237
+ row = dict(B=B, tok_s=sum(ts) / len(ts), tok_s_min=min(ts), tok_s_max=max(ts), sampling="T=1 exact",
238
+ n_prompts=B, mode=A.mode, seeds=[x["seed"] for x in runs],
239
+ preemptions=(p1 - p0) if (p0 is not None and p1 is not None) else None,
240
+ method="all B requests in one offline generate call; cache reset; "
241
+ "(sum n - B)/(t(128 new)-t(1 new)) (prefill-only run subtracted); "
242
+ "random 32-token prompts torch.randint(1000,100000) seed 1000+r", runs=runs)
243
+ R["batched"].append(stamp(row))
244
+ print("batched", B, round(row["tok_s"], 1), flush=True)
245
+ except Exception as e:
246
+ import traceback
247
+ R["errors"].append(f"batched B={B}: {e!r} {traceback.format_exc()[-1500:]}")
248
+ print("ERR batched", B, e, flush=True)
249
+ if "out of memory" in repr(e).lower():
250
+ break
251
+ _save(R, out_path)
252
+
253
+ if "shared" in plan:
254
+ P1 = rand_prompts(1, 1000, 4242)[0]
255
+ for N in plan["shared"]:
256
+ if late():
257
+ R["errors"].append(f"deadline: skipped shared N={N}")
258
+ continue
259
+ try:
260
+ A.reset_cache()
261
+ A.gen([P1] * N, 64, 1.0)
262
+ runs = []
263
+ for r in range(reps):
264
+ A.reset_cache()
265
+ Pr = rand_prompts(1, 1000, 4243 + r)[0]
266
+ tN, outs = _t(lambda: A.gen([Pr] * N, 64, 1.0, seed=(4243 + r) * 10000))
267
+ STREAMS[f"shared/N{N}/s{4243 + r}"] = outs
268
+ n_tot = sum(len(o) for o in outs)
269
+ runs.append(dict(wall=tN, n_tokens=n_tot, tok_s=n_tot / tN))
270
+ ts = [x["tok_s"] for x in runs]
271
+ row = dict(N=N, total_tok_s=sum(ts) / len(ts), tok_s_min=min(ts), tok_s_max=max(ts), mode=A.mode,
272
+ prompt_seeds=[4243 + r for r in range(reps)],
273
+ method="N copies of one never-seen random 1000-token prompt in one offline call, T=1, "
274
+ "prefix cache reset before each run, prefix caching enabled; N*64/wall", runs=runs)
275
+ R["shared"].append(stamp(row))
276
+ print("shared", N, round(row["total_tok_s"], 1), flush=True)
277
+ except Exception as e:
278
+ import traceback
279
+ R["errors"].append(f"shared N={N}: {e!r} {traceback.format_exc()[-1500:]}")
280
+ print("ERR shared", N, e, flush=True)
281
+ _save(R, out_path)
282
+
283
+ if "longctx" in plan:
284
+ wt = text_ids(tok, "wikitext")
285
+ L = 8192
286
+ for N in plan["longctx"]:
287
+ if late():
288
+ R["errors"].append(f"deadline: skipped longctx N={N}")
289
+ continue
290
+ try:
291
+ # distinct windows: user i starts at token 4096*i (+offset per rep), wrapping within the corpus
292
+ def wins(off):
293
+ return [[wt[(off + 4096 * i + j) % len(wt)] for j in range(L)] for i in range(N)]
294
+ runs = []
295
+ p0 = A.preemptions()
296
+ for r in range(reps):
297
+ P = wins(1000 * r)
298
+ A.reset_cache()
299
+ t1, _ = _t(lambda: A.gen(P, 1, 0.0))
300
+ A.reset_cache()
301
+ tN, outs = _t(lambda: A.gen(P, 128, 0.0))
302
+ STREAMS[f"longctx/N{N}/r{r}"] = outs
303
+ n_tot = sum(len(o) for o in outs)
304
+ runs.append(dict(t1=t1, t128=tN, n_tokens=n_tot, tok_s=(n_tot - N) / max(tN - t1, 1e-9),
305
+ max_running=getattr(A, "max_running", None)))
306
+ p1 = A.preemptions()
307
+ ts = [x["tok_s"] for x in runs]
308
+ row = dict(N=N, ctx=L, tok_s=sum(ts) / len(ts), tok_s_min=min(ts), tok_s_max=max(ts), mode=A.mode,
309
+ sampling="greedy", preemptions=(p1 - p0) if (p0 is not None and p1 is not None) else None,
310
+ capacity=A.config.get("_resolved"),
311
+ method="N distinct 8192-token wikitext windows (start 4096*i + 1000*rep) in one offline "
312
+ "call, max_num_seqs/max_running = N; (sum n - N)/(t(128)-t(1)); greedy", runs=runs)
313
+ R["longctx"].append(stamp(row))
314
+ print("longctx", N, round(row["tok_s"], 1), row["preemptions"], flush=True)
315
+ except Exception as e:
316
+ import traceback
317
+ R["errors"].append(f"longctx N={N}: {e!r} {traceback.format_exc()[-1500:]}")
318
+ print("ERR longctx", N, e, flush=True)
319
+ _save(R, out_path)
320
+
321
+ if "ttft" in plan:
322
+ wt = text_ids(tok, "wikitext")
323
+ sh = text_ids(tok, "shakespeare")
324
+ for L in plan["ttft"]:
325
+ try:
326
+ for _ in range(2): # warm this length on a tuning window
327
+ A.reset_cache()
328
+ A.gen([sh[:L]], 1, 0.0)
329
+ ts, firsts = [], []
330
+ nrep = plan.get("ttft_reps", 7)
331
+ for r in range(nrep):
332
+ ids = [wt[(7919 * (r + 1) + j) % len(wt)] for j in range(L)] # fresh window per rep
333
+ A.reset_cache()
334
+ t, o = _t(lambda: A.gen([ids], 1, 0.0))
335
+ ts.append(t)
336
+ firsts.append(o[0][0])
337
+ row = dict(ctx=L, ttft_s=statistics.median(ts), ttft_min=min(ts), ttft_max=max(ts), runs=ts,
338
+ first_tokens=firsts, mode=A.mode,
339
+ method="fresh never-seen wikitext window per rep (offset 7919*(r+1)), cache reset, "
340
+ "wall time of one offline gen(ids, max_tokens=1) call; median of %d" % nrep)
341
+ R["ttft"].append(stamp(row))
342
+ print("ttft", L, [round(x, 3) for x in ts], flush=True)
343
+ except Exception as e:
344
+ import traceback
345
+ R["errors"].append(f"ttft {L}: {e!r} {traceback.format_exc()[-1500:]}")
346
+ print("ERR ttft", L, e, flush=True)
347
+ _save(R, out_path)
348
+ _save(R, out_path)
349
+ return R