Skip to content
fn

shift_tokens_right

→Tensor
shift_tokens_right(input_ids: Tensor, pad_token_id: int | None, decoder_start_token_id: int)

Shift token ids one place to the right, to make the decoder inputs of labels.

The first column becomes decoder_start_token_id, every other column is the one before it in input_ids, and the -100 that marks a position to ignore in a label is replaced by pad_token_id. input_ids is not changed.

Parameters

input_idsTensor
Token ids of shape (batch_size, sequence_length).
pad_token_idint
The id of the padding token.
decoder_start_token_idint
The id the shifted sequences start with.

Returns

Tensor

The shifted ids, of the same shape and dtype.

Raises

ValueError
pad_token_id is None, or input_ids is not two-dimensional.

Examples

>>> import lucid
>>> from lucid.peft import shift_tokens_right
>>> labels = lucid.tensor([[5, 6, -100]])
>>> shift_tokens_right(labels, pad_token_id=0, decoder_start_token_id=2)
tensor([[2, 5, 6]], dtype=lucid.int64)
>>> shift_tokens_right(lucid.tensor([[-100, 7]]), pad_token_id=0, decoder_start_token_id=2)
tensor([[2, 0]], dtype=lucid.int64)

Used by 1