khtsly commited on
Commit
d0e76bf
·
verified ·
1 Parent(s): 01f6f11

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. luau_infer.py +17 -1
  2. modeling_luau_coder.py +13 -10
luau_infer.py CHANGED
@@ -626,6 +626,19 @@ class Engine:
626
  return st["res"]
627
 
628
 
 
 
 
 
 
 
 
 
 
 
 
 
 
629
  class _Lookup:
630
  """Host mirror of the engine's draft for one row: estimates whether speculation pays."""
631
 
@@ -701,7 +714,8 @@ class FusedDecoder:
701
  def decode(self, input_ids: Tensor, attention_mask: Tensor | None, *, max_new_tokens: int,
702
  sample: bool = False, temperature: float = 1.0, top_k: int = 0, top_p: float = 1.0,
703
  min_p: float = 0.0, repetition_penalty: float = 1.0, num_samples: int = 1, eos=None,
704
- pad: int | None = None, stop=(), tokenizer=None, streamer=None, stopping_criteria=None) -> Tensor:
 
705
  """Prompt + completion ids ``(B * num_samples, T0 + n)``, ``pad`` after a row's end."""
706
  eng = self.engine
707
  if attention_mask is None:
@@ -725,6 +739,8 @@ class FusedDecoder:
725
  eng.allocate(B, max(T0 + n_new + (self.DEPTH + 3) * (K + 1), self.MIN_CACHE if B <= 8 else 0))
726
  eng.set_eos(eos_set)
727
  first = eng.prefill(input_ids, attention_mask, sp)
 
 
728
  crit_ids = None
729
  if stopping_criteria:
730
  crit_ids = torch.full((B, T0 + n_new + K + 1), pad, dtype=input_ids.dtype, device=input_ids.device)
 
626
  return st["res"]
627
 
628
 
629
+ class Unfusable(Exception):
630
+ """Settings the fused decoder can't reproduce, found after the prompt pass."""
631
+
632
+
633
+ def _observes_only(processors, input_ids: Tensor, logits: Tensor, first: Tensor) -> bool:
634
+ """True if ``processors`` leave the scores unchanged, at the prompt and with generated tokens."""
635
+ fake = torch.cat((input_ids, first[:, None].expand(-1, 8)), 1)
636
+ for ids in (input_ids, fake):
637
+ if not torch.equal(processors(ids, logits.clone()), logits):
638
+ return False
639
+ return True
640
+
641
+
642
  class _Lookup:
643
  """Host mirror of the engine's draft for one row: estimates whether speculation pays."""
644
 
 
714
  def decode(self, input_ids: Tensor, attention_mask: Tensor | None, *, max_new_tokens: int,
715
  sample: bool = False, temperature: float = 1.0, top_k: int = 0, top_p: float = 1.0,
716
  min_p: float = 0.0, repetition_penalty: float = 1.0, num_samples: int = 1, eos=None,
717
+ pad: int | None = None, stop=(), tokenizer=None, streamer=None, stopping_criteria=None,
718
+ logits_processor=None) -> Tensor:
719
  """Prompt + completion ids ``(B * num_samples, T0 + n)``, ``pad`` after a row's end."""
720
  eng = self.engine
721
  if attention_mask is None:
 
739
  eng.allocate(B, max(T0 + n_new + (self.DEPTH + 3) * (K + 1), self.MIN_CACHE if B <= 8 else 0))
740
  eng.set_eos(eos_set)
741
  first = eng.prefill(input_ids, attention_mask, sp)
742
+ if logits_processor and not _observes_only(logits_processor, input_ids, eng.last_logits, first):
743
+ raise Unfusable("logits processors that change the scores")
744
  crit_ids = None
745
  if stopping_criteria:
746
  crit_ids = torch.full((B, T0 + n_new + K + 1), pad, dtype=input_ids.dtype, device=input_ids.device)
modeling_luau_coder.py CHANGED
@@ -1426,8 +1426,6 @@ def _fused_fallback_reason(model, inputs, gc, model_kwargs, logits_processor, st
1426
  used = [k for k, v in extra.items() if v is not None and v is not False]
1427
  if used:
1428
  return f"unsupported generate() arguments {used}"
1429
- if logits_processor:
1430
- return "custom logits processors"
1431
  if set(model_kwargs) - _FUSED_MODEL_KWARGS:
1432
  return f"model kwargs {sorted(set(model_kwargs) - _FUSED_MODEL_KWARGS)}"
1433
  for name, ok in _FUSED_NEUTRAL.items():
@@ -1496,15 +1494,20 @@ class _FusedGenerationMixin:
1496
  n_new = gc.max_new_tokens if gc.max_new_tokens is not None else (gc.max_length or 20) - inputs.shape[1]
1497
  stop = gc.stop_strings or []
1498
  stop = [stop] if isinstance(stop, str) else list(stop)
 
1499
  decoder = self._fused_decoder(spec)
1500
- with torch.inference_mode():
1501
- return decoder.decode(
1502
- inputs.to(self.device), mask.to(self.device), max_new_tokens=max(0, int(n_new)),
1503
- sample=bool(gc.do_sample), temperature=gc.temperature or 1.0, top_k=gc.top_k or 0,
1504
- top_p=1.0 if gc.top_p is None else gc.top_p, min_p=gc.min_p or 0.0,
1505
- repetition_penalty=gc.repetition_penalty or 1.0, num_samples=gc.num_return_sequences or 1,
1506
- eos=eos, pad=pad, stop=stop, tokenizer=model_kwargs.get("tokenizer"), streamer=streamer,
1507
- stopping_criteria=stopping_criteria or None)
 
 
 
 
1508
 
1509
  def _fused_decoder(self, spec: int):
1510
  """The decoder of these weights, kept between calls (rebuilt when a parameter changed)."""
 
1426
  used = [k for k, v in extra.items() if v is not None and v is not False]
1427
  if used:
1428
  return f"unsupported generate() arguments {used}"
 
 
1429
  if set(model_kwargs) - _FUSED_MODEL_KWARGS:
1430
  return f"model kwargs {sorted(set(model_kwargs) - _FUSED_MODEL_KWARGS)}"
1431
  for name, ok in _FUSED_NEUTRAL.items():
 
1494
  n_new = gc.max_new_tokens if gc.max_new_tokens is not None else (gc.max_length or 20) - inputs.shape[1]
1495
  stop = gc.stop_strings or []
1496
  stop = [stop] if isinstance(stop, str) else list(stop)
1497
+ from .luau_infer import Unfusable
1498
  decoder = self._fused_decoder(spec)
1499
+ try:
1500
+ with torch.inference_mode():
1501
+ return decoder.decode(
1502
+ inputs.to(self.device), mask.to(self.device), max_new_tokens=max(0, int(n_new)),
1503
+ sample=bool(gc.do_sample), temperature=gc.temperature or 1.0, top_k=gc.top_k or 0,
1504
+ top_p=1.0 if gc.top_p is None else gc.top_p, min_p=gc.min_p or 0.0,
1505
+ repetition_penalty=gc.repetition_penalty or 1.0, num_samples=gc.num_return_sequences or 1,
1506
+ eos=eos, pad=pad, stop=stop, tokenizer=model_kwargs.get("tokenizer"), streamer=streamer,
1507
+ stopping_criteria=stopping_criteria or None, logits_processor=logits_processor or None)
1508
+ except Unfusable as e:
1509
+ self._fused_last_reason = str(e)
1510
+ return None
1511
 
1512
  def _fused_decoder(self, spec: int):
1513
  """The decoder of these weights, kept between calls (rebuilt when a parameter changed)."""