Skip to content

Commit 315dcbe

Browse files
authored
Do not use accelerate hooks if the device_map has only 1 device (huggingface#43019)
* unique device * comment * fix * simplify and add mismatched * style
1 parent dc06f2d commit 315dcbe

1 file changed

Lines changed: 38 additions & 46 deletions

File tree

src/transformers/modeling_utils.py

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3963,7 +3963,7 @@ def from_pretrained(
39633963
weight_mapping=weight_conversions,
39643964
)
39653965

3966-
model.eval() # Set model in evaluation mode to deactivate DropOut modules by default
3966+
model.eval() # Set model in evaluation mode to deactivate Dropout modules by default
39673967
model.set_use_kernels(use_kernels, kernel_config)
39683968

39693969
# If it is a model with generation capabilities, attempt to load generation files (generation config,
@@ -3979,8 +3979,8 @@ def from_pretrained(
39793979
**kwargs,
39803980
)
39813981

3982-
# for device_map="auto" : dispatch model with hooks on all devices if necessary
3983-
if device_map is not None and device_mesh is None:
3982+
# If the device_map has more than 1 device: dispatch model with hooks on all devices
3983+
if device_map is not None and len(set(device_map.values())) > 1:
39843984
accelerate_dispatch(model, hf_quantizer, device_map, offload_folder, offload_index, offload_buffers)
39853985

39863986
if hf_quantizer is not None:
@@ -4056,7 +4056,6 @@ def _load_pretrained_model(
40564056
expanded_device_map = expand_device_map(device_map, expected_keys)
40574057
caching_allocator_warmup(model, expanded_device_map, hf_quantizer)
40584058

4059-
tp_plan = getattr(model, "_tp_plan", None)
40604059
error_msgs = []
40614060

40624061
if is_deepspeed_zero3_enabled() and not is_quantized:
@@ -4091,17 +4090,17 @@ def _load_pretrained_model(
40914090

40924091
missing_keys, unexpected_keys, mismatched_keys, disk_offload_index, conversion_errors = (
40934092
convert_and_load_state_dict_in_model(
4094-
model,
4095-
merged_state_dict,
4096-
weight_mapping,
4097-
tp_plan,
4098-
hf_quantizer,
4099-
dtype,
4100-
device_map,
4101-
model.dtype_plan,
4102-
device_mesh,
4103-
disk_offload_index,
4104-
disk_offload_folder,
4093+
model=model,
4094+
state_dict=merged_state_dict,
4095+
weight_mapping=weight_mapping,
4096+
tp_plan=model._tp_plan,
4097+
hf_quantizer=hf_quantizer,
4098+
dtype=dtype,
4099+
device_map=device_map,
4100+
dtype_plan=model.dtype_plan,
4101+
device_mesh=device_mesh,
4102+
disk_offload_index=disk_offload_index,
4103+
disk_offload_folder=disk_offload_folder,
41054104
)
41064105
)
41074106

@@ -4112,10 +4111,10 @@ def _load_pretrained_model(
41124111
# Marks tied weights as `_is_hf_initialized` to avoid initializing them (it's very important for efficiency)
41134112
model.mark_tied_weights_as_initialized()
41144113

4115-
# Move missing (and potentially mismatched) keys back to cpu from meta device (because they won't be moved when
4116-
# loading the weights as they are not in the loaded state dict)
4117-
miss_and_mismatched = missing_keys | {k[0] for k in mismatched_keys}
4118-
model._move_missing_keys_from_meta_to_cpu(miss_and_mismatched, hf_quantizer)
4114+
# Move missing (and potentially mismatched) keys back to cpu from meta device (because they were not moved when
4115+
# loading the weights as they were not in the loaded state dict)
4116+
missing_and_mismatched = missing_keys | {k[0] for k in mismatched_keys}
4117+
model._move_missing_keys_from_meta_to_cpu(missing_and_mismatched, hf_quantizer)
41194118

41204119
# Correctly initialize the missing (and potentially mismatched) keys (all parameters without the `_is_hf_initialzed` flag)
41214120
model._initialize_missing_keys(is_quantized)
@@ -4126,33 +4125,28 @@ def _load_pretrained_model(
41264125
# Adjust missing and unexpected keys
41274126
missing_keys, unexpected_keys = model._adjust_missing_and_unexpected_keys(missing_keys, unexpected_keys)
41284127

4129-
# Post-processing for tensor parallelism
4130-
if device_mesh is not None:
4131-
# When using TP, the device map is a single device for all parameters
4132-
tp_device = list(device_map.values())[0]
4133-
# This is needed for the RotaryEmbedding, which was not initialized on the correct device as it is
4134-
# not part of the state_dict (persistent=False)
4135-
for buffer in model.buffers(): # TODO to avoid this buffer could be added to the ckpt
4136-
if buffer.device != tp_device:
4137-
buffer.data = buffer.to(tp_device)
4138-
4139-
# In this case, the top-most task module weights were not moved to device and parallelized as they
4140-
# were not part of the loaded weights: do it now
4141-
if missing_keys:
4142-
state_dict = model.state_dict()
4143-
for name in missing_keys:
4144-
param = state_dict[name]
4145-
# Shard the param
4128+
unique_devices = set(device_map.values()) if device_map is not None else set()
4129+
# Post-processing for only 1-value device_map (this includes TP) as we won't use hooks in this case
4130+
if len(unique_devices) == 1:
4131+
device = unique_devices.pop()
4132+
# This is needed for all non-persistent buffers (such as RotaryEmbedding modules), which were not initialized
4133+
# on the correct device as it is not part of the state_dict
4134+
for _, buffer in model.named_non_persistent_buffers():
4135+
buffer.data = buffer.data.to(device)
4136+
4137+
# The missing/mismatch weights were not moved to device (and parallelized for TP) as they were not part of the
4138+
# loaded weights: do it now if we have any
4139+
missing_and_mismatched = missing_keys | {k[0] for k in mismatched_keys}
4140+
for name in missing_and_mismatched:
4141+
param = model.get_parameter_or_buffer(name)
4142+
# For TP, shard the param
4143+
if device_mesh is not None:
41464144
shard_and_distribute_module(
4147-
model,
4148-
param.to(tp_device),
4149-
param,
4150-
name,
4151-
None,
4152-
False,
4153-
device_mesh.get_local_rank(),
4154-
device_mesh,
4145+
model, param.to(device), param, name, None, False, device_mesh.get_local_rank(), device_mesh
41554146
)
4147+
# Otherwise, just move it to device
4148+
else:
4149+
param.data = param.data.to(device)
41564150

41574151
log_state_dict_report(
41584152
model=model,
@@ -4421,8 +4415,6 @@ def _adjust_missing_and_unexpected_keys(
44214415
) -> tuple[set[str], set[str]]:
44224416
"""Adjust the `missing_keys` and `unexpected_keys` based on current model's exception rules, to avoid
44234417
raising unneeded warnings/errors.
4424-
Also, set the `_is_hf_initialized` on tied weight keys, to avoid initializing them as they are going to
4425-
be tied anyway.
44264418
"""
44274419
# Old checkpoints may have keys for rotary_emb.inv_freq forach layer, however we moved this buffer to the main model
44284420
# (so the buffer name has changed). Remove them in such a case. This is another exception that was not added to

0 commit comments

Comments
 (0)