ls_mlkit.model.decoder_tf.causal_transformer module

class ls_mlkit.model.decoder_tf.causal_transformer.AttentionBlock(embed_dim, num_heads, dropout=0, batch_first=True)[source]

Bases: Module

forward(x, att_mask=None, key_padding_mask=None, need_weights=True, average_attn_weights=True, is_causal=True, use_cache=False, past_key_values=None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ls_mlkit.model.decoder_tf.causal_transformer.CausalLanguageModel(vocab_size, embed_dim, num_head, dropout=0, num_block=3, max_pos_len=5000, batch_first=True)[source]

Bases: Module

forward(x: Tensor, att_mask: Tensor = None, key_padding_mask: Tensor = None, need_weights: bool = True, average_attn_weights: bool = True, use_cache: bool = False, past_key_values: Tensor = None, is_causal: bool = True, need_hidden_states: bool = False)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

generate_square_subsequent_mask(sz: int, device=None, dtype=None)[source]

Generate a square causal mask for the sequence.

The masked positions are filled with ‘True’. Unmasked positions are filled with False

class ls_mlkit.model.decoder_tf.causal_transformer.CausalLanguageModelConfig(vocab_size=32000, embed_dim=1024, num_head=2, dropout=0, num_block=3, max_pos_len=5000, batch_first=True, **kwargs)[source]

Bases: object

class ls_mlkit.model.decoder_tf.causal_transformer.CausalLanguageModelConfigForAuto(vocab_size=30000, embed_dim=1024, num_head=2, dropout=0, num_block=3, max_pos_len=5000, batch_first=True, **kwargs)[source]

Bases: PretrainedConfig

model_type: str = 'D-TF-no-PE'
class ls_mlkit.model.decoder_tf.causal_transformer.CausalLanguageModelForAuto(config: CausalLanguageModelConfigForAuto)[source]

Bases: PreTrainedModel, GenerationMixin

base_model_prefix = 'zls_causal_tf'
config_class

alias of CausalLanguageModelConfigForAuto

forward(input_ids: LongTensor = None, attention_mask: Tensor | None = None, output_attentions: bool | None = True, average_attn_weights: bool = True, position_ids: LongTensor | None = None, past_key_values=None, inputs_embeds: FloatTensor | None = None, labels: LongTensor | None = None, use_cache: bool | None = False, output_hidden_states: bool | None = None, return_dict: bool | None = None, cache_position: LongTensor | None = None)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_input_embeddings()[source]

Returns the model’s input embeddings.

Returns:

A torch module mapping vocabulary to hidden states.

Return type:

nn.Module

get_output_embeddings()[source]

Returns the model’s output embeddings.

Returns:

A torch module mapping hidden states to vocabulary.

Return type:

nn.Module

prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, **kwargs)[source]

Prepare the model inputs for generation. In includes operations like computing the 4D attention mask or slicing inputs given the existing cache.

See the forward pass in the model documentation for expected arguments (different models might have different requirements for e.g. past_key_values). This function should work as is for most LLMs.

class ls_mlkit.model.decoder_tf.causal_transformer.FeedForwardBlock(embed_dim, k=4, dropout=0.0, bias=False, act='relu')[source]

Bases: Module

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class ls_mlkit.model.decoder_tf.causal_transformer.MultiHeadAttention(embed_dim, num_heads, dropout=0, bias=False, add_bias_kv=False, add_zero_attn=False, kdim=None, vdim=None, batch_first=True, device=None, dtype=None)[source]

Bases: Module

attention(query: Tensor, key: Tensor, value: Tensor, key_padding_mask: Tensor | None = None, need_weights: bool = True, attn_mask: Tensor | None = None, average_attn_weights: bool = True, use_cache: bool = False, past_key_values=None) Tuple[Tensor, Tensor | None][source]
forward(q, k, v, key_padding_mask=None, attn_mask=None, average_attn_weights=True, need_weights=True, use_cache=False, past_key_values=None, is_causal=False)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

merge_masks(attn_mask: Tensor | None, key_padding_mask: Tensor | None, batch_size: int, seq_len: int) Tuple[Tensor | None, int | None][source]

Determine mask type and combine masks if necessary.

If only one mask is provided, that mask and the corresponding mask type will be returned. If both masks are provided, they will be both expanded to shape (batch_size, num_heads, seq_len, seq_len), combined with logical or and mask type 2 will be returned :param attn_mask: attention mask of shape (seq_len, seq_len), mask type 0 :param key_padding_mask: padding mask of shape (batch_size, seq_len), mask type 1 :param query: query embeddings of shape (batch_size, seq_len, embed_dim)

Returns:

merged mask mask_type: merged mask type (0, 1, or 2)

Return type:

merged_mask

class ls_mlkit.model.decoder_tf.causal_transformer.TransformerBlock(embed_dim, num_head, dropout, batch_first=True)[source]

Bases: Module

forward(x, att_mask=None, key_padding_mask=None, need_weights=True, average_attn_weights=True, use_cache=False, past_key_values=None, is_causal=True)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ls_mlkit.model.decoder_tf.causal_transformer.generate(prompt_tokens: Tensor, max_new_tokens: int, model, use_cache: bool = False) Tensor[source]

Generate text tokens autoregressively.

Parameters:
  • prompt_tokens – Input token ids of shape (batch_size, seq_len)

  • max_new_tokens – Number of new tokens to generate

  • use_cache – Whether to use KV cache during generation

Returns:

Generated token ids including prompt, shape (batch_size, seq_len + max_new_tokens)

ls_mlkit.model.decoder_tf.causal_transformer.get_causal_model(vocab_size=5000, embed_dim=1024, num_head=8, dropout=0, num_block=16, max_pos_len=5000, batch_first=True, **kwargs)[source]
ls_mlkit.model.decoder_tf.causal_transformer.register_model()[source]
ls_mlkit.model.decoder_tf.causal_transformer.test_gen(test_model, test_config)[source]