HimalayaGPT commited on
Commit
17ec36f
·
verified ·
1 Parent(s): 04f9bde

Add early-stop on special tokens + repetition penalty controls in standalone runner

Browse files
Files changed (1) hide show
  1. run_standalone_inference.py +81 -1
run_standalone_inference.py CHANGED
@@ -49,6 +49,24 @@ def parse_args() -> argparse.Namespace:
49
  p.add_argument("--temperature", type=float, default=0.8)
50
  p.add_argument("--top-k", type=int, default=50)
51
  p.add_argument("--max-new-tokens", type=int, default=96)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  p.add_argument("--seed", type=int, default=42)
53
  p.add_argument("--device", choices=["auto", "cuda", "cpu"], default="auto")
54
  p.add_argument(
@@ -160,6 +178,17 @@ def _top_k_filter(logits, top_k: int):
160
  return masked
161
 
162
 
 
 
 
 
 
 
 
 
 
 
 
163
  def _has_meta_tensors(model) -> bool:
164
  import torch
165
 
@@ -224,7 +253,43 @@ def _load_model_from_pretrained(local_dir: str, torch_dtype, device, use_device_
224
  return model
225
 
226
 
227
- def generate_compat(model, input_ids, max_new_tokens: int, temperature: float, top_k: int, seed: int):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  import torch
229
  import torch.nn.functional as F
230
 
@@ -237,6 +302,7 @@ def generate_compat(model, input_ids, max_new_tokens: int, temperature: float, t
237
  for _ in range(max_new_tokens):
238
  attention_mask = torch.ones_like(ids)
239
  logits = model(input_ids=ids, attention_mask=attention_mask, return_dict=True).logits[:, -1, :]
 
240
  logits = _top_k_filter(logits, top_k)
241
  if temperature > 0:
242
  probs = F.softmax(logits / temperature, dim=-1)
@@ -244,6 +310,10 @@ def generate_compat(model, input_ids, max_new_tokens: int, temperature: float, t
244
  else:
245
  next_ids = torch.argmax(logits, dim=-1, keepdim=True)
246
  ids = torch.cat((ids, next_ids), dim=1)
 
 
 
 
247
  return ids
248
 
249
 
@@ -300,6 +370,8 @@ def main() -> None:
300
 
301
  prompt_style = _resolve_prompt_style(args.prompt_style, tok)
302
  max_prompt_tokens = max(1, context_window - args.max_new_tokens)
 
 
303
 
304
  print(f"repo={args.repo_id} revision={args.revision} snapshot_sha={sha}")
305
  print(
@@ -307,6 +379,10 @@ def main() -> None:
307
  f"use_device_map={args.use_device_map} load_mode={args.load_mode}"
308
  )
309
  print(f"context_window={context_window} max_prompt_tokens={max_prompt_tokens}")
 
 
 
 
310
 
311
  torch.manual_seed(args.seed)
312
  if torch.cuda.is_available():
@@ -321,8 +397,12 @@ def main() -> None:
321
  model=model,
322
  input_ids=input_ids,
323
  max_new_tokens=args.max_new_tokens,
 
324
  temperature=args.temperature,
325
  top_k=args.top_k,
 
 
 
326
  seed=args.seed + i,
327
  )
328
 
 
49
  p.add_argument("--temperature", type=float, default=0.8)
50
  p.add_argument("--top-k", type=int, default=50)
51
  p.add_argument("--max-new-tokens", type=int, default=96)
52
+ p.add_argument("--min-new-tokens", type=int, default=1, help="Do not stop before this many generated tokens")
53
+ p.add_argument(
54
+ "--repetition-penalty",
55
+ type=float,
56
+ default=1.08,
57
+ help="Penalty >1.0 discourages repeats (1.0 disables).",
58
+ )
59
+ p.add_argument(
60
+ "--stop-tokens",
61
+ default="<|assistant_end|>,<|output_end|>,<|user_start|>",
62
+ help="Comma-separated special tokens that trigger early stop.",
63
+ )
64
+ p.add_argument(
65
+ "--stop-on-special",
66
+ action=argparse.BooleanOptionalAction,
67
+ default=True,
68
+ help="Enable early stop when generated token matches --stop-tokens.",
69
+ )
70
  p.add_argument("--seed", type=int, default=42)
71
  p.add_argument("--device", choices=["auto", "cuda", "cpu"], default="auto")
72
  p.add_argument(
 
178
  return masked
179
 
180
 
181
+ def _apply_repetition_penalty(logits, token_ids, penalty: float):
182
+ import torch
183
+
184
+ if penalty <= 1.0 or token_ids.numel() == 0:
185
+ return logits
186
+ uniq = torch.unique(token_ids)
187
+ penalized = logits.clone()
188
+ penalized[:, uniq] = penalized[:, uniq] / penalty
189
+ return penalized
190
+
191
+
192
  def _has_meta_tensors(model) -> bool:
193
  import torch
194
 
 
253
  return model
254
 
255
 
256
+ def _parse_stop_tokens(raw: str) -> List[str]:
257
+ out = []
258
+ for part in raw.split(","):
259
+ token = part.strip()
260
+ if token:
261
+ out.append(token)
262
+ return out
263
+
264
+
265
+ def _resolve_stop_ids(tokenizer, stop_tokens: List[str]) -> List[int]:
266
+ ids: List[int] = []
267
+ for token in stop_tokens:
268
+ tid = _special_id(tokenizer, token)
269
+ if tid is not None:
270
+ ids.append(int(tid))
271
+ # preserve order but deduplicate
272
+ seen = set()
273
+ uniq = []
274
+ for tid in ids:
275
+ if tid not in seen:
276
+ seen.add(tid)
277
+ uniq.append(tid)
278
+ return uniq
279
+
280
+
281
+ def generate_compat(
282
+ model,
283
+ input_ids,
284
+ max_new_tokens: int,
285
+ min_new_tokens: int,
286
+ temperature: float,
287
+ top_k: int,
288
+ repetition_penalty: float,
289
+ stop_ids: List[int],
290
+ stop_on_special: bool,
291
+ seed: int,
292
+ ):
293
  import torch
294
  import torch.nn.functional as F
295
 
 
302
  for _ in range(max_new_tokens):
303
  attention_mask = torch.ones_like(ids)
304
  logits = model(input_ids=ids, attention_mask=attention_mask, return_dict=True).logits[:, -1, :]
305
+ logits = _apply_repetition_penalty(logits, ids[0], penalty=repetition_penalty)
306
  logits = _top_k_filter(logits, top_k)
307
  if temperature > 0:
308
  probs = F.softmax(logits / temperature, dim=-1)
 
310
  else:
311
  next_ids = torch.argmax(logits, dim=-1, keepdim=True)
312
  ids = torch.cat((ids, next_ids), dim=1)
313
+ if stop_on_special and stop_ids and (ids.shape[1] - input_ids.shape[1]) >= max(0, min_new_tokens):
314
+ next_id = int(next_ids.item())
315
+ if next_id in stop_ids:
316
+ break
317
  return ids
318
 
319
 
 
370
 
371
  prompt_style = _resolve_prompt_style(args.prompt_style, tok)
372
  max_prompt_tokens = max(1, context_window - args.max_new_tokens)
373
+ stop_tokens = _parse_stop_tokens(args.stop_tokens)
374
+ stop_ids = _resolve_stop_ids(tok, stop_tokens) if args.stop_on_special else []
375
 
376
  print(f"repo={args.repo_id} revision={args.revision} snapshot_sha={sha}")
377
  print(
 
379
  f"use_device_map={args.use_device_map} load_mode={args.load_mode}"
380
  )
381
  print(f"context_window={context_window} max_prompt_tokens={max_prompt_tokens}")
382
+ print(
383
+ f"generation: temperature={args.temperature} top_k={args.top_k} "
384
+ f"repetition_penalty={args.repetition_penalty} stop_on_special={args.stop_on_special} stop_ids={stop_ids}"
385
+ )
386
 
387
  torch.manual_seed(args.seed)
388
  if torch.cuda.is_available():
 
397
  model=model,
398
  input_ids=input_ids,
399
  max_new_tokens=args.max_new_tokens,
400
+ min_new_tokens=args.min_new_tokens,
401
  temperature=args.temperature,
402
  top_k=args.top_k,
403
+ repetition_penalty=args.repetition_penalty,
404
+ stop_ids=stop_ids,
405
+ stop_on_special=args.stop_on_special,
406
  seed=args.seed + i,
407
  )
408