Skip to content

prefill model #5807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions backends/qualcomm/quantizer/custom_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,29 @@ def annotate_matmul_input1(node: Node, quantization_config: QuantizationConfig):
if "SDPA" in full_qualified_name:
annotate_matmul(node, quantization_config_16a8w)
annotate_matmul_input1(node.args[1], quantization_config_8a8w)


def custom_annotate_matmul_16a8w(gm: torch.fx.GraphModule):
"""
Annotate matmul op with 16a8w quantization config
"""

def annotate_matmul(node: Node, quantization_config: QuantizationConfig):
input_qspec_map = {}
input_act = node.args[0]
input_spec = quantization_config.input_activation
input_qspec_map[input_act] = input_spec
input_act1 = node.args[1]
input_spec1 = quantization_config.weight
input_qspec_map[input_act1] = input_spec1
node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation(
input_qspec_map=input_qspec_map,
output_qspec=quantization_config.output_activation,
_annotated=True,
)

# Annotate 16a8w for matmul op to get better performance
quantization_config_16a8w = get_16a8w_qnn_ptq_config()
for node in gm.graph.nodes:
if node.op == "call_function" and node.target == torch.ops.aten.matmul.default:
annotate_matmul(node, quantization_config_16a8w)
2 changes: 2 additions & 0 deletions backends/qualcomm/serialization/qnn_compile_spec_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class QcomChipset(IntEnum):
SM8475 = 42 # v69
SM8550 = 43 # v73
SM8650 = 57 # v75
SSG2115P = 46 # v73... I wish I can know where the number comes from...


@dataclass
Expand All @@ -47,6 +48,7 @@ class SocInfo:
QcomChipset.SM8475: SocInfo(QcomChipset.SM8475, HtpInfo(HtpArch.V69, 8)),
QcomChipset.SM8550: SocInfo(QcomChipset.SM8550, HtpInfo(HtpArch.V73, 8)),
QcomChipset.SM8650: SocInfo(QcomChipset.SM8650, HtpInfo(HtpArch.V75, 8)),
QcomChipset.SSG2115P: SocInfo(QcomChipset.SSG2115P, HtpInfo(HtpArch.V73, 2)),
}


Expand Down
3 changes: 2 additions & 1 deletion backends/qualcomm/serialization/schema.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum QcomChipset: int {
SM8450 = 36,
SM8475 = 42,
SM8550 = 43,
SSG2115P = 46,
SM8650 = 57,
}

Expand Down Expand Up @@ -170,7 +171,7 @@ table QnnExecuTorchOptions {

/// Profiling level of the delegate and the backend. Default is off.
profile_level:QnnExecuTorchProfileLevel;

/// Enables usage of shared buffer between application and backend for graph I/O.
shared_buffer:bool;

Expand Down
1 change: 1 addition & 0 deletions backends/qualcomm/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class TestQNN(unittest.TestCase):
model: QcomChipset = None
compiler_specs: List[CompileSpec] = None
arch_table = {
"SSG2115P": QcomChipset.SSG2115P,
"SM8650": QcomChipset.SM8650,
"SM8550": QcomChipset.SM8550,
"SM8475": QcomChipset.SM8475,
Expand Down
55 changes: 27 additions & 28 deletions examples/models/llama2/export_llama_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,23 @@
get_quant_embedding_transform,
get_quant_weight_transform,
)
from .source_transformation.quantized_kv_cache import (
replace_kv_cache_with_quantized_kv_cache,
)

# from .source_transformation.quantized_kv_cache import (
# replace_kv_cache_with_quantized_kv_cache,
# )
from .source_transformation.rms_norm import replace_rms_norm_with_native_rms_norm

from .source_transformation.rope import materialze_broadcast_of_rope_freq_cis
from .source_transformation.sdpa import (
replace_causal_mask,
replace_kv_cache_with_coreml_kv_cache,
replace_kv_cache_with_simple_kv_cache,
replace_sdpa_with_coreml_sdpa,
replace_sdpa_with_custom_op,
replace_sdpa_with_flex_sdpa,
replace_sdpa_with_simple_sdpa,
)

# from .source_transformation.sdpa import (
# replace_causal_mask,
# replace_kv_cache_with_coreml_kv_cache,
# replace_kv_cache_with_simple_kv_cache,
# replace_sdpa_with_coreml_sdpa,
# replace_sdpa_with_custom_op,
# replace_sdpa_with_flex_sdpa,
# replace_sdpa_with_simple_sdpa,
# )

IS_FBCODE = True # os.environ.get("FBCODE_PLATFORM", False)
FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
Expand Down Expand Up @@ -910,23 +912,20 @@ def _get_source_transforms( # noqa
assert args.use_kv_cache, "quantize_kv_cache requires use_kv_cache=True"
transforms.append(replace_kv_cache_with_quantized_kv_cache)

if args.qnn:
# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.qualcomm.utils.utils`
from executorch.backends.qualcomm.utils.utils import convert_linear_to_conv2d

# transforms.append(replace_kv_cache_with_simple_kv_cache)
# transforms.append(replace_sdpa_with_flex_sdpa)
# transforms.append(replace_causal_mask)
transforms.append(replace_rms_norm_with_native_rms_norm)
if args.optimized_rotation_path:
transforms.append(fuse_layer_norms)
transforms.append(get_model_with_r1_r2(args.optimized_rotation_path))
transforms.append(convert_linear_to_conv2d)
if args.use_kv_cache:
if args.qnn:
# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.qualcomm.utils.utils`
from executorch.backends.qualcomm.utils.utils import (
convert_linear_to_conv2d,
)

transforms.append(replace_kv_cache_with_simple_kv_cache)
transforms.append(replace_sdpa_with_flex_sdpa)
transforms.append(replace_causal_mask)
transforms.append(replace_rms_norm_with_native_rms_norm)
if args.optimized_rotation_path:
transforms.append(fuse_layer_norms)
transforms.append(get_model_with_r1_r2(args.optimized_rotation_path))
transforms.append(convert_linear_to_conv2d)

elif args.mps:
if args.mps:
# Currently mps doesn't support sdpa op, use the simpler decomposition
# to get free perf gain.
transforms.append(replace_sdpa_with_simple_sdpa)
Expand Down
Loading
Loading