Skip to content

Hash bug fix pr #384

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

Merged
merged 10 commits into from
May 16, 2025
Merged
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
27 changes: 19 additions & 8 deletions QEfficient/transformers/models/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: str, *args, **kwargs):
kwargs.update({"attn_implementation": "eager", "low_cpu_mem_usage": False})

model = cls._hf_auto_class.from_pretrained(pretrained_model_name_or_path, *args, **kwargs)
return cls(model)
return cls(model, pretrained_model_name_or_path=pretrained_model_name_or_path)

@property
def model_name(self) -> str:
Expand Down Expand Up @@ -160,6 +160,7 @@ def __init__(self, model: nn.Module, **kwargs):
super().__init__(model)
self.model.config.use_cache = True
self.num_layers = model.config.num_hidden_layers
self.pretrained_model_name_or_path = kwargs.get("pretrained_model_name_or_path", None)

@classmethod
@with_replaced_quantizers
Expand Down Expand Up @@ -212,7 +213,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, *args, **kwargs):
model, kv_offload=kv_offload
)

return cls(model)
return cls(model, pretrained_model_name_or_path=pretrained_model_name_or_path)

@property
def model_hash(self) -> str:
Expand All @@ -226,6 +227,9 @@ def model_hash(self) -> str:
mhash = hashlib.sha256()
mhash.update(to_hashable(self.model.config.to_diff_dict()))
mhash.update(to_hashable(self._transform_names()))

mhash.update(to_hashable(self.pretrained_model_name_or_path))

mhash = mhash.hexdigest()[:16]
return mhash

Expand Down Expand Up @@ -441,6 +445,7 @@ def model_hash(self) -> str:
mhash.update(to_hashable(self.model.model.config.to_diff_dict()))
mhash.update(to_hashable(self._transform_names()))
mhash.update(to_hashable({"QEffVisionEncoderForTextImageToTextModel": True}))
mhash.update(to_hashable(self.model.model.pretrained_model_name_or_path))
mhash = mhash.hexdigest()[:16]
return mhash

Expand Down Expand Up @@ -504,6 +509,7 @@ def model_hash(self) -> str:
mhash.update(to_hashable(self.model.config.to_diff_dict()))
mhash.update(to_hashable(self._transform_names()))
mhash.update(to_hashable({"QEffCausalLMForTextImageToTextModel": True}))
mhash.update(to_hashable(self.model.model.pretrained_model_name_or_path))
mhash = mhash.hexdigest()[:16]
return mhash

Expand Down Expand Up @@ -531,9 +537,9 @@ def __init__(
raise NotImplementedError("Continuous batching is not supported for image-text-to-text models yet.")
self.model = model
self.config = model.config
self.model.pretrained_model_name_or_path = kwargs.get("pretrained_model_name_or_path", None)
self.vision_model = QEffVisionEncoderForTextImageToTextModel(model)
self.lang_model = QEffCausalLMForTextImageToTextModel(model)

self.input_shapes, self.output_names = None, None

@property
Expand All @@ -553,7 +559,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: str, **kwargs):

kwargs.update({"attn_implementation": "eager", "low_cpu_mem_usage": False})
model = cls._hf_auto_class.from_pretrained(pretrained_model_name_or_path, **kwargs)
return cls(model, **kwargs)
return cls(model, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)

@property
def onnx_path(self):
Expand Down Expand Up @@ -878,6 +884,7 @@ def __init__(
self.model.config.vision_config.use_flash_attn = "false"
else:
self.model.config.text_config.use_cache = True
self.pretrained_model_name_or_path = kwargs.get("pretrained_model_name_or_path", None)

@classmethod
def from_pretrained(
Expand All @@ -900,7 +907,7 @@ def from_pretrained(
config.vision_config.use_flash_attn = "false"
model = cls._hf_auto_class.from_pretrained(pretrained_model_name_or_path, config, *args, **kwargs)

return cls(model, **kwargs)
return cls(model, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)

def export(
self,
Expand Down Expand Up @@ -1139,6 +1146,7 @@ def model_hash(self) -> str:
mhash.update(to_hashable(self.model.config.to_diff_dict()))
mhash.update(to_hashable(self._transform_names()))
mhash.update(to_hashable({"QEFFAutoModelForImageTextToText1QPC": True}))
mhash.update(to_hashable(self.pretrained_model_name_or_path))
mhash = mhash.hexdigest()[:16]
return mhash

Expand Down Expand Up @@ -1254,7 +1262,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: str, kv_offload: Optiona

kwargs.update({"attn_implementation": "eager", "low_cpu_mem_usage": False})
model = cls._hf_auto_class.from_pretrained(pretrained_model_name_or_path, **kwargs)
return cls(model, kv_offload=kv_offload, **kwargs)
return cls(model, kv_offload=kv_offload, pretrained_model_name_or_path=pretrained_model_name_or_path, **kwargs)


MISCLASSIFIED_CAUSAL_LM_TO_QEFF_AUTO_CLASS_MAP = {"InternVLChatModel": QEFFAutoModelForImageTextToText}
Expand Down Expand Up @@ -1319,13 +1327,13 @@ def __init__(
)

super().__init__(model)

# Set use_cache=True to get KV values as output during ONNX export
self.model.config.use_cache = True
self.num_layers = model.config.num_hidden_layers
self.continuous_batching = continuous_batching
self.model, transformed = SpDTransform.apply(self.model, qaic_config, **kwargs)
self.is_tlm = transformed
self.pretrained_model_name_or_path = kwargs.get("pretrained_model_name_or_path", None)

@property
def model_name(self) -> str:
Expand Down Expand Up @@ -1400,11 +1408,11 @@ def from_pretrained(
return MISCLASSIFIED_CAUSAL_LM_TO_QEFF_AUTO_CLASS_MAP[model.__class__.__name__](
model, kv_offload=kv_offload
)

return cls(
model,
continuous_batching=continuous_batching,
qaic_config=qaic_config,
pretrained_model_name_or_path=pretrained_model_name_or_path,
**kwargs,
)

Expand All @@ -1416,6 +1424,7 @@ def model_hash(self) -> str:
mhash.update(to_hashable({"continuous_batching": self.continuous_batching}))
mhash.update(to_hashable({"is_tlm": self.is_tlm}))
mhash.update(to_hashable(self._transform_names()))
mhash.update(to_hashable(self.pretrained_model_name_or_path))
mhash = mhash.hexdigest()[:16]
return mhash

Expand Down Expand Up @@ -1756,6 +1765,7 @@ def __init__(self, model: nn.Module, **kwargs):
super().__init__(model)
self.model.config.use_cache = True
self.num_layers = model.config.num_hidden_layers
self.pretrained_model_name_or_path = kwargs.get("pretrained_model_name_or_path", None)

@property
def model_hash(self) -> str:
Expand All @@ -1769,6 +1779,7 @@ def model_hash(self) -> str:
mhash = hashlib.sha256()
mhash.update(to_hashable(self.model.config.to_diff_dict()))
mhash.update(to_hashable(self._transform_names()))
mhash.update(to_hashable(self.pretrained_model_name_or_path))
mhash = mhash.hexdigest()[:16]
return mhash

Expand Down
8 changes: 5 additions & 3 deletions tests/transformers/models/test_causal_lm_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def check_causal_lm_pytorch_vs_kv_vs_ort_vs_ai100(
pytorch_hf_tokens = api_runner.run_hf_model_on_pytorch(model_hf)

is_tlm = False if num_speculative_tokens is None else True
qeff_model = QEFFAutoModelForCausalLM(model_hf, is_tlm=is_tlm)
qeff_model = QEFFAutoModelForCausalLM(model_hf, is_tlm=is_tlm, pretrained_model_name_or_path=model_name)

pytorch_kv_tokens = api_runner.run_kv_model_on_pytorch(qeff_model.model)

Expand Down Expand Up @@ -183,7 +183,9 @@ def check_causal_lm_pytorch_vs_kv_vs_ort_vs_ai100(
pytorch_hf_tokens = api_runner.run_hf_model_on_pytorch_CB(model_hf)
pytorch_hf_tokens = np.vstack(pytorch_hf_tokens)

qeff_model = QEFFAutoModelForCausalLM(model_hf, continuous_batching=True, is_tlm=is_tlm)
qeff_model = QEFFAutoModelForCausalLM(
model_hf, continuous_batching=True, is_tlm=is_tlm, pretrained_model_name_or_path=model_name
)
onnx_model_path = qeff_model.export()

if not get_available_device_id():
Expand Down Expand Up @@ -219,7 +221,7 @@ def test_causal_lm_export_with_deprecated_api(model_name):
model_config["n_layer"] = 1
model, _ = load_causal_lm_model(model_config)
tokenizer = load_hf_tokenizer(pretrained_model_name_or_path=model_name)
qeff_model = QEFFAutoModelForCausalLM(model)
qeff_model = QEFFAutoModelForCausalLM(model, model_name=model_name, pretrained_model_name_or_path=model_name)
new_api_onnx_model_path = qeff_model.export()
_, old_api_onnx_model_path = qualcomm_efficient_converter(
model_name=model_name, model_kv=qeff_model, tokenizer=tokenizer
Expand Down
2 changes: 1 addition & 1 deletion tests/transformers/models/test_embedding_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def check_embed_pytorch_vs_ort_vs_ai100(
pt_outputs = pt_model(**inputs)
pt_embeddings = pt_outputs[0][0].detach().numpy()
# Pytorch transformed model
qeff_model = QEFFAutoModel(pt_model)
qeff_model = QEFFAutoModel(pt_model, pretrained_model_name_or_path=model_name)
qeff_pt_outputs = qeff_model.generate(inputs=inputs, runtime_ai100=False)
qeff_pt_embeddings = qeff_pt_outputs[0][0].detach().numpy()
mad = np.mean(np.abs(pt_embeddings - qeff_pt_embeddings))
Expand Down
2 changes: 1 addition & 1 deletion tests/transformers/models/test_speech_seq2seq_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def check_seq2seq_pytorch_vs_kv_vs_ort_vs_ai100(

pytorch_hf_tokens = run_seq2seq_pytorch_hf(model_hf, processor, data, sample_rate, ctx_len)

qeff_model = QEFFAutoModelForSpeechSeq2Seq(model_hf)
qeff_model = QEFFAutoModelForSpeechSeq2Seq(model_hf, pretrained_model_name_or_path=model_name)

pytorch_kv_tokens = run_seq2seq_pytorch_with_kv(qeff_model, processor, data, sample_rate, ctx_len)

Expand Down
Loading