§
    ­¸jl�  ã                  óv  — d Z ddlmZ ddlmZmZmZm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ZddlmZ dd	lmZ  e¦   «         Zd=d„Zd>d„Z G d„ d¦  «        Z	 d?d@d„Zej        j        	 d?dAd„¦   «         Z	 	 dBdCd%„Z	 	 dBdCd&„Zd'ZdDd,„Z 	 	 	 dEdFd2„Z!dGd3„Z"	 	 	 dEdHd5„Z# G d6„ d7¦  «        Z$dId9„Z%dJd<„Z&dS )Kz8Tensor utilities: NestedTensor, collate_fn, and helpers.é    )Úannotations)ÚCallableÚIteratorÚMappingÚSequence)Úpartial)ÚMappingProxyType)ÚAnyÚcastÚoverloadN)ÚTensor)Ú
get_loggerÚvalueÚintÚmultipleÚreturnc                ó~   — | dk     rt          d| › �¦  «        ‚|dk    rt          d|› �¦  «        ‚| |z   dz
  |z  |z  S )an  Round *value* up to the next multiple of *multiple*.

    Args:
        value: Non-negative integer to round.
        multiple: Positive integer divisor.

    Returns:
        The smallest integer greater than or equal to *value* that is an exact multiple of *multiple*.

    Raises:
        ValueError: If ``value`` is negative or ``multiple`` is not positive.
    r   z value must be non-negative, got z)multiple must be a positive integer, got é   )Ú
ValueError)r   r   s     úh/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/utilities/tensors.pyÚ_round_up_to_multipler      s^   € ð ˆq‚y€yÝÐC¸EÐCÐCÑDÔDÐDØ�1‚}€}ÝÐOÀXÐOÐOÑPÔPÐPØ�XÑ Ñ! hÑ.°(Ñ:Ð:ó    Úthe_listúlist[list[int]]ú	list[int]c                ó¦   — t          | d         ¦  «        }| dd…         D ]0}t          |¦  «        D ]\  }}t          ||         |¦  «        ||<   ŒŒ1|S )z¶Return element-wise maximums of a list of lists.

    Args:
        the_list: List of integer lists, all of the same length.

    Returns:
        List of per-position maximums.
    r   r   N)ÚlistÚ	enumerateÚmax)r   ÚmaxesÚsublistÚindexÚitems        r   Ú_max_by_axisr$   3   sj   € õ �˜!”ÑÔ€EØ˜A˜B˜B”<ð 3ð 3ˆÝ$ WÑ-Ô-ð 	3ð 	3‰KˆE�4Ý˜u Uœ|¨TÑ2Ô2ˆE�%‰LˆLð	3à€Lr   c                  ó<   — e Zd ZdZddd„Zdd„Zdd„Zdd„Zdd„ZdS )ÚNestedTensorzŸBatch of tensors with variable spatial sizes, padded to a common size.

    Stores both the padded tensor and a boolean mask indicating padding positions.
    FÚtensorsr   ÚmaskúTensor | NoneÚ
no_paddingÚboolr   ÚNonec                ó0   — || _         || _        || _        d S ©N)r'   r(   r*   )Úselfr'   r(   r*   s       r   Ú__init__zNestedTensor.__init__I   s   € ØˆŒØˆŒ	ð %ˆŒˆˆr   Údeviceútorch.deviceÚkwargsr
   ú'NestedTensor'c                ó�   —  | j         j        |fi |¤Ž}| j        }|�|€J ‚ |j        |fi |¤Ž}nd}t          ||| j        ¦  «        S )zÔMove tensors and mask to *device*.

        Args:
            device: Target device. **kwargs: Additional arguments forwarded to ``Tensor.to``.

        Returns:
            New NestedTensor on *device*.
        N)r'   Útor(   r&   r*   )r/   r1   r3   Úcast_tensorr(   Ú	cast_masks         r   r6   zNestedTensor.toS   sl   € ð &�d”l”o fÐ7Ð7°Ð7Ð7ˆØŒyˆØÐØÐ#Ð#Ð#Ø˜œ Ð1Ð1¨&Ð1Ð1ˆIˆIàˆIÝ˜K¨°D´OÑDÔDÐDr   c                óœ   — t          | j                             ¦   «         | j        �| j                             ¦   «         nd| j        ¦  «        S )u…   Pin tensor and mask memory for faster CPUâ†’GPU transfer.

        Returns:
            New NestedTensor with pinned memory.
        N)r&   r'   Ú
pin_memoryr(   r*   ©r/   s    r   r:   zNestedTensor.pin_memorye   sI   € õ ØŒL×#Ò#Ñ%Ô%Ø&*¤iÐ&;ˆDŒI× Ò Ñ"Ô"Ð"ÀØŒOñ
ô 
ð 	
r   útuple[Tensor, Tensor | None]c                ó   — | j         | j        fS )zyReturn ``(tensors, mask)`` tuple.

        Returns:
            Tuple of the padded tensor and the boolean mask.
        )r'   r(   r;   s    r   Ú	decomposezNestedTensor.decomposeq   s   € ð Œ|˜TœYÐ&Ð&r   Ústrc                ó*   — t          | j        ¦  «        S r.   )r?   r'   r;   s    r   Ú__repr__zNestedTensor.__repr__y   s   € Ý�4”<Ñ Ô Ð r   N©F)r'   r   r(   r)   r*   r+   r   r,   )r1   r2   r3   r
   r   r4   )r   r4   )r   r<   )r   r?   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r6   r:   r>   rA   © r   r   r&   r&   C   s‹   € € € € € ðð ð
%ð %ð %ð %ð %ðEð Eð Eð Eð$

ð 

ð 

ð 

ð'ð 'ð 'ð 'ð!ð !ð !ð !ð !ð !r   r&   Útensor_listúlist[Tensor] | TensorÚ
block_sizeú
int | Nonec                ó¬  ‡— t          t          t                   | ¦  «        }|d         j        dk    �rt	          j        ¦   «         rt          ||¬¦  «        S t          d„ |D ¦   «         ¦  «        Š|�2t          ‰d         |¦  «        ‰d<   t          ‰d         |¦  «        ‰d<   t          |¦  «        g‰z   }|\  }}}}|d         j
        }|d         j        }	t          ˆfd„|D ¦   «         ¦  «        }
|
r^t          | t          ¦  «        rI|                      ¦   «         r5t          j        |||ft          j        |	¬	¦  «        }t%          | |d
¦  «        S t          j        |||	¬	¦  «        }|
r$t          j        |||ft          j        |	¬	¦  «        n#t          j        |||ft          j        |	¬	¦  «        }t)          |||¦  «        D ]n\  }}}|d|j        d         …d|j        d         …d|j        d         …f                              |¦  «         |
s!d|d|j        d         …d|j        d         …f<   Œont/          d¦  «        ‚t%          |||
¦  «        S )aK  Pack image tensors into a single NestedTensor, padding when needed.

    Args:
        tensor_list: List of 3-D ``(C, H, W)`` tensors with possibly different spatial sizes,
            or one 4-D ``(B, C, H, W)`` batch tensor.
        block_size: When set, round the padded ``H`` and ``W`` up to the next
            multiple of *block_size* before allocating the batch tensor.  Used to satisfy backbone divisibility
            requirements (e.g. windowed-attention backbones require ``H % (patch_size * num_windows) == 0``).  The
            rounded-up strip is explicitly tracked in the ``mask`` as padding.

    Returns:
        NestedTensor with all images padded to the maximum spatial dimensions (rounded up to *block_size* when
        provided). When *tensor_list* is already a contiguous 4-D batch that needs no padding, the returned
        ``.tensors`` is *tensor_list* itself (same storage, not a copy): mutating it after this call also mutates
        the NestedTensor. A non-contiguous 4-D batch instead gets an independent, contiguous copy, like every
        other input shape.
    r   é   ©rJ   c                ó6   — g | ]}t          |j        ¦  «        ‘ŒS rG   ©r   Úshape)Ú.0Úimgs     r   ú
<listcomp>z2nested_tensor_from_tensor_list.<locals>.<listcomp>›   s    € Ð CÐ CÐ C°S¥ c¤i¡¤Ð CÐ CÐ Cr   Nr   é   c              3  óH   •K  — | ]}t          |j        ¦  «        ‰k    V — Œd S r.   rP   )rR   rS   Úmax_sizes     €r   ú	<genexpr>z1nested_tensor_from_tensor_list.<locals>.<genexpr>§   s0   øè è € ÐGÐG¸�˜cœi™œ¨HÒ4ÐGÐGÐGÐGÐGÐGr   ©Údtyper1   TFznot supported)r   r   r   ÚndimÚtorchvisionÚ_is_tracingÚ$_onnx_nested_tensor_from_tensor_listr$   r   ÚlenrZ   r1   ÚallÚ
isinstanceÚis_contiguousÚtorchÚzerosr+   r&   ÚonesÚziprQ   Úcopy_r   )rH   rJ   ÚimagesÚbatch_shapeÚbÚcÚhÚwrZ   r1   r*   r(   ÚtensorrS   Úpad_imgÚmrW   s                   @r   Únested_tensor_from_tensor_listrq   }   so  ø€ õ* •(�6Ô" KÑ0Ô0€Fàˆa„y„~˜ÒÑÝÔ"Ñ$Ô$ð 	Wõ 8¸È:ÐVÑVÔVÐVõ  Ð CÐ C¸FÐ CÑ CÔ CÑDÔDˆØÐ!Ý/°¸´¸ZÑHÔHˆH�Q‰KÝ/°¸´¸ZÑHÔHˆH�Q‰Kå˜6‘{”{�m hÑ.ˆØ ‰
ˆˆ1ˆa�Ø�q”	”ˆØ˜”Ô!ˆõ ÐGÐGÐGÐGÀÐGÑGÔGÑGÔGˆ
Øð 		9�* [µ&Ñ9Ô9ð 		9¸k×>WÒ>WÑ>YÔ>Yð 		9õ ”;  1 a˜yµ´
À6ÐJÑJÔJˆDÝ ¨T°4Ñ8Ô8Ð8Ý”˜[°¸fÐEÑEÔEˆð ðH�EŒK˜˜A˜q˜	­¬¸FÐCÑCÔCÐCå”˜Q  1˜I­U¬ZÀÐGÑGÔGð 	õ
  # 6¨6°4Ñ8Ô8ð 	:ð 	:‰OˆC�˜!Ø�N�c”i ”l�N N c¤i°¤l N°N°c´iÀ´l°NÐBÔC×IÒIÈ#ÑNÔNÐNØð :Ø49��.�C”I˜a”L�. . C¤I¨a¤L .Ð0Ñ1øð	:õ
 ˜Ñ)Ô)Ð)Ý˜  jÑ1Ô1Ð1r   úSequence[Tensor]c                óÎ  ‡— g }t          | d                              ¦   «         ¦  «        D ]…Št          j        ˆfd„| D ¦   «         ¦  «                             t          j        ¦  «        }t          j        |¦  «                             t          j        ¦  «        }|                     |¦  «         Œ†|�Nt          j	        |t          j        ¬¦  «        }|d         |z   dz
  |z  |z  |d<   |d         |z   dz
  |z  |z  |d<   t          |¦  «        }g }g }| D �]}	d„ t          |t          |	j        ¦  «        ¦  «        D ¦   «         }
t          j        j                             |	d|
d         d|
d         d|
d         f¦  «        }|                     |¦  «         t          j        |	d         t          j        |	j        ¬¦  «        }t          j        j                             |d|
d         d|
d         fd	d¦  «        }|                     |                     t          j        ¦  «        ¦  «         �Œt          j        |¦  «        }t          j        |¦  «        }t)          ||¬
¦  «        S )až  ONNX-tracing-compatible variant of ``nested_tensor_from_tensor_list``.

    Args:
        tensor_list: List of 3-D ``(C, H, W)`` tensors or one 4-D ``(B, C, H, W)`` batch tensor.
        block_size: When set, round ``H`` and ``W`` up to the next multiple of
            this value before padding.  See :func:`nested_tensor_from_tensor_list`.

    Returns:
        Padded NestedTensor suitable for ONNX export.
    r   c                óN   •— g | ]!}t          j        |j        ‰         ¦  «        ‘Œ"S rG   )rc   Ú	as_tensorrQ   )rR   rS   Úis     €r   rT   z8_onnx_nested_tensor_from_tensor_list.<locals>.<listcomp>×   s)   ø€ Ð VÐ VÐ VÀ3¥¤°´¸1´Ñ!>Ô!>Ð VÐ VÐ Vr   N©rZ   r   rU   c                ó   — g | ]
\  }}||z
  ‘ŒS rG   rG   )rR   Ús1Ús2s      r   rT   z8_onnx_nested_tensor_from_tensor_list.<locals>.<listcomp>è   s    € ÐQÐQÐQ¡  R�B˜‘GÐQÐQÐQr   rY   Úconstant)r(   )ÚrangeÚdimrc   Ústackr6   Úfloat32r   Úint64Úappendru   Útuplerf   rQ   ÚnnÚ
functionalÚpadÚ
zeros_liker   r1   r+   r&   )rH   rJ   rW   Ú	dim_sizesÚ
max_size_iÚbsÚmax_size_tupleÚpadded_imgsÚpadded_masksrS   ÚpaddingÚ
padded_imgrp   Úpadded_maskrn   r(   rv   s                   @r   r^   r^   Ã   s;  ø€ ð  €HÝ�;˜q”>×%Ò%Ñ'Ô'Ñ(Ô(ð $ð $ˆõ ”KÐ VÐ VÐ VÐ VÈ+Ð VÑ VÔ VÑWÔW×ZÒZÕ[`Ô[hÑiÔiˆ	Ý”Y˜yÑ)Ô)×,Ò,­U¬[Ñ9Ô9ˆ
Ø�Š˜
Ñ#Ô#Ð#Ð#ØÐåŒ_˜Z­u¬{Ð;Ñ;Ô;ˆØ  œ bÑ(¨1Ñ,°Ñ3°rÑ9ˆ�‰Ø  œ bÑ(¨1Ñ,°Ñ3°rÑ9ˆ�‰Ý˜8‘_”_€Nð €KØ€LØð 	8ñ 	8ˆØQÐQ­3¨~½uÀSÄYÑ?OÔ?OÑ+PÔ+PÐQÑQÔQˆõ ”XÔ(×,Ò,¨S°1°g¸a´jÀ!ÀWÈQÄZÐQRÐT[Ð\]ÔT^Ð2_Ñ`Ô`ˆ
Ø×Ò˜:Ñ&Ô&Ð&åÔ˜S œV­5¬9¸S¼ZÐHÑHÔHˆÝ”hÔ)×-Ò-¨a°!°W¸Q´ZÀÀGÈAÄJÐ1OÐQ[Ð]^Ñ_Ô_ˆØ×Ò˜KŸNšN­5¬:Ñ6Ô6Ñ7Ô7Ð7Ñ7åŒ[˜Ñ%Ô%€FÝŒ;�|Ñ$Ô$€Då˜ TÐ*Ñ*Ô*Ð*r   rd   FÚinputr   ÚgridÚpadding_moder?   Úalign_cornersr+   c                óX  ‡‡‡‡‡‡ — ddl mc m} | j        j        dvr|                     | |d||¬¦  «        S |dvrd|›d�}t          |¦  «        ‚| j        \  ŠŠ}Š |j        d	         |j        d
         cŠŠ|r)|d         d	z   ‰ d	z
  z  d
z  }|d         d	z   |d	z
  z  d
z  }n(|d         d	z   ‰ z  d
z  dz
  }|d         d	z   |z  d
z  dz
  }|                     ¦   «          	                    ¦   «         }	|                     ¦   «          	                    ¦   «         }
|	d	z   }|
d	z   }||	 
                    ¦   «         z
                       | j        ¦  «                             d	¦  «        }||
 
                    ¦   «         z
                       | j        ¦  «                             d	¦  «        }|                     d¦  «        }||z
  }||z
  }|dk    re|	                     d‰ d	z
  ¦  «        }	|
                     d|d	z
  ¦  «        }
|                     d‰ d	z
  ¦  «        }|                     d|d	z
  ¦  «        }n˜|	dk    |	‰ k     z  }|dk    |‰ k     z  }|
dk    |
|k     z  }|dk    ||k     z  }|	                     d‰ d	z
  ¦  «        }	|
                     d|d	z
  ¦  «        }
|                     d‰ d	z
  ¦  «        }|                     d|d	z
  ¦  «        }|                      d
¦  «        Šdˆˆˆˆˆˆ fd„} ||
|	¦  «        } ||
|¦  «        } |||	¦  «        } |||¦  «        }|dk    rl|||z                       d	¦  «        z  }|||z                       d	¦  «        z  }|||z                       d	¦  «        z  }|||z                       d	¦  «        z  }||z  |z  ||z  |z  z   ||z  |z  z   ||z  |z  z   S )u™  Bilinear grid sampling compatible with all PyTorch backends including MPS and XLA.

    Drop-in replacement for ``F.grid_sample(input, grid, mode='bilinear', ...)``. On MPS, ``F.grid_sample`` backward
    (``grid_sampler_2d_backward``) is not yet implemented and silently falls back to CPU. On XLA, routing through the
    gather path avoids relying on ``F.grid_sample``'s XLA decomposition (host-fallback risk on older torch_xla).  This
    function uses gather-based index arithmetic â€” natively supported on every backend â€” for the MPS/XLA path, while
    delegating to ``F.grid_sample`` on CUDA/CPU where its fused kernel is faster.  The two paths are numerically
    identical, so model accuracy is unaffected.

    Args:
        input: Feature map of shape ``(N, C, H, W)``.
        grid: Sampling grid of shape ``(N, Hg, Wg, 2)`` with values in ``[-1, 1]``.
        padding_mode: ``"zeros"`` returns 0 for out-of-bounds samples; ``"border"`` clamps to the nearest border pixel.
        align_corners: If ``True``, grid extremes ``Â±1`` map to pixel centres at positions ``0`` and ``H-1``/``W-1``.

    Returns:
        Sampled tensor of shape ``(N, C, Hg, Wg)``.
    r   N©ÚmpsÚxlaÚbilinear©Úmoder’   r“   ©rd   ÚborderúUnsupported padding_mode=úP for manual grid sampling. Only 'zeros' and 'border' are supported in this path.r   rU   ©.r   ©.r   ç      à?g      ð?rœ   Úiy_r   Úix_r   c                óâ   •— | ‰z  |z                         d¦  «                             d¦  «                             ‰‰d¦  «        }‰                     d|¦  «                             ‰‰‰‰¦  «        S )Nr   éÿÿÿÿrU   )ÚflattenÚ	unsqueezeÚexpandÚgatherÚview)	r¢   r£   ÚidxÚ
batch_sizeÚchannelsÚflatÚgrid_heightÚ
grid_widthÚwidths	      €€€€€€r   Ú_gatherz&_bilinear_grid_sample.<locals>._gatherF  sh   ø€ Ø�U‰{˜SÑ ×)Ò)¨!Ñ,Ô,×6Ò6°qÑ9Ô9×@Ò@ÀÈXÐWYÑZÔZˆØ�{Š{˜1˜cÑ"Ô"×'Ò'¨
°H¸kÈ:ÑVÔVÐVr   rd   )r¢   r   r£   r   r   r   )Útorch.nn.functionalrƒ   r„   r1   ÚtypeÚgrid_sampler   rQ   ÚfloorÚlongÚfloatr6   rZ   r§   Ú
new_tensorÚclampr¦   )!r�   r‘   r’   r“   ÚFÚmsgÚheightÚixÚiyÚix0Úiy0Úix1Úiy1Úwx1Úwy1ÚoneÚwx0Úwy0Úin_x0Úin_x1Úin_y0Úin_y1r²   Úv00Úv10Úv01Úv11r¬   r­   r®   r¯   r°   r±   s!                              @@@@@@r   Ú_bilinear_grid_samplerÑ   ø   sN  øøøøøø€ ð0 $Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#à„|Ô Ð.Ð.Ø�}Š}˜U D¨zÈÐdqˆ}ÑrÔrÐràÐ.Ð.Ð.ðD¨ð Dð Dð Dð 	õ ˜‰oŒoÐà*/¬+Ñ'€J�˜& %Ø"œj¨œm¨T¬Z¸¬]Ð€K�ð ð 3Ø�6Œl˜QÑ 5¨1¡9Ñ-°Ñ1ˆØ�6Œl˜QÑ 6¨A¡:Ñ.°Ñ2ˆˆà�6Œl˜QÑ %Ñ'¨!Ñ+¨cÑ1ˆØ�6Œl˜QÑ &Ñ(¨1Ñ,¨sÑ2ˆà
�(Š(‰*Œ*�/Š/Ñ
Ô
€CØ
�(Š(‰*Œ*�/Š/Ñ
Ô
€CØ
�‰'€CØ
�‰'€Cð �—	’	‘”Ñ×
Ò
 ¤Ñ
,Ô
,×
6Ò
6°qÑ
9Ô
9€CØ�—	’	‘”Ñ×
Ò
 ¤Ñ
,Ô
,×
6Ò
6°qÑ
9Ô
9€CØ
�.Š.˜Ñ
Ô
€CØ
�‰)€CØ
�‰)€Cà�xÒÐØ�iŠi˜˜5 1™9Ñ%Ô%ˆØ�iŠi˜˜6 A™:Ñ&Ô&ˆØ�iŠi˜˜5 1™9Ñ%Ô%ˆØ�iŠi˜˜6 A™:Ñ&Ô&ˆˆà˜’˜c EškÑ*ˆØ˜’˜c EškÑ*ˆØ˜’˜c FšlÑ+ˆØ˜’˜c FšlÑ+ˆØ�iŠi˜˜5 1™9Ñ%Ô%ˆØ�iŠi˜˜6 A™:Ñ&Ô&ˆØ�iŠi˜˜5 1™9Ñ%Ô%ˆØ�iŠi˜˜6 A™:Ñ&Ô&ˆð �=Š=˜ÑÔ€DðWð Wð Wð Wð Wð Wð Wð Wð Wð Wð Wð ˆ'�#�sÑ
Ô
€CØ
ˆ'�#�sÑ
Ô
€CØ
ˆ'�#�sÑ
Ô
€CØ
ˆ'�#�sÑ
Ô
€Cà�wÒÐØ�U˜U‘]×-Ò-¨aÑ0Ô0Ñ0ˆØ�U˜U‘]×-Ò-¨aÑ0Ô0Ñ0ˆØ�U˜U‘]×-Ò-¨aÑ0Ô0Ñ0ˆØ�U˜U‘]×-Ò-¨aÑ0Ô0Ñ0ˆà�‰9�s‰?˜S 3™Y¨™_Ñ,¨s°S©y¸3©Ñ>ÀÀsÁÈSÁÑPÐPr   c                ó  — ddl mc m} | j        j        dvr|                     | |d||¬¦  «        S |dvrd|›d�}t          |¦  «        ‚| j        \  }}}}	|j        d	         |j        d
         }}
|r)|d         d	z   |	d	z
  z  d
z  }|d         d	z   |d	z
  z  d
z  }n(|d         d	z   |	z  d
z  dz
  }|d         d	z   |z  d
z  dz
  }|                     ¦   «          	                    ¦   «         }|                     ¦   «          	                    ¦   «         }d}|dk    r|dk    ||	k     z  |dk    z  ||k     z  }| 
                    d|	d	z
  ¦  «        }| 
                    d|d	z
  ¦  «        }||	z  |z                        d	¦  «                             d	¦  «                             ||d¦  «        }|                      d
¦  «                             d
|¦  «                             |||
|¦  «        }|�;t!          j        |                     d	¦  «        |t!          j        |¦  «        ¦  «        }|d                              d	¦  «                             |j        ¦  «        }t!          j        t!          j        |t           j        ¬¦  «        ||¦  «        S )už	  Nearest-neighbour grid sampling compatible with all PyTorch backends including MPS and XLA.

    Drop-in replacement for ``F.grid_sample(input, grid, mode='nearest', ...)`` for well-formed
    inputs (``grid``'s batch size matches ``input``'s and its trailing dimension is 2), the same
    contract :func:`_bilinear_grid_sample` relies on -- neither helper independently validates a
    malformed shape the way ``F.grid_sample`` does. This is the nearest-mode counterpart of
    :func:`_bilinear_grid_sample`: on XLA, ``F.grid_sample`` lowers to an ``aten::grid_sampler_2d``
    host fallback, so mask-label sampling silently leaves the device on every criterion call. CUDA
    and CPU keep the fused kernel, which is faster there.

    Ties are rounded with :func:`torch.round` (round-half-to-even), which matches ``F.grid_sample``
    for random grids and for most constructed exact ties. ``floor(x + 0.5)`` is a worse substitute,
    but not a universally opposite one: it disagrees with round-half-to-even at half of all exact
    ties, not every one (verified over [-2.5, -1.5, -0.5, 0.5, 1.5, 2.5]: it agrees at -2.5, -0.5,
    and 1.5).

    Neither rounding rule perfectly matches ``F.grid_sample`` in every case, because the kernel's own
    answer at an exact tie is build-dependent: at width=673 the tie x=0.5 rounds up to 1 on the Linux
    x86 wheels and down to 0 on the macOS and Windows wheels. This gather path always applies
    round-half-to-even, so it is reproducible across hosts where the kernel is not. It is the same
    divergence already documented in
    ``SetCriterion._sample_target_masks_at_points`` (``src/rfdetr/models/criterion.py``), which
    corrects it on CPU/CUDA by calling the real kernel for tied points only; on XLA/MPS that
    correction now reads through this same gather path and so no longer reaches the kernel's
    original tie value in that rare case.

    Args:
        input: Feature map of shape ``(N, C, H, W)``.
        grid: Sampling grid of shape ``(N, Hg, Wg, 2)`` with values in ``[-1, 1]``.
        padding_mode: ``"zeros"`` returns 0 for out-of-bounds samples; ``"border"`` clamps to the
            nearest border pixel.
        align_corners: If ``True``, grid extremes ``Â±1`` map to pixel centres at ``0`` and
            ``H-1``/``W-1``.

    Returns:
        Sampled tensor of shape ``(N, C, Hg, Wg)``.

    Raises:
        ValueError: If *padding_mode* is not ``"zeros"`` or ``"border"``.
    r   Nr•   Únearestr™   r›   r�   rž   r   rU   rŸ   r    r¡   rd   r¥   rw   )r³   rƒ   r„   r1   r´   rµ   r   rQ   Úroundr·   rº   r¦   r§   r¨   r©   rª   rc   Úwherer†   r6   rZ   r+   )r�   r‘   r’   r“   r»   r¼   r¬   r­   r½   r±   r¯   r°   r¾   r¿   Ú
ix_nearestÚ
iy_nearestÚinsider«   ÚoutputÚgrid_xs                       r   Ú_nearest_grid_samplerÛ   X  sµ  € ð\ $Ð#Ð#Ð#Ð#Ð#Ð#Ð#Ð#à„|Ô Ð.Ð.Ø�}Š}˜U D¨yÀ|Ðcpˆ}ÑqÔqÐqàÐ.Ð.Ð.ðD¨ð Dð Dð Dð 	õ ˜‰oŒoÐà*/¬+Ñ'€J�˜& %Ø"œj¨œm¨T¬Z¸¬]�€Kàð 3Ø�6Œl˜QÑ 5¨1¡9Ñ-°Ñ1ˆØ�6Œl˜QÑ 6¨A¡:Ñ.°Ñ2ˆˆà�6Œl˜QÑ %Ñ'¨!Ñ+¨cÑ1ˆØ�6Œl˜QÑ &Ñ(¨1Ñ,¨sÑ2ˆà—’‘”—’Ñ"Ô"€JØ—’‘”—’Ñ"Ô"€Jà€FØ�wÒÐØ ’/ j°5Ò&8Ñ9¸ZÈ1º_ÑMÐQ[Ð^dÒQdÑeˆØ×!Ò! ! U¨Q¡YÑ/Ô/€JØ×!Ò! ! V¨a¡ZÑ0Ô0€Jà˜Ñ 
Ñ*×
3Ò
3°AÑ
6Ô
6×
@Ò
@ÀÑ
CÔ
C×
JÒ
JÈ:ÐW_ÐacÑ
dÔ
d€CØ�]Š]˜1ÑÔ×$Ò$ Q¨Ñ,Ô,×1Ò1°*¸hÈÐU_Ñ`Ô`€FàÐÝ”˜V×-Ò-¨aÑ0Ô0°&½%Ô:JÈ6Ñ:RÔ:RÑSÔSˆð �&Œ\×#Ò# AÑ&Ô&×)Ò)¨&¬,Ñ7Ô7€FÝŒ;•uÔ'¨µe´jÐAÑAÔAÀ6È6ÑRÔRÐRr   )ÚboxesÚlabelsÚareaÚiscrowdÚ	keypointsÚmasksÚtargetsúSequence[dict[str, Any]]Úpad_toútuple[dict[str, Any], ...]c                ó  — |dk     rt          d|› �¦  «        ‚g }| D �]T}t          |¦  «        }t          |d         j        d         ¦  «        }||k    r t                               d||||z
  ¦  «         t          D ]¶}|                     |¦  «        }|�t          j	        |¦  «        r|j
        dk    rŒ9|j        d         |k    r|d|…         ||<   ŒX|j        d         |k     rM|                     ||j        d         z
  g|j        dd…         ¢R ¦  «        }t          j        ||g¦  «        ||<   Œ·t          j        |¦  «        t          ||¦  «        k     |d<   |                     |¦  «         �ŒVt!          |¦  «        S )aØ  Pad every per-object field of each target dict to *pad_to* rows and record which rows are real.

    XLA keys its compiled graph on tensor shapes, and the detection loss is shaped by how many
    ground-truth boxes each image carries -- so a batch with a new per-image box-count tuple forces a
    recompilation.  Padding here, while the tensors are still on the host, keeps every downstream
    shape constant.  The added ``valid`` mask is what lets the matcher and the losses ignore the
    filler rows.

    Args:
        targets: Per-image target dicts, as produced by the dataset.
        pad_to: Row count every image is padded (or truncated) to. Must be a positive integer.

    Returns:
        The same dicts with every entry in :data:`_PER_OBJECT_TARGET_KEYS` resized to *pad_to* rows and a
        boolean ``valid`` mask. Entries that are not per-object, such as ``orig_size``, are left alone.

    Raises:
        ValueError: If ``pad_to`` is not a positive integer.

    Examples:
        >>> import torch
        >>> out = pad_targets_to_fixed_count([{"boxes": torch.zeros(1, 4), "labels": torch.zeros(1)}], 3)
        >>> out[0]["boxes"].shape, out[0]["valid"].tolist()
        (torch.Size([3, 4]), [True, False, False])
    r   z'pad_to must be a positive integer, got rÜ   r   z pad_targets_to=%d is smaller than this image's %d ground-truth boxes; the extra %d box(es) are dropped from training rather than recompiling the graph for them.NÚvalid)r   Údictr   rQ   ÚloggerÚwarningÚ_PER_OBJECT_TARGET_KEYSÚgetrc   Ú	is_tensorr[   Ú	new_zerosÚcatÚarangeÚminr�   r‚   )	râ   rä   ÚpaddedÚtargetÚentryÚcountÚkeyr   Úfillers	            r   Úpad_targets_to_fixed_countrø   µ  s’  € ð4 �‚z€zÝÐKÀ6ÐKÐKÑLÔLÐLØ€FØð ñ ˆÝ�V‘”ˆÝ�E˜'”NÔ(¨Ô+Ñ,Ô,ˆØ�6Š>ˆ>Ý�NŠNðcàØØ˜‘ñô ð õ +ð 	8ð 	8ˆCØ—I’I˜c‘N”NˆEØˆ}¥E¤O°EÑ$:Ô$:ˆ}¸e¼jÈAºo¸oØØŒ{˜1Œ~ Ò&Ð&Ø" 7 F 7œ^��c‘
�
Ø”˜Q” &Ò(Ð(ØŸš¨&°5´;¸q´>Ñ*AÐ)TÀEÄKÐPQÐPRÐPRÄOÐ)TÐ)TÑUÔU�Ý"œY¨¨v Ñ7Ô7��c‘
øÝœ fÑ-Ô-µ°E¸6Ñ0BÔ0BÒBˆˆg‰Ø�Š�eÑÔÐÑÝ�‰=Œ=Ðr   Úbatchúlist[tuple[Any, ...]]ÚpackÚpad_targets_toútuple[Any, ...]c                óš  — t          t          | Ž ¦  «        }t          t          |d         ¦  «        |¬¦  «        }t          |dd…         ¦  «        }|�|rt          |d         |¦  «        |d<   |rVt	          |¦  «        dk    rCt          d„ |d         D ¦   «         ¦  «        r$|t          t          |d         ¦  «        ¦  «        fS |g|¢R S )a[  Module-level collate helper used as the base for :func:`make_collate_fn`.

    Defined at module scope (rather than as a closure inside :func:`make_collate_fn`) so that the resulting
    :class:`functools.partial` is picklable for multi-process DataLoaders and DDP spawn workers.

    Args:
        batch: List of ``(image, target)`` pairs from a dataset.
        block_size: When set, round batch ``H`` and ``W`` up to the next multiple of this value before padding.  See
            :func:`nested_tensor_from_tensor_list`.
        pack: When set, concatenate the target dicts into a :class:`PackedTargets` so that a batch crosses the
            worker-to-main boundary as a handful of tensors instead of one per field per sample.
        pad_targets_to: When set, pad every target dict's per-object fields to this many rows before packing, so
            XLA's compiled graph stops keying on the per-image ground-truth box count. Applied before *pack*: every
            target the packer sees already shares one row count, and packing padded targets is bit-identical to
            packing the same targets first and padding the packed result would be, but without ever materialising
            the intermediate variable-length batch.

    Returns:
        Tuple of ``(NestedTensor_of_images, tuple_of_targets)``, the targets padded and/or packed as configured.
    r   rN   r   Nc              3  ó@   K  — | ]}t          |t          ¦  «        V — Œd S r.   )ra   rè   ©rR   rô   s     r   rX   z+_collate_with_block_size.<locals>.<genexpr>
  s,   è è € Ð&TÐ&TÀ5¥z°%½Ñ'>Ô'>Ð&TÐ&TÐ&TÐ&TÐ&TÐ&Tr   )r   rf   rq   rø   r_   r`   Úpack_targets)rù   rJ   rû   rü   Úcolumnsrh   Úrests          r   Ú_collate_with_block_sizer  ë  sË   € õ4 •3˜�;ÑÔ€GÝ+­D°¸´Ñ,<Ô,<ÈÐTÑTÔT€FÝ�˜˜˜”ÑÔ€DØÐ! dÐ!Ý,¨T°!¬W°nÑEÔEˆˆQ‰Øð 5•�D‘	”	˜Q’�¥3Ð&TÐ&TÈDÐQRÌGÐ&TÑ&TÔ&TÑ#TÔ#T�Ø�¥T¨$¨q¬'¡]¤]Ñ3Ô3Ð4Ð4Øˆ?�Tˆ?ˆ?Ðr   c                ó$   — t          | d¬¦  «        S )a  Collate a list of (image, target) pairs into a batched NestedTensor.

    Uses :func:`nested_tensor_from_tensor_list` with no ``block_size`` rounding. For DataLoaders that need
    backbone-aware rounding (e.g. windowed attention requires divisibility by ``patch_size * num_windows``), use
    :func:`make_collate_fn` instead to obtain a parameterised collate callable.

    Args:
        batch: List of ``(image, target)`` pairs from a dataset.

    Returns:
        Tuple of ``(NestedTensor_of_images, tuple_of_targets)``.
    NrN   )r  )rù   s    r   Ú
collate_fnr    s   € õ $ E°dÐ;Ñ;Ô;Ð;r   ú2Callable[[list[tuple[Any, ...]]], tuple[Any, ...]]c                ó2   — t          t          | ||¬¦  «        S )a)  Build a collate function that rounds batch ``H``/``W`` up to *block_size*.

    Used by the training DataModule to ensure that batched inputs satisfy the backbone's spatial divisibility
    requirement (``patch_size * num_windows``). Passing ``block_size=None`` produces a callable equivalent to
    :func:`collate_fn`.

    The returned callable is a :class:`functools.partial`, not a closure, so it is picklable and safe to use with
    multi-process DataLoaders (``num_workers > 0``) and DDP spawn workers.

    Args:
        block_size: When set, batch ``H`` and ``W`` are rounded up to the next
            multiple of this value before padding.  The rounded-up strip is marked as padding in the NestedTensor mask.
        pack: When set, the collated targets are returned as a :class:`PackedTargets`.
        pad_targets_to: When set, every target dict's per-object fields are padded to this many rows before
            packing. See :func:`_collate_with_block_size`.

    Returns:
        A collate callable suitable for ``torch.utils.data.DataLoader``.
    ©rJ   rû   rü   )r   r  r	  s      r   Úmake_collate_fnr
    s   € õ0 Õ+¸
ÈÐ^lÐmÑmÔmÐmr   c                  ó¢   — e Zd ZdZdZd#d„Zd$d„Zed%d„¦   «         Zed&d„¦   «         Zd'd„Zd(d„Z	d)d„Z
ed*d„¦   «         Zd+d„Zd,d-d „Zd,d.d!„Zd"S )/ÚPackedTargetsa  A batch of per-sample target dicts held as one concatenated tensor per field.

    Every tensor a DataLoader worker returns is moved into its own shared-memory segment and handed to the parent
    as a file descriptor. rf-detr returns one dict of small tensors per sample, so a batch of 16 crosses the
    worker-to-main boundary as 114 objects, of which 112 carry a few kilobytes in total yet pay the per-object
    cost. Concatenating each field into a single tensor and carrying the per-sample shapes as plain ints -- which
    pickle by value and never reach shared memory -- takes that to 9 objects without changing a single byte of
    payload.

    Instances behave as a sequence of the original dicts for iteration and indexing, so consumers that merely
    read a batch need no change. The class deliberately does not subclass :class:`collections.abc.Sequence`:
    PyTorch's pin-memory worker dispatches on that ABC *before* it looks for a ``pin_memory`` method, so a
    Sequence subclass would be taken apart and pinned tensor by tensor, rebuilding in the main process exactly
    what the packing avoided. Indexing rebuilds a dict of views: reassigning a key on the returned dict does not
    write back into the packed storage, but an in-place write into one of its tensors does, since the view shares
    the packed field's storage. Call :meth:`as_list` for a same-device materialised copy, or :meth:`to_list` to
    materialise directly on another device; both return independent tensors that are safe to mutate in place, the way
    the unpacked batch already is.

    Args:
        values: One flat tensor per field, holding that field concatenated across the batch.
        shapes: Per-field list of the original per-sample shapes.
        batch: Number of samples packed.
    )Ú_batchÚ_shapesÚ_valuesÚvaluesúdict[str, Tensor]Úshapesúdict[str, list[list[int]]]rù   r   r   r,   c                ó0   — || _         || _        || _        d S r.   )r  r  r  )r/   r  r  rù   s       r   r0   zPackedTargets.__init__V  s   € ØˆŒØˆŒØˆŒˆˆr   c                ó   — | j         S )z*Return the number of samples in the batch.)r  r;   s    r   Ú__len__zPackedTargets.__len__[  s
   € àŒ{Ðr   r"   c                ó   — d S r.   rG   ©r/   r"   s     r   Ú__getitem__zPackedTargets.__getitem___  s   € Ø<?¸Cr   Úsliceúlist[dict[str, Tensor]]c                ó   — d S r.   rG   r  s     r   r  zPackedTargets.__getitem__b  s   € ØDGÀCr   úint | sliceú+dict[str, Tensor] | list[dict[str, Tensor]]c                ó,  ‡ — t          |t          ¦  «        r-ˆ fd„t          |                     ‰ j        ¦  «        Ž D ¦   «         S |dk     r
|‰ j        z   n|}d|cxk    r‰ j        k     sn t          d|› d‰ j        › �¦  «        ‚i }‰ j                             ¦   «         D ]t\  }}d}‰ j        |         d|…         D ]}|t          |¦  «        z  }Œ‰ j        |         |         }|||t          |¦  «        z   …          
                    |¦  «        ||<   Œu|S )a]  Rebuild the target dict of one sample, or a list of them for a slice.

        Args:
            index: Sample position, negative or slice as for any sequence.

        Returns:
            The sample's field-to-tensor mapping, each tensor a view into the packed storage.

        Raises:
            IndexError: If *index* is out of range.
        c                ó    •— g | ]
}‰|         ‘ŒS rG   rG   ©rR   Úpositionr/   s     €r   rT   z-PackedTargets.__getitem__.<locals>.<listcomp>r  s   ø€ ÐVÐVÐV x�D˜”NÐVÐVÐVr   r   zindex z  is out of range for a batch of N)ra   r  r|   Úindicesr  Ú
IndexErrorr  Úitemsr  Ú_numelÚreshape)r/   r"   r"  ró   rö   r®   ÚoffsetrQ   s   `       r   r  zPackedTargets.__getitem__e  sB  ø€ õ �e�UÑ#Ô#ð 	WØVÐVÐVÐVµ5¸%¿-º-ÈÌÑ:TÔ:TÐ3UÐVÑVÔVÐVØ*/°!ª)¨)�5˜4œ;Ñ&Ð&¸ˆØ�HÐ*Ð*Ò*Ð*˜tœ{Ò*Ð*Ð*Ð*ÝÐZ eÐZÐZÈTÌ[ÐZÐZÑ[Ô[Ð[Ø$&ˆØœ×+Ò+Ñ-Ô-ð 	Oð 	O‰IˆC�ØˆFØœ cÔ*¨9¨H¨9Ô5ð (ð (�Ø�& ™-œ-Ñ'��Ø”L Ô% hÔ/ˆEØ˜v¨µ¸±´Ñ(>Ð>Ô?×GÒGÈÑNÔNˆF�3‰KˆKØˆr   úIterator[dict[str, Tensor]]c              #  óL   K  — t          | j        ¦  «        D ]}| |         V — ŒdS )z4Iterate the batch one rebuilt target dict at a time.N©r|   r  )r/   r"  s     r   Ú__iter__zPackedTargets.__iter__  s<   è è € å˜dœkÑ*Ô*ð 	!ð 	!ˆHØ�x”.Ð Ð Ð Ð ð	!ð 	!r   c                óz   — t          d„ | j                             ¦   «         D ¦   «         | j        | j        ¦  «        S )a§  Pin every packed field, keeping the batch packed.

        Called by PyTorch's pin-memory worker. Pinning the concatenated fields costs one pinned allocation
        per field rather than one per field per sample, and leaves the batch packed until
        ``transfer_batch_to_device`` materialises its independent destination tensors.

        Returns:
            A packed batch whose fields are in pinned memory.
        c                ó>   — i | ]\  }}||                      ¦   «         “ŒS rG   )r:   )rR   rö   r®   s      r   ú
<dictcomp>z,PackedTargets.pin_memory.<locals>.<dictcomp>Ž  s(   € Ð[Ð[Ð[¹¸¸d˜c 4§?¢?Ñ#4Ô#4Ð[Ð[Ð[r   )r  r  r%  r  r  r;   s    r   r:   zPackedTargets.pin_memory„  s;   € õ Ð[Ð[ÀdÄl×FXÒFXÑFZÔFZÐ[Ñ[Ô[Ð]aÔ]iÐkoÔkvÑwÔwÐwr   úMapping[str, Tensor]c                ó*   — t          | j        ¦  «        S )am  Return the concatenated tensor of each field, read-only.

        These are the objects that actually cross the process boundary, so this is what to inspect when checking
        that a batch ships as a handful of tensors rather than one per field per sample.

        Returns:
            Mapping of field name to that field concatenated across the batch.
        )r	   r  r;   s    r   ÚfieldszPackedTargets.fields�  s   € õ   ¤Ñ-Ô-Ð-r   c                óD   ‡ — ˆ fd„t          ‰ j        ¦  «        D ¦   «         S )a(  Materialise the batch as the plain list of dicts the unpacked path produces.

        Each tensor is cloned. ``__getitem__`` returns views into the shared per-field storage -- cheap for
        read-only access, but on the unpacked path every sample already owns independent storage, so an
        in-place write on one sample's tensor here must not alias into another sample's view of the same
        field, or into the packed storage itself.

        Returns:
            One dict per sample, ordered as packed, holding independent tensors.
        c                óX   •— g | ]&}d „ ‰|                               ¦   «         D ¦   «         ‘Œ'S )c                ó>   — i | ]\  }}||                      ¦   «         “ŒS rG   )Úclone)rR   rö   r   s      r   r/  z4PackedTargets.as_list.<locals>.<listcomp>.<dictcomp>§  s&   € ÐMÐMÐM©
¨¨U��e—k’k‘m”mÐMÐMÐMr   )r%  r!  s     €r   rT   z)PackedTargets.as_list.<locals>.<listcomp>§  s9   ø€ ÐqÐqÐqÐRZÐMÐM°d¸8´n×6JÒ6JÑ6LÔ6LÐMÑMÔMÐqÐqÐqr   r+  r;   s   `r   Úas_listzPackedTargets.as_listœ  s-   ø€ ð rÐqÐqÐqÕ^cÐdhÔdoÑ^pÔ^pÐqÑqÔqÐqr   Fr1   útorch.device | strÚnon_blockingr+   c                óh  — d„ t          | j        ¦  «        D ¦   «         }| j                             ¦   «         D ]y\  }}d}t	          || j        |         d¬¦  «        D ]T\  }}t          |¦  «        }	||||	z   …                              |¦  «        }
|
                     ||d¬¦  «        ||<   ||	z  }ŒUŒz|S )aö  Materialise independent per-sample tensors directly on *device*.

        Moving the packed object and then calling :meth:`as_list` makes the concatenated destination fields coexist
        with all of their per-sample clones. That transient duplicate is negligible for boxes and labels but can be
        large for segmentation masks. Transferring each packed view directly into its final independently-owned tensor
        avoids the full-field destination copy while preserving the unpacked path's mutation semantics.

        Args:
            device: Destination device.
            non_blocking: Whether to request asynchronous copies.

        Returns:
            One dict per sample, ordered as packed, with independent tensors on *device*.
        c                ó   — g | ]}i ‘ŒS rG   rG   )rR   Ú_s     r   rT   z)PackedTargets.to_list.<locals>.<listcomp>¸  s   € Ð0PÐ0PÐ0P¸°Ð0PÐ0PÐ0Pr   r   T)Ústrict)r1   r9  Úcopy)	r|   r  r  r%  rf   r  r&  r'  r6   )r/   r1   r9  Úmaterialisedrö   r®   r(  ró   rQ   Úelementsr   s              r   Úto_listzPackedTargets.to_list©  sÞ   € ð 1QÐ0P½UÀ4Ä;Ñ=OÔ=OÐ0PÑ0PÔ0PˆØœ×+Ò+Ñ-Ô-ð 
	#ð 
	#‰IˆC�ØˆFÝ!$ \°4´<ÀÔ3DÈTÐ!RÑ!RÔ!Rð #ð #‘�˜Ý! %™=œ=�Ø˜V f¨xÑ&7Ð7Ô8×@Ò@ÀÑGÔG�Ø#ŸhšhØ!Ø!-Øð 'ñ ô ��s‘ð
 ˜(Ñ"��ð#ð Ðr   c                óò   ‡‡‡— ˆˆfd„| j                              ¦   «         D ¦   «         Št          ˆfd„| j                              ¦   «         D ¦   «         ¦  «        r| S t          ‰| j        | j        ¦  «        S )a  Move every packed field to *device* in one transfer per field.

        Args:
            device: Destination device.
            non_blocking: Whether to request an asynchronous copy.

        Returns:
            A packed batch on *device*; ``self`` if it is already there.
        c                óF   •— i | ]\  }}||                      ‰‰¬ ¦  «        “ŒS ))r9  )r6   )rR   rö   r®   r1   r9  s      €€r   r/  z$PackedTargets.to.<locals>.<dictcomp>Ð  s/   ø€ ÐgÐgÐgÁYÀSÈ$��d—g’g˜f°<�gÑ@Ô@ÐgÐgÐgr   c              3  ó2   •K  — | ]\  }}‰|         |u V — Œd S r.   rG   )rR   rö   r®   Úmoveds      €r   rX   z#PackedTargets.to.<locals>.<genexpr>Ñ  s0   øè è € ÐHÐH¡i c¨4ˆu�SŒz˜TÐ!ÐHÐHÐHÐHÐHÐHr   )r  r%  r`   r  r  r  )r/   r1   r9  rE  s    ``@r   r6   zPackedTargets.toÆ  s‚   øøø€ ð hÐgÐgÐgÐgÐRVÔR^×RdÒRdÑRfÔRfÐgÑgÔgˆÝÐHÐHÐHÐH°4´<×3EÒ3EÑ3GÔ3GÐHÑHÔHÑHÔHð 	ØˆKÝ˜U D¤L°$´+Ñ>Ô>Ð>r   N)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   r0  )r   r  rB   )r1   r8  r9  r+   r   r  )r1   r8  r9  r+   r   r  )rC   rD   rE   rF   Ú	__slots__r0   r  r   r  r,  r:   Úpropertyr2  r7  rA  r6   rG   r   r   r  r  :  s%  € € € € € ðð ð2 1€Iðð ð ð ð
ð ð ð ð Ø?Ð?Ð?ñ „XØ?àØGÐGÐGñ „XØGðð ð ð ð4!ð !ð !ð !ð

xð 
xð 
xð 
xð ð	.ð 	.ð 	.ñ „Xð	.ðrð rð rð rðð ð ð ð ð:?ð ?ð ?ð ?ð ?ð ?ð ?r   r  rQ   c                ó   — d}| D ]}||z  }Œ|S )zÕReturn the number of elements a tensor of *shape* holds.

    Args:
        shape: Tensor shape as a list of dimension sizes.

    Returns:
        The product of the dimensions; 1 for a scalar's empty shape.
    r   rG   )rQ   rõ   r}   s      r   r&  r&  Ö  s(   € ð €EØð ð ˆØ�‰ˆˆØ€Lr   úSequence[dict[str, Tensor]]ú-PackedTargets | tuple[dict[str, Tensor], ...]c                ó  ‡‡‡	— | st          | ¦  «        S t          t          | d         ¦  «        ¦  «        }i }i }| D ]•}t          t          |¦  «        ¦  «        |k    rt          | ¦  «        c S |                     ¦   «         D ]M}t          |t          ¦  «        r#|j        t          j        k    s|j        s|j	        rt          | ¦  «        c c S ŒNŒ–|D ]VŠ	| d         ‰	         j
        Š| d         ‰	         j        Št          ˆˆˆ	fd„| D ¦   «         ¦  «        rt          | ¦  «        c S ŒW|D ]@Š	ˆ	fd„| D ¦   «         }t          j        d„ |D ¦   «         ¦  «        |‰	<   d„ |D ¦   «         |‰	<   ŒAt          ||t          | ¦  «        ¦  «        S )a  Concatenate a batch of target dicts into one tensor per field.

    Args:
        targets: One dict per sample, all sharing the same keys.

    Returns:
        The packed batch, or *targets* unchanged as a tuple when it cannot be packed losslessly -- an empty batch,
        samples whose key sets differ, gradient-tracked values, inconsistent field devices, or a value that is not a
        dense (strided), non-quantized tensor. Falling back keeps a custom dataset working at the original throughput
        rather than failing on it.
    r   c              3  ó\   •K  — | ]&}|‰         j         ‰k    p|‰         j        ‰k    V — Œ'd S r.   rY   )rR   ró   r1   rZ   rö   s     €€€r   rX   zpack_targets.<locals>.<genexpr>  s@   øè è € Ð`Ð`Èfˆv�cŒ{Ô  EÒ)ÐI¨V°C¬[Ô-?À6Ò-IÐ`Ð`Ð`Ð`Ð`Ð`r   c                ó    •— g | ]
}|‰         ‘ŒS rG   rG   )rR   ró   rö   s     €r   rT   z pack_targets.<locals>.<listcomp>  s   ø€ Ð4Ð4Ð4 &�&˜”+Ð4Ð4Ð4r   c                ó8   — g | ]}|                      d ¦  «        ‘ŒS )r¥   )r'  r   s     r   rT   z pack_targets.<locals>.<listcomp>  s$   € Ð GÐ GÐ G°u §¢¨rÑ!2Ô!2Ð GÐ GÐ Gr   c                ó0   — g | ]}d „ |j         D ¦   «         ‘ŒS )c                ó,   — g | ]}t          |¦  «        ‘ŒS rG   )r   )rR   r}   s     r   rT   z+pack_targets.<locals>.<listcomp>.<listcomp>  s   € Ð8Ð8Ð8 S�˜C™œÐ8Ð8Ð8r   )rQ   r   s     r   rT   z pack_targets.<locals>.<listcomp>  s)   € ÐMÐMÐM¸UÐ8Ð8¨E¬KÐ8Ñ8Ô8ÐMÐMÐMr   )r‚   Úsortedr  ra   r   Úlayoutrc   ÚstridedÚis_quantizedÚrequires_gradrZ   r1   Úanyrï   r  r_   )
râ   Úkeysr  r  ró   r   Úcolumnr1   rZ   rö   s
          @@@r   r  r  å  sß  øøø€ ð ð Ý�W‰~Œ~ÐÝ•˜ œ
Ñ#Ô#Ñ$Ô$€DØ "€FØ)+€FØð &ð &ˆÝ•˜‘”Ñ Ô  DÒ(Ð(Ý˜‘>”>Ð!Ð!Ð!Ø—]’]‘_”_ð 	&ð 	&ˆEõ ˜u¥fÑ-Ô-ð&à”<¥5¤=Ò0Ð0ØÔ%ð 1àÔ&ð 1õ ˜W‘~”~Ð%Ð%Ð%Ð%Ð%ð	 1ð	&ð ð "ð "ˆð ˜”
˜3”Ô%ˆØ˜”˜C”Ô'ˆÝÐ`Ð`Ð`Ð`Ð`Ð`ÐX_Ð`Ñ`Ô`Ñ`Ô`ð 	"Ý˜‘>”>Ð!Ð!Ð!ð	"àð Nð NˆØ4Ð4Ð4Ð4¨GÐ4Ñ4Ô4ˆÝ”iÐ GÐ GÀÐ GÑ GÔ GÑHÔHˆˆs‰ØMÐMÀfÐMÑMÔMˆˆs‰ˆÝ˜ ­¨W©¬Ñ6Ô6Ð6r   )r   r   r   r   r   r   )r   r   r   r   r.   )rH   rI   rJ   rK   r   r&   )rH   rr   rJ   rK   r   r&   )rd   F)
r�   r   r‘   r   r’   r?   r“   r+   r   r   )râ   rã   rä   r   r   rå   )NFN)
rù   rú   rJ   rK   rû   r+   rü   rK   r   rý   )rù   rú   r   rý   )rJ   rK   rû   r+   rü   rK   r   r  )rQ   r   r   r   )râ   rI  r   rJ  )'rF   Ú
__future__r   Úcollections.abcr   r   r   r   Ú	functoolsr   Útypesr	   Útypingr
   r   r   rc   r\   r   Úrfdetr.utilities.loggerr   ré   r   r$   r&   rq   ÚjitÚunusedr^   rÑ   rÛ   rë   rø   r  r  r
  r  r&  r  rG   r   r   ú<module>ra     sª  ðð ?Ð >à "Ð "Ð "Ð "Ð "Ð "à AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AØ Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à €€€Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€ð;ð ;ð ;ð ;ð(ð ð ð ð 7!ð 7!ð 7!ð 7!ð 7!ñ 7!ô 7!ð 7!ðx "ðA2ð A2ð A2ð A2ð A2ðL „Ôð "ð1+ð 1+ð 1+ð 1+ñ Ôð1+ðn  Øð	]Qð ]Qð ]Qð ]Qð ]QðF  Øð	USð USð USð USð USðt WÐ ð3ð 3ð 3ð 3ðp "ØØ!%ð	!ð !ð !ð !ð !ðH<ð <ð <ð <ð" "ØØ!%ðnð nð nð nð nð6Y?ð Y?ð Y?ð Y?ð Y?ñ Y?ô Y?ð Y?ðxð ð ð ð,7ð ,7ð ,7ð ,7ð ,7ð ,7r   