-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (179 loc) · 6.63 KB
/
Copy pathmain.py
File metadata and controls
190 lines (179 loc) · 6.63 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
from mlx_lm import load
import mlx_lm
import mlx.core as mx
import argparse
import mlx_lm.sample_utils
from model_names import shortcut_name_to_full_name
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, default="qwen3-0.6b")
parser.add_argument("--draft-model", type=str, default=None)
parser.add_argument(
"--prompt",
type=str,
default="Give me a short introduction to large language model.",
)
parser.add_argument("--solution", type=str, default="tiny_llm")
parser.add_argument("--loader", type=str, default="week1")
parser.add_argument("--device", type=str, default="gpu")
parser.add_argument("--sampler-temp", type=float, default=0)
parser.add_argument("--sampler-top-p", type=float, default=None)
parser.add_argument("--sampler-top-k", type=int, default=None)
parser.add_argument("--enable-thinking", action="store_true")
parser.add_argument(
"--disable-paged-attention",
action="store_true",
help="run the Week 3 Day 4 dense-gather compatibility checkpoint",
)
parser.add_argument(
"--week2-checkpoint",
choices=(
"kv-cache",
"quantized-matvec",
"decode-attention",
"rmsnorm",
"rope",
"swiglu",
"simd-matmul",
"split-k",
),
help="run one cumulative Week 2 model checkpoint",
)
args = parser.parse_args()
if args.week2_checkpoint is not None and args.loader != "week2":
parser.error("--week2-checkpoint requires --loader week2")
if args.week2_checkpoint is not None and args.solution == "mlx":
parser.error("--week2-checkpoint is not supported with --solution mlx")
if (
args.solution != "mlx"
and args.device != "gpu"
and (
args.loader == "week3"
or (args.loader == "week2" and args.week2_checkpoint != "kv-cache")
)
):
parser.error(
"The completed Week 2 and Week 3 custom-kernel models are GPU-only; "
"use the Week 2 kv-cache checkpoint for the readable pre-kernel path"
)
if args.disable_paged_attention and args.loader != "week3":
parser.error("--disable-paged-attention requires --loader week3")
if args.disable_paged_attention and args.solution == "mlx":
parser.error("--disable-paged-attention is not supported with --solution mlx")
use_mlx = False
if args.solution == "tiny_llm":
print("Using your tiny_llm solution")
from tiny_llm import (
models,
simple_generate,
simple_generate_with_kv_cache,
speculative_generate,
sampler,
)
elif args.solution == "tiny_llm_ref" or args.solution == "ref":
print("Using tiny_llm_ref solution")
from tiny_llm_ref import (
models,
simple_generate,
simple_generate_with_kv_cache,
speculative_generate,
sampler,
)
elif args.solution == "mlx":
use_mlx = True
from mlx_lm.generate import stream_generate
print("Using the original mlx model")
else:
raise ValueError(f"Solution {args.solution} not supported")
args.model = shortcut_name_to_full_name(args.model)
mlx_model, tokenizer = load(args.model)
if args.draft_model:
args.draft_model = shortcut_name_to_full_name(args.draft_model)
draft_mlx_model, draft_tokenizer = load(args.draft_model)
if args.loader == "week1":
raise ValueError("Draft model not supported for week1")
else:
draft_mlx_model = None
draft_tokenizer = None
with mx.stream(mx.gpu if args.device == "gpu" else mx.cpu):
if use_mlx:
tiny_llm_model = mlx_model
else:
if args.loader == "week1":
print(f"Using week1 loader for {args.model}")
tiny_llm_model = models.dispatch_model(args.model, mlx_model, week=1)
elif args.loader == "week2":
print(
f"Using week2 loader with thinking={args.enable_thinking} for {args.model}"
)
dispatch_kwargs = {}
if args.week2_checkpoint is not None:
dispatch_kwargs["checkpoint"] = args.week2_checkpoint
tiny_llm_model = models.dispatch_model(
args.model, mlx_model, week=2, **dispatch_kwargs
)
if draft_mlx_model is not None:
print(f"Using draft model {args.draft_model}")
draft_tiny_llm_model = models.dispatch_model(
args.draft_model,
draft_mlx_model,
week=2,
**dispatch_kwargs,
)
else:
draft_tiny_llm_model = None
elif args.loader == "week3":
print(
f"Using week3 loader with paged_attention={not args.disable_paged_attention} "
f"thinking={args.enable_thinking} for {args.model}"
)
tiny_llm_model = models.dispatch_model(
args.model,
mlx_model,
week=3,
enable_paged_attention=not args.disable_paged_attention,
)
if draft_mlx_model is not None:
print(f"Using draft model {args.draft_model}")
draft_tiny_llm_model = models.dispatch_model(
args.draft_model,
draft_mlx_model,
week=3,
enable_paged_attention=not args.disable_paged_attention,
)
else:
draft_tiny_llm_model = None
else:
raise ValueError(f"Loader {args.loader} not supported")
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": args.prompt},
]
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=args.enable_thinking,
)
if not use_mlx:
sampler = sampler.make_sampler(
args.sampler_temp, top_p=args.sampler_top_p, top_k=args.sampler_top_k
)
if args.loader == "week1":
simple_generate(tiny_llm_model, tokenizer, prompt, sampler=sampler)
elif args.loader in ("week2", "week3"):
if draft_tiny_llm_model is not None:
speculative_generate(
draft_tiny_llm_model,
tiny_llm_model,
draft_tokenizer,
tokenizer,
prompt,
)
else:
simple_generate_with_kv_cache(tiny_llm_model, tokenizer, prompt)
else:
sampler = mlx_lm.sample_utils.make_sampler(
args.sampler_temp, top_p=args.sampler_top_p, top_k=args.sampler_top_k
)
for resp in stream_generate(tiny_llm_model, tokenizer, prompt, sampler=sampler):
print(resp.text, end="", flush=True)