
    +hq/                        S SK r S SKrS SKrS SKJr  S SKJr  S SKJrJ	r	J
r
  S SKrS SKJr  S SKJr  SSKJrJr  Sr " S	 S
\5      r\ " S S\5      5       r " S S\5      rS\R0                  S\	\   S\R0                  4S jrS\R6                  4S\S\R0                  4S jjr\R:                  R                   " S S5      5       rS\S\R0                  S\R0                  S\R0                  4S jrS\S\R0                  S\R0                  S\R0                  4S jr S\S\R0                  S\R0                  S\R0                  4S jr!g)     N)	dataclass)Enum)OptionalTupleUnion)validate_hf_hub_args   )
BaseOutputPushToHubMixinzscheduler_config.jsonc                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
FlaxKarrasDiffusionSchedulers#      r	                N)__name__
__module____qualname____firstlineno__FlaxDDIMSchedulerFlaxDDPMSchedulerFlaxPNDMSchedulerFlaxLMSDiscreteSchedulerFlaxDPMSolverMultistepSchedulerFlaxEulerDiscreteScheduler__static_attributes__r       d/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/schedulers/scheduling_utils_flax.pyr   r   #   s&     &'#!"r    r   c                   8    \ rS rSr% Sr\R                  \S'   Srg)FlaxSchedulerOutput,   a&  
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_sampler   N)	r   r   r   r   __doc__jnpndarray__annotations__r   r   r    r!   r#   r#   ,   s     r    r#   c                       \ rS rSrSr\rS/r/ rSr	\
\   SS\\\\R                   4      S\\   4S jj5       5       rSS	\\\R                   4   S
\4S jjr\S 5       r\
S 5       rSrg)FlaxSchedulerMixin:   aA  
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).
dtypeTNpretrained_model_name_or_path	subfolderc                     U R                   " SUUSS.UD6u  pTU R                  " U4SS0UD6u  pg[        US5      (       a"  [        USS5      (       a  UR	                  5       nU(       a  UWU4$ UW4$ )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.

    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 (`hf auth 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_kwargsr1   create_state	has_stateFr   )load_configfrom_confighasattrgetattrr2   )	clsr.   r/   r1   kwargsconfig	schedulerunused_kwargsstates	            r!   from_pretrained"FlaxSchedulerMixin.from_pretrainedI   s    ~  
*G!%
 	
 $'??6#_PT#_X^#_ 	9n--')[RW2X2X**,Ee]22%r    save_directorypush_to_hubc                 ,    U R                   " SXS.UD6  g)a  
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@   rA   Nr   )save_config)selfr@   rA   r9   s       r!   save_pretrained"FlaxSchedulerMixin.save_pretrained   s     	ZZSYZr    c                 "    U R                  5       $ )z
Returns all schedulers that are compatible with this scheduler

Returns:
    `List[SchedulerMixin]`: List of compatible schedulers
)_get_compatibles)rD   s    r!   compatiblesFlaxSchedulerMixin.compatibles   s     $$&&r    c                    [        [        U R                  /U R                  -   5      5      n[        R
                  " [        R                  S5      S   5      nU Vs/ s H   n[        X#5      (       d  M  [        X#5      PM"     nnU$ s  snf )N.r   )	listsetr   _compatibles	importlibimport_modulesplitr6   r7   )r8   compatible_classes_strdiffusers_libraryccompatible_classess        r!   rH   #FlaxSchedulerMixin._get_compatibles   s}    !%c3<<.3;K;K*K&L!M%33HNN34G4JK3I
3IaWUfMj)G%)3I 	 
 "!
s   B5Br   )NNF)F)r   r   r   r   r&   SCHEDULER_CONFIG_NAMEconfig_nameignore_for_configrO   has_compatiblesclassmethodr   r   r   strosPathLiker>   boolrE   propertyrI   rH   r   r   r    r!   r+   r+   :   s     (K 	LO LP#'"	K '/c2;;6F0G'HK  C=K   K Z[eC4D.E [TX [" ' ' " "r    r+   xshapereturnc           	          [        U5      U R                  :  d   e[        R                  " U R	                  U R
                  S[        U5      U R                  -
  -  -   5      U5      $ )N)r   )lenndimr'   broadcast_toreshaperc   )rb   rc   s     r!   broadcast_to_shape_from_leftrj      sO    u:AIIaggE
QVV8K0L&LMuUUr    g+?num_diffusion_timestepsc           
          S n/ n[        U 5       H<  nXP-  nUS-   U -  nUR                  [        SU" U5      U" U5      -  -
  U5      5        M>     [        R                  " XBS9$ )aN  
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                 h    [         R                  " U S-   S-  [         R                  -  S-  5      S-  $ )NgMb?gT㥛 ?r	   )mathcospi)	time_steps    r!   	alpha_bar&betas_for_alpha_bar.<locals>.alpha_bar   s.    xxU*e3dgg=ABaGGr    r   r-   )rangeappendminr'   array)rk   max_betar-   rr   betasit1t2s           r!   betas_for_alpha_barr~      sl    $H E*+(!e..SYr]Yr]::HEF , 99U((r    c                       \ rS rSr% \R
                  \S'   \R
                  \S'   \R
                  \S'   \S 5       rSr	g)CommonSchedulerState   alphasrz   alphas_cumprodc                    UR                   nUR                  b+  [        R                  " UR                  UR                  S9nGOUR
                  S:X  a@  [        R                  " UR                  UR                  UR                  UR                  S9nOUR
                  S:X  aI  [        R                  " UR                  S-  UR                  S-  UR                  UR                  S9S-  nO^UR
                  S:X  a  [        UR                  UR                  S9nO/[        SUR
                   SUR                  R                   35      eS	U-
  n[        R                  " US
S9nU " UUUS9$ )Nrt   linearscaled_linear      ?r	   squaredcos_cap_v2zbeta_schedule z" is not implemented for scheduler g      ?r   )axis)r   rz   r   )r:   trained_betasr'   asarrayr-   beta_schedulelinspace
beta_startbeta_endnum_train_timestepsr~   NotImplementedError	__class__r   cumprod)r8   r;   r:   rz   r   r   s         r!   createCommonSchedulerState.create   sL   !!+KK 4 4IOOLE!!X-LL!2!2FOOVE_E_gpgvgvwE!!_4 %%s*FOOS,@&B\B\dmdsds   !!%88'(B(B)//ZE% !5!5 66XYbYlYlYuYuXvw  uV!4)
 	
r    r   N)
r   r   r   r   r'   r(   r)   r\   r   r   r   r    r!   r   r      s2    KK;;KK
 
r    r   r=   original_samplesnoise	timestepsc                     U R                   nXC   S-  nUR                  5       n[        XQR                  5      nSXC   -
  S-  nUR                  5       n[        XaR                  5      nXV4$ )Nr   r   )r   flattenrj   rc   )r=   r   r   r   r   sqrt_alpha_prodsqrt_one_minus_alpha_prods          r!   get_sqrt_alpha_prodr     sw     ))N$/36O%--/O2?DZDZ[O!"^%>!>3 F 9 A A C <=VXnXn o55r    c                 4    [        XX#5      u  pEXA-  XR-  -   nU$ Nr   )r=   r   r   r   r   r   noisy_sampless          r!   add_noise_commonr     s,     2EU^c1o.O#69R9ZZMr    samplec                 4    [        XX#5      u  pEXB-  XQ-  -
  nU$ r   r   )r=   r   r   r   r   r   velocitys          r!   get_velocity_commonr      s(    1DUTY1e.O&)B)KKHOr    )"rP   rn   r^   dataclassesr   enumr   typingr   r   r   flax	jax.numpynumpyr'   huggingface_hub.utilsr   utilsr
   r   rX   r   r#   r+   r(   intrj   float32r~   structr   r   r   r   r   r    r!   <module>r      su     	 !  ) )   6 . 0 #D # 
* 
 
@" @"FVCKK Vc
 Vs{{ V
 @ECKK ) )\_\g\g ): %
 %
 %
P6636;;6GJ{{6_b_j_j6 36;;GJ{{_b_j_j3 S[[ QTQ\Q\ ilitit r    