Skip to content

Commit 7b9cfba

Browse files
committed
Modifications to the flow of hash creation and filtration of params for export
Signed-off-by: Dhiraj Kumar Sah <[email protected]>
1 parent 0fc5f3b commit 7b9cfba

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

QEfficient/base/modeling_qeff.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@
2222
from QEfficient.base.pytorch_transforms import PytorchTransform
2323
from QEfficient.compile.qnn_compiler import compile as qnn_compile
2424
from QEfficient.generation.cloud_infer import QAICInferenceSession
25+
<<<<<<< HEAD
2526
from QEfficient.utils import constants, create_json, dump_qconfig, generate_mdp_partition_config, load_json
2627
from QEfficient.utils.cache import QEFF_HOME, to_hashable
28+
=======
29+
from QEfficient.utils import (
30+
constants,
31+
create_json,
32+
dump_qconfig,
33+
filter_and_create_export_hash,
34+
hash_compile_params,
35+
)
36+
from QEfficient.utils.cache import QEFF_HOME
37+
>>>>>>> dd35ad1 (Modifications to the flow of hash creation and filtration of params for export)
2738

2839
logger = logging.getLogger(__name__)
2940

@@ -50,13 +61,12 @@ def create_model_params(self, **kwargs) -> Dict:
5061

5162
model_params["config"] = self.model.config.to_diff_dict()
5263
model_params["_transform_names"] = self._transform_names()
53-
# TODO: Add keywords list to filter out params that are not needed for hashing
5464
return model_params
5565

5666
def __init__(self, model: torch.nn.Module, **kwargs) -> None:
5767
super().__init__()
5868
self.model = model
59-
self.model_params = self.create_model_params(**kwargs)
69+
self.hash_params = self.create_model_params(**kwargs)
6070

6171
if hasattr(self.model.config, "architectures"):
6272
self.model_architecture = self.model.config.architectures[0]
@@ -123,7 +133,6 @@ def compile(self, *args, **kwargs) -> Path:
123133
:str: Path of the compiled ``qpc`` package.
124134
"""
125135

126-
# @dump_model_params
127136
def _export(
128137
self,
129138
example_inputs: Dict[str, torch.Tensor],
@@ -146,8 +155,8 @@ def _export(
146155
"""
147156

148157
export_dir = Path(export_dir or (QEFF_HOME / self.model_architecture / self.model_name))
149-
export_hash, hashed_params = filter_and_hash_export_params(
150-
model_params=copy.deepcopy(self.model_params),
158+
export_hash = filter_and_create_export_hash(
159+
model_params=self.hash_params,
151160
output_names=output_names,
152161
dynamic_axes=dynamic_axes,
153162
export_kwargs=export_kwargs,
@@ -232,7 +241,7 @@ def _export(
232241

233242
# Dump JSON file with hashed parameters
234243
hashed_params_export_path = export_dir / "hashed_model_params.json"
235-
create_json(hashed_params_export_path, hashed_params)
244+
create_json(hashed_params_export_path, self.hash_params)
236245
logger.info("Hashed parameters exported successfully.")
237246

238247
self.onnx_path = onnx_path
@@ -307,6 +316,7 @@ def _compile(
307316
continue
308317
command.append(f"{option}={value}")
309318

319+
<<<<<<< HEAD
310320
# Create a dummy mdp_ts_json if mdp-load-partition-config not provided and num_devices > 1
311321
if mdp_ts_json_path is not None:
312322
mdp_ts_json = load_json(str(mdp_ts_json_path))
@@ -335,6 +345,15 @@ def _compile(
335345
# Check if already compiled
336346
compile_hash = hash_dict_params(self.compile_params)
337347
compile_hash = compile_hash.hexdigest()[:16]
348+
=======
349+
compile_hash, hashed_params = hash_compile_params(
350+
command=command,
351+
specializations=specializations,
352+
custom_io=custom_io,
353+
mdp_ts_num_devices=mdp_ts_num_devices,
354+
num_speculative_tokens=num_speculative_tokens,
355+
)
356+
>>>>>>> dd35ad1 (Modifications to the flow of hash creation and filtration of params for export)
338357
compile_dir = qpc_path.with_name(qpc_path.name + "-" + compile_hash)
339358

340359
qpc_path = compile_dir / "qpc"

QEfficient/transformers/models/modeling_auto.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(self, model: nn.Module, **kwargs) -> None:
7373
):
7474
raise AssertionError("Please use `from_pretrained` method to load quantized models")
7575

76-
super().__init__(model)
76+
super().__init__(model, **kwargs)
7777

7878
def __repr__(self) -> str:
7979
return self.__class__.__name__ + "\n" + self.model.__repr__()
@@ -174,7 +174,7 @@ def __init__(self, model: nn.Module, pooling=None, **kwargs):
174174
self.model, _ = PoolingTransform.apply(self.model, pooling)
175175

176176
self.model.base_model.config.use_cache = True
177-
self.model_params["qeff_class"] = self.__class__.__name__
177+
self.hash_params["qeff_class"] = self.__class__.__name__
178178

179179
@classmethod
180180
@with_replaced_quantizers
@@ -435,7 +435,7 @@ class QEffVisionEncoderForTextImageToTextModel(QEFFBaseModel):
435435
def __init__(self, model: nn.modules, **kwargs):
436436
super().__init__(model, **kwargs)
437437
self.model = model.get_qeff_vision_encoder()
438-
self.model_params["qeff_class"] = self.__class__.__name__
438+
self.hash_params["qeff_class"] = self.__class__.__name__
439439

440440
def export(self, inputs, output_names, dynamic_axes, export_dir=None):
441441
return self._export(inputs, output_names, dynamic_axes, export_dir)
@@ -490,7 +490,7 @@ class QEffCausalLMForTextImageToTextModel(QEFFBaseModel):
490490
def __init__(self, model, **kwargs):
491491
super().__init__(model, **kwargs)
492492
self.model = model.get_qeff_language_decoder()
493-
self.model_params["qeff_class"] = self.__class__.__name__
493+
self.hash_params["qeff_class"] = self.__class__.__name__
494494

495495
def export(self, inputs, output_names, dynamic_axes, export_dir=None):
496496
return self._export(inputs, output_names, dynamic_axes, export_dir)
@@ -543,8 +543,8 @@ def __init__(
543543
raise NotImplementedError("Continuous batching is not supported for image-text-to-text models yet.")
544544
self.model = model
545545
self.config = model.config
546-
self.vision_model = QEffVisionEncoderForTextImageToTextModel(model)
547-
self.lang_model = QEffCausalLMForTextImageToTextModel(model)
546+
self.vision_model = QEffVisionEncoderForTextImageToTextModel(model, **kwargs)
547+
self.lang_model = QEffCausalLMForTextImageToTextModel(model, **kwargs)
548548
self.input_shapes, self.output_names = None, None
549549

550550
@property
@@ -916,7 +916,7 @@ def __init__(
916916
self.model.config.vision_config.use_flash_attn = "false"
917917
else:
918918
self.model.config.text_config.use_cache = True
919-
self.model_params["qeff_class"] = self.__class__.__name__
919+
self.hash_params["qeff_class"] = self.__class__.__name__
920920

921921
@classmethod
922922
def from_pretrained(
@@ -940,10 +940,6 @@ def from_pretrained(
940940
model = cls._hf_auto_class.from_pretrained(pretrained_model_name_or_path, config, *args, **kwargs)
941941

942942
return cls(model, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)
943-
# # Bypass __call__ and manually initialize
944-
# instance = object.__new__(cls)
945-
# instance.__init__(model, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)
946-
# return instance
947943

948944
def export(
949945
self,
@@ -1288,11 +1284,6 @@ def from_pretrained(cls, pretrained_model_name_or_path: str, kv_offload: Optiona
12881284
model = cls._hf_auto_class.from_pretrained(pretrained_model_name_or_path, **kwargs)
12891285
return cls(model, kv_offload=kv_offload, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)
12901286

1291-
# # Bypass __call__ and manually initialize
1292-
# instance = object.__new__(cls)
1293-
# instance.__init__(model, kv_offload=kv_offload, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)
1294-
# return instance
1295-
12961287

12971288
MISCLASSIFIED_CAUSAL_LM_TO_QEFF_AUTO_CLASS_MAP = {"InternVLChatModel": QEFFAutoModelForImageTextToText}
12981289

@@ -1376,7 +1367,11 @@ def __init__(
13761367
self.pretrained_model_name_or_path = kwargs.get("pretrained_model_name_or_path", None)
13771368
self.model, transformed = SpDTransform.apply(self.model, qaic_config, **kwargs)
13781369
self.is_tlm = transformed
1370+
<<<<<<< HEAD
13791371

1372+
=======
1373+
self.hash_params["qeff_class"] = self.__class__.__name__
1374+
>>>>>>> dd35ad1 (Modifications to the flow of hash creation and filtration of params for export)
13801375
# ---Sampling---
13811376
# Note: SamplerTransform should be applied after all other transforms
13821377
# are done. The role of the sampler is to just add nodes at the output of the
@@ -1917,7 +1912,7 @@ def __init__(self, model: nn.Module, **kwargs):
19171912
super().__init__(model, **kwargs)
19181913
self.model.config.use_cache = True
19191914
self.num_layers = model.config.num_hidden_layers
1920-
self.model_params["qeff_class"] = self.__class__.__name__
1915+
self.hash_params["qeff_class"] = self.__class__.__name__
19211916

19221917
@property
19231918
def get_model_config(self) -> dict:

QEfficient/utils/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
check_and_assign_cache_dir,
1414
create_json,
1515
custom_format_warning,
16-
dump_model_params,
1716
dump_qconfig,
17+
<<<<<<< HEAD
1818
generate_mdp_partition_config,
19+
=======
20+
filter_and_create_export_hash,
21+
>>>>>>> dd35ad1 (Modifications to the flow of hash creation and filtration of params for export)
1922
get_num_layers_from_config,
2023
get_num_layers_vlm,
2124
get_onnx_dir_name,
@@ -24,6 +27,7 @@
2427
get_qpc_dir_path,
2528
get_sliding_window_layers,
2629
get_sliding_window_shapes,
30+
hash_compile_params,
2731
hf_download,
2832
load_hf_processor,
2933
load_hf_tokenizer,

0 commit comments

Comments
 (0)