1 file changed, +42, -1
8a97966Add extendable function in decoder
author
mogi <git@paramogi.world> on 06-08-2026 13:04 UTC+1
committer
mogi <git@paramogi.world> on 06-08-2026 13:04 UTC+1
parent844fe0a
+42, -1
      @@ -77,6 +77,13 @@ class ConstrainedDecoder(BaseModel):
 77 77     def generate(self, prompt_ids: Sequence[int], constraint: Constraint) -> bytes:
 78 78         """Generate the bytes of one complete, constraint-satisfying answer.
 79 79 
    80+        A finished structure is not always a finished answer: when one function
    81+        name is a prefix of another, ``fn_get`` is complete and ``fn_get_square_root``
    82+        is still reachable, and stopping at the first would silently hide the
    83+        second. So where the structure could still grow, whether it does is the
    84+        model's call, taken the same way every other one is — by asking what it
    85+        wants to say next and stopping unless that is a legal continuation.
    86+
 80 87         Args:
 81 88             prompt_ids: Token ids of the context to continue.
 82 89             constraint: Structure the answer must have.
      @@ -93,10 +100,13 @@ class ConstrainedDecoder(BaseModel):
 93100         emitted = bytearray()
 94101 
 95102         for step in range(self.max_new_tokens):
 96   -            if constraint.is_complete(state):
   103+            if constraint.is_complete(state) and not self._extendable(constraint, state):
 97104                 return bytes(emitted)
 98105 
 99106             logits = np.asarray(self.llm.get_logits_from_input_ids(context), dtype=np.float64)
   107+            if constraint.is_complete(state) and not self._wants_more(logits, constraint, state):
   108+                return bytes(emitted)
   109+
100110             token_id = self._select_token(logits, constraint, state, step)
101111             if token_id is None:
102112                 raise DecoderError("no vocabulary token can continue the required structure")
      @@ -114,6 +124,37 @@ class ConstrainedDecoder(BaseModel):
114124             raise DecoderError(f"structure still unfinished after {self.max_new_tokens} tokens")
115125         return bytes(emitted)
116126 
   127+    def _extendable(self, constraint: Constraint, state: Any) -> bool:
   128+        """Tell whether a complete structure could still legally grow.
   129+
   130+        Args:
   131+            constraint: Structure being enforced.
   132+            state: Current constraint state.
   133+
   134+        Returns:
   135+            ``True`` if any byte at all may still be appended.
   136+        """
   137+        return any(constraint.advance(state, byte) is not None for byte in range(_BYTE_VALUES))
   138+
   139+    def _wants_more(self, logits: NDArray[np.float64], constraint: Constraint, state: Any) -> bool:
   140+        """Tell whether the model would rather keep going than stop here.
   141+
   142+        The test is the token it ranks first out of the whole vocabulary, before
   143+        any masking. A model with more to say puts a legal continuation there; a
   144+        model that considers the answer finished puts an end-of-turn marker
   145+        there, and those emit no text, so they can never continue anything.
   146+
   147+        Args:
   148+            logits: Raw next-token scores.
   149+            constraint: Structure being enforced.
   150+            state: Current constraint state.
   151+
   152+        Returns:
   153+            ``True`` if the model's favourite token extends the structure.
   154+        """
   155+        piece = self.tokenizer.token_bytes(int(np.argmax(logits)))
   156+        return bool(piece) and consume(constraint, state, piece) is not None
   157+
117158     def _select_token(
118159         self,
119160         logits: NDArray[np.float64],