
    bi+                     z    d dl Z d dlmZmZ d dlmZ ddlmZ ddlm	Z	  e	j                  e      Z G d de      Zy)	    N)OptionalUnion)validate_hf_hub_args   )ConfigMixin)loggingc                   \    e Zd ZdZd Zeeddeee	e
j                  f      fd              Zy)	AutoModelconfig.jsonc                     t        | j                  j                   d| j                  j                   d| j                  j                   d      )Nz+ is designed to be instantiated using the `z5.from_pretrained(pretrained_model_name_or_path)` or `z.from_pipe(pipeline)` methods.)EnvironmentError	__class____name__)selfargskwargss      V/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/models/auto_model.py__init__zAutoModel.__init__   sR    ~~&&' (..112 3''((FH
 	
    Npretrained_model_or_pathc                 F   |j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  d	d      }	||||||d
}
d}d}	 d| _         | j                  |fi |
}|	|	|v r||	   \  }}|
j                  d	|	i       |l|jd| _         | j                  |fd	|	i|
}d|v r|d   }d}|
j                  d	|	i       n/d|v rd}d}|
j                  d	|	dn|	i       nt        d| d      ddlm	}m
}  ||||dd      \  }}|t        d| d      i |
|} |j                  |fi |S # t        $ r}t
        j                  |       Y d}~d}~ww xY w)uv  
        Instantiate a pretrained PyTorch 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.
            torch_dtype (`torch.dtype`, *optional*):
                Override the default `torch.dtype` and load the model with another dtype.
            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.
            device_map (`str` or `Dict[str, Union[int, str, torch.device]]`, *optional*):
                A map that specifies where each submodule should go. It doesn't need to be defined for each
                parameter/buffer name; once a given module name is inside, every submodule of it will be sent to the
                same device. Defaults to `None`, meaning that the model will be loaded on CPU.

                Set `device_map="auto"` to have 🤗 Accelerate automatically compute the most optimized `device_map`. For
                more information about each option see [designing a device
                map](https://hf.co/docs/accelerate/main/en/usage_guides/big_modeling#designing-a-device-map).
            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.
            low_cpu_mem_usage (`bool`, *optional*, defaults to `True` if torch version >= 1.9.0 else `False`):
                Speed up model loading only loading the pretrained weights and not initializing the weights. This also
                tries to not use more than 1x model size in CPU memory (including peak memory) while loading the model.
                Only supported for PyTorch >= 1.9.0. If you are using an older version of PyTorch, setting this
                argument to `True` will raise an error.
            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 `hf
        auth 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 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:
        - conv_in.weight: found shape torch.Size([320, 4, 3, 3]) in the checkpoint and torch.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_dirNforce_downloadFproxiestokenlocal_files_onlyrevision	subfolder)r   r   r   r   r   r   zmodel_index.jsonr   _class_name	diffusers
model_typer
   transformers z7Couldn't find model associated with the config file at .r   )ALL_IMPORTABLE_CLASSESget_class_obj_and_candidates)library_name
class_nameimportable_classes	pipelinesis_pipeline_modulez'AutoModel can't find a model linked to )popconfig_nameload_configupdater   loggerdebug
ValueError pipelines.pipeline_loading_utilsr%   r&   from_pretrained)clsr   r   r   r   r   r   r   r   r   load_config_kwargslibraryorig_class_nameconfiger%   r&   	model_cls_s                      r   r4   zAutoModel.from_pretrained%   s   T JJ{D1	$4e<**Y-

7D)!::&8%@::j$/JJ{D1	 #, 0 
 		0CO$S__%=TASTF$f)<+1)+<("));	*BC ?6+CO$S__%=iiVhiF&"("7%"));	*BC'"-("));i>OU^*_` #Z[sZttu!vwwk3 &5$
	1 FFWWXYZZ1&1&1(y(()ALVLLE   	LLOO	s   ;E8 8	F FF )N)r   
__module____qualname__r-   r   classmethodr   r   r   strosPathLiker4    r   r   r
   r
      sH    K
 gMxc2;;FV@W7X gM  gMr   r
   )rA   typingr   r   huggingface_hub.utilsr   configuration_utilsr   utilsr   
get_loggerr   r0   r
   rC   r   r   <module>rI      s:    
 " 6 -  
		H	%sM sMr   