Skip to content
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

Adapt transformers 4.45.1 #2019

Merged
merged 8 commits into from
Sep 30, 2024
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
3 changes: 3 additions & 0 deletions neural_compressor/torch/algorithms/weight_only/awq.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ def block_inference(self, model):
"""
total_out = []
for args, kwargs in zip(self.total_block_args, self.total_block_kwargs):
# to avoid layer_past: Dynamic_cache when transformers higher than 4.45.1
if "layer_past" in kwargs.keys() and kwargs["layer_past"] is not None:
kwargs["layer_past"] = None
out = model(*args, **kwargs)
if isinstance(out, tuple): # pragma: no cover
out = out[0]
Expand Down
43 changes: 30 additions & 13 deletions neural_compressor/torch/algorithms/weight_only/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,19 +834,36 @@ def _load_remaining_pretrained_weight(self, model):
resolved_archive_file = [resolved_archive_file]
for shard_file in resolved_archive_file:
state_dict = load_state_dict(shard_file)
_load_state_dict_into_meta_model(
model=model,
state_dict=state_dict,
loaded_state_dict_keys=self.loaded_state_dict_keys,
start_prefix="",
expected_keys=list(state_dict.keys()),
device_map={"": self.device},
offload_folder=offload_folder,
state_dict_folder=tempfile.mkdtemp() if offload_state_dict else None,
state_dict_index={} if offload_state_dict else None,
dtype=torch_dtype,
keep_in_fp32_modules=[],
)
import transformers
from packaging.version import Version

if Version(transformers.__version__) >= Version("4.45.0"): # pragma: no cover
_load_state_dict_into_meta_model(
model=model,
state_dict=state_dict,
start_prefix="",
expected_keys=list(state_dict.keys()),
device_map={"": self.device},
offload_folder=offload_folder,
state_dict_folder=tempfile.mkdtemp() if offload_state_dict else None,
state_dict_index={} if offload_state_dict else None,
dtype=torch_dtype,
keep_in_fp32_modules=[],
)
else:
_load_state_dict_into_meta_model(
model=model,
state_dict=state_dict,
loaded_state_dict_keys=self.loaded_state_dict_keys,
start_prefix="",
expected_keys=list(state_dict.keys()),
device_map={"": self.device},
offload_folder=offload_folder,
state_dict_folder=tempfile.mkdtemp() if offload_state_dict else None,
state_dict_index={} if offload_state_dict else None,
dtype=torch_dtype,
keep_in_fp32_modules=[],
)

# make sure token embedding weights are still tied if needed
model.tie_weights()
Expand Down
Loading