text
stringlengths
1
1.02k
class_index
int64
0
10.8k
source
stringlengths
85
188
pretrained_model_name_or_path, subfolder, _add_variant(WEIGHTS_INDEX_NAME, variant) ) is_sharded = True # At this stage we don't have a weight file so we will raise an error. elif not use_safetensors and ( os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF_WEIGHTS_NAME + ".index")) or os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF2_WEIGHTS_NAME)) ): raise EnvironmentError( f"Error no file named {_add_variant(WEIGHTS_NAME, variant)} found in directory" f" {pretrained_model_name_or_path} but there is a file for TensorFlow weights. Use" " `from_tf=True` to load this model from those weights." ) elif not use_safetensors and os.path.isfile(
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
os.path.join(pretrained_model_name_or_path, subfolder, FLAX_WEIGHTS_NAME) ): raise EnvironmentError( f"Error no file named {_add_variant(WEIGHTS_NAME, variant)} found in directory" f" {pretrained_model_name_or_path} but there is a file for Flax weights. Use `from_flax=True`" " to load this model from those weights." ) elif use_safetensors: raise EnvironmentError( f"Error no file named {_add_variant(SAFE_WEIGHTS_NAME, variant)} found in directory" f" {pretrained_model_name_or_path}." ) else: raise EnvironmentError( f"Error no file named {_add_variant(WEIGHTS_NAME, variant)}, {_add_variant(SAFE_WEIGHTS_NAME, variant)},"
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
f" {TF2_WEIGHTS_NAME}, {TF_WEIGHTS_NAME + '.index'} or {FLAX_WEIGHTS_NAME} found in directory" f" {pretrained_model_name_or_path}." ) elif os.path.isfile(os.path.join(subfolder, pretrained_model_name_or_path)): archive_file = pretrained_model_name_or_path is_local = True elif os.path.isfile(os.path.join(subfolder, pretrained_model_name_or_path + ".index")): if not from_tf: raise ValueError( f"We found a TensorFlow checkpoint at {pretrained_model_name_or_path + '.index'}, please set " "from_tf to True to load from this checkpoint." ) archive_file = os.path.join(subfolder, pretrained_model_name_or_path + ".index") is_local = True elif is_remote_url(pretrained_model_name_or_path): filename = pretrained_model_name_or_path
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
resolved_archive_file = download_url(pretrained_model_name_or_path) else: # set correct filename if from_tf: filename = TF2_WEIGHTS_NAME elif from_flax: filename = FLAX_WEIGHTS_NAME elif use_safetensors is not False: filename = _add_variant(SAFE_WEIGHTS_NAME, variant) else: filename = _add_variant(WEIGHTS_NAME, variant)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
try: # Load from URL or cache if already cached cached_file_kwargs = { "cache_dir": cache_dir, "force_download": force_download, "proxies": proxies, "resume_download": resume_download, "local_files_only": local_files_only, "token": token, "user_agent": user_agent, "revision": revision, "subfolder": subfolder, "_raise_exceptions_for_gated_repo": False, "_raise_exceptions_for_missing_entries": False, "_commit_hash": commit_hash, } resolved_archive_file = cached_file(pretrained_model_name_or_path, filename, **cached_file_kwargs)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Since we set _raise_exceptions_for_missing_entries=False, we don't get an exception but a None # result when internet is up, the repo and revision exist, but the file does not. if resolved_archive_file is None and filename == _add_variant(SAFE_WEIGHTS_NAME, variant): # Maybe the checkpoint is sharded, we try to grab the index name in this case. resolved_archive_file = cached_file( pretrained_model_name_or_path, _add_variant(SAFE_WEIGHTS_INDEX_NAME, variant), **cached_file_kwargs, ) if resolved_archive_file is not None: is_sharded = True elif use_safetensors: if revision == "main": resolved_archive_file, revision, is_sharded = auto_conversion(
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
pretrained_model_name_or_path, **cached_file_kwargs ) cached_file_kwargs["revision"] = revision if resolved_archive_file is None: raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named" f" {_add_variant(SAFE_WEIGHTS_NAME, variant)} or {_add_variant(SAFE_WEIGHTS_INDEX_NAME, variant)} " "and thus cannot be loaded with `safetensors`. Please make sure that the model has " "been saved with `safe_serialization=True` or do not set `use_safetensors=True`." ) else: # This repo has no safetensors file of any kind, we switch to PyTorch.
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
filename = _add_variant(WEIGHTS_NAME, variant) resolved_archive_file = cached_file( pretrained_model_name_or_path, filename, **cached_file_kwargs ) if resolved_archive_file is None and filename == _add_variant(WEIGHTS_NAME, variant): # Maybe the checkpoint is sharded, we try to grab the index name in this case. resolved_archive_file = cached_file( pretrained_model_name_or_path, _add_variant(WEIGHTS_INDEX_NAME, variant), **cached_file_kwargs, ) if resolved_archive_file is not None: is_sharded = True if not local_files_only and not is_offline_mode(): if resolved_archive_file is not None:
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if filename in [WEIGHTS_NAME, WEIGHTS_INDEX_NAME]: # If the PyTorch file was found, check if there is a safetensors file on the repository # If there is no safetensors file on the repositories, start an auto conversion safe_weights_name = SAFE_WEIGHTS_INDEX_NAME if is_sharded else SAFE_WEIGHTS_NAME has_file_kwargs = { "revision": revision, "proxies": proxies, "token": token, "cache_dir": cache_dir, "local_files_only": local_files_only, } cached_file_kwargs = { "cache_dir": cache_dir, "force_download": force_download,
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
"resume_download": resume_download, "local_files_only": local_files_only, "user_agent": user_agent, "subfolder": subfolder, "_raise_exceptions_for_gated_repo": False, "_raise_exceptions_for_missing_entries": False, "_commit_hash": commit_hash, **has_file_kwargs, } if not has_file(pretrained_model_name_or_path, safe_weights_name, **has_file_kwargs): Thread( target=auto_conversion, args=(pretrained_model_name_or_path,), kwargs={"ignore_errors_during_conversion": True, **cached_file_kwargs},
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
name="Thread-auto_conversion", ).start() else: # Otherwise, no PyTorch file was found, maybe there is a TF or Flax model file. # We try those to give a helpful error message. has_file_kwargs = { "revision": revision, "proxies": proxies, "token": token, "cache_dir": cache_dir, "local_files_only": local_files_only, } if has_file(pretrained_model_name_or_path, TF2_WEIGHTS_NAME, **has_file_kwargs): raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named"
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
f" {_add_variant(WEIGHTS_NAME, variant)} but there is a file for TensorFlow weights." " Use `from_tf=True` to load this model from those weights." ) elif has_file(pretrained_model_name_or_path, FLAX_WEIGHTS_NAME, **has_file_kwargs): raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named" f" {_add_variant(WEIGHTS_NAME, variant)} but there is a file for Flax weights. Use" " `from_flax=True` to load this model from those weights." ) elif variant is not None and has_file( pretrained_model_name_or_path, WEIGHTS_NAME, **has_file_kwargs ):
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named" f" {_add_variant(WEIGHTS_NAME, variant)} but there is a file without the variant" f" {variant}. Use `variant=None` to load this model from those weights." ) else: raise EnvironmentError( f"{pretrained_model_name_or_path} does not appear to have a file named" f" {_add_variant(WEIGHTS_NAME, variant)}, {_add_variant(SAFE_WEIGHTS_NAME, variant)}," f" {TF2_WEIGHTS_NAME}, {TF_WEIGHTS_NAME} or {FLAX_WEIGHTS_NAME}." )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
except EnvironmentError: # Raise any environment error raise by `cached_file`. It will have a helpful error message adapted # to the original exception. raise except Exception as e: # For any other exception, we throw a generic error. raise EnvironmentError( f"Can't load the model for '{pretrained_model_name_or_path}'. If you were trying to load it" " from 'https://huggingface.co/models', make sure you don't have a local directory with the" f" same name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a" f" directory containing a file named {_add_variant(WEIGHTS_NAME, variant)}," f" {TF2_WEIGHTS_NAME}, {TF_WEIGHTS_NAME} or {FLAX_WEIGHTS_NAME}." ) from e
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if is_local: logger.info(f"loading weights file {archive_file}") resolved_archive_file = archive_file else: logger.info(f"loading weights file {filename} from cache at {resolved_archive_file}") elif gguf_file: from .modeling_gguf_pytorch_utils import load_gguf_checkpoint
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Case 1: the GGUF file is present locally if os.path.isfile(gguf_file): gguf_path = gguf_file # Case 2: The GGUF path is a location on the Hub # Load from URL or cache if already cached else: cached_file_kwargs = { "cache_dir": cache_dir, "force_download": force_download, "proxies": proxies, "resume_download": resume_download, "local_files_only": local_files_only, "token": token, "user_agent": user_agent, "revision": revision, "subfolder": subfolder, "_raise_exceptions_for_gated_repo": False, "_raise_exceptions_for_missing_entries": False, "_commit_hash": commit_hash, }
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
gguf_path = cached_file(pretrained_model_name_or_path, gguf_file, **cached_file_kwargs) # we need a dummy model to help rename state_dict with torch.device("meta"): dummy_model = cls(config) state_dict = load_gguf_checkpoint(gguf_path, return_tensors=True, model_to_load=dummy_model)["tensors"] resolved_archive_file = None is_sharded = False else: resolved_archive_file = None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# We'll need to download and cache each checkpoint shard if the checkpoint is sharded. if is_sharded: # resolved_archive_file becomes a list of files that point to the different checkpoint shards in this case. resolved_archive_file, sharded_metadata = get_checkpoint_shard_files( pretrained_model_name_or_path, resolved_archive_file, cache_dir=cache_dir, force_download=force_download, proxies=proxies, resume_download=resume_download, local_files_only=local_files_only, token=token, user_agent=user_agent, revision=revision, subfolder=subfolder, _commit_hash=commit_hash, )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if ( is_safetensors_available() and isinstance(resolved_archive_file, str) and resolved_archive_file.endswith(".safetensors") ): with safe_open(resolved_archive_file, framework="pt") as f: metadata = f.metadata()
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if metadata is None: # Assume it's a pytorch checkpoint (introduced for timm checkpoints) pass elif metadata.get("format") == "pt": pass elif metadata.get("format") == "tf": from_tf = True logger.info("A TensorFlow safetensors file is being loaded in a PyTorch model.") elif metadata.get("format") == "flax": from_flax = True logger.info("A Flax safetensors file is being loaded in a PyTorch model.") elif metadata.get("format") == "mlx": # This is a mlx file, we assume weights are compatible with pt pass else: raise ValueError( f"Incompatible safetensors file. File metadata is not ['pt', 'tf', 'flax', 'mlx'] but {metadata.get('format')}" ) from_pt = not (from_tf | from_flax)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# load pt weights early so that we know which dtype to init the model under if from_pt: if not is_sharded and state_dict is None: # Time to load the checkpoint state_dict = load_state_dict(resolved_archive_file, weights_only=weights_only) # set dtype to instantiate the model under: # 1. If torch_dtype is not None, we use that dtype # 2. If torch_dtype is "auto", we auto-detect dtype from the loaded state_dict, by checking its first # weights entry that is of a floating type - we assume all floating dtype weights are of the same dtype # we also may have config.torch_dtype available, but we won't rely on it till v5 dtype_orig = None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if torch_dtype is not None: if isinstance(torch_dtype, str): if torch_dtype == "auto": if hasattr(config, "torch_dtype") and config.torch_dtype is not None: torch_dtype = config.torch_dtype logger.info(f"Will use torch_dtype={torch_dtype} as defined in model's config object") else: if is_sharded and "dtype" in sharded_metadata: torch_dtype = sharded_metadata["dtype"] elif not is_sharded: torch_dtype = get_state_dict_dtype(state_dict) else: one_state_dict = load_state_dict(resolved_archive_file[0], weights_only=weights_only) torch_dtype = get_state_dict_dtype(one_state_dict) del one_state_dict # free CPU memory
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
logger.info( "Since the `torch_dtype` attribute can't be found in model's config object, " "will use torch_dtype={torch_dtype} as derived from model's weights" ) elif hasattr(torch, torch_dtype): torch_dtype = getattr(torch, torch_dtype) for sub_config_key in config.sub_configs.keys(): sub_config = getattr(config, sub_config_key) sub_config.torch_dtype = torch_dtype elif isinstance(torch_dtype, torch.dtype): pass elif isinstance(torch_dtype, dict): for key, curr_dtype in torch_dtype.items(): if hasattr(config, key): value = getattr(config, key) value.torch_dtype = curr_dtype
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# main torch dtype for modules that aren't part of any sub-config torch_dtype = torch_dtype.get("") config.torch_dtype = torch_dtype if isinstance(torch_dtype, str) and hasattr(torch, torch_dtype): torch_dtype = getattr(torch, torch_dtype) elif torch_dtype is None: torch_dtype = torch.float32 else: raise ValueError( f"`torch_dtype` can be one of: `torch.dtype`, `'auto'`, a string of a valid `torch.dtype` or a `dict` with valid `torch_dtype` " f"for each sub-config in composite configs, but received {torch_dtype}" )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
dtype_orig = cls._set_default_torch_dtype(torch_dtype) else: # set fp32 as the default dtype for BC default_dtype = str(torch.get_default_dtype()).split(".")[-1] config.torch_dtype = default_dtype for key in config.sub_configs.keys(): value = getattr(config, key) value.torch_dtype = default_dtype # Check if `_keep_in_fp32_modules` is not None use_keep_in_fp32_modules = (cls._keep_in_fp32_modules is not None) and ( (torch_dtype == torch.float16) or hasattr(hf_quantizer, "use_keep_in_fp32_modules") )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if is_sharded: loaded_state_dict_keys = sharded_metadata["all_checkpoint_keys"] else: loaded_state_dict_keys = list(state_dict.keys()) if ( gguf_path is None and (low_cpu_mem_usage or (use_keep_in_fp32_modules and is_accelerate_available())) and pretrained_model_name_or_path is not None ): # In case some weights need to be kept in float32 and accelerate is not installed, # we later on want to take the path where state_dict is not None, that is the one # that do not require accelerate. state_dict = None config.name_or_path = pretrained_model_name_or_path # Instantiate model. init_contexts = [no_init_weights(_enable=_fast_init)] tp_device = None if is_deepspeed_zero3_enabled() and not is_quantized and not _is_ds_init_called: import deepspeed
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
logger.info("Detected DeepSpeed ZeRO-3: activating zero.init() for this model") init_contexts = [ deepspeed.zero.Init(config_dict_or_path=deepspeed_config()), set_zero3_state(), ] + init_contexts elif low_cpu_mem_usage: if not is_accelerate_available(): raise ImportError( f"Using `low_cpu_mem_usage=True` or a `device_map` requires Accelerate: `pip install 'accelerate>={ACCELERATE_MIN_VERSION}'`" ) init_contexts.append(init_empty_weights()) elif tp_plan is not None: if not torch.distributed.is_initialized(): raise ValueError("Tensor Parallel requires torch.distributed to be initialized first.")
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Detect the accelerator on the machine. If no accelerator is available, it returns CPU. device_type = torch._C._get_accelerator().type device_module = torch.get_device_module(device_type) # Get device with index assuming equal number of devices per host tp_device = torch.device(device_type, torch.distributed.get_rank() % device_module.device_count()) init_contexts.append(tp_device) if is_deepspeed_zero3_enabled() and is_quantized: init_contexts.append(set_quantized_state()) config = copy.deepcopy(config) # We do not want to modify the config inplace in from_pretrained. if not getattr(config, "_attn_implementation_autoset", False): config = cls._autoset_attn_implementation( config, use_flash_attention_2=use_flash_attention_2, torch_dtype=torch_dtype, device_map=device_map )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
with ContextManagers(init_contexts): # Let's make sure we don't run the init function of buffer modules model = cls(config, *model_args, **model_kwargs) # make sure we use the model's config since the __init__ call might have copied it config = model.config # Check first if we are `from_pt` if use_keep_in_fp32_modules: if is_accelerate_available() and not is_deepspeed_zero3_enabled(): low_cpu_mem_usage = True keep_in_fp32_modules = model._keep_in_fp32_modules else: keep_in_fp32_modules = [] if hf_quantizer is not None: hf_quantizer.preprocess_model( model=model, device_map=device_map, keep_in_fp32_modules=keep_in_fp32_modules )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# We store the original dtype for quantized models as we cannot easily retrieve it # once the weights have been quantized # Note that once you have loaded a quantized model, you can't change its dtype so this will # remain a single source of truth config._pre_quantization_dtype = torch_dtype if isinstance(device_map, str): special_dtypes = {} if hf_quantizer is not None: special_dtypes.update(hf_quantizer.get_special_dtypes_update(model, torch_dtype)) special_dtypes.update( { name: torch.float32 for name, _ in model.named_parameters() if any(m in name for m in keep_in_fp32_modules) } ) target_dtype = torch_dtype if hf_quantizer is not None: target_dtype = hf_quantizer.adjust_target_dtype(target_dtype)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
no_split_modules = model._get_no_split_modules(device_map) if device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]: raise ValueError( "If passing a string for `device_map`, please choose 'auto', 'balanced', 'balanced_low_0' or " "'sequential'." )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
device_map_kwargs = {"no_split_module_classes": no_split_modules} if "special_dtypes" in inspect.signature(infer_auto_device_map).parameters: device_map_kwargs["special_dtypes"] = special_dtypes elif len(special_dtypes) > 0: logger.warning( "This model has some weights that should be kept in higher precision, you need to upgrade " "`accelerate` to properly deal with them (`pip install --upgrade accelerate`)." ) if device_map != "sequential": max_memory = get_balanced_memory( model, dtype=target_dtype, low_zero=(device_map == "balanced_low_0"), max_memory=max_memory, **device_map_kwargs, ) else: max_memory = get_max_memory(max_memory) if hf_quantizer is not None:
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
max_memory = hf_quantizer.adjust_max_memory(max_memory) device_map_kwargs["max_memory"] = max_memory
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Make sure tied weights are tied before creating the device map. model.tie_weights() device_map = infer_auto_device_map(model, dtype=target_dtype, **device_map_kwargs) if hf_quantizer is not None: hf_quantizer.validate_environment(device_map=device_map) elif device_map is not None: model.tie_weights() tied_params = find_tied_parameters(model) # check if we don't have tied param in different devices check_tied_parameters_on_same_device(tied_params, device_map)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if from_tf: if resolved_archive_file.endswith(".index"): # Load from a TensorFlow 1.X checkpoint - provided by original authors model = cls.load_tf_weights(model, config, resolved_archive_file[:-6]) # Remove the '.index' else: # Load from our TensorFlow 2.0 checkpoints try: from .modeling_tf_pytorch_utils import load_tf2_checkpoint_in_pytorch_model
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
model, loading_info = load_tf2_checkpoint_in_pytorch_model( model, resolved_archive_file, allow_missing_keys=True, output_loading_info=True ) except ImportError: logger.error( "Loading a TensorFlow model in PyTorch, requires both PyTorch and TensorFlow to be installed." " Please see https://pytorch.org/ and https://www.tensorflow.org/install/ for installation" " instructions." ) raise elif from_flax: try: from .modeling_flax_pytorch_utils import load_flax_checkpoint_in_pytorch_model
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
model = load_flax_checkpoint_in_pytorch_model(model, resolved_archive_file) except ImportError: logger.error( "Loading a Flax model in PyTorch, requires both PyTorch and Flax to be installed. Please see" " https://pytorch.org/ and https://flax.readthedocs.io/en/latest/installation.html for" " installation instructions." ) raise elif from_pt: # restore default dtype if dtype_orig is not None: torch.set_default_dtype(dtype_orig) load_contexts = [] # Make sure we load onto targeted device if tp_device is not None: load_contexts.append(tp_device)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
with ContextManagers(load_contexts): ( model, missing_keys, unexpected_keys, mismatched_keys, offload_index, error_msgs, ) = cls._load_pretrained_model( model, state_dict, loaded_state_dict_keys, # XXX: rename? resolved_archive_file, pretrained_model_name_or_path, ignore_mismatched_sizes=ignore_mismatched_sizes, sharded_metadata=sharded_metadata, _fast_init=_fast_init, low_cpu_mem_usage=low_cpu_mem_usage, device_map=device_map, offload_folder=offload_folder, offload_state_dict=offload_state_dict, dtype=torch_dtype, hf_quantizer=hf_quantizer,
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
keep_in_fp32_modules=keep_in_fp32_modules, gguf_path=gguf_path, weights_only=weights_only, )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# make sure token embedding weights are still tied if needed model.tie_weights() # Set model in evaluation mode to deactivate DropOut modules by default model.eval()
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# If it is a model with generation capabilities, attempt to load the generation config if model.can_generate() and generation_config is not None: logger.info("The user-defined `generation_config` will be used to override the default generation config.") model.generation_config = model.generation_config.from_dict(generation_config.to_dict()) elif model.can_generate() and pretrained_model_name_or_path is not None: try: model.generation_config = GenerationConfig.from_pretrained( pretrained_model_name_or_path, cache_dir=cache_dir, force_download=force_download, resume_download=resume_download, proxies=proxies, local_files_only=local_files_only, token=token, revision=revision, subfolder=subfolder, _from_auto=from_auto_class,
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
_from_pipeline=from_pipeline, **kwargs, ) except OSError: logger.info( "Generation config file not found, using a generation config created from the model config." ) pass
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Dispatch model with hooks on all devices if necessary if device_map is not None: device_map_kwargs = { "device_map": device_map, "offload_dir": offload_folder, "offload_index": offload_index, "offload_buffers": offload_buffers, } if "skip_keys" in inspect.signature(dispatch_model).parameters: device_map_kwargs["skip_keys"] = model._skip_keys_device_placement # For HQQ method we force-set the hooks for single GPU envs if ( "force_hooks" in inspect.signature(dispatch_model).parameters and hf_quantizer is not None and hf_quantizer.quantization_config.quant_method == QuantizationMethod.HQQ ): device_map_kwargs["force_hooks"] = True if ( hf_quantizer is not None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
and hf_quantizer.quantization_config.quant_method == QuantizationMethod.FBGEMM_FP8 and isinstance(device_map, dict) and ("cpu" in device_map.values() or "disk" in device_map.values()) ): device_map_kwargs["offload_buffers"] = True
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if not is_fsdp_enabled() and not is_deepspeed_zero3_enabled(): dispatch_model(model, **device_map_kwargs) if hf_quantizer is not None: hf_quantizer.postprocess_model(model, config=config) model.hf_quantizer = hf_quantizer if _adapter_model_path is not None: model.load_adapter( _adapter_model_path, adapter_name=adapter_name, token=token, adapter_kwargs=adapter_kwargs, ) if output_loading_info: if loading_info is None: loading_info = { "missing_keys": missing_keys, "unexpected_keys": unexpected_keys, "mismatched_keys": mismatched_keys, "error_msgs": error_msgs, } return model, loading_info
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if tp_plan is not None: assert tp_device is not None, "tp_device not set!" if not model.supports_tp_plan: raise NotImplementedError("This model does not have a tensor parallel plan.") # Assuming sharding the model onto the world world_size = torch.distributed.get_world_size() device_mesh = torch.distributed.init_device_mesh(tp_device.type, (world_size,)) # Apply Tensor Parallelism model.tensor_parallel(device_mesh) return model @staticmethod def _fix_state_dict_key_on_load(key) -> Tuple[str, bool]: """Replace legacy parameter names with their modern equivalents. E.g. beta -> bias, gamma -> weight."""
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Rename LayerNorm beta & gamma params for some early models ported from Tensorflow (e.g. Bert) # This rename is logged. if key.endswith("LayerNorm.beta"): return key.replace("LayerNorm.beta", "LayerNorm.bias"), True if key.endswith("LayerNorm.gamma"): return key.replace("LayerNorm.gamma", "LayerNorm.weight"), True
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Rename weight norm parametrizations to match changes across torch versions. # Impacts a number of speech/wav2vec models. e.g. Hubert, Wav2Vec2, and others. # This rename is not logged. if hasattr(nn.utils.parametrizations, "weight_norm"): if key.endswith("weight_g"): return key.replace("weight_g", "parametrizations.weight.original0"), True if key.endswith("weight_v"): return key.replace("weight_v", "parametrizations.weight.original1"), True else: if key.endswith("parametrizations.weight.original0"): return key.replace("parametrizations.weight.original0", "weight_g"), True if key.endswith("parametrizations.weight.original1"): return key.replace("parametrizations.weight.original1", "weight_v"), True return key, False
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
@classmethod def _fix_state_dict_keys_on_load(cls, state_dict): """Fixes state dict keys by replacing legacy parameter names with their modern equivalents. Logs if any parameters have been renamed. """ renamed_keys = {} state_dict_keys = list(state_dict.keys()) for key in state_dict_keys: new_key, has_changed = cls._fix_state_dict_key_on_load(key) if has_changed: state_dict[new_key] = state_dict.pop(key) # track gamma/beta rename for logging if key.endswith("LayerNorm.gamma"): renamed_keys["LayerNorm.gamma"] = (key, new_key) elif key.endswith("LayerNorm.beta"): renamed_keys["LayerNorm.beta"] = (key, new_key)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if renamed_keys: warning_msg = f"A pretrained model of type `{cls.__name__}` " warning_msg += "contains parameters that have been renamed internally (a few are listed below but more are present in the model):\n" for old_key, new_key in renamed_keys.values(): warning_msg += f"* `{old_key}` -> `{new_key}`\n" warning_msg += "If you are using a model from the Hub, consider submitting a PR to adjust these weights and help future users." logger.info_once(warning_msg) return state_dict @staticmethod def _fix_state_dict_key_on_save(key) -> Tuple[str, bool]: """ Similar to `_fix_state_dict_key_on_load` allows to define hook for state dict key renaming on model save. Do nothing by default, but can be overridden in particular models. """ return key, False
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
def _fix_state_dict_keys_on_save(self, state_dict): """ Similar to `_fix_state_dict_keys_on_load` allows to define hook for state dict key renaming on model save. Apply `_fix_state_dict_key_on_save` to all keys in `state_dict`. """ return {self._fix_state_dict_key_on_save(key)[0]: value for key, value in state_dict.items()}
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
@classmethod def _load_pretrained_model( cls, model, state_dict, loaded_keys, resolved_archive_file, pretrained_model_name_or_path, ignore_mismatched_sizes=False, sharded_metadata=None, _fast_init=True, low_cpu_mem_usage=False, device_map=None, offload_folder=None, offload_state_dict=None, dtype=None, hf_quantizer=None, keep_in_fp32_modules=None, gguf_path=None, weights_only=True, ): is_safetensors = False is_quantized = hf_quantizer is not None state_dict_folder = None state_dict_index = None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if device_map is not None and "disk" in device_map.values(): archive_file = ( resolved_archive_file[0] if isinstance(resolved_archive_file, (list, tuple)) else resolved_archive_file ) is_safetensors = archive_file.endswith(".safetensors") if offload_folder is None and not is_safetensors: raise ValueError( "The current `device_map` had weights offloaded to the disk. Please provide an `offload_folder`" " for them. Alternatively, make sure you have `safetensors` installed if the model you are using" " offers the weights in this format." ) if offload_folder is not None: os.makedirs(offload_folder, exist_ok=True) if offload_state_dict is None: offload_state_dict = True is_sharded_safetensors = is_safetensors and sharded_metadata is not None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# tie the model weights before retrieving the state_dict model.tie_weights() # Retrieve missing & unexpected_keys model_state_dict = model.state_dict() expected_keys = list(model_state_dict.keys()) prefix = model.base_model_prefix if hf_quantizer is not None: expected_keys = hf_quantizer.update_expected_keys(model, expected_keys, loaded_keys) original_loaded_keys = loaded_keys loaded_keys = [cls._fix_state_dict_key_on_load(key)[0] for key in loaded_keys] if len(prefix) > 0: has_prefix_module = any(s.startswith(prefix) for s in loaded_keys) expects_prefix_module = any(s.startswith(prefix) for s in expected_keys) else: has_prefix_module = False expects_prefix_module = False
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# key re-naming operations are never done on the keys # that are loaded, but always on the keys of the newly initialized model remove_prefix_from_model = not has_prefix_module and expects_prefix_module add_prefix_to_model = has_prefix_module and not expects_prefix_module if remove_prefix_from_model: _prefix = f"{prefix}." expected_keys_not_prefixed = [s for s in expected_keys if not s.startswith(_prefix)] expected_keys = [s[len(_prefix) :] if s.startswith(_prefix) else s for s in expected_keys] elif add_prefix_to_model: expected_keys = [".".join([prefix, s]) for s in expected_keys] missing_keys = sorted(set(expected_keys) - set(loaded_keys)) unexpected_keys = set(loaded_keys) - set(expected_keys)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Remove nonpersistent buffers from unexpected keys: they are not in the state dict but will be in the model # buffers model_buffers = {n for n, _ in model.named_buffers()} if remove_prefix_from_model: model_buffers = {key[len(_prefix) :] if key.startswith(_prefix) else key for key in model_buffers} elif add_prefix_to_model: model_buffers = {".".join([prefix, key]) for key in model_buffers} unexpected_keys = sorted(unexpected_keys - model_buffers) # Clean up buffer for `inv-freq` because RoPE embedding moved under base model (https://github.com/huggingface/transformers/pull/34858) has_inv_freq_buffers = any(buffer.endswith("rotary_emb.inv_freq") for buffer in model_buffers) if has_inv_freq_buffers: unexpected_keys = {k for k in unexpected_keys if "rotary_emb.inv_freq" not in k}
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
model.tie_weights() if device_map is None and not is_fsdp_enabled() and not is_deepspeed_zero3_enabled(): ptrs = collections.defaultdict(list) for name, tensor in model.state_dict().items(): id_tensor = id_tensor_storage(tensor) ptrs[id_tensor].append(name) # These are all the pointers of shared tensors. tied_params = [names for _, names in ptrs.items() if len(names) > 1] else: # id function doesn't work for meta tensor so we need this function tied_params = find_tied_parameters(model)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
for group in tied_params: if remove_prefix_from_model: group = [key[len(_prefix) :] if key.startswith(_prefix) else key for key in group] elif add_prefix_to_model: group = [".".join([prefix, key]) for key in group] missing_in_group = [k for k in missing_keys if k in group] if len(missing_in_group) > 0 and len(missing_in_group) < len(group): missing_keys = [k for k in missing_keys if k not in missing_in_group] # Some models may have keys that are not in the state by design, removing them before needlessly warning # the user. if cls._keys_to_ignore_on_load_missing is not None: for pat in cls._keys_to_ignore_on_load_missing: missing_keys = [k for k in missing_keys if re.search(pat, k) is None]
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if cls._keys_to_ignore_on_load_unexpected is not None: for pat in cls._keys_to_ignore_on_load_unexpected: unexpected_keys = [k for k in unexpected_keys if re.search(pat, k) is None] if hf_quantizer is not None: missing_keys = hf_quantizer.update_missing_keys(model, missing_keys, prefix) # retrieve weights on meta device and put them back on CPU. # This is not ideal in terms of memory, but if we don't do that not, we can't initialize them in the next step if low_cpu_mem_usage: for key in missing_keys: if key in list(model_state_dict.keys()): key = key elif f"{prefix}.{key}" in list(model_state_dict.keys()): key = f"{prefix}.{key}" elif key.startswith(prefix) and ".".join(key.split(".")[1:]) in list(model_state_dict.keys()): key = ".".join(key.split(".")[1:]) param = model_state_dict[key]
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# upcast in fp32 if any target_dtype = dtype if ( keep_in_fp32_modules is not None and dtype == torch.float16 and any( module_to_keep_in_fp32 in key.split(".") for module_to_keep_in_fp32 in keep_in_fp32_modules ) ): target_dtype = torch.float32
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if param.device == torch.device("meta"): value = torch.empty(*param.size(), dtype=target_dtype) if ( not is_quantized or (getattr(hf_quantizer, "requires_parameters_quantization", False)) or not hf_quantizer.check_quantized_param( model, param_value=value, param_name=key, state_dict={} ) ): set_module_tensor_to_device(model, key, "cpu", value) else: hf_quantizer.create_quantized_param(model, value, key, "cpu", state_dict, unexpected_keys)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# retrieve uninitialized modules and initialize before maybe overriding that with the pretrained weights. if _fast_init: if not ignore_mismatched_sizes: if remove_prefix_from_model: _loaded_keys = [f"{prefix}.{k}" for k in loaded_keys] elif add_prefix_to_model: _loaded_keys = [k[len(prefix) + 1 :] for k in loaded_keys] else: _loaded_keys = loaded_keys not_initialized_submodules = set_initialized_submodules(model, _loaded_keys) # If we're about to tie the output embeds to the input embeds we don't need to init them if ( hasattr(model.config.get_text_config(decoder=True), "tie_word_embeddings") and model.config.get_text_config(decoder=True).tie_word_embeddings ): output_embeddings = model.get_output_embeddings()
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if output_embeddings is not None: # Still need to initialize if there is a bias term since biases are not tied. if not hasattr(output_embeddings, "bias") or output_embeddings.bias is None: output_embeddings._is_hf_initialized = True else: not_initialized_submodules = dict(model.named_modules()) # This will only initialize submodules that are not marked as initialized by the line above. if is_deepspeed_zero3_enabled() and not is_quantized: import deepspeed
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
not_initialized_parameters = list( set( itertools.chain.from_iterable( submodule.parameters(recurse=False) for submodule in not_initialized_submodules.values() ) ) ) with deepspeed.zero.GatheredParameters(not_initialized_parameters, modifier_rank=0): model.apply(model._initialize_weights) else: model.apply(model._initialize_weights) # Set some modules to fp32 if any if keep_in_fp32_modules is not None: for name, param in model.named_parameters(): if any(module_to_keep_in_fp32 in name.split(".") for module_to_keep_in_fp32 in keep_in_fp32_modules): # param = param.to(torch.float32) does not work here as only in the local scope. param.data = param.data.to(torch.float32)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Make sure we are able to load base models as well as derived models (with heads) start_prefix = "" model_to_load = model if len(cls.base_model_prefix) > 0 and not hasattr(model, cls.base_model_prefix) and has_prefix_module: start_prefix = cls.base_model_prefix + "." if len(cls.base_model_prefix) > 0 and hasattr(model, cls.base_model_prefix) and not has_prefix_module: model_to_load = getattr(model, cls.base_model_prefix) base_model_expected_keys = list(model_to_load.state_dict().keys()) if any(key in expected_keys_not_prefixed and key not in base_model_expected_keys for key in loaded_keys): raise ValueError( "The state dictionary of the model you are trying to load is corrupted. Are you sure it was " "properly saved?" ) if device_map is not None:
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
device_map = {k.replace(f"{cls.base_model_prefix}.", ""): v for k, v in device_map.items()}
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
def _find_mismatched_keys( state_dict, model_state_dict, loaded_keys, original_loaded_keys, add_prefix_to_model, remove_prefix_from_model, ignore_mismatched_sizes, ): mismatched_keys = [] if ignore_mismatched_sizes: for checkpoint_key, model_key in zip(original_loaded_keys, loaded_keys): # If the checkpoint is sharded, we may not have the key here. if checkpoint_key not in state_dict: continue if remove_prefix_from_model: # The model key starts with `prefix` but `checkpoint_key` doesn't so we add it. model_key = f"{prefix}.{model_key}" elif add_prefix_to_model: # The model key doesn't start with `prefix` but `checkpoint_key` does so we remove it.
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
model_key = ".".join(model_key.split(".")[1:])
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if ( model_key in model_state_dict and state_dict[checkpoint_key].shape != model_state_dict[model_key].shape ): if ( state_dict[checkpoint_key].shape[-1] == 1 and state_dict[checkpoint_key].numel() * 2 == model_state_dict[model_key].numel() ): # This skips size mismatches for 4-bit weights. Two 4-bit values share an 8-bit container, causing size differences. # Without matching with module type or paramter type it seems like a practical way to detect valid 4bit weights. pass else: mismatched_keys.append( (checkpoint_key, state_dict[checkpoint_key].shape, model_state_dict[model_key].shape) )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
del state_dict[checkpoint_key] return mismatched_keys
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if resolved_archive_file is not None: folder = os.path.sep.join(resolved_archive_file[0].split(os.path.sep)[:-1]) else: folder = None if device_map is not None and is_safetensors: param_device_map = expand_device_map(device_map, original_loaded_keys, start_prefix) str_dtype = str(dtype).replace("torch.", "") if dtype is not None else "float32" if sharded_metadata is None: archive_file = ( resolved_archive_file[0] if isinstance(resolved_archive_file, (list, tuple)) else resolved_archive_file ) weight_map = {p: archive_file for p in original_loaded_keys} else: weight_map = {p: os.path.join(folder, f) for p, f in sharded_metadata["weight_map"].items()} offload_index = { p[len(start_prefix) :]: {"safetensors_file": f, "weight_name": p, "dtype": str_dtype}
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
for p, f in weight_map.items() if p.startswith(start_prefix) and param_device_map[p[len(start_prefix) :]] == "disk" } else: offload_index = None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if state_dict is not None: # Whole checkpoint mismatched_keys = _find_mismatched_keys( state_dict, model_state_dict, loaded_keys, original_loaded_keys, add_prefix_to_model, remove_prefix_from_model, ignore_mismatched_sizes, )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# For GGUF models `state_dict` is never set to None as the state dict is always small if gguf_path or low_cpu_mem_usage: fixed_state_dict = cls._fix_state_dict_keys_on_load(state_dict) error_msgs, offload_index, state_dict_index = _load_state_dict_into_meta_model( model_to_load, fixed_state_dict, start_prefix, expected_keys, device_map=device_map, offload_folder=offload_folder, offload_index=offload_index, state_dict_folder=state_dict_folder, state_dict_index=state_dict_index, dtype=dtype, hf_quantizer=hf_quantizer, is_safetensors=is_safetensors, keep_in_fp32_modules=keep_in_fp32_modules, unexpected_keys=unexpected_keys, ) else:
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Sharded checkpoint or whole but low_cpu_mem_usage==True assign_to_params_buffers = check_support_param_buffer_assignment( model_to_load, state_dict, start_prefix ) fixed_state_dict = cls._fix_state_dict_keys_on_load(state_dict) error_msgs = _load_state_dict_into_model( model_to_load, fixed_state_dict, start_prefix, assign_to_params_buffers )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
else: # This should always be a list but, just to be sure. if not isinstance(resolved_archive_file, list): resolved_archive_file = [resolved_archive_file] error_msgs = [] mismatched_keys = [] if not is_safetensors: offload_index = {} if device_map is not None and "disk" in device_map.values() else None if offload_state_dict: state_dict_folder = tempfile.mkdtemp() state_dict_index = {} else: state_dict_folder = None state_dict_index = None if is_sharded_safetensors: disk_only_shard_files = get_disk_only_shard_files( device_map, sharded_metadata=sharded_metadata, start_prefix=start_prefix ) disk_only_shard_files = [os.path.join(folder, f) for f in disk_only_shard_files] else: disk_only_shard_files = []
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if len(resolved_archive_file) > 1: resolved_archive_file = logging.tqdm(resolved_archive_file, desc="Loading checkpoint shards") assign_to_params_buffers = None for shard_file in resolved_archive_file: # Skip the load for shards that only contain disk-offloaded weights when using safetensors for the offload. if shard_file in disk_only_shard_files: continue map_location = None if ( device_map is not None and hf_quantizer is not None and hf_quantizer.quantization_config.quant_method == QuantizationMethod.TORCHAO and hf_quantizer.quantization_config.quant_type == "int4_weight_only" ): map_location = torch.device([d for d in device_map.values() if d not in ["cpu", "disk"]][0]) state_dict = load_state_dict(
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
shard_file, is_quantized=is_quantized, map_location=map_location, weights_only=weights_only )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Mistmatched keys contains tuples key/shape1/shape2 of weights in the checkpoint that have a shape not # matching the weights in the model. mismatched_keys += _find_mismatched_keys( state_dict, model_state_dict, loaded_keys, original_loaded_keys, add_prefix_to_model, remove_prefix_from_model, ignore_mismatched_sizes, ) if low_cpu_mem_usage: if is_fsdp_enabled() and not is_local_dist_rank_0() and not is_quantized: for key, param in model_to_load.state_dict().items(): if param.device == torch.device("meta"): set_module_tensor_to_device( model_to_load, key, "cpu", torch.empty(*param.size(), dtype=dtype) )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
else: fixed_state_dict = cls._fix_state_dict_keys_on_load(state_dict) new_error_msgs, offload_index, state_dict_index = _load_state_dict_into_meta_model( model_to_load, fixed_state_dict, start_prefix, expected_keys, device_map=device_map, offload_folder=offload_folder, offload_index=offload_index, state_dict_folder=state_dict_folder, state_dict_index=state_dict_index, dtype=dtype, hf_quantizer=hf_quantizer, is_safetensors=is_safetensors, keep_in_fp32_modules=keep_in_fp32_modules, unexpected_keys=unexpected_keys, )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
error_msgs += new_error_msgs else: # Sharded checkpoint or whole but low_cpu_mem_usage==True if assign_to_params_buffers is None: assign_to_params_buffers = check_support_param_buffer_assignment( model_to_load, state_dict, start_prefix ) fixed_state_dict = cls._fix_state_dict_keys_on_load(state_dict) error_msgs += _load_state_dict_into_model( model_to_load, fixed_state_dict, start_prefix, assign_to_params_buffers )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# force memory release del state_dict gc.collect() if offload_index is not None and len(offload_index) > 0: if model != model_to_load: # We need to add the prefix of the base model prefix = cls.base_model_prefix if not is_safetensors: for weight_name in offload_index: shutil.move( os.path.join(offload_folder, f"{weight_name}.dat"), os.path.join(offload_folder, f"{prefix}.{weight_name}.dat"), ) offload_index = {f"{prefix}.{key}": value for key, value in offload_index.items()} if not is_safetensors: save_offload_index(offload_index, offload_folder) offload_index = None
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if offload_state_dict: # Load back temporarily offloaded state dict load_offloaded_weights(model_to_load, state_dict_index, state_dict_folder) shutil.rmtree(state_dict_folder) if len(error_msgs) > 0: error_msg = "\n\t".join(error_msgs) if "size mismatch" in error_msg: error_msg += ( "\n\tYou may consider adding `ignore_mismatched_sizes=True` in the model `from_pretrained` method." ) raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if len(unexpected_keys) > 0: archs = [] if model.config.architectures is None else model.config.architectures warner = logger.warning if model.__class__.__name__ in archs else logger.info warner( f"Some weights of the model checkpoint at {pretrained_model_name_or_path} were not used when" f" initializing {model.__class__.__name__}: {unexpected_keys}\n- This IS expected if you are" f" initializing {model.__class__.__name__} from the checkpoint of a model trained on another task or" " with another architecture (e.g. initializing a BertForSequenceClassification model from a" " BertForPreTraining model).\n- This IS NOT expected if you are initializing" f" {model.__class__.__name__} from the checkpoint of a model that you expect to be exactly identical" " (initializing a BertForSequenceClassification model from a BertForSequenceClassification model)."
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
) else: logger.info(f"All model checkpoint weights were used when initializing {model.__class__.__name__}.\n") if len(missing_keys) > 0: logger.warning( f"Some weights of {model.__class__.__name__} were not initialized from the model checkpoint at" f" {pretrained_model_name_or_path} and are newly initialized: {missing_keys}\nYou should probably" " TRAIN this model on a down-stream task to be able to use it for predictions and inference." ) elif len(mismatched_keys) == 0: logger.info( f"All the weights of {model.__class__.__name__} were initialized from the model checkpoint at" f" {pretrained_model_name_or_path}.\nIf your task is similar to the task the model of the checkpoint" f" was trained on, you can already use {model.__class__.__name__} for predictions without further" " training." )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
if len(mismatched_keys) > 0: mismatched_warning = "\n".join( [ f"- {key}: found shape {shape1} in the checkpoint and {shape2} in the model instantiated" for key, shape1, shape2 in mismatched_keys ] ) logger.warning( f"Some weights of {model.__class__.__name__} were not initialized from the model checkpoint at" f" {pretrained_model_name_or_path} and are newly initialized because the shapes did not" f" match:\n{mismatched_warning}\nYou should probably TRAIN this model on a down-stream task to be able" " to use it for predictions and inference." )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
return model, missing_keys, unexpected_keys, mismatched_keys, offload_index, error_msgs def retrieve_modules_from_names(self, names, add_prefix=False, remove_prefix=False): module_keys = {".".join(key.split(".")[:-1]) for key in names} # torch.nn.ParameterList is a special case where two parameter keywords # are appended to the module name, *e.g.* bert.special_embeddings.0 module_keys = module_keys.union( {".".join(key.split(".")[:-2]) for key in names if len(key) > 0 and key[-1].isdigit()} )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
retrieved_modules = [] # retrieve all modules that has at least one missing weight name for name, module in self.named_modules(): if remove_prefix: _prefix = f"{self.base_model_prefix}." name = name[len(_prefix) :] if name.startswith(_prefix) else name elif add_prefix: name = ".".join([self.base_model_prefix, name]) if len(name) > 0 else self.base_model_prefix if name in module_keys: retrieved_modules.append(module) return retrieved_modules @staticmethod def _load_pretrained_model_low_mem( model, loaded_state_dict_keys, resolved_archive_file, start_prefix="", hf_quantizer=None, pretrained_model_name_or_path=None, weights_only=True, ): """ This is an experimental function that loads the model using ~1.x model size CPU memory Before you call it do:
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
1. save which state_dict keys are available 2. drop state_dict before model is created, since the latter takes 1x model size memory Here then we continue: 3. switch to the meta device all params/buffers that are going to be replaced from the loaded state_dict 4. load state_dict 2nd time 5. replace the params/buffers from the state_dict Currently, it doesn't handle missing_keys, unexpected_keys, mismatched_keys. It can't handle deepspeed. To handle bitsandbytes, needs non-empty hf_quantizer argument. """
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
_move_model_to_meta(model, loaded_state_dict_keys, start_prefix) state_dict = load_state_dict(resolved_archive_file, weights_only=weights_only) expected_keys = loaded_state_dict_keys # plug for missing expected_keys. TODO: replace with proper keys fixed_state_dict = model._fix_state_dict_keys_on_load(state_dict) error_msgs = _load_state_dict_into_meta_model( model, fixed_state_dict, start_prefix, expected_keys=expected_keys, hf_quantizer=hf_quantizer, ) return error_msgs @classmethod def register_for_auto_class(cls, auto_class="AutoModel"): """ Register this class with a given auto class. This should only be used for custom models as the ones in the library are already mapped with an auto class. <Tip warning={true}> This API is experimental and may have some slight breaking changes in the next releases. </Tip>
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
Args: auto_class (`str` or `type`, *optional*, defaults to `"AutoModel"`): The auto class to register this new model with. """ if not isinstance(auto_class, str): auto_class = auto_class.__name__ import transformers.models.auto as auto_module if not hasattr(auto_module, auto_class): raise ValueError(f"{auto_class} is not a valid auto class.") cls._auto_class = auto_class def to_bettertransformer(self) -> "PreTrainedModel": """ Converts the model to use [PyTorch's native attention implementation](https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html), integrated to Transformers through [Optimum library](https://huggingface.co/docs/optimum/bettertransformer/overview). Only a subset of all Transformers models are supported.
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
PyTorch's attention fastpath allows to speed up inference through kernel fusions and the use of [nested tensors](https://pytorch.org/docs/stable/nested.html). Detailed benchmarks can be found in [this blog post](https://medium.com/pytorch/bettertransformer-out-of-the-box-performance-for-huggingface-transformers-3fbe27d50ab2). Returns: [`PreTrainedModel`]: The model converted to BetterTransformer. """ if not is_optimum_available(): raise ImportError("The package `optimum` is required to use Better Transformer.") from optimum.version import __version__ as optimum_version if version.parse(optimum_version) < version.parse("1.7.0"): raise ImportError( f"Please install optimum>=1.7.0 to use Better Transformer. The version {optimum_version} was found." ) from optimum.bettertransformer import BetterTransformer return BetterTransformer.transform(self)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
def reverse_bettertransformer(self): """ Reverts the transformation from [`~PreTrainedModel.to_bettertransformer`] so that the original modeling is used, for example in order to save the model. Returns: [`PreTrainedModel`]: The model converted back to the original modeling. """ if not is_optimum_available(): raise ImportError("The package `optimum` is required to use Better Transformer.") from optimum.version import __version__ as optimum_version if version.parse(optimum_version) < version.parse("1.7.0"): raise ImportError( f"Please install optimum>=1.7.0 to use Better Transformer. The version {optimum_version} was found." ) from optimum.bettertransformer import BetterTransformer return BetterTransformer.reverse(self)
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
def warn_if_padding_and_no_attention_mask(self, input_ids, attention_mask): """ Shows a one-time warning if the input_ids appear to contain padding and no attention mask was given. """ # Skip the check during tracing. if is_torch_fx_proxy(input_ids) or torch.jit.is_tracing() or is_torchdynamo_compiling(): return if (attention_mask is not None) or (self.config.pad_token_id is None): return # Check only the first and last input IDs to reduce overhead. if self.config.pad_token_id in input_ids[:, [-1, 0]]: warn_string = ( "We strongly recommend passing in an `attention_mask` since your input_ids may be padded. See " "https://huggingface.co/docs/transformers/troubleshooting" "#incorrect-output-when-padding-tokens-arent-masked." )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# If the pad token is equal to either BOS, EOS, or SEP, we do not know whether the user should use an # attention_mask or not. In this case, we should still show a warning because this is a rare case. if ( (self.config.bos_token_id is not None and self.config.bos_token_id == self.config.pad_token_id) or (self.config.eos_token_id is not None and self.config.eos_token_id == self.config.pad_token_id) or (self.config.sep_token_id is not None and self.config.sep_token_id == self.config.pad_token_id) ): warn_string += ( f"\nYou may ignore this warning if your `pad_token_id` ({self.config.pad_token_id}) is identical " f"to the `bos_token_id` ({self.config.bos_token_id}), `eos_token_id` ({self.config.eos_token_id}), " f"or the `sep_token_id` ({self.config.sep_token_id}), and your input is not padded." )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
logger.warning_once(warn_string) @property def supports_tp_plan(self): """ Returns whether the model has a tensor parallelism plan. """ if self._tp_plan is not None: return True # Check if base model has a TP plan if getattr(self.base_model, "_tp_plan", None) is not None: return True return False def tensor_parallel(self, device_mesh): """ Tensor parallelize the model across the given device mesh. Args: device_mesh (`torch.distributed.DeviceMesh`): The device mesh to use for tensor parallelism. """ if not is_torch_greater_or_equal("2.5"): raise EnvironmentError("tensor parallel is only supported for `torch>=2.5`.")
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# Tensor parallelize a nn.Module based on the `_tp_plan` attribute of the module. # No op if `_tp_plan` attribute does not exist under the module. # This is a helper function to be used with `model.apply` to recursively # parallelize a model. def tplize(mod: torch.nn.Module) -> None: tp_plan = getattr(mod, "_tp_plan", None) if tp_plan is None: return logger.debug(f"Applying tensor parallel to {mod.__class__.__name__}: {tp_plan}") # In model configs, we use a neutral type (string) to specify # parallel styles, here we translate them into torch TP types. # Using tree_map because `tp_plan` is a dict. tp_plan = torch.utils._pytree.tree_map( translate_to_torch_parallel_style, tp_plan, ) # Apply TP to current module. torch.distributed.tensor.parallel.parallelize_module( mod,
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
device_mesh=device_mesh, parallelize_plan=tp_plan, )
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
# `apply` is a native method of `nn.Module` that recursively applies a # function to every submodule. self.apply(tplize) @property def loss_function(self): loss_type = getattr(self, "loss_type", None) if loss_type is None or loss_type not in LOSS_MAPPING: logger.warning_once( f"`loss_type={loss_type}` was set in the config but it is unrecognised." f"Using the default loss: `ForCausalLMLoss`." ) loss_type = "ForCausalLM" return LOSS_MAPPING[loss_type]
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py
def get_compiled_call(self, compile_config: CompileConfig): """Return a `torch.compile`'d version of `self.__call__`. This is useful to dynamically choose between non-compiled/compiled `forward` during inference, especially to switch between prefill (where we don't want to use compiled version to avoid recomputing the graph with new shapes) and iterative decoding (where we want the speed-ups of compiled version with static shapes).""" # Only reset it if not present or different from previous config default_config = getattr(self.generation_config, "compile_config", CompileConfig()) if ( not hasattr(self, "_compiled_call") or getattr(self, "_last_compile_config", default_config) != compile_config ): self._last_compile_config = compile_config self._compiled_call = torch.compile(self.__call__, **compile_config.to_dict()) return self._compiled_call
230
/Users/nielsrogge/Documents/python_projecten/transformers/src/transformers/modeling_utils.py