Skip to content

Commit 522ed49

Browse files
authored
Qualcomm AI Engine Direct - Fix HF Qwen (pytorch#20317)
### Summary Mainline HF Qwen is broken. This needs to be resolved in order to enable HF LLM flow in Optimum ExecuTorch ### Test plan Passings e2e test in test_qnn_delegate.py
1 parent 55c54c7 commit 522ed49

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

examples/qualcomm/oss_scripts/llm_utils/decoder_model_wrapper.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ def save_config_to_constant_methods(
3838
# Check for cache_config and its attributes
3939
cache_config = getattr(generation_config, "cache_config", None)
4040
if cache_config is not None:
41-
max_seq_len = getattr(cache_config, "max_cache_len", None)
41+
if isinstance(cache_config, dict):
42+
max_seq_len = cache_config.get("max_cache_len", None)
43+
else:
44+
max_seq_len = getattr(cache_config, "max_cache_len", None)
4245
if max_seq_len is not None:
4346
metadata["get_max_seq_len"] = max_seq_len
4447

@@ -115,7 +118,7 @@ def _qnn_attention_mask(
115118

116119
# Simplest and most efficient way to obtain a causal mask
117120
causal_mask = kv_arange <= reshaped_cache_position
118-
atten_mask = torch.full((causal_mask.shape[0], kv_length), torch.tensor(-65504.0))
121+
atten_mask = torch.full((causal_mask.shape[0], kv_length), -65504.0)
119122
atten_mask = atten_mask.masked_fill(causal_mask, 0)
120123
atten_mask = atten_mask[None, None, :, :].expand(batch_size, -1, -1, -1)
121124

@@ -133,7 +136,7 @@ def __init__(self, model):
133136
logging.info(f"Metadata to be recorded in PTE: {self._metadata}")
134137
self.exportable_module = TorchExportableModuleForDecoderOnlyLM(
135138
self.model,
136-
max_batch_size=1,
139+
batch_size=1,
137140
max_cache_len=self._metadata.get("get_max_seq_len"),
138141
)
139142
self._register_attention_mask_for_4_53(self.exportable_module)
@@ -154,7 +157,9 @@ def get_example_inputs(self):
154157
return (example_input_ids, example_cache_position)
155158

156159
def forward(self, input_ids: torch.Tensor, cache_position: torch.Tensor):
157-
return self.exportable_module(input_ids, cache_position)
160+
return self.exportable_module(
161+
input_ids=input_ids, cache_position=cache_position
162+
)
158163

159164
def get_metadata(self):
160165
return self._metadata

examples/qualcomm/oss_scripts/qwen2_5/qwen2_5.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import torch
1616
from executorch.backends.qualcomm.export_utils import (
17-
get_backend_type,
1817
QnnConfig,
1918
setup_common_args_and_variables,
2019
SimpleADB,
@@ -75,7 +74,7 @@ def compile(args: argparse.Namespace, qnn_config: QnnConfig): # noqa: C901
7574
args.calibration_limit,
7675
args.prompt,
7776
tokenizer_json_path,
78-
get_backend_type(qnn_config.backend),
77+
qnn_config.backend,
7978
qnn_config.soc_model,
8079
)
8180

@@ -158,7 +157,7 @@ def post_process():
158157
runner="examples/models/llama/llama_main",
159158
)
160159
# No pregen inputs, input_list is not required
161-
adb.push(inputs=[], input_list="", files=[tokenizer_json_path])
160+
adb.push(inputs=[], files=[tokenizer_json_path])
162161
adb.execute(custom_runner_cmd=runner_cmd)
163162

164163
adb.pull(host_output_path=args.artifact, callback=post_process)

0 commit comments

Comments
 (0)