Spaces:
Running on Zero
Running on Zero
Add bounded larger-family CUDA quantizer controls
Browse files
app.py
CHANGED
|
@@ -371,6 +371,114 @@ def run_family_bf16_control(family: str) -> dict:
|
|
| 371 |
}
|
| 372 |
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
with gr.Blocks() as demo:
|
| 375 |
gr.Markdown(
|
| 376 |
"# Embedding Quantization CUDA Control\n"
|
|
@@ -416,6 +524,24 @@ with gr.Blocks() as demo:
|
|
| 416 |
concurrency_limit=1,
|
| 417 |
api_name="run_family_bf16_control",
|
| 418 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
|
| 421 |
if __name__ == "__main__":
|
|
|
|
| 371 |
}
|
| 372 |
|
| 373 |
|
| 374 |
+
def _run_family_quantized_control(family: str, variant: str) -> dict:
|
| 375 |
+
if family not in BF16_FAMILIES:
|
| 376 |
+
raise ValueError(f"unsupported family: {family}")
|
| 377 |
+
if variant not in QUANTIZERS:
|
| 378 |
+
raise ValueError(f"unsupported quantizer: {variant}")
|
| 379 |
+
|
| 380 |
+
spec = BF16_FAMILIES[family]
|
| 381 |
+
config = quantization_config(variant)
|
| 382 |
+
pairs = json.loads(INPUT_PATH.read_text())
|
| 383 |
+
query_texts = [detailed_instruction(item["query"]) for item in pairs]
|
| 384 |
+
document_texts = [item["document"] for item in pairs]
|
| 385 |
+
family_tokenizer = AutoTokenizer.from_pretrained(
|
| 386 |
+
spec["path"], padding_side="left", trust_remote_code=True
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
gc.collect()
|
| 390 |
+
torch.cuda.empty_cache()
|
| 391 |
+
torch.cuda.reset_peak_memory_stats()
|
| 392 |
+
torch.cuda.synchronize()
|
| 393 |
+
allocation_before = int(torch.cuda.memory_allocated())
|
| 394 |
+
load_started = time.perf_counter()
|
| 395 |
+
family_model = AutoModel.from_pretrained(
|
| 396 |
+
spec["path"],
|
| 397 |
+
quantization_config=config,
|
| 398 |
+
device_map={"": 0},
|
| 399 |
+
trust_remote_code=True,
|
| 400 |
+
).eval()
|
| 401 |
+
torch.cuda.synchronize()
|
| 402 |
+
load_seconds = time.perf_counter() - load_started
|
| 403 |
+
allocation_after_load = int(torch.cuda.memory_allocated())
|
| 404 |
+
|
| 405 |
+
encode_started = time.perf_counter()
|
| 406 |
+
queries = np.stack([
|
| 407 |
+
encode_with(family_model, text, family_tokenizer) for text in query_texts
|
| 408 |
+
])
|
| 409 |
+
documents = np.stack([
|
| 410 |
+
encode_with(family_model, text, family_tokenizer) for text in document_texts
|
| 411 |
+
])
|
| 412 |
+
torch.cuda.synchronize()
|
| 413 |
+
encode_seconds = time.perf_counter() - encode_started
|
| 414 |
+
metrics, scores, ranks = retrieval_metrics(queries, documents)
|
| 415 |
+
metrics.update({
|
| 416 |
+
"lane": f"cuda-zerogpu-{variant}",
|
| 417 |
+
"family": family,
|
| 418 |
+
"model": spec["source"],
|
| 419 |
+
"source_revision": spec["revision"],
|
| 420 |
+
"quantizer": variant,
|
| 421 |
+
"quantization_config": config.to_dict(),
|
| 422 |
+
"load_seconds": load_seconds,
|
| 423 |
+
"encode_seconds": encode_seconds,
|
| 424 |
+
"texts_per_second": len(query_texts + document_texts) / encode_seconds,
|
| 425 |
+
"torch_version": torch.__version__,
|
| 426 |
+
"cuda_device": torch.cuda.get_device_name(0),
|
| 427 |
+
"cuda_allocation_before_load": allocation_before,
|
| 428 |
+
"cuda_allocation_after_load": allocation_after_load,
|
| 429 |
+
"cuda_incremental_model_allocation": allocation_after_load - allocation_before,
|
| 430 |
+
"cuda_peak_bytes": int(torch.cuda.max_memory_allocated()),
|
| 431 |
+
})
|
| 432 |
+
output_dir = DATA_ROOT / "cloud-results" / family
|
| 433 |
+
result = {
|
| 434 |
+
"metrics": metrics,
|
| 435 |
+
"cuda_bf16_comparison": compare_vectors(
|
| 436 |
+
queries,
|
| 437 |
+
documents,
|
| 438 |
+
scores,
|
| 439 |
+
ranks,
|
| 440 |
+
output_dir / "cuda-bf16.npz",
|
| 441 |
+
"vs_cuda_bf16",
|
| 442 |
+
),
|
| 443 |
+
"mlx_bf16_comparison": compare_vectors(
|
| 444 |
+
queries,
|
| 445 |
+
documents,
|
| 446 |
+
scores,
|
| 447 |
+
ranks,
|
| 448 |
+
DATA_ROOT / "local-results/full-q4-q6-q8" / family / "quality/bf16.npz",
|
| 449 |
+
"vs_mlx_bf16",
|
| 450 |
+
),
|
| 451 |
+
}
|
| 452 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 453 |
+
stem = f"cuda-{variant}"
|
| 454 |
+
np.savez_compressed(
|
| 455 |
+
output_dir / f"{stem}.npz",
|
| 456 |
+
queries=queries,
|
| 457 |
+
documents=documents,
|
| 458 |
+
scores=scores,
|
| 459 |
+
ranks=ranks,
|
| 460 |
+
metrics=np.array(json.dumps(metrics)),
|
| 461 |
+
)
|
| 462 |
+
(output_dir / f"{stem}.json").write_text(json.dumps(result, indent=2) + "\n")
|
| 463 |
+
|
| 464 |
+
del family_model, family_tokenizer
|
| 465 |
+
gc.collect()
|
| 466 |
+
torch.cuda.empty_cache()
|
| 467 |
+
return result
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
@spaces.GPU(duration=300)
|
| 471 |
+
def run_family_quantized_control(family: str, variant: str) -> dict:
|
| 472 |
+
try:
|
| 473 |
+
return _run_family_quantized_control(family, variant)
|
| 474 |
+
except Exception as error:
|
| 475 |
+
return {
|
| 476 |
+
"diagnostic_error": type(error).__name__,
|
| 477 |
+
"diagnostic_message": str(error),
|
| 478 |
+
"diagnostic_traceback": traceback.format_exc(),
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
|
| 482 |
with gr.Blocks() as demo:
|
| 483 |
gr.Markdown(
|
| 484 |
"# Embedding Quantization CUDA Control\n"
|
|
|
|
| 524 |
concurrency_limit=1,
|
| 525 |
api_name="run_family_bf16_control",
|
| 526 |
)
|
| 527 |
+
gr.Markdown("## Larger-family CUDA-native quantizer controls")
|
| 528 |
+
quant_family = gr.Dropdown(
|
| 529 |
+
choices=list(BF16_FAMILIES),
|
| 530 |
+
value="qwen3-embedding-8b",
|
| 531 |
+
label="Model family",
|
| 532 |
+
)
|
| 533 |
+
family_quantizer = gr.Dropdown(
|
| 534 |
+
choices=list(QUANTIZERS), value="bnb-int8", label="Quantizer"
|
| 535 |
+
)
|
| 536 |
+
family_quant_button = gr.Button("Run bounded family quantizer control")
|
| 537 |
+
family_quant_output = gr.JSON(label="Family quantized result")
|
| 538 |
+
family_quant_button.click(
|
| 539 |
+
fn=run_family_quantized_control,
|
| 540 |
+
inputs=[quant_family, family_quantizer],
|
| 541 |
+
outputs=family_quant_output,
|
| 542 |
+
concurrency_limit=1,
|
| 543 |
+
api_name="run_family_quantized_control",
|
| 544 |
+
)
|
| 545 |
|
| 546 |
|
| 547 |
if __name__ == "__main__":
|