
    kh)#                     l   d Z ddlZddlZddlmZmZmZmZ ddlZdddddd	eej                     d
e
de
de
dedeeej                     ee
   eeef   f   fdZdddej                  dede
dej                  fdZdddej                  deeef   dee
   dede
dej                  fdZ G d d      Zy)u  Variable‑size Mixup / CutMix utilities for NaFlex data loaders.

This module provides:

* `mix_batch_variable_size` – pixel‑level Mixup/CutMix that operates on a
  list of images whose spatial sizes differ, mixing only their central overlap
  so no resizing is required.
* `pairwise_mixup_target` – builds soft‑label targets that exactly match the
  per‑sample pixel provenance produced by the mixer.
* `NaFlexMixup` – a callable functor that wraps the two helpers and stores
  all augmentation hyper‑parameters in one place, making it easy to plug into
  different dataset wrappers.

Hacked together by / Copyright 2025, Ross Wightman, Hugging Face
    N)DictListTupleUnion皙?      ?      ?   mixup_alphacutmix_alphaswitch_problocal_shuffleimgsr   r   r   r   returnc                    t               dk  rt        d      |dkD  r2|dkD  r-t        j                  d      j	                         |k  }|r|n|}n|dkD  rd}|}n|dkD  rd}|}nt        d      t        j
                  j                  ||      j                         j	                         }t        dt        d|            }t        t        t                      fd	
      }|dkD  r6t        dt        |      |      D ]  }	t        j                  ||	|	|z            i }
t        |ddd   |ddd         D ]  \  }}||
|<   ||
|<    t               dz  r|d   nd}dgt               z  }dgt               z  }t        t                     D ]  }||k(  r	 |   ||<   |
|   } |    |   }}|j                  \  }}}|j                  \  }}}||z  }t        ||      t        ||      }}||z  }||z
  dz  ||z
  dz  }}||z
  dz  ||z
  dz  } }|j!                         }|rt#        j$                  d|z
        }!t'        ||!z        t'        ||!z        }#}"|"|#z  }$t        j(                  d||"z
        }%t        j(                  d||#z
        }&||%z   ||&z   }(}'||%z   | |&z   }*})|dd|)|)|"z   |*|*|#z   f   |dd|'|'|"z   |(|(|#z   f<   |||<   d|$t+        |      z  z
  }+|+||<   W|dd|||z   |||z   f   },|dd|||z   | | |z   f   }-|,j-                  |      j/                  |-d|z
        }.|.|dd|||z   |||z   f<   |||<   ||z
  |z  ||z  |z  z   }+|+||<    |||
fS )aO  Apply Mixup or CutMix on a batch of variable-sized images.

    Sorts images by aspect ratio and pairs neighboring samples. Only the mutual
    central overlap region of each pair is mixed.

    Args:
        imgs: List of transformed images shaped (C, H, W).
        mixup_alpha: Beta distribution alpha for Mixup. Set to 0 to disable.
        cutmix_alpha: Beta distribution alpha for CutMix. Set to 0 to disable.
        switch_prob: Probability of using CutMix when both modes are enabled.
        local_shuffle: Size of local windows for shuffling after aspect sorting.

    Returns:
        Tuple of (mixed_imgs, lam_list, pair_to) where:
            - mixed_imgs: List of mixed images
            - lam_list: Per-sample lambda values representing mixing degree
            - pair_to: Mapping i -> j of which sample was mixed with which
       z1Need at least two images to perform Mixup/CutMix.         FTu=   Both mixup_alpha and cutmix_alpha are zero – nothing to do.r   c                 N    |    j                   d   |    j                   d   z  S )Nr      )shape)ir   s    R/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/data/naflex_mixup.py<lambda>z)mix_batch_variable_size.<locals>.<lambda>E   s'    47==3Cd1gmmTUFV3V     )keyr   r   Nalpha)len
ValueErrortorchranditemdistributionsBetasamplemaxminsortedrangerandomshufflezipr   clonemathsqrtintrandintfloatmuladd_)/r   r   r   r   r   
use_cutmixr    lam_raworderstartpair_toabodd_one
mixed_imgslam_listr   jxixj_hiwihjwj	dest_areaohowoverlap_areatop_ileft_itop_jleft_j	cut_ratiochcwcut_areay_offx_offyl_ixl_iyl_jxl_jcorrected_lampatch_ipatch_jblendeds/   `                                              r   mix_batch_variable_sizer`      s   4 4y1}LMM S\C/ZZ^((*[8
 *	s	
		
XYY!!&&ue4;;=BBDG#s3()G 5T#)VWEq1c%j-8 	?ENN5u}'<=>	? !GE#A#Jadd, 1

 t9q=eBidG&*Vc$i%7J ECI-H3t9 +(< GJqMAJa$q'BHH	2rHH	2rG	 Rc"bkBBwbQbQvbQbQvXXZ		#-0Ii(#b9n*=BBwHNN1b2g.ENN1b2g.E$D$D68D$)OTSWZ\S\_9\6]Bq$r	/4?23JqM(U9-="==M'HQK E%"*,fVb[.@@AGE%"*,fVb[.@@AGkk'*//sW}/MG:ABq%
"F6B;$667JqM&5BW|E[^gEggM'HQKW+(Z x((r   r   )	smoothingtargetsnum_classesra   c                    ||z  }d|z
  |z   }t        j                  | j                  d      |f|t         j                  | j                        }|j                  d| j                  d      |       |S )Nr   r   )dtypedevicer   )r#   fullsizefloat32rf   scatter_	unsqueeze)rb   rc   ra   off_valon_valy_onehots         r   smoothed_sparse_targetro      so     +%G9_w&Fzz	a+&mm~~	H a**1-v6Or   r<   rA   c                    t        | ||      }|j                         } |j                         D ]5  \  }}||   }| |   j                  |      j	                  ||   d|z
         7 | S )u  Create soft targets that match the pixel‑level mixing performed.

    Args:
        targets: (B,) tensor of integer class indices.
        pair_to: Mapping of sample index to its mixed partner as returned by mix_batch_variable_size().
        lam_list: Per‑sample fractions of own pixels, also from the mixer.
        num_classes: Total number of classes in the dataset.
        smoothing: Label‑smoothing value in the range [0, 1).

    Returns:
        Tensor of shape (B, num_classes) whose rows sum to 1.
    rc   ra   r   r   )ro   r0   itemsmul_r7   )	rb   r<   rA   rc   ra   rn   r   rB   lams	            r   pairwise_mixup_targetru      sr    ( &g;R[\HnnG @1qk
!!(1+S3Y!?@ Nr   c                       e Zd ZdZdddddddded	ed
edededededdfdZdeej                     dej                  de
eej                     eej                     f   fdZy)NaFlexMixupz<Callable wrapper that combines mixing and target generation.r   r   r	   r
   r   )r   r   r   probr   label_smoothingrc   r   r   r   rx   r   ry   r   Nc                f    || _         || _        || _        || _        || _        || _        || _        y)u4  Configure the augmentation.

        Args:
            num_classes: Total number of classes.
            mixup_alpha: Beta α for Mixup. 0 disables Mixup.
            cutmix_alpha: Beta α for CutMix. 0 disables CutMix.
            switch_prob: Probability of selecting CutMix when both modes are enabled.
            prob: Probability of applying any mixing per batch.
            local_shuffle: Window size used to shuffle images after aspect sorting so pairings vary between epochs.
            smoothing: Label‑smoothing value. 0 disables smoothing.
        N)rc   r   r   r   rx   r   ra   )selfrc   r   r   r   rx   r   ry   s           r   __init__zNaFlexMixup.__init__   s:    , '&(&	*(r   r   rb   c                    t        |t        j                        st        j                  |      }t	        j                         | j
                  kD  r5t        || j                  | j                        }||j                  d      fS t        || j                  | j                  | j                  | j                        \  }}}t        |||| j                  | j                        }||j                  d      fS )u  Apply the augmentation and generate matching targets.

        Args:
            imgs: List of already transformed images shaped (C, H, W).
            targets: Hard labels with shape (B,).

        Returns:
            mixed_imgs: List of mixed images in the same order and shapes as the input.
            targets: Soft‑label tensor shaped (B, num_classes) suitable for cross‑entropy with soft targets.
        rq   r   r   )
isinstancer#   Tensortensorr-   rx   ro   rc   ra   unbindr`   r   r   r   r   ru   )r{   r   rb   r@   rA   r<   s         r   __call__zNaFlexMixup.__call__   s     '5<<0ll7+G==?TYY&,W$BRBR^b^l^lmG***(?((**((,,)
%
Hg (((nn
 7>>!,,,r   )__name__
__module____qualname____doc__r3   r5   r|   r   r#   r   r   r   r   r   r   rw   rw      s    F "%"%!$!"%() ) 	)
  ) ) ) ) #) 
)<%-u||$%- \\%- 
tELL!4#55	6	%-r   rw   )r   r1   r-   typingr   r   r   r   r#   r   r5   r3   r`   ro   ru   rw   r   r   r   <module>r      s4     + +  !! j)5<< j) j) 	j)
 j) j) 4tE{DcN:;j)b 	  	
 \\2 c3h u+
   \\:F- F-r   