
    bi+i                     "   d dl Z d dlmZ d dlmZmZmZ d dlZd dlm	Z
 d dlZd dlmZmZ d dlmZmZ d dlmZmZ d dlmZmZ d dlmZmZmZmZ d d	lmZ d
dl m!Z!m"Z" d
dl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+  e)jX                  e-      Z. G d de(      Z/y)    N)UnpicklingError)AnyDictUnion)
FrozenDictunfreeze)
from_bytesto_bytes)flatten_dictunflatten_dict)create_repohf_hub_download)EntryNotFoundErrorRepositoryNotFoundErrorRevisionNotFoundErrorvalidate_hf_hub_args)	HTTPError   )__version__is_torch_available)CONFIG_NAMEFLAX_WEIGHTS_NAMEHUGGINGFACE_CO_RESOLVE_ENDPOINTWEIGHTS_NAMEPushToHubMixinlogging   )"convert_pytorch_state_dict_to_flaxc            	          e Zd ZdZeZg dZg dZed        Z	dde
eef   dej                  ded	efd
Zdde
eef   defdZdde
eef   defdZdde
eef   defdZdej*                  d	efdZeeej0                  fde
eej6                  f   dej                  fd              Z	 	 dde
eej6                  f   de
eef   dedefdZy)FlaxModelMixina*  
    Base class for all Flax models.

    [`FlaxModelMixin`] takes care of storing the model configuration and provides methods for loading, downloading and
    saving models.

        - **config_name** ([`str`]) -- Filename to save a model to when calling [`~FlaxModelMixin.save_pretrained`].
    )_diffusers_version_class_name_name_or_path)nameparentdtypec                      | |fi |S )zZ
        All context managers that the model should be initialized under go here.
         )clsconfigkwargss      _/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/models/modeling_flax_utils.py_from_configzFlaxModelMixin._from_config@   s    
 6$V$$    Nparamsr&   maskreturnc                    fd}|t        j                  ||      S t        |      }t        j                  |      \  }}t	        ||j                               D ]  \  }}	|s	||	   }
 ||
      ||	<    t        |      S )zk
        Helper method to cast floating-point values of given parameter `PyTree` to given `dtype`.
        c                     t        | t        j                        r?t        j                  | j                  t        j
                        r| j                        } | S N)
isinstancejnpndarray
issubdtyper&   floatingastype)paramr&   s    r,   conditional_castz:FlaxModelMixin._cast_floating_to.<locals>.conditional_castM   s9    %-#..cll2[U+Lr.   )jaxtree_mapr   tree_flattenzipkeysr   )selfr/   r&   r0   r<   flat_params	flat_mask_maskedkeyr;   s     `        r,   _cast_floating_toz FlaxModelMixin._cast_floating_toG   s    	
 <<< 0&99"6*''-	1y+*:*:*<= 	;KFC#C(#3E#:C 	;
 k**r.   c                 D    | j                  |t        j                  |      S )a  
        Cast the floating-point `params` to `jax.numpy.bfloat16`. This returns a new `params` tree and does not cast
        the `params` in place.

        This method can be used on a TPU to explicitly convert the model parameters to bfloat16 precision to do full
        half-precision training or to save weights in bfloat16 for inference in order to save memory and improve speed.

        Arguments:
            params (`Union[Dict, FrozenDict]`):
                A `PyTree` of model parameters.
            mask (`Union[Dict, FrozenDict]`):
                A `PyTree` with same structure as the `params` tree. The leaves should be booleans. It should be `True`
                for params you want to cast, and `False` for those you want to skip.

        Examples:

        ```python
        >>> from diffusers import FlaxUNet2DConditionModel

        >>> # load model
        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5")
        >>> # By default, the model parameters will be in fp32 precision, to cast these to bfloat16 precision
        >>> params = model.to_bf16(params)
        >>> # If you don't want to cast certain parameters (for example layer norm bias and scale)
        >>> # then pass the mask as follows
        >>> from flax import traverse_util

        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5")
        >>> flat_params = traverse_util.flatten_dict(params)
        >>> mask = {
        ...     path: (path[-2] != ("LayerNorm", "bias") and path[-2:] != ("LayerNorm", "scale"))
        ...     for path in flat_params
        ... }
        >>> mask = traverse_util.unflatten_dict(mask)
        >>> params = model.to_bf16(params, mask)
        ```)rH   r6   bfloat16rB   r/   r0   s      r,   to_bf16zFlaxModelMixin.to_bf16_   s    J %%fcllDAAr.   c                 D    | j                  |t        j                  |      S )a  
        Cast the floating-point `params` to `jax.numpy.float32`. This method can be used to explicitly convert the
        model parameters to fp32 precision. This returns a new `params` tree and does not cast the `params` in place.

        Arguments:
            params (`Union[Dict, FrozenDict]`):
                A `PyTree` of model parameters.
            mask (`Union[Dict, FrozenDict]`):
                A `PyTree` with same structure as the `params` tree. The leaves should be booleans. It should be `True`
                for params you want to cast, and `False` for those you want to skip.

        Examples:

        ```python
        >>> from diffusers import FlaxUNet2DConditionModel

        >>> # Download model and configuration from huggingface.co
        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5")
        >>> # By default, the model params will be in fp32, to illustrate the use of this method,
        >>> # we'll first cast to fp16 and back to fp32
        >>> params = model.to_f16(params)
        >>> # now cast back to fp32
        >>> params = model.to_fp32(params)
        ```)rH   r6   float32rK   s      r,   to_fp32zFlaxModelMixin.to_fp32   s    2 %%fckk4@@r.   c                 D    | j                  |t        j                  |      S )a  
        Cast the floating-point `params` to `jax.numpy.float16`. This returns a new `params` tree and does not cast the
        `params` in place.

        This method can be used on a GPU to explicitly convert the model parameters to float16 precision to do full
        half-precision training or to save weights in float16 for inference in order to save memory and improve speed.

        Arguments:
            params (`Union[Dict, FrozenDict]`):
                A `PyTree` of model parameters.
            mask (`Union[Dict, FrozenDict]`):
                A `PyTree` with same structure as the `params` tree. The leaves should be booleans. It should be `True`
                for params you want to cast, and `False` for those you want to skip.

        Examples:

        ```python
        >>> from diffusers import FlaxUNet2DConditionModel

        >>> # load model
        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5")
        >>> # By default, the model params will be in fp32, to cast these to float16
        >>> params = model.to_fp16(params)
        >>> # If you want don't want to cast certain parameters (for example layer norm bias and scale)
        >>> # then pass the mask as follows
        >>> from flax import traverse_util

        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5")
        >>> flat_params = traverse_util.flatten_dict(params)
        >>> mask = {
        ...     path: (path[-2] != ("LayerNorm", "bias") and path[-2:] != ("LayerNorm", "scale"))
        ...     for path in flat_params
        ... }
        >>> mask = traverse_util.unflatten_dict(mask)
        >>> params = model.to_fp16(params, mask)
        ```)rH   r6   float16rK   s      r,   to_fp16zFlaxModelMixin.to_fp16   s    J %%fckk4@@r.   rngc                     t        d|        )Nz.init_weights method has to be implemented for )NotImplementedError)rB   rS   s     r,   init_weightszFlaxModelMixin.init_weights   s    !$RSWRX"YZZr.   pretrained_model_name_or_pathc                    |j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }	|j                  dd      }
|j                  d	d      }|j                  d
d      }|j                  dd      }t        ddd}| | j                  |f|d||	|
|||d|\  }} | j                  |f|dd\  }}||nt        j
                  j                  ||      }t        j
                  j                  |      rW|r|t        j
                  j                  t        j
                  j                  |t                    st        dt         d| d      t        j
                  j                  |t              }nt        j
                  j                  t        j
                  j                  |t                    r%t        j
                  j                  |t              }nt        j
                  j                  t        j
                  j                  |t                    rt        t         d| d      t        dt         dt         d| d      	 t        ||st        nt        |||	|
||||
      }|r1t'               rd'd(lm} nt        d)       ||      }t-        ||      }n0	 t/        |d*      5 }t1        | |j3                               }ddd       tB        jD                  jG                  d/       }tI        |      }tC        jJ                  |jL                  tB        jN                  jQ                  d0      1      }tS        tI        tU        |            jW                               }tI        tU        |            }|tS        |jW                               z
  }tS        |jW                               |z
  }|r#tX        j[                  d2| d3| d4       || _.        |jW                         D ]U  } | |v s||    j^                  ||    j^                  k7  s(t#        d5|  d6||    j^                   d7||    j^                   d8       |D ]  }!||!=  ta        |      d0kD  rKtX        j[                  d9| d:|jb                  jd                   d;| d<|jb                  jd                   d=	       n-tX        jg                  d>|jb                  jd                   d?       ta        |      d0kD  r4tX        j[                  d@|jb                  jd                   dA| dB| dC       nGtX        jg                  dD|jb                  jd                   dE| dF|jb                  jd                   dG       |ti        |      fS # t        $ r t        | d      t        $ r t        | d| d      t        $ r t        | dt         d      t         $ r}t        d| d|       d}~wt"        $ r% t        d t$         d!| d"t         dt         d#	      t        $ r! t        d$| d%| d&t         dt         d	      w xY w# 1 sw Y   `xY w# t4        t6        j8                  j:                  f$ rx}	 t/        |      5 }|j3                         j=                  d+      rt?        d,      t"        |# 1 sw Y   nxY wn"# t@        t"        f$ r t        d-| d.      w xY wY d}~d}~ww xY w)Ha  
        Instantiate a pretrained Flax model from a pretrained model configuration.

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

                    - A string, the *model id* (for example `runwayml/stable-diffusion-v1-5`) of a pretrained model
                      hosted on the Hub.
                    - A path to a *directory* (for example `./my_model_directory`) containing the model weights saved
                      using [`~FlaxModelMixin.save_pretrained`].
            dtype (`jax.numpy.dtype`, *optional*, defaults to `jax.numpy.float32`):
                The data type of the computation. Can be one of `jax.numpy.float32`, `jax.numpy.float16` (on GPUs) and
                `jax.numpy.bfloat16` (on TPUs).

                This can be used to enable mixed-precision training or half-precision inference on GPUs or TPUs. If
                specified, all the computation will be performed with the given `dtype`.

                <Tip>

                This only specifies the dtype of the *computation* and does not influence the dtype of model
                parameters.

                If you wish to change the dtype of the model parameters, see [`~FlaxModelMixin.to_fp16`] and
                [`~FlaxModelMixin.to_bf16`].

                </Tip>

            model_args (sequence of positional arguments, *optional*):
                All remaining positional arguments are passed to the underlying model's `__init__` method.
            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.
            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.
            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.
            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_pt (`bool`, *optional*, defaults to `False`):
                Load the model weights from a PyTorch checkpoint save file.
            kwargs (remaining dictionary of keyword arguments, *optional*):
                Can be used to update the configuration object (after it is loaded) and initiate the model (for
                example, `output_attentions=True`). Behaves differently depending on whether a `config` is provided or
                automatically loaded:

                    - If a configuration is provided with `config`, `kwargs` are directly passed to the underlying
                      model's `__init__` method (we assume all relevant updates to the configuration have already been
                      done).
                    - If a configuration is not provided, `kwargs` are first passed to the configuration class
                      initialization function [`~ConfigMixin.from_config`]. Each key of the `kwargs` that corresponds
                      to a configuration attribute is used to override said attribute with the supplied `kwargs` value.
                      Remaining keys that do not correspond to any configuration attribute are passed to the underlying
                      model's `__init__` function.

        Examples:

        ```python
        >>> from diffusers import FlaxUNet2DConditionModel

        >>> # Download model and configuration from huggingface.co and cache.
        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5")
        >>> # Model was saved using *save_pretrained('./test/saved_model/')* (for example purposes, not runnable).
        >>> model, params = FlaxUNet2DConditionModel.from_pretrained("./test/saved_model/")
        ```

        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.
        ```
        r*   N	cache_dirforce_downloadFfrom_ptproxieslocal_files_onlytokenrevision	subfoldermodelflax)	diffusers	file_type	frameworkT)rY   return_unused_kwargsrZ   r\   r]   r^   r_   r`   )r&   rf   zError no file named z found in directory  z file found in directory z-. Please load the model using `from_pt=True`.z or .)	filenamerY   rZ   r\   r]   r^   
user_agentr`   r_   z is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
If this is a private repository, make sure to pass a token having permission to this repo with `token` or log in with `hf auth login`.z is not a valid git identifier (branch name, tag name or commit id) that exists for this model name. Check the model page at 'https://huggingface.co/z' for available revisions.z& does not appear to have a file named z:There was a specific connection error when trying to load z:
zWe couldn't connect to 'zM' to load this model, couldn't find it in the cached files and it looks like z8 is not the path to a directory containing a file named z.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.zCan't load the model for 'z'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'z=' is the correct path to a directory containing a file named r   )load_state_dictz|Can't load the model in PyTorch format because PyTorch is not installed. Please, install PyTorch or use native Flax weights.rbversionzYou seem to have cloned a repository without having git-lfs installed. Please install git-lfs and run `git lfs install` followed by `git lfs pull` in the folder you cloned.zUnable to convert z  to Flax deserializable object. c                 \    t        j                  | t        j                  d      d         S )Ncpu)backendr   )r=   
device_putlocal_devices)xs    r,   <lambda>z0FlaxModelMixin.from_pretrained.<locals>.<lambda>  s#    3CTCT]bCcdeCf1g r.   r   )rS   zThe checkpoint z is missing required keys: zI. Make sure to call model.init_weights to initialize the missing weights.z)Trying to load the pretrained weight for z failed: checkpoint has shape z, which is incompatible with the model shape z. z(Some weights of the model checkpoint at z! were not used when initializing z: z,
- This IS expected if you are initializing zU from the checkpoint of a model trained on another task or with another architecture.z9All model checkpoint weights were used when initializing z.
zSome weights of z3 were not initialized from the model checkpoint at z and are newly initialized: zo
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.zAll the weights of z/ were initialized from the model checkpoint at zf.
If your task is similar to the task the model of the checkpoint was trained on, you can already use z* for predictions without further training.)5popr   load_configfrom_configospathjoinisdirisfiler   EnvironmentErrorr   r   r   r   r   r   
ValueErrorr   r   modeling_utilsrk   r   openr	   readr   msgpack
exceptions	ExtraData
startswithOSErrorUnicodeDecodeErrorr=   	tree_utilr>   r   
eval_shaperV   randomPRNGKeysetr   rA   loggerwarning_missing_keysshapelen	__class____name__infor   )"r)   rW   r&   
model_argsr+   r*   rY   rZ   r[   r\   r]   r^   r_   r`   rj   unused_kwargsra   model_kwargspretrained_path_with_subfolder
model_fileerrrk   pytorch_model_filestatestate_fefparams_shape_treerequired_paramsshape_statemissing_keysunexpected_keysrG   unexpected_keys"                                     r,   from_pretrainedzFlaxModelMixin.from_pretrained   s   t Hd+JJ{D1	$4e<**Y.**Y-!::&8%@

7D)::j$/JJ{D1	 % 

 >$3COO-%#%)-!1!#% %!FM .coofnEX\n`mn|
   *;YG 	'
 77==78ww~~bggll3QS_&`a*.|n<PQoPppqr   WW\\*H,W
-KM^ _`WW\\*HJ[\
-K\ Z[&#n$=>\=] ^- - 
 '*+<*=T,Oc56a9 
1,16=.<'#1#%5)'%
d !#;&J  "1!< 77I5QEn*d+ <w&sGLLN;E<$ &&'gino U#NN5+=+=3::CUCUVWCXYl84E+FGLLNO"8,=#>?&UZZ\)::ejjl+o=NN!"?!@@[\h[i jZ Z !-C::< 	Ck!eCj&6&6+c:J:P:P&P ?uDbSz''((TU`adUeUkUkTllnp 	 . 	&Nn%	& !#NN:;X:Y Z!!&!9!9 :"_<M N!!&!9!9 : ;.. KKSTYTcTcTlTlSmmpqr|q NN"5??#;#;"< =122N|n ]nn KK%eoo&>&>%? @12 388=8P8P7Q R nU+++} + &45 6h h 
 ) &j !//L.MMgi 
 & &455[\m[nnop   &PQnPoore   &./N.O P>>[=\ ]::K9LDQ]P^ _]]  $ &01N0O P--J,K L//@.Al^STV 0< <#W%7%7%A%AB nnj) 4Q668..y9")!6#  #-!34 4 4 +J7 n*-?
|Kk+lmmn4nsn   !V Y* $Y?Y* AY0XAYY'"Y* *#\[2[[	[\ [77\  \save_directoryis_main_processpush_to_hubc                 0   t         j                  j                  |      rt        j	                  d| d       yt        j
                  |d       |r|j                  dd      }|j                  dd      }|j                  dd	      }|j                  d
d      }	|j                  d|j                  t         j                  j                        d         }
t        |
d||	      j                  }
| }|r|j                  |       t         j                  j                  |t              }t        |d      5 }t        |      }|j!                  |       ddd       t        j#                  d|        |r| j%                  |
	       yy# 1 sw Y   ;xY w)a  
        Save a model and its configuration file to a directory so that it can be reloaded using the
        [`~FlaxModelMixin.from_pretrained`] class method.

        Arguments:
            save_directory (`str` or `os.PathLike`):
                Directory to save a model and its configuration file to. Will be created if it doesn't exist.
            params (`Union[Dict, FrozenDict]`):
                A `PyTree` of model parameters.
            is_main_process (`bool`, *optional*, defaults to `True`):
                Whether the process calling this is the main process or not. Useful during distributed training and you
                need to call this function on all processes. In this case, set `is_main_process=True` only on the main
                process to avoid race conditions.
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
                repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
                namespace).
            kwargs (`Dict[str, Any]`, *optional*):
                Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
        zProvided path (z#) should be a directory, not a fileNT)exist_okcommit_messageprivate	create_prFr^   repo_id)r   r   r^   wbzModel weights saved in )r^   r   r   )rx   ry   r|   r   errormakedirsru   splitsepr   r   save_configrz   r   r   r
   writer   _upload_folder)rB   r   r/   r   r   r+   r   r   r   r^   r   model_to_saveoutput_model_filer   model_bytess                  r,   save_pretrainedzFlaxModelMixin.save_pretrained  si   8 77>>.)LL?>*::]^_
NT2#ZZ(8$?NjjD1G

;6IJJw-EjjN,@,@,Mb,QRG!'D'QVW__G %%n5 GGLL9JK#T* 	!a"6*KGGK 	! 	-.?-@AB-#    	! 	!s   5FFr4   )TF)r   
__module____qualname____doc__r   config_name_automatically_saved_args_flax_internal_argsclassmethodr-   r   r   r   r6   r&   r   rH   rL   rO   rR   r=   ArrayrV   r   rN   strrx   PathLiker   boolr   r(   r.   r,   r    r    2   sw    K V5% %+dJ.>(? +		 +Y\ +hk +0%BeD*$45 %BS %BNAeD*$45 AS A6%AeD*$45 %AS %AN[		 [d [  ;;a,',S"++-='>a, yya,  a,N	 !%!@c2;;./@ dJ&'@ 	@
 @r.   r    )0rx   pickler   typingr   r   r   r=   	jax.numpynumpyr6   msgpack.exceptionsr   flax.core.frozen_dictr   r   flax.serializationr	   r
   flax.traverse_utilr   r   huggingface_hubr   r   huggingface_hub.utilsr   r   r   r   requestsr    r   r   utilsr   r   r   r   r   r   modeling_flax_pytorch_utilsr   
get_loggerr   r   r    r(   r.   r,   <module>r      sl     
 " # # 
   6 3 ; 8   .  L 
		H	%~^ ~r.   