MCPcopy Index your code
hub / github.com/huggingface/diffusers / _fetch_state_dict

Function _fetch_state_dict

src/diffusers/loaders/lora_base.py:198–274  ·  view source on GitHub ↗
(
    pretrained_model_name_or_path_or_dict,
    weight_name,
    use_safetensors,
    local_files_only,
    cache_dir,
    force_download,
    proxies,
    token,
    revision,
    subfolder,
    user_agent,
    allow_pickle,
    metadata=None,
)

Source from the content-addressed store, hash-verified

196
197
198def _fetch_state_dict(
199 pretrained_model_name_or_path_or_dict,
200 weight_name,
201 use_safetensors,
202 local_files_only,
203 cache_dir,
204 force_download,
205 proxies,
206 token,
207 revision,
208 subfolder,
209 user_agent,
210 allow_pickle,
211 metadata=None,
212):
213 model_file = None
214 if not isinstance(pretrained_model_name_or_path_or_dict, dict):
215 # Let's first try to load .safetensors weights
216 if (use_safetensors and weight_name is None) or (
217 weight_name is not None and weight_name.endswith(".safetensors")
218 ):
219 try:
220 # Here we're relaxing the loading check to enable more Inference API
221 # friendliness where sometimes, it's not at all possible to automatically
222 # determine `weight_name`.
223 if weight_name is None:
224 weight_name = _best_guess_weight_name(
225 pretrained_model_name_or_path_or_dict,
226 file_extension=".safetensors",
227 local_files_only=local_files_only,
228 )
229 model_file = _get_model_file(
230 pretrained_model_name_or_path_or_dict,
231 weights_name=weight_name or LORA_WEIGHT_NAME_SAFE,
232 cache_dir=cache_dir,
233 force_download=force_download,
234 proxies=proxies,
235 local_files_only=local_files_only,
236 token=token,
237 revision=revision,
238 subfolder=subfolder,
239 user_agent=user_agent,
240 )
241 state_dict = safetensors.torch.load_file(model_file, device="cpu")
242 metadata = _load_sft_state_dict_metadata(model_file)
243
244 except (IOError, safetensors.SafetensorError) as e:
245 if not allow_pickle:
246 raise e
247 # try loading non-safetensors weights
248 model_file = None
249 metadata = None
250 pass
251
252 if model_file is None:
253 if weight_name is None:
254 weight_name = _best_guess_weight_name(
255 pretrained_model_name_or_path_or_dict, file_extension=".bin", local_files_only=local_files_only

Callers 15

load_lora_adapterMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85
lora_state_dictMethod · 0.85

Calls 4

_best_guess_weight_nameFunction · 0.85
_get_model_fileFunction · 0.85
load_state_dictFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…