
    
3j%                     d   S SK r S SKJr  S SKJr  S SKrSSKJrJr  SSK	J
r
  SSKJr  SS	\R                  S
\R                  S-  S\R                  4S jjr  SS\R                  S\R                  S\S
\R                  S-  S\R                  4
S jjr\ " S S\
5      5       r " S S\\5      rg)    N)	dataclass)Literal   )ConfigMixinregister_to_config)
BaseOutput   )SchedulerMixint	generatorreturnc                 D   Ub  UR                   OU R                   n[        R                  " XS9R                  SSUS9R	                  U R                   5      n[        R
                  " [        R
                  " UR                  S5      5      * R                  S5      5      * $ )ar  
Generate Gumbel noise for sampling.

Args:
    t (`torch.Tensor`):
        Input tensor to match the shape and dtype of the output noise.
    generator (`torch.Generator`, *optional*):
        A random number generator for reproducible sampling.

Returns:
    `torch.Tensor`:
        Gumbel-distributed noise with the same shape, dtype, and device as the input tensor.
devicer   r	   r   #B;)r   torch
zeros_likeuniform_tologclamp)r   r   r   noises       `/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/schedulers/scheduling_amused.pygumbel_noiser      s     "+!6YAHHFQ.771	7RUUVWV^V^_EII		%++e"455<<UCDDD    mask_lenprobstemperaturec                     [         R                  " UR                  S5      5      U[        XS9-  -   n[         R                  " USS9R
                  n[         R                  " USU R                  5       5      nXF:  nU$ )aY  
Mask tokens by selecting the top-k lowest confidence scores with temperature-based randomness.

Args:
    mask_len (`torch.Tensor`):
        Number of tokens to mask per sample in the batch.
    probs (`torch.Tensor`):
        Probability scores for each token.
    temperature (`float`, *optional*, defaults to 1.0):
        Temperature parameter for controlling randomness in the masking process.
    generator (`torch.Generator`, *optional*):
        A random number generator for reproducible sampling.

Returns:
    `torch.Tensor`:
        Boolean mask indicating which tokens should be masked.
r   r   dimr	   )r   r   r   r   sortvaluesgatherlong)r   r   r   r   
confidencesorted_confidencecut_offmaskings           r   mask_by_random_topkr,      sg    . 5;;u-.|E?g1ggJ

:26==ll,aAG"GNr   c                   `    \ rS rSr% Sr\R                  \S'   Sr\R                  S-  \S'   Sr
g)AmusedSchedulerOutput=   a  
Output class for the scheduler's `step` function output.

Args:
    prev_sample (`torch.LongTensor` of shape `(batch_size, height, width)` or `(batch_size, sequence_length)`):
        Computed sample `(x_{t-1})` of previous timestep with token IDs. `prev_sample` should be used as next model
        input in the denoising loop.
    pred_original_sample (`torch.LongTensor` of shape `(batch_size, height, width)` or `(batch_size, sequence_length)`, *optional*):
        The predicted fully denoised sample `(x_{0})` with token IDs based on the model output from the current
        timestep. `pred_original_sample` can be used to preview progress or for guidance.
prev_sampleNpred_original_sample )__name__
__module____qualname____firstlineno____doc__r   Tensor__annotations__r1   	Generator__static_attributes__r2   r   r   r.   r.   =   s'    
 37%//D07r   r.   c                      \ rS rSr% SrSr\R                  S-  \S'   \R                  S-  \S'   \	 SS\
S\S	   4S
 jj5       r  SS\
S\
\\
\
4   -  \\
   -  S\\R                   -  4S jjr   SS\R$                  S\
S\R&                  S\S\R                  S-  S\S\\-  4S jjr SS\R&                  S\
S\R                  S-  S\R&                  4S jjrSrg)AmusedSchedulerO   a  
A scheduler for masked token generation as used in [`AmusedPipeline`].

This scheduler iteratively unmasks tokens based on their confidence scores, following either a cosine or linear
schedule. Unlike traditional diffusion schedulers that work with continuous pixel values, this scheduler operates
on discrete token IDs, making it suitable for autoregressive and non-autoregressive masked token generation models.

This scheduler inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the
generic methods the library implements for all schedulers such as loading and saving.

Args:
    mask_token_id (`int`):
        The token ID used to represent masked tokens in the sequence.
    masking_schedule (`Literal["cosine", "linear"]`, *optional*, defaults to `"cosine"`):
        The schedule type for determining the mask ratio at each timestep. Can be either `"cosine"` or `"linear"`.
r	   Ntemperatures	timestepsmask_token_idmasking_schedule)cosinelinearc                      S U l         S U l        g Nr?   r@   )selfrA   rB   s      r   __init__AmusedScheduler.__init__f   s     !r   num_inference_stepsr   r   c                    [         R                  " XS9R                  S5      U l        [	        U[
        [        45      (       a"  [         R                  " US   US   XS9U l        g [         R                  " USXS9U l        g )Nr   r   r	   g{Gz?)	r   arangeflipr@   
isinstancetuplelistlinspacer?   )rH   rK   r   r   s       r   set_timestepsAmusedScheduler.set_timestepso   se     &9INNqQkE4=11 %{1~{1~Ob rD %{DBU eDr   model_outputtimestepsamplestarting_mask_ratior   return_dictr   c                    UR                   S:H  =(       a    UR                   S:H  nU(       aH  UR                  u  ppUR                  XU-  5      nUR                  XX-  5      R                  SSS5      nX0R                  R
                  :H  nUR                  SS9nUR                  nUb  UR                  UR                  5      OUnUR                  R                  S:X  a.  UR                  [        R                  :w  a  UR                  5       nUR                  SUR                  S5      5      n[        R                  " USUS	9R                  US
9nUS S 2S4   R                   " UR                  S S 6 n[        R"                  " UUU5      nUS:X  a  UnGOUR                  S   nU R$                  U:H  R'                  5       nUS-   [)        U R$                  5      -  nU R                  R*                  S:X  a+  [        R,                  " U[.        R0                  -  S-  5      nOBU R                  R*                  S:X  a  SU-
  nO"[3        SU R                  R*                   35      eUU-  nUU-  R5                  5       n[        R6                  " UR9                  SSS9S-
  U5      n[        R:                  " [        R<                  " S/UR                  S
9U5      n[        R>                  " USUS S 2S S 2S 4   5      S S 2S S 2S4   n[        R"                  " UU[        R@                  " UR                  5      R:                  5      n[C        UUU RD                  U   U5      n[        R"                  " UU R                  R
                  U5      nU(       a%  UR                  WW
W5      nUR                  XU5      nU(       d  UU4$ [G        UU5      $ )N      r   r   r	   r!   r"   cpur   r   rC   rD   unknown masking schedule T)r#   keepdim)$ndimshapereshapepermuteconfigrA   softmaxr   r   typedtyper   float32floatsizemultinomialviewwherer@   nonzerolenrB   cosmathpi
ValueErrorfloorminsummaxtensorr&   finfor,   r?   r.   )rH   rU   rV   rW   rX   r   rY   two_dim_input
batch_sizecodebook_sizeheightwidthunknown_mapr   r   probs_r1   r0   seq_lenstep_idxratio
mask_ratior   selected_probsr+   s                            r   stepAmusedScheduler.step|   sA    q(C\->->!-C7C7I7I4Jv^^J?F'//
6>ZbbcdfgijkL 9 99$$$,/8/D)**+%==&6<<5==+H\\^FEJJrN3$00iPSS[aSb3AqD9>>CR@PQ${{;8LfUq=.Kll1oG(2;;=H\S%88E{{++x7"YYutww':;
--9Y
 #<T[[=Y=Y<Z![\\,z9J*,335HyyR!F!JHUHyyqc,:M:M!NPXYH"\\%5I!QPT*5UVWXZ[]^W^_N"[[nekkR`RfRfFgFkFklN)(NDDUDUV^D_ajkG  ++gt{{/H/HJ^_K%--j&%HK#7#?#?
TY#Z !566$[2FGGr   c                 ~   U R                   U:H  R                  5       nUS-   [        U R                   5      -  nU R                  R                  S:X  a+  [
        R                  " U[        R                  -  S-  5      nOBU R                  R                  S:X  a  SU-
  nO"[        SU R                  R                   35      e[
        R                  " UR                  Ub  UR                  OUR                  US9R                  UR                  5      U:  nUR                  5       nU R                  R                  X'   U$ )a3  
Add noise to a sample by randomly masking tokens according to the masking schedule.

Args:
    sample (`torch.LongTensor`):
        The input sample containing token IDs to be partially masked.
    timesteps (`int`):
        The timestep that determines how much masking to apply. Higher timesteps result in more masking.
    generator (`torch.Generator`, *optional*):
        A random number generator for reproducible masking.

Returns:
    `torch.LongTensor`:
        The sample with some tokens replaced by `mask_token_id` according to the masking schedule.
r	   rC   r   rD   r^   )r   r   )r@   rn   ro   rd   rB   r   rp   rq   rr   rs   randra   r   r   clonerA   )	rH   rW   r@   r   r   r   r   mask_indicesmasked_samples	            r   	add_noiseAmusedScheduler.add_noise   s   * NNi/88:AT^^!44;;''835477?Q#67J[[))X5UJ89U9U8VWXX JJ9NY%5%5TZTaTamvb 	 &*kk&?&?#r   rG   )rC   ))r   r   N)      ?NTrF   )r3   r4   r5   r6   r7   orderr   r:   r9   r   intr   rI   rP   rQ   strr   rS   r8   
LongTensorri   boolr.   r   r   r;   r2   r   r   r=   r=   O   sp   " E//D((%% 9A ""45  :@%)	f f 5c?*T#Y6f ell"	f$ &),0 CHllCH CH   	CH
 #CH ??T)CH CH 
	&CHR -1	*  * * ??T)	*
 
		* *r   r=   rF   )r   N)rq   dataclassesr   typingr   r   configuration_utilsr   r   utilsr   scheduling_utilsr
   r8   r:   r   ri   r,   r.   r=   r2   r   r   <module>r      s     !   A  ,EELL EU__t-C Eu|| E, (,	ll<<  %	
 \\< 8J 8 8"\nk \r   