Views
No views yet
git clone to download this repository, you must have Git LFS installed (git lfs install followed by git lfs pull), otherwise best_model_10m.pt will just be a tiny pointer file and PyTorch will fail to load it.1import os
2import torch
3from huggingface_hub import hf_hub_download
4
5# Select the model repository
6repo_id = "nowefnyieryfner/llama-10m-base"
7model_filename = "best_model_10m.pt"
8
9print(f"Downloading model files from {repo_id}...")
10
11# Download files from Hugging Face Hub (cached automatically)
12model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
13model_code_path = hf_hub_download(repo_id=repo_id, filename="model.py")
14tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.json")
15
16# Dynamically import GPT from the downloaded model.py
17import importlib.util
18spec = importlib.util.spec_from_file_location("model", model_code_path)
19model_module = importlib.util.module_from_spec(spec)
20spec.loader.exec_module(model_module)
21GPT = model_module.GPT
22
23# Load tokenizer
24from tokenizers import Tokenizer
25tokenizer = Tokenizer.from_file(tokenizer_path)
26
27# Load model weights
28print("Loading model weights...")
29checkpoint = torch.load(model_path, map_location="cpu")
30config = checkpoint["config"]
31model = GPT(config)
32model.load_state_dict(checkpoint["model"])
33model.eval()
34
35# Move to GPU if available
36device = "cuda" if torch.cuda.is_available() else "cpu"
37model = model.to(device)
38print(f"Model loaded successfully on {device}!")
39
40# Sample prompt
41prompt = "<|im_start|>user\nWrite a python function to check if a number is prime.<|im_end|>\n<|im_start|>assistant\n"
42x = torch.tensor(tokenizer.encode(prompt).ids, dtype=torch.long, device=device).unsqueeze(0)
43
44print("\n--- Generating response ---")
45y = model.generate(x, max_new_tokens=100, temperature=0.8, top_k=50)
46print(tokenizer.decode(y[0].tolist()))pip install torch tokenizers huggingface_hub