
    wij                     &   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)j,        e-          Z. G d de(          Z/dS )    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j        fde
eej        f         dej        fd                        Z	 	 dde
eej        f         de
eef         dedefdZdS )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      t/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/models/modeling_flax_utils.py_from_configzFlaxModelMixin._from_config@   s    
 s6$$V$$$    Nparamsr&   maskreturnc                 &   fd}|t          j        ||          S t          |          }t          j        |          \  }}t	          ||                                          D ]\  }}	|r||	         }
 ||
          ||	<   t          |          S )zk
        Helper method to cast floating-point values of given parameter `PyTree` to given `dtype`.
        c                     t          | t          j                  r9t          j        | j        t          j                  r|                               } | S N)
isinstancejnpndarray
issubdtyper&   floatingastype)paramr&   s    r,   conditional_castz:FlaxModelMixin._cast_floating_to.<locals>.conditional_castM   sD    %-- ,#.cl2[2[ ,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9"6**'--	1y+*:*:*<*<== 	; 	;KFC ;#C(#3#3E#:#:C k***r.   c                 D    |                      |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 %%fclDAAAr.   c                 D    |                      |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 %%fck4@@@r.   c                 D    |                      |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 %%fck4@@@r.   rngc                 &    t          d|            )Nz.init_weights method has to be implemented for )NotImplementedError)rB   rS   s     r,   init_weightszFlaxModelMixin.init_weights   s    !"YSW"Y"YZZZr.   pretrained_model_name_or_pathc                 j   |                     dd          }|                     dd          }|                     dd          }|                     dd          }|                     dd          }	|                     dd          }
|                     d	d          }|                     d
d          }|                     dd          }|                     dd          }t          ddd}| | j        |f|d||	|
||||d	|\  }} | j        |f|dd|\  }}||nt          j                            ||          }t          j                            |          rn|rt          j                            t          j                            |t                              st          dt           d| d          t          j                            |t                    }nt          j                            t          j                            |t                              r't          j                            |t                    }nt          j                            t          j                            |t                              rt          t           d| d          t          dt           dt           d| d          	 t          ||st          nt          |||
|	|||||          }n# 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|r@t'                      rd(d)lm} nt          d*           ||          }t-          ||          }n	 t/          |d+          5 }t1          | |                                          }ddd           n# 1 swxY w Y   n# t4          t6          j        j        f$ r}	 t/          |          5 }|                                                    d,          rt?          d-          t"          |# 1 swxY w Y   n(# t@          t"          f$ r t          d.| d/          w xY wY d}~nd}~ww xY wtB          j"        #                    d0 |          }tI          |          }tC          j%        |j&        tB          j'        (                    d1          2          }tS          tI          tU          |                    +                                          }tI          tU          |                    }|tS          |+                                          z
  }tS          |+                                          |z
  } |r(tX          -                    d3| d4| d5           || _.        |+                                D ]Q}!|!|v rK||!         j/        ||!         j/        k    r/t#          d6|! d7||!         j/         d8||!         j/         d9          R| D ]}"||"= ta          |           d1k    r<tX          -                    d:| d;|j1        j2         d<|  d=|j1        j2         d>	           n(tX          3                    d?|j1        j2         d@           ta          |          d1k    r/tX          -                    dA|j1        j2         dB| dC| dD           n8tX          3                    dE|j1        j2         dF| dG|j1        j2         dH           |ti          |          fS )IaY  
        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.
            resume_download:
                Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v1
                of Diffusers.
            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resume_downloadproxieslocal_files_onlytokenrevision	subfoldermodelflax)	diffusers	file_type	frameworkT)	rY   return_unused_kwargsrZ   r\   r]   r^   r_   r`   ra   )r&   rg   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^   r_   
user_agentra   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 `huggingface-cli 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'    3CT]bCcCcCcdeCf1g1g 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_utilsrl   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`   ra   rk   unused_kwargsrb   model_kwargspretrained_path_with_subfolder
model_fileerrrl   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[
   x Hd++JJ{D11	$4e<<**Y.. **%6==**Y--!::&8%@@

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

 >$3CO-%#%)- /!1!#% % % %!FM .cofnEX\nn`mnn|
   *);YGG 	'
 7==788 I	 w~~bgll3QS_&`&`aa *r|rrQorrr    W\\*H,WW

-KM^ _ _`` W\\*HJ[\\

-K\ Z Z[[ 	&# - ->\ - - -  
 '9+< 9 9, 9 959 9 9  
3,16=O..<'#1#$3%5)'%  

 +   &4      )   & i i/Li i i  
 &   &4pp\mppp      &Qn         &]/N ] ]>[] ]:K] ]Q]] ] ]   $   &V1N V V-JV V 0AV V GSV V V    	n!## ;;;;;;;&J   "1!<!< 77I5QQEEn*d++ <w&sGLLNN;;E< < < < < < < < < < < < < < <#W%7%AB n n nnj)) 4Q6688..y99 4")!6# #  #-!34 4 4 4 4 4 4 4 4 +J7 n n n*+l
+l+l+lmmmn4 4 4 4 4n  &&'g'ginoo U##N5+=3:CUCUVWCXCXYYYl84E+F+FGGLLNNOO"8,=#>#>??&UZZ\\):)::ejjll++o= 	-NNZ"? Z Z\h Z Z Z   !-C::<< 	 	Ck!!eCj&6+c:J:P&P&P p p pSz'p pU`adUeUkp p p   . 	& 	&Nn%%!##NN.;X . .!&!9. .=L. .!&!9. . .    KKqTYTcTlqqqrrr|q  NNn5?#; n n1n nO[n n n    KKeo&>  1 8=8P     nU++++s   (K5 5A&OM00A(OQ) .#QQ) Q!!Q) $Q!%Q) )TS?SS	SS	ST%TTTTFsave_directoryis_main_processpush_to_hubc                    t           j                            |          r t                              d| d           dS t          j        |d           |r|                    dd          }|                    dd          }|                    d	d          }|                    d
d          }	|                    d|                    t           j        j                  d                   }
t          |
d||	          j
        }
| }|r|                    |           t           j                            |t                    }t          |d          5 }t          |          }|                    |           ddd           n# 1 swxY w Y   t                              d|            |r|                     ||
|	||           dS dS )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privateF	create_prr_   repo_id)r   r   r_   wbzModel weights saved in )r_   r   r   )ry   rz   r}   r   errormakedirsrv   splitsepr   r   save_configr{   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  s   8 7>>.)) 	LL^>^^^___F
NT2222 	`#ZZ(8$??NjjE22G

;66IJJw--EjjN,@,@,M,Mb,QRRG!'D'QVWWW_G  	6%%n555 GLL9JKK#T** 	!a"6**KGGK   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	A.?AABBB 	-#       	 	s   %F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   strry   PathLiker   boolr   r(   r.   r,   r    r    2   s4         K V V V555% % [%+ +dJ.>(? +	 +Y\ +hk + + + +0%B %BeD*$45 %BS %B %B %B %BNA AeD*$45 AS A A A A6%A %AeD*$45 %AS %A %A %A %AN[	 [d [ [ [ [  ;g, g,',S"+-='>g, yg, g, g,  [g,Z	 !%!@ @c2;./@ dJ&'@ 	@
 @ @ @ @ @ @r.   r    )0ry   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      s    
			 " " " " " " # # # # # # # # # # 



           6 6 6 6 6 6 6 6 3 3 3 3 3 3 3 3 ; ; ; ; ; ; ; ; 8 8 8 8 8 8 8 8                  . . . . . . . .                L K K K K K 
	H	%	%D D D D D^ D D D D Dr.   