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

Method from_pretrained

src/diffusers/models/adapter.py:148–213  ·  view source on GitHub ↗

r""" Instantiate a pretrained `MultiAdapter` model from multiple pre-trained adapter models. The model is set in evaluation mode by default using `model.eval()` (Dropout modules are deactivated). To train the model, set it back to training mode using `model.train()`.

(cls, pretrained_model_path: str | os.PathLike | None, **kwargs)

Source from the content-addressed store, hash-verified

146
147 @classmethod
148 def from_pretrained(cls, pretrained_model_path: str | os.PathLike | None, **kwargs):
149 r"""
150 Instantiate a pretrained `MultiAdapter` model from multiple pre-trained adapter models.
151
152 The model is set in evaluation mode by default using `model.eval()` (Dropout modules are deactivated). To train
153 the model, set it back to training mode using `model.train()`.
154
155 Warnings:
156 *Weights from XXX not initialized from pretrained model* means that the weights of XXX are not pretrained
157 with the rest of the model. It is up to you to train those weights with a downstream fine-tuning. *Weights
158 from XXX not used in YYY* means that the layer XXX is not used by YYY, so those weights are discarded.
159
160 Args:
161 pretrained_model_path (`os.PathLike`):
162 A path to a *directory* containing model weights saved using
163 [`~diffusers.models.adapter.MultiAdapter.save_pretrained`], e.g., `./my_model_directory/adapter`.
164 torch_dtype (`torch.dtype`, *optional*):
165 Override the default `torch.dtype` and load the model under this dtype.
166 output_loading_info(`bool`, *optional*, defaults to `False`):
167 Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
168 device_map (`str` or `dict[str, int | str | torch.device]`, *optional*):
169 A map that specifies where each submodule should go. It doesn't need to be refined to each
170 parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the
171 same device.
172
173 To have Accelerate compute the most optimized `device_map` automatically, set `device_map="auto"`. For
174 more information about each option see [designing a device
175 map](https://hf.co/docs/accelerate/main/en/usage_guides/big_modeling#designing-a-device-map).
176 max_memory (`Dict`, *optional*):
177 A dictionary mapping device identifiers to their maximum memory. Default to the maximum memory
178 available for each GPU and the available CPU RAM if unset.
179 low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
180 Speed up model loading by not initializing the weights and only loading the pre-trained weights. This
181 also tries to not use more than 1x model size in CPU memory (including peak memory) while loading the
182 model. This is only supported when torch version >= 1.9.0. If you are using an older version of torch,
183 setting this argument to `True` will raise an error.
184 variant (`str`, *optional*):
185 If specified, load weights from a `variant` file (*e.g.* pytorch_model.<variant>.bin). `variant` will
186 be ignored when using `from_flax`.
187 use_safetensors (`bool`, *optional*, defaults to `None`):
188 If `None`, the `safetensors` weights will be downloaded if available **and** if`safetensors` library is
189 installed. If `True`, the model will be forcibly loaded from`safetensors` weights. If `False`,
190 `safetensors` is not used.
191 """
192 idx = 0
193 adapters = []
194
195 # load adapter and append to list until no adapter directory exists anymore
196 # first adapter has to be saved under `./mydirectory/adapter` to be compliant with `DiffusionPipeline.from_pretrained`
197 # second, third, ... adapters have to be saved under `./mydirectory/adapter_1`, `./mydirectory/adapter_2`, ...
198 model_path_to_load = pretrained_model_path
199 while os.path.isdir(model_path_to_load):
200 adapter = T2IAdapter.from_pretrained(model_path_to_load, **kwargs)
201 adapters.append(adapter)
202
203 idx += 1
204 model_path_to_load = pretrained_model_path + f"_{idx}"
205

Callers 8

runMethod · 0.45
load_ip_adapterMethod · 0.45
load_ip_adapterMethod · 0.45
load_ip_adapterMethod · 0.45

Calls 1

infoMethod · 0.80

Tested by

no test coverage detected