
    wi)0                         d dl Z d dlZd dlZd dlm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mZ ddlmZmZ dZ G d	 d
e          Ze G d de                      Z G d de          Zdej        de	e         dej        fdZdej        fdedej        fdZej        j         G d d                      Zdedej        dej        dej        fdZdedej        dej        dej        fdZ dedej        dej        dej        fdZ!dS )     N)	dataclass)Enum)OptionalTupleUnion)validate_hf_hub_args   )
BaseOutputPushToHubMixinzscheduler_config.jsonc                   &    e Zd ZdZdZdZdZdZdZdS )FlaxKarrasDiffusionSchedulers   r	               N)	__name__
__module____qualname__FlaxDDIMSchedulerFlaxDDPMSchedulerFlaxPNDMSchedulerFlaxLMSDiscreteSchedulerFlaxDPMSolverMultistepSchedulerFlaxEulerDiscreteScheduler     z/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/schedulers/scheduling_utils_flax.pyr   r   #   s4         &'#!"r   r   c                   (    e Zd ZU dZej        ed<   dS )FlaxSchedulerOutputa>  
    Base class for the scheduler's step function output.

    Args:
        prev_sample (`jnp.ndarray` of shape `(batch_size, num_channels, height, width)` for images):
            Computed sample (x_{t-1}) of previous timestep. `prev_sample` should be used as next model input in the
            denoising loop.
    prev_sampleN)r   r   r   __doc__jnpndarray__annotations__r   r   r   r    r    ,   s,           r   r    c                       e Zd ZdZeZdgZg ZdZe	e
	 	 	 ddeeeej        f                  dee         fd                        Zdd	eeej        f         d
efdZed             Ze	d             ZdS )FlaxSchedulerMixinaY  
    Mixin containing common functions for the schedulers.

    Class attributes:
        - **_compatibles** (`List[str]`) -- A list of classes that are compatible with the parent class, so that
          `from_config` can be used from a class different than the one used to save the config (should be overridden
          by parent class).
    dtypeTNFpretrained_model_name_or_path	subfolderc                      | j         d||dd|\  }} | j        |fddi|\  }}t          |d          r%t          |dd          r|                                }|r|||fS ||fS )a  
        Instantiate a Scheduler class from a pre-defined JSON-file.

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

                    - A string, the *model id* of a model repo on huggingface.co. Valid model ids should have an
                      organization name, like `google/ddpm-celebahq-256`.
                    - A path to a *directory* containing model weights saved using [`~SchedulerMixin.save_pretrained`],
                      e.g., `./my_model_directory/`.
            subfolder (`str`, *optional*):
                In case the relevant files are located inside a subfolder of the model repo (either remote in
                huggingface.co or downloaded locally), you can specify the folder name here.
            return_unused_kwargs (`bool`, *optional*, defaults to `False`):
                Whether kwargs that are not consumed by the Python class should be returned or not.

            cache_dir (`Union[str, os.PathLike]`, *optional*):
                Path to a directory in which a downloaded pretrained model configuration should be cached if the
                standard cache should not be 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, e.g., `{'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 or not to only look at local files (i.e., do not try to download the model).
            token (`str` or *bool*, *optional*):
                The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
                when running `transformers-cli login` (stored in `~/.huggingface`).
            revision (`str`, *optional*, defaults to `"main"`):
                The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
                git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
                identifier allowed by git.

        <Tip>

         It is required to be logged in (`huggingface-cli login`) when you want to use private or [gated
         models](https://huggingface.co/docs/hub/models-gated#gated-models).

        </Tip>

        <Tip>

        Activate the special ["offline-mode"](https://huggingface.co/transformers/installation.html#offline-mode) to
        use this method in a firewalled environment.

        </Tip>

        T)r)   r*   return_unused_kwargsr,   create_state	has_stateFr   )load_configfrom_confighasattrgetattrr-   )	clsr)   r*   r,   kwargsconfig	schedulerunused_kwargsstates	            r   from_pretrainedz"FlaxSchedulerMixin.from_pretrainedI   s    B ) 
*G!%
 
 	
 
 $33?6#_#_PT#_X^#_#_ 	=9n-- 	-')[RW2X2X 	-**,,E 	3e]22%r   save_directorypush_to_hubc                 &     | j         d||d| dS )aX  
        Save a scheduler configuration object to the directory `save_directory`, so that it can be re-loaded using the
        [`~FlaxSchedulerMixin.from_pretrained`] class method.

        Args:
            save_directory (`str` or `os.PathLike`):
                Directory where the configuration JSON file will be saved (will be created if it does not exist).
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face 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 keyword arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
        )r:   r;   Nr   )save_config)selfr:   r;   r4   s       r   save_pretrainedz"FlaxSchedulerMixin.save_pretrained   s+     	ZKZZSYZZZZZr   c                 *    |                                  S )z
        Returns all schedulers that are compatible with this scheduler

        Returns:
            `List[SchedulerMixin]`: List of compatible schedulers
        )_get_compatibles)r>   s    r   compatibleszFlaxSchedulerMixin.compatibles   s     $$&&&r   c                     t          t          | j        g| j        z                       }t	          j        t                              d          d                   fd|D             }|S )N.r   c                 P    g | ]"}t          |          t          |          #S r   )r1   r2   ).0cdiffusers_librarys     r   
<listcomp>z7FlaxSchedulerMixin._get_compatibles.<locals>.<listcomp>   sE     
 
 
./WUfhiMjMj
%q))
 
 
r   )listsetr   _compatibles	importlibimport_modulesplit)r3   compatible_classes_strcompatible_classesrH   s      @r   rA   z#FlaxSchedulerMixin._get_compatibles   sx    !%c3<.3;K*K&L&L!M!M%3HNN34G4G4JKK
 
 
 
3I
 
 
 "!r   )NNF)F)r   r   r   r"   SCHEDULER_CONFIG_NAMEconfig_nameignore_for_configrL   has_compatiblesclassmethodr   r   r   strosPathLiker9   boolr?   propertyrB   rA   r   r   r   r'   r'   :   s         (K 	LO LP#'"	M  M '/c2;6F0G'HM  C=M  M  M   [M ^[ [eC4D.E [TX [ [ [ [" ' ' X' " " [" " "r   r'   xshapereturnc           	          t          |          | j        k    sJ t          j        |                     | j        dt          |          | j        z
  z  z             |          S )N)r   )lenndimr#   broadcast_toreshaper]   )r\   r]   s     r   broadcast_to_shape_from_leftrd      sS    u::AIIagE

QV8K0L&LMMuUUUr   g+?num_diffusion_timestepsc           
          d }g }t          |           D ]J}|| z  }|dz   | z  }|                    t          d ||           ||          z  z
  |                     Kt          j        ||          S )az  
    Create a beta schedule that discretizes the given alpha_t_bar function, which defines the cumulative product of
    (1-beta) over time from t = [0,1].

    Contains a function alpha_bar that takes an argument t and transforms it to the cumulative product of (1-beta) up
    to that part of the diffusion process.


    Args:
        num_diffusion_timesteps (`int`): the number of betas to produce.
        max_beta (`float`): the maximum beta to use; use values lower than 1 to
                     prevent singularities.

    Returns:
        betas (`jnp.ndarray`): the betas used by the scheduler to step the model outputs
    c                 \    t          j        | dz   dz  t           j        z  dz            dz  S )NgMb?gT㥛 ?r	   )mathcospi)	time_steps    r   	alpha_barz&betas_for_alpha_bar.<locals>.alpha_bar   s-    xU*e3dg=ABBaGGr   r   r(   )rangeappendminr#   array)re   max_betar(   rl   betasit1t2s           r   betas_for_alpha_barrw      s    $H H H E*++ G G((!e..SYYr]]YYr]]::HEEFFFF9U%((((r   c                   b    e Zd ZU ej        ed<   ej        ed<   ej        ed<   ed             ZdS )CommonSchedulerStatealphasrs   alphas_cumprodc                 :   |j         }|j        !t          j        |j        |j                  }n|j        dk    r-t          j        |j        |j        |j	        |j                  }n|j        dk    r6t          j        |j        dz  |j        dz  |j	        |j                  dz  }nK|j        dk    rt          |j	        |j                  }n$t          d|j         d|j        j                   d	|z
  }t          j        |d
          } | |||          S )Nrm   linearscaled_linear      ?r	   squaredcos_cap_v2zbeta_schedule z" is not implemented for scheduler g      ?r   )axis)rz   rs   r{   )r5   trained_betasr#   asarrayr(   beta_schedulelinspace
beta_startbeta_endnum_train_timestepsrw   NotImplementedError	__class__r   cumprod)r3   r6   r5   rs   rz   r{   s         r   createzCommonSchedulerState.create   sP   !+K 4IOLLLEE!X--L!2FOVE_gpgvwwwEE!_44 %s*FOS,@&B\dmds    E !%888'(B)/ZZZEE%w!5wwYbYlYuww   uV!444s)
 
 
 	
r   N)r   r   r   r#   r$   r%   rV   r   r   r   r   ry   ry      sY         K;K
 
 [
 
 
r   ry   r8   original_samplesnoise	timestepsc                     | j         }||         dz  }|                                }t          ||j                  }d||         z
  dz  }|                                }t          ||j                  }||fS )Nr   r   )r{   flattenrd   r]   )r8   r   r   r   r{   sqrt_alpha_prodsqrt_one_minus_alpha_prods          r   get_sqrt_alpha_prodr   
  s     )N$Y/36O%--//O2?DTDZ[[O!"^I%>!>3 F 9 A A C C <=VXhXn o o555r   c                 F    t          | |||          \  }}||z  ||z  z   }|S Nr   )r8   r   r   r   r   r   noisy_sampless          r   add_noise_commonr     s>     2EUL\^cen1o1o.O.#&669RUZ9ZZMr   samplec                 F    t          | |||          \  }}||z  ||z  z
  }|S r   r   )r8   r   r   r   r   r   velocitys          r   get_velocity_commonr   "  s7    1DUFTY[d1e1e.O.&)BV)KKHOr   )"rM   rh   rX   dataclassesr   enumr   typingr   r   r   flax	jax.numpynumpyr#   huggingface_hub.utilsr   utilsr
   r   rR   r   r    r'   r$   intrd   float32rw   structry   r   r   r   r   r   r   <module>r      s        				 ! ! ! ! ! !       ) ) ) ) ) ) ) ) ) )        6 6 6 6 6 6 . . . . . . . . 0 # # # # #D # # # 
 
 
 
 
* 
 
 
B" B" B" B" B" B" B" B"JVCK Vc
 Vs{ V V V V
 @ECK ) ) )\_\g ) ) ) ): %
 %
 %
 %
 %
 %
 %
 %
P6636;6GJ{6_b_j6 6 6 6 36;GJ{_b_j   3 S[ QTQ\ ilit      r   