-
Notifications
You must be signed in to change notification settings - Fork 974
Expand file tree
/
Copy path__init__.py
More file actions
604 lines (536 loc) · 18.3 KB
/
__init__.py
File metadata and controls
604 lines (536 loc) · 18.3 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
# Copyright (c) Qualcomm Innovation Center, Inc.
# All rights reserved
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import os
from abc import ABC
from dataclasses import dataclass
from enum import Enum
from functools import partial
from typing import Callable, Dict, Optional, Sequence, Type, Union
from executorch.examples.models.codegen import (
convert_weights as convert_codegen_weights,
)
from executorch.examples.models.gemma import convert_weights as convert_gemma_weights
from executorch.examples.models.gemma2 import convert_weights as convert_gemma2_weights
from executorch.examples.models.gemma3 import convert_weights as convert_gemma3_weights
from executorch.examples.models.glm import convert_weights as convert_glm_weights
from executorch.examples.models.granite import (
convert_weights as convert_granite_weights,
)
from executorch.examples.models.granite_speech import (
convert_weights as convert_granite_speech_weights,
)
from executorch.examples.models.internvl3 import (
convert_weights as convert_internvl3_weights,
)
from executorch.examples.models.phi_4_mini import (
convert_weights as convert_phi_4_mini_weights,
)
from executorch.examples.models.qwen2_5 import (
convert_weights as convert_qwen2_5_weights,
)
from executorch.examples.models.qwen3 import convert_weights as convert_qwen3_weights
from executorch.examples.models.smollm2 import (
convert_weights as convert_smollm2_weights,
)
from executorch.examples.models.smollm3 import (
convert_weights as convert_smollm3_weights,
)
from executorch.examples.models.smolvlm import (
convert_weights as convert_smolvlm_weights,
)
from executorch.examples.qualcomm.oss_scripts.llama.decoder_constants import (
AUDIO_ENCODER,
DECODER_MODEL_VERSION,
VISION_ENCODER,
)
from executorch.examples.qualcomm.oss_scripts.llama.encoder.encoder_config import (
AudioModalityConfig,
GraniteSpeechEncoder,
InternVL3Encoder,
MultiModalityConfig,
SmolVLMEncoder,
VisionModalityConfig,
)
from executorch.examples.qualcomm.oss_scripts.llama.model.static_llama import (
LlamaModel,
LlamaModelWithoutEmbedding,
MultiScopeAwareLlamaModel,
)
from executorch.examples.qualcomm.oss_scripts.llama.static_llm_quant_recipe import (
CodegenQuantRecipe,
Gemma2QuantRecipe,
Gemma3QuantRecipe,
Gemma_2BQuantRecipe,
GLM_1_5B_InstructQuantRecipe,
Granite_3_3_2B_InstructQuantRecipe,
GraniteSpeech_3_3_2B_InstructQuantRecipe,
InternVL3_1B_QuantRecipe,
Llama3_1BQuantRecipe,
Llama3_3BQuantRecipe,
LlamaStories110MQuantRecipe,
LlamaStories260KQuantRecipe,
Phi4MiniQuantRecipe,
Qwen2_5_0_5BQuantRecipe,
Qwen2_5_1_5BQuantRecipe,
Qwen3_0_6BQuantRecipe,
Qwen3_1_7BQuantRecipe,
Smollm2QuantRecipe,
Smollm3QuantRecipe,
SmolVLMQuantRecipe,
StaticLLMQuantRecipe,
)
from tabulate import tabulate
BASE_DIR = os.path.dirname(__file__)
LLM_VARIANT_ARCHS: Dict[str, LlamaModel] = {
"gemma3-1b": MultiScopeAwareLlamaModel,
"granite_speech_3_3-2b": LlamaModelWithoutEmbedding,
"smolvlm_500m_instruct": LlamaModelWithoutEmbedding,
"internvl3_1b": LlamaModelWithoutEmbedding,
"gemma2-2b": MultiScopeAwareLlamaModel,
}
@dataclass(init=False, frozen=True)
class LLMModelConfig(ABC):
"""
Base class for all LLM Models, including native Llama and Hugging Face Models.
This class stores configs for each supported LLM model, including quant config
repo_id: Hugging Face Repo ID.
params_path: Path to model's config.json. If the corresponding .json has not yet exist, please create one.
convert_weights: Used to convert Hugging Face weights parameters to Static Decoder's parameter naming.
decoder_model_version: This is to determine the chat template to use during runtime(qnn_llama_runner).
transform_weight: Set to true to change Hugging Face weight to improve the performance of RoPE in HTP backend.
instruct_model: True if the model uses chat templates. Check Hugging Face model card to ensure the model uses chat templates.
num_sharding: Specify the number of splits by inserting the fallback custom op. The graph will be split evenly by layers.
masked_softmax: The MaskedSoftmax feature is designed to optimize the LLMs accuracy and performance executed on HTP backend.
MaskedSoftmax is used to replace the Softmax(Add(In, Mask)) structure in attention block in LLMs during backend optimization.
For more details, please refer to QNN documents. Note that it is only supported starting from QNN 2.35.
seq_mse_candidates: Number of steps to sequentially search for optimum scales for quantized parameters which will minimize
the MSE of activation value between floating point golden & fake quantization.
r1: Enable SpinQuant R1 quantization optimization.
r2: Enable SpinQuant R2 quantization optimization.
r3: Enable SpinQuant R3 quantization optimization.
quant_recipe: Quantization recipe to use when setting quant configs for the model.
"""
repo_id: str
params_path: str
convert_weights: Callable
# TODO: Replace decoder_model_version with chat_template.jinja
decoder_model_version: str
transform_weight: bool
instruct_model: bool
num_sharding: int
masked_softmax: bool
seq_mse_candidates: int
r1: bool
r2: bool
r3: bool
quant_recipe: StaticLLMQuantRecipe
def __str__(self): # noqa: C901
"""
Visualize the current LLMModelConfig settings in a readable table format.
This method helps users quickly inspect key configuration,
skipping internal or irrelevant attributes and formatting complex types
like functions, enums, and partials for clarity.
Returns:
str: A table showing the current config for LLM models.
"""
def format_value(v):
if isinstance(v, partial):
func_name = (
v.func.__name__ if hasattr(v.func, "__name__") else str(v.func)
)
return f"partial({func_name})"
elif isinstance(v, Callable):
return v.__name__ if hasattr(v, "__name__") else str(v)
elif isinstance(v, Enum):
return f"{v.__class__.__name__}.{v.name}"
elif isinstance(v, (tuple, list)):
return "(" + ", ".join(format_value(i) for i in v) + ")"
elif isinstance(v, (str, int, float, bool)):
return v
else:
return f"<{v.__class__.__name__}>"
attrs = {}
for k in dir(self):
if k.startswith("_") or k in {"convert_weights", "params_path"}:
continue
try:
v = getattr(self, k)
if k in {"get_kv_io_bit_width", "get_logits_output_bit_width"}:
v = v()
except Exception:
v = f"Warning: failed to retrieve config for '{k}'"
if isinstance(v, (str, int, float, bool, tuple, list, Callable)):
attrs[k] = format_value(v)
table = [(k, v) for k, v in attrs.items()]
return tabulate(table, headers=["Config", "Value"], tablefmt="grid")
SUPPORTED_LLM_MODELS: Dict[str, LLMModelConfig] = {}
def register_llm_model(
name: str,
modality_encoders: Optional[
Union[MultiModalityConfig, Sequence[MultiModalityConfig]]
] = None,
):
def decorator(cls: Type[LLMModelConfig]):
cls.decoder_model_version = DECODER_MODEL_VERSION[name]
encs = (
modality_encoders
if isinstance(modality_encoders, (list, tuple))
else (modality_encoders,)
)
for enc in encs:
if enc is None:
continue
if issubclass(enc, AudioModalityConfig):
setattr(cls, AUDIO_ENCODER, enc)
elif issubclass(enc, VisionModalityConfig):
setattr(cls, VISION_ENCODER, enc)
else:
raise ValueError(f"Unsupported encoder type {enc} for model {name}.")
SUPPORTED_LLM_MODELS[name.lower()] = cls()
return cls()
return decorator
@register_llm_model("stories260k")
@dataclass(init=False, frozen=True)
class LlamaStories260K(LLMModelConfig):
repo_id = None
params_path = None
convert_weights = None
transform_weight = True
instruct_model = False
num_sharding = 1
masked_softmax = False
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = LlamaStories260KQuantRecipe
@register_llm_model("stories110m")
@dataclass(init=False, frozen=True)
class LlamaStories110M(LLMModelConfig):
repo_id = None
params_path = None
convert_weights = None
transform_weight = True
instruct_model = False
num_sharding = 1
masked_softmax = False
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = LlamaStories110MQuantRecipe
@register_llm_model("llama3_2-1b_instruct")
@dataclass(init=False, frozen=True)
class Llama3_2_1B_Instruct(LLMModelConfig):
repo_id = None
params_path = None
convert_weights = None
transform_weight = True
# The Llama3_2 enabled should be instruct, however, Llama's tokenizer does not provide utility to apply chat template.
instruct_model = False
num_sharding = 1
masked_softmax = False
seq_mse_candidates = 50
r1 = False
r2 = False
r3 = False
quant_recipe = Llama3_1BQuantRecipe
@register_llm_model("llama3_2-3b_instruct")
@dataclass(init=False, frozen=True)
class Llama3_2_3B_Instruct(LLMModelConfig):
repo_id = None
params_path = None
convert_weights = None
transform_weight = True
# The Llama3_2 enabled should be instruct, however, Llama's tokenizer does not provide utility to apply chat template.
instruct_model = False
num_sharding = 4
masked_softmax = False
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Llama3_3BQuantRecipe
@register_llm_model("codegen2_1b")
@dataclass(init=False, frozen=True)
class Codegen(LLMModelConfig):
repo_id: str = "Salesforce/codegen2-1B_P"
params_path: str = os.path.join(
BASE_DIR, "../../../models/codegen/config/config.json"
)
convert_weights = convert_codegen_weights
transform_weight = True
instruct_model = False
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = CodegenQuantRecipe
@register_llm_model("gemma-2b")
@dataclass(init=False, frozen=True)
class Gemma_2B(LLMModelConfig):
repo_id: str = "google/gemma-2b-it"
params_path: str = os.path.join(
BASE_DIR, "../../../models/gemma/config/2b_config.json"
)
convert_weights = convert_gemma_weights
transform_weight = False
instruct_model = True
num_sharding = 4
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Gemma_2BQuantRecipe
@register_llm_model("gemma2-2b")
@dataclass(init=False, frozen=True)
class Gemma2(LLMModelConfig):
repo_id: str = "google/gemma-2-2b-it"
params_path: str = os.path.join(
BASE_DIR, "../../../models/gemma2/config/2b_config.json"
)
convert_weights = convert_gemma2_weights
transform_weight = False
instruct_model = True
num_sharding = 4
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Gemma2QuantRecipe
@register_llm_model("gemma3-1b")
@dataclass(init=False, frozen=True)
class Gemma3(LLMModelConfig):
repo_id: str = "google/gemma-3-1b-it"
params_path: str = os.path.join(
BASE_DIR, "../../../models/gemma3/config/1b_config.json"
)
convert_weights = convert_gemma3_weights
transform_weight = False
instruct_model = True
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Gemma3QuantRecipe
@register_llm_model("glm-1_5b")
@dataclass(init=False, frozen=True)
class GLM_1_5B(LLMModelConfig):
repo_id: str = "THUDM/glm-edge-1.5b-chat"
params_path: str = os.path.join(
BASE_DIR, "../../../models/glm/config/1_5b_config.json"
)
convert_weights = convert_glm_weights
transform_weight = True
instruct_model = True
num_sharding = 1
group_size = 32
masked_softmax = False
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = GLM_1_5B_InstructQuantRecipe
@register_llm_model("granite_3_3-2b_instruct")
@dataclass(init=False, frozen=True)
class Granite_3_3_2b_Instruct(LLMModelConfig):
repo_id: str = "ibm-granite/granite-3.3-2b-instruct"
params_path: str = os.path.join(
BASE_DIR, "../../../models/granite/config/2b_config.json"
)
convert_weights = convert_granite_weights
transform_weight = False
instruct_model = True
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Granite_3_3_2B_InstructQuantRecipe
@register_llm_model(
"granite_speech_3_3-2b",
modality_encoders=GraniteSpeechEncoder,
)
@dataclass(init=False, frozen=True)
class GraniteSpeech_3_3_2b(LLMModelConfig):
repo_id: str = "ibm-granite/granite-speech-3.3-2b"
params_path: str = os.path.join(
BASE_DIR, "../../../models/granite_speech/config/2b_config.json"
)
convert_weights = convert_granite_speech_weights
transform_weight = False
instruct_model = True
num_sharding = 4
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = GraniteSpeech_3_3_2B_InstructQuantRecipe
@register_llm_model("phi_4_mini")
@dataclass(init=False, frozen=True)
class Phi4Mini(LLMModelConfig):
repo_id: str = "microsoft/Phi-4-mini-instruct"
params_path: str = os.path.join(
BASE_DIR, "../../../models/phi_4_mini/config/config.json"
)
convert_weights = convert_phi_4_mini_weights
transform_weight = False
instruct_model = True
num_sharding = 8
masked_softmax = False
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Phi4MiniQuantRecipe
@register_llm_model("qwen2_5-0_5b")
@dataclass(init=False, frozen=True)
class Qwen2_5_0_5B(LLMModelConfig):
repo_id: str = "Qwen/Qwen2.5-0.5B"
params_path: str = os.path.join(
BASE_DIR, "../../../models/qwen2_5/config/0_5b_config.json"
)
convert_weights = convert_qwen2_5_weights
transform_weight = False
instruct_model = False
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = True
quant_recipe = Qwen2_5_0_5BQuantRecipe
@register_llm_model("qwen2_5-1_5b")
@dataclass(init=False, frozen=True)
class Qwen2_5_1_5B(LLMModelConfig):
repo_id: str = "Qwen/Qwen2.5-1.5B"
params_path: str = os.path.join(
BASE_DIR, "../../../models/qwen2_5/config/1_5b_config.json"
)
convert_weights = convert_qwen2_5_weights
transform_weight = False
instruct_model = False
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = True
quant_recipe = Qwen2_5_1_5BQuantRecipe
@register_llm_model("qwen3-0_6b")
@dataclass(init=False, frozen=True)
class Qwen3_0_6B(LLMModelConfig):
repo_id: str = "Qwen/Qwen3-0.6B"
params_path: str = os.path.join(
BASE_DIR, "../../../models/qwen3/config/0_6b_config.json"
)
convert_weights = convert_qwen3_weights
transform_weight = False
instruct_model = True
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 50
r1 = False
r2 = False
r3 = False
quant_recipe = Qwen3_0_6BQuantRecipe
@register_llm_model("qwen3-1_7b")
@dataclass(init=False, frozen=True)
class Qwen3_1_7B(LLMModelConfig):
repo_id: str = "Qwen/Qwen3-1.7B"
params_path: str = os.path.join(
BASE_DIR, "../../../models/qwen3/config/1_7b_config.json"
)
convert_weights = convert_qwen3_weights
transform_weight = False
instruct_model = True
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = True
quant_recipe = Qwen3_1_7BQuantRecipe
@register_llm_model("smollm2_135m")
@dataclass(init=False, frozen=True)
class Smollm2_135M(LLMModelConfig):
repo_id: str = "HuggingFaceTB/SmolLM2-135M-Instruct"
params_path: str = os.path.join(
BASE_DIR, "../../../models/smollm2/135M_config.json"
)
convert_weights = convert_smollm2_weights
transform_weight = True
instruct_model = True
num_sharding = 1
masked_softmax = False
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = True
quant_recipe = Smollm2QuantRecipe
@register_llm_model("smollm3-3b")
@dataclass(init=False, frozen=True)
class Smollm3_3B(LLMModelConfig):
repo_id: str = "HuggingFaceTB/SmolLM3-3B"
params_path: str = os.path.join(BASE_DIR, "../../../models/smollm3/3b_config.json")
convert_weights = convert_smollm3_weights
transform_weight = False
instruct_model = True
num_sharding = 4
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = Smollm3QuantRecipe
@register_llm_model(
"internvl3_1b",
modality_encoders=InternVL3Encoder,
)
@dataclass(init=False, frozen=True)
class InternVL3_1B(LLMModelConfig):
repo_id: str = "OpenGVLab/InternVL3-1B-hf"
params_path: str = os.path.join(
BASE_DIR, "../../../models/internvl3/1b_config.json"
)
convert_weights = convert_internvl3_weights
transform_weight = False
instruct_model = True
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = InternVL3_1B_QuantRecipe
@register_llm_model(
"smolvlm_500m_instruct",
modality_encoders=SmolVLMEncoder,
)
@dataclass(init=False, frozen=True)
class SmolVLM_500M(LLMModelConfig):
repo_id: str = "HuggingFaceTB/SmolVLM-500M-Instruct"
params_path: str = os.path.join(
BASE_DIR, "../../../models/smolvlm/500M_config.json"
)
convert_weights = convert_smolvlm_weights
transform_weight = False
instruct_model = True
num_sharding = 1
masked_softmax = True
seq_mse_candidates = 0
r1 = False
r2 = False
r3 = False
quant_recipe = SmolVLMQuantRecipe