Skip to content

Commit

Permalink
Exception Handled for non-qnn setup
Browse files Browse the repository at this point in the history
Signed-off-by: Abukhoyer Shaik <[email protected]>
  • Loading branch information
abukhoy committed Jan 16, 2025
1 parent 8ffbba1 commit 54b0f18
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 176 deletions.
20 changes: 5 additions & 15 deletions QEfficient/transformers/models/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import hashlib
import logging
import os
import warnings
from pathlib import Path
from typing import List, Optional, Union
Expand All @@ -24,7 +23,7 @@
from QEfficient.transformers.models.pytorch_transforms import CustomOpsTransform, KVCacheTransform, SpDTransform
from QEfficient.transformers.quantizers.auto import QEFF_AUTO_QUANTIZATION_CONFIG_MAPPING, with_replaced_quantizers
from QEfficient.transformers.quantizers.quant_transforms import AwqToMatmulNbitsTransform, GPTQToMatmulNbitsTransform
from QEfficient.utils import constants, create_and_dump_configs, get_padding_shape_from_config
from QEfficient.utils import constants, create_and_dump_qconfigs, get_padding_shape_from_config
from QEfficient.utils.cache import to_hashable

logger = logging.getLogger(__file__)
Expand Down Expand Up @@ -382,25 +381,16 @@ def compile(
**compiler_options,
)

# Construct the qconfig json file path
qconfig_file_path = os.path.join(os.path.dirname(self.qpc_path), "qconfig.json")
# Construct the qconfig json file
huggingface_config = self.model.config.__dict__

pytorch_transforms = [cls.__name__ for cls in self._pytorch_transforms]
onnx_transforms = [cls.__name__ for cls in self._onnx_transforms]

onnx_path = str(self.onnx_path)
specializations_file_path = str(os.path.join(os.path.dirname(self.qpc_path), "specializations.json"))
compile_dir = str(os.path.dirname(self.qpc_path))

create_and_dump_configs(
qconfig_file_path,
specializations_file_path,
create_and_dump_qconfigs(
qpc_path,
onnx_path,
huggingface_config,
pytorch_transforms,
onnx_transforms,
onnx_path,
compile_dir,
prefill_seq_len,
ctx_len,
batch_size,
Expand Down
2 changes: 1 addition & 1 deletion QEfficient/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from QEfficient.utils._utils import ( # noqa: F401
check_and_assign_cache_dir,
create_and_dump_configs,
create_and_dump_qconfigs,
get_num_layers_from_config,
get_onnx_dir_name,
get_padding_shape_from_config,
Expand Down
51 changes: 30 additions & 21 deletions QEfficient/utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,12 @@ def create_json(file_path: str, json_data: object):
print(f"Failed to create JSON File {file_path}: {e}")


def create_and_dump_configs(
config_file_path,
specializations_file_path,
def create_and_dump_qconfigs(
qpc_path,
onnx_path,
huggingface_config,
pytorch_transforms,
onnx_transforms,
onnx_path,
compile_dir,
prefill_seq_len,
ctx_len,
batch_size,
Expand All @@ -418,13 +416,36 @@ def create_and_dump_configs(
enable_qnn,
qnn_config,
):
"""
This Method creates a JSON file which contains all the configs for a model.
Such as huggingface configs, QEff transforms, QAIC sdk version, QNN sdk, compilation dir, qpc dir and
many other compilation options.
"""
qconfig_file_path = os.path.join(os.path.dirname(qpc_path), "qconfig.json")
onnx_path = str(onnx_path)
specializations_file_path = str(os.path.join(os.path.dirname(qpc_path), "specializations.json"))
compile_dir = str(os.path.dirname(qpc_path))
qnn_config_path = (
(qnn_config if qnn_config is not None else "QEfficient/compile/qnn_config.json") if enable_qnn else None
)

# Extract QAIC SDK Apps Version from SDK XML file
try:
tree = ET.parse(Constants.SDK_APPS_XML)
root = tree.getroot()
qaic_version = root.find(".//base_version").text
except (FileNotFoundError, ET.ParseError, AttributeError):
except Exception as e:
print(f"Failed to open XML File {Constants.SDK_APPS_XML}: {e}")
qaic_version = None

# Extract QNN SDK details from YAML file
try:
yaml_file_path = os.path.join(os.getenv(QnnConstants.QNN_SDK_PATH_ENV_VAR_NAME), "sdk.yaml")
with open(yaml_file_path, "r") as file:
yaml_data = yaml.safe_load(file)
except Exception:
yaml_data = None

# Ensure all objects in the configs dictionary are JSON serializable
def make_serializable(obj):
if isinstance(obj, (int, float, str, bool, type(None))):
Expand All @@ -436,18 +457,7 @@ def make_serializable(obj):
else:
return str(obj)

qnn_config_path = (
(qnn_config if qnn_config is not None else "QEfficient/compile/qnn_config.json") if enable_qnn else None
)
yaml_file_path = os.path.join(os.getenv(QnnConstants.QNN_SDK_PATH_ENV_VAR_NAME), "sdk.yaml")
yaml_data = {}
try:
with open(yaml_file_path, "r") as file:
yaml_data = yaml.safe_load(file)
except Exception:
yaml_data = None

configs = {
qconfigs = {
"huggingface_config": make_serializable(huggingface_config),
"qpc_config": {
"QEff_config": {
Expand Down Expand Up @@ -477,7 +487,6 @@ def make_serializable(obj):
}

if yaml_data:
configs["qpc_config"]["qnn_config"].update(yaml_data)
qconfigs["qpc_config"]["qnn_config"].update(yaml_data)

with open(config_file_path, "w") as file:
json.dump(configs, file, indent=4)
create_json(qconfig_file_path, qconfigs)
Loading

0 comments on commit 54b0f18

Please sign in to comment.