Skip to content

AutoModel

The AutoModel is designed to make it easy to load a checkpoint without needing to know the specific model class. AutoModel automatically retrieves the correct model class from the checkpoint config.json file.

from mindone.diffusers import AutoModel, AutoPipelineForText2Image

unet = AutoModel.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", subfolder="unet")
pipe = AutoPipelineForText2Image.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", unet=unet)

mindone.diffusers.AutoModel

Bases: ConfigMixin

Source code in mindone/diffusers/models/auto_model.py
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class AutoModel(ConfigMixin):
    config_name = "config.json"

    def __init__(self, *args, **kwargs):
        raise EnvironmentError(
            f"{self.__class__.__name__} is designed to be instantiated "
            f"using the `{self.__class__.__name__}.from_pretrained(pretrained_model_name_or_path)` or "
            f"`{self.__class__.__name__}.from_pipe(pipeline)` methods."
        )

    @classmethod
    @validate_hf_hub_args
    def from_pretrained(cls, pretrained_model_or_path: Optional[Union[str, os.PathLike]] = None, **kwargs):
        r"""
        Instantiate a pretrained MindSpore model from a pretrained model configuration.

        The model is set in evaluation mode - `model.eval()` - by default, and dropout modules are deactivated. To
        train the model, set it back in training mode with `model.train()`.

        Parameters:
            pretrained_model_name_or_path (`str` or `os.PathLike`, *optional*):
                Can be either:

                    - A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
                      the Hub.
                    - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
                      with [`~ModelMixin.save_pretrained`].

            cache_dir (`Union[str, os.PathLike]`, *optional*):
                Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
                is not used.
            mindspore_dtype (`str` or `mindspore.Type`, *optional*):
                Override the default `mindspore.Type` and load the model with another dtype. If `"auto"` is passed, the
                dtype is automatically derived from the model's weights.
            force_download (`bool`, *optional*, defaults to `False`):
                Whether or not to force the (re-)download of the model weights and configuration files, overriding the
                cached versions if they exist.
            proxies (`Dict[str, str]`, *optional*):
                A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
                'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
            output_loading_info (`bool`, *optional*, defaults to `False`):
                Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
            local_files_only(`bool`, *optional*, defaults to `False`):
                Whether to only load local model weights and configuration files or not. If set to `True`, the model
                won't be downloaded from the Hub.
            token (`str` or *bool*, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
                `diffusers-cli login` (stored in `~/.huggingface`) is used.
            revision (`str`, *optional*, defaults to `"main"`):
                The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier
                allowed by Git.
            from_flax (`bool`, *optional*, defaults to `False`):
                Load the model weights from a Flax checkpoint save file.
            subfolder (`str`, *optional*, defaults to `""`):
                The subfolder location of a model file within a larger model repository on the Hub or locally.
            mirror (`str`, *optional*):
                Mirror source to resolve accessibility issues if you're downloading a model in China. We do not
                guarantee the timeliness or safety of the source, and you should refer to the mirror site for more
                information.
            max_memory (`Dict`, *optional*):
                A dictionary device identifier for the maximum memory. Will default to the maximum memory available for
                each GPU and the available CPU RAM if unset.
            offload_folder (`str` or `os.PathLike`, *optional*):
                The path to offload weights if `device_map` contains the value `"disk"`.
            offload_state_dict (`bool`, *optional*):
                If `True`, temporarily offloads the CPU state dict to the hard drive to avoid running out of CPU RAM if
                the weight of the CPU state dict + the biggest shard of the checkpoint does not fit. Defaults to `True`
                when there is some disk offload.
            variant (`str`, *optional*):
                Load weights from a specified `variant` filename such as `"fp16"` or `"ema"`. This is ignored when
                loading `from_flax`.
            use_safetensors (`bool`, *optional*, defaults to `None`):
                If set to `None`, the `safetensors` weights are downloaded if they're available **and** if the
                `safetensors` library is installed. If set to `True`, the model is forcibly loaded from `safetensors`
                weights. If set to `False`, `safetensors` weights are not loaded.
            disable_mmap ('bool', *optional*, defaults to 'False'):
                Whether to disable mmap when loading a Safetensors model. This option can perform better when the model
                is on a network mount or hard drive, which may not handle the seeky-ness of mmap very well.

        <Tip>

        To use private or [gated models](https://huggingface.co/docs/hub/models-gated#gated-models), log-in with
        `huggingface-cli login`. You can also activate the special
        ["offline-mode"](https://huggingface.co/diffusers/installation.html#offline-mode) to use this method in a
        firewalled environment.

        </Tip>

        Example:

        ```py
        from mindone.diffusers import AutoModel

        unet = AutoModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")
        ```

        If you get the error message below, you need to finetune the weights for your downstream task:

        ```bash
        Some weights of UNet2DConditionModel were not initialized from the model checkpoint at runwayml/stable-diffusion-v1-5 and are newly initialized because the shapes did not match: # noqa
        - conv_in.weight: found shape mindspore.Size([320, 4, 3, 3]) in the checkpoint and mindspore.Size([320, 9, 3, 3]) in the model instantiated
        You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
        ```
        """
        cache_dir = kwargs.pop("cache_dir", None)
        force_download = kwargs.pop("force_download", False)
        proxies = kwargs.pop("proxies", None)
        token = kwargs.pop("token", None)
        local_files_only = kwargs.pop("local_files_only", False)
        revision = kwargs.pop("revision", None)
        subfolder = kwargs.pop("subfolder", None)

        load_config_kwargs = {
            "cache_dir": cache_dir,
            "force_download": force_download,
            "proxies": proxies,
            "token": token,
            "local_files_only": local_files_only,
            "revision": revision,
            "subfolder": subfolder,
        }

        config = cls.load_config(pretrained_model_or_path, **load_config_kwargs)
        orig_class_name = config["_class_name"]

        library = importlib.import_module("mindone.diffusers")

        model_cls = getattr(library, orig_class_name, None)
        if model_cls is None:
            raise ValueError(f"AutoModel can't find a model linked to {orig_class_name}.")

        kwargs = {**load_config_kwargs, **kwargs}
        return model_cls.from_pretrained(pretrained_model_or_path, **kwargs)

mindone.diffusers.AutoModel.from_pretrained(pretrained_model_or_path=None, **kwargs) classmethod

Instantiate a pretrained MindSpore model from a pretrained model configuration.

The model is set in evaluation mode - model.eval() - by default, and dropout modules are deactivated. To train the model, set it back in training mode with model.train().

PARAMETER DESCRIPTION
pretrained_model_name_or_path

Can be either:

- A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
  the Hub.
- A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
  with [`~ModelMixin.save_pretrained`].

TYPE: `str` or `os.PathLike`, *optional*

cache_dir

Path to a directory where a downloaded pretrained model configuration is cached if the standard cache is not used.

TYPE: `Union[str, os.PathLike]`, *optional*

mindspore_dtype

Override the default mindspore.Type and load the model with another dtype. If "auto" is passed, the dtype is automatically derived from the model's weights.

TYPE: `str` or `mindspore.Type`, *optional*

force_download

Whether or not to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist.

TYPE: `bool`, *optional*, defaults to `False`

proxies

A dictionary of proxy servers to use by protocol or endpoint, for example, {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.

TYPE: `Dict[str, str]`, *optional*

output_loading_info

Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.

TYPE: `bool`, *optional*, defaults to `False`

local_files_only(`bool`,

Whether to only load local model weights and configuration files or not. If set to True, the model won't be downloaded from the Hub.

TYPE: *optional*, defaults to `False`

token

The token to use as HTTP bearer authorization for remote files. If True, the token generated from diffusers-cli login (stored in ~/.huggingface) is used.

TYPE: `str` or *bool*, *optional*

revision

The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier allowed by Git.

TYPE: `str`, *optional*, defaults to `"main"`

from_flax

Load the model weights from a Flax checkpoint save file.

TYPE: `bool`, *optional*, defaults to `False`

subfolder

The subfolder location of a model file within a larger model repository on the Hub or locally.

TYPE: `str`, *optional*, defaults to `""`

mirror

Mirror source to resolve accessibility issues if you're downloading a model in China. We do not guarantee the timeliness or safety of the source, and you should refer to the mirror site for more information.

TYPE: `str`, *optional*

max_memory

A dictionary device identifier for the maximum memory. Will default to the maximum memory available for each GPU and the available CPU RAM if unset.

TYPE: `Dict`, *optional*

offload_folder

The path to offload weights if device_map contains the value "disk".

TYPE: `str` or `os.PathLike`, *optional*

offload_state_dict

If True, temporarily offloads the CPU state dict to the hard drive to avoid running out of CPU RAM if the weight of the CPU state dict + the biggest shard of the checkpoint does not fit. Defaults to True when there is some disk offload.

TYPE: `bool`, *optional*

variant

Load weights from a specified variant filename such as "fp16" or "ema". This is ignored when loading from_flax.

TYPE: `str`, *optional*

use_safetensors

If set to None, the safetensors weights are downloaded if they're available and if the safetensors library is installed. If set to True, the model is forcibly loaded from safetensors weights. If set to False, safetensors weights are not loaded.

TYPE: `bool`, *optional*, defaults to `None`

disable_mmap

Whether to disable mmap when loading a Safetensors model. This option can perform better when the model is on a network mount or hard drive, which may not handle the seeky-ness of mmap very well.

TYPE: 'bool', *optional*, defaults to 'False'

To use private or gated models, log-in with huggingface-cli login. You can also activate the special "offline-mode" to use this method in a firewalled environment.

Example:

from mindone.diffusers import AutoModel

unet = AutoModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")

If you get the error message below, you need to finetune the weights for your downstream task:

Some weights of UNet2DConditionModel were not initialized from the model checkpoint at runwayml/stable-diffusion-v1-5 and are newly initialized because the shapes did not match: # noqa
- conv_in.weight: found shape mindspore.Size([320, 4, 3, 3]) in the checkpoint and mindspore.Size([320, 9, 3, 3]) in the model instantiated
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
Source code in mindone/diffusers/models/auto_model.py
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
@classmethod
@validate_hf_hub_args
def from_pretrained(cls, pretrained_model_or_path: Optional[Union[str, os.PathLike]] = None, **kwargs):
    r"""
    Instantiate a pretrained MindSpore model from a pretrained model configuration.

    The model is set in evaluation mode - `model.eval()` - by default, and dropout modules are deactivated. To
    train the model, set it back in training mode with `model.train()`.

    Parameters:
        pretrained_model_name_or_path (`str` or `os.PathLike`, *optional*):
            Can be either:

                - A string, the *model id* (for example `google/ddpm-celebahq-256`) of a pretrained model hosted on
                  the Hub.
                - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
                  with [`~ModelMixin.save_pretrained`].

        cache_dir (`Union[str, os.PathLike]`, *optional*):
            Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
            is not used.
        mindspore_dtype (`str` or `mindspore.Type`, *optional*):
            Override the default `mindspore.Type` and load the model with another dtype. If `"auto"` is passed, the
            dtype is automatically derived from the model's weights.
        force_download (`bool`, *optional*, defaults to `False`):
            Whether or not to force the (re-)download of the model weights and configuration files, overriding the
            cached versions if they exist.
        proxies (`Dict[str, str]`, *optional*):
            A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
            'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
        output_loading_info (`bool`, *optional*, defaults to `False`):
            Whether or not to also return a dictionary containing missing keys, unexpected keys and error messages.
        local_files_only(`bool`, *optional*, defaults to `False`):
            Whether to only load local model weights and configuration files or not. If set to `True`, the model
            won't be downloaded from the Hub.
        token (`str` or *bool*, *optional*):
            The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
            `diffusers-cli login` (stored in `~/.huggingface`) is used.
        revision (`str`, *optional*, defaults to `"main"`):
            The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier
            allowed by Git.
        from_flax (`bool`, *optional*, defaults to `False`):
            Load the model weights from a Flax checkpoint save file.
        subfolder (`str`, *optional*, defaults to `""`):
            The subfolder location of a model file within a larger model repository on the Hub or locally.
        mirror (`str`, *optional*):
            Mirror source to resolve accessibility issues if you're downloading a model in China. We do not
            guarantee the timeliness or safety of the source, and you should refer to the mirror site for more
            information.
        max_memory (`Dict`, *optional*):
            A dictionary device identifier for the maximum memory. Will default to the maximum memory available for
            each GPU and the available CPU RAM if unset.
        offload_folder (`str` or `os.PathLike`, *optional*):
            The path to offload weights if `device_map` contains the value `"disk"`.
        offload_state_dict (`bool`, *optional*):
            If `True`, temporarily offloads the CPU state dict to the hard drive to avoid running out of CPU RAM if
            the weight of the CPU state dict + the biggest shard of the checkpoint does not fit. Defaults to `True`
            when there is some disk offload.
        variant (`str`, *optional*):
            Load weights from a specified `variant` filename such as `"fp16"` or `"ema"`. This is ignored when
            loading `from_flax`.
        use_safetensors (`bool`, *optional*, defaults to `None`):
            If set to `None`, the `safetensors` weights are downloaded if they're available **and** if the
            `safetensors` library is installed. If set to `True`, the model is forcibly loaded from `safetensors`
            weights. If set to `False`, `safetensors` weights are not loaded.
        disable_mmap ('bool', *optional*, defaults to 'False'):
            Whether to disable mmap when loading a Safetensors model. This option can perform better when the model
            is on a network mount or hard drive, which may not handle the seeky-ness of mmap very well.

    <Tip>

    To use private or [gated models](https://huggingface.co/docs/hub/models-gated#gated-models), log-in with
    `huggingface-cli login`. You can also activate the special
    ["offline-mode"](https://huggingface.co/diffusers/installation.html#offline-mode) to use this method in a
    firewalled environment.

    </Tip>

    Example:

    ```py
    from mindone.diffusers import AutoModel

    unet = AutoModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")
    ```

    If you get the error message below, you need to finetune the weights for your downstream task:

    ```bash
    Some weights of UNet2DConditionModel were not initialized from the model checkpoint at runwayml/stable-diffusion-v1-5 and are newly initialized because the shapes did not match: # noqa
    - conv_in.weight: found shape mindspore.Size([320, 4, 3, 3]) in the checkpoint and mindspore.Size([320, 9, 3, 3]) in the model instantiated
    You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
    ```
    """
    cache_dir = kwargs.pop("cache_dir", None)
    force_download = kwargs.pop("force_download", False)
    proxies = kwargs.pop("proxies", None)
    token = kwargs.pop("token", None)
    local_files_only = kwargs.pop("local_files_only", False)
    revision = kwargs.pop("revision", None)
    subfolder = kwargs.pop("subfolder", None)

    load_config_kwargs = {
        "cache_dir": cache_dir,
        "force_download": force_download,
        "proxies": proxies,
        "token": token,
        "local_files_only": local_files_only,
        "revision": revision,
        "subfolder": subfolder,
    }

    config = cls.load_config(pretrained_model_or_path, **load_config_kwargs)
    orig_class_name = config["_class_name"]

    library = importlib.import_module("mindone.diffusers")

    model_cls = getattr(library, orig_class_name, None)
    if model_cls is None:
        raise ValueError(f"AutoModel can't find a model linked to {orig_class_name}.")

    kwargs = {**load_config_kwargs, **kwargs}
    return model_cls.from_pretrained(pretrained_model_or_path, **kwargs)