keras-hub-nightly 0.21.0.dev202505280410__py3-none-any.whl → 0.22.0.dev202505300409__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- keras_hub/models/__init__.py +9 -0
- keras_hub/src/models/mixtral/mixtral_presets.py +4 -4
- keras_hub/src/models/qwen/qwen_presets.py +6 -6
- keras_hub/src/models/qwen3/qwen3_attention.py +369 -0
- keras_hub/src/models/qwen3/qwen3_backbone.py +191 -0
- keras_hub/src/models/qwen3/qwen3_causal_lm_preprocessor.py +10 -0
- keras_hub/src/models/qwen3/qwen3_decoder.py +309 -0
- keras_hub/src/models/qwen3/qwen3_layernorm.py +38 -0
- keras_hub/src/models/qwen3/qwen3_tokenizer.py +48 -0
- keras_hub/src/models/qwen_moe/qwen_moe_presets.py +2 -2
- keras_hub/src/models/vit/vit_backbone.py +31 -11
- keras_hub/src/models/vit/vit_image_converter.py +0 -70
- keras_hub/src/models/vit/vit_layers.py +33 -18
- keras_hub/src/models/vit/vit_presets.py +11 -11
- keras_hub/src/utils/transformers/convert_qwen3.py +145 -0
- keras_hub/src/utils/transformers/preset_loader.py +3 -0
- keras_hub/src/version.py +1 -1
- {keras_hub_nightly-0.21.0.dev202505280410.dist-info → keras_hub_nightly-0.22.0.dev202505300409.dist-info}/METADATA +1 -1
- {keras_hub_nightly-0.21.0.dev202505280410.dist-info → keras_hub_nightly-0.22.0.dev202505300409.dist-info}/RECORD +21 -14
- {keras_hub_nightly-0.21.0.dev202505280410.dist-info → keras_hub_nightly-0.22.0.dev202505300409.dist-info}/WHEEL +0 -0
- {keras_hub_nightly-0.21.0.dev202505280410.dist-info → keras_hub_nightly-0.22.0.dev202505300409.dist-info}/top_level.txt +0 -0
keras_hub/models/__init__.py
CHANGED
@@ -444,6 +444,15 @@ from keras_hub.src.models.qwen.qwen_tokenizer import (
|
|
444
444
|
from keras_hub.src.models.qwen.qwen_tokenizer import (
|
445
445
|
QwenTokenizer as QwenTokenizer,
|
446
446
|
)
|
447
|
+
from keras_hub.src.models.qwen3.qwen3_backbone import (
|
448
|
+
Qwen3Backbone as Qwen3Backbone,
|
449
|
+
)
|
450
|
+
from keras_hub.src.models.qwen3.qwen3_causal_lm_preprocessor import (
|
451
|
+
Qwen3CausalLMPreprocessor as Qwen3CausalLMPreprocessor,
|
452
|
+
)
|
453
|
+
from keras_hub.src.models.qwen3.qwen3_tokenizer import (
|
454
|
+
Qwen3Tokenizer as Qwen3Tokenizer,
|
455
|
+
)
|
447
456
|
from keras_hub.src.models.qwen_moe.qwen_moe_backbone import (
|
448
457
|
QwenMoeBackbone as QwenMoeBackbone,
|
449
458
|
)
|
@@ -4,8 +4,8 @@ backbone_presets = {
|
|
4
4
|
"mixtral_8_7b_en": {
|
5
5
|
"metadata": {
|
6
6
|
"description": (
|
7
|
-
"32-layer Mixtral MoE model with 7 billion"
|
8
|
-
"active parameters and 8 experts per MoE layer."
|
7
|
+
"32-layer Mixtral MoE model with 7 billion"
|
8
|
+
"active parameters and 8 experts per MoE layer."
|
9
9
|
),
|
10
10
|
"params": 46702792704,
|
11
11
|
"path": "mixtral",
|
@@ -15,8 +15,8 @@ backbone_presets = {
|
|
15
15
|
"mixtral_8_instruct_7b_en": {
|
16
16
|
"metadata": {
|
17
17
|
"description": (
|
18
|
-
"Instruction fine-tuned 32-layer Mixtral MoE model"
|
19
|
-
"with 7 billion active parameters and 8 experts per MoE layer."
|
18
|
+
"Instruction fine-tuned 32-layer Mixtral MoE model"
|
19
|
+
"with 7 billion active parameters and 8 experts per MoE layer."
|
20
20
|
),
|
21
21
|
"params": 46702792704,
|
22
22
|
"path": "mixtral",
|
@@ -28,8 +28,8 @@ backbone_presets = {
|
|
28
28
|
"qwen2.5_instruct_0.5b_en": {
|
29
29
|
"metadata": {
|
30
30
|
"description": (
|
31
|
-
"Instruction fine-tuned 24-layer Qwen model with 0.5 "
|
32
|
-
"billion parameters."
|
31
|
+
"Instruction fine-tuned 24-layer Qwen model with 0.5 "
|
32
|
+
"billion parameters."
|
33
33
|
),
|
34
34
|
"params": 494032768,
|
35
35
|
"path": "qwen",
|
@@ -39,8 +39,8 @@ backbone_presets = {
|
|
39
39
|
"qwen2.5_instruct_32b_en": {
|
40
40
|
"metadata": {
|
41
41
|
"description": (
|
42
|
-
"Instruction fine-tuned 64-layer Qwen model with 32 "
|
43
|
-
"billion parameters."
|
42
|
+
"Instruction fine-tuned 64-layer Qwen model with 32 "
|
43
|
+
"billion parameters."
|
44
44
|
),
|
45
45
|
"params": 32763876352,
|
46
46
|
"path": "qwen",
|
@@ -50,8 +50,8 @@ backbone_presets = {
|
|
50
50
|
"qwen2.5_instruct_72b_en": {
|
51
51
|
"metadata": {
|
52
52
|
"description": (
|
53
|
-
"Instruction fine-tuned 80-layer Qwen model with 72 "
|
54
|
-
"billion parameters."
|
53
|
+
"Instruction fine-tuned 80-layer Qwen model with 72 "
|
54
|
+
"billion parameters."
|
55
55
|
),
|
56
56
|
"params": 72706203648,
|
57
57
|
"path": "qwen",
|
@@ -0,0 +1,369 @@
|
|
1
|
+
import math
|
2
|
+
|
3
|
+
import keras
|
4
|
+
from keras import ops
|
5
|
+
|
6
|
+
from keras_hub.src.layers.modeling.rotary_embedding import RotaryEmbedding
|
7
|
+
from keras_hub.src.models.qwen3.qwen3_layernorm import Qwen3LayerNorm
|
8
|
+
from keras_hub.src.utils.keras_utils import clone_initializer
|
9
|
+
from keras_hub.src.utils.keras_utils import fused_attention_op_available
|
10
|
+
|
11
|
+
|
12
|
+
class Qwen3Attention(keras.layers.Layer):
|
13
|
+
"""A multi-head attention layer for Qwen3 models
|
14
|
+
|
15
|
+
This attention implementation supports grouped-query attention (GQA) where
|
16
|
+
the number of key-value heads can be less than the number of query heads.
|
17
|
+
|
18
|
+
Args:
|
19
|
+
num_query_heads: Number of query heads.
|
20
|
+
num_key_value_heads: Number of key/value heads (for GQA).
|
21
|
+
rope_max_wavelength: Maximum wavelength for RoPE (Rotary Position
|
22
|
+
Embedding).
|
23
|
+
rope_scaling_factor: Scaling factor for RoPE, used for extending
|
24
|
+
context length.
|
25
|
+
kernel_initializer: Initializer for the kernel weights.
|
26
|
+
dropout: Dropout rate for attention weights.
|
27
|
+
sliding_window_size: Size of the sliding window for attention.
|
28
|
+
**kwargs: Additional keyword arguments to pass to the Layer.
|
29
|
+
"""
|
30
|
+
|
31
|
+
def __init__(
|
32
|
+
self,
|
33
|
+
num_query_heads,
|
34
|
+
num_key_value_heads,
|
35
|
+
head_dim,
|
36
|
+
rope_max_wavelength=10000,
|
37
|
+
rope_scaling_factor=1,
|
38
|
+
kernel_initializer="glorot_uniform",
|
39
|
+
dropout=0.0,
|
40
|
+
layer_norm_epsilon=1e-5,
|
41
|
+
sliding_window_size=None,
|
42
|
+
**kwargs,
|
43
|
+
):
|
44
|
+
super().__init__(
|
45
|
+
**kwargs,
|
46
|
+
)
|
47
|
+
self.num_query_heads = num_query_heads
|
48
|
+
self.num_key_value_heads = num_key_value_heads
|
49
|
+
self.head_dim = head_dim
|
50
|
+
self.dropout = dropout
|
51
|
+
|
52
|
+
self.layer_norm_epsilon = layer_norm_epsilon
|
53
|
+
|
54
|
+
self.num_key_value_groups = num_query_heads // num_key_value_heads
|
55
|
+
self.rope_max_wavelength = rope_max_wavelength
|
56
|
+
|
57
|
+
self.kernel_initializer = keras.initializers.get(
|
58
|
+
clone_initializer(kernel_initializer)
|
59
|
+
)
|
60
|
+
|
61
|
+
self.rope_scaling_factor = rope_scaling_factor
|
62
|
+
self.sliding_window_size = sliding_window_size
|
63
|
+
|
64
|
+
def build(self, inputs_shape):
|
65
|
+
# Einsum variables:
|
66
|
+
# b = batch size
|
67
|
+
# q = query length
|
68
|
+
# k = key/value length
|
69
|
+
# m = model dim
|
70
|
+
# u = num query heads
|
71
|
+
# v = num key/value heads
|
72
|
+
# h = head dim
|
73
|
+
hidden_dim = inputs_shape[-1]
|
74
|
+
if not self.head_dim:
|
75
|
+
self.head_dim = hidden_dim // self.num_query_heads
|
76
|
+
|
77
|
+
self._inv_norm_factor = 1.0 / math.sqrt(self.head_dim)
|
78
|
+
self._query_dense = keras.layers.EinsumDense(
|
79
|
+
equation="bqm,muh->bquh",
|
80
|
+
output_shape=(None, self.num_query_heads, self.head_dim),
|
81
|
+
kernel_initializer=self.kernel_initializer,
|
82
|
+
dtype=self.dtype_policy,
|
83
|
+
name="query",
|
84
|
+
)
|
85
|
+
self._query_dense.build(inputs_shape)
|
86
|
+
|
87
|
+
self._query_dense_layer_norm = Qwen3LayerNorm(
|
88
|
+
epsilon=self.layer_norm_epsilon,
|
89
|
+
dtype=self.dtype_policy,
|
90
|
+
head_dim=self.head_dim,
|
91
|
+
name="query_dense_layernorm",
|
92
|
+
)
|
93
|
+
self._query_dense_layer_norm.build(inputs_shape)
|
94
|
+
|
95
|
+
self._key_dense = keras.layers.EinsumDense(
|
96
|
+
equation="bkm,mvh->bkvh",
|
97
|
+
output_shape=(
|
98
|
+
None,
|
99
|
+
self.num_key_value_heads,
|
100
|
+
self.head_dim,
|
101
|
+
),
|
102
|
+
kernel_initializer=self.kernel_initializer,
|
103
|
+
dtype=self.dtype_policy,
|
104
|
+
name="key",
|
105
|
+
)
|
106
|
+
self._key_dense.build(inputs_shape)
|
107
|
+
|
108
|
+
self._key_dense_layer_norm = Qwen3LayerNorm(
|
109
|
+
epsilon=self.layer_norm_epsilon,
|
110
|
+
dtype=self.dtype_policy,
|
111
|
+
head_dim=self.head_dim,
|
112
|
+
name="key_dense_layernorm",
|
113
|
+
)
|
114
|
+
self._key_dense_layer_norm.build(inputs_shape)
|
115
|
+
|
116
|
+
self._value_dense = keras.layers.EinsumDense(
|
117
|
+
equation="bkm,mvh->bkvh",
|
118
|
+
output_shape=(
|
119
|
+
None,
|
120
|
+
self.num_key_value_heads,
|
121
|
+
self.head_dim,
|
122
|
+
),
|
123
|
+
kernel_initializer=self.kernel_initializer,
|
124
|
+
dtype=self.dtype_policy,
|
125
|
+
name="value",
|
126
|
+
)
|
127
|
+
self._value_dense.build(inputs_shape)
|
128
|
+
|
129
|
+
self._softmax = keras.layers.Softmax(
|
130
|
+
axis=-1,
|
131
|
+
dtype="float32",
|
132
|
+
name="attention_softmax",
|
133
|
+
)
|
134
|
+
|
135
|
+
self._dropout_layer = keras.layers.Dropout(
|
136
|
+
rate=self.dropout,
|
137
|
+
dtype=self.dtype_policy,
|
138
|
+
)
|
139
|
+
|
140
|
+
self._output_dense = keras.layers.EinsumDense(
|
141
|
+
equation="bquh,uhm->bqm",
|
142
|
+
output_shape=(None, hidden_dim),
|
143
|
+
kernel_initializer=self.kernel_initializer,
|
144
|
+
dtype=self.dtype_policy,
|
145
|
+
name="attention_output",
|
146
|
+
)
|
147
|
+
self._output_dense.build(
|
148
|
+
(None, None, self.num_query_heads, self.head_dim)
|
149
|
+
)
|
150
|
+
|
151
|
+
self.rotary_embedding_layer = RotaryEmbedding(
|
152
|
+
max_wavelength=self.rope_max_wavelength,
|
153
|
+
scaling_factor=self.rope_scaling_factor,
|
154
|
+
dtype=self.dtype_policy,
|
155
|
+
)
|
156
|
+
|
157
|
+
self._dot_product_equation = "bquh,bkuh->buqk"
|
158
|
+
self._combine_equation = "buqk,bkuh->bquh"
|
159
|
+
|
160
|
+
self.built = True
|
161
|
+
|
162
|
+
def call(
|
163
|
+
self,
|
164
|
+
hidden_states,
|
165
|
+
attention_mask=None,
|
166
|
+
cache=None,
|
167
|
+
cache_update_index=None,
|
168
|
+
training=None,
|
169
|
+
):
|
170
|
+
"""Applies attention mechanism to the input hidden states.
|
171
|
+
|
172
|
+
Args:
|
173
|
+
hidden_states: Input tensor of shape [batch_size, seq_length,
|
174
|
+
hidden_size].
|
175
|
+
attention_mask: Mask tensor of shape [batch_size, seq_length,
|
176
|
+
seq_length].
|
177
|
+
cache: Optional cached key and value tensors.
|
178
|
+
cache_update_index: Index at which to update the cache.
|
179
|
+
training: Boolean indicating whether in training mode.
|
180
|
+
|
181
|
+
Returns:
|
182
|
+
attention_output: Output tensor after applying attention.
|
183
|
+
cache: Updated cache tensors (if cache is provided).
|
184
|
+
"""
|
185
|
+
start_index = (
|
186
|
+
cache_update_index if cache_update_index is not None else 0
|
187
|
+
)
|
188
|
+
|
189
|
+
query = self._query_dense(hidden_states)
|
190
|
+
query = self._query_dense_layer_norm(query)
|
191
|
+
|
192
|
+
# Compute RoPE for queries
|
193
|
+
query = self.rotary_embedding_layer(query, start_index=start_index)
|
194
|
+
|
195
|
+
def _compute_key_value(x):
|
196
|
+
key = self._key_dense(x)
|
197
|
+
key = self._key_dense_layer_norm(key)
|
198
|
+
key = self.rotary_embedding_layer(key, start_index=start_index)
|
199
|
+
|
200
|
+
value = self._value_dense(x)
|
201
|
+
|
202
|
+
return key, value
|
203
|
+
|
204
|
+
if cache is not None:
|
205
|
+
key_cache = cache[:, 0, ...]
|
206
|
+
value_cache = cache[:, 1, ...]
|
207
|
+
if cache_update_index is None:
|
208
|
+
key = key_cache
|
209
|
+
value = value_cache
|
210
|
+
else:
|
211
|
+
key_update, value_update = _compute_key_value(hidden_states)
|
212
|
+
start = [0, cache_update_index, 0, 0]
|
213
|
+
key = ops.slice_update(key_cache, start, key_update)
|
214
|
+
value = ops.slice_update(value_cache, start, value_update)
|
215
|
+
cache = ops.stack((key, value), axis=1)
|
216
|
+
else:
|
217
|
+
if cache_update_index is not None:
|
218
|
+
raise ValueError(
|
219
|
+
"`cache_update_index` should not be set if `cache` is "
|
220
|
+
f"`None`. Received: cache={cache}, "
|
221
|
+
f"cache_update_index={cache_update_index}"
|
222
|
+
)
|
223
|
+
key, value = _compute_key_value(hidden_states)
|
224
|
+
|
225
|
+
# [batch_shape, seq_len, num_key_value_heads, head_dim]
|
226
|
+
# -> [batch_shape, seq_len, num_heads, head_dim]
|
227
|
+
key = ops.repeat(key, repeats=self.num_key_value_groups, axis=2)
|
228
|
+
value = ops.repeat(value, repeats=self.num_key_value_groups, axis=2)
|
229
|
+
|
230
|
+
attention_output = self._compute_attention(
|
231
|
+
query,
|
232
|
+
key,
|
233
|
+
value,
|
234
|
+
attention_mask,
|
235
|
+
cache_update_index=cache_update_index,
|
236
|
+
)
|
237
|
+
|
238
|
+
attention_output = self._dropout_layer(
|
239
|
+
attention_output, training=training
|
240
|
+
)
|
241
|
+
|
242
|
+
attention_output = self._output_dense(attention_output)
|
243
|
+
|
244
|
+
if cache is not None:
|
245
|
+
return attention_output, cache
|
246
|
+
return attention_output
|
247
|
+
|
248
|
+
def _masked_softmax(self, attention_scores, attention_mask=None):
|
249
|
+
"""Applies softmax with optional masking.
|
250
|
+
|
251
|
+
Args:
|
252
|
+
attention_scores: Attention score tensor.
|
253
|
+
attention_mask: Optional mask tensor.
|
254
|
+
|
255
|
+
Returns:
|
256
|
+
Masked softmax attention weights.
|
257
|
+
"""
|
258
|
+
if attention_mask is not None:
|
259
|
+
return self._softmax(
|
260
|
+
attention_scores, attention_mask[:, None, :, :]
|
261
|
+
)
|
262
|
+
return self._softmax(attention_scores)
|
263
|
+
|
264
|
+
def _compute_attention(
|
265
|
+
self, query, key, value, attention_mask=None, cache_update_index=None
|
266
|
+
):
|
267
|
+
"""Computes attention using query, key, and value tensors.
|
268
|
+
|
269
|
+
Uses Flash Attention when available for better performance.
|
270
|
+
|
271
|
+
Args:
|
272
|
+
query: Query tensor.
|
273
|
+
key: Key tensor.
|
274
|
+
value: Value tensor.
|
275
|
+
attention_mask: Optional mask tensor.
|
276
|
+
cache_update_index: Index for sliding window computation.
|
277
|
+
|
278
|
+
Returns:
|
279
|
+
attention_output: Output tensor after applying attention.
|
280
|
+
"""
|
281
|
+
if fused_attention_op_available():
|
282
|
+
# Use `dot_product_attention` with Flash Attention support if
|
283
|
+
# available.
|
284
|
+
if attention_mask is not None:
|
285
|
+
attention_mask = ops.expand_dims(attention_mask, axis=1)
|
286
|
+
attention_mask = ops.cast(attention_mask, dtype="bool")
|
287
|
+
attention_output = ops.dot_product_attention(
|
288
|
+
query,
|
289
|
+
key,
|
290
|
+
value,
|
291
|
+
mask=attention_mask,
|
292
|
+
scale=self._inv_norm_factor,
|
293
|
+
)
|
294
|
+
return attention_output
|
295
|
+
|
296
|
+
attention_scores = ops.einsum(self._dot_product_equation, query, key)
|
297
|
+
|
298
|
+
attention_scores = ops.multiply(
|
299
|
+
attention_scores,
|
300
|
+
ops.cast(self._inv_norm_factor, self.compute_dtype),
|
301
|
+
)
|
302
|
+
if not self.sliding_window_size:
|
303
|
+
attention_mask = self._mask_sliding_window(
|
304
|
+
attention_mask,
|
305
|
+
cache_update_index=cache_update_index
|
306
|
+
if cache_update_index
|
307
|
+
else 0,
|
308
|
+
)
|
309
|
+
attention_scores = self._masked_softmax(
|
310
|
+
attention_scores, attention_mask
|
311
|
+
)
|
312
|
+
attention_scores = ops.cast(attention_scores, self.compute_dtype)
|
313
|
+
attention_output = ops.einsum(
|
314
|
+
self._combine_equation, attention_scores, value
|
315
|
+
)
|
316
|
+
|
317
|
+
return attention_output
|
318
|
+
|
319
|
+
def _mask_sliding_window(
|
320
|
+
self,
|
321
|
+
attention_mask,
|
322
|
+
cache_update_index=0,
|
323
|
+
):
|
324
|
+
"""Creates and combines a sliding window mask with the attention mask.
|
325
|
+
|
326
|
+
Args:
|
327
|
+
attention_mask: Original attention mask.
|
328
|
+
cache_update_index: Starting index for the sliding window.
|
329
|
+
|
330
|
+
Returns:
|
331
|
+
Combined attention mask with sliding window constraints.
|
332
|
+
"""
|
333
|
+
_, query_len, key_len = ops.shape(attention_mask)
|
334
|
+
# Compute the sliding window for square attention.
|
335
|
+
all_ones = ops.ones((key_len, key_len), "bool")
|
336
|
+
if keras.config.backend() == "tensorflow":
|
337
|
+
# TODO: trui/tril has issues with dynamic shape on the tensorflow
|
338
|
+
# backend. We should fix, but use `band_part` for now.
|
339
|
+
import tensorflow as tf
|
340
|
+
|
341
|
+
band_size = ops.minimum(key_len, self.sliding_window_size - 1)
|
342
|
+
band_size = ops.cast(band_size, "int32")
|
343
|
+
sliding_mask = tf.linalg.band_part(all_ones, band_size, band_size)
|
344
|
+
else:
|
345
|
+
sliding_mask = ops.triu(
|
346
|
+
all_ones, -1 * self.sliding_window_size + 1
|
347
|
+
) * ops.tril(all_ones, self.sliding_window_size - 1)
|
348
|
+
# Slice the window for short queries during generation.
|
349
|
+
start = (cache_update_index, 0)
|
350
|
+
sliding_mask = ops.slice(sliding_mask, start, (query_len, key_len))
|
351
|
+
sliding_mask = ops.expand_dims(sliding_mask, 0)
|
352
|
+
return ops.logical_and(attention_mask, ops.cast(sliding_mask, "bool"))
|
353
|
+
|
354
|
+
def get_config(self):
|
355
|
+
config = super().get_config()
|
356
|
+
config.update(
|
357
|
+
{
|
358
|
+
"num_query_heads": self.num_query_heads,
|
359
|
+
"num_key_value_heads": self.num_key_value_heads,
|
360
|
+
"rope_max_wavelength": self.rope_max_wavelength,
|
361
|
+
"rope_scaling_factor": self.rope_scaling_factor,
|
362
|
+
"kernel_initializer": keras.initializers.serialize(
|
363
|
+
self.kernel_initializer
|
364
|
+
),
|
365
|
+
"dropout": self.dropout,
|
366
|
+
"sliding_window_size": self.sliding_window_size,
|
367
|
+
}
|
368
|
+
)
|
369
|
+
return config
|
@@ -0,0 +1,191 @@
|
|
1
|
+
import keras
|
2
|
+
from keras import ops
|
3
|
+
|
4
|
+
from keras_hub.src.api_export import keras_hub_export
|
5
|
+
from keras_hub.src.layers.modeling.reversible_embedding import (
|
6
|
+
ReversibleEmbedding,
|
7
|
+
)
|
8
|
+
from keras_hub.src.models.backbone import Backbone
|
9
|
+
from keras_hub.src.models.qwen3.qwen3_decoder import Qwen3TransformerDecoder
|
10
|
+
from keras_hub.src.models.qwen3.qwen3_layernorm import Qwen3LayerNorm
|
11
|
+
|
12
|
+
|
13
|
+
def _qwen3_kernel_initializer(stddev=0.02):
|
14
|
+
return keras.initializers.RandomNormal(stddev=stddev)
|
15
|
+
|
16
|
+
|
17
|
+
@keras_hub_export("keras_hub.models.Qwen3Backbone")
|
18
|
+
class Qwen3Backbone(Backbone):
|
19
|
+
"""The Qwen3 Transformer core architecture with hyperparameters.
|
20
|
+
|
21
|
+
This network implements a Transformer-based decoder network,
|
22
|
+
Qwen3, as described in the Qwen3 model architecture.
|
23
|
+
It includes the embedding lookups and transformer layers.
|
24
|
+
|
25
|
+
The default constructor gives a fully customizable, randomly initialized
|
26
|
+
Qwen3 model with any number of layers, heads, and embedding
|
27
|
+
dimensions. To load preset architectures and weights, use the `from_preset`
|
28
|
+
constructor.
|
29
|
+
|
30
|
+
Args:
|
31
|
+
vocabulary_size (int): The size of the token vocabulary.
|
32
|
+
num_layers (int): The number of transformer layers.
|
33
|
+
num_query_heads (int): The number of query attention heads for
|
34
|
+
each transformer.
|
35
|
+
hidden_dim (int): The size of the transformer encoding and pooling
|
36
|
+
layers.
|
37
|
+
intermediate_dim (int): The output dimension of the first Dense layer in
|
38
|
+
a three-layer feedforward network for each transformer.
|
39
|
+
num_key_value_heads (int): The number of key and value attention heads
|
40
|
+
for each transformer.
|
41
|
+
rope_max_wavelength (int, optional): The maximum angular wavelength of
|
42
|
+
the sine/cosine curves, for rotary embeddings. Defaults to `10000`.
|
43
|
+
rope_scaling_factor (float, optional): The scaling factor for
|
44
|
+
calculation of rotary embedding. Defaults to `1.0`.
|
45
|
+
layer_norm_epsilon (float, optional): Epsilon for the layer
|
46
|
+
normalization layers in the transformer decoder. Defaults to `1e-6`.
|
47
|
+
dropout (float, optional): Dropout rate for attention and hidden layers.
|
48
|
+
Defaults to `0`.
|
49
|
+
dtype: string or `keras.mixed_precision.DTypePolicy`. The dtype to use
|
50
|
+
for model computations and weights. Note that some computations,
|
51
|
+
such as softmax and layer normalization, will always be done at
|
52
|
+
float32 precision regardless of dtype.
|
53
|
+
tie_word_embeddings (bool, optional): Whether to tie input and output
|
54
|
+
embeddings. Defaults to `True`.
|
55
|
+
sliding_window_size (int, optional): Size of the sliding window for
|
56
|
+
attention when enabled. Defaults to `32768`.
|
57
|
+
|
58
|
+
Examples:
|
59
|
+
|
60
|
+
```python
|
61
|
+
input_data = {
|
62
|
+
"token_ids": np.ones(shape=(1, 12), dtype="int32"),
|
63
|
+
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]]),
|
64
|
+
}
|
65
|
+
|
66
|
+
# Pretrained Qwen3 decoder.
|
67
|
+
model = keras_hub.models.Qwen3Backbone.from_preset("qwen32.5_0.5b_en")
|
68
|
+
model(input_data)
|
69
|
+
|
70
|
+
# Randomly initialized Qwen3 decoder with custom config.
|
71
|
+
model = keras_hub.models.Qwen3Backbone(
|
72
|
+
vocabulary_size=10,
|
73
|
+
hidden_dim=512,
|
74
|
+
num_layers=2,
|
75
|
+
num_query_heads=32,
|
76
|
+
num_key_value_heads=8,
|
77
|
+
intermediate_dim=1024,
|
78
|
+
layer_norm_epsilon=1e-6,
|
79
|
+
dtype="float32"
|
80
|
+
)
|
81
|
+
model(input_data)
|
82
|
+
```
|
83
|
+
"""
|
84
|
+
|
85
|
+
def __init__(
|
86
|
+
self,
|
87
|
+
vocabulary_size,
|
88
|
+
num_layers,
|
89
|
+
num_query_heads,
|
90
|
+
num_key_value_heads,
|
91
|
+
head_dim,
|
92
|
+
hidden_dim,
|
93
|
+
intermediate_dim,
|
94
|
+
rope_max_wavelength=10000,
|
95
|
+
rope_scaling_factor=1.0,
|
96
|
+
layer_norm_epsilon=1e-6,
|
97
|
+
dropout=0.0,
|
98
|
+
tie_word_embeddings=True,
|
99
|
+
sliding_window_size=32768,
|
100
|
+
dtype=None,
|
101
|
+
**kwargs,
|
102
|
+
):
|
103
|
+
# === Layers ===
|
104
|
+
self.token_embedding = ReversibleEmbedding(
|
105
|
+
input_dim=vocabulary_size,
|
106
|
+
output_dim=hidden_dim,
|
107
|
+
tie_weights=tie_word_embeddings,
|
108
|
+
embeddings_initializer=_qwen3_kernel_initializer(stddev=0.01),
|
109
|
+
dtype=dtype,
|
110
|
+
name="token_embedding",
|
111
|
+
)
|
112
|
+
self.transformer_layers = []
|
113
|
+
for i in range(num_layers):
|
114
|
+
layer = Qwen3TransformerDecoder(
|
115
|
+
intermediate_dim=intermediate_dim,
|
116
|
+
head_dim=head_dim,
|
117
|
+
num_query_heads=num_query_heads,
|
118
|
+
num_key_value_heads=num_key_value_heads,
|
119
|
+
rope_max_wavelength=rope_max_wavelength,
|
120
|
+
rope_scaling_factor=rope_scaling_factor,
|
121
|
+
layer_norm_epsilon=layer_norm_epsilon,
|
122
|
+
activation=ops.silu,
|
123
|
+
kernel_initializer=_qwen3_kernel_initializer(stddev=0.02),
|
124
|
+
dropout=dropout,
|
125
|
+
sliding_window_size=sliding_window_size,
|
126
|
+
dtype=dtype,
|
127
|
+
name=f"transformer_layer_{i}",
|
128
|
+
)
|
129
|
+
self.transformer_layers.append(layer)
|
130
|
+
self.layer_norm = Qwen3LayerNorm(
|
131
|
+
epsilon=layer_norm_epsilon,
|
132
|
+
dtype=dtype,
|
133
|
+
name="sequence_output_layernorm",
|
134
|
+
)
|
135
|
+
|
136
|
+
# === Functional Model ===
|
137
|
+
token_id_input = keras.Input(
|
138
|
+
shape=(None,), dtype="int32", name="token_ids"
|
139
|
+
)
|
140
|
+
padding_mask_input = keras.Input(
|
141
|
+
shape=(None,), dtype="int32", name="padding_mask"
|
142
|
+
)
|
143
|
+
x = self.token_embedding(token_id_input)
|
144
|
+
for transformer_layer in self.transformer_layers:
|
145
|
+
x = transformer_layer(x, decoder_padding_mask=padding_mask_input)
|
146
|
+
sequence_output = self.layer_norm(x)
|
147
|
+
super().__init__(
|
148
|
+
inputs={
|
149
|
+
"token_ids": token_id_input,
|
150
|
+
"padding_mask": padding_mask_input,
|
151
|
+
},
|
152
|
+
outputs=sequence_output,
|
153
|
+
dtype=dtype,
|
154
|
+
**kwargs,
|
155
|
+
)
|
156
|
+
|
157
|
+
# === Config ===
|
158
|
+
self.vocabulary_size = vocabulary_size
|
159
|
+
self.num_layers = num_layers
|
160
|
+
self.num_query_heads = num_query_heads
|
161
|
+
self.hidden_dim = hidden_dim
|
162
|
+
self.head_dim = head_dim
|
163
|
+
self.intermediate_dim = intermediate_dim
|
164
|
+
self.rope_max_wavelength = rope_max_wavelength
|
165
|
+
self.num_key_value_heads = num_key_value_heads
|
166
|
+
self.rope_scaling_factor = rope_scaling_factor
|
167
|
+
self.layer_norm_epsilon = layer_norm_epsilon
|
168
|
+
self.dropout = dropout
|
169
|
+
self.tie_word_embeddings = tie_word_embeddings
|
170
|
+
self.sliding_window_size = sliding_window_size
|
171
|
+
|
172
|
+
def get_config(self):
|
173
|
+
config = super().get_config()
|
174
|
+
config.update(
|
175
|
+
{
|
176
|
+
"vocabulary_size": self.vocabulary_size,
|
177
|
+
"num_layers": self.num_layers,
|
178
|
+
"num_query_heads": self.num_query_heads,
|
179
|
+
"hidden_dim": self.hidden_dim,
|
180
|
+
"head_dim": self.head_dim,
|
181
|
+
"intermediate_dim": self.intermediate_dim,
|
182
|
+
"rope_max_wavelength": self.rope_max_wavelength,
|
183
|
+
"rope_scaling_factor": self.rope_scaling_factor,
|
184
|
+
"num_key_value_heads": self.num_key_value_heads,
|
185
|
+
"layer_norm_epsilon": self.layer_norm_epsilon,
|
186
|
+
"dropout": self.dropout,
|
187
|
+
"tie_word_embeddings": self.tie_word_embeddings,
|
188
|
+
"sliding_window_size": self.sliding_window_size,
|
189
|
+
}
|
190
|
+
)
|
191
|
+
return config
|
@@ -0,0 +1,10 @@
|
|
1
|
+
from keras_hub.src.api_export import keras_hub_export
|
2
|
+
from keras_hub.src.models.causal_lm_preprocessor import CausalLMPreprocessor
|
3
|
+
from keras_hub.src.models.qwen3.qwen3_backbone import Qwen3Backbone
|
4
|
+
from keras_hub.src.models.qwen3.qwen3_tokenizer import Qwen3Tokenizer
|
5
|
+
|
6
|
+
|
7
|
+
@keras_hub_export("keras_hub.models.Qwen3CausalLMPreprocessor")
|
8
|
+
class Qwen3CausalLMPreprocessor(CausalLMPreprocessor):
|
9
|
+
backbone_cls = Qwen3Backbone
|
10
|
+
tokenizer_cls = Qwen3Tokenizer
|