qwen3_5¶
Pytorch implementation of Qwen 3.5. It is widely inspired by Sebastian Raschka’s book and work https://github.com/rasbt/LLMs-from-scratch/blob/main/ch05/16_qwen3.5.
Qwen3.5 helper blocks copied from Hugging Face Transformers Source file: https://github.com/huggingface/transformers/blob/main/src/transformers/models/qwen3_5/modeling_qwen3_5.py
- class mfai.pytorch.models.llms.qwen3_5.FeedForward(settings)[source]¶
Bases:
Module- Parameters:
settings (Qwen3_5Settings)
- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.llms.qwen3_5.GroupedQueryAttention(d_in, num_heads, num_kv_groups, head_dim=None, qk_norm=False, dtype=None)[source]¶
Bases:
Module- Parameters:
- forward(x, mask, cos, sin, start_pos=0, cache=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
tuple[Tensor,tuple[Tensor,Tensor]]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.llms.qwen3_5.KVCache(n_layers)[source]¶
Bases:
object- Parameters:
n_layers (int)
- class mfai.pytorch.models.llms.qwen3_5.Qwen3_5(settings)[source]¶
Bases:
Module- Parameters:
settings (Qwen3_5Settings)
- forward(in_idx, cache=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- model_type = 4¶
- settings_kls¶
alias of
Qwen3_5Settings
- class mfai.pytorch.models.llms.qwen3_5.Qwen3_5GatedDeltaNet(settings, layer_idx)[source]¶
Bases:
Module- Parameters:
settings (Qwen3_5Settings)
layer_idx (int)
- forward(hidden_states, cache_params=None, cache_position=None, attention_mask=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.- Parameters:
hidden_states (Tensor)
cache_params (Qwen3_5LinearAttentionCache | None)
cache_position (Tensor | None)
attention_mask (Tensor | None)
- Return type:
- class mfai.pytorch.models.llms.qwen3_5.Qwen3_5LinearAttentionCache(n_layers)[source]¶
Bases:
object- Parameters:
n_layers (int)
- class mfai.pytorch.models.llms.qwen3_5.Qwen3_5RMSNormGated(hidden_size, eps=1e-06)[source]¶
Bases:
Module- forward(hidden_states, gate=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.llms.qwen3_5.Qwen3_5Settings(vocab_size=248320, context_length=262144, emb_dim=1024, n_heads=8, n_layers=24, hidden_dim=3584, head_dim=256, qk_norm=True, n_kv_groups=2, rope_base=10000000, partial_rotary_factor=0.25, rms_norm_eps=1e-06, linear_conv_kernel_dim=4, linear_key_head_dim=128, linear_value_head_dim=128, linear_num_key_heads=16, linear_num_value_heads=16, dtype=torch.bfloat16, layer_types=('linear_attention', 'linear_attention', 'linear_attention', 'full_attention', 'linear_attention', 'linear_attention', 'linear_attention', 'full_attention', 'linear_attention', 'linear_attention', 'linear_attention', 'full_attention', 'linear_attention', 'linear_attention', 'linear_attention', 'full_attention', 'linear_attention', 'linear_attention', 'linear_attention', 'full_attention', 'linear_attention', 'linear_attention', 'linear_attention', 'full_attention'), hidden_activation='silu')[source]¶
Bases:
objectQwen3.5-0.8B text configuration
- Parameters:
vocab_size (int)
context_length (int)
emb_dim (int)
n_heads (int)
n_layers (int)
hidden_dim (int)
head_dim (int)
qk_norm (bool)
n_kv_groups (int)
rope_base (int)
partial_rotary_factor (float)
rms_norm_eps (float)
linear_conv_kernel_dim (int)
linear_key_head_dim (int)
linear_value_head_dim (int)
linear_num_key_heads (int)
linear_num_value_heads (int)
dtype (dtype)
hidden_activation (str)
- classmethod from_dict(kvs, *, infer_missing=False)¶
- classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)¶
- classmethod schema(*, infer_missing=False, only=None, exclude=(), many=False, context=None, load_only=(), dump_only=(), partial=False, unknown=None)¶
- to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)¶
- class mfai.pytorch.models.llms.qwen3_5.RMSNorm(emb_dim, eps=1e-06)[source]¶
Bases:
Module- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.llms.qwen3_5.TransformerBlock(settings, layer_type, layer_idx)[source]¶
Bases:
Module- Parameters:
settings (Qwen3_5Settings)
layer_type (str)
layer_idx (int)
- forward(x, mask, cos, sin, start_pos=0, cache=None, linear_cache=None, cache_position=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
tuple[Tensor,tuple[Tensor,Tensor] |None]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- mfai.pytorch.models.llms.qwen3_5.apply_mask_to_padding_states(hidden_states, attention_mask)[source]¶
Tunes out the hidden states for padding tokens, see https://github.com/state-spaces/mamba/issues/66
- mfai.pytorch.models.llms.qwen3_5.compute_rope_params(head_dim, theta_base=10000, context_length=4096, partial_rotary_factor=1.0, dtype=torch.float32)[source]¶
- mfai.pytorch.models.llms.qwen3_5.l2norm(x, dim=-1, eps=1e-06)[source]¶
This function is intended to align with the l2norm implementation in the FLA library.
- mfai.pytorch.models.llms.qwen3_5.torch_causal_conv1d_update(hidden_states, conv_state, weight, bias=None)[source]¶
- mfai.pytorch.models.llms.qwen3_5.torch_chunk_gated_delta_rule(query, key, value, gate, beta, chunk_size=64, initial_state=None, output_final_state=False, use_qk_l2norm_in_kernel=False)[source]¶