-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmodels.py
More file actions
629 lines (532 loc) · 23.9 KB
/
models.py
File metadata and controls
629 lines (532 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
"""
Three model variants for Qwen3-Next experiment:
1. BaselineQwen3: Standard Qwen3-Next
2. DSAQwen3: All attention replaced with DeepSeek Sparse Attention
3. HybridQwen3: DSA for full_attention, Gated DeltaNet for linear_attention
"""
import torch
import torch.nn as nn
from typing import Optional, Tuple
import sys
import os
# Add root to path
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, root_dir)
# Import Qwen3-Next components
from models.qwen3_next.modular_qwen3_next import (
Qwen3NextModel,
Qwen3NextForCausalLM,
Qwen3NextDecoderLayer,
Qwen3NextAttention,
Qwen3NextGatedDeltaNet,
Qwen3NextRMSNorm,
Qwen3NextMLP,
Qwen3NextSparseMoeBlock,
Qwen3NextDynamicCache,
Qwen3NextRotaryEmbedding,
)
from models.qwen3_next.configuration_qwen3_next import Qwen3NextConfig
# Import DeepSeek Sparse Attention
from experiments.exp2_attention_mechanisms.exp1_sparse_vs_classic_attention.sparse_attention import (
DeepSeekSparseAttention,
LightningIndexer,
)
from transformers.cache_utils import Cache
from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
from transformers.processing_utils import Unpack
class DSADecoderLayer(nn.Module):
"""
Decoder layer that uses DeepSeek Sparse Attention for ALL attention
(replaces both full_attention and linear_attention)
"""
def __init__(self, config: Qwen3NextConfig, layer_idx: int):
super().__init__()
self.hidden_size = config.hidden_size
self.layer_idx = layer_idx
# Use DeepSeek Sparse Attention
self.self_attn = DeepSeekSparseAttention(
d_model=config.hidden_size,
n_heads=config.num_attention_heads,
max_seq_len=config.max_position_embeddings,
indexer_heads=getattr(config, 'indexer_heads', 4),
indexer_dim=getattr(config, 'indexer_dim', 64),
sparse_top_k=getattr(config, 'sparse_top_k', 512),
dropout=config.attention_dropout,
)
# MLP (same as Qwen3-Next)
if (layer_idx not in config.mlp_only_layers) and (
config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0
):
self.mlp = Qwen3NextSparseMoeBlock(config)
else:
self.mlp = Qwen3NextMLP(config, intermediate_size=config.intermediate_size)
self.input_layernorm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.post_attention_layernorm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
def forward(
self,
hidden_states: torch.Tensor,
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
) -> torch.FloatTensor:
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
# DeepSeek Sparse Attention
hidden_states, _ = self.self_attn(hidden_states)
hidden_states = residual + hidden_states
# MLP
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
if isinstance(hidden_states, tuple):
hidden_states, _ = hidden_states
hidden_states = residual + hidden_states
return hidden_states
class HybridDecoderLayer(nn.Module):
"""
Hybrid decoder layer:
- full_attention layers use DeepSeek Sparse Attention
- linear_attention layers use Gated DeltaNet (original)
"""
def __init__(self, config: Qwen3NextConfig, layer_idx: int):
super().__init__()
self.hidden_size = config.hidden_size
self.layer_idx = layer_idx
self.layer_type = config.layer_types[layer_idx]
# Token mixer - choose based on layer type
if self.layer_type == "linear_attention":
# Keep original Gated DeltaNet
self.linear_attn = Qwen3NextGatedDeltaNet(config, layer_idx)
elif self.layer_type == "full_attention":
# Replace with DeepSeek Sparse Attention
self.self_attn = DeepSeekSparseAttention(
d_model=config.hidden_size,
n_heads=config.num_attention_heads,
max_seq_len=config.max_position_embeddings,
indexer_heads=getattr(config, 'indexer_heads', 4),
indexer_dim=getattr(config, 'indexer_dim', 64),
sparse_top_k=getattr(config, 'sparse_top_k', 512),
dropout=config.attention_dropout,
)
# MLP (same as Qwen3-Next)
if (layer_idx not in config.mlp_only_layers) and (
config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0
):
self.mlp = Qwen3NextSparseMoeBlock(config)
else:
self.mlp = Qwen3NextMLP(config, intermediate_size=config.intermediate_size)
self.input_layernorm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.post_attention_layernorm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
def forward(
self,
hidden_states: torch.Tensor,
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs: Unpack[FlashAttentionKwargs],
) -> torch.FloatTensor:
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
# Token Mixer - based on layer type
if self.layer_type == "linear_attention":
hidden_states = self.linear_attn(
hidden_states=hidden_states,
cache_params=past_key_values,
cache_position=cache_position,
attention_mask=attention_mask,
)
elif self.layer_type == "full_attention":
# Use DeepSeek Sparse Attention
hidden_states, _ = self.self_attn(hidden_states)
hidden_states = residual + hidden_states
# MLP
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
if isinstance(hidden_states, tuple):
hidden_states, _ = hidden_states
hidden_states = residual + hidden_states
return hidden_states
# ==================== Model Variants ====================
class BaselineQwen3(Qwen3NextForCausalLM):
"""
Variant 1: Standard Qwen3-Next (no changes)
Uses original full_attention and linear_attention layers
"""
pass # No changes needed - use as-is
class DSAQwen3Model(nn.Module):
"""
Variant 2: All attention layers replaced with DeepSeek Sparse Attention
"""
def __init__(self, config: Qwen3NextConfig):
super().__init__()
self.config = config
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, config.pad_token_id)
# Replace all layers with DSA decoder layers
self.layers = nn.ModuleList(
[DSADecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
)
self.norm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.rotary_emb = Qwen3NextRotaryEmbedding(config=config)
self.gradient_checkpointing = False
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
):
if (input_ids is None) ^ (inputs_embeds is not None):
raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids)
if cache_position is None:
past_seen_tokens = 0
cache_position = torch.arange(
past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
)
if position_ids is None:
position_ids = cache_position.unsqueeze(0)
hidden_states = inputs_embeds
position_embeddings = self.rotary_emb(hidden_states, position_ids)
for decoder_layer in self.layers:
hidden_states = decoder_layer(
hidden_states,
position_embeddings=position_embeddings,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
cache_position=cache_position,
**kwargs,
)
hidden_states = self.norm(hidden_states)
return type('ModelOutput', (), {
'last_hidden_state': hidden_states,
'past_key_values': past_key_values,
})()
class DSAQwen3(nn.Module):
"""Variant 2: DSA-Only Qwen3 (for CausalLM)"""
def __init__(self, config: Qwen3NextConfig):
super().__init__()
self.config = config
self.model = DSAQwen3Model(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
labels: Optional[torch.LongTensor] = None,
**kwargs,
):
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, **kwargs)
logits = self.lm_head(outputs.last_hidden_state)
loss = None
if labels is not None:
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
return type('CausalLMOutput', (), {
'loss': loss,
'logits': logits,
'past_key_values': outputs.past_key_values,
})()
class HybridQwen3Model(nn.Module):
"""
Variant 3: Hybrid model
- full_attention → DeepSeek Sparse Attention
- linear_attention → Gated DeltaNet (original)
"""
def __init__(self, config: Qwen3NextConfig):
super().__init__()
self.config = config
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, config.pad_token_id)
# Use hybrid decoder layers
self.layers = nn.ModuleList(
[HybridDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
)
self.norm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.rotary_emb = Qwen3NextRotaryEmbedding(config=config)
self.gradient_checkpointing = False
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
):
if (input_ids is None) ^ (inputs_embeds is not None):
raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids)
if use_cache and past_key_values is None:
past_key_values = Qwen3NextDynamicCache(config=self.config)
if cache_position is None:
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
cache_position = torch.arange(
past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
)
if position_ids is None:
position_ids = cache_position.unsqueeze(0)
hidden_states = inputs_embeds
position_embeddings = self.rotary_emb(hidden_states, position_ids)
for decoder_layer in self.layers:
hidden_states = decoder_layer(
hidden_states,
position_embeddings=position_embeddings,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
cache_position=cache_position,
**kwargs,
)
hidden_states = self.norm(hidden_states)
return type('ModelOutput', (), {
'last_hidden_state': hidden_states,
'past_key_values': past_key_values,
})()
class HybridQwen3(nn.Module):
"""Variant 3: Hybrid Qwen3 (for CausalLM)"""
def __init__(self, config: Qwen3NextConfig):
super().__init__()
self.config = config
self.model = HybridQwen3Model(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
labels: Optional[torch.LongTensor] = None,
**kwargs,
):
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, **kwargs)
logits = self.lm_head(outputs.last_hidden_state)
loss = None
if labels is not None:
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
return type('CausalLMOutput', (), {
'loss': loss,
'logits': logits,
'past_key_values': outputs.past_key_values,
})()
class EnhancedDecoderLayer(nn.Module):
"""
Enhanced decoder layer that supports all three layer types:
- full_attention: Standard Qwen3 attention
- linear_attention: Gated DeltaNet
- dsa_attention: DeepSeek Sparse Attention
"""
def __init__(self, config: Qwen3NextConfig, layer_idx: int):
super().__init__()
self.hidden_size = config.hidden_size
self.layer_idx = layer_idx
self.layer_type = config.layer_types[layer_idx]
# Token mixer - choose based on layer type
if self.layer_type == "linear_attention":
self.linear_attn = Qwen3NextGatedDeltaNet(config, layer_idx)
elif self.layer_type == "full_attention":
self.self_attn = Qwen3NextAttention(config, layer_idx)
elif self.layer_type == "dsa_attention":
self.dsa_attn = DeepSeekSparseAttention(
d_model=config.hidden_size,
n_heads=config.num_attention_heads,
max_seq_len=config.max_position_embeddings,
indexer_heads=getattr(config, 'indexer_heads', 4),
indexer_dim=getattr(config, 'indexer_dim', 64),
sparse_top_k=getattr(config, 'sparse_top_k', 512),
dropout=config.attention_dropout,
)
else:
raise ValueError(f"Unknown layer type: {self.layer_type}")
# MLP (same as Qwen3-Next)
if (layer_idx not in config.mlp_only_layers) and (
config.num_experts > 0 and (layer_idx + 1) % config.decoder_sparse_step == 0
):
self.mlp = Qwen3NextSparseMoeBlock(config)
else:
self.mlp = Qwen3NextMLP(config, intermediate_size=config.intermediate_size)
self.input_layernorm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.post_attention_layernorm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
def forward(
self,
hidden_states: torch.Tensor,
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs: Unpack[FlashAttentionKwargs],
) -> torch.FloatTensor:
residual = hidden_states
hidden_states = self.input_layernorm(hidden_states)
# Token Mixer - based on layer type
if self.layer_type == "linear_attention":
hidden_states = self.linear_attn(
hidden_states=hidden_states,
cache_params=past_key_values,
cache_position=cache_position,
attention_mask=attention_mask,
)
elif self.layer_type == "full_attention":
hidden_states, _ = self.self_attn(
hidden_states=hidden_states,
position_embeddings=position_embeddings,
attention_mask=attention_mask,
past_key_values=past_key_values,
cache_position=cache_position,
**kwargs,
)
elif self.layer_type == "dsa_attention":
hidden_states, _ = self.dsa_attn(hidden_states)
hidden_states = residual + hidden_states
# MLP
residual = hidden_states
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states)
if isinstance(hidden_states, tuple):
hidden_states, _ = hidden_states
hidden_states = residual + hidden_states
return hidden_states
class EnhancedQwen3NextModel(nn.Module):
"""Enhanced Qwen3 model supporting full_attention, linear_attention, and dsa_attention"""
def __init__(self, config: Qwen3NextConfig):
super().__init__()
self.config = config
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, config.pad_token_id)
# Use enhanced decoder layers
self.layers = nn.ModuleList(
[EnhancedDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
)
self.norm = Qwen3NextRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
self.rotary_emb = Qwen3NextRotaryEmbedding(config=config)
self.gradient_checkpointing = False
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Cache] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
use_cache: Optional[bool] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
):
if (input_ids is None) ^ (inputs_embeds is not None):
raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
if inputs_embeds is None:
inputs_embeds = self.embed_tokens(input_ids)
if use_cache and past_key_values is None:
past_key_values = Qwen3NextDynamicCache(config=self.config)
if cache_position is None:
past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
cache_position = torch.arange(
past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
)
if position_ids is None:
position_ids = cache_position.unsqueeze(0)
hidden_states = inputs_embeds
position_embeddings = self.rotary_emb(hidden_states, position_ids)
for decoder_layer in self.layers:
hidden_states = decoder_layer(
hidden_states,
position_embeddings=position_embeddings,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_values=past_key_values,
cache_position=cache_position,
**kwargs,
)
hidden_states = self.norm(hidden_states)
return type('ModelOutput', (), {
'last_hidden_state': hidden_states,
'past_key_values': past_key_values,
})()
class EnhancedQwen3NextForCausalLM(nn.Module):
"""Enhanced CausalLM supporting full_attention, linear_attention, and dsa_attention"""
def __init__(self, config: Qwen3NextConfig):
super().__init__()
self.config = config
self.model = EnhancedQwen3NextModel(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
labels: Optional[torch.LongTensor] = None,
**kwargs,
):
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, **kwargs)
logits = self.lm_head(outputs.last_hidden_state)
loss = None
if labels is not None:
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
return type('CausalLMOutput', (), {
'loss': loss,
'logits': logits,
'past_key_values': outputs.past_key_values,
})()
def create_model(variant: str, config):
"""
Factory function to create model variants
Args:
variant: One of ["baseline", "dsa", "hybrid"]
config: ExperimentConfig
Returns:
Model instance
"""
# Convert ExperimentConfig to Qwen3NextConfig
qwen_config = Qwen3NextConfig(
vocab_size=config.vocab_size,
hidden_size=config.hidden_size,
num_hidden_layers=config.num_hidden_layers,
num_attention_heads=config.num_attention_heads,
num_key_value_heads=config.num_key_value_heads,
intermediate_size=config.intermediate_size,
max_position_embeddings=config.max_position_embeddings,
rope_theta=config.rope_theta,
attention_dropout=config.attention_dropout,
hidden_dropout_prob=config.hidden_dropout,
rms_norm_eps=config.rms_norm_eps,
layer_types=config.layer_types,
linear_num_value_heads=config.linear_num_value_heads,
linear_num_key_heads=config.linear_num_key_heads,
linear_key_head_dim=config.linear_key_head_dim,
linear_value_head_dim=config.linear_value_head_dim,
linear_conv_kernel_dim=config.linear_conv_kernel_dim,
num_experts=config.num_experts,
num_local_experts=config.num_experts, # Add for Mixtral compatibility
router_jitter_noise=0.0, # Add for Mixtral compatibility
decoder_sparse_step=config.decoder_sparse_step,
mlp_only_layers=config.mlp_only_layers,
# DSA specific
indexer_heads=config.indexer_heads,
indexer_dim=config.indexer_dim,
sparse_top_k=config.sparse_top_k,
)
if variant == "baseline":
return BaselineQwen3(qwen_config)
elif variant == "dsa":
return DSAQwen3(qwen_config)
elif variant == "hybrid":
return HybridQwen3(qwen_config)
else:
raise ValueError(f"Unknown variant: {variant}. Choose from: baseline, dsa, hybrid")