forked from rasbt/reasoning-from-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch02.py
More file actions
193 lines (150 loc) · 5.84 KB
/
Copy pathch02.py
File metadata and controls
193 lines (150 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt)
# Source for "Build a Reasoning Model (From Scratch)": https://mng.bz/lZ5B
# Code repository: https://github.com/rasbt/reasoning-from-scratch
from .qwen3 import KVCache
import warnings
import torch
def get_device(enable_tensor_cores=True):
if torch.cuda.is_available():
device = torch.device("cuda")
print("Using NVIDIA CUDA GPU")
if enable_tensor_cores:
major, minor = map(int, torch.__version__.split(".")[:2])
if (major, minor) >= (2, 9):
torch.backends.cuda.matmul.fp32_precision = "tf32"
torch.backends.cudnn.conv.fp32_precision = "tf32"
else:
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
elif torch.backends.mps.is_available():
device = torch.device("mps")
print("Using Apple Silicon GPU (MPS)")
elif torch.xpu.is_available():
device = torch.device("xpu")
print("Using Intel GPU")
else:
device = torch.device("cpu")
print("Using CPU")
return device
@torch.inference_mode()
def generate_text_basic(model, token_ids, max_new_tokens, eos_token_id=None):
input_length = token_ids.shape[1]
model.eval()
for _ in range(max_new_tokens):
out = model(token_ids)[:, -1]
next_token = torch.argmax(out, dim=-1, keepdim=True)
# Stop if all sequences in the batch have generated EOS
if (eos_token_id is not None
and next_token.item() == eos_token_id):
break
token_ids = torch.cat([token_ids, next_token], dim=1)
return token_ids[:, input_length:]
# Previously, chapter 2 used a non-streaming function, and the
# *_stream functions were introduced in the exercises
# While the simpler generate_text_basic_cache is not used anymore
# it is kept here for backwards compatibility reasons
@torch.inference_mode()
def generate_text_basic_cache(
model,
token_ids,
max_new_tokens,
eos_token_id=None
):
input_length = token_ids.shape[1]
model.eval()
cache = KVCache(n_layers=model.cfg["n_layers"])
model.reset_kv_cache()
out = model(token_ids, cache=cache)[:, -1]
generated_tokens = []
for _ in range(max_new_tokens):
next_token = torch.argmax(out, dim=-1, keepdim=True)
if (eos_token_id is not None
and next_token.item() == eos_token_id):
break
generated_tokens.append(next_token)
out = model(next_token, cache=cache)[:, -1]
if generated_tokens:
return torch.cat(generated_tokens, dim=1)
return token_ids[:, input_length:]
@torch.inference_mode()
def generate_text_basic_stream(
model,
token_ids,
max_new_tokens,
eos_token_id=None
):
model.eval()
for _ in range(max_new_tokens):
out = model(token_ids)[:, -1]
next_token = torch.argmax(out, dim=-1, keepdim=True)
if (eos_token_id is not None
and torch.all(next_token == eos_token_id)):
break
yield next_token
token_ids = torch.cat([token_ids, next_token], dim=1)
@torch.inference_mode()
def generate_text_basic_stream_cache(
model,
token_ids,
max_new_tokens,
eos_token_id=None
):
# input_length = token_ids.shape[1]
model.eval()
cache = KVCache(n_layers=model.cfg["n_layers"])
model.reset_kv_cache()
out = model(token_ids, cache=cache)[:, -1]
for _ in range(max_new_tokens):
next_token = torch.argmax(out, dim=-1, keepdim=True)
if (eos_token_id is not None
and torch.all(next_token == eos_token_id)):
break
yield next_token # New: Yield each token as it's generated
# token_ids = torch.cat([token_ids, next_token], dim=1)
out = model(next_token, cache=cache)[:, -1]
# return token_ids[:, input_length:]
def _print_peak_memory_stats(device):
device = torch.device(device)
backend = getattr(torch, device.type, None)
if backend is None or not hasattr(backend, "is_available"):
return
if not backend.is_available():
return
sync_fn = getattr(backend, "synchronize", None)
if callable(sync_fn):
try:
sync_fn(device=device)
except TypeError:
sync_fn()
try:
max_mem_bytes = backend.max_memory_allocated(device=device)
except TypeError:
max_mem_bytes = backend.max_memory_allocated()
max_mem_gb = max_mem_bytes / (1024 ** 3)
print(f"Max {device.type.upper()} memory allocated: {max_mem_gb:.2f} GB")
try:
backend.reset_peak_memory_stats(device=device)
except TypeError:
backend.reset_peak_memory_stats()
def generate_stats(output_token_ids, tokenizer, start_time,
end_time):
total_time = end_time - start_time
print(f"\n\nTime: {total_time:.2f} sec")
print(f"{int(output_token_ids.numel() / total_time)} tokens/sec")
for name, backend in (("CUDA", getattr(torch, "cuda", None)),
("XPU", getattr(torch, "xpu", None))):
if backend is not None and backend.is_available():
# Check whether we are actually using this backend
device_type = output_token_ids.device.type
if device_type != name.lower():
warnings.warn(
f"{name} is available but tensors are on "
f"{device_type}. Memory stats may be 0."
)
# Synchronize if supported (important for async backends)
if hasattr(backend, "synchronize"):
backend.synchronize()
max_mem_bytes = backend.max_memory_allocated()
max_mem_gb = max_mem_bytes / (1024 ** 3)
print(f"Max {name} memory allocated: {max_mem_gb:.2f} GB")
backend.reset_peak_memory_stats()