Repository navigation
Expand file tree
/
Copy pathtp_gemma3_12b.py
More file actions
279 lines (220 loc) · 7.9 KB
/
Copy pathtp_gemma3_12b.py
File metadata and controls
279 lines (220 loc) · 7.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""run.py:"""
#!/usr/bin/env python
# Example showing how to use muiLLM's tensor parallelism support on the Gemma 3 12B model
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from transformers import (
AutoModelForCausalLM,
GenerationMixin,
AutoProcessor,
AutoTokenizer,
)
import torch
import torch.nn as nn
from typing import List, Union
def generate(
model: GenerationMixin,
processor,
prompt: Union[str, List[str]],
max_new_tokens=20,
streamer=None,
**kwargs,
) -> Union[str, List[str]]:
if streamer is not None:
print("--- start streaming ---")
single_prompt = isinstance(prompt, str)
if single_prompt:
prompts = [prompt]
else:
prompts = prompt
with torch.no_grad():
inputs = processor.apply_chat_template(
prompt,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(device="cuda")
input_len = inputs["input_ids"].shape[-1]
outputs = model.generate(
**inputs,
streamer=streamer,
max_new_tokens=max_new_tokens,
do_sample=True,
**kwargs,
)
outputs = [outputs[i][input_len:] for i in range(len(outputs))]
if streamer is not None:
print("--- end streaming ---")
texts = [
processor.decode(outputs[i], skip_special_tokens=False)
for i in range(len(outputs))
]
if single_prompt:
return texts[0]
else:
return texts
def time_func(f):
import time
start_time = time.time()
ret = f()
end_time = time.time()
elapsed_time = end_time - start_time
return ret, elapsed_time
def profile_func(f, trace_path="trace.json"):
from torch.profiler import profile, ProfilerActivity
with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA]) as prof:
ret = f()
prof.export_chrome_trace(trace_path)
return ret
def run(rank, size):
stream_output = os.getenv("STREAM_OUTPUT", "1") == "1"
# this example requires the Gemma 3 12B model
# Provided that you have a HF token to access the Gemma models, you can download it with
# huggingface-cli download --token <your_token> google/gemma-3-12b-it --local-dir google-gemma-3-12b-it
# either set this environment variable before running the example, or adapt the path
model_id = os.getenv("GEMMA3_12B_PATH", "/storage/models/google-gemma-3-12b-it/")
## Load the original model & tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id, padding_side="left")
processor = AutoProcessor.from_pretrained(model_id)
model: nn.Module = AutoModelForCausalLM.from_pretrained(
model_id, device_map="cuda", torch_dtype=torch.bfloat16
)
from transformers import TextStreamer
streamer = (
TextStreamer(tokenizer, skip_prompt=True)
if (stream_output and rank == 0)
else None
)
if rank == 0:
print("Model : ", model)
# 5 tokens prompt
prompt = [
{
"role": "system",
"content": [{"type": "text", "text": "You are a helpful assistant."}],
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Write a blogpost about how Croatia is the dream destination for summer vacations.",
},
],
},
]
tokenized_prompts = processor.apply_chat_template(
prompt,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
)
if rank == 0:
print("tokenized prompts: ", tokenized_prompts["input_ids"].shape)
num_input_tokens = tokenized_prompts["input_ids"].shape[1]
batch_size = tokenized_prompts["input_ids"].shape[0]
num_output_tokens = 256
num_total_tokens = (num_input_tokens + num_output_tokens) * batch_size
# Have a look at the original speed
if num_total_tokens < 100:
text, time = time_func(
lambda: generate(model, processor, prompt, 10, disable_compile=True)
)
text, time = time_func(
lambda: generate(
model, processor, prompt, num_output_tokens, disable_compile=True
)
)
# check how many tokens were actually generated
tokenized_outputs = tokenizer(text, return_tensors="pt", padding="longest")
num_output_tokens = tokenized_outputs["input_ids"].shape[1]
num_total_tokens = (num_input_tokens + num_output_tokens) * batch_size
print("[Original] Completion: ", text)
print("[Original] Time: ", time)
print(
f"tot toks/s: {num_total_tokens / time} (batch size {batch_size}, prompt len {num_input_tokens}, output len {num_output_tokens})"
)
# Use the muiLLM replacements layers
from muillm.engine import init_engine
# use auto-detected tensor parallelism level by setting to None
model = init_engine(model, tensor_parallelism=None)
if rank == 0:
print("Optimized models: ", model)
tokenized_prompts = processor.apply_chat_template(
prompt,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
)
if rank == 0:
print("tokenized prompts: ", tokenized_prompts["input_ids"].shape)
num_input_tokens = tokenized_prompts["input_ids"].shape[1]
batch_size = tokenized_prompts["input_ids"].shape[0]
num_output_tokens = 256
num_total_tokens = (num_input_tokens + num_output_tokens) * batch_size
# Have a look at the speed
text, time = time_func(
lambda: generate(model, processor, prompt, 10, disable_compile=True)
)
text, time = time_func(
lambda: generate(
model,
processor,
prompt,
num_output_tokens,
streamer=streamer,
disable_compile=True,
)
)
# check how many tokens were actually generated
tokenized_outputs = tokenizer(text, return_tensors="pt", padding="longest")
num_output_tokens = tokenized_outputs["input_ids"].shape[1]
num_total_tokens = (num_input_tokens + num_output_tokens) * batch_size
if rank == 0:
if streamer is None:
print("[Optimized] Completion: ", text)
print("[Optimized] Time: ", time)
print(
f"tot toks/s: {num_total_tokens / time} (batch size {batch_size}, prompt len {num_input_tokens}, output len {num_output_tokens})"
)
# Save a pytorch trace (visualizable for example with https://ui.perfetto.dev)
text, time = profile_func(
lambda: time_func(
lambda: generate(
model, processor, prompt, num_output_tokens, disable_compile=True
)
),
trace_path=f"trace_gemma3_12b_muillm_tp{size}_unbatched_rank{rank}.json",
)
def init_process(rank, size, fn, backend="nccl"):
"""Initialize the distributed environment."""
os.environ["MASTER_ADDR"] = "127.0.0.1"
os.environ["MASTER_PORT"] = "29500"
os.environ["WORLD_SIZE"] = str(size)
os.environ["LOCAL_SIZE"] = str(size)
os.environ["RANK"] = str(rank)
os.environ["LOCAL_RANK"] = str(rank)
local_size = torch.cuda.device_count()
print(f"(rank {rank}) local_size = {local_size}")
# set the current device to the GPU we need
torch.cuda.set_device(rank)
dist.init_process_group(backend, rank=rank, world_size=size)
fn(rank, size)
if __name__ == "__main__":
# get the number of GPUs
size = torch.cuda.device_count()
print(f"{size} GPUs available.")
# Spawn one subprocess per GPU
processes = []
mp.set_start_method("spawn")
for rank in range(size):
p = mp.Process(target=init_process, args=(rank, size, run))
p.start()
processes.append(p)
for p in processes:
p.join()