cazyundee commited on
Commit
9a9fa33
·
verified ·
1 Parent(s): 4380259

Normalize TinyLlama chat template inputs

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -576,8 +576,15 @@ def _generate_tinyllama(prompt, max_new_tokens=64, temperature=0.7):
576
  temperature = max(0.0, min(2.0, float(temperature)))
577
  messages = [{"role": "user", "content": prompt}]
578
  if hasattr(tokenizer, "apply_chat_template"):
579
- inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
580
- attention_mask = torch.ones_like(inputs)
 
 
 
 
 
 
 
581
  else:
582
  encoded = tokenizer(prompt, return_tensors="pt")
583
  inputs = encoded["input_ids"]
@@ -585,7 +592,7 @@ def _generate_tinyllama(prompt, max_new_tokens=64, temperature=0.7):
585
  t0 = time.time()
586
  with torch.inference_mode():
587
  output = model.generate(
588
- inputs,
589
  attention_mask=attention_mask,
590
  max_new_tokens=max_new_tokens,
591
  do_sample=temperature > 0,
 
576
  temperature = max(0.0, min(2.0, float(temperature)))
577
  messages = [{"role": "user", "content": prompt}]
578
  if hasattr(tokenizer, "apply_chat_template"):
579
+ templated = tokenizer.apply_chat_template(
580
+ messages, add_generation_prompt=True, return_tensors="pt"
581
+ )
582
+ if isinstance(templated, dict) or hasattr(templated, "input_ids"):
583
+ inputs = templated["input_ids"]
584
+ attention_mask = templated.get("attention_mask", torch.ones_like(inputs))
585
+ else:
586
+ inputs = templated
587
+ attention_mask = torch.ones_like(inputs)
588
  else:
589
  encoded = tokenizer(prompt, return_tensors="pt")
590
  inputs = encoded["input_ids"]
 
592
  t0 = time.time()
593
  with torch.inference_mode():
594
  output = model.generate(
595
+ input_ids=inputs,
596
  attention_mask=attention_mask,
597
  max_new_tokens=max_new_tokens,
598
  do_sample=temperature > 0,