Dexter commited on
Commit
1f2c5db
·
verified ·
1 Parent(s): 2db3686

Upload mirothinker server.py

Browse files
Files changed (1) hide show
  1. mirothinker server.py +8 -3
mirothinker server.py CHANGED
@@ -69,12 +69,17 @@ def chat(req: ChatRequest):
69
  continue_final = bool(req.continue_final_message)
70
 
71
  # Build the prompt with the model's own chat template.
72
- input_ids = tokenizer.apply_chat_template(
 
 
73
  req.messages,
74
  add_generation_prompt=not continue_final,
75
  continue_final_message=continue_final,
76
  return_tensors="pt",
77
- ).to(model.device)
 
 
 
78
 
79
  prompt_tokens = input_ids.shape[-1]
80
  max_new = req.max_completion_tokens or req.max_tokens or 16384
@@ -90,7 +95,7 @@ def chat(req: ChatRequest):
90
  gen_kwargs["repetition_penalty"] = req.repetition_penalty
91
 
92
  with torch.inference_mode():
93
- out = model.generate(input_ids, **gen_kwargs)
94
 
95
  new_tokens = out[0][prompt_tokens:]
96
  text = tokenizer.decode(new_tokens, skip_special_tokens=True)
 
69
  continue_final = bool(req.continue_final_message)
70
 
71
  # Build the prompt with the model's own chat template.
72
+ # return_dict=True gives a BatchEncoding (input_ids + attention_mask);
73
+ # handle it as a dict so .shape / generate work correctly.
74
+ enc = tokenizer.apply_chat_template(
75
  req.messages,
76
  add_generation_prompt=not continue_final,
77
  continue_final_message=continue_final,
78
  return_tensors="pt",
79
+ return_dict=True,
80
+ )
81
+ enc = {k: v.to(model.device) for k, v in enc.items()}
82
+ input_ids = enc["input_ids"]
83
 
84
  prompt_tokens = input_ids.shape[-1]
85
  max_new = req.max_completion_tokens or req.max_tokens or 16384
 
95
  gen_kwargs["repetition_penalty"] = req.repetition_penalty
96
 
97
  with torch.inference_mode():
98
+ out = model.generate(**enc, **gen_kwargs)
99
 
100
  new_tokens = out[0][prompt_tokens:]
101
  text = tokenizer.decode(new_tokens, skip_special_tokens=True)