Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
75 lines (67 loc) · 3.13 KB

File metadata and controls

75 lines (67 loc) · 3.13 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
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
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2026, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# 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.
# ===----------------------------------------------------------------------=== #
"""Provides functions for applying logits processors to model output batches before sampling."""
from __future__ import annotations
from max.driver import CPU, Buffer
from max.pipelines.context import TextGenerationContextType
from max.pipelines.context.logit_processors_type import (
BatchLogitsProcessor,
BatchProcessorInputs,
ProcessorInputs,
)
def apply_logits_processors(
context_batch: list[TextGenerationContextType],
batch_logits: Buffer,
batch_logit_offsets: Buffer | None,
batch_processors: list[BatchLogitsProcessor] | None = None,
) -> None:
"""Applies logits processors to a batch of logits.
Args:
context_batch: The batch of contexts containing the inputs to the model.
batch_logits: The model logits, a float32 tensor with shape `(N_batch, vocab_size)`.
batch_logit_offsets: If the model returns multiple logits, this is a tensor with
shape `(batch_size + 1, 1)` that contains the offsets of each sequence in
the batch. Otherwise, this is `None`.
logits_processors: List of logits processors to apply to the logits for
each context in the batch. The length of this list must match the
number of contexts in the batch.
batch_processors: List of batch processors to apply to the batch logits.
These are applied in order after the individual context-level
processors.
"""
batch_logit_offsets_cpu: Buffer | None = None
for i, context in enumerate(context_batch):
processors = context.sampling_params.logits_processors
if processors is None:
continue
if batch_logit_offsets_cpu is None and batch_logit_offsets is not None:
batch_logit_offsets_cpu = batch_logit_offsets.to(CPU())
if batch_logit_offsets_cpu is not None:
start_idx = batch_logit_offsets_cpu[i].item()
end_idx = batch_logit_offsets_cpu[i + 1].item()
else:
start_idx = i
end_idx = i + 1
logits = batch_logits[start_idx:end_idx, :]
assert isinstance(logits, Buffer)
for processor in processors:
processor(ProcessorInputs(logits=logits, context=context))
if batch_processors is not None:
for batch_processor in batch_processors:
batch_processor(
BatchProcessorInputs(
logits=batch_logits,
logit_offsets=batch_logit_offsets,
context_batch=context_batch,
)
)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.