Skip to content

sequence

Functions:

Name Description
build_attention_mask

Build an attention mask for the given sequences.

pad_sequence

Pack a sequence of variable length tensors into a single tensor, padding them to the same length on the specified side.

build_attention_mask

build_attention_mask(
    sequences: list[Tensor] | tuple[Tensor, ...],
    padding_side: Literal["left", "right"] | str = "right",
    pad_to_multiple_of: int | None = None,
    max_length: int | None = None,
) -> torch.Tensor

Build an attention mask for the given sequences.

Parameters:

Name Type Description Default

sequences

list[Tensor] | tuple[Tensor, ...]

A list or tuple of 1D tensors of shape (sequence_length,).

required

padding_side

Literal['left', 'right'] | str

The side to pad the inputs. Either 'left' or 'right'.

'right'

pad_to_multiple_of

int | None

The multiple length to pad the sequences to.

None

max_length

int | None

The maximum length after which sequences will be truncated.

None

Raises:

Type Description
ValueError

If padding_side is not one of 'left' or 'right'.

Returns:

Type Description
torch.Tensor

A 2D boolean attention mask tensor of shape (batch_size, max_seq_length).

Added in version 0.84.0.

pad_sequence

pad_sequence(
    sequences: list[Tensor] | tuple[Tensor, ...],
    padding_value: float = 0.0,
    padding_side: Literal["left", "right"] | str = "right",
    pad_to_multiple_of: int | None = None,
    truncation_side: Literal["left", "right"]
    | str = "right",
    max_length: int | None = None,
) -> torch.Tensor

Pack a sequence of variable length tensors into a single tensor, padding them to the same length on the specified side.

Parameters:

Name Type Description Default

sequences

list[Tensor] | tuple[Tensor, ...]

A list or tuple 1D tensors of shape (sequence_length,).

required

padding_value

float

The value to use for padding.

0.0

padding_side

Literal['left', 'right'] | str

The side on which to pad the sequences. Either 'left' or 'right'.

'right'

pad_to_multiple_of

int | None

The multiple length to pad the sequences to.

None

truncation_side

Literal['left', 'right'] | str

The side to truncate the sequences if max_length is specified. Either 'left' or 'right'.

'right'

max_length

int | None

The maximum length after which sequences will be truncated.

None

Raises:

Type Description
ValueError

If padding_side is not one of 'left' or 'right'.

ValueError

If truncation_side is not one of 'left' or 'right'.

ValueError

If max_length is not a multiple of pad_to_multiple_of.

Returns:

Type Description
torch.Tensor

A 2D tensor of shape (len(sequences), max(len(seq) for seq in sequences)), where the sequences are truncated to max_length if

torch.Tensor

provided, and padded to pad_to_multiple_of if provided, with padding_value on padding_side.

Added in version 0.84.0.