+
    ýäûjR{  ã                  ó(  € ^ RI Ht ^ RIHtHtHtHtHtHtH	t	H
t
 ^ RIt^ RIHu Ht ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIH t  ^ RI!H"t" ^RI#H$t$ ^RI%H&t& ^RI'H(t( ^RI)H*t*H+t+ R.t,R R lt- ! R R]&4      t.R# )é    )Úannotations)ÚAnyÚDictÚIteratorÚListÚOptionalÚSequenceÚTupleÚUnionN)Únn)Ú_AugmentationBase)Ú_raise_input_rank_error)Úextract_tensor_patches)ÚKORNIA_CHECK_SHAPE)Ú_device_literal)Ú_to_device_nonblocking)ÚBoxes)Ú	Keypoints)ÚSequentialBase)ÚImageSequential)ÚInputSequentialOps)Ú	ParamItemÚPatchParamItemÚPatchSequentialc               ó$   € V ^8„  d   QhRRRRRR/# )é   ÚindiceszUnion[List[int], torch.Tensor]Údeviceztorch.deviceÚreturnútorch.Tensor© )Úformats   "Ú_C:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\augmentation\container\patch.pyÚ__annotate__r$   ,   s(   € ÷ Dñ DÐ:ð DÀLð DÐUañ Dó    c                ó6  aa€ \        V \        P                  4      '       d   \        W4      # V  Uu. uF  p\	        V4      NK  	  upo\        S4      ^8”  d¢   S^,          S^ ,          ,
          oS^ 8”  d„   \        ;QJ d)    VV3R l\        S4       4       F  '       d   K   RM	  RM! VV3R l\        S4       4       4      '       d0   \        P                  ! S^ ,          SR,          ^,           SVR7      # \        SV\        P                  R7      # u upi )aˆ  Return the patch ``indices`` as a long tensor on ``device``, without a host-to-device copy that waits.

Indexing a CUDA tensor with a Python list copies the list to the device and waits for every queued kernel.
``forward_parameters`` stores evenly spaced indices, which ``torch.arange`` builds on the device; any other list is
a device literal. Both index exactly the patches the list names.
c              3  ó\   <"  € T F!  w  rVS^ ,          VS,          ,           8H  x € K#  	  R# 5i©r   Nr!   )Ú.0ÚkÚvÚstepÚvaluess   &  €€r#   Ú	<genexpr>Ú!_patch_indices.<locals>.<genexpr>8   s'   øé € ÐTÑBS¹$¸!˜A ¨¥¨Q°­XÕ!5Ö5ÓBSùs   ƒ),FT)r   )r   Údtypeéÿÿÿÿ)Ú
isinstanceÚtorchÚTensorr   ÚintÚlenÚallÚ	enumerateÚaranger   Úlong)r   r   Úir,   r-   s   && @@r#   Ú_patch_indicesr<   ,   sÀ   ù€ ô �'œ5Ÿ<™<×(Ò(Ü% gÓ6Ð6Ù%Ó&™g˜Œc�!Žf™gÑ&€FÜ
ˆ6ƒ{�Q„Ø�a�y˜6 !�9Õ$ˆØ�!Œ8Ÿ›ÕTÄ)ÈFÔBSÓTŸŸšÕTÄ)ÈFÔBSÓT×TÒTÜ—<’<  q¥	¨6°"­:¸­>¸4ÈÔOÐOÜ˜6¨&¼¿
¹
ÔCÐCùò 's   ²Dc                  ól  a € ] tR t^=tRtRR4RRRRRRRR	R
RRR/R V 3R lllt]R R l4       tR5R R lltR5R R llt	R6R R llt
R6R R lltR R ltR R ltR R ltR5R R  lltR5R! R" lltR5R# R$ lltR5R% R& lltR5R' R( lltR5R) R* lltR5R+ R, lltR5R- R. lltR6R/ R0 lltR5R1 R2 lltR3tV ;t# )7r   a—  Apply image transforms independently to a regular grid of image patches.

``output = PatchSequential(*modules, grid_size=...)(input)`` splits each image into a grid, runs the configured
children on the patch batch, then joins the patches.

See the Convention block on :class:`~kornia.augmentation.AugmentationBase2D`.

.. image:: _static/img/PatchSequential.png

Processing runs on each patch region as in :cite:`lin2021patch`.

Args:
    *args: a list of processing modules.
    grid_size: positive ``(rows, columns)`` of patches in each image.
    padding: ``"same"`` temporarily zero-pads to a multiple of ``grid_size`` and removes the added
        border after augmentation, preserving the input spatial size. ``"valid"`` crops the remainder
        before augmentation and keeps the cropped spatial size. Padding and cropping are centered;
        an odd extra pixel is placed on the right or bottom. Both modes preserve the batch size.
        Geometric transforms can move the temporary zero border into the retained image; removing
        the added border does not undo that movement.
    same_on_batch: apply the same transformation across the batch.
        If None, it will not overwrite the function-wise settings.
    keepdim: whether to keep the output shape the same as input (True) or broadcast it
        to the batch form (False). If None, it will not overwrite the function-wise settings.
    patchwise_apply: apply image processing args will be applied patch-wisely.
        if ``True`` and ``random_apply=False``, the number of args must equal the number of patches.
        if ``False``, the image processing args will be applied as a sequence to all patches.
    random_apply: randomly select children to apply in random order. Selections repeat when the requested count is
        larger than the number of children that can be drawn: the children with a nonzero selection weight, mix
        augmentations not counted, plus one place when the count and the mix augmentations together reach the number
        of children, since a mix augmentation is then certain to take a place. With only mix children one of them is
        drawn, and with all weights 0 and no mix child nothing is.
        An int or a range that is not fixed to the number of children also repeats when the drawn count is larger
        than the sum of the weights. That rule is deprecated since kornia 0.9.1, with a ``FutureWarning`` on the
        calls whose draw it changes, as in :class:`~kornia.augmentation.container.ImageSequential`.
        If ``int`` (batchwise mode only), a fixed number of transformations will be selected.
        If ``(a,)`` (batchwise mode only), x number of transformations (a <= x <= len(args)) will be selected.
        If ``(a, b)`` (batchwise mode only), x number of transformations (a <= x <= b) will be selected.
        If ``True`` and not ``patchwise_apply``, the whole list of args will be processed in a random order,
        whatever the scale of the weights, and so will a count fixed to the number of children. With a non-mix child
        of weight 0 or more than one mix augmentation, that count cannot be filled: children repeat and some may be
        left out.
        If ``True`` and ``patchwise_apply``, 1 to 4 transformations will be selected per patch, a range under the
        deprecated rule above. Once a patch has drawn a mix augmentation, later patches count no place for one.
        If ``False`` and not ``patchwise_apply``, the whole list of args will be processed in original order.
        If ``False`` and ``patchwise_apply``, the whole list of args will be processed in original order
        location-wisely.
    random_apply_weights: selection weights for each child, as in
        :class:`~kornia.augmentation.container.ImageSequential`.

Convention:
    - like :class:`~kornia.augmentation.container.ImageSequential`, this container takes image tensors only: it has
      no ``data_keys`` and no ``.transform_matrix`` attribute.
    - input images have shape ``(B, C, H, W)``. Each image is split into ``N = rows * columns`` patches,
      ordered by row then column. Parameters index the flattened ``B * N`` patch batch.
    - with ``patchwise_apply=True`` and ``random_apply=False``, each module processes its grid location
      across all images. ``same_on_batch`` controls parameter sharing, not which images are processed.
    - with ``patchwise_apply=False``, the selected sequence is applied to every patch.
    - an empty image batch retains the grid's patch count and spatial sizes. Children must themselves
      support empty inputs; geometric patch inverses and mask transforms remain unsupported.
    - ``padding="valid"`` requires at least one pixel per patch in each spatial dimension. For example,
      an ``8 x 8`` image on a ``(3, 3)`` grid remains ``8 x 8`` with ``"same"`` and becomes ``6 x 6``
      with ``"valid"``. The batch size, channel count, device and dtype are preserved.

.. note::
    Transformation matrix returned only considers the transformation applied in ``kornia.augmentation`` module.
    Those transformations in ``kornia.geometry`` will not be taken into account.

.. note::
    See a working example `here <https://www.kornia.org/tutorials/nbs/data_patch_sequential.html>`__.

.. warning::
    Under ``torch.compile`` with the default inductor backend, torch earlier than 2.15 returns wrong values when a
    child reads the patches at other positions, a flip for example, after an earlier child has written them
    (`pytorch/pytorch#198270 <https://github.com/pytorch/pytorch/issues/198270>`__). Eager execution,
    the ``aot_eager`` backend and torch 2.15 or later are not affected.

See Also:
    - :class:`~kornia.augmentation.container.ImageSequential`: applies its child sequence to whole images.
    - :class:`~kornia.augmentation.container.AugmentationSequential`: transforms images with aligned annotations.

Examples:
    >>> import kornia.augmentation as K
    >>> input = torch.randn(2, 3, 224, 224)
    >>> seq = PatchSequential(
    ...     ImageSequential(
    ...         K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
    ...         K.RandomPerspective(0.2, p=0.5),
    ...         K.RandomSolarize(0.1, 0.1, p=0.5),
    ...     ),
    ...     K.RandomAffine(360, p=1.0),
    ...     ImageSequential(
    ...         K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
    ...         K.RandomPerspective(0.2, p=0.5),
    ...         K.RandomSolarize(0.1, 0.1, p=0.5),
    ...     ),
    ...     K.RandomSolarize(0.1, 0.1, p=0.1),
    ...     grid_size=(2,2),
    ...     patchwise_apply=True,
    ...     same_on_batch=True,
    ...     random_apply=False,
    ... )
    >>> out = seq(input)
    >>> out.shape
    torch.Size([2, 3, 224, 224])
    >>> out1 = seq(input, params=seq._params)
    >>> torch.equal(out, out1)
    True

Perform ``OneOf`` transformation with ``random_apply=1`` and ``random_apply_weights`` in ``PatchSequential``.

    >>> import kornia
    >>> input = torch.randn(2, 3, 224, 224)
    >>> seq = PatchSequential(
    ...     ImageSequential(
    ...         K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
    ...         K.RandomPerspective(0.2, p=0.5),
    ...         K.RandomSolarize(0.1, 0.1, p=0.5),
    ...     ),
    ...     K.RandomAffine(360, p=1.0),
    ...     K.RandomSolarize(0.1, 0.1, p=0.1),
    ...     grid_size=(2,2),
    ...     patchwise_apply=False,
    ...     random_apply=1,
    ...     random_apply_weights=[0.5, 0.3, 0.8]
    ... )
    >>> out = seq(input)
    >>> out.shape
    torch.Size([2, 3, 224, 224])

Ú	grid_sizeÚpaddingÚsameÚsame_on_batchNÚkeepdimÚpatchwise_applyTÚrandom_applyFÚrandom_apply_weightsc               ó<   € V ^8„  d   QhRRRRRRRRR	RR
RRRRRRR/	# )r   Úargsz	nn.Moduler>   úTuple[int, int]r?   ÚstrrA   zOptional[bool]rB   rC   ÚboolrD   z!Union[int, bool, Tuple[int, int]]rE   z.Optional[Union[Sequence[float], torch.Tensor]]r   ÚNoner!   )r"   s   "r#   r$   ÚPatchSequential.__annotate__Â   se   € ÷ *5ñ *5àð*5ð #ð*5ð ð	*5ð
 &ð*5ð  ð*5ð ð*5ð 8ð*5ð Mð*5ð 
ñ*5r%   c               	ó  <€ V P                  V4       V'       d
   VR J d   Rp	M›V'       d_   VRJ dY   \        V4      V^ ,          V^,          ,          8w  d1   \        R\        V4       RV^ ,          V^,          ,           R24      hTp	M5V'       d,   \        V\        \
        34      '       d   \        RV R24      hTp	\        S
V `  ! VRVRVR	V	R
V/  VR9  d   \        RV R24      hWn        W n	        WPn
        V  R# )TFzBThe number of processing modules must be equal with grid size.Got z and z<. Please set random_apply = True or patchwise_apply = False.zFOnly boolean value allowed when `patchwise_apply` is set to True. Got Ú.rA   rB   rD   rE   z0`padding` must be either `same` or `valid`. Got N)é   é   )r@   Úvalid)Ú_validate_grid_sizer6   Ú
ValueErrorr2   r5   ÚtupleÚsuperÚ__init__r>   r?   rC   )Úselfr>   r?   rA   rB   rC   rD   rE   rG   Ú_random_applyÚ	__class__s   &$$$$$$$* €r#   rV   ÚPatchSequential.__init__Â   s#  ø€ ð 	× Ñ  Ô+ç˜|¨tÓ3à"‰Mß °Ó!6Ü�4‹y˜I a�L¨9°Q­<Õ7Ô7Ü ðÜ˜t›9˜+ U¨9°Q­<¸)ÀA½,Õ+FÐ*Gð HQðQóð ð
 )‰Mß¤¨L¼3Ä¸,×!GÒ!GÜÐeÐfrÐesÐstÐuÓvÐvà(ˆMÜ‰ÒØð	
à'ð	
ð ð	
ð 'ð		
ð
 "6ò	
ð Ð+Ô+ÜÐOÐPWÈyÐXYÐZÓ[Ð[Ø"ŒØŒØ.ÔÛr%   c               ó    € V ^8„  d   QhRRRR/# )r   r>   rH   r   rK   r!   )r"   s   "r#   r$   rL   ï   s   € ÷ `ñ ` ð `¸4ñ `r%   c                	óð   € \        V \        \        34      '       dK   \        V 4      ^8w  g;   \        ;QJ d    R V  4       F  '       g   K   RM	  RM! R V  4       4      '       d   \        RV  R24      hR# )r   c              3  ó–   "  € T F?  p\        V\        4      '       * ;'       g    \        V\        4      ;'       g    V^ 8*  x € KA  	  R# 5ir(   )r2   r5   rJ   )r)   Úsizes   & r#   r.   Ú6PatchSequential._validate_grid_size.<locals>.<genexpr>ó   s<   é € ÐjÑ`iÐX\”z $¬Ó,Ô,×SÐS´
¸4ÄÓ0F×SÐSÈ$ÐRSÉ)ÔSÓ`iùs   ‚ A	£A	»A	TFz2grid_size must contain two positive integers. Got rN   N)r2   rT   Úlistr6   ÚanyrS   )r>   s   &r#   rR   Ú#PatchSequential._validate_grid_sizeî   sb   € ô ˜9¤u¬d m×4Ò4Ü�9‹~ Ô"ß‹sÑjÑ`iÓj�s�sŠsÑjÑ`iÓj×jÒjäÐQÐR[ÐQ\Ð\]Ð^Ó_Ð_ñ kr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   Úinputr    r?   rI   r>   úOptional[Tuple[int, int]]r   úTuple[int, int, int, int]r!   )r"   s   "r#   r$   rL   ÷   s2   € ÷ Fñ FØ!ðFØ,/ðFØ<UðFà	"ñFr%   c                ó:   € V P                  VP                  W#4      # )a7  Compute spatial padding needed before patch extraction.

Args:
    input: Input image tensor.
    padding: Padding mode, either ``"same"`` or ``"valid"``.
    grid_size: Optional grid override. Uses ``self.grid_size`` when omitted.

Returns:
    Padding tuple ``(left, right, top, bottom)``. Negative values crop the border.

Raises:
    ValueError: The input is not four-dimensional, has an empty spatial dimension, or the
        grid cannot produce non-empty patches in ``"valid"`` mode.
    NotImplementedError: ``padding`` is neither ``"same"`` nor ``"valid"``.
)Ú_compute_paddingÚshape)rW   rd   r?   r>   s   &&&&r#   Úcompute_paddingÚPatchSequential.compute_padding÷   s   € ð$ ×$Ñ$ U§[¡[°'ÓEÐEr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   Úbatch_shapeú
torch.Sizer?   rI   r>   re   r   rf   r!   )r"   s   "r#   r$   rL     s2   € ÷ cñ cØ%ðcØ03ðcØ@Yðcà	"ñcr%   c                ó|  € Vf   V P                   pV P                  V4       \        V4      ^8w  d   \        VR4       VR	R w  rEV^ 8:  g   V^ 8:  d   \	        R4      hVR8X  du   WC^ ,          8  g   WS^,          8  d   \	        R4      hWC^ ,          ,          WS^,          ,          rvV^,          ) Ww^,          ,
          ) V^,          ) Wf^,          ,
          ) 3# VR8X  dP   V) V^ ,          ,          V) V^,          ,          rvV^,          Ww^,          ,
          V^,          Wf^,          ,
          3# \        RV R24      h)
z@Compute padding from an image shape without allocating a tensor.Nú(B, C, H, W)z6PatchSequential requires non-empty spatial dimensions.rQ   z>grid_size must produce non-empty patches with padding='valid'.r@   z2Expect `padding` as either 'valid' or 'same'. Got rN   éþÿÿÿ)r>   rR   r6   r   rS   ÚNotImplementedError)rW   rm   r?   r>   ÚheightÚwidthÚphÚpws   &&&&    r#   rh   Ú PatchSequential._compute_padding  s&  € ð ÒØŸ™ˆIØ× Ñ  Ô+Üˆ{Ó˜qÔ Ü# K°Ô@Ø# B CÐ(‰ˆØ�QŒ;˜% 1œ*ÜÐUÓVÐVØ�gÔØ !�Ô$¨¸!µÔ(<Ü Ð!aÓbÐbØ¨�lÕ*¨E¸aµLÕ,@�Ø˜A•g�J "¨Q¥w¥, °2¸µ7°¸rÈ!ÅG½|¸_ÐMÐMØ�fÔØ�W˜y¨�|Õ+¨e¨V°iÀµlÕ-B�Ø˜!•G˜R¨¥'�\¨2°­7°B¸q½µLÐAÐAÜ!Ð$VÐW^ÐV_Ð_`Ð"aÓbÐbr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rd   r    r>   re   Úpadú#Optional[Tuple[int, int, int, int]]r   r!   )r"   s   "r#   r$   rL   !  s8   € ÷ :Bñ :Bàð:Bð -ð:Bð 1ð	:Bð
 
ñ:Br%   c                ó¤  € Vf   V P                   pV P                  V4       VP                  ^8w  d   \        VP                  R4       VP                  RR w  rEVe:   WC^,          ,           V^,          ,           WS^ ,          ,           V^,          ,           rTV^ 8:  g2   V^ 8:  g+   WB^ ,          ,          '       g   WR^,          ,          '       d   \        RWE3 RV R24      hVe!   \        P                  ! V\        V4      4      pVP                  R4      VR,          ,          VP                  R4      VR,          ,          3pTp\        WV4      # )aM  Extract ``(B, rows * columns, C, h, w)`` patches from an image batch.

The spatial size after applying ``pad`` must be positive and divisible by ``grid_size``.
Use :meth:`compute_padding` to determine the padding or cropping for a given mode.

Example:
    >>> import kornia.augmentation as K
    >>> pas = PatchSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), patchwise_apply=False)
    >>> pas.extract_patches(torch.arange(16).view(1, 1, 4, 4), grid_size=(2, 2))
    tensor([[[[[ 0,  1],
               [ 4,  5]]],
    <BLANKLINE>
    <BLANKLINE>
             [[[ 2,  3],
               [ 6,  7]]],
    <BLANKLINE>
    <BLANKLINE>
             [[[ 8,  9],
               [12, 13]]],
    <BLANKLINE>
    <BLANKLINE>
             [[[10, 11],
               [14, 15]]]]])
    >>> pas.extract_patches(torch.arange(54).view(1, 1, 6, 9), grid_size=(2, 2), pad=(-1, -2, -2, -2))
    tensor([[[[[19, 20, 21]]],
    <BLANKLINE>
    <BLANKLINE>
             [[[22, 23, 24]]],
    <BLANKLINE>
    <BLANKLINE>
             [[[28, 29, 30]]],
    <BLANKLINE>
    <BLANKLINE>
             [[[31, 32, 33]]]]])

Nrp   zPadded image size z- must be positive and divisible by grid_size rN   rq   r1   )r>   rR   Úndimr   ri   rS   ÚFry   r`   r^   r   )rW   rd   r>   ry   rs   rt   Úwindow_sizeÚstrides   &&&&    r#   Úextract_patchesÚPatchSequential.extract_patches!  s  € ðT ÒØŸ™ˆIØ× Ñ  Ô+Ø�:‰:˜Œ?Ü# E§K¡K°Ô@ØŸ™ B CÐ(‰ˆØŠ?Ø"¨¥V�O¨c°!­fÕ4°eÀ!½fµnÀsÈ1ÅvÕ6M�EØ�QŒ;˜% 1œ*¨¸1µ×(=Ô(=ÀÐSTÍ×AUÔAUÜØ$ f _Ð$5Ð5bÐclÐbmÐmnÐoóð ð Š?Ü—E’E˜%¤ c£Ó+ˆEØ—z‘z "“~¨°2­Õ6¸¿
¹
À2»È)ÐTVÍ-Õ8WÐXˆØˆÜ% e¸&ÓAÐAr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   Úpatchesr    r>   re   ry   rz   r   r!   )r"   s   "r#   r$   rL   ]  s2   € ÷ $ñ $àð$ð -ð$ð 1ð	$ð
 
ñ$r%   c           
     ó  € Vf   V P                   pV P                  V4       \        V. RO4       VP                  w  rErgpWR^ ,          V^,          ,          8w  d   \	        RV RV R24      hVP                  WB^ ,          V^,          WgV4      p	V	P                  ^ ^^^^^4      P                  WFV^ ,          V,          V^,          V,          4      p
Ve2   \        P                  ! Y£ Uu. uF  p\        V^ 4      ) NK  	  up4      p
V
# u upi )a“  Restore images from ``(B, rows * columns, C, h, w)`` patches.

Uses ``self.grid_size`` unless overridden. Only positive entries in ``pad`` are removed:
pixels cropped during extraction cannot be recovered and are not replaced with zeroes.

Example:
    >>> import kornia.augmentation as K
    >>> pas = PatchSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), patchwise_apply=False)
    >>> out = pas.extract_patches(torch.arange(16).view(1, 1, 4, 4), grid_size=(2, 2))
    >>> pas.restore_from_patches(out, grid_size=(2, 2))
    tensor([[[[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11],
              [12, 13, 14, 15]]]])

zThe patch count z, must equal grid_size[0] * grid_size[1] for rN   )ÚBÚNÚCÚHÚW)
r>   rR   r   ri   rS   ÚreshapeÚpermuter}   ry   Úmax)rW   rƒ   r>   ry   ÚbatchÚcountÚchannelsrs   rt   Úpatches_tensorÚrestored_tensorr;   s   &&&&        r#   Úrestore_from_patchesÚ$PatchSequential.restore_from_patches]  s  € ð, ÒØŸ™ˆIØ× Ñ  Ô+Ü˜7Ò$=Ô>Ø07·±Ñ-ˆ�h¨Ø˜a•L 9¨Q¥<Õ/Ô/ÜÐ/°¨wÐ6bÐclÐbmÐmnÐoÓpÐpØ Ÿ™¨¸!µ¸iÈ½lÈHÐ^cÓdˆØ(×0Ñ0°°A°q¸!¸QÀÓB×JÑJØ˜Y q�\¨FÕ2°I¸aµLÀ5Õ4Hó
ˆð Š?ÜŸeše OÈ#Ó5NÉ#ÀQ´s¸1¸a³y³jÉ#Ñ5NÓOˆOØÐùò 6Os   Ã*D	
c               ó    € V ^8„  d   QhRRRR/# )r   rm   rn   r   úList[PatchParamItem]r!   )r"   s   "r#   r$   rL   ƒ  s   € ÷ 3ñ 3¨jð 3Ð=Qñ 3r%   c           	     ó:  € \        V4      ^8X  d²   V P                  WP                  4      w  r#rEVw  rgr‰\        P                  ! VV P
                  ^ ,          V P
                  ^,          ,          VW„,           V,           V P
                  ^ ,          ,          W’,           V,           V P
                  ^,          ,          34      p\        V4      ^8w  d   \        VR4       V^,          V P
                  ^ ,          V P
                  ^,          ,          8w  d   \        RV R24      h. p
V P                  '       g¡   V P                  \        P                  ! ^V^ ,          V^,          ,          .VR,          O4      4      p\        P                  ! ^ V^ ,          V^,          ,          4      pV UUu. uF  w  rÞ\        VP                  4       V4      NK!  	  p
ppV
# V P                  '       gz   V P                  '       dh   V P                  \        P                  ! V^ ,          V^,          ,          ^.VR,          O4      4      pV UUu. uF  w  rß\        V.V4      NK  	  p
ppV
# V P                  \        P                  ! V^,          V^ ,          .VR,          O4      4      p\        P                  ! ^ V^ ,          V^,          ,          V^,          R7      pV UUu. uF%  w  rß\        WÏ,           P                  4       V4      NK'  	  p
ppV
# u uppi u uppi u uppi )aì  Generate patch-level parameters for a forward pass.

Args:
    batch_shape: Image shape ``(B, C, H, W)`` or extracted patch shape ``(B, N, C, h, w)``,
        with ``N = grid_size[0] * grid_size[1]``. Image shapes are converted to patch shapes
        using this container's ``grid_size`` and ``padding`` without allocating an image.

Returns:
    List of :class:`PatchParamItem` entries, including patch indices and
    operation params.

Example:
    >>> seq = PatchSequential(K.RandomHorizontalFlip(p=1), grid_size=(2, 3), patchwise_apply=False)
    >>> image = torch.rand(2, 3, 5, 7)
    >>> params = seq.forward_parameters(image.shape)
    >>> output = seq(image, params=params)
    >>> torch.equal(output, seq(image, params=params))
    True
z3either (B, C, H, W) or (B, rows * columns, C, h, w)zSExpected image shape (B, C, H, W) or patch shape (B, rows * columns, C, h, w). Got rN   :r   NN)r,   )r6   rh   r?   r3   ÚSizer>   r   rS   rC   Úgenerate_parametersr9   r   ÚtolistrA   rD   )rW   rm   ÚleftÚrightÚtopÚbottomr�   r�   rs   rt   Ú	out_paramÚparamsr   ÚpÚ_r;   s   &&              r#   Úforward_parametersÚ"PatchSequential.forward_parametersƒ  s–  € ô( ˆ{Ó˜qÔ Ø'+×'<Ñ'<¸[Ï,É,Ó'WÑ$ˆD˜Ø-8Ñ*ˆE˜VÜŸ*š*àØ—N‘N 1Õ%¨¯©°qÕ(9Õ9ØØ•\ FÕ*¨t¯~©~¸aÕ/@Õ@Ø•\ EÕ)¨d¯n©n¸QÕ.?Õ?ðóˆKô ˆ{Ó˜qÔ Ü# KÐ1fÔgØ�q�>˜TŸ^™^¨AÕ.°·±ÀÕ1BÕBÔBÜØeÐfqÐerÐrsÐtóð ð +-ˆ	Ø×#×#Ð#Ø×-Ñ-¬e¯jªj¸!¸[È½^ÈkÐZ[ÍnÕ=\Ð9oÐ_jÐkmÕ_nÐ9oÓ.pÓqˆFÜ—l’l 1 k°!¥n°{À1µ~Õ&EÓFˆGÙIOÔPÉÁÀœ¨¯©Ó(8¸!Ö<ÉˆIÑPð Ðð ×#×#Ð#¨×(9×(9Ð(9Ø×-Ñ-¬e¯jªj¸+Àa½.È;ÐWXÍ>Õ:YÐ[\Ð9oÐ_jÐkmÕ_nÐ9oÓ.pÓqˆFÙ<BÔC¹F±D°Aœ¨¨¨QÖ/¹FˆIÑCð Ðð ×-Ñ-¬e¯jªj¸+Àa½.È+ÐVWÍ.Ð9kÐ[fÐgiÕ[jÐ9kÓ.lÓmˆFÜ—l’l 1 k°!¥n°{À1µ~Õ&EÈKÐXYÍNÔ[ˆGÙOUÔVÉvÁtÀqœ¨­×(<Ñ(<Ó(>ÀÖBÉvˆIÑVØÐùó Qùó Dùó
 Ws   Æ.%LÉLË+Lc               ó    € V ^8„  d   QhRRRR/# )r   rm   rn   r   zIterator[Tuple[ParamItem, int]]r!   )r"   s   "r#   r$   rL   ¸  s   € ÷ 8ñ 8¨zð 8Ð>]ñ 8r%   c              #  óX  "  € V P                   '       dß   Rp\        V^ ,          4       FÄ  pV P                  VR7      w  rET;'       d    V'       * pV F–  p\        V^,          \        \
        \        P                  34      '       dK   \        V^ ,          V^,          P                  \        P                  ! VR,          4      4      4      V3x € K  \        V^ ,          R4      V3x € K˜  	  KÆ  	  R# \        V P                  4       4       F˜  w  r7\        V^,          \        \
        \        P                  34      '       dK   \        V^ ,          V^,          P                  \        P                  ! VR,          4      4      4      V3x € K�  \        V^ ,          R4      V3x € Kš  	  R# 5i)zÏGet multiple forward sequence but maximumly one mix augmentation in between.

Args:
    batch_shape: 5-dim shape arranged as :math:``(N, B, C, H, W)``, in which N represents
        the number of sequence.

T)Úwith_mix:rO   NNN)rD   ÚrangeÚget_random_forward_sequencer2   r   r   ÚKÚMixAugmentationBaseV2r   r¢   r3   r—   r8   Únamed_children)rW   rm   r¦   r;   ÚseqÚ	mix_addedÚsÚnchilds   &&      r#   r˜   Ú#PatchSequential.generate_parameters¸  sK  é € ð ××ÐàˆHÜ˜; q�>Ö*�Ø!%×!AÑ!AÈ8Ð!AÓ!T‘�Ø#×5Ð5¨I¬�Û�AÜ! ! A¥$Ô):¼NÌA×LcÑLcÐ(d×eÒeÜ'¨¨!­¨a°­d×.EÑ.EÄeÇjÂjÐQ\Ð]_ÕQ`ÓFaÓ.bÓcÐefÐfÔfä'¨¨!­¨dÓ3°QÐ6Ô6ó	 ó +ô ' t×':Ñ':Ó'<Ö=‘	�Ü˜f Q�iÔ*;¼^ÌQ×MdÑMdÐ)e×fÒfÜ# F¨1¥I¨v°a­y×/KÑ/KÌEÏJÊJÐWbÐceÕWfÓLgÓ/hÓiÐklÐlÔlä# F¨1¥I¨tÓ4°aÐ7Ô7ó	 >ùs   ‚AF*Á	E!F*c               ó$   € V ^8„  d   QhRRRRRR/# )r   rd   r    rŸ   r•   r   r!   )r"   s   "r#   r$   rL   Ò  s#   € ÷ 'ñ ' |ð 'Ð=Qð 'ÐVbñ 'r%   c                ó‚  € VP                   pVP                  ! R.VRR O5!  pV F†  pV P                  VP                  P                  4      p\        VP                  VP                  4      pW,          p\        P                  ! WuVP                  / R7      pVP                  V4      W&   Kˆ  	  VP                  V4      # )zÕApply module parameters to selected patch indices.

Args:
    input: Tensor of extracted patches.
    params: Patch operation payloads with ``indices`` and per-module params.

Returns:
    Augmented patch tensor.
N)Ú
extra_argsr1   éýÿÿÿ)ri   rŠ   Úget_submoduleÚparamÚnamer<   r   r   r   Ú	transformÚto)	rW   rd   rŸ   Úin_shapeÚpatch_paramÚmoduler   Ú_inputÚoutputs	   &&&      r#   Úforward_by_paramsÚ!PatchSequential.forward_by_paramsÒ  s¥   € ð —;‘;ˆØ—’˜bÐ1 8¨B¨C =Ó1ˆã!ˆKà×'Ñ'¨×(9Ñ(9×(>Ñ(>Ó?ˆFÜ$ [×%8Ñ%8¸%¿,¹,ÓGˆGØ•^ˆFÜ'×1Ò1°&À+×BSÑBSÐ`bÔcˆFà#ŸY™Y uÓ-ˆE‹Nñ "ð �}‰}˜XÓ&Ð&r%   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   rd   r    rŸ   úOptional[List[PatchParamItem]]r³   úOptional[Dict[str, Any]]r   r!   )r"   s   "r#   r$   rL   ê  s,   € ÷ ñ Ø!ðØ+IðØWoðà	ñr%   c                ó  € V P                  WP                  4      pV P                  WP                  V4      pVf   V P	                  VP
                  4      pV P                  W4      pV P                  WP                  VR7      pW n        V# )aL  Apply patch-wise augmentation to an input tensor.

Args:
    input: Input image tensor.
    params: Patch-level parameters from :meth:`forward_parameters`, or ``None`` to generate them.
    extra_args: Optional runtime overrides (unused in this implementation).

Returns:
    Augmented image tensor after extract-transform-restore.
)ry   )	rj   r?   r€   r>   r¢   ri   r¿   r’   Ú_params)rW   rd   rŸ   r³   ry   s   &&&& r#   Útransform_inputsÚ PatchSequential.transform_inputsê  sz   € ð ×"Ñ" 5¯,©,Ó7ˆØ×$Ñ$ U¯N©N¸CÓ@ˆØŠ>Ø×,Ñ,¨U¯[©[Ó9ˆFØ×&Ñ& uÓ5ˆØ×)Ñ)¨%·±ÀSÐ)ÓIˆØŒàˆr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   rd   r    rŸ   r•   r³   rÄ   r   r!   )r"   s   "r#   r$   rL     ó2   € ÷ lñ lØ!ðlØ+?ðlØMeðlà	ñlr%   c                óH   € V P                  4       '       d   V# \        R4      h)a9  Inverse the patch pipeline when supported.

Args:
    input: Augmented image tensor.
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Original tensor for intensity-only pipelines.

Raises:
    NotImplementedError: Geometric patch pipelines cannot be inverted.
úFPatchSequential inverse cannot be used with geometric transformations.©Úis_intensity_onlyrr   ©rW   rd   rŸ   r³   s   &&&&r#   Úinverse_inputsÚPatchSequential.inverse_inputs  ó$   € ð  ×!Ñ!×#Ò#ØˆLä!Ð"jÓkÐkr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# rÊ   r!   )r"   s   "r#   r$   rL     s2   € ÷ nñ nØ!ðnØ+?ðnØMeðnà	ñnr%   c                óH   € V P                  4       '       d   V# \        R4      h)aQ  Transform masks for intensity-only patch pipelines.

Args:
    input: Input mask tensor.
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Unchanged mask tensor when the pipeline is intensity-only.

Raises:
    NotImplementedError: Geometric patch pipelines are unsupported for masks.
úHPatchSequential for boxes cannot be used with geometric transformations.rÎ   rÐ   s   &&&&r#   Útransform_masksÚPatchSequential.transform_masks  ó$   € ð  ×!Ñ!×#Ò#ØˆLä!Ð"lÓmÐmr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# rÊ   r!   )r"   s   "r#   r$   rL   +  rË   r%   c                óH   € V P                  4       '       d   V# \        R4      h)aI  Inverse mask transforms for supported patch pipelines.

Args:
    input: Augmented mask tensor.
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Original mask tensor for intensity-only pipelines.

Raises:
    NotImplementedError: Geometric patch pipelines cannot be inverted.
rÍ   rÎ   rÐ   s   &&&&r#   Úinverse_masksÚPatchSequential.inverse_masks+  rÓ   r%   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   rd   r   rŸ   r•   r³   rÄ   r   r!   )r"   s   "r#   r$   rL   @  s2   € ÷ nñ nØðnØ$8ðnØF^ðnà	ñnr%   c                óH   € V P                  4       '       d   V# \        R4      h)aE  Transform boxes for intensity-only patch pipelines.

Args:
    input: Input boxes.
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Unchanged boxes when the pipeline is intensity-only.

Raises:
    NotImplementedError: Geometric patch pipelines are unsupported for boxes.
rÖ   rÎ   rÐ   s   &&&&r#   Útransform_boxesÚPatchSequential.transform_boxes@  rÙ   r%   c               ó(   € V ^8„  d   QhRRRRRRRR/# rß   r!   )r"   s   "r#   r$   rL   U  s2   € ÷ lñ lØðlØ$8ðlØF^ðlà	ñlr%   c                óH   € V P                  4       '       d   V# \        R4      h)a<  Inverse box transforms for supported patch pipelines.

Args:
    input: Augmented boxes.
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Original boxes for intensity-only pipelines.

Raises:
    NotImplementedError: Geometric patch pipelines cannot be inverted.
rÍ   rÎ   rÐ   s   &&&&r#   Úinverse_boxesÚPatchSequential.inverse_boxesU  rÓ   r%   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   rd   r   rŸ   r•   r³   rÄ   r   r!   )r"   s   "r#   r$   rL   j  s2   € ÷ rñ rØðrØ(<ðrØJbðrà	ñrr%   c                óH   € V P                  4       '       d   V# \        R4      h)aU  Transform keypoints for intensity-only patch pipelines.

Args:
    input: Input keypoints.
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Unchanged keypoints when the pipeline is intensity-only.

Raises:
    NotImplementedError: Geometric patch pipelines are unsupported for keypoints.
zLPatchSequential for keypoints cannot be used with geometric transformations.rÎ   rÐ   s   &&&&r#   Útransform_keypointsÚ#PatchSequential.transform_keypointsj  s$   € ð  ×!Ñ!×#Ò#ØˆLä!Ð"pÓqÐqr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# rè   r!   )r"   s   "r#   r$   rL     s2   € ÷ lñ lØðlØ(<ðlØJbðlà	ñlr%   c                óH   € V P                  4       '       d   V# \        R4      h)a¦  Inverse keypoint transforms for supported patch pipelines.

Args:
    input: Augmented keypoints (conceptually ``(B, N, 2)`` coordinates,
        where the last dimension stores ``(x, y)``).
    params: Parameters used during forward.
    extra_args: Optional runtime overrides.

Returns:
    Original keypoints for intensity-only pipelines.

Raises:
    NotImplementedError: Geometric patch pipelines cannot be inverted.
rÍ   rÎ   rÐ   s   &&&&r#   Úinverse_keypointsÚ!PatchSequential.inverse_keypoints  s$   € ð" ×!Ñ!×#Ò#ØˆLä!Ð"jÓkÐkr%   c               ó(   € V ^8„  d   QhRRRRRRRR/# rÂ   r!   )r"   s   "r#   r$   rL   •  s8   € ÷ lñ làðlð /ðlð -ð	lð
 
ñlr%   c                óH   € V P                  4       '       d   V# \        R4      h)z�Inverse transformation.

Used to inverse a torch.Tensor according to the performed transformation by a forward pass, or with respect to
provided parameters.
rÍ   rÎ   rÐ   s   &&&&r#   ÚinverseÚPatchSequential.inverse•  s$   € ð ×!Ñ!×#Ò#ØˆLä!Ð"jÓkÐkr%   c               ó$   € V ^8„  d   QhRRRRRR/# )r   rd   r    rŸ   rÃ   r   r!   )r"   s   "r#   r$   rL   ¥  s#   € ÷ ;ñ ;˜\ð ;Ð3Qð ;Ð]iñ ;r%   c                ój   € \        V\        34      '       d   \        R4      hV P                  WR7      # )zSAugment an image batch, optionally replaying previously generated patch parameters.z'tuple input is not currently supported.)rŸ   )r2   rT   rS   rÇ   )rW   rd   rŸ   s   &&&r#   ÚforwardÚPatchSequential.forward¥  s1   € ä�eœe˜X×&Ò&ÜÐFÓGÐGà×$Ñ$ UÐ$Ó:Ð:r%   )rÆ   r>   r?   rC   )rP   rP   )N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rV   ÚstaticmethodrR   rj   rh   r€   r’   r¢   r˜   r¿   rÇ   rÑ   r×   rÜ   rá   rå   rê   rî   rò   rö   Ú__static_attributes__Ú__classcell__)rY   s   @r#   r   r   =   sÜ   ø† ñBðH*5ð &,ð*5ð ð	*5ð
 )-ð*5ð #'ð*5ð !%ð*5ð ;@ð*5ð PT÷*5ò *5ðX ô`ó ð`÷F÷(c÷,:B÷x$õL3õj8õ4'÷0÷.l÷*n÷*l÷*n÷*l÷*r÷*l÷,l÷ ;õ ;r%   )/Ú
__future__r   Útypingr   r   r   r   r   r	   r
   r   r3   Útorch.nn.functionalr   Ú
functionalr}   Úkornia.augmentationÚaugmentationr©   Úkornia.augmentation.baser   Ú!kornia.augmentation.utils.helpersr   Úkornia.contrib.extract_patchesr   Úkornia.core.checkr   Úkornia.core.opsr   Úkornia.core.utilsr   Úkornia.geometry.boxesr   Úkornia.geometry.keypointsr   Úbaser   Úimager   Úopsr   rŸ   r   r   Ú__all__r<   r   r!   r%   r#   Ú<module>r     sb   ðõ$ #ç N× NÓ Nã ß Ð Ý å Ý 6Ý EÝ AÝ 0Ý +Ý 4Ý 'Ý /å  Ý "Ý #ß -àÐ
€õDô"m	;�oö m	;r%   