Skip to content

Commit fcc1020

Browse files
Qualcomm AI Engine Direct - improve llama3.2 3B TPS (#20903)
### **User description** ### Summary - Update num_sharding from 4 to 3, TPS change from 26.27 to 28.95. - Remove dummy Quantize/Dequantize due to tag_ios failure + remove dummy mul 1, TPS change from 28.95 to 29.34. - Minor fix before change: <img width="991" height="242" alt="image" src="https://github.com/user-attachments/assets/0d04eeb9-0934-405e-a21c-e2bf31a77211" /> after change: <img width="1045" height="242" alt="image" src="https://github.com/user-attachments/assets/b7992e63-1677-4e8a-a974-3702e4d12d4c" /> #### Minor fix - Update cli tool option - Update unit test for spill_fill for newer version of sdk (in newer version of sdk, the spill_fill become 0 and fail the unit test) - Update unit test root (examples... -> executorch.examples...) ### Test plan ``` python ./examples/qualcomm/oss_scripts/llama/llama.py --artifact llama3_2_3b_instruct --build_folder build-android --soc_model SM8850 --checkpoint ~/.llama/checkpoints/Llama3.2-3B-Instruct/consolidated.00.pth --params ~/.llama/checkpoints/Llama3.2-3B-Instruct/params.json --tokenizer_model ~/.llama/checkpoints/Llama3.2-3B-Instruct/tokenizer.model --prompt "I would like to learn python, could you teach me with a simple example?" --temperature 0 --decoder_model llama3_2-3b_instruct --model_mode kv --max_seq_len 4096 --device ef5e4029 --host localhost --system_prompt "You are a helpful assistant." --max_context_len 4096 --calib_tasks wikitext ```
1 parent 5828778 commit fcc1020

7 files changed

Lines changed: 33 additions & 16 deletions

File tree

backends/qualcomm/debugger/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import shutil
55
import subprocess
66
import tempfile
7-
from pathlib import Path
87
from typing import Sequence, Tuple
98

109
import executorch.backends.qualcomm.python.PyQnnManagerAdaptor as PyQnnManager
@@ -211,7 +210,7 @@ def __init__(
211210
self.adb = adb
212211
self.sample_input = sample_input
213212
self.build_folder = build_folder
214-
self.root = str(Path(__file__).resolve().parents[3])
213+
self.root = os.getcwd()
215214
self.config = {
216215
"backend_extension_config": {
217216
"backend_extensions": {

backends/qualcomm/tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ def forward(self, x):
15321532
class LargeTensorLinear(torch.nn.Module):
15331533
def __init__(self):
15341534
super().__init__()
1535-
hidden_dim = 8192
1535+
hidden_dim = 16384
15361536
self.linear1_1 = torch.nn.Linear(512, hidden_dim)
15371537
self.linear1_2 = torch.nn.Linear(512, hidden_dim)
15381538
self.linear1_3 = torch.nn.Linear(512, hidden_dim)

backends/qualcomm/tests/test_qnn_delegate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10470,7 +10470,7 @@ def test_cli(self):
1047010470
cmds = [
1047110471
"python",
1047210472
"-m",
10473-
"examples.qualcomm.util_scripts.cli",
10473+
"executorch.examples.qualcomm.util_scripts.cli",
1047410474
"quantize",
1047510475
"--artifact",
1047610476
f"{tmp_dir}/relu.pt2",
@@ -10489,7 +10489,7 @@ def test_cli(self):
1048910489
cmds = [
1049010490
"python",
1049110491
"-m",
10492-
"examples.qualcomm.util_scripts.cli",
10492+
"executorch.examples.qualcomm.util_scripts.cli",
1049310493
"compile",
1049410494
"--artifact",
1049510495
f"{tmp_dir}/q_out/relu_quantized.pt2",
@@ -10507,7 +10507,7 @@ def test_cli(self):
1050710507
cmds = [
1050810508
"python",
1050910509
"-m",
10510-
"examples.qualcomm.util_scripts.cli",
10510+
"executorch.examples.qualcomm.util_scripts.cli",
1051110511
"execute",
1051210512
"--artifact",
1051310513
f"{tmp_dir}/c_out/relu_quantized.pte",
@@ -10551,7 +10551,7 @@ def test_cli_with_input_list_assignment(self):
1055110551
cmds = [
1055210552
"python",
1055310553
"-m",
10554-
"examples.qualcomm.util_scripts.cli",
10554+
"executorch.examples.qualcomm.util_scripts.cli",
1055510555
"quantize",
1055610556
"--artifact",
1055710557
f"{tmp_dir}/sub.pt2",
@@ -10570,7 +10570,7 @@ def test_cli_with_input_list_assignment(self):
1057010570
cmds = [
1057110571
"python",
1057210572
"-m",
10573-
"examples.qualcomm.util_scripts.cli",
10573+
"executorch.examples.qualcomm.util_scripts.cli",
1057410574
"compile",
1057510575
"--artifact",
1057610576
f"{tmp_dir}/q_out/sub_quantized.pt2",
@@ -10588,7 +10588,7 @@ def test_cli_with_input_list_assignment(self):
1058810588
cmds = [
1058910589
"python",
1059010590
"-m",
10591-
"examples.qualcomm.util_scripts.cli",
10591+
"executorch.examples.qualcomm.util_scripts.cli",
1059210592
"execute",
1059310593
"--artifact",
1059410594
f"{tmp_dir}/c_out/sub_quantized.pte",

examples/qualcomm/oss_scripts/llama/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class Llama3_2_3B_Instruct(LLMModelConfig):
281281
transform_weight = True
282282
# The Llama3_2 enabled should be instruct, however, Llama's tokenizer does not provide utility to apply chat template.
283283
instruct_model = False
284-
num_sharding = 4
284+
num_sharding = 3
285285
masked_softmax = False
286286
seq_mse_candidates = 0
287287
r1 = False

examples/qualcomm/oss_scripts/llama/model/static_llama.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def forward(
730730
self.freqs_sin[input_pos][0] if self.use_kv_cache else self.freqs_sin
731731
)
732732

733-
hidden_states = self.embedding_scale_factor * self.tok_embeddings(tokens)
733+
hidden_states = self.tok_embeddings(tokens)
734734

735735
for ind, decoder_layer in enumerate(self.layers):
736736
k_caches = None
@@ -865,7 +865,11 @@ def forward(
865865
self.freqs_sin[input_pos][0] if self.use_kv_cache else self.freqs_sin
866866
)
867867

868-
hidden_states = self.embedding_scale_factor * hidden_states
868+
hidden_states = (
869+
self.embedding_scale_factor * hidden_states
870+
if self.embedding_scale_factor != 1.0
871+
else hidden_states
872+
)
869873

870874
for ind, decoder_layer in enumerate(self.layers):
871875
k_caches = None
@@ -1024,7 +1028,8 @@ def forward(
10241028
else self.local_freqs_sin
10251029
)
10261030

1027-
hidden_states = self.embedding_scale_factor * self.tok_embeddings(tokens)
1031+
hidden_states = self.tok_embeddings(tokens)
1032+
10281033
for ind, decoder_layer in enumerate(self.layers):
10291034
k_caches = None
10301035
v_caches = None

examples/qualcomm/oss_scripts/llama/wrappers/llm_wrappers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ def _prepare_model(self): # noqa: C901
241241
# Llama does x.to(float16) * w whilst Gemma3 is (x * w).to(float16)
242242
# See https://github.com/huggingface/transformers/pull/29402
243243
state_dict[k] = v.float() + torch.ones(v.shape, dtype=torch.float32)
244+
for k, v in state_dict.items():
245+
if "tok_embeddings.weight" == k:
246+
state_dict[k] = v.float() * self.model_args.embedding_scale_factor
244247
else:
245248
state_dict = torch.load(
246249
self.control_args.checkpoint,
@@ -466,9 +469,7 @@ def _tag_ios(self, node, fixed_point_type):
466469
(self.meta["get_ar_len"], self.meta["get_head_dim"] // 2),
467470
}
468471

469-
freq_op = {
470-
exir_ops.edge.aten.select.int,
471-
}
472+
freq_op = {exir_ops.edge.aten.select.int, exir_ops.edge.aten.select_copy.int}
472473
quant_io_type = None
473474

474475
if node.op == "placeholder":

examples/qualcomm/util_scripts/cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ def quantize(args):
166166
quant_dtype=quant_dtype,
167167
per_channel_conv=args.per_channel,
168168
per_channel_linear=args.per_row,
169+
per_channel_embedding=args.per_channel_embedding,
170+
act_symmetric=args.act_symmetric,
169171
act_observer=act_observer,
170172
backend=get_backend_type(args.backend),
171173
soc_model=args.soc_model,
@@ -499,6 +501,16 @@ def main():
499501
action="store_true",
500502
help="Use per_row encoding for operator linear.",
501503
)
504+
sub_quantize.add_argument(
505+
"--per_channel_embedding",
506+
action="store_true",
507+
help="Use per_channel encoding for operator embedding.",
508+
)
509+
sub_quantize.add_argument(
510+
"--act_symmetric",
511+
action="store_true",
512+
help="Use symmetric quantization for activations.",
513+
)
502514
sub_quantize.add_argument(
503515
"--activation_observer",
504516
type=str,

0 commit comments

Comments
 (0)