Upload export_qwen2_wc.py with huggingface_hub
Browse files- export_qwen2_wc.py +78 -83
export_qwen2_wc.py
CHANGED
@@ -180,12 +180,20 @@ def generate() -> None:
|
|
180 |
torch_model = StatefulQwen2ForCausalLM(MODEL_ID, max_context_size=max_context_size)
|
181 |
torch_model.eval()
|
182 |
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
causal_mask: torch.Tensor = torch.ones((1, 1, 1, input_ids.shape[-1] + 1), dtype=torch.float32)
|
185 |
|
186 |
# Set the output length
|
187 |
output_length = 20
|
188 |
|
|
|
|
|
189 |
# Initialize the output tensor
|
190 |
output_tokens = input_ids
|
191 |
|
@@ -195,9 +203,12 @@ def generate() -> None:
|
|
195 |
#torch_model.kv_cache.past_seen_tokens = causal_mask.shape[-1] - output_tokens.shape[-1]
|
196 |
|
197 |
# Get the model output
|
198 |
-
model_inp = output_tokens[:, -
|
|
|
|
|
|
|
199 |
print(f"KEVINDEBUG model_inp: {model_inp} causal_mask: {causal_mask}")
|
200 |
-
output = torch_model(
|
201 |
|
202 |
# Get the most likely token IDs
|
203 |
output_ids = torch.argmax(output, dim=-1)
|
@@ -209,9 +220,6 @@ def generate() -> None:
|
|
209 |
# Update the causal mask
|
210 |
causal_mask = torch.ones((1, 1, 1, output_tokens.shape[-1] + 1), dtype=torch.float32)
|
211 |
|
212 |
-
# Decode output tokens using the tokenizer
|
213 |
-
from transformers import AutoTokenizer
|
214 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
215 |
decoded_output = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
216 |
print(f"input : {tokenizer.decode(input_ids[0])} output: {decoded_output}")
|
217 |
|
@@ -277,88 +285,75 @@ if __name__ == "__main__":
|
|
277 |
generate()
|
278 |
|
279 |
###
|
280 |
-
#(
|
281 |
-
#
|
282 |
-
#KEVINDEBUG model_inp: tensor([[
|
283 |
-
#KEVINDEBUG output_tokens: tensor([[
|
284 |
-
#KEVINDEBUG model_inp: tensor([[
|
285 |
-
#KEVINDEBUG output_tokens: tensor([[
|
286 |
-
#KEVINDEBUG model_inp: tensor([[
|
287 |
-
#KEVINDEBUG output_tokens: tensor([[
|
288 |
-
#KEVINDEBUG model_inp: tensor([[
|
289 |
-
#KEVINDEBUG output_tokens: tensor([[
|
290 |
-
#
|
291 |
-
#KEVINDEBUG
|
292 |
-
#KEVINDEBUG
|
293 |
-
#
|
294 |
-
#
|
295 |
-
#KEVINDEBUG
|
296 |
-
#
|
297 |
-
#KEVINDEBUG
|
298 |
-
#
|
299 |
-
#
|
300 |
-
#
|
301 |
-
#KEVINDEBUG output_tokens: tensor([[
|
302 |
-
#
|
303 |
-
#KEVINDEBUG model_inp: tensor([[
|
304 |
-
#
|
305 |
-
#
|
306 |
-
#
|
307 |
-
#KEVINDEBUG
|
308 |
-
#
|
309 |
-
#KEVINDEBUG
|
310 |
-
# 1181, 17646, 11, 7674, 11]])
|
311 |
-
#KEVINDEBUG model_inp: tensor([[ 7985, 264, 32794, 911, 60249, 11, 17689, 11, 21080, 389,
|
312 |
-
# 1181, 17646, 11, 7674, 11]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
313 |
-
#KEVINDEBUG output_tokens: tensor([[ 7985, 264, 32794, 911, 60249, 11, 17689, 11, 21080, 389,
|
314 |
-
# 1181, 17646, 11, 7674, 11, 323]])
|
315 |
-
#KEVINDEBUG model_inp: tensor([[ 7985, 264, 32794, 911, 60249, 11, 17689, 11, 21080, 389,
|
316 |
-
# 1181, 17646, 11, 7674, 11, 323]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
317 |
-
#KEVINDEBUG output_tokens: tensor([[ 7985, 264, 32794, 911, 60249, 11, 17689, 11, 21080, 389,
|
318 |
-
# 1181, 17646, 11, 7674, 11, 323, 35005]])
|
319 |
-
#KEVINDEBUG model_inp: tensor([[ 7985, 264, 32794, 911, 60249, 11, 17689, 11, 21080, 389,
|
320 |
-
# 1181, 17646, 11, 7674, 11, 323, 35005]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
321 |
# 1.]]]])
|
322 |
-
#KEVINDEBUG output_tokens: tensor([[
|
323 |
-
#
|
324 |
-
#KEVINDEBUG model_inp: tensor([[
|
325 |
-
# 1181, 17646, 11, 7674, 11, 323, 35005, 13]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
326 |
# 1., 1.]]]])
|
327 |
-
#KEVINDEBUG output_tokens: tensor([[
|
328 |
-
#
|
329 |
-
#KEVINDEBUG model_inp: tensor([[
|
330 |
-
# 1181, 17646, 11, 7674, 11, 323, 35005, 13, 5443]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
331 |
# 1., 1., 1.]]]])
|
332 |
-
#KEVINDEBUG output_tokens: tensor([[
|
333 |
-
#
|
334 |
-
#KEVINDEBUG model_inp: tensor([[
|
335 |
-
# 1181, 17646, 11, 7674, 11, 323, 35005, 13, 5443, 42020]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
336 |
# 1., 1., 1., 1.]]]])
|
337 |
-
#KEVINDEBUG output_tokens: tensor([[
|
338 |
-
#
|
339 |
-
#
|
340 |
-
#KEVINDEBUG model_inp: tensor([[
|
341 |
-
# 17646, 11, 7674, 11, 323, 35005, 13, 5443, 42020, 40445]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
342 |
# 1., 1., 1., 1., 1.]]]])
|
343 |
-
#KEVINDEBUG output_tokens: tensor([[
|
344 |
-
#
|
345 |
-
#
|
346 |
-
#KEVINDEBUG model_inp: tensor([[
|
347 |
-
# 11, 7674, 11, 323, 35005, 13, 5443, 42020, 40445, 323]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
348 |
# 1., 1., 1., 1., 1., 1.]]]])
|
349 |
-
#KEVINDEBUG output_tokens: tensor([[
|
350 |
-
#
|
351 |
-
#
|
352 |
-
#KEVINDEBUG model_inp: tensor([[
|
353 |
-
# 7674, 11, 323, 35005, 13, 5443, 42020, 40445, 323, 32976]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
354 |
# 1., 1., 1., 1., 1., 1., 1.]]]])
|
355 |
-
#KEVINDEBUG output_tokens: tensor([[
|
356 |
-
#
|
357 |
-
#
|
358 |
-
#KEVINDEBUG model_inp: tensor([[
|
359 |
-
# 11, 323, 35005, 13, 5443, 42020, 40445, 323, 32976, 7987]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
360 |
# 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
361 |
-
#KEVINDEBUG output_tokens: tensor([[
|
362 |
-
#
|
363 |
-
#
|
364 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
180 |
torch_model = StatefulQwen2ForCausalLM(MODEL_ID, max_context_size=max_context_size)
|
181 |
torch_model.eval()
|
182 |
|
183 |
+
# Decode output tokens using the tokenizer
|
184 |
+
from transformers import AutoTokenizer
|
185 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
186 |
+
#initial_prompt = "Write a christmas Carol"
|
187 |
+
initial_prompt = "Here is a poem about Valencia"
|
188 |
+
|
189 |
+
input_ids = tokenizer(initial_prompt, return_tensors='pt').input_ids
|
190 |
causal_mask: torch.Tensor = torch.ones((1, 1, 1, input_ids.shape[-1] + 1), dtype=torch.float32)
|
191 |
|
192 |
# Set the output length
|
193 |
output_length = 20
|
194 |
|
195 |
+
is_first_run = True
|
196 |
+
|
197 |
# Initialize the output tensor
|
198 |
output_tokens = input_ids
|
199 |
|
|
|
203 |
#torch_model.kv_cache.past_seen_tokens = causal_mask.shape[-1] - output_tokens.shape[-1]
|
204 |
|
205 |
# Get the model output
|
206 |
+
model_inp = output_tokens[:, -1:]
|
207 |
+
if is_first_run:
|
208 |
+
model_inp = input_ids
|
209 |
+
is_first_run = False
|
210 |
print(f"KEVINDEBUG model_inp: {model_inp} causal_mask: {causal_mask}")
|
211 |
+
output = torch_model(model_inp, causal_mask) # Start with a sub-squence that long so need multiple previous when size only very lwo larger later same past arg a so try keeping right padded!
|
212 |
|
213 |
# Get the most likely token IDs
|
214 |
output_ids = torch.argmax(output, dim=-1)
|
|
|
220 |
# Update the causal mask
|
221 |
causal_mask = torch.ones((1, 1, 1, output_tokens.shape[-1] + 1), dtype=torch.float32)
|
222 |
|
|
|
|
|
|
|
223 |
decoded_output = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
224 |
print(f"input : {tokenizer.decode(input_ids[0])} output: {decoded_output}")
|
225 |
|
|
|
285 |
generate()
|
286 |
|
287 |
###
|
288 |
+
#KEVINDEBUG model_inp: tensor([[ 8420, 374, 264, 32794, 911, 60249]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1.]]]])
|
289 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11]])
|
290 |
+
#KEVINDEBUG model_inp: tensor([[11]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
291 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689]])
|
292 |
+
#KEVINDEBUG model_inp: tensor([[17689]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
293 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13]])
|
294 |
+
#KEVINDEBUG model_inp: tensor([[13]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
295 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084]])
|
296 |
+
#KEVINDEBUG model_inp: tensor([[1084]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
297 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
298 |
+
# 374]])
|
299 |
+
#KEVINDEBUG model_inp: tensor([[374]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
300 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
301 |
+
# 374, 264]])
|
302 |
+
#KEVINDEBUG model_inp: tensor([[264]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
303 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
304 |
+
# 374, 264, 32794]])
|
305 |
+
#KEVINDEBUG model_inp: tensor([[32794]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
306 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
307 |
+
# 374, 264, 32794, 911]])
|
308 |
+
#KEVINDEBUG model_inp: tensor([[911]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
309 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
310 |
+
# 374, 264, 32794, 911, 279]])
|
311 |
+
#KEVINDEBUG model_inp: tensor([[279]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
312 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
313 |
+
# 374, 264, 32794, 911, 279, 3283]])
|
314 |
+
#KEVINDEBUG model_inp: tensor([[3283]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
315 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
316 |
+
# 374, 264, 32794, 911, 279, 3283, 315]])
|
317 |
+
#KEVINDEBUG model_inp: tensor([[315]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
# 1.]]]])
|
319 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
320 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249]])
|
321 |
+
#KEVINDEBUG model_inp: tensor([[60249]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
322 |
# 1., 1.]]]])
|
323 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
324 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11]])
|
325 |
+
#KEVINDEBUG model_inp: tensor([[11]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
326 |
# 1., 1., 1.]]]])
|
327 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
328 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689]])
|
329 |
+
#KEVINDEBUG model_inp: tensor([[17689]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
330 |
# 1., 1., 1., 1.]]]])
|
331 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
332 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689,
|
333 |
+
# 13]])
|
334 |
+
#KEVINDEBUG model_inp: tensor([[13]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
335 |
# 1., 1., 1., 1., 1.]]]])
|
336 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
337 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689,
|
338 |
+
# 13, 576]])
|
339 |
+
#KEVINDEBUG model_inp: tensor([[576]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
340 |
# 1., 1., 1., 1., 1., 1.]]]])
|
341 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
342 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689,
|
343 |
+
# 13, 576, 32794]])
|
344 |
+
#KEVINDEBUG model_inp: tensor([[32794]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
345 |
# 1., 1., 1., 1., 1., 1., 1.]]]])
|
346 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
347 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689,
|
348 |
+
# 13, 576, 32794, 374]])
|
349 |
+
#KEVINDEBUG model_inp: tensor([[374]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
|
|
350 |
# 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
351 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
352 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689,
|
353 |
+
# 13, 576, 32794, 374, 5326]])
|
354 |
+
#KEVINDEBUG model_inp: tensor([[5326]]) causal_mask: tensor([[[[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
|
355 |
+
# 1., 1., 1., 1., 1., 1., 1., 1., 1.]]]])
|
356 |
+
#KEVINDEBUG output_tokens: tensor([[ 8420, 374, 264, 32794, 911, 60249, 11, 17689, 13, 1084,
|
357 |
+
# 374, 264, 32794, 911, 279, 3283, 315, 60249, 11, 17689,
|
358 |
+
# 13, 576, 32794, 374, 5326, 304]])
|
359 |
+
#input : Here is a poem about Valencia output: Here is a poem about Valencia, Spain. It is a poem about the city of Valencia, Spain. The poem is written in
|