
    biq/                        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j0                  de	e   dej0                  fdZdej6                  fdedej0                  fdZej:                  j                   G d d             Zdedej0                  dej0                  dej0                  fdZdedej0                  dej0                  dej0                  fdZ dedej0                  dej0                  dej0                  fdZ!y)     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y)FlaxKarrasDiffusionSchedulers   r	               N)	__name__
__module____qualname__FlaxDDIMSchedulerFlaxDDPMSchedulerFlaxPNDMSchedulerFlaxLMSDiscreteSchedulerFlaxDPMSolverMultistepSchedulerFlaxEulerDiscreteScheduler     e/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/schedulers/scheduling_utils_flax.pyr   r   #   s&     &'#!"r   r   c                   0    e Zd ZU dZej
                  ed<   y)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y)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).
    dtypeTNpretrained_model_name_or_path	subfolderc                      | j                   d||dd|\  }} | j                  |fddi|\  }}t        |d      rt        |dd      r|j	                         }|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.

            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_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    ~ ) 
*G!%
 	
 $33??6#_PT#_X^#_ 	=9n-')[RW2X**,Ee]22%r   save_directorypush_to_hubc                 .     | j                   d||d| y)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ZSYZr   c                 "    | j                         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        j                  d      d         }|D cg c]  }t        ||      st        ||       }}|S c c}w )N.r   )	listsetr   _compatibles	importlibimport_modulesplitr1   r2   )r3   compatible_classes_strdiffusers_libraryccompatible_classess        r   rA   z#FlaxSchedulerMixin._get_compatibles   s}    !%c3<<.3;K;K*K&L!M%33HNN34G4JK3I
./WUfhiMjG%q)
 
 "!
s   B .B )NNF)F)r   r   r   r"   SCHEDULER_CONFIG_NAMEconfig_nameignore_for_configrG   has_compatiblesclassmethodr   r   r   strosPathLiker9   boolr?   propertyrB   rA   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           	          t        |      | j                  k\  sJ t        j                  | j	                  | j
                  dt        |      | j                  z
  z  z         |      S )N)r   )lenndimr#   broadcast_toreshaperZ   )rY   rZ   s     r   broadcast_to_shape_from_leftra      sO    u:AIIaggE
QVV8K0L&LMuUUr   g+?num_diffusion_timestepsc           
          d }g }t        |       D ]<  }|| z  }|dz   | z  }|j                  t        d ||       ||      z  z
  |             > t        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                 f    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.    xxU*e3dgg=ABaGGr   r   r(   )rangeappendminr#   array)rb   max_betar(   ri   betasit1t2s           r   betas_for_alpha_barrt      sx    $H E*+ G((!e..SYr]Yr]::HEFG 99U%((r   c                   x    e Zd ZU ej                  ed<   ej                  ed<   ej                  ed<   ed        Zy)CommonSchedulerStatealphasrp   alphas_cumprodc                    |j                   }|j                  -t        j                  |j                  |j                        }n|j
                  dk(  rBt        j                  |j                  |j                  |j                  |j                        }n|j
                  dk(  rKt        j                  |j                  dz  |j                  dz  |j                  |j                        dz  }n`|j
                  dk(  r"t        |j                  |j                        }n/t        d|j
                   d|j                  j                         d	|z
  }t        j                  |d
      } | |||      S )Nrj   linearscaled_linear      ?r	   squaredcos_cap_v2zbeta_schedule z" is not implemented for scheduler g      ?r   )axis)rw   rp   rx   )r5   trained_betasr#   asarrayr(   beta_schedulelinspace
beta_startbeta_endnum_train_timestepsrt   NotImplementedError	__class__r   cumprod)r3   r6   r5   rp   rw   rx   s         r   createz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   N)r   r   r   r#   r$   r%   rS   r   r   r   r   rv   rv      s2    KK;;KK
 
r   rv   r8   original_samplesnoise	timestepsc                     | j                   }||   dz  }|j                         }t        ||j                        }d||   z
  dz  }|j                         }t        ||j                        }||fS )Nr|   r   )rx   flattenra   rZ   )r8   r   r   r   rx   sqrt_alpha_prodsqrt_one_minus_alpha_prods          r   get_sqrt_alpha_prodr     s     ))N$Y/36O%--/O2?DTDZDZ[O!"^I%>!>3 F 9 A A C <=VXhXnXn o555r   c                 >    t        | |||      \  }}||z  ||z  z   }|S Nr   )r8   r   r   r   r   r   noisy_sampless          r   add_noise_commonr     s;     2EUL\^cen1o.O.#&669RUZ9ZZMr   samplec                 >    t        | |||      \  }}||z  ||z  z
  }|S r   r   )r8   r   r   r   r   r   velocitys          r   get_velocity_commonr      s4    1DUFTY[d1e.O.&)BV)KKHOr   )"rH   re   rU   dataclassesr   enumr   typingr   r   r   flax	jax.numpynumpyr#   huggingface_hub.utilsr   utilsr
   r   rO   r   r    r'   r$   intra   float32rt   structrv   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   