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

Function _beam_init

axlearn/common/decoding.py:373–429  ·  view source on GitHub ↗

Initializes the beam search state data structure. Args: inputs: An int tensor of shape [batch_size, length] where length <= max_decode_length. time_step: Initial time steps for decoding of shape [batch_size]. batch_size: Size of batch. beam_size: Number of hypoth

(
    *,
    inputs: Tensor,
    time_step: Tensor,
    beam_size: int,
    max_decode_len: int,
    cache: NestedTensor,
    pad_id: int,
    prefix_merger: Optional[PrefixMerger] = None,
)

Source from the content-addressed store, hash-verified

371
372
373def _beam_init(
374 *,
375 inputs: Tensor,
376 time_step: Tensor,
377 beam_size: int,
378 max_decode_len: int,
379 cache: NestedTensor,
380 pad_id: int,
381 prefix_merger: Optional[PrefixMerger] = None,
382) -> _BeamState:
383 """Initializes the beam search state data structure.
384
385 Args:
386 inputs: An int tensor of shape [batch_size, length] where length <= max_decode_length.
387 time_step: Initial time steps for decoding of shape [batch_size].
388 batch_size: Size of batch.
389 beam_size: Number of hypotheses per beam (aka num_decodes).
390 max_decode_len: The maximum length of the sequence to be generated.
391 cache: State of the decoder model.
392 pad_id: Token ID associated with padded input.
393 prefix_merger: Optional prefix merger.
394
395 Returns:
396 Fully initialized _BeamState.
397
398 Raises:
399 ValueError: If inputs has an invalid shape.
400 """
401 if inputs.shape[0] != time_step.shape[0]:
402 raise ValueError(
403 f"Expected inputs.shape[0] ({inputs.shape[0]}) "
404 f"== time_step.shape[0] ({time_step.shape[0]})."
405 )
406 if inputs.shape[1] > max_decode_len:
407 raise ValueError(
408 f"Expected inputs.shape[1] ({inputs.shape[1]}) <= max_decode_len ({max_decode_len})."
409 )
410 batch_size = inputs.shape[0]
411 live_seqs = jnp.full((batch_size, beam_size, max_decode_len), pad_id, dtype=jnp.int32)
412 # Inputs are the prefix we will use for teacher forcing.
413 live_seqs = live_seqs.at[:, :, : inputs.shape[1]].set(inputs[:, None, :])
414
415 prefix_merger_state = None
416 if prefix_merger is not None:
417 prefix_merger_state = prefix_merger.init_state(tokens=live_seqs)
418
419 return _BeamState(
420 cur_index=time_step,
421 # Handle first time step by masking out scores of all but the top hypothesis in the beam.
422 live_scores=jnp.tile(jnp.array([0.0] + [NEG_INF] * (beam_size - 1)), [batch_size, 1]),
423 finished_scores=jnp.ones((batch_size, beam_size)) * NEG_INF,
424 live_seqs=live_seqs,
425 finished_seqs=jnp.zeros((batch_size, beam_size, max_decode_len), jnp.int32),
426 # Expand cache to num_decodes size.
427 cache=vectorized_tree_map(lambda x: add_decoding_dim(x, beam_size), cache),
428 prefix_merger=prefix_merger_state,
429 )
430

Callers 1

beam_search_decodeFunction · 0.85

Calls 5

vectorized_tree_mapFunction · 0.90
_BeamStateClass · 0.85
add_decoding_dimFunction · 0.85
setMethod · 0.45
init_stateMethod · 0.45

Tested by

no test coverage detected