TiGa-RCE commited on
Commit
7973967
·
verified ·
1 Parent(s): 22338c1

Add mounted 1.5B and 8B BF16 cross-runtime controls

Browse files
Files changed (3) hide show
  1. README.md +4 -0
  2. __pycache__/app.cpython-313.pyc +0 -0
  3. app.py +109 -2
README.md CHANGED
@@ -20,3 +20,7 @@ BF16 vectors. CUDA results do not reproduce MLX/Metal performance.
20
  The second milestone adds CUDA-native bitsandbytes INT8 and NF4 controls.
21
  They are deliberately reported as separate quantizers and are not treated as
22
  equivalents of MLX Q, oQ, or oQe formats.
 
 
 
 
 
20
  The second milestone adds CUDA-native bitsandbytes INT8 and NF4 controls.
21
  They are deliberately reported as separate quantizers and are not treated as
22
  equivalents of MLX Q, oQ, or oQe formats.
23
+
24
+ Matched BF16 controls are also available for GTE-Qwen2-1.5B and
25
+ Qwen3-Embedding-8B. Their CUDA vectors are compared against the corresponding
26
+ saved MLX BF16 vectors from the private reproduction bucket.
__pycache__/app.cpython-313.pyc CHANGED
Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ
 
app.py CHANGED
@@ -38,8 +38,11 @@ def detailed_instruction(query: str) -> str:
38
  return f"Instruct: {TASK}\nQuery:{query}"
39
 
40
 
41
- def encode_with(active_model, text: str) -> np.ndarray:
42
- batch = tokenizer(text, return_tensors="pt", truncation=True, max_length=32768)
 
 
 
43
  batch = {key: value.to("cuda") for key, value in batch.items()}
44
  with torch.inference_mode():
45
  output = active_model(**batch)
@@ -246,6 +249,95 @@ def run_quantized_control(variant: str) -> dict:
246
  return result
247
 
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  with gr.Blocks() as demo:
250
  gr.Markdown(
251
  "# Embedding Quantization CUDA Control\n"
@@ -276,6 +368,21 @@ with gr.Blocks() as demo:
276
  concurrency_limit=1,
277
  api_name="run_quantized_control",
278
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
 
281
  if __name__ == "__main__":
 
38
  return f"Instruct: {TASK}\nQuery:{query}"
39
 
40
 
41
+ def encode_with(active_model, text: str, active_tokenizer=None) -> np.ndarray:
42
+ active_tokenizer = active_tokenizer or tokenizer
43
+ batch = active_tokenizer(
44
+ text, return_tensors="pt", truncation=True, max_length=32768
45
+ )
46
  batch = {key: value.to("cuda") for key, value in batch.items()}
47
  with torch.inference_mode():
48
  output = active_model(**batch)
 
249
  return result
250
 
251
 
252
+ BF16_FAMILIES = {
253
+ "gte-qwen2-1.5b": {
254
+ "path": Path("/models/gte-qwen2-1.5b"),
255
+ "source": "Alibaba-NLP/gte-Qwen2-1.5B-instruct",
256
+ "revision": "a9af15a6372d7d6b25e9fb07c2ccb9e1fe645644",
257
+ },
258
+ "qwen3-embedding-8b": {
259
+ "path": Path("/models/qwen3-embedding-8b"),
260
+ "source": "Qwen/Qwen3-Embedding-8B",
261
+ "revision": "1d8ad4ca9b3dd8059ad90a75d4983776a23d44af",
262
+ },
263
+ }
264
+
265
+
266
+ @spaces.GPU(duration=300)
267
+ def run_family_bf16_control(family: str) -> dict:
268
+ if family not in BF16_FAMILIES:
269
+ raise ValueError(f"unsupported family: {family}")
270
+ spec = BF16_FAMILIES[family]
271
+ pairs = json.loads(INPUT_PATH.read_text())
272
+ query_texts = [detailed_instruction(item["query"]) for item in pairs]
273
+ document_texts = [item["document"] for item in pairs]
274
+
275
+ gc.collect()
276
+ torch.cuda.empty_cache()
277
+ torch.cuda.reset_peak_memory_stats()
278
+ torch.cuda.synchronize()
279
+ allocation_before = int(torch.cuda.memory_allocated())
280
+ load_started = time.perf_counter()
281
+ family_tokenizer = AutoTokenizer.from_pretrained(spec["path"], padding_side="left")
282
+ family_model = AutoModel.from_pretrained(
283
+ spec["path"], dtype=torch.bfloat16, trust_remote_code=True
284
+ ).to("cuda").eval()
285
+ torch.cuda.synchronize()
286
+ load_seconds = time.perf_counter() - load_started
287
+ allocation_after_load = int(torch.cuda.memory_allocated())
288
+
289
+ encode_started = time.perf_counter()
290
+ queries = np.stack([
291
+ encode_with(family_model, text, family_tokenizer) for text in query_texts
292
+ ])
293
+ documents = np.stack([
294
+ encode_with(family_model, text, family_tokenizer) for text in document_texts
295
+ ])
296
+ torch.cuda.synchronize()
297
+ encode_seconds = time.perf_counter() - encode_started
298
+ metrics, scores, ranks = retrieval_metrics(queries, documents)
299
+ metrics.update({
300
+ "lane": "cuda-zerogpu-bf16",
301
+ "family": family,
302
+ "model": spec["source"],
303
+ "source_revision": spec["revision"],
304
+ "load_seconds": load_seconds,
305
+ "encode_seconds": encode_seconds,
306
+ "texts_per_second": len(query_texts + document_texts) / encode_seconds,
307
+ "torch_version": torch.__version__,
308
+ "cuda_device": torch.cuda.get_device_name(0),
309
+ "cuda_allocation_before_load": allocation_before,
310
+ "cuda_allocation_after_load": allocation_after_load,
311
+ "cuda_incremental_model_allocation": allocation_after_load - allocation_before,
312
+ "cuda_peak_bytes": int(torch.cuda.max_memory_allocated()),
313
+ })
314
+ local_reference = (
315
+ DATA_ROOT / "local-results/full-q4-q6-q8" / family / "quality/bf16.npz"
316
+ )
317
+ result = {
318
+ "metrics": metrics,
319
+ "mlx_bf16_comparison": compare_vectors(
320
+ queries, documents, scores, ranks, local_reference, "cuda_vs_mlx_bf16"
321
+ ),
322
+ }
323
+ output_dir = DATA_ROOT / "cloud-results" / family
324
+ output_dir.mkdir(parents=True, exist_ok=True)
325
+ np.savez_compressed(
326
+ output_dir / "cuda-bf16.npz",
327
+ queries=queries,
328
+ documents=documents,
329
+ scores=scores,
330
+ ranks=ranks,
331
+ metrics=np.array(json.dumps(metrics)),
332
+ )
333
+ (output_dir / "cuda-bf16.json").write_text(json.dumps(result, indent=2) + "\n")
334
+
335
+ del family_model, family_tokenizer
336
+ gc.collect()
337
+ torch.cuda.empty_cache()
338
+ return result
339
+
340
+
341
  with gr.Blocks() as demo:
342
  gr.Markdown(
343
  "# Embedding Quantization CUDA Control\n"
 
368
  concurrency_limit=1,
369
  api_name="run_quantized_control",
370
  )
371
+ gr.Markdown("## Larger-family BF16 cross-runtime controls")
372
+ family = gr.Dropdown(
373
+ choices=list(BF16_FAMILIES),
374
+ value="gte-qwen2-1.5b",
375
+ label="Model family",
376
+ )
377
+ family_button = gr.Button("Run bounded family BF16 control")
378
+ family_output = gr.JSON(label="Family result")
379
+ family_button.click(
380
+ fn=run_family_bf16_control,
381
+ inputs=family,
382
+ outputs=family_output,
383
+ concurrency_limit=1,
384
+ api_name="run_family_bf16_control",
385
+ )
386
 
387
 
388
  if __name__ == "__main__":