Skip to content

Cleaned and Rebased PR for (#481) to change the hash creation module … #537

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
55 changes: 29 additions & 26 deletions QEfficient/base/modeling_qeff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#
# ----------------------------------------------------------------------------

import hashlib
import inspect
import logging
import shutil
Expand All @@ -22,8 +21,16 @@
from QEfficient.base.pytorch_transforms import PytorchTransform
from QEfficient.compile.qnn_compiler import compile as qnn_compile
from QEfficient.generation.cloud_infer import QAICInferenceSession
from QEfficient.utils import constants, create_json, dump_qconfig, generate_mdp_partition_config, load_json
from QEfficient.utils.cache import QEFF_HOME, to_hashable
from QEfficient.utils import (
constants,
create_json,
create_model_params,
dump_qconfig,
export_wrapper,
generate_mdp_partition_config,
hash_dict_params,
load_json,
)

logger = logging.getLogger(__name__)

Expand All @@ -45,12 +52,16 @@ class QEFFBaseModel(ABC):
def _transform_names(cls) -> List[str]:
return [x.__name__ for x in cls._pytorch_transforms + cls._onnx_transforms]

def __init__(self, model: torch.nn.Module) -> None:
def __init__(self, model: torch.nn.Module, **kwargs) -> None:
super().__init__()
self.model = model
self.hash_params = create_model_params(self, **kwargs)
self.onnx_path: Optional[str] = None
self.qpc_path: Optional[str] = None
self.qpc_session: Optional[QAICInferenceSession] = None
self.model_architecture = (
(arch := getattr(self.model.config, "architectures", None)) and len(arch) > 0 and arch[0]
) or None

# Apply the transformations
any_transformed = False
Expand All @@ -67,10 +78,6 @@ def __init__(self, model: torch.nn.Module) -> None:
@abstractmethod
def model_name(self) -> str: ...

@property
@abstractmethod
def model_hash(self) -> str: ...

@abstractmethod
def export(self, export_dir: Optional[str] = None) -> Path:
"""
Expand Down Expand Up @@ -114,6 +121,7 @@ def compile(self, *args, **kwargs) -> Path:
:str: Path of the compiled ``qpc`` package.
"""

@export_wrapper
def _export(
self,
example_inputs: Dict[str, torch.Tensor],
Expand All @@ -134,8 +142,6 @@ def _export(
:onnx_transform_kwargs (dict): Additional arguments to be passed to `Transform.apply` for this class.
:export_dir (str): Specify the export directory. The export_dir will be suffixed with a hash corresponding to current model.
"""
export_dir = Path(export_dir or (QEFF_HOME / self.model_name))
export_dir = export_dir.with_name(export_dir.name + "-" + self.model_hash)
onnx_path = export_dir / f"{self.model_name}.onnx"
if onnx_path.is_file():
self.onnx_path = onnx_path
Expand Down Expand Up @@ -299,23 +305,16 @@ def _compile(
else:
mdp_ts_json = None

compile_hash = hashlib.sha256(to_hashable(command))

if specializations is not None:
compile_hash.update(to_hashable(specializations))

if custom_io is not None:
compile_hash.update(to_hashable(custom_io))

if num_speculative_tokens:
compile_hash.update(to_hashable({"num_speculative_tokens": num_speculative_tokens}))

# Hash the MDP partition config and the number of devices.
compile_hash.update(to_hashable(mdp_ts_json))
compile_hash.update(to_hashable({"mdp_ts_num_devices": mdp_ts_num_devices}))
compile_hash_params = {
"command": command,
"specializations": specializations,
"custom_io": custom_io,
"mdp_ts_num_devices": mdp_ts_num_devices,
"mdp_ts_json": mdp_ts_json,
"num_speculative_tokens": num_speculative_tokens,
}
compile_hash = hash_dict_params(compile_hash_params)

# Check if already compiled
compile_hash = compile_hash.hexdigest()[:16]
compile_dir = qpc_path.with_name(qpc_path.name + "-" + compile_hash)
qpc_path = compile_dir / "qpc"
qpc_path.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -366,6 +365,10 @@ def _compile(
]
)
)
# Dump JSON file with hashed parameters
hashed_compile_params_path = compile_dir / "hashed_compile_params.json"
create_json(hashed_compile_params_path, compile_hash_params)
logger.info("Hashed parameters exported successfully.")

self.qpc_path = qpc_path

Expand Down
2 changes: 1 addition & 1 deletion QEfficient/compile/qnn_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from typing import Dict, List, Optional

from QEfficient.utils._utils import create_json, execute_command, load_json
from QEfficient.utils.cache import to_hashable
from QEfficient.utils.constants import QnnConstants
from QEfficient.utils.generate_qnn_network_specialization_config import (
generate_data_format_config,
generate_qnn_specialization,
)
from QEfficient.utils.hash_utils import to_hashable
from QEfficient.utils.logging_utils import logger


Expand Down
2 changes: 1 addition & 1 deletion QEfficient/peft/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from QEfficient.transformers.models.pytorch_transforms import CustomOpsTransform, KVCacheTransform
from QEfficient.utils import constants
from QEfficient.utils._utils import get_padding_shape_from_config
from QEfficient.utils.cache import to_hashable
from QEfficient.utils.hash_utils import to_hashable

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion QEfficient/peft/lora/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from QEfficient import QEFFAutoModelForCausalLM
from QEfficient.peft.lora.pytorch_transforms import LoraModelInputsTransform, TargetModulesTransform
from QEfficient.utils import constants, get_padding_shape_from_config
from QEfficient.utils.cache import to_hashable
from QEfficient.utils.hash_utils import to_hashable
from QEfficient.utils.logging_utils import logger


Expand Down
Loading
Loading