MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / from_pretrained

Method from_pretrained

diffusers/src/diffusers/models/adapter.py:146–214  ·  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, you should first set it back in training mode with `model.tra

(cls, pretrained_model_path: Optional[Union[str, os.PathLike]], **kwargs)

Source from the content-addressed store, hash-verified

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

Callers 6

runMethod · 0.45
load_ip_adapterMethod · 0.45

Calls 1

infoMethod · 0.80

Tested by

no test coverage detected