@@ -462,7 +462,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
462462 device_map (`str` or `Dict[str, Union[int, str, torch.device]]`, *optional*):
463463 A map that specifies where each submodule should go. It doesn't need to be defined for each
464464 parameter/buffer name; once a given module name is inside, every submodule of it will be sent to the
465- same device.
465+ same device. Defaults to `None`, meaning that the model will be loaded on CPU.
466466
467467 Set `device_map="auto"` to have 🤗 Accelerate automatically compute the most optimized `device_map`. For
468468 more information about each option see [designing a device
@@ -774,7 +774,12 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
774774 else : # else let accelerate handle loading and dispatching.
775775 # Load weights and dispatch according to the device_map
776776 # by default the device_map is None and the weights are loaded on the CPU
777+ force_hook = True
777778 device_map = _determine_device_map (model , device_map , max_memory , torch_dtype )
779+ if device_map is None and is_sharded :
780+ # we load the parameters on the cpu
781+ device_map = {"" : "cpu" }
782+ force_hook = False
778783 try :
779784 accelerate .load_checkpoint_and_dispatch (
780785 model ,
@@ -784,7 +789,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
784789 offload_folder = offload_folder ,
785790 offload_state_dict = offload_state_dict ,
786791 dtype = torch_dtype ,
787- force_hooks = True ,
792+ force_hooks = force_hook ,
788793 strict = True ,
789794 )
790795 except AttributeError as e :
@@ -808,12 +813,14 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
808813 model ._temp_convert_self_to_deprecated_attention_blocks ()
809814 accelerate .load_checkpoint_and_dispatch (
810815 model ,
811- model_file ,
816+ model_file if not is_sharded else sharded_ckpt_cached_folder ,
812817 device_map ,
813818 max_memory = max_memory ,
814819 offload_folder = offload_folder ,
815820 offload_state_dict = offload_state_dict ,
816821 dtype = torch_dtype ,
822+ force_hook = force_hook ,
823+ strict = True ,
817824 )
818825 model ._undo_temp_convert_self_to_deprecated_attention_blocks ()
819826 else :
0 commit comments