Qwen3_5MoeBackbone model

[source]

Qwen3_5MoeBackbone class

keras_hub.models.Qwen3_5MoeBackbone(
    vocabulary_size,
    num_layers,
    num_query_heads,
    num_key_value_heads,
    head_dim,
    hidden_dim,
    moe_intermediate_dim,
    shared_expert_intermediate_size,
    num_experts,
    top_k,
    layer_types=None,
    partial_rotary_factor=0.25,
    rope_max_wavelength=10000,
    rope_scaling_factor=1.0,
    layer_norm_epsilon=1e-06,
    dropout=0.0,
    tie_word_embeddings=False,
    sliding_window_size=32768,
    linear_num_key_heads=16,
    linear_num_value_heads=32,
    linear_key_head_dim=128,
    linear_value_head_dim=128,
    linear_conv_kernel_dim=4,
    vision_encoder=None,
    mrope_section=None,
    router_aux_loss_coefficient=0.001,
    dtype=None,
    **kwargs
)

The Qwen3.5 MoE Transformer core architecture with hyperparameters.

This network implements a hybrid Transformer-based decoder with two layer types and a Mixture-of-Experts (MoE) feedforward: - full_attention: Standard grouped-query attention with partial rotary embeddings and sigmoid output gating. - linear_attention: GatedDeltaNet recurrent linear attention with causal conv1d and delta rule recurrence.

The feedforward in every layer is a SparseMoeBlock with top-k expert routing and a shared expert with sigmoid gating.

The backbone optionally accepts a vision_encoder to enable multimodal (image + text) inputs.

Arguments

  • vocabulary_size: int. The size of the token vocabulary.
  • num_layers: int. The number of transformer layers.
  • num_query_heads: int. The number of query attention heads.
  • num_key_value_heads: int. The number of key and value attention heads.
  • head_dim: int. Dimension of each attention head.
  • hidden_dim: int. The size of the transformer hidden dimension.
  • moe_intermediate_dim: int. Intermediate dim per expert.
  • shared_expert_intermediate_size: int. Intermediate dim for the shared expert.
  • num_experts: int. Total number of routed experts.
  • top_k: int. Number of experts per token.
  • layer_types: list. List of layer types, one per layer.
  • partial_rotary_factor: float. Fraction of head_dim that gets RoPE. Defaults to 0.25.
  • rope_max_wavelength: int. Maximum wavelength for RoPE. Defaults to 10000.
  • rope_scaling_factor: float. Scaling factor for RoPE. Defaults to 1.0.
  • layer_norm_epsilon: float. Epsilon for layer norms. Defaults to 1e-6.
  • dropout: float. Dropout rate. Defaults to 0.0.
  • tie_word_embeddings: bool. Whether to tie input and output embeddings. Defaults to False.
  • sliding_window_size: int. Sliding window size for full attention layers. Defaults to 32768.
  • linear_num_key_heads: int. Key heads for linear attention.
  • linear_num_value_heads: int. Value heads for linear attention.
  • linear_key_head_dim: int. Key head dim for linear attention.
  • linear_value_head_dim: int. Value head dim for linear attention.
  • linear_conv_kernel_dim: int. Conv kernel size for linear attention.
  • vision_encoder: optional vision encoder for multimodal inputs.
  • mrope_section: list or None. M-RoPE section sizes.
  • router_aux_loss_coefficient: float. Coefficient for the router's auxiliary load-balancing loss.
  • dtype: string or keras.mixed_precision.DTypePolicy.

Example

input_data = {
    "token_ids": np.ones(shape=(1, 12), dtype="int32"),
    "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]]),
}

model = keras_hub.models.Qwen3_5MoeBackbone(
    vocabulary_size=248320,
    num_layers=4,
    num_query_heads=16,
    num_key_value_heads=2,
    head_dim=256,
    hidden_dim=2048,
    moe_intermediate_dim=512,
    shared_expert_intermediate_size=512,
    num_experts=8,
    top_k=2,
)
model(input_data)

[source]

from_preset method

Qwen3_5MoeBackbone.from_preset(preset, load_weights=True, **kwargs)

Instantiate a keras_hub.models.Backbone from a model preset.

A preset is a directory of configs, weights and other file assets used to save and load a pre-trained model. The preset can be passed as a one of:

  1. a built-in preset identifier like 'bert_base_en'
  2. a Kaggle Models handle like 'kaggle://user/bert/keras/bert_base_en'
  3. a Hugging Face handle like 'hf://user/bert_base_en'
  4. a ModelScope handle like 'modelscope://user/bert_base_en'
  5. a path to a local preset directory like './bert_base_en'

This constructor can be called in one of two ways. Either from the base class like keras_hub.models.Backbone.from_preset(), or from a model class like keras_hub.models.GemmaBackbone.from_preset(). If calling from the base class, the subclass of the returning object will be inferred from the config in the preset directory.

For any Backbone subclass, you can run cls.presets.keys() to list all built-in presets available on the class.

Arguments

  • preset: string. A built-in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.
  • load_weights: bool. If True, the weights will be loaded into the model architecture. If False, the weights will be randomly initialized.

Examples

# Load a Gemma backbone with pre-trained weights.
model = keras_hub.models.Backbone.from_preset(
    "gemma_2b_en",
)

# Load a Bert backbone with a pre-trained config and random weights.
model = keras_hub.models.Backbone.from_preset(
    "bert_base_en",
    load_weights=False,
)
Preset Parameters Description
qwen3_5_moe_35b_a3b_base 35.11B 35 billion total parameter Qwen3.5 MoE base model with ~3 billion active parameters per token. Features a 3:1 hybrid attention stack (GatedDeltaNet linear attention and full attention) with sparse Mixture-of-Experts feedforward for highly efficient inference. Supports text and multimodal inputs.
qwen3_5_moe_35b_a3b 35.11B 35 billion total parameter Qwen3.5 MoE instruction-tuned model with ~3 billion active parameters per token. Features a 3:1 hybrid attention stack (GatedDeltaNet linear attention and full attention) with sparse Mixture-of-Experts feedforward. Optimized for chat, reasoning, coding, and multimodal tasks.
qwen3_6_moe_35b_a3b 35.11B 35 billion total parameter Qwen3.6 MoE instruction-tuned model with ~3 billion active parameters per token. Features a 3:1 hybrid attention stack (GatedDeltaNet linear attention and full attention) with sparse Mixture-of-Experts feedforward. Optimized for fast inference and extended context lengths.

token_embedding property

keras_hub.models.Qwen3_5MoeBackbone.token_embedding

A keras.layers.Embedding instance for embedding token ids.

This layer embeds integer token ids to the hidden dim of the model.


[source]

enable_lora method

Qwen3_5MoeBackbone.enable_lora(rank, target_layer_names=None)

Enable Lora on the backbone.

Calling this method will freeze all weights on the backbone, while enabling Lora on the query & value EinsumDense layers of the attention layers.

Arguments

  • rank: The rank of the LoRA factorization.
  • target_layer_names: A list of strings, the names of the layers to apply LoRA to. If None, this will be populated with the default LoRA layer names as returned by backbone.default_lora_layer_names().