
    
3j
M                        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
\	5      5       r " S S\\5      rSS
/rg)    )annotations)	dataclassN   )ConfigMixinregister_to_config)
BaseOutput   )SchedulerMixinc                  L    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'   Srg)BlockRefinementSchedulerOutput   a  
Output class for block refinement scheduling.

Args:
    prev_sample (`torch.LongTensor` of shape `(batch_size, block_length)`):
        Updated block tokens after the current refinement step.
    transfer_index (`torch.BoolTensor` of shape `(batch_size, block_length)`):
        Boolean mask indicating which tokens were committed (mask-filling).
    editing_transfer_index (`torch.BoolTensor` of shape `(batch_size, block_length)`):
        Boolean mask indicating which tokens were edited (non-mask replacement).
    sampled_tokens (`torch.LongTensor` of shape `(batch_size, block_length)`):
        Sampled token IDs from the model logits.
    sampled_probs (`torch.Tensor` of shape `(batch_size, block_length)`):
        Probabilities of the sampled tokens.
torch.LongTensorprev_sampletorch.BoolTensortransfer_indexediting_transfer_indexsampled_tokenstorch.Tensorsampled_probs N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r       j/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/schedulers/scheduling_block_refinement.pyr   r      s'      "!$$,,$$r   r   c                     \ rS rSrSrSr\     S         SS jj5       rSSS jjrSS jr	\
SS j5       r\
SS	 j5       r\
              SS
 j5       rSSSSSSSSSSS.
                             SS jjr\
                SS j5       r                SS jrSS.             S S jjrSrg)!BlockRefinementScheduler3   a(  
Scheduler for block-wise iterative refinement (commit-by-confidence).

At each step, the scheduler samples candidate tokens from model logits and commits those with the highest
confidence. The number of tokens to commit per step is determined by evenly distributing the block length across
the number of refinement steps.

Optionally supports editing: after all mask tokens are resolved, tokens can be replaced if the model predicts a
different token with confidence above a positive `editing_threshold` (`None`, `0.0`, or negative disables editing).
r	   Nc                    X l         [        R                  " U R                   S-
  SS[        R                  S9U l        S U l        g )Nr	   dtype)num_inference_stepstorcharangelong	timesteps_transfer_schedule)selfblock_lengthr'   	thresholdediting_thresholdminimal_topks         r   __init__!BlockRefinementScheduler.__init__A   s:     $7 d&>&>&BBRWR\R\];?r   c                8   US::  a  [        SU S35      eXl        [        R                  " U R                  S-
  SSU[        R                  S9U l        U R                  U R                  R                  U R                  5      R                  Ub  UOSS9U l
        g )	Nr   z'`num_inference_steps` must be > 0, got .r	   r$   devicer&   cpu)r7   )
ValueErrorr'   r(   r)   r*   r+   get_num_transfer_tokensconfigr.   tor,   )r-   r'   r7   s      r   set_timesteps&BlockRefinementScheduler.set_timestepsN   s    !#FGZF[[\]^^#6 d&>&>&BBSYafakakl"&">">t{{?W?WY]YqYq"r"u"u#/6U #v #
r   c                    US::  a#  [         R                  " S[         R                  S9$ X-  nX-  n[         R                  " U4U[         R                  S9nUSU=== S-  sss& U$ )zREvenly distribute `block_length` token commits across `num_inference_steps` steps.r   )r   r%   Nr	   )r(   zerosr*   full)r-   r.   r'   base	remainderouts         r   r:   0BlockRefinementScheduler.get_num_transfer_tokensW   s^    !#;;t5::662 6	jj-/UZZHJY1
r   c                   Ub  US:  a  U $ SUs=:  a  S::  d  O  [        SU S35      e[        R                  " U SSS9u  p#[        R                  " USS	9nUR	                  SS	9nU[        U5      :  nUS
SS24   R                  5       US
SS24'   SUS'   UR                  U[        R                  " UR                  5      R                  5      nU R                  SX25      nU$ )z Nucleus (top-p) logit filtering.N      ?        z`top_p` must be in (0, 1], got r5   Tr$   )
descendingdimrJ   .r	   r   ).r   )r9   r(   sortsoftmaxcumsumfloatclonemasked_fillfinfor&   minscatter)logitstop_psorted_logitssorted_indicessorted_probscumulative_probssorted_indices_to_removefiltereds           r   _top_p_filtering)BlockRefinementScheduler._top_p_filteringc   s     =ESLMe"s">ugQGHH(-

6dPR(S%}}];'..2.6#3eEl#B ,DS#2#X,N,T,T,V ab)+, (%112JEKKXeXkXkLlLpLpq>>"nDr   c                    Ub  US::  a  U $ XR                   S   :  a  U $ [        R                  " XSS9u  p#US   nU R                  X:  [        R                  " U R
                  5      R                  5      $ )zTop-k logit filtering.r   r$   )krJ   ).r$   N)shaper(   topkrQ   rR   r&   rS   )rU   top_kvalues_min_keeps        r   _top_k_filtering)BlockRefinementScheduler._top_k_filteringw   sm     =EQJMLL$$MJJvB7	-(!!&"3U[[5N5R5RSSr   c                  US:  a  [        SU S35      eU R                  S   nU R                  SU5      nUS:X  d  U(       d  [        R                  " UR                  5       SS9nUR                  SSS9n	[        R                  " USU	5      n
U	R                  " U R                  S	S 6 U
R                  " U R                  S	S 6 4$ UnUS
:w  a  Xq-  n[        R                  XS9n[        R                  XS9n[        R                  " UR                  5       SS9n[        R                  " USUS9n	[        R                  " USU	5      n
U	R                  " U R                  S	S 6 U
R                  " U R                  S	S 6 4$ )zESample tokens from logits with temperature scaling, top-k, and top-p.r   z `temperature` must be >= 0, got r5   r$   rH   rK   T)rJ   keepdimNrG   )rc   )rV   r	   )num_samples	generator)r9   ra   reshaper(   rM   rO   argmaxgatherviewr!   rg   r]   multinomial)rU   temperaturerc   rV   rl   use_multinomial
vocab_sizeflat_logitsprobstoken
token_probscaledr\   s                r   _sample_from_logits,BlockRefinementScheduler._sample_from_logits   sh    ??}ANOO\\"%
nnR4#_MM+"3"3"52>E&&2t&<EeR7J::v||CR01:??FLLQTRTDU3VVV# .F+<<V<Q+<<X<Shnn.B7!!%Q)L\\%U3
zz6<<,-zSb@Q/RRRr   rH   autoT)
rr   rV   rc   sampling_methodr/   r0   r1   prompt_maskrl   return_dictc          
        U	c  [        U R                  R                  5      n	U
c  U R                  R                  n
Uc  U R                  R                  nUS:H  =(       d    US:H  =(       a    US:g  nU R                  UUUUUUS9u  nnUR                  u  nnX4:H  nUR                  5       n[        U[        R                  5      (       a  [        UR                  5       5      nO[        U5      n[        R                  " U[        R                  S9nU(       Ga[  U R                  GbM  [!        U[#        U R                  5      S-
  5      n[        U R                  U   R                  5       5      n[        R$                  " UUR'                  [        R(                  S9[        R*                  " U[        R,                  * [        R(                  S95      n[/        U5       H  nUU   U	:  nUR1                  5       R                  5       U:  a  UUU'   M4  [!        U[        UU   R1                  5       R                  5       5      5      nUS:  d  Mp  [        R2                  " UU   US	9u  nnS
UUU4'   M     U
SL=(       a    U
S:  n [        R                  " U[        R                  S9n!U (       a  Uc.  [        R4                  " UUR6                  [        R                  S9nU) UR9                  S5      ) -  n"[        R$                  " U"UR'                  [        R(                  S9[        R*                  " U[        R,                  * [        R(                  S95      n#U#[        U
5      :  n$UU:g  n%U$U%-  U"-  n!UU!-  n&UR;                  5       n'U&R                  5       (       a  UU&   U'U&'   U(       d  U'UU!UU4$ [=        U'UU!UUS9$ )a1  
Perform a single refinement step: sample from logits, commit confident tokens, and optionally edit existing
ones.

Args:
    model_output (`torch.Tensor` of shape `(batch_size, block_length, vocab_size)`):
        Raw logits from the model for the current block.
    timestep (`int` or `torch.Tensor`):
        Current step index within the block's refinement schedule.
    sample (`torch.LongTensor` of shape `(batch_size, block_length)`):
        Current block token IDs (contains mask tokens for uncommitted positions).
    mask_token_id (`int`):
        Token ID used for masked positions.
    temperature (`float`):
        Sampling temperature.
    top_p (`float`, *optional*):
        Nucleus sampling cutoff.
    top_k (`int`, *optional*):
        Top-k sampling cutoff.
    sampling_method (`str`):
        Sampling method (`auto`, `greedy`, `multinomial`).
    threshold (`float`, *optional*):
        Confidence threshold for committing tokens. Defaults to config value.
    editing_threshold (`float`, *optional*):
        Confidence threshold for editing non-mask tokens; must be positive to enable editing. Defaults to
        config value.
    minimal_topk (`int`, *optional*):
        Minimum tokens to commit per step. Defaults to config value.
    prompt_mask (`torch.BoolTensor`, *optional*):
        Boolean mask of shape `(block_length,)` where `True` marks prompt (non-editable) positions.
    generator (`torch.Generator`, *optional*):
        RNG for sampling.
    return_dict (`bool`):
        Whether to return a `BlockRefinementSchedulerOutput` or a tuple.
Nrq   r|   rH   )rr   rc   rV   rl   rs   r%   r	   r   )r`   Tr6   )r   r   r   r   r   )rO   r;   r/   r0   r1   rz   ra   any
isinstancer(   Tensorintitem
zeros_likeboolr,   rS   lenwherer<   float32	full_likeinfrangesumrb   r@   r7   	unsqueezerP   r   )(r-   model_outputtimestepsamplemask_token_idrr   rV   rc   r}   r/   r0   r1   r~   rl   r   rs   r   r   
batch_sizer.   active_blockmasks_remaining
step_indexr   clamped_stepnum_to_transfer
confidenceb	high_confr`   re   idxediting_enabledr   editableediting_confhigh_conf_edittoken_changedfinal_transferr   s(                                           r   stepBlockRefinementScheduler.step   sa   p dkk334I$ $ = =;;33L *]:pRX?X?o]hlo]o(,(@(@#+ )A )
% $*<< 
L.&**,h--X]]_-JXJ )).

Kt66Bz3t/F/F+G!+KLL!$"9"9,"G"L"L"NOO  u}} 5		zOJ :&&qMI5	==?'')_<(1N1%OSa1D1D1F1K1K1M-NOA1u!&JqMQ!?315q#v. ' ,47S<MPS<S!&!1!1.

!S"#kk,v}}TYT^T^_%;+@+@+C*CDH ;;  u}} 5		zOL
 *E2C,DDN*f4M%3m%Ch%N" (*@@lln*8*HK'0FXeee-#)#9)'
 	
r   c                   U R                   S   n[        U5       H  nX8   (       a  M  X   X(      U:H  R                  5       R                  5       n	U	(       d  M?  X   U:H  R	                  SS9n
[        U
S   5      S:X  a  Mg  [        U
S   S   R                  5       5      n
Xj:  a  M  XXj24   U:g  R                  5       R                  5       (       d  M  SX8'   M     U$ )a=  
Update per-batch finished flags when EOS tokens are committed.

Args:
    cur_x (`torch.LongTensor` of shape `(batch_size, seq_len)`):
        Current full sequence including all blocks up to the current window.
    sampled_tokens (`torch.LongTensor` of shape `(batch_size, block_length)`):
        Tokens sampled by the scheduler in this step.
    final_transfer (`torch.BoolTensor` of shape `(batch_size, block_length)`):
        Combined mask of committed and edited positions.
    finished (`torch.BoolTensor` of shape `(batch_size,)`):
        Current per-batch finished flags.
    eos_token_id (`int`):
        EOS token ID.
    mask_token_id (`int`):
        Mask token ID.
    prompt_length (`int`):
        Number of prompt tokens at the start of the sequence.

Returns:
    `torch.BoolTensor`: Updated finished flags.
r   T)as_tuple)ra   r   r   r   nonzeror   r   all)cur_xr   r   finishedeos_token_idr   prompt_lengthr   r   eos_in_commitseos_poss              r   check_eos_finished+BlockRefinementScheduler.check_eos_finished/  s    @ [[^
z"A{,/0ABlRWWY^^`N!x</88$8GG71:!#'!*Q-,,./G'../=@EEGLLNN" # r   c                    UR                  5       (       a  gU(       d  U(       d  gU(       d  UR                  5       (       d  gU(       a  XR                  :  a  gU(       d  XV:  a  gg)a  
Determine whether the inner refinement loop should continue for the current block.

Args:
    step_idx (`int`):
        Current refinement step index within this block.
    masks_remaining (`bool`):
        Whether any mask tokens remain in the block.
    editing_enabled (`bool`):
        Whether editing mode is active.
    editing_transfer_index (`torch.BoolTensor`):
        Which tokens were edited in this step.
    post_steps (`int`):
        Number of post-mask editing steps taken so far.
    max_post_steps (`int`):
        Maximum allowed post-mask editing steps.
    finished (`torch.BoolTensor`):
        Per-batch finished flags (from EOS detection).

Returns:
    `bool`: `True` if refinement should continue, `False` to break.
FT)r   r   r'   )r-   step_idxr   r   r   
post_stepsmax_post_stepsr   s           r   check_block_should_continue4BlockRefinementScheduler.check_block_should_continue`  sO    @ <<>>'='A'A'C'Cx+C+CC:#>r   )rl   c                  UR                   u  pxUR                  n	UR                  5       n
UR                  5       n[        R                  " U[        R
                  S9n[        R                  " U[        R
                  S9nUR                  [        R
                  S9n[        X8U5       H  n[        XU-   5      nUU-
  nUS::  a  M  [        R                  " US4XS9n[        R                  " UU4XS9U:  nUUSS2UU24   -  nU) USS2UU24   -  nUUSS2UU24'   UUSS2UU24'   M     [        R                  " U[        R                  " X5      U
5      n
[        R                  " U[        R                  " X5      U5      nXX4$ )uE  
Apply the forward (noising) process for semi-autoregressive block masking.

For each block after the prompt, a random fraction of valid (non-padding) tokens are replaced with
`mask_token_id`. Two complementary views are returned: `noisy` and `noisy_rev`, where the masked positions in
one are the unmasked positions in the other.

Args:
    original_samples (`torch.LongTensor` of shape `(batch_size, seq_len)`):
        Clean token IDs.
    attention_mask (`torch.LongTensor` of shape `(batch_size, seq_len)`):
        Padding mask (1 for valid, 0 for padding).
    prompt_length (`int`):
        Number of leading prompt tokens to keep unmasked.
    block_length (`int`):
        Block size for masking.
    mask_token_id (`int`):
        Token ID to use for masked positions.
    generator (`torch.Generator`, *optional*):
        RNG for reproducibility.

Returns:
    `tuple[torch.LongTensor, torch.LongTensor, torch.BoolTensor, torch.BoolTensor]`:
        `(noisy, noisy_rev, masked, masked_rev)` — the two complementary noisy sequences and their
        corresponding boolean masks.
r%   r   r	   )r7   rl   N)ra   r7   rP   r(   r   r   r<   r   rS   randr   r   )r-   original_samplesattention_maskr   r.   r   rl   r   seq_lenr7   noisy	noisy_revmasked
masked_revvalidblock_start	block_endseg_lenp_masksegseg_revs                        r   	add_noise"BlockRefinementScheduler.add_noise  s   H /44
!(( &&($**,	!!"2%**E%%&6ejjI
!!

!3 FKG<%?@I+-G!|ZZQTF**j'26WZ``CaY!6677CtuQI(=%=>>G/2F1k)++,3:Jq+i//0 G FEOOE$I5QKK
EOOI,UW`a	33r   )r,   r'   r+   )    r   gffffff?Nr	   )
r.   r   r'   r   r/   rO   r0   float | Noner1   r   )N)r'   r   r7   zstr | torch.device | NonereturnNone)r.   r   r'   r   r   r   )rU   r   rV   r   r   r   )rU   r   rc   
int | Noner   r   )rU   r   rr   rO   rc   r   rV   r   rl   torch.Generator | Noners   r   r   z%tuple[torch.LongTensor, torch.Tensor])r   r   r   zint | torch.Tensorr   r   r   r   rr   rO   rV   r   rc   r   r}   strr/   r   r0   r   r1   r   r~   ztorch.BoolTensor | Nonerl   r   r   r   r   z|BlockRefinementSchedulerOutput | tuple[torch.LongTensor, torch.BoolTensor, torch.BoolTensor, torch.LongTensor, torch.Tensor])r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r.   r   r   r   rl   r   r   zMtuple[torch.LongTensor, torch.LongTensor, torch.BoolTensor, torch.BoolTensor])r   r   r   r   r   orderr   r2   r=   r:   staticmethodr]   rg   rz   r   r   r   r   r   r   r   r   r!   r!   3   s   	 E #%*.
@
@ !
@ 	
@
 (
@ 
@ 
@
  & T T !S!S !S 	!S
 !S *!S !S 
/!S !ST !" %"&*.#'/3,0 !G
"G
 %G
 !	G
 G
 G
 G
 G
 G
  G
 (G
 !G
 -G
 *G
  !G
$	f%G
R ..(. ). #	.
 . . . 
. .`** * 	*
 !1* * * #* 
*h -1=4*=4 )=4
 =4 =4 =4 *=4 
W=4 =4r   r!   )
__future__r   dataclassesr   r(   configuration_utilsr   r   utilsr   scheduling_utilsr
   r   r!   __all__r   r   r   <module>r      sR    # !  A  ,  Z    0V4~{ V4r &'G
Hr   