()
| 403 | # ── Main ───────────────────────────────────────────────────────────── |
| 404 | |
| 405 | def main(): |
| 406 | parser = argparse.ArgumentParser(description="Extract nomic-embed-code token embeddings") |
| 407 | parser.add_argument("--output-dir", default="vendored/nomic", |
| 408 | help="Output directory (default: vendored/nomic)") |
| 409 | parser.add_argument("--device", default=None, |
| 410 | help="Device: cuda, mps, cpu (auto-detected)") |
| 411 | parser.add_argument("--skip-attention", action="store_true", |
| 412 | help="Skip simulated attention (faster, lower quality)") |
| 413 | parser.add_argument("--batch-size", type=int, default=BATCH_SIZE, |
| 414 | help=f"Batch size (default: {BATCH_SIZE})") |
| 415 | parser.add_argument("--checkpoint", default=None, |
| 416 | help="Checkpoint file path (auto: <output-dir>/checkpoint.npz)") |
| 417 | args = parser.parse_args() |
| 418 | |
| 419 | batch_size = args.batch_size |
| 420 | |
| 421 | # Auto-detect device |
| 422 | # Prefer CPU for 7B models on Apple Silicon — MPS shares unified memory |
| 423 | # with the system and can cause OOM/crashes. CPU keeps allocation predictable. |
| 424 | # Use --device mps to override if you have enough headroom (32GB+). |
| 425 | if args.device: |
| 426 | device = args.device |
| 427 | elif torch.cuda.is_available(): |
| 428 | device = "cuda" |
| 429 | else: |
| 430 | device = "cpu" |
| 431 | |
| 432 | # Force line-buffered stdout so tee/log sees output immediately |
| 433 | sys.stdout.reconfigure(line_buffering=True) |
| 434 | |
| 435 | print(f"device={device}") |
| 436 | print(f"threads={torch.get_num_threads()}") |
| 437 | print(f"model={MODEL_NAME}") |
| 438 | print(f"output_dim={OUTPUT_DIM}") |
| 439 | print() |
| 440 | |
| 441 | # Create output dir |
| 442 | out_dir = Path(args.output_dir) |
| 443 | out_dir.mkdir(parents=True, exist_ok=True) |
| 444 | |
| 445 | checkpoint_path = args.checkpoint or str(out_dir / "checkpoint.npz") |
| 446 | |
| 447 | # ── Step 1: Load model + tokenizer ── |
| 448 | print("step 1: loading model + tokenizer...") |
| 449 | t0 = time.time() |
| 450 | tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True) |
| 451 | model = AutoModel.from_pretrained( |
| 452 | MODEL_NAME, |
| 453 | trust_remote_code=True, |
| 454 | dtype=torch.float16, # 7B×2B = ~14GB (vs 28GB float32) |
| 455 | low_cpu_mem_usage=True, # Stream weights, no 2x peak during load |
| 456 | ) |
| 457 | model = model.to(device) |
| 458 | print(f" loaded in {time.time() - t0:.1f}s") |
| 459 | print(f" hidden_size={model.config.hidden_size}") |
| 460 | print(f" vocab_size={tokenizer.vocab_size}") |
| 461 | print() |
| 462 |
no test coverage detected