QwenMoeBackbone
classkeras_hub.models.QwenMoeBackbone(
vocabulary_size,
num_layers,
num_query_heads,
num_key_value_heads,
hidden_dim,
intermediate_dim,
moe_intermediate_dim,
shared_expert_intermediate_dim,
num_experts,
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,
use_sliding_window_attention=False,
sliding_window_size=32768,
output_router_logits=False,
router_aux_loss_coefficient=0.001,
mlp_only_layers=[],
training=None,
**kwargs
)
Qwen 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.QwenMoeCausalLM
.
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.QwenMoeBackbone.from_preset("qwen_moe_a2_7b")
model(input_data)
__Randomly initialized Qwen MoE decoder with custom config__
.
model = keras_hub.models.QwenMoeBackbone(
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,
shared_expert_intermediate_dim=4096,
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.21.1/keras_hub/src/models/backbone.py#L122)</span>
### `from_preset` method
```python
QwenMoeBackbone.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'
'./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 |
---|---|---|
qwen1.5_moe_2.7b_en | 14.32B | 24-layer Qwen MoE model with 2.7 billion active parameters and 8 experts per MoE layer. |
token_embedding
propertykeras_hub.models.QwenMoeBackbone.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
methodQwenMoeBackbone.enable_lora(rank, target_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.