Views
No views yet
1import torch
2from llms_from_scratch.ch04 import GPTModel
3# For llms_from_scratch installation instructions, see:
4# https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg
5
6BASE_CONFIG = {
7 "vocab_size": 50257, # Vocabulary size
8 "context_length": 1024, # Context length
9 "drop_rate": 0.0, # Dropout rate
10 "qkv_bias": True # Query-key-value bias
11}
12
13
14gpt = GPTModel(BASE_CONFIG)
15gpt.load_state_dict(torch.load(file_name, weights_only=True))
16gpt.eval()
17
18device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19gpt.to(device);1import tiktoken
2from llms_from_scratch.ch05 import generate, text_to_token_ids, token_ids_to_text
3
4
5torch.manual_seed(123)
6
7tokenizer = tiktoken.get_encoding("gpt2")
8
9token_ids = generate(
10 model=gpt.to(device),
11 idx=text_to_token_ids("Every effort moves", tokenizer).to(device),
12 max_new_tokens=30,
13 context_size=BASE_CONFIG["context_length"],
14 top_k=1,
15 temperature=1.0
16)
17
18print("Output text:\n", token_ids_to_text(token_ids, tokenizer))