Qwen3MoeBackbone classkeras_hub.models.Qwen3MoeBackbone(
vocabulary_size,
num_layers,
num_query_heads,
num_key_value_heads,
hidden_dim,
intermediate_dim,
moe_intermediate_dim,
num_experts,
head_dim=None,
top_k=4,
norm_top_k_prob=False,
decoder_sparse_step=1,
rope_max_wavelength=10000,
rope_scaling_factor=1.0,
layer_norm_epsilon=1e-06,
dropout=0,
dtype=None,
tie_word_embeddings=False,
sliding_window_size=32768,
router_aux_loss_coefficient=0.001,
mlp_only_layers=None,
training=None,
**kwargs
)
Qwen3 MoE core network with hyperparameters.
This backbone implements the base Transformer network for the Qwen MoE
model. It includes embedding lookups and transformer layers with a Mixture
of Experts (MoE) architecture, where each layer uses a sparse set of experts
for efficient computation. This backbone outputs the final hidden states for
each token, not generative predictions over the vocabulary space. For higher
-level object for text generation, see keras_hub.models.Qwen3MoeCausalLM.
The default constructor gives a fully customizable, randomly initialized
Qwen MoE model with any number of layers, heads, and embedding dimensions.
To load preset architectures and weights, use the from_preset constructor.
Arguments
keras.mixed_precision.DTypePolicy. The dtype to use for
the model's computations and weights. Note that some computations,
such as softmax and layer normalization, will always be done at
float32 precision regardless of dtype.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]]),
}
__Pretrained Qwen MoE decoder__
.
model = keras_hub.models.Qwen3MoeBackbone.from_preset("qwen3_moe_a2_7b")
model(input_data)
__Randomly initialized Qwen MoE decoder with custom config__
.
model = keras_hub.models.Qwen3MoeBackbone(
vocabulary_size=151936,
num_layers=28,
num_query_heads=16,
num_key_value_heads=8,
hidden_dim=2048,
intermediate_dim=4096,
moe_intermediate_dim=128,
num_experts=60,
top_k=4,
head_dim=128,
max_sequence_length=4096,
)
model(input_data)
----
<span style="float:right;">[[source]](https://github.com/keras-team/keras-hub/tree/v0.23.0/keras_hub/src/models/backbone.py#L117)</span>
### `from_preset` method
```python
Qwen3MoeBackbone.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:
'bert_base_en''kaggle://user/bert/keras/bert_base_en''hf://user/bert_base_en''modelscope://user/bert_base_en''./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
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_moe_30b_a3b_en | 30.53B | Mixture-of-Experts (MoE) model has 30.5 billion total parameters with 3.3 billion activated, built on 48 layers and utilizes 32 query and 4 key/value attention heads with 128 experts (8 active). |
| qwen3_moe_235b_a22b_en | 235.09B | Mixture-of-Experts (MoE) model has 235 billion total parameters with 22 billion activated, built on 94 layers and utilizes 64 query and 4 key/value attention heads with 128 experts (8 active). |
token_embedding propertykeras_hub.models.Qwen3MoeBackbone.token_embedding
A keras.layers.Embedding instance for embedding token ids.
This layer embeds integer token ids to the hidden dim of the model.
enable_lora methodQwen3MoeBackbone.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
None, this will be populated with the
default LoRA layer names as returned by
backbone.default_lora_layer_names().