Skip to content

vllm.model_executor.models.cohere_asr

Classes:

CausalConv1D

Bases: Conv1d

A causal version of nn.Conv1d where each step would have limited access to locations on its right or left. All arguments are the same as nn.Conv1d except padding.

If padding is set None, then paddings are set automatically to make it a causal convolution where each location would not see any steps on its right.

If padding is set as a list (size of 2), then padding[0] would be used as left padding and padding[1] as right padding. It would make it possible to control the number of steps to be accessible on the right and left. This mode is not supported when stride > 1. padding[0]+padding[1] should be equal to (kernel_size - 1).

CohereASRMultiHeadAttention

Bases: Module

Multi-Head Attention layer of Transformer.

Parameters:

  • n_head

    (int) –

    number of heads

  • n_feat

    (int) –

    size of the features

  • use_bias

    (bool, default: True ) –

    whether to remove bias in linear and conv layers

Methods:

__init__(n_head, n_feat, use_bias=True)

Construct an MultiHeadedAttention object.

forward(query, key, value, mask, pos_emb=None)

Compute 'Scaled Dot Product Attention'.

Parameters:

  • query

    (Tensor) –

    (batch, time1, size)

  • key

    (Tensor) –

    (batch, time2, size)

  • value

    (Tensor) –

    (batch, time2, size)

  • mask

    (Tensor) –

    (batch, time1, time2)

  • pos_emb

    (Tensor, default: None ) –

    optional relative position embeddings

Returns:

  • output ( Tensor ) –

    transformed value (batch, time1, d_model) weighted by the query dot key attention

forward_attention(value, scores, mask)

Compute attention context vector.

Parameters:

  • value

    (Tensor) –

    (batch, time2, size)

  • scores

    (Tensor) –

    (batch, time1, time2)

  • mask

    (Tensor) –

    (batch, time1, time2)

returns: value (torch.Tensor): transformed value (batch, time2, d_model) weighted by the attention scores

forward_qkv(query, key, value)

Transforms query, key and value.

Parameters:

  • query

    (Tensor) –

    (batch, time1, size)

  • key

    (Tensor) –

    (batch, time2, size)

  • value

    (Tensor) –

    (batch, time2, size)

returns: q (torch.Tensor): (batch, head, time1, size) k (torch.Tensor): (batch, head, time2, size) v (torch.Tensor): (batch, head, time2, size)

ConformerConvolution

Bases: Module

The convolution module for the Conformer model.

Parameters:

  • d_model

    (int) –

    hidden dimension

  • kernel_size

    (int) –

    kernel size for depthwise convolution

  • pointwise_activation

    (str, default: 'glu_' ) –

    name of the activation function to be used for the pointwise conv. Note that Conformer uses a special key glu_ which is treated as the original default from the paper.

  • use_bias

    (bool, default: True ) –

    Use bias in all Linear and Conv1d layers to improve activation flow and stabilize training of huge models. Defaults to True

ConformerEncoder

Bases: Module

The encoder for ASR model of Conformer. Based on this paper: 'Conformer: Convolution-augmented Transformer for Speech Recognition' by Anmol Gulati et al. https://arxiv.org/abs/2005.08100

Methods:

set_max_audio_length(max_audio_length)

Sets maximum input length. Pre-calculates internal seq_range mask.

Parameters:

  • max_audio_length

    (int) –

    New maximum sequence length.

ConformerFeedForward

Bases: Module

feed-forward module of Conformer model. use_bias (bool): Apply bias to all Linear and Conv1d layers to improve activation flow and stabilize training of huge models.

ConformerLayer

Bases: Module

A single block of the Conformer encoder.

Parameters:

  • d_model

    (int) –

    input dimension of MultiheadAttentionMechanism and PositionwiseFeedForward

  • d_ff

    (int) –

    hidden dimension of PositionwiseFeedForward

  • self_attention_model

    (str, default: 'rel_pos' ) –

    type of the attention layer and positional encoding

  • n_heads

    (int, default: 4 ) –

    number of heads for multi-head attention

  • conv_kernel_size

    (int, default: 31 ) –

    kernel size for depthwise convolution in convolution module

  • use_bias

    (bool, default: True ) –

    Apply bias to all Linear and Conv1d layers from each ConformerLayer to improve activation flow and stabilize training of huge models. Defaults to True.

Methods:

forward(x, att_mask=None, pos_emb=None, pad_mask=None)

Parameters:

  • x

    (Tensor) –

    input signals (B, T, d_model)

  • att_mask

    (Tensor, default: None ) –

    attention masks(B, T, T)

  • pos_emb

    (Tensor, default: None ) –

    (L, 1, d_model)

  • pad_mask

    (tensor, default: None ) –

    padding mask

Returns: x (torch.Tensor): (B, T, d_model)

ConvSubsampling

Bases: Module

Methods:

  • calc_length –

    Calculates the output length of a Tensor passed

calc_length(lengths, all_paddings, kernel_size, stride, ceil_mode, repeat_num=1)

Calculates the output length of a Tensor passed through a convolution or max pooling layer

FixedPositionalEncoding

Bases: Module

Fixed positional encoding (embedding layer) from sine and cosine functions of different frequencies according to https://arxiv.org/abs/1706.03762

Parameters:

  • hidden_size

    (int) –

    size of the embeddings in the model, also known as d_model

  • max_sequence_length

    (int, default: 512 ) –

    maximum allowed length of the input sequence

_build_pos_enc(hidden_size, max_sequence_length)

Builds/replaces pre-computed positional encoding.

MaskedConvSequential

Bases: Sequential

Methods:

_create_mask(tensor, lengths)

Create broadcastable mask from per-sample lengths.

Returns a (B, 1, T, 1) mask that broadcasts over channels and features without materializing a full (B, C, T, F) tensor.

apply_channel_mask(tensor, mask)

Apply mask in-place via broadcasting.

tensor: (B, C, T, F), mask: (B, 1, T, 1)

calculate_conv_output_size(input_size, kernel_size, stride, padding)

Calculate exact output size after convolution.

PositionalEncoding

Bases: Module

Fixed sinusoidal positional encoding.

Parameters:

  • d_model

    (int) –

    embedding dim

  • max_len

    (int, default: 5000 ) –

    maximum input length

  • xscale

    (bool, default: None ) –

    whether to scale the input by sqrt(d_model)

Methods:

  • forward –

    Adds positional encoding.

forward(x, cache_len=0)

Adds positional encoding.

Parameters:

  • x

    (Tensor) –

    Input. Its shape is (batch, time, feature_size)

  • cache_len

    (int, default: 0 ) –

    the size of the cache which is used to shift positions

Returns: x+pos_emb (torch.Tensor): Its shape is (batch, time, feature_size) pos_emb (torch.Tensor): Its shape is (1, time, feature_size)

RelPositionMultiHeadAttention

Bases: CohereASRMultiHeadAttention

Multi-Head Attention layer of Transformer-XL with support of relative positional encoding. Paper: https://arxiv.org/abs/1901.02860 Args: n_head (int): number of heads n_feat (int): size of the features use_bias (bool): whether to apply bias in linear and conv layers of MultiHeadAttention

Methods:

  • __init__ –

    Construct an RelPositionMultiHeadedAttention object.

  • forward –

    Compute 'Scaled Dot Product Attention' with rel. positional encoding.

  • rel_shift –

    Compute relative positional encoding.

__init__(n_head, n_feat, pos_bias_u, pos_bias_v, use_bias=True)

Construct an RelPositionMultiHeadedAttention object.

forward(query, key, value, mask, pos_emb=None)

Compute 'Scaled Dot Product Attention' with rel. positional encoding.

Parameters:

  • query

    (Tensor) –

    (batch, time1, size)

  • key

    (Tensor) –

    (batch, time2, size)

  • value

    (Tensor) –

    (batch, time2, size)

  • mask

    (Tensor) –

    (batch, time1, time2)

  • pos_emb

    (Tensor, default: None ) –

    (batch, time1, size)

Returns:

  • output ( Tensor ) –

    transformed value (batch, time1, d_model) weighted by the query dot key attention

rel_shift(x)

Compute relative positional encoding.

Parameters:

  • x

    (Tensor) –

    (batch, nheads, time, 2*time-1)

RelPositionalEncoding

Bases: PositionalEncoding

Relative positional encoding for TransformerXL's layers See : Appendix B in https://arxiv.org/abs/1901.02860 Args: d_model (int): embedding dim max_len (int): maximum input length xscale (bool): whether to scale the input by sqrt(d_model)

Methods:

  • extend_pe –

    Reset and extend the positional encodings if needed.

  • forward –

    Compute positional encoding.

extend_pe(length, device, dtype)

Reset and extend the positional encodings if needed.

forward(x, cache_len=0)

Compute positional encoding.

Parameters:

  • x

    (Tensor) –

    Input. Its shape is (batch, time, feature_size)

  • cache_len

    (int, default: 0 ) –

    the size of the cache which is used to shift positions

Returns: x (torch.Tensor): Its shape is (batch, time, feature_size) pos_emb (torch.Tensor): Its shape is (1, time, feature_size)

Swish

Bases: SiLU

Swish activation function introduced in 'https://arxiv.org/abs/1710.05941' Mathematically identical to SiLU. See note in nn.SiLU for references.