processors
Processors and guides to control generation in steerable models.
base_logits_processor
Base class for logits processors.
OutlinesLogitsProcessor
Base class for logits processors.
This class implements a shared __call__
method is called by the models
and returns the processed logits. It relies on the process_logits
method
that must be implemented by the subclasses to do the actual processing. The
tensor_adapter
attribute, created at initialization based on the
tensor library name specified in the constructor, is used to manipulate the
tensors using the appropriate library for the model (numpy, torch...).
Source code in outlines/processors/base_logits_processor.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
__call__(input_ids, logits)
Entrypoint for logits processors, this is the method that is
called by the model.
Because different models use different structures to store the
input_ids and logits, we standardize their format to 2D tensors
before calling the process_logits
method. After processing, the
logits are cast back to the original array library type before being
returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
TensorType
|
The ids of the tokens of the existing sequences in a tensor. |
required |
logits
|
TensorType
|
The logits for the current generation step in a tensor. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The processed logits as a tensor. |
Source code in outlines/processors/base_logits_processor.py
__init__(tensor_library_name)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor_library_name
|
str
|
The name of the library to use to manipulate tensors. Possible values are "jax", "mlx", "numpy", "tensorflow" and "torch". You must choose the library that your model is using. |
required |
Source code in outlines/processors/base_logits_processor.py
process_logits(input_ids, logits)
abstractmethod
Main method to implement for logits processors subclasses.
This method applies a mask on the logits to bias the generation.
It is called by the __call__
method that standardizes the shape of
input_ids
and logits
to ensure they are 2D tensors.
Elements to keep in mind when designing universal logits processors:
- logits processors are only used once and never re-applied for a new
sequence generator
- Some models only pass output_ids, some models such as llamacpp and
transformers prefix with input_ids
- Some sampling methods, such as beam search, result in unstable
sequence ordering in models like vLLM
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
TensorType
|
The ids of the tokens of the existing sequences in a 2D tensor. |
required |
logits
|
TensorType
|
The logits for the current generation step in a 2D tensor. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The processed logits as a 2D tensor. |
Source code in outlines/processors/base_logits_processor.py
guide
Guides to control generation in steerable models.
Logits processors rely on guides to control the generation process.
CFGGuide
Bases: Guide
Guide to generate text that is in the language of a context-free Lark grammar.
Source code in outlines/processors/guide.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
|
__init__(cfg_string, tokenizer)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg_string
|
str
|
The context-free grammar to generate text from. |
required |
tokenizer
|
Tokenizer
|
The tokenizer to use to convert tokens to ids. |
required |
Source code in outlines/processors/guide.py
can_terminate_state(state)
Return whether generation is allowed to terminate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
CFGState
|
The guide's current state. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether generation is allowed to terminate. |
Source code in outlines/processors/guide.py
copy()
get_next_instruction(state)
Return the next instruction for guided generation.
Current lazy approach: - For each token in the vocabulary - create a copy of the parsers state - add the tokens to the parsers input text - if valid, add token to returned tokens
Further refinements are necessary for performant text processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
CFGState
|
The guides current PartialParserState, or None if complete |
required |
Returns:
Type | Description |
---|---|
Instruction
|
A |
Source code in outlines/processors/guide.py
get_next_state(state, token_id)
Update the state of the guide.
Decode the token_id, and calculate the new parser_state with the token applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
CFGState
|
The guides current PartialParserState, or None if complete |
required |
token_id
|
int
|
The id of the token that was just generated. |
required |
Returns:
Type | Description |
---|---|
CFGState
|
The guides new PartialParserState |
Source code in outlines/processors/guide.py
is_final_state(state)
Return whether the given state is a final state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
CFGState
|
The guide's current state. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the given state is a final state. |
Source code in outlines/processors/guide.py
iter_valid_token_ids(state, candidate_token_ids)
Iterate over the given token_ids and yield those that are valid for the current parser state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser_state
|
The current state of the parser, or None if complete. |
required | |
token_ids
|
The list of token ids to check for validity. |
required |
Yields:
Type | Description |
---|---|
int
|
Valid token ids. |
Source code in outlines/processors/guide.py
must_terminate_state(state)
Indicate whether generation must terminate as there are no legal continuations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
CFGState
|
The guide's current state. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether generation must terminate. |
Source code in outlines/processors/guide.py
Guide
Bases: Guide
Base definition of a generation guide.
A generation guide defines the behavior of a finite-state machine that
guides a text generation procedure. Unlike the DFAs built from regular
expressions guides, it can also emit a Write
instructions which tells
the model that it can append a sequence of tokens (or token word) instead
of generating it.
Source code in outlines/processors/guide.py
RegexGuide
Bases: RegexGuide
Guide to generate text in the language of a regular expression.
This class is a wrapper around the CoreRegexGuide class that adds a cache to the create_states_mapping function.
Source code in outlines/processors/guide.py
from_regex(regex_string, tokenizer, **kwargs)
classmethod
Create a RegexGuide from a regular expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regex_string
|
str
|
The regular expression to generate text from. |
required |
tokenizer
|
The tokenizer to use to convert tokens to ids. |
required | |
kwargs
|
Additional keyword arguments to pass to the CoreRegexGuide constructor. |
{}
|
Returns:
Type | Description |
---|---|
RegexGuide
|
A RegexGuide instance. |
Source code in outlines/processors/guide.py
StopAtEOSGuide
Bases: Guide
Guide to generate tokens until the EOS token has been generated.
Source code in outlines/processors/guide.py
__init__(tokenizer)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer
|
Tokenizer
|
The tokenizer used to convert tokens to ids. |
required |
copy()
get_next_instruction(state)
Return the next instruction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
int
|
The guide's current state. |
required |
Returns:
Type | Description |
---|---|
Instruction
|
An |
Source code in outlines/processors/guide.py
get_next_state(state, token_id)
Return the next state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
int
|
The guide's current state. |
required |
token_id
|
int
|
The id of the token that was just generated. |
required |
Returns:
Type | Description |
---|---|
int
|
The next state. |
Source code in outlines/processors/guide.py
is_final_state(state)
Return whether the given state is a final state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
int
|
The guide's current state. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the given state is a final state. |
Source code in outlines/processors/guide.py
cached_create_states_mapping(regex_string, tokenizer, *args, **kwargs)
Wrap the uncached create_states_mapping function in a cache.
Source code in outlines/processors/guide.py
structured
Logits processors for structured generation.
/ Don't want to self-host? \ Try .json at http://dottxt.co /
\ ^__^
\ (oo)\_______
(__)\ )\/ ||----w |
|| ||
Copyright 2024- the Outlines developers
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
CFGLogitsProcessor
Bases: GuideLogitsProcessor
Bias generation based on a context-free grammar.
Source code in outlines/processors/structured.py
__init__(cfg_str, tokenizer, tensor_library_name)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cfg_str
|
str
|
A string that represents a grammar. |
required |
tokenizer
|
Tokenizer
|
The tokenizer used to convert tokens to ids. |
required |
tensor_library_name
|
str
|
The name of the library to use to manipulate the tensors. |
required |
Source code in outlines/processors/structured.py
process_logits(input_ids, logits)
Same behavior as GuideLogitsProcessor, but uses rejection sampling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
TensorType
|
The ids of the tokens of the existing sequences. |
required |
logits
|
TensorType
|
The logits for the current generation step. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The biased logits. |
Source code in outlines/processors/structured.py
GuideLogitsProcessor
Bases: OutlinesLogitsProcessor
Bias generation using a guide.
Attributes:
Name | Type | Description |
---|---|---|
tokenizer |
Tokenizer
|
The outlines tokenizer used to convert tokens to ids. |
guide |
Guide
|
The outlines guide used to bias the logits. |
Source code in outlines/processors/structured.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
__init__(tokenizer, guide, tensor_library_name)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tokenizer
|
Tokenizer
|
The tokenizer used to convert tokens to ids. |
required |
guide
|
Guide
|
The |
required |
tensor_library_name
|
str
|
The name of the library to use to manipulate the tensors. |
required |
Source code in outlines/processors/structured.py
copy()
Return a copy of the logits processor.
process_logits(input_ids, logits)
Use the Guide to bias the logits before sampling the next token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ids
|
TensorType
|
The ids of the tokens of the existing sequences. |
required |
logits
|
TensorType
|
The logits for the current generation step. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The biased logits. |
Source code in outlines/processors/structured.py
JSONLogitsProcessor
Bases: RegexLogitsProcessor
Bias generation based on a JSON schema.
Source code in outlines/processors/structured.py
__init__(schema, tokenizer, tensor_library_name, whitespace_pattern=None)
Parameters
Parameters
schema
A JSON schema that encodes the structure we want the model to generate.
tokenizer
The tokenizer used to convert tokens to ids.
tensor_library_name
The name of the library to use to manipulate the tensors.
whitespace_pattern
Pattern to use for JSON syntactic whitespace (doesn't impact string
literals). For example, to allow only a single space or newline with
`whitespace_pattern=r"[
]?"`.
Source code in outlines/processors/structured.py
RegexLogitsProcessor
Bases: GuideLogitsProcessor
Bias generation based on a regular expression.
Source code in outlines/processors/structured.py
__init__(regex_string, tokenizer, tensor_library_name)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
regex_string
|
str
|
A string that represents a regular expression. |
required |
tokenizer
|
Tokenizer
|
An Outlines tokenizer. |
required |
tensor_library_name
|
str
|
The name of the library to use to manipulate the tensors. |
required |
Source code in outlines/processors/structured.py
tensor_adapters
Library specific objects to manipulate tensors.
base
Base class for tensor adapters.
TensorAdapter
Bases: ABC
Abstract base class for tensor adapters.
This class defines the interface for tensor adapters that are used to
manipulate tensors in different libraries. Concrete implementations of
this class should provide specific implementations for each method as
well as providing a library_name
attribute.
TODO: Update the version of outlines-core used to receive plain arrays
instead of torch tensors. In the meantime, implementations of this class
must make sure that their full_like
and concatenate
methods can
handle torch tensors.
Source code in outlines/processors/tensor_adapters/base.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
apply_mask(tensor, mask, value)
abstractmethod
Fill the elements of the tensor where the mask is True with the specified value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to fill. |
required |
mask
|
TensorType
|
The mask to apply to the tensor. |
required |
value
|
Any
|
The value to fill the tensor with. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor with the mask applied. |
Source code in outlines/processors/tensor_adapters/base.py
argsort_descending(tensor)
abstractmethod
Return the indices that would sort the tensor in descending order along axis -1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to sort. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The indices that would sort the tensor in descending order along axis -1. |
Source code in outlines/processors/tensor_adapters/base.py
boolean_ones_like(tensor)
abstractmethod
Create a boolean ones tensor with the same shape as the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to create a boolean ones tensor with the same shape. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
A boolean ones tensor with the same shape as the input tensor. |
Source code in outlines/processors/tensor_adapters/base.py
concatenate(tensors)
abstractmethod
Concatenate a list of tensors along axis 0.
ATTENTION: This method can either receive a list of torch tensors or a list of tensors from the library used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensors
|
list[Union[Tensor, TensorType]]
|
The list of tensors to concatenate. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The concatenated tensor. |
Source code in outlines/processors/tensor_adapters/base.py
full_like(tensor, fill_value)
abstractmethod
Create a tensor with the same shape as the input tensor filled with a scalar value.
ATTENTION: This method receives a torch tensor regardless of the library used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
Tensor
|
The tensor to create a new tensor with the same shape. |
required |
fill_value
|
Any
|
The value to fill the new tensor with. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
A tensor with the same shape as the input tensor filled with the specified value. |
Source code in outlines/processors/tensor_adapters/base.py
get_device(tensor)
abstractmethod
Get the name of the tensor's device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to get the device of. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the tensor's device. |
Source code in outlines/processors/tensor_adapters/base.py
shape(tensor)
abstractmethod
Get the shape of the tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to get the shape of. |
required |
Returns:
Type | Description |
---|---|
list[int]
|
The shape of the tensor. The list contains as many elements as there are dimensions in the tensor. |
Source code in outlines/processors/tensor_adapters/base.py
squeeze(tensor)
abstractmethod
Remove a dimension from the tensor at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to remove a dimension from. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor with one less dimension. |
Source code in outlines/processors/tensor_adapters/base.py
to_device(tensor, device)
abstractmethod
Move the tensor to a specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to move to a specified device. |
required |
device
|
str
|
The name of the device to move the tensor to. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor moved to the specified device. |
Source code in outlines/processors/tensor_adapters/base.py
to_list(tensor)
abstractmethod
Convert the tensor to a list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to convert to a list. |
required |
Returns:
Type | Description |
---|---|
list
|
The tensor as a list. |
Source code in outlines/processors/tensor_adapters/base.py
to_scalar(tensor)
abstractmethod
Return the only element of the tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to return the only element of. |
required |
Returns:
Type | Description |
---|---|
Any
|
The only element of the tensor. |
Source code in outlines/processors/tensor_adapters/base.py
unsqueeze(tensor)
abstractmethod
Add a dimension to the tensor at axis 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
TensorType
|
The tensor to add a dimension to. |
required |
Returns:
Type | Description |
---|---|
TensorType
|
The tensor with an additional dimension. |
Source code in outlines/processors/tensor_adapters/base.py
jax
Tensor adapter for the jax
library.
mlx
Tensor adapter for the mlx
library.
numpy
Tensor adapter for the numpy
library.
tensorflow
Tensor adapter for the tensorflow
library.
torch
Tensor adapter for the torch
library.