MCPcopy Create free account
hub / github.com/apple/axlearn / sample_decode

Function sample_decode

axlearn/common/decoding.py:978–1151  ·  view source on GitHub ↗

Performs sampling decoding. Args: inputs: An int32 Tensor of shape [batch_size, length] containing a sequence of tokens. Please refer to `beam_search_decode` for more information on `inputs`. time_step: Initial time steps for decoding of shape [batch_size].

(
    *,
    inputs: Tensor,
    time_step: Tensor,
    cache: NestedTensor,
    tokens_to_scores: Callable[[Tensor, NestedTensor], tuple[Tensor, NestedTensor]],
    stop_decoding_condition: StopDecodingCondition,
    num_decodes: int,
    prng_key: Tensor,
    max_decode_len: Optional[int] = None,
    loop: Literal["lax", "python"] = "lax",
    pad_id: int = 0,
    input_token_scores: Optional[Tensor] = None,
)

Source from the content-addressed store, hash-verified

976
977
978def sample_decode(
979 *,
980 inputs: Tensor,
981 time_step: Tensor,
982 cache: NestedTensor,
983 tokens_to_scores: Callable[[Tensor, NestedTensor], tuple[Tensor, NestedTensor]],
984 stop_decoding_condition: StopDecodingCondition,
985 num_decodes: int,
986 prng_key: Tensor,
987 max_decode_len: Optional[int] = None,
988 loop: Literal["lax", "python"] = "lax",
989 pad_id: int = 0,
990 input_token_scores: Optional[Tensor] = None,
991) -> SampleOutputs:
992 """Performs sampling decoding.
993
994 Args:
995 inputs: An int32 Tensor of shape [batch_size, length] containing a sequence of tokens.
996 Please refer to `beam_search_decode` for more information on `inputs`.
997 time_step: Initial time steps for decoding of shape [batch_size].
998 Please refer to `beam_search_decode` for more information on `time_step`.
999 cache: State of the decoder model.
1000 tokens_to_scores: Fast autoregressive decoder function taking single token
1001 slices and cache and returning next-token scores and updated cache.
1002 [batch*num_decodes, vocab], {} = tokens_to_scores([batch*num_decodes, 1], {}).
1003 NestedTensor usually has batch*num_decodes as the leading dim.
1004 The scores represents logits for sampling the next token, i.e.,
1005 the sampling probabilities will be softmax(scores, axis=-1).
1006 The caller can implement temperature-based, top-k, or top-p sampling as part
1007 of `tokens_to_scores`.
1008 stop_decoding_condition: StopDecodingCondition instance which given index, current sequences
1009 and prompt mask returns a bool tensor indicating if a sequence is complete.
1010 num_decodes: Number of decoded sequences to be returned for each input sequence.
1011 prng_key: The random key.
1012 max_decode_len: An optional maximum length of decoded sequence. If
1013 None, it uses `inputs.shape[1]` as `max_decode_len`.
1014 loop: "lax" or "python". The latter should only be used for debugging.
1015 pad_id: Token ID associated with padded input.
1016 input_token_scores: Optional initial scores of shape [batch_size, length] where
1017 length < max_decode_len, e.g. as produced by prefilling. Note that length should be
1018 strictly less than max_decode_len, as we exclude the scores for the dummy prompt token.
1019 In other words, input_token_scores[i, j - 1] represents the score for inputs[i, j],
1020 since the token at inputs[i, 0] does not have a score. Defaults to all zeros.
1021
1022 Returns:
1023 SampleOutputs, containing the finished or live sequences and their corresponding scores.
1024
1025 Raises:
1026 NotImplementedError: If an unsupported loop is provided.
1027 """
1028 batch_size = inputs.shape[0]
1029 if max_decode_len is None:
1030 max_decode_len = inputs.shape[1]
1031 # Account for conditioning input token.
1032 max_decode_len += 1
1033
1034 # Initialize state.
1035 sample_decode_init_state = _decode_init(

Callers 2

sample_decodeMethod · 0.90
sample_decodeMethod · 0.90

Calls 4

_decode_initFunction · 0.85
SampleOutputsClass · 0.85

Tested by

no test coverage detected