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

FIX [bnb] Make unexpected_keys optional #29420

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3769,7 +3769,7 @@ def _fix_key(key):
):
set_module_tensor_to_device(model, key, "cpu", value)
else:
hf_quantizer.create_quantized_param(model, value, key, "cpu", state_dict)
hf_quantizer.create_quantized_param(model, value, key, "cpu", state_dict, unexpected_keys)

# retrieve uninitialized modules and initialize before maybe overriding that with the pretrained weights.
if _fast_init:
Expand Down
7 changes: 4 additions & 3 deletions src/transformers/quantizers/quantizer_bnb_4bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
from typing import TYPE_CHECKING, Any, Dict, List, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from packaging import version

Expand Down Expand Up @@ -143,7 +143,7 @@ def create_quantized_param(
param_name: str,
target_device: "torch.device",
state_dict: Dict[str, Any],
unexpected_keys: List[str],
unexpected_keys: Optional[List[str]] = None,
):
"""
combines logic from _load_state_dict_into_meta_model and .integrations.bitsandbytes.py::set_module_quantized_tensor_to_device()
Expand Down Expand Up @@ -198,7 +198,8 @@ def create_quantized_param(
for k, v in state_dict.items():
if param_name + "." in k:
quantized_stats[k] = v
unexpected_keys.remove(k)
if unexpected_keys is not None:
younesbelkada marked this conversation as resolved.
Show resolved Hide resolved
unexpected_keys.remove(k)

new_value = bnb.nn.Params4bit.from_prequantized(
data=param_value,
Expand Down
7 changes: 4 additions & 3 deletions src/transformers/quantizers/quantizer_bnb_8bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
from typing import TYPE_CHECKING, Any, Dict, List, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from packaging import version

Expand Down Expand Up @@ -162,7 +162,7 @@ def create_quantized_param(
param_name: str,
target_device: "torch.device",
state_dict: Dict[str, Any],
unexpected_keys: List[str],
unexpected_keys: Optional[List[str]],
younesbelkada marked this conversation as resolved.
Show resolved Hide resolved
):
"""
combines logic from _load_state_dict_into_meta_model and .integrations.bitsandbytes.py::set_module_quantized_tensor_to_device()
Expand Down Expand Up @@ -207,7 +207,8 @@ def create_quantized_param(
module._parameters[tensor_name] = new_value
if fp16_statistics is not None:
setattr(module.weight, "SCB", fp16_statistics.to(target_device))
unexpected_keys.remove(fp16_statistics_key)
if unexpected_keys is not None:
unexpected_keys.remove(fp16_statistics_key)

def _process_model_after_weight_loading(self, model: "PreTrainedModel", **kwargs):
model.is_loaded_in_8bit = True
Expand Down
Loading