Views
No views yet
1import torch
2from huggingface_hub import hf_hub_download
3
4from model import MoELMConfig, MoELanguageModel
5from tokenizer_utils import EOS_TOKEN, decode, encode, load_tokenizer, token_id
6
7repo = "owenqwenllmwine/t-nano"
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10ckpt_path = hf_hub_download(repo, "final.pt")
11tok_path = hf_hub_download(repo, "tokenizer.json")
12
13tokenizer = load_tokenizer(tok_path)
14
15ckpt = torch.load(ckpt_path, map_location=device)
16model = MoELanguageModel(MoELMConfig.from_dict(ckpt["config"])).to(device)
17model.load_state_dict(ckpt["model"])
18model.eval()
19
20prompt = "The history of artificial intelligence begins"
21x = torch.tensor([encode(tokenizer, prompt, add_bos=True)], device=device)
22
23with torch.no_grad():
24 y = model.generate(
25 x,
26 max_new_tokens=200,
27 eos_token_id=token_id(tokenizer, EOS_TOKEN),
28 temperature=0.8,
29 top_k=50,
30 top_p=0.95,
31 )
32
33print(decode(tokenizer, y[0].tolist(), skip_special_tokens=True))