Views
No views yet
pip install git+https://github.com/huggingface/transformers.git@nanochat-implementation1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4
5model_id="nanochat-students/d20-chat-transformers"
6max_new_tokens=64
7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
9tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=False)
10model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=False, dtype=torch.bfloat16).to(device)
11model.eval()
12
13conversation = [
14 {"role": "user", "content": "What is the capital of France?"},
15]
16
17inputs = tokenizer.apply_chat_template(
18 conversation,
19 add_generation_prompt=True,
20 tokenize=True,
21 return_tensors="pt"
22).to(device)
23
24with torch.no_grad():
25 outputs = model.generate(
26 **inputs,
27 max_new_tokens=max_new_tokens,
28 )
29
30# Decode only the generated tokens (excluding the input prompt)
31generated_tokens = outputs[0, inputs.input_ids.shape[1]:]
32print(tokenizer.decode(generated_tokens, skip_special_tokens=True))vllm serve nanochat-students/nanochat-d20 --enforce-eager 1url http://localhost:8000/v1/completions \
2> -H "Content-Type: application/json" \
3> -d '{"model": "nanochat-students/nanochat-d20", "prompt": "What is the capital of France?, "max_tokens": 7, "temperature": 0}'