Views
No views yet
1# Clone the NanoChat repository
2git clone https://github.com/karpathy/nanochat.git
3cd nanochat
4
5# Install dependencies (requires CUDA)
6uv venv
7uv sync --extra gpu
8
9# Activate the virtual environment
10source .venv/bin/activate1# Prepare environment and clone NanoChat
2wget https://raw.githubusercontent.com/jasonacox/dgx-spark/main/nanochat/prepare.sh
3chmod +x prepare.sh
4./prepare.sh --setup-only1# Clone the test script
2wget https://raw.githubusercontent.com/jasonacox/dgx-spark/main/nanochat/hf_test.py
3
4# Set python environment
5source nanochat/.venv/bin/activate
6
7# Install dependencies
8pip install huggingface_hub
9
10# Run with this model
11python hf_test.py --model jasonacox/jason-nanochat-1.8B-midtrain1import sys
2import os
3import glob
4from huggingface_hub import snapshot_download
5import torch
6from contextlib import nullcontext
7
8# Download model from HuggingFace
9print("Downloading model...")
10model_path = snapshot_download(
11 repo_id="jasonacox/jason-nanochat-1.8B-midtrain",
12 cache_dir=os.path.expanduser("~/.cache/nanochat/hf_downloads")
13)
14
15# Setup NanoChat (clone if needed)
16nanochat_path = "nanochat"
17if not os.path.exists(nanochat_path):
18 os.system("git clone https://github.com/karpathy/nanochat.git")
19 os.system("cd nanochat && uv sync --extra gpu")
20
21sys.path.insert(0, nanochat_path)
22
23from nanochat.checkpoint_manager import build_model
24from nanochat.common import compute_init, autodetect_device_type
25from nanochat.engine import Engine
26
27# Initialize
28device_type = autodetect_device_type()
29_, _, _, _, device = compute_init(device_type)
30ptdtype = torch.bfloat16
31autocast_ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype) if device_type == "cuda" else nullcontext()
32
33# Load model
34checkpoint_files = glob.glob(os.path.join(model_path, "model_*.pt"))
35step = int(os.path.basename(checkpoint_files[0]).split("_")[-1].split(".")[0])
36model, tokenizer, _ = build_model(model_path, step, device, phase="eval")
37engine = Engine(model, tokenizer)
38
39# Generate
40prompt = "Hello, how are you?"
41tokens = tokenizer.encode(prompt)
42print(f"Prompt: {prompt}\nResponse: ", end="", flush=True)
43
44with autocast_ctx:
45 for token_column, _ in engine.generate(tokens, num_samples=1, max_tokens=100, temperature=0.8, top_k=50):
46 print(tokenizer.decode([token_column[0]]), end="", flush=True)
47print()1@misc{jason-nanochat-1.8B,
2 author = {Jason A. Cox},
3 title = {jason-nanochat-1.8B-midtrain},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/jasonacox/jason-nanochat-1.8B-midtrain}}
7}