-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathqwen.py
More file actions
598 lines (505 loc) · 25.2 KB
/
Copy pathqwen.py
File metadata and controls
598 lines (505 loc) · 25.2 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
# coding=utf-8
""" Qwen3 MOE model for NXD inference. This is a re-implementation of the NxDI source code for Qwen3 MOE, provided here for easy kernel development."""
import torch
from transformers import AutoTokenizer, GenerationConfig
from neuronx_distributed_inference.utils.hf_adapter import HuggingFaceGenerationAdapter, load_pretrained_config
from neuronx_distributed_inference.models.config import MoENeuronConfig, OnDeviceSamplingConfig
from neuronx_distributed_inference.models.qwen3_moe.modeling_qwen3_moe import Qwen3MoeInferenceConfig
torch.manual_seed(0)
import gc
import warnings
from typing import List, Optional, Tuple, Union, Dict, Any
import torch
import math
from neuronx_distributed_inference.models.model_base import NeuronBaseForCausalLM, NeuronBaseModel
from neuronx_distributed_inference.modules.attention.gqa import GQA
from neuronx_distributed_inference.modules.custom_calls import CustomRMSNorm
# Try except for the compatibility with older compiler version
try:
from neuronxcc.nki._private_kernels.attention import attention_isa_kernel
except ImportError:
from neuronxcc.nki.kernels.attention import attention_isa_kernel
from neuronx_distributed.parallel_layers import parallel_state
from neuronx_distributed.parallel_layers.layers import ColumnParallelLinear, ParallelEmbedding
from neuronx_distributed.utils import cpu_mode
from torch import nn
from torch_neuronx.xla_impl.ops import nki_jit
from transformers import Qwen3MoeForCausalLM
from transformers.generation import SampleDecoderOnlyOutput, SampleEncoderDecoderOutput
from transformers.models.qwen3_moe.modeling_qwen3_moe import Qwen3MoeRMSNorm
from neuronx_distributed_inference.models.config import InferenceConfig, MoENeuronConfig, MOE_TKG_MK_INTERMEDIATE_PER_TP
from neuronx_distributed_inference.models.model_wrapper import CONTEXT_ENCODING_MODEL_TAG, TOKEN_GENERATION_MODEL_TAG
from neuronx_distributed_inference.modules.attention.attention_base import NeuronAttentionBase
from neuronx_distributed_inference.modules.attention.utils import RotaryEmbedding
from neuronx_distributed_inference.modules.moe_v2 import initialize_moe_module
from neuronx_distributed_inference.models.layer_boundary_marker import (
ModuleMarkerEndWrapper,
ModuleMarkerStartWrapper,
)
_flash_fwd_call = nki_jit()(attention_isa_kernel)
SampleOutput = Union[SampleEncoderDecoderOutput, SampleDecoderOnlyOutput]
GQA_SHARDING_STRATEGY = GQA.REPLICATE_TO_TP_DEGREE
# Get the modules_to_not_convert from the neuron configs
def get_modules_to_not_convert(neuron_config: MoENeuronConfig):
return getattr(neuron_config, "modules_to_not_convert", None)
def _helper_concat_and_delete_qkv(qwen_state_dict: Dict[str, Any], layer_num: int, attr: str):
"""
Helper function to concatenate and delete QKV attributes for fusedqkv (weight or scale).
Args:
qwen_state_dict: The state dictionary containing model weights
layer_num: The index of the layer to process
attr: The attribute to process ('weight' or 'scale')
"""
qwen_state_dict[f"layers.{layer_num}.self_attn.Wqkv.{attr}"] = torch.cat(
[
qwen_state_dict[f"layers.{layer_num}.self_attn.q_proj.{attr}"],
qwen_state_dict[f"layers.{layer_num}.self_attn.k_proj.{attr}"],
qwen_state_dict[f"layers.{layer_num}.self_attn.v_proj.{attr}"],
],
)
del qwen_state_dict[f"layers.{layer_num}.self_attn.q_proj.{attr}"]
del qwen_state_dict[f"layers.{layer_num}.self_attn.k_proj.{attr}"]
del qwen_state_dict[f"layers.{layer_num}.self_attn.v_proj.{attr}"]
def convert_state_dict_to_fused_qkv(qwen_state_dict: Dict[str, Any], cfg: InferenceConfig):
"""
This function concats the qkv weights and scales to a Wqkv weight and scale for fusedqkv, and deletes the qkv weights.
"""
mods_to_not_conv = get_modules_to_not_convert(cfg.neuron_config)
if mods_to_not_conv is None:
mods_to_not_conv = []
for l in range(cfg.num_hidden_layers): # noqa: E741
_helper_concat_and_delete_qkv(qwen_state_dict, l, "weight")
if (
cfg.neuron_config.quantized_mlp_kernel_enabled or cfg.neuron_config.quantized
) and f"layers.{l}.self_attn" not in mods_to_not_conv:
_helper_concat_and_delete_qkv(qwen_state_dict, l, "scale")
gc.collect()
return qwen_state_dict
def maybe_dequantize_layer(neuron_state_dict, config):
scale_layers = []
for layer_key in neuron_state_dict.keys():
if "_scale_inv" in layer_key:
scales = neuron_state_dict[layer_key]
scale_layers.append(layer_key)
fp8_layer_name = layer_key.replace("_scale_inv", "")
fp8_layer = neuron_state_dict[fp8_layer_name]
block_size = config.quantization_config["weight_block_size"]
scales_expanded = scales.repeat_interleave(block_size[0], dim=0).repeat_interleave(block_size[1], dim=1)
scaled_layer = fp8_layer.to(torch.float32) * scales_expanded.to(torch.float32)
neuron_state_dict[fp8_layer_name] = scaled_layer.to(config.neuron_config.torch_dtype)
# delete scale layers
for scale_layer in scale_layers:
del neuron_state_dict[scale_layer]
def convert_qwen3_moe_hf_to_neuron_state_dict(neuron_state_dict, config):
"""
Helper function which converts the huggingface checkpoints to state dictionary compatible with the stucture of the neuron MoE model.
"""
assert config.neuron_config.glu_mlp is True, "Only GLU MLP is supported"
# dequantize layers if needed
maybe_dequantize_layer(neuron_state_dict, config)
# to facilitate rank usage in base model
neuron_state_dict["rank_util.rank"] = torch.arange(
0, config.neuron_config.tp_degree, dtype=torch.int32
)
for l in range(config.num_hidden_layers): # noqa: E741
# To facilitate rank usage in attention
neuron_state_dict[f"layers.{l}.self_attn.rank_util.rank"] = torch.arange(
0, config.neuron_config.tp_degree, dtype=torch.int32
)
# Rename the q_norm, k_norm names
neuron_state_dict[f"layers.{l}.self_attn.k_layernorm.weight"] = (
neuron_state_dict[f"layers.{l}.self_attn.k_norm.weight"].detach().clone()
)
del neuron_state_dict[f"layers.{l}.self_attn.k_norm.weight"]
# Rename the q_norm, k_norm names
neuron_state_dict[f"layers.{l}.self_attn.q_layernorm.weight"] = (
neuron_state_dict[f"layers.{l}.self_attn.q_norm.weight"].detach().clone()
)
del neuron_state_dict[f"layers.{l}.self_attn.q_norm.weight"]
# Copy router weights
neuron_state_dict[f"layers.{l}.mlp.router.linear_router.weight"] = (
neuron_state_dict[f"layers.{l}.mlp.gate.weight"].detach().clone()
)
del neuron_state_dict[f"layers.{l}.mlp.gate.weight"]
intermediate_size, hidden_size = neuron_state_dict[
f"layers.{l}.mlp.experts.0.gate_proj.weight"
].shape
device = neuron_state_dict[f"layers.{l}.mlp.experts.0.gate_proj.weight"].device
dtype = neuron_state_dict[f"layers.{l}.mlp.experts.0.gate_proj.weight"].dtype
# copy the MLP parameters
gate_up_proj = torch.empty(
config.num_experts,
hidden_size,
2 * intermediate_size,
dtype=dtype,
device=device,
)
for e in range(config.num_experts):
# Copy gate_proj and up_proj after concatenation
gate_proj_weights = (
neuron_state_dict[f"layers.{l}.mlp.experts.{e}.gate_proj.weight"]
.T.detach()
.clone()
)
up_proj_weights = (
neuron_state_dict[f"layers.{l}.mlp.experts.{e}.up_proj.weight"]
.T.detach()
.clone()
)
gate_up_proj_slice = torch.narrow(gate_up_proj, 0, e, 1)
gate_proj_slice = torch.narrow(gate_up_proj_slice, 2, 0, intermediate_size)
gate_proj_slice.copy_(gate_proj_weights)
up_proj_slice = torch.narrow(
gate_up_proj_slice, 2, intermediate_size, intermediate_size
)
up_proj_slice.copy_(up_proj_weights)
del neuron_state_dict[f"layers.{l}.mlp.experts.{e}.gate_proj.weight"]
del neuron_state_dict[f"layers.{l}.mlp.experts.{e}.up_proj.weight"]
# padding gate_up_proj on intermediate size
pad_size = getattr(config, "moe_intermediate_pad_size", 0)
if pad_size > 0:
gate_up_proj = gate_up_proj.reshape(config.num_experts, hidden_size, 2, -1)
# padding right on gate_up_proj: (num_experts, hidden_size, 2, intermediate_size)
gate_up_proj = torch.nn.functional.pad(gate_up_proj, (0, pad_size))
gate_up_proj = gate_up_proj.reshape(config.num_experts, hidden_size, -1)
neuron_state_dict[f"layers.{l}.mlp.expert_mlps.mlp_op.gate_up_proj.weight"] = gate_up_proj
down_proj = torch.empty(
config.num_experts,
intermediate_size,
hidden_size,
dtype=dtype,
device=device,
)
for e in range(config.num_experts):
# Copy down_proj
down_proj_weights = (
neuron_state_dict[f"layers.{l}.mlp.experts.{e}.down_proj.weight"]
.T.detach()
.clone()
)
down_proj_slice = torch.narrow(down_proj, 0, e, 1)
down_proj_slice.copy_(down_proj_weights)
del neuron_state_dict[f"layers.{l}.mlp.experts.{e}.down_proj.weight"]
# padding down_proj on intermediate size
if pad_size > 0:
# padding bottom on down_proj: (num_experts, intermediate_size, hidden_size)
down_proj = torch.nn.functional.pad(down_proj, (0, 0, 0, pad_size))
neuron_state_dict[f"layers.{l}.mlp.expert_mlps.mlp_op.down_proj.weight"] = down_proj
gc.collect()
if config.neuron_config.fused_qkv:
neuron_state_dict = convert_state_dict_to_fused_qkv(neuron_state_dict, config)
return neuron_state_dict
def get_rmsnorm_cls():
# Initialize to the appropriate implementation of RMSNorm
# If infer on NXD -> CustomRMSNorm
# If infer on CPU -> HF_RMSNorm (CustomRMSNorm does not work on CPU)
return Qwen3MoeRMSNorm if cpu_mode() else CustomRMSNorm
class Qwen3MoeInferenceConfig(InferenceConfig):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Qwen3-MoE config has `num_experts` instead of `num_local_experts`
# We need to add `num_local_experts` as it is expected by `initialize_moe_module`
self.num_local_experts = self.num_experts
# Qwen3-MoE has no shared experts
self.n_shared_experts = 0
# ExpertMLPsV2 reads moe_intermediate from config.intermediate_size
# check whether need to pad intermediate size
self.maybe_pad_intermediate()
# enable moe_fused_nki_kernel
self.enable_moe_fused_nki_kernel()
self.intermediate_size = self.moe_intermediate_size
# We need router dtype to be FP32 for accuracy
self.neuron_config.router_config.dtype = torch.float32
# HF uses softmax (non-configurable) act for Qwen3-MoE
self.neuron_config.router_config.act_fn = "softmax"
# Set DISABLE_NUMERIC_CC_TOKEN=1 for Qwen3 MoE as a workaround
# for the extra add/multiple in all-gather/reduce-scatter CC ops
# https://github.com/pytorch/xla/pull/3825 (openxla PR https://github.com/openxla/xla/pull/7677 not accepted)
self.neuron_config.disable_numeric_cc_token = True
# Qwen3 normalizes top k affinities
self.neuron_config.normalize_top_k_affinities = True
def maybe_pad_intermediate(self):
moe_tp_degree = self.neuron_config.moe_tp_degree
I_TP = self.moe_intermediate_size // moe_tp_degree
if getattr(self.neuron_config.blockwise_matmul_config, "use_shard_on_intermediate_dynamic_while", False):
# If shard-on-I enabled, check the intermediate size per tp is divisible by SHARD_ON_INTERMEDIATE_DIMENTION_PER_TP
if I_TP % SHARD_ON_INTERMEDIATE_DIMENTION_PER_TP != 0:
padded_moe_intermediate_size = math.ceil(I_TP / SHARD_ON_INTERMEDIATE_DIMENTION_PER_TP) * SHARD_ON_INTERMEDIATE_DIMENTION_PER_TP * moe_tp_degree
self.moe_intermediate_pad_size = max(padded_moe_intermediate_size - self.moe_intermediate_size, 0)
# set moe_intermediate_size to padded size
self.moe_intermediate_size = padded_moe_intermediate_size
def enable_moe_fused_nki_kernel(self):
I_TP = self.moe_intermediate_size // self.neuron_config.moe_tp_degree
# if moe_fused_nki_kernel_enabled is enabled and the intermeidiate_size_per_tp is divisible by MOE_TKG_MK_INTERMEDIATE_PER_TP
if getattr(self.neuron_config, "moe_fused_nki_kernel_enabled", False) and I_TP % MOE_TKG_MK_INTERMEDIATE_PER_TP == 0:
self.moe_fused_nki_kernel_enabled = True
def get_required_attributes(self) -> List[str]:
return [
"head_dim",
"hidden_act",
"hidden_size",
"max_position_embeddings",
"moe_intermediate_size",
"norm_topk_prob",
"num_attention_heads",
"num_experts",
"num_experts_per_tok",
"num_hidden_layers",
"num_key_value_heads",
"rms_norm_eps",
"rope_scaling",
"rope_theta",
"tie_word_embeddings",
"vocab_size",
]
@classmethod
def get_neuron_config_cls(cls):
return MoENeuronConfig
class NeuronQwen3MoEAttention(NeuronAttentionBase):
def __init__(self, config: Qwen3MoeInferenceConfig):
rotary_emb = RotaryEmbedding(
config.head_dim,
max_position_embeddings=config.max_position_embeddings,
base=config.rope_theta,
)
super().__init__(
config=config,
hidden_size=config.hidden_size,
num_attention_heads=config.num_attention_heads,
num_key_value_heads=config.num_key_value_heads,
head_dim=config.head_dim,
rotary_emb=rotary_emb,
rms_norm_eps=config.rms_norm_eps,
# qk_norm in the base class is different from Qwen3RMSNorm
use_qk_norm=False,
)
# Override q_layernorm and k_layernorm with RMSNorm
self.q_layernorm = get_rmsnorm_cls()(self.head_dim, self.rms_norm_eps)
self.k_layernorm = get_rmsnorm_cls()(self.head_dim, self.rms_norm_eps)
if not parallel_state.model_parallel_is_initialized():
raise ValueError(
"NeuronQwen3MoEAttention has to be initialized in a distributed env. Please use neuronx_distributed"
" module to initialize a distributed env."
)
class NeuronQwen3MoeDecoderLayer(nn.Module):
"""
Just replace the attention with the NXD version, and MLP with the NXD version
"""
def __init__(self, config: Qwen3MoeInferenceConfig, layer_idx: int):
super().__init__()
self.hidden_size = config.hidden_size
self.self_attn = NeuronQwen3MoEAttention(config=config)
self.moe_fused_nki_kernel_enabled = getattr(config, "moe_fused_nki_kernel_enabled", False)
self.input_layernorm = get_rmsnorm_cls()(
config.hidden_size,
eps=config.rms_norm_eps,
)
self.post_attention_layernorm = get_rmsnorm_cls()(
config.hidden_size,
eps=config.rms_norm_eps,
)
if self.moe_fused_nki_kernel_enabled:
self.mlp = initialize_moe_module(
config=config, rmsnorm=self.post_attention_layernorm, init_tkg_module=True
)
else:
self.mlp = initialize_moe_module(
config=config,
)
self.qkv_kernel_enabled = config.neuron_config.qkv_kernel_enabled
self.sequence_parallel_enabled = config.neuron_config.sequence_parallel_enabled
self.qkv_kernel_fused_rmsnorm = not self.sequence_parallel_enabled
self.moe_mask_padded_tokens = config.neuron_config.moe_mask_padded_tokens
def forward(
self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_value: Optional[Tuple[torch.Tensor]] = None,
padding_mask: Optional[torch.Tensor] = None,
**kwargs,
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
"""
Args:
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`torch.FloatTensor`, *optional*):
attention mask of size `(batch_size, sequence_length)` if flash attention is used or `(batch_size, 1,
query_sequence_length, key_sequence_length)` if default attention is used.
position_ids (`torch.FloatTensor`, *optional*):
position ids of size `(batch_size, sequence_length)`.
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
"""
if "padding_mask" in kwargs:
warnings.warn(
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
)
residual = hidden_states
qkv_fused_rmsnorm = None
# We wrap input_layernorm/self_attn/post_attention_layernorm with module markers start/end
# as a hint for compiler's modular-flow to avoid layer boundries in-between decoder layer components
hidden_states = ModuleMarkerStartWrapper()(hidden_states)
if self.input_layernorm:
if self.qkv_kernel_enabled and self.qkv_kernel_fused_rmsnorm:
qkv_fused_rmsnorm = self.input_layernorm
else:
hidden_states = self.input_layernorm(hidden_states)
# Self Attention
hidden_states, present_key_value, cos_cache, sin_cache = self.self_attn(
hidden_states=hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_value,
rmsnorm=qkv_fused_rmsnorm,
**kwargs,
)
hidden_states = residual + hidden_states
# MoE
residual = hidden_states
if not self.moe_fused_nki_kernel_enabled:
hidden_states = self.post_attention_layernorm(hidden_states)
hidden_states = self.mlp(hidden_states, padding_mask)[0]
hidden_states = residual + hidden_states
# End module marker
hidden_states = ModuleMarkerEndWrapper()(hidden_states)
outputs = (hidden_states, present_key_value, cos_cache, sin_cache, None)
return outputs
class NeuronQwen3MoeModel(NeuronBaseModel):
"""
NeuronQwen3MoeModel extends the Qwen3MoeModel to be traceable.
The forward function of this class is traced.
"""
def setup_attr_for_model(self, config: Qwen3MoeInferenceConfig):
self.on_device_sampling = config.neuron_config.on_device_sampling_config is not None
self.tp_degree = config.neuron_config.tp_degree
self.hidden_size = config.hidden_size
self.num_attention_heads = config.num_attention_heads
self.num_key_value_heads = config.num_key_value_heads
self.max_batch_size = config.neuron_config.max_batch_size
self.buckets = config.neuron_config.buckets
def init_model(self, config: Qwen3MoeInferenceConfig):
self.padding_idx = config.pad_token_id
self.vocab_size = config.vocab_size
self.embed_tokens = ParallelEmbedding(
config.vocab_size,
config.hidden_size,
self.padding_idx,
dtype=config.neuron_config.torch_dtype,
shard_across_embedding=True,
)
self.layers = nn.ModuleList(
[
NeuronQwen3MoeDecoderLayer(config, layer_idx)
for layer_idx in range(config.num_hidden_layers)
]
)
self.norm = get_rmsnorm_cls()(self.hidden_size, eps=config.rms_norm_eps)
self.lm_head = ColumnParallelLinear(
config.hidden_size,
config.vocab_size,
gather_output=False if self.on_device_sampling else True,
bias=False,
)
class NeuronQwen3MoeForCausalLM(NeuronBaseForCausalLM):
"""
This class can be used as Qwen3MoeForCausalLM
"""
_model_cls = NeuronQwen3MoeModel
@staticmethod
def load_hf_model(model_path, **kwargs):
return Qwen3MoeForCausalLM.from_pretrained(model_path, **kwargs)
@classmethod
def get_config_cls(cls):
return Qwen3MoeInferenceConfig
@staticmethod
def convert_hf_to_neuron_state_dict(state_dict: dict, config: Qwen3MoeInferenceConfig) -> dict:
return convert_qwen3_moe_hf_to_neuron_state_dict(state_dict, config)
# Wraps NeuronBaseForCausalLM.enable_context_encoding() to add compile_tag.
def enable_context_encoding(self):
self.compile_tag = CONTEXT_ENCODING_MODEL_TAG
super().enable_context_encoding()
# Wraps NeuronBaseForCausalLM.enable_token_generation() to add compile_tag.
def enable_token_generation(self):
self.compile_tag = TOKEN_GENERATION_MODEL_TAG
super().enable_token_generation()
def get_compiler_args(self):
# Set compiler optimization level based on model tag
if self.compile_tag == CONTEXT_ENCODING_MODEL_TAG:
optimization_level = "-O1"
elif self.compile_tag == TOKEN_GENERATION_MODEL_TAG:
# Disable Modular flow for TKG graph with EP enabled as it causes perf degradation
optimization_level = "-O3" if self.neuron_config.moe_ep_degree > 1 else "-O1"
compiler_args = f"--enable-saturate-infinity --enable-mixed-precision-accumulation --model-type transformer {optimization_level}"
# Add flags for cc-overlap
compiler_args += (
" --tensorizer-options='--enable-ccop-compute-overlap --cc-pipeline-tiling-factor=2'"
)
compiler_args += " --auto-cast=none"
# Enable vector-offset DGE
compiler_args += " --internal-enable-dge-levels vector_dynamic_offsets"
compiler_args += " --internal-hlo2tensorizer-options='--verify-hlo=true'"
if self.neuron_config.scratchpad_page_size:
compiler_args += (
f" --hbm-scratchpad-page-size={self.neuron_config.scratchpad_page_size} "
)
if self.neuron_config.attn_block_tkg_nki_kernel_enabled:
assert (
self.neuron_config.attn_block_tkg_nki_kernel_cascaded_attention
), "If using attn_block_tkg_nki_kernel_enabled for Qwen3MoE you must also use attn_block_tkg_nki_kernel_cascaded_attention"
# Enabled RMSNorm pre-RoPE in the Attn TKG MK
self.neuron_config.pre_rope_rmsnorm = True
# When enabling the Cascaded Attn TKG MK we will run over 5 million instructions on E2E
compiler_args += " --internal-max-instruction-limit=15000000"
return compiler_args
def generate(skip_compile=False):
# Initialize configs and tokenizer.
generation_config = GenerationConfig.from_pretrained(model_path)
if not skip_compile:
neuron_config = MoENeuronConfig(
tp_degree=4,
batch_size=1,
max_context_length=128,
seq_len=1024,
on_device_sampling_config=OnDeviceSamplingConfig(do_sample=True, temperature=0.6, top_k=20, top_p=0.95),
enable_bucketing=False,
flash_decoding_enabled=False
)
config = Qwen3MoeInferenceConfig(
neuron_config,
load_config=load_pretrained_config(model_path),
)
tokenizer = AutoTokenizer.from_pretrained(model_path, padding_side="right")
tokenizer.pad_token = tokenizer.eos_token
# Compile and save model.
print("\nCompiling and saving model...")
model = NeuronQwen3MoeForCausalLM(model_path, config)
model.compile(traced_model_path)
tokenizer.save_pretrained(traced_model_path)
# Load from compiled checkpoint.
print("\nLoading model from compiled checkpoint...")
model = NeuronQwen3MoeForCausalLM(traced_model_path)
model.load(traced_model_path)
tokenizer = AutoTokenizer.from_pretrained(traced_model_path)
# Generate outputs.
print("\nGenerating outputs...")
prompt = "Give me a short introduction to large language models."
messages = [
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
inputs = tokenizer([text], padding=True, return_tensors="pt")
generation_model = HuggingFaceGenerationAdapter(model)
outputs = generation_model.generate(
inputs.input_ids,
generation_config=generation_config,
attention_mask=inputs.attention_mask,
max_length=model.config.neuron_config.max_length,
)
output_tokens = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False)
print("Generated outputs:")
for i, output_token in enumerate(output_tokens):
print(f"Output {i}: {output_token}")