Ë
    ÛÓ^j(Ô  ã                   ó¦  — d dl Zd dlZd dl mZ d dlmZ d dlZd dlZd dl	m
c mZ d dl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mZ dd
lmZmZ ddlmZ ddlmZm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'  e«       rd dl(m)Z)  e!«       rd dl*m+Z+ d dl,m-Z-  e d¬«      e G d„ de«      «       «       Z.	 dLdej                  dej                  dej                  fd„Z/dededefd„Z0dej                  dej                  dej                  fd„Z1 G d „ d!e
jd                  «      Z3deded"e4defd#„Z5dej                  dej                  d"e4dej                  fd$„Z6 G d%„ d&e
jd                  «      Z7 G d'„ d(e
jd                  «      Z8 G d)„ d*e
jd                  «      Z9	 dMd+e
jd                  d,ej                  d-ej                  d.ej                  d/ej                  dz  d0e:d1e:fd2„Z; G d3„ d4e
jd                  «      Z< G d5„ d6e
jd                  «      Z= G d7„ d8e
jd                  «      Z> G d9„ d:e
jd                  «      Z? G d;„ d<e
jd                  «      Z@ G d=„ d>e«      ZA G d?„ d@e
j„                  «      ZC G dA„ dBe
jd                  «      ZD G dC„ dDe
jd                  «      ZE G dE„ dFe
jd                  «      ZFe  G dG„ dHe«      «       ZG e dI¬«       G dJ„ dKeG«      «       ZHdHdKgZIy)Né    N)ÚCallable)Ú	dataclass)ÚTensorÚnné   )Úinitialization)ÚACT2FN)ÚModelOutputÚis_scipy_availableÚrequires_backends)ÚGradientCheckpointingLayer)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚis_accelerate_available)Úmerge_with_config_defaults)Úcapture_outputsé   )Ú
EomtConfig)Úlinear_sum_assignment)ÚPartialState)Úreducea˜  
    Class for outputs of [`EomtForUniversalSegmentationOutput`].

    This output can be directly passed to [`~EomtImageProcessor.post_process_semantic_segmentation`] or
    [`~EomtImageProcessor.post_process_instance_segmentation`] or
    [`~EomtImageProcessor.post_process_panoptic_segmentation`] to compute final segmentation maps. Please, see
    [`~EomtImageProcessor] for details regarding usage.
    )Úcustom_introc                   ó<  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
ej                  dz  ed<   dZeej                     dz  ed<   dZeej                     dz  ed<   dZeej                      dz  ed	<   y)
Ú"EomtForUniversalSegmentationOutputa*  
    loss (`torch.Tensor`, *optional*):
        The computed loss, returned when labels are present.
    class_queries_logits (`torch.FloatTensor`):
        A tensor of shape `(batch_size, num_queries, num_labels + 1)` representing the proposed classes for each
        query. Note the `+ 1` is needed because we incorporate the null class.
    masks_queries_logits (`torch.FloatTensor`):
        A tensor of shape `(batch_size, num_queries, height, width)` representing the proposed masks for each
        query.
    last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
        Last hidden states (final feature map) of the last layer.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
        shape `(batch_size, sequence_length, hidden_size)`. Hidden-states all layers of the model.
    attentions (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `tuple(torch.FloatTensor)` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`. Self and Cross Attentions weights from transformer decoder.
    patch_offsets (`list[torch.Tensor]`, *optional*):
        list of tuples indicating the image index and start and end positions of patches for semantic segmentation.
    NÚlossÚclass_queries_logitsÚmasks_queries_logitsÚlast_hidden_stateÚhidden_statesÚ
attentionsÚpatch_offsets)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtorchÚFloatTensorÚ__annotations__r   r    r!   r"   Útupler#   r$   Úlistr   © ó    úq/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/eomt/modeling_eomt.pyr   r   3   s¬   … ñð* &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø59Ð˜%×+Ñ+¨dÑ2Ó9Ø59Ð˜%×+Ñ+¨dÑ2Ó9Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ó6Ø/3€M�4˜Ÿ™Ñ%¨Ñ,Ô3r/   r   Úinput_featuresÚpoint_coordinatesÚreturnc                 óÚ   — |j                  «       dk(  rd}|j                  d«      }t        j                  j                  j
                  | d|z  dz
  fi |¤Ž}|r|j                  d«      }|S )a(  
    A wrapper around `torch.nn.functional.grid_sample` to support 3D point_coordinates tensors.

    Args:
        input_features (`torch.Tensor` of shape (batch_size, channels, height, width)):
            A tensor that contains features map on a height * width grid
        point_coordinates (`torch.Tensor` of shape (batch_size, num_points, 2) or (batch_size, grid_height, grid_width,:
        2)):
            A tensor that contains [0, 1] * [0, 1] normalized point coordinates
        add_dim (`bool`):
            boolean value to keep track of added dimension

    Returns:
        point_features (`torch.Tensor` of shape (batch_size, channels, num_points) or (batch_size, channels,
        height_grid, width_grid):
            A tensor that contains features for points in `point_coordinates`.
    r   Té   g       @ç      ð?)ÚdimÚ	unsqueezer)   r   Ú
functionalÚgrid_sampleÚsqueeze)r1   r2   Úadd_dimÚkwargsÚpoint_featuress        r0   Úsample_pointr?   ^   st   € ð( ×ÑÓ !Ò#ØˆØ-×7Ñ7¸Ó:Ðô —X‘X×(Ñ(×4Ñ4°^ÀSÐK\ÑE\Ð_bÑEbÑmÐflÑm€NÙØ'×/Ñ/°Ó2ˆàÐr/   ÚinputsÚlabelsc                 ó  — | j                  «       j                  d«      } dt        j                  | |j                  «      z  }| j                  d«      dd…df   |j                  d«      ddd…f   z   }d|dz   |dz   z  z
  }|S )aÈ  
    A pair wise version of the dice loss, see `dice_loss` for usage.

    Args:
        inputs (`torch.Tensor`):
            A tensor representing a mask
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).

    Returns:
        `torch.Tensor`: The computed loss between each pairs.
    r   r5   éÿÿÿÿN)ÚsigmoidÚflattenr)   ÚmatmulÚTÚsum)r@   rA   Ú	numeratorÚdenominatorr   s        r0   Úpair_wise_dice_lossrK   ~   s|   € ð �^‰^Ó×%Ñ% aÓ(€FØ”E—L‘L ¨¯©Ó2Ñ2€Ià—*‘*˜R“.¢ D Ñ)¨F¯J©J°r«N¸4Â¸7Ñ,CÑC€KØ�	˜A‘ +°¡/Ñ2Ñ2€DØ€Kr/   c                 ó\  — | j                   d   }t        j                  d¬«      } || t        j                  | «      «      } || t        j
                  | «      «      }t        j                  ||z  |j                  «      }t        j                  ||z  d|z
  j                  «      }||z   }|S )aê  
    A pair wise version of the cross entropy loss, see `sigmoid_cross_entropy_loss` for usage.

    Args:
        inputs (`torch.Tensor`):
            A tensor representing a mask.
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).

    Returns:
        loss (`torch.Tensor`): The computed loss between each pairs.
    r   Únone©Ú	reduction)Úshaper   ÚBCEWithLogitsLossr)   Ú	ones_likeÚ
zeros_likerF   rG   )	r@   rA   Úheight_and_widthÚ	criterionÚcross_entropy_loss_posÚcross_entropy_loss_negÚloss_posÚloss_negr   s	            r0   Ú$pair_wise_sigmoid_cross_entropy_lossrZ   ”   s™   € ð —|‘| A‘Ðä×$Ñ$¨vÔ6€IÙ& v¬u¯©¸vÓ/FÓGÐÙ& v¬u×/?Ñ/?ÀÓ/GÓHÐä�|‰|Ð2Ð5EÑEÀvÇxÁxÓP€HÜ�|‰|Ð2Ð5EÑEÈÈFÉ
Ç~Á~ÓV€HØ�hÑ€DØ€Kr/   c                   óØ   ‡ — e Zd ZdZ	 ddedededefˆ fd„Z ej                  «       dej                  dej                  d	ej                  d
ej                  de
ee	      f
d„«       Zˆ xZS )ÚEomtHungarianMatcheraq  This class computes an assignment between the labels and the predictions of the network.

    For efficiency reasons, the labels don't include the no_object. Because of this, in general, there are more
    predictions than labels. In this case, we do a 1-to-1 matching of the best predictions, while the others are
    un-matched (and thus treated as non-objects).
    Ú
cost_classÚ	cost_maskÚ	cost_diceÚ
num_pointsc                 óŽ   •— t         ‰| �  «        |dk(  r|dk(  r|dk(  rt        d«      ‚|| _        || _        || _        || _        y)aH  Creates the matcher

        Params:
            cost_class (`float`, *optional*, defaults to 1.0):
                Relative weight of the classification error in the matching cost.
            cost_mask (`float`, *optional*,  defaults to 1.0):
                This is the relative weight of the focal loss of the binary mask in the matching cost.
            cost_dice (`float`, *optional*, defaults to 1.0):
                This is the relative weight of the dice loss of the binary mask in the matching cost.
            num_points (`int`, *optional*, defaults to 12544):
                No. of points to sample on which the mask loss will be calculated. The same set of K points are
                uniformly sampled for all prediction and ground truth masks to construct the cost matrix for bipartite
                matching.
        r   zAll costs can't be 0N)ÚsuperÚ__init__Ú
ValueErrorr`   r]   r^   r_   )Úselfr]   r^   r_   r`   Ú	__class__s        €r0   rc   zEomtHungarianMatcher.__init__¸   sK   ø€ ô" 	‰ÑÔØ˜Š?˜y¨Aš~°)¸q².ÜÐ3Ó4Ð4à$ˆŒØ$ˆŒØ"ˆŒØ"ˆ�r/   r    r   Úmask_labelsÚclass_labelsr3   c           	      ó¨  — g }|j                   d   }t        |«      D �]Ï  }||   j                  d«      }||   }	|dd…||   f    }
||   j                  |	«      }|dd…df   }|	dd…df   }	t	        j
                  d| j                  d|	j                  ¬«      }|j                  |j                   d   dd«      }t        ||d¬«      j                  d«      }|j                  |	j                   d   dd«      }t        |	|d¬«      j                  d«      }	t        |	|«      }t        |	|«      }| j                  |z  | j                  |
z  z   | j                  |z  z   }t	        j                   |t	        j"                  d	«      «      }t	        j$                  |t	        j"                  d
«      «      }t	        j&                  |d«      }t)        |j+                  «       «      }|j-                  |«       �ŒÒ |D ��cg c]O  \  }}t	        j.                  |t        j0                  ¬«      t	        j.                  |t        j0                  ¬«      f‘ŒQ }}}|S c c}}w )ao  
        Params:
            masks_queries_logits (`torch.Tensor`):
                A tensor of dim `batch_size, num_queries, num_labels` with the classification logits.
            class_queries_logits (`torch.Tensor`):
                A tensor of dim `batch_size, num_queries, height, width` with the predicted masks.
            class_labels (`torch.Tensor`):
                A tensor of dim `num_target_boxes` (where num_target_boxes is the number of ground-truth objects in the
                target) containing the class labels.
            mask_labels (`torch.Tensor`):
                A tensor of dim `num_target_boxes, height, width` containing the target masks.

        Returns:
            matched_indices (`list[tuple[Tensor]]`): A list of size batch_size, containing tuples of (index_i, index_j)
            where:
                - index_i is the indices of the selected predictions (in order)
                - index_j is the indices of the corresponding selected labels (in order)
            For each batch element, it holds:
                len(index_i) = len(index_j) = min(num_queries, num_target_boxes).
        r   rC   Nr   r5   ©ÚdeviceF©Úalign_cornersg    _ Bg    _ Â©Údtype)rP   ÚrangeÚsoftmaxÚtor)   Úrandr`   rk   Úrepeatr?   r;   rZ   rK   r^   r]   r_   ÚminimumÚtensorÚmaximumÚ
nan_to_numr   ÚcpuÚappendÚ	as_tensorÚint64)re   r    r   rg   rh   ÚindicesÚ
batch_sizeÚiÚ
pred_probsÚ	pred_maskr]   Útarget_maskr2   Útarget_coordinatesÚpred_coordinatesr^   r_   Úcost_matrixÚassigned_indicesÚjÚmatched_indicess                        r0   ÚforwardzEomtHungarianMatcher.forwardÒ   s/  € ð8 *,ˆð *×/Ñ/°Ñ2ˆ
Ü�zÓ"ó 	-ˆAØ-¨aÑ0×8Ñ8¸Ó<ˆJØ,¨QÑ/ˆIð %¢Q¨°Q©Ð%7Ñ8Ð8ˆJØ% a™.×+Ñ+¨IÓ6ˆKØ%¢a¨ gÑ.ˆKØ!¢! T 'Ñ*ˆIô !&§
¡
¨1¨d¯o©o¸qÈ×IYÑIYÔ ZÐà!2×!9Ñ!9¸+×:KÑ:KÈAÑ:NÐPQÐSTÓ!UÐÜ& {Ð4FÐV[Ô\×dÑdÐefÓgˆKà0×7Ñ7¸	¿¹ÈÑ8JÈAÈqÓQÐÜ$ YÐ0@ÐPUÔV×^Ñ^Ð_`ÓaˆIô =¸YÈÓTˆIä+¨I°{ÓCˆIàŸ.™.¨9Ñ4°t·±ÈÑ7SÑSÐVZ×VdÑVdÐgpÑVpÑpˆKäŸ-™-¨´U·\±\À$Ó5GÓHˆKÜŸ-™-¨´U·\±\À%Ó5HÓIˆKÜ×*Ñ*¨;¸Ó:ˆKä0EÀkÇoÁoÓFWÓ0XÐØ�N‰NÐ+Ö,ð?	-ðF ho÷
Ù_cÐ_`ÐbcŒU�_‰_˜Q¤e§k¡kÔ2´E·O±OÀAÌUÏ[É[Ô4YÒZð
ˆñ 
ð Ðùó
s   Ç5AI)r6   r6   r6   i 1  )r%   r&   r'   r(   ÚfloatÚintrc   r)   Úno_gradr   r-   r,   r‰   Ú__classcell__©rf   s   @r0   r\   r\   °   s    ø„ ñð joñ#Øð#Ø27ð#ØJOð#Øcfõ#ð4 €U‡]�]ƒ_ðDà#Ÿl™lðDð $Ÿl™lðDð —\‘\ð	Dð
 —l‘lðDð 
ˆe�F‰mÑ	òDó ôDr/   r\   Ú	num_masksc                 óú   — | j                  «       j                  d«      }d||z  j                  d«      z  }|j                  d«      |j                  d«      z   }d|dz   |dz   z  z
  }|j                  «       |z  }|S )a4  
    Compute the DICE loss, similar to generalized IOU for masks as follows:

    $$ \mathcal{L}_{\text{dice}(x, y) = 1 - \frac{2 * x \cap y }{x \cup y + 1}} $$

    In practice, since `labels` is a binary mask, (only 0s and 1s), dice can be computed as follow

    $$ \mathcal{L}_{\text{dice}(x, y) = 1 - \frac{2 * x * y }{x + y + 1}} $$

    Args:
        inputs (`torch.Tensor`):
            A tensor representing a mask.
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).
        num_masks (`int`):
            The number of masks present in the current batch, used for normalization.

    Returns:
        `torch.Tensor`: The computed loss.
    r   r5   rC   )rD   rE   rH   )r@   rA   r�   ÚprobsrI   rJ   r   s          r0   Ú	dice_lossr’     sz   € ð, �N‰NÓ×$Ñ$ QÓ'€EØ�U˜V‘^×(Ñ(¨Ó,Ñ,€IØ—)‘)˜B“- &§*¡*¨R£.Ñ0€KØ�	˜A‘ +°¡/Ñ2Ñ2€DØ�8‰8‹:˜	Ñ!€DØ€Kr/   c                 óˆ   — t        j                  d¬«      } || |«      }|j                  d«      j                  «       |z  }|S )a|  
    Args:
        inputs (`torch.Tensor`):
            A float tensor of arbitrary shape.
        labels (`torch.Tensor`):
            A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
            (0 for the negative class and 1 for the positive class).

    Returns:
        loss (`torch.Tensor`): The computed loss.
    rM   rN   r   )r   rQ   ÚmeanrH   )r@   rA   r�   rU   Úcross_entropy_lossr   s         r0   Úsigmoid_cross_entropy_lossr–   8  sD   € ô ×$Ñ$¨vÔ6€IÙ" 6¨6Ó2Ðà×"Ñ" 1Ó%×)Ñ)Ó+¨iÑ7€DØ€Kr/   c                   óÒ  ‡ — e Zd Zdedeeef   fˆ fd„Zdeee	      dee	   fd„Z
dee   deeef   fd„Zd	ed
ee   deej                     deeef   fd„Zdej                  deej                     deej                     de	deeej                  f   f
d„Zd„ Zd„ Zdej                  dej                  fd„Zdej                  de	de	dedej                  f
d„Z	 ddej                  d	ej                  deej                     d
eej                     deeej                  f   dz  deeej                  f   fd„Zd
ej                  dej0                  dej                  fd„Zˆ xZS )ÚEomtLossÚconfigÚweight_dictc                 óò  •— t         ‰| �  «        t        | dg«       |j                  | _        || _        |j
                  | _        t        j                  | j                  dz   «      }| j                  |d<   | j                  d|«       |j                  | _        |j                  | _        |j                  | _        t        |j                  |j                   |j"                  | j                  ¬«      | _        y)aH  
        The Eomt Loss. The loss is computed very similar to DETR. The process happens in two steps: 1) we
        compute hungarian assignment between ground truth masks and the outputs of the model 2) we supervise each pair
        of matched ground-truth / prediction (supervise class and mask)

        Args:
            config (`EomtConfig`):
                The configuration for Eomt model also containing loss calculation specific parameters.
            weight_dict (`dict[str, float]`):
                A dictionary of weights to be applied to the different losses.
        Úscipyr   rC   Úempty_weight)r]   r_   r^   r`   N)rb   rc   r   Ú
num_labelsrš   Úno_object_weightÚeos_coefr)   ÚonesÚregister_bufferÚtrain_num_pointsr`   Úoversample_ratioÚimportance_sample_ratior\   Úclass_weightÚdice_weightÚmask_weightÚmatcher)re   r™   rš   r�   rf   s       €r0   rc   zEomtLoss.__init__M  sÎ   ø€ ô 	‰ÑÔÜ˜$  	Ô*Ø ×+Ñ+ˆŒØ&ˆÔð ×/Ñ/ˆŒÜ—z‘z $§/¡/°AÑ"5Ó6ˆØŸ=™=ˆ�RÑØ×Ñ˜^¨\Ô:ð !×1Ñ1ˆŒØ &× 7Ñ 7ˆÔØ'-×'EÑ'EˆÔ$ä+Ø×*Ñ*Ø×(Ñ(Ø×(Ñ(Ø—‘ô	
ˆ�r/   Úsizesr3   c                 ón   — |d   }|dd  D ]'  }t        |«      D ]  \  }}t        ||   |«      ||<   Œ Œ) |S )Nr   r   )Ú	enumerateÚmax)re   rª   ÚmaxesÚsublistÚindexÚitems         r0   Ú_max_by_axiszEomtLoss._max_by_axisp  sS   € Ø�a‘ˆØ˜Q˜R�yò 	7ˆGÜ(¨Ó1ò 7‘��tÜ" 5¨¡<°Ó6��e’ñ7ð	7ð ˆr/   Útensorsc                 ó`  — | j                  |D �cg c]  }t        |j                  «      ‘Œ c}«      }t        |«      g|z   }|\  }}}}|d   j                  }	|d   j
                  }
t        j                  ||	|
¬«      }t        j                  |||ft        j                  |
¬«      }t        |||«      D ]o  \  }}}|d |j                  d   …d |j                  d   …d |j                  d   …f   j                  |«       d|d |j                  d   …d |j                  d   …f<   Œq ||fS c c}w )Nr   ©ro   rk   r   r5   F)r²   r-   rP   Úlenro   rk   r)   Úzerosr¡   ÚboolÚzipÚcopy_)re   r³   rv   Úmax_sizeÚbatch_shaper~   Ú_ÚheightÚwidthro   rk   Úpadded_tensorsÚpadding_masksÚpadded_tensorÚpadding_masks                  r0   Ú_pad_images_to_max_in_batchz$EomtLoss._pad_images_to_max_in_batchx  s7  € à×$Ñ$ÀwÖ%O¸V¤d¨6¯<©<Õ&8Ò%OÓPˆä˜7“|�n xÑ/ˆØ'2Ñ$ˆ
�A�v˜uØ˜‘
× Ñ ˆØ˜‘×"Ñ"ˆÜŸ™ [¸ÀfÔMˆÜŸ
™
 J°¸Ð#>ÄeÇjÁjÐY_Ô`ˆä36°wÀÐP]Ó3^ò 	GÑ/ˆF�M <ØÐ+˜FŸL™L¨™OÐ+Ð->¨v¯|©|¸A©Ð->Ð@QÀ&Ç,Á,ÈqÁ/Ð@QÐQÑR×XÑXÐY_Ô`ØAFˆLÐ*˜6Ÿ<™<¨™?Ð*Ð,=¨f¯l©l¸1©oÐ,=Ð=Ò>ð	Gð ˜}Ð,Ð,ùò &Ps   �D+r   rh   r}   c           	      óÐ  — |}|j                   \  }}}t        j                  | j                  ¬«      }| j	                  |«      }	t        j                  t        ||«      D �
��cg c]  \  }
\  }}|
|   ‘Œ c}}}
«      }t        j                  ||f| j                  t
        j                  |j                  ¬«      }|||	<   |j                  dd«      } |||«      }d|i}|S c c}}}
w )a…  Compute the losses related to the labels using cross entropy.

        Args:
            class_queries_logits (`torch.Tensor`):
                A tensor of shape `batch_size, num_queries, num_labels`
            class_labels (`list[torch.Tensor]`):
                List of class labels of shape `(labels)`.
            indices (`tuple[np.array])`:
                The indices computed by the Hungarian matcher.

        Returns:
            `dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key:
            - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels.
        )Úweight)Ú
fill_valuero   rk   r   r5   Úloss_cross_entropy)rP   r   ÚCrossEntropyLossr�   Ú$_get_predictions_permutation_indicesr)   Úcatr¹   Úfullrž   r|   rk   Ú	transpose)re   r   rh   r}   Úpred_logitsr~   Únum_queriesr½   rU   ÚidxÚtargetr‡   Útarget_classes_oÚtarget_classesÚpred_logits_transposedÚloss_ceÚlossess                    r0   Úloss_labelszEomtLoss.loss_labels‰  sâ   € ð" +ˆØ%0×%6Ñ%6Ñ"ˆ
�K Ü×'Ñ'¨t×/@Ñ/@ÔAˆ	Ø×7Ñ7¸Ó@ˆÜ Ÿ9™9Ü-0°¸wÓ-G×HÐH™>˜6¡6 A qˆV�A‹YÔHó
Ðô Ÿ™Ø˜Ð%°$·/±/ÌÏÉÐ]h×]oÑ]oô
ˆð /ˆ�sÑà!,×!6Ñ!6°q¸!Ó!<ÐÙÐ2°NÓCˆØ&¨Ð0ˆØˆùô Is   Á#C!r    rg   r�   c                 ó  ‡ — ‰ j                  |«      }‰ j                  |«      }||   }‰ j                  |«      \  }}	||   }|dd…df   }|dd…df   }t        j                  «       5  ‰ j                  |ˆ fd„‰ j                  ‰ j                  ‰ j                  «      }
t        ||
d¬«      j                  d«      }ddd«       t        |
d¬«      j                  d«      }t        ||«      t        |||«      dœ}~~|S # 1 sw Y   ŒExY w)a¤  Compute the losses related to the masks using sigmoid_cross_entropy_loss and dice loss.

        Args:
            masks_queries_logits (`torch.Tensor`):
                A tensor of shape `(batch_size, num_queries, height, width)`.
            mask_labels (`torch.Tensor`):
                List of mask labels of shape `(labels, height, width)`.
            indices (`tuple[np.array])`:
                The indices computed by the Hungarian matcher.
            num_masks (`int)`:
                The number of masks, used for normalization.

        Returns:
            losses (`dict[str, Tensor]`): A dict of `torch.Tensor` containing two keys:
            - **loss_mask** -- The loss computed using sigmoid cross entropy loss on the predicted and ground truth.
              masks.
            - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth,
              masks.
        Nc                 ó&   •— ‰j                  | «      S ©N)Úcalculate_uncertainty)Úlogitsre   s    €r0   ú<lambda>z%EomtLoss.loss_masks.<locals>.<lambda>Ö  s   ø€ ˜t×9Ñ9¸&ÓA€ r/   Frl   r   )Ú	loss_maskÚ	loss_dice)rÊ   Ú _get_targets_permutation_indicesrÄ   r)   rŒ   Úsample_points_using_uncertaintyr`   r¤   r¥   r?   r;   r–   r’   )re   r    rg   r}   r�   Úsrc_idxÚtgt_idxÚ
pred_masksÚtarget_masksr½   r2   Úpoint_labelsÚpoint_logitsrÖ   s   `             r0   Ú
loss_maskszEomtLoss.loss_masks«  s,  ø€ ð4 ×;Ñ;¸GÓDˆØ×7Ñ7¸Ó@ˆà)¨'Ñ2ˆ
ð ×:Ñ:¸;ÓG‰ˆ�aØ# GÑ,ˆð  ¢ 4 Ñ(ˆ
Ø#¢A t GÑ,ˆô �]‰]‹_ñ 		iØ $× DÑ DØÛAØ—‘Ø×%Ñ%Ø×,Ñ,ó!Ðô (¨Ð6GÐW\Ô]×eÑeÐfgÓhˆL÷		iô $ JÐ0AÐQVÔW×_Ñ_Ð`aÓbˆô 4°LÀ,ÐPYÓZÜ" <°¸yÓIñ
ˆð
 ØØˆ÷)		ið 		iús   Á(AD Ä D	c                 ó  — t        j                  t        |«      D ���cg c]  \  }\  }}t        j                  ||«      ‘Œ  c}}}«      }t        j                  |D ��cg c]  \  }}|‘Œ	 c}}«      }||fS c c}}}w c c}}w rÚ   ©r)   rË   r¬   Ú	full_like)re   r}   r   Úsrcr½   Úbatch_indicesÚpredictions_indicess          r0   rÊ   z-EomtLoss._get_predictions_permutation_indicesé  sj   € äŸ	™	ÌiÐX_ÓN`×"aÐ"a¹{¸qÁ(À3È¤5§?¡?°3¸Õ#:Ô"aÓbˆÜ#Ÿi™i¸W×(E±°#°qªÓ(EÓFÐØÐ1Ð1Ð1ùô #bùÛ(Eó   Ÿ#A7ÁA>
c                 ó  — t        j                  t        |«      D ���cg c]  \  }\  }}t        j                  ||«      ‘Œ  c}}}«      }t        j                  |D ��cg c]  \  }}|‘Œ	 c}}«      }||fS c c}}}w c c}}w rÚ   rê   )re   r}   r   r½   Útgtrí   Útarget_indicess          r0   rà   z)EomtLoss._get_targets_permutation_indicesï  sh   € äŸ	™	ÌiÐX_ÓN`×"aÐ"a¹{¸qÁ(À1Àc¤5§?¡?°3¸Õ#:Ô"aÓbˆÜŸ™¸×#@©H¨Q°¢CÓ#@ÓAˆØ˜nÐ,Ð,ùô #bùÛ#@rï   rÜ   c                 ó2   — t        j                  |«       }|S )a‚  
        In Eomt paper, uncertainty is estimated as L1 distance between 0.0 and the logit prediction in 'logits'
        for the foreground class in `classes`.

        Args:
            logits (`torch.Tensor`):
            A tensor of shape (R, 1, ...) for class-specific or class-agnostic, where R is the total number of predicted masks in all images and C is:
            the number of foreground classes. The values are logits.

        Returns:
            scores (`torch.Tensor`): A tensor of shape (R, 1, ...) that contains uncertainty scores with the most
            uncertain locations having the highest uncertainty score.
        )r)   Úabs)re   rÜ   Úuncertainty_scoress      r0   rÛ   zEomtLoss.calculate_uncertaintyõ  s   € ô  %Ÿy™y¨Ó0Ð1ÐØ!Ð!r/   r`   r¤   r¥   c           	      ó�  — |j                   d   }t        ||z  «      }t        j                  ||d|j                  ¬«      }t        ||d¬«      }	 ||	«      }
t        ||z  «      }||z
  }t        j                  |
dd…ddd…f   |d¬«      d   }|t        j                  |t        j                  |j                  ¬	«      z  }||dd…df   z  }|j                  d
d«      |j                  d
«      dd…f   j                  ||d«      }|dkD  r:t        j                  |t        j                  ||d|j                  ¬«      gd¬«      }|S )a€  
        This function is meant for sampling points in [0, 1] * [0, 1] coordinate space based on their uncertainty. The
        uncertainty is calculated for each point using the passed `uncertainty function` that takes points logit
        prediction as input.

        Args:
            logits (`float`):
                Logit predictions for P points.
            uncertainty_function:
                A function that takes logit predictions for P points and returns their uncertainties.
            num_points (`int`):
                The number of points P to sample.
            oversample_ratio (`int`):
                Oversampling parameter.
            importance_sample_ratio (`float`):
                Ratio of points that are sampled via importance sampling.

        Returns:
            point_coordinates (`torch.Tensor`):
                Coordinates for P sampled points.
        r   r5   rj   Frl   Nr   )Úkr7   rµ   rC   ©r7   )rP   r‹   r)   rs   rk   r?   ÚtopkÚarangeÚlongÚviewrË   )re   rÜ   Úuncertainty_functionr`   r¤   r¥   Ú	num_boxesÚnum_points_sampledr2   rç   Úpoint_uncertaintiesÚnum_uncertain_pointsÚnum_random_pointsrÐ   Úshifts                  r0   rá   z(EomtLoss.sample_points_using_uncertainty  sI  € ð< —L‘L ‘Oˆ	Ü  Ð.>Ñ!>Ó?Ðô "ŸJ™J yÐ2DÀaÐPV×P]ÑP]Ô^Ðä# FÐ,=ÈUÔSˆá2°<Ó@Ðä"Ð#:¸ZÑ#GÓHÐØ&Ð)=Ñ=Ðä�j‰jÐ,ªQ°²1¨WÑ5Ð9MÐSTÔUÐVWÑXˆØ"¤U§\¡\°)Ä5Ç:Á:ÐV\×VcÑVcÔ%dÑdˆØˆu’Q˜�W‰~ÑˆØ-×2Ñ2°2°qÓ9¸#¿(¹(À2»,Ê¸/ÑJ×OÑOÐPYÐ[oÐqrÓsÐà˜qÒ Ü %§	¡	Ø"¤E§J¡J¨yÐ:KÈQÐW]×WdÑWdÔ$eÐfØô!Ðð !Ð r/   NÚauxiliary_predictionsc                 ó¤  — | j                  ||||«      }| j                  ||d   j                  ¬«      }i | j                  ||||«      ¥| j	                  |||«      ¥}|�jt        |«      D ]\  \  }	}
|
d   }|
d   }| j                  ||||«      }|j                  «       D ��ci c]  \  }}|› d|	› �|“Œ }}}|j                  |«       Œ^ |S c c}}w )a²  
        This performs the loss computation.

        Args:
            masks_queries_logits (`torch.Tensor`):
                A tensor of shape `(batch_size, num_queries, height, width)`.
            class_queries_logits (`torch.Tensor`):
                A tensor of shape `(batch_size, num_queries, num_labels)`.
            mask_labels (`torch.Tensor`):
                List of mask labels of shape `(labels, height, width)`.
            class_labels (`list[torch.Tensor]`):
                List of class labels of shape `(labels)`.
            auxiliary_predictions (`dict[str, torch.Tensor]`, *optional*):
                if `use_auxiliary_loss` was set to `true` in [`EomtConfig`], then it contains the logits from
                the inner layers of the EomtMaskedAttentionDecoder.

        Returns:
            losses (`dict[str, Tensor]`): A dict of `torch.Tensor` containing three keys:
            - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels.
            - **loss_mask** -- The loss computed using sigmoid cross_entropy loss on the predicted and ground truth
              masks.
            - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth
              masks.
            if `use_auxiliary_loss` was set to `true` in [`EomtConfig`], the dictionary contains additional
            losses for each auxiliary predictions.
        r   rj   r    r   r½   )	r©   Úget_num_masksrk   rè   r×   r¬   r‰   ÚitemsÚupdate)re   r    r   rg   rh   r  r}   r�   rÖ   rÐ   Úaux_outputsÚ	loss_dictÚkeyÚvalues                 r0   r‰   zEomtLoss.forward=  s
  € ðH —,‘,Ð3Ð5IÈ;ÐXdÓeˆà×&Ñ& |¸LÈ¹O×<RÑ<RÐ&ÓSˆ	ð%
Ø�o‰oÐ2°KÀÈ)ÓTð%
à×ÑÐ3°\À7ÓKð%
ˆð
 !Ð,Ü$-Ð.CÓ$Dò )Ñ ��[Ø'2Ð3IÑ'JÐ$Ø'2Ð3IÑ'JÐ$Ø ŸL™LÐ)=Ð?SÐU`ÐbnÓo�	ØENÇ_Á_ÓEV×W±z°s¸E ˜u A c U˜^¨UÑ2ÐW�	ÑWØ—‘˜iÕ(ð)ð ˆùó Xs   Â"Crk   c                 ó&  — t        d„ |D «       «      }t        j                  |t        j                  |¬«      }d}t	        «       r2t
        j                  i k7  rt        |«      }t        «       j                  }t        j                  ||z  d¬«      }|S )zk
        Computes the average number of target masks across the batch, for normalization purposes.
        c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wrÚ   )r¶   )Ú.0Úclassess     r0   ú	<genexpr>z)EomtLoss.get_num_masks.<locals>.<genexpr>x  s   è ø€ ÒA¨œ˜GŸÑAùs   ‚rµ   r   )Úmin)
rH   r)   r{   rŠ   r   r   Ú_shared_stater   Únum_processesÚclamp)re   rh   rk   r�   Ú
world_sizes        r0   r  zEomtLoss.get_num_maskst  su   € ô ÑA°LÔAÓAˆ	Ü—O‘O I´U·[±[ÈÔPˆ	Øˆ
Ü"Ô$Ü×)Ñ)¨RÒ/Ü" 9Ó-�	Ü)›^×9Ñ9�
ä—K‘K 	¨JÑ 6¸AÔ>ˆ	ØÐr/   rÚ   )r%   r&   r'   r   ÚdictÚstrrŠ   rc   r-   r‹   r²   r   r,   rÄ   ÚnpÚarrayr×   r)   rè   rÊ   rà   rÛ   rá   r‰   rk   r  r�   rŽ   s   @r0   r˜   r˜   L  s  ø„ ð!
˜zð !
¸¸SÀ%¸ZÑ8Hõ !
ðF $ t¨C¡y¡/ð °d¸3±ió ð-°4¸±<ð -ÀEÈ&ÐRXÈ.ÑDYó -ð" Ø$*ð Ø:>¸v¹,ð ØQVÐWY×W_ÑW_ÑQ`ð à	ˆc�6ˆkÑ	ó ðD<à#Ÿl™lð<ð ˜%Ÿ,™,Ñ'ð<ð �r—x‘x‘ð	<ð
 ð<ð 
ˆc�5—<‘<ÐÑ	 ó<ò|2ò-ð"¨E¯L©Lð "¸U¿\¹\ó "ð"5!à—‘ð5!ð ð	5!ð
 ð5!ð "'ð5!ð 
�‰ó5!ðz AEñ5à#Ÿl™lð5ð $Ÿl™lð5ð ˜%Ÿ,™,Ñ'ð	5ð
 ˜5Ÿ<™<Ñ(ð5ð  $ C¨¯©Ð$5Ñ6¸Ñ=ð5ð 
ˆc�5—<‘<ÐÑ	 ó5ðn¨%¯,©,ð ÀÇÁð ÐQV×Q]ÑQ]÷ r/   r˜   c                   óZ   ‡ — e Zd ZdZˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚEomtPatchEmbeddingszì
    This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
    `hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
    Transformer.
    c                 óÌ  •— t         ‰| �  «        |j                  |j                  }}|j                  |j
                  }}t        |t        j                  j                  «      r|n||f}t        |t        j                  j                  «      r|n||f}|d   |d   z  |d   |d   z  z  }|| _        || _        || _        || _
        t        j                  ||||¬«      | _        y )Nr   r   ©Úkernel_sizeÚstride)rb   rc   Ú
image_sizeÚ
patch_sizeÚnum_channelsÚhidden_sizeÚ
isinstanceÚcollectionsÚabcÚIterableÚnum_patchesr   ÚConv2dÚ
projection)re   r™   r!  r"  r#  r$  r)  rf   s          €r0   rc   zEomtPatchEmbeddings.__init__‹  sÔ   ø€ Ü‰ÑÔØ!'×!2Ñ!2°F×4EÑ4E�Jˆ
Ø$*×$7Ñ$7¸×9KÑ9K�kˆä#-¨j¼+¿/¹/×:RÑ:RÔ#S‘ZÐZdÐfpÐYqˆ
Ü#-¨j¼+¿/¹/×:RÑ:RÔ#S‘ZÐZdÐfpÐYqˆ
Ø! !‘}¨
°1©Ñ5¸*ÀQ¹-È:ÐVWÉ=Ñ:XÑYˆØ$ˆŒØ$ˆŒØ(ˆÔØ&ˆÔäŸ)™) L°+È:Ð^hÔiˆ�r/   Úpixel_valuesr3   c                 óÚ   — |j                   d   }|| j                  k7  rt        d| j                  › d|› d�«      ‚| j                  |«      j	                  d«      j                  dd«      }|S )Nr   zoMake sure that the channel dimension of the pixel values match with the one set in the configuration. Expected z	 but got ú.r5   )rP   r#  rd   r+  rE   rÍ   )re   r,  r#  Ú
embeddingss       r0   r‰   zEomtPatchEmbeddings.forwardš  sz   € Ø#×)Ñ)¨!Ñ,ˆØ˜4×,Ñ,Ò,ÜðØ!×.Ñ.Ð/¨y¸¸ÀaðIóð ð —_‘_ \Ó2×:Ñ:¸1Ó=×GÑGÈÈ1ÓMˆ
ØÐr/   )	r%   r&   r'   r(   rc   r)   r   r‰   r�   rŽ   s   @r0   r  r  „  s)   ø„ ñôjð E§L¡Lð °U·\±\÷ r/   r  c                   ód   ‡ — e Zd ZdZdeddfˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚEomtEmbeddingszM
    Construct the CLS token, mask token, position and patch embeddings.
    r™   r3   Nc                 ó¼  •— t         ‰| �  «        || _        |j                  | _        t	        j
                  t        j                  dd|j                  «      «      | _	        t	        j
                  t        j                  d|j                  |j                  «      «      | _        t        |«      | _        | j                  j                  }t	        j                   |j"                  «      | _        d|j                  z   | _        t	        j(                  ||j                  «      | _        | j-                  dt        j.                  |«      j1                  d«      d¬«       y )Nr   Úposition_ids©r   rC   F)Ú
persistent)rb   rc   r™   r"  r   Ú	Parameterr)   Úrandnr$  Ú	cls_tokenr·   Únum_register_tokensÚregister_tokensr  Úpatch_embeddingsr)  ÚDropoutÚhidden_dropout_probÚdropoutÚnum_prefix_tokensÚ	EmbeddingÚposition_embeddingsr¢   rú   Úexpand)re   r™   r)  rf   s      €r0   rc   zEomtEmbeddings.__init__ª  sú   ø€ Ü‰ÑÔàˆŒØ ×+Ñ+ˆŒäŸ™¤e§k¡k°!°Q¸×8JÑ8JÓ&KÓLˆŒÜ!Ÿ|™|¬E¯K©K¸¸6×;UÑ;UÐW]×WiÑWiÓ,jÓkˆÔä 3°FÓ ;ˆÔØ×+Ñ+×7Ñ7ˆÜ—z‘z &×"<Ñ"<Ó=ˆŒØ!" V×%?Ñ%?Ñ!?ˆÔÜ#%§<¡<°¸V×=OÑ=OÓ#PˆÔ Ø×Ñ˜^¬U¯\©\¸+Ó-F×-MÑ-MÈgÓ-VÐchÐÕir/   r,  c                 óÄ  — |j                   \  }}}}| j                  j                  j                  j                  }| j                  |j                  |¬«      «      }| j                  j                  |dd«      }| j                  j                  |dd«      }|| j                  | j                  «      z   }t        j                  |||gd¬«      }| j                  |«      }|S )Nrn   rC   r   rø   )rP   r;  r+  rÆ   ro   rr   r8  rB  r:  rA  r3  r)   rË   r>  )re   r,  r~   r½   Útarget_dtyper/  Ú
cls_tokensr:  s           r0   r‰   zEomtEmbeddings.forwardº  sÆ   € Ø*×0Ñ0Ñˆ
�A�q˜!Ø×,Ñ,×7Ñ7×>Ñ>×DÑDˆØ×*Ñ*¨<¯?©?À¨?Ó+NÓOˆ
à—^‘^×*Ñ*¨:°r¸2Ó>ˆ
Ø×.Ñ.×5Ñ5°jÀ"ÀbÓIˆà $×":Ñ":¸4×;LÑ;LÓ"MÑMˆ
Ü—Y‘Y 
¨O¸ZÐHÈaÔPˆ
à—\‘\ *Ó-ˆ
àÐr/   ©
r%   r&   r'   r(   r   rc   r)   r   r‰   r�   rŽ   s   @r0   r1  r1  ¥  s9   ø„ ñðj˜zð j¨dõ jð  E§L¡Lð °U·\±\÷ r/   r1  ÚmoduleÚqueryr  r  Úattention_maskÚscalingr>  c                 óÀ  — t        j                  ||j                  dd«      «      |z  }|�||z   }t        j                  j                  |dt         j                  ¬«      j                  |j                  «      }t        j                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )NrC   éþÿÿÿ)r7   ro   )ÚpÚtrainingr   r5   )r)   rF   rÍ   r   r9   rq   Úfloat32rr   ro   r>  rN  Ú
contiguous)
rG  rH  r  r  rI  rJ  r>  r=   Úattn_weightsÚattn_outputs
             r0   Úeager_attention_forwardrS  Ê  sº   € ô —<‘<  s§}¡}°R¸Ó'<Ó=ÀÑG€LØÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r/   c            
       ó    ‡ — e Zd ZdZˆ fd„Z	 ddej                  dej                  dz  deej                  ej                  dz  f   fd„Zˆ xZ	S )	ÚEomtAttentionz=Multi-headed attention from 'Attention Is All You Need' paperc                 ó  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        | j                  | j                  z  | _        | j                  | j                  z  | j                  k7  r&t        d| j                  › d| j                  › d�«      ‚| j                  dz  | _	        |j                  | _        d| _        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        y )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).g      à¿F)rb   rc   r™   r$  Ú	embed_dimÚnum_attention_headsÚ	num_headsÚhead_dimrd   ÚscaleÚattention_dropoutr>  Ú	is_causalr   ÚLinearÚk_projÚv_projÚq_projÚout_proj©re   r™   rf   s     €r0   rc   zEomtAttention.__init__ä  s  ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™¨$¯.©.Ñ8ˆŒØ�=‰=˜4Ÿ>™>Ñ)¨T¯^©^Ò;ÜØMÈdÏnÉnÐM]ð ^Ø—N‘NÐ# 2ð'óð ð —]‘] DÑ(ˆŒ
Ø×/Ñ/ˆŒØˆŒä—i‘i §¡°·±Ó?ˆŒÜ—i‘i §¡°·±Ó?ˆŒÜ—i‘i §¡°·±Ó?ˆŒÜŸ	™	 $§.¡.°$·.±.ÓAˆ�r/   Nr"   rI  r3   c           
      ó®  — |j                   dd }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }t        j                  | j                  j                  t        «      }	 |	| ||||| j                  | j                  | j                  sdn| j                  ¬«      \  }
} |
j                   g |¢d‘­Ž j#                  «       }
| j%                  |
«      }
|
|fS )z#Input shape: Batch x Time x ChannelNrC   r   r5   ç        )r]  rJ  r>  )rP   rZ  ra  rü   rÍ   r_  r`  r   Úget_interfacer™   Ú_attn_implementationrS  r]  r[  rN  r>  ÚreshaperP  rb  )re   r"   rI  r=   Úinput_shapeÚhidden_shapeÚqueriesÚkeysÚvaluesÚattention_interfacerR  rQ  s               r0   r‰   zEomtAttention.forwardø  s<  € ð $×)Ñ)¨#¨2Ð.ˆà8˜Ð8 bÐ8¨$¯-©-Ñ8ˆØ—+‘+˜mÓ,×1Ñ1°,Ó?×IÑIÈ!ÈQÓOˆØ�{‰{˜=Ó)×.Ñ.¨|Ó<×FÑFÀqÈ!ÓLˆØ—‘˜]Ó+×0Ñ0°Ó>×HÑHÈÈAÓNˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØØ—n‘nØ—J‘JØ#Ÿ}š}‘C°$·,±,ô	%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ—m‘m KÓ0ˆà˜LÐ(Ð(r/   rÚ   )
r%   r&   r'   r(   rc   r)   r   r,   r‰   r�   rŽ   s   @r0   rU  rU  á  sV   ø„ ÙGôBð. /3ñ!)à—|‘|ð!)ð Ÿ™ tÑ+ð!)ð
 
ˆu�|‰|˜UŸ\™\¨DÑ0Ð0Ñ	1÷!)r/   rU  c                   óX   ‡ — e Zd Zdˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚEomtLayerScaler3   c                 óª   •— t         ‰| �  «        t        j                  |j                  t        j                  |j                  «      z  «      | _        y rÚ   )	rb   rc   r   r6  Úlayerscale_valuer)   r¡   r$  Úlambda1rc  s     €r0   rc   zEomtLayerScale.__init__  s8   ø€ Ü‰ÑÔÜ—|‘| F×$;Ñ$;¼e¿j¹jÈ×I[ÑI[Ó>\Ñ$\Ó]ˆ�r/   Úhidden_statec                 ó    — || j                   z  S rÚ   )rs  ©re   rt  s     r0   r‰   zEomtLayerScale.forward!  s   € Ø˜dŸl™lÑ*Ð*r/   ©r3   N©r%   r&   r'   rc   r)   r   r‰   r�   rŽ   s   @r0   rp  rp    s$   ø„ õ^ð+ E§L¡Lð +°U·\±\÷ +r/   rp  c                   óX   ‡ — e Zd Zdˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚEomtMLPr3   c                 ó~  •— t         ‰| �  «        |j                  x}}t        |j                  |j                  z  «      }t        j                  ||d¬«      | _        t        |j                  t        «      rt        |j                     | _        n|j                  | _        t        j                  ||d¬«      | _        y )NT©Úbias)rb   rc   r$  r‹   Ú	mlp_ratior   r^  Úfc1r%  Ú
hidden_actr  r	   Ú
activationÚfc2©re   r™   Úin_featuresÚout_featuresÚhidden_featuresrf   s        €r0   rc   zEomtMLP.__init__&  s�   ø€ Ü‰ÑÔØ%+×%7Ñ%7Ð7ˆ�lÜ˜f×0Ñ0°6×3CÑ3CÑCÓDˆÜ—9‘9˜[¨/ÀÔEˆŒÜ�f×'Ñ'¬Ô-Ü$ V×%6Ñ%6Ñ7ˆD�Oà$×/Ñ/ˆDŒOÜ—9‘9˜_¨lÀÔFˆ�r/   rt  c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S rÚ   )r  r�  r‚  rv  s     r0   r‰   zEomtMLP.forward1  s2   € Ø—x‘x Ó-ˆØ—‘ |Ó4ˆØ—x‘x Ó-ˆØÐr/   rw  rx  rŽ   s   @r0   rz  rz  %  s$   ø„ õ	Gð E§L¡Lð °U·\±\÷ r/   rz  c                   óX   ‡ — e Zd Zdˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚEomtSwiGLUFFNr3   c                 ó0  •— t         ‰| �  «        |j                  x}}t        |j                  |j                  z  «      }t        |dz  dz  «      dz   dz  dz  }t        j                  |d|z  d¬«      | _        t        j                  ||d¬«      | _        y )Nr5   r   é   é   Tr|  )	rb   rc   r$  r‹   r~  r   r^  Ú
weights_inÚweights_outrƒ  s        €r0   rc   zEomtSwiGLUFFN.__init__9  sŠ   ø€ Ü‰ÑÔØ%+×%7Ñ%7Ð7ˆ�lÜ˜f×0Ñ0°6×3CÑ3CÑCÓDˆÜ˜°Ñ2°QÑ6Ó7¸!Ñ;ÀÑAÀAÑEˆäŸ)™) K°°_Ñ1DÈ4ÔPˆŒÜŸ9™9 _°lÈÔNˆÕr/   rt  c                 ó¶   — | j                  |«      }|j                  dd¬«      \  }}t        j                  j	                  |«      |z  }| j                  |«      S )Nr5   rC   rø   )r�  Úchunkr   r9   ÚsilurŽ  )re   rt  Úx1Úx2Úhiddens        r0   r‰   zEomtSwiGLUFFN.forwardB  sS   € Ø—‘ |Ó4ˆØ×#Ñ# A¨2Ð#Ó.‰ˆˆBÜ—‘×#Ñ# BÓ'¨"Ñ,ˆØ×Ñ Ó'Ð'r/   rw  rx  rŽ   s   @r0   r‰  r‰  8  s$   ø„ õOð( E§L¡Lð (°U·\±\÷ (r/   r‰  c                   ór   ‡ — e Zd ZdZd	deddfˆ fd„Zdej                  dej                  fd„Zde	fd„Z
ˆ xZS )
ÚEomtDropPathzÏStochastic depth (DropPath) per sample, for residual blocks.

    Identity when ``drop_prob`` is 0 or outside training. See `Deep Networks with Stochastic Depth
    <https://arxiv.org/abs/1603.09382>`_.
    Ú	drop_probr3   Nc                 ó0   •— t         ‰| �  «        || _        y rÚ   )rb   rc   r—  )re   r—  rf   s     €r0   rc   zEomtDropPath.__init__P  s   ø€ Ü‰ÑÔØ"ˆ�r/   r"   c                 óP  — | j                   dk(  s| j                  s|S d| j                   z
  }|j                  d   fd|j                  dz
  z  z   }t	        j
                  ||j                  |j                  ¬«      }t	        j                  ||z   «      }|j                  |«      |z  S )Nre  r   r   )r   rµ   )
r—  rN  rP   Úndimr)   rs   ro   rk   ÚfloorÚdiv)re   r"   Ú	keep_probrP   Úrandom_tensors        r0   r‰   zEomtDropPath.forwardT  s—   € Ø�>‰>˜SÒ ¨¯ªØ Ð Ø˜Ÿ™Ñ&ˆ	Ø×$Ñ$ QÑ'Ð)¨D°M×4FÑ4FÈÑ4JÑ,KÑKˆÜŸ
™
 5°×0CÑ0CÈM×L`ÑL`ÔaˆÜŸ™ M°IÑ$=Ó>ˆØ× Ñ  Ó+¨mÑ;Ð;r/   c                 ó    — d| j                   › �S )Nzp=)r—  ©re   s    r0   Ú
extra_reprzEomtDropPath.extra_repr]  s   € Ø�D—N‘NÐ#Ð$Ð$r/   ©re  )r%   r&   r'   r(   rŠ   rc   r)   r   r‰   r  r¡  r�   rŽ   s   @r0   r–  r–  I  sB   ø„ ññ# %ð #°$õ #ð< U§\¡\ð <°e·l±ló <ð%˜C÷ %r/   r–  c                   ó†   ‡ — e Zd ZdZdeddfˆ fd„Z	 d	dej                  dej                  dz  dej                  fd„Zˆ xZ	S )
Ú	EomtLayerzCThis corresponds to the Block class in the original implementation.r™   r3   Nc                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  ¬«      | _        t        |«      | _        t        |«      | _
        |j                  dkD  rt        |j                  «      nt        j                  «       | _        t        j                  |j                  |j
                  ¬«      | _        |j                   rt#        |«      | _        nt'        |«      | _        t        |«      | _        y )N©Úepsre  )rb   rc   r   Ú	LayerNormr$  Úlayer_norm_epsÚnorm1rU  Ú	attentionrp  Úlayer_scale1Údrop_path_rater–  ÚIdentityÚ	drop_pathÚnorm2Úuse_swiglu_ffnr‰  Úmlprz  Úlayer_scale2rc  s     €r0   rc   zEomtLayer.__init__d  s¼   ø€ Ü‰ÑÔä—\‘\ &×"4Ñ"4¸&×:OÑ:OÔPˆŒ
Ü& vÓ.ˆŒÜ*¨6Ó2ˆÔØ@F×@UÑ@UÐX[Ò@[œ f×&;Ñ&;Ô<Ôac×alÑalÓanˆŒä—\‘\ &×"4Ñ"4¸&×:OÑ:OÔPˆŒ
à× Ò Ü$ VÓ,ˆD�Hä˜v“ˆDŒHÜ*¨6Ó2ˆÕr/   r"   rI  c                 ó*  — | j                  |«      }| j                  ||«      \  }}| j                  |«      }| j                  |«      |z   }| j	                  |«      }| j                  |«      }| j                  |«      }| j                  |«      |z   }|S rÚ   )rª  r«  r¬  r¯  r°  r²  r³  )re   r"   rI  Úhidden_states_normÚself_attention_outputr½   Úlayer_outputs          r0   r‰   zEomtLayer.forwardt  s›   € ð
 "ŸZ™Z¨Ó6ÐØ#'§>¡>Ð2DÀnÓ#UÑ Ð˜qØ $× 1Ñ 1Ð2GÓ HÐð Ÿ™Ð'<Ó=ÀÑMˆð —z‘z -Ó0ˆØ—x‘x Ó-ˆØ×(Ñ(¨Ó6ˆð —~‘~ lÓ3°mÑCˆàÐr/   rÚ   rF  rŽ   s   @r0   r¤  r¤  a  sP   ø„ ÙMð3˜zð 3¨dõ 3ð& /3ñà—|‘|ðð Ÿ™ tÑ+ðð 
�‰÷	r/   r¤  c                   óX   ‡ — e Zd Zdˆ fd„	Zdej
                  dej
                  fd„Zˆ xZS )ÚEomtLayerNorm2dc                 ó*   •— t         ‰| �  |||¬«       y )N)r§  Úelementwise_affine)rb   rc   )re   r#  r§  Úaffinerf   s       €r0   rc   zEomtLayerNorm2d.__init__Œ  s   ø€ Ü‰Ñ˜¨3À6ÐÕJr/   rt  r3   c                 óØ   — |j                  dddd«      }t        j                  || j                  | j                  | j
                  | j                  «      }|j                  dddd«      }|S )Nr   r5   r   r   )ÚpermuteÚFÚ
layer_normÚnormalized_shaperÆ   r}  r§  rv  s     r0   r‰   zEomtLayerNorm2d.forward�  sb   € Ø#×+Ñ+¨A¨q°!°QÓ7ˆÜ—|‘| L°$×2GÑ2GÈÏÉÐVZ×V_ÑV_Ðae×aiÑaiÓjˆØ#×+Ñ+¨A¨q°!°QÓ7ˆØÐr/   )g�íµ ÷Æ°>Trx  rŽ   s   @r0   r¹  r¹  ‹  s$   ø„ õKð E§L¡Lð °U·\±\÷ r/   r¹  c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚEomtScaleLayerr™   c                 ó  •— t         ‰| �  «        |j                  }t        j                  ||dd¬«      | _        t        |j                     | _        t        j                  ||dd|d¬«      | _
        t        |«      | _        y )Nr5   r  r   r   F)r  ÚpaddingÚgroupsr}  )rb   rc   r$  r   ÚConvTranspose2dÚconv1r	   r€  r�  r*  Úconv2r¹  Úlayernorm2d©re   r™   r$  rf   s      €r0   rc   zEomtScaleLayer.__init__—  su   ø€ Ü‰ÑÔØ×(Ñ(ˆÜ×'Ñ'¨°[ÈaÐXYÔZˆŒ
Ü  ×!2Ñ!2Ñ3ˆŒÜ—Y‘YØØØØØØô
ˆŒ
ô +¨;Ó7ˆÕr/   r"   r3   c                 óŽ   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }|S rÚ   )rÈ  r�  rÉ  rÊ  ©re   r"   s     r0   r‰   zEomtScaleLayer.forward§  sB   € ØŸ
™
 =Ó1ˆØŸ™¨Ó6ˆØŸ
™
 =Ó1ˆØ×(Ñ(¨Ó7ˆØÐr/   ©	r%   r&   r'   r   rc   r)   r   r‰   r�   rŽ   s   @r0   rÃ  rÃ  –  s*   ø„ ð8˜zõ 8ð  U§\¡\ð °e·l±l÷ r/   rÃ  c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚEomtScaleBlockr™   c                 óÖ   •— t         ‰| �  «        |j                  | _        t	        j
                  t        | j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        y c c}w rÚ   )	rb   rc   Únum_upscale_blocksÚ
num_blocksr   Ú
ModuleListrp   rÃ  Úblock©re   r™   r½   rf   s      €r0   rc   zEomtScaleBlock.__init__°  sG   ø€ Ü‰ÑÔØ ×3Ñ3ˆŒÜ—]‘]ÄEÈ$Ï/É/ÓDZÖ#[¸q¤N°6Õ$:Ò#[Ó\ˆ�
ùÒ#[s   ÁA&r"   r3   c                 ó8   — | j                   D ]
  } ||«      }Œ |S rÚ   )rÕ  )re   r"   rÕ  s      r0   r‰   zEomtScaleBlock.forwardµ  s%   € Ø—Z‘Zò 	1ˆEÙ! -Ó0‰Mð	1àÐr/   rÎ  rŽ   s   @r0   rÐ  rÐ  ¯  s,   ø„ ð]˜zõ ]ð
 U§\¡\ð °e·l±l÷ r/   rÐ  c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚEomtMaskHeadr™   c                 ó  •— t         ‰| �  «        |j                  }t        j                  ||«      | _        t        j                  ||«      | _        t        j                  ||«      | _        t        |j                     | _
        y rÚ   )rb   rc   r$  r   r^  r  r‚  Úfc3r	   r€  r�  rË  s      €r0   rc   zEomtMaskHead.__init__¼  sa   ø€ Ü‰ÑÔà×(Ñ(ˆÜ—9‘9˜[¨+Ó6ˆŒÜ—9‘9˜[¨+Ó6ˆŒÜ—9‘9˜[¨+Ó6ˆŒÜ  ×!2Ñ!2Ñ3ˆ�r/   r"   r3   c                 ó¨   — | j                  | j                  |«      «      }| j                  | j                  |«      «      }| j                  |«      }|S rÚ   )r�  r  r‚  rÛ  rÍ  s     r0   r‰   zEomtMaskHead.forwardÅ  sD   € ØŸ™¨¯©°Ó(?Ó@ˆØŸ™¨¯©°Ó(?Ó@ˆØŸ™ Ó/ˆØÐr/   rÎ  rŽ   s   @r0   rÙ  rÙ  »  s*   ø„ ð4˜zõ 4ð U§\¡\ð °e·l±l÷ r/   rÙ  c                   ó˜   ‡ — e Zd ZU dZeed<   dZdZdZdZ	dgZ
dZeed	œZ ej                   «       d
ej$                  ddfˆ fd„«       Zˆ xZS )ÚEomtPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    r™   Úeomtr,  )ÚimageFr¤  T)r"   r#   rG  r3   Nc                 ón  •— t         ‰| �  |«       | j                  j                  }t	        |t
        j                  t
        j                  t
        j                  f«      r¹t        j                  |j                  t        j                  d«      ¬«       |j                  �xt        j
                  j                  j!                  |j                  «      \  }}|dkD  rdt        j                  |«      z  nd}t        j"                  |j                  | |«       y y t	        |t
        j$                  «      rtt        j&                  |j                  dd¬«       |j(                  �Et+        |j                  dd«      s-t        j,                  |j                  |j(                     «       y y y t	        |t.        «      rBt1        |d	«      r5t        j2                  |j4                  | j                  j6                  «       y y t	        |t8        «      r›t        j:                  |j<                  d|¬«       t        j,                  |j>                  «       t        j@                  |jB                  t        jD                  |jB                  jF                  d
   «      jI                  d«      «       y t	        |tJ        «      rRt        jL                  |jN                  dz   «      }|jP                  |d
<   t        j@                  |jR                  |«       y t	        |tT        «      r t        jV                  |jX                  «       y y )Né   )Úar   r   re  )r”   ÚstdÚ_is_hf_initializedFrs  rC   r4  )-rb   Ú_init_weightsr™   Úinitializer_ranger%  r   r^  r*  rÇ  ÚinitÚkaiming_uniform_rÆ   ÚmathÚsqrtr}  r)   Ú_calculate_fan_in_and_fan_outÚuniform_r@  Únormal_Úpadding_idxÚgetattrÚzeros_rp  ÚhasattrÚ	constant_rs  rr  r1  Útrunc_normal_r8  r:  rº   r3  rú   rP   rB  r˜   r¡   rž   r    r�   ÚEomtForUniversalSegmentationÚones_Úattn_mask_probs)re   rG  rä  Úfan_inr½   Úboundr�   rf   s          €r0   ræ  z!EomtPreTrainedModel._init_weightsß  s*  ø€ ä‰Ñ˜fÔ%Ø�k‰k×+Ñ+ˆÜ�fœrŸy™y¬"¯)©)´R×5GÑ5GÐHÔIÜ×!Ñ! &§-¡-´4·9±9¸Q³<Õ@Ø�{‰{Ð&Ü!ŸH™HŸM™M×GÑGÈÏÉÓV‘	�˜Ø17¸!²˜œDŸI™I fÓ-Ò-À�Ü—‘˜fŸk™k¨E¨6°5Õ9ð 'ô ˜¤§¡Ô-Ü�L‰L˜Ÿ™¨S°aÕ8à×!Ñ!Ð-´g¸f¿m¹mÐMaÐchÔ6iÜ—‘˜FŸM™M¨&×*<Ñ*<Ñ=Õ>ð 7jÐ-ä˜¤Ô/Ü�v˜yÔ)Ü—‘˜vŸ~™~¨t¯{©{×/KÑ/KÕLð *ä˜¤Ô/Ü×Ñ˜v×/Ñ/°c¸sÕCÜ�K‰K˜×.Ñ.Ô/Ü�J‰J�v×*Ñ*¬E¯L©L¸×9LÑ9L×9RÑ9RÐSUÑ9VÓ,W×,^Ñ,^Ð_fÓ,gÕhÜ˜¤Ô)Ü Ÿ:™: f×&7Ñ&7¸!Ñ&;Ó<ˆLØ%Ÿ™ˆL˜ÑÜ�J‰J�v×*Ñ*¨LÕ9Ü˜Ô <Ô=Ü�J‰J�v×-Ñ-Õ.ð >r/   )r%   r&   r'   r(   r   r+   Úbase_model_prefixÚmain_input_nameÚinput_modalitiesÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_sdpar¤  rU  Ú_can_record_outputsr)   rŒ   r   ÚModuleræ  r�   rŽ   s   @r0   rÞ  rÞ  Ì  sp   ø… ñð
 ÓØÐØ$€OØ!ÐØ&+Ð#Ø$˜ÐØ€Nà"Ø#ñÐð
 €U‡]�]ƒ_ð/ B§I¡Ið /°$ô /ó ô/r/   rÞ  zV
    The EoMT Model with head on top for instance/semantic/panoptic segmentation.
    c                   ó  ‡ — e Zd ZdZdefˆ fd„Zdededededeeef   d	eeef   fd
„Z	deeef   d	efd„Z
eee	 	 	 ddedee   dz  dee   dz  dee   dz  dee   d	efd„«       «       «       Zd„ Zdej                  fd„Zed„ «       Zˆ xZS )rõ  r,  r™   c                 óÞ  •— t         ‰| �  |«       || _        |j                  | _        t	        |«      | _        t        j                  |j                  |j                  ¬«      | _
        t        j                  |j                  |j                  «      | _        t        j                  t        |j                  «      D �cg c]  }t!        |«      ‘Œ c}«      | _        t%        |«      | _        t)        |«      | _        t        j,                  |j                  |j.                  dz   «      | _        |j2                  |j4                  z  |j2                  |j4                  z  f| _        |j8                  |j:                  |j<                  dœ| _        tA        || j>                  ¬«      | _!        | jE                  dtG        jH                  |jJ                  «      «       | jM                  «        y c c}w )Nr¦  r   )rÈ   rÞ   rß   )r™   rš   r÷  )'rb   rc   r™   Únum_hidden_layersr1  r/  r   r¨  r$  r©  Ú	layernormr@  rÏ   rH  rÔ  rp   r¤  ÚlayersrÐ  Úupscale_blockrÙ  Ú	mask_headr^  rž   Úclass_predictorr!  r"  Ú	grid_sizer¦   r¨   r§   rš   r˜   rU   r¢   r)   r¡   rÓ  Ú	post_initrÖ  s      €r0   rc   z%EomtForUniversalSegmentation.__init__  sp  ø€ Ü‰Ñ˜Ô ØˆŒØ!'×!9Ñ!9ˆÔÜ(¨Ó0ˆŒÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒä—\‘\ &×"4Ñ"4°f×6HÑ6HÓIˆŒ
Ü—m‘mÄÀf×F^ÑF^Ó@_Ö$`¸1¤Y¨vÕ%6Ò$`ÓaˆŒä+¨FÓ3ˆÔÜ% fÓ-ˆŒä!Ÿy™y¨×);Ñ);¸V×=NÑ=NÐQRÑ=RÓSˆÔà ×+Ñ+¨v×/@Ñ/@Ñ@À&×BSÑBSÐW]×WhÑWhÑBhÐiˆŒà"(×"5Ñ"5Ø×+Ñ+Ø×+Ñ+ñ.
ˆÔô "¨¸T×=MÑ=MÔNˆŒà×ÑÐ.´·
±
¸6×;LÑ;LÓ0MÔNà�‰Õùò% %as   Â>G*r    r   rg   rh   r  r3   c                 óº   — | j                  |||||¬«      }| j                  j                  «       D ]'  \  }}|j                  «       D ]  \  }	}
||	v sŒ|
|z  }
Œ Œ) |S )N©r    r   rg   rh   r  )rU   rš   r  )re   r    r   rg   rh   r  r
  r  rÆ   Úloss_keyr   s              r0   Úget_loss_dictz*EomtForUniversalSegmentation.get_loss_dict!  s|   € ð (,§~¡~Ø!5Ø!5Ø#Ø%Ø"7ð (6ó (
ˆ	ð  ×+Ñ+×1Ñ1Ó3ò 	#‰KˆC�Ø"+§/¡/Ó"3ò #‘�˜$Ø˜(’?Ø˜F‘N‘Dñ#ð	#ð
 Ðr/   r
  c                 ó4   — t        |j                  «       «      S rÚ   )rH   rm  )re   r
  s     r0   Úget_lossz%EomtForUniversalSegmentation.get_loss9  s   € Ü�9×#Ñ#Ó%Ó&Ð&r/   Nr$   r=   c                 ó(  — d\  }}d}|€t        d«      ‚| j                  |«      }	t        | j                  «      D �]Î  \  }
}|
| j                  | j
                  j                  z
  k(  rp| j                  j                  ddd…dd…f   j                  |	j                  d   dd«      j                  |	j                  «      }t        j                  ||	fd¬«      }	|
| j                  | j
                  j                  z
  k\  �r| j                  s7| j                   |
| j                  z
  | j
                  j                  z      dkD  �r¿| j#                  |	«      }| j%                  |«      \  }}||fz  }||fz  }t        j&                  |	j                  d   |	j                  d   |	j                  d   |	j                  t        j(                  ¬«      }t+        j,                  || j.                  d	¬
«      }|j1                  |j3                  d«      |j3                  d«      d«      }| j
                  j4                  }|| j                  j6                  z   }|dkD  |dd…d|…|d…f<   | j9                  || j                   |
| j                  z
  | j
                  j                  z      |||j                  ¬«      }|dd…ddf   j                  d| j
                  j:                  dd«      }|j=                  «       j?                  | d«      } ||	|«      }	�ŒÑ | j#                  |	«      }| j%                  |«      \  }}||fz  }||fz  }d}|�B|�@d}tA        ||«      D ]/  \  }}| jC                  ||||d¬«      }|| jE                  |«      z  }Œ1 tG        |||||¬«      S )ag  
        mask_labels (`list[torch.Tensor]`, *optional*):
            list of mask labels of shape `(num_labels, height, width)` to be fed to a model
        class_labels (`list[torch.LongTensor]`, *optional*):
            list of target class labels of shape `(num_labels, height, width)` to be fed to a model. They identify the
            labels of `mask_labels`, e.g. the label of `mask_labels[i][j]` if `class_labels[i][j]`.
        patch_offsets (`list[torch.Tensor]`, *optional*):
            list of tuples indicating the image index and start and end positions of patches for semantic segmentation.
        )r.   r.   Nz You have to specify pixel_valuesr   rC   r   rø   )rk   ro   Úbilinear)ÚsizeÚmode)ÚprobÚnum_query_tokensÚencoder_start_tokensrk   .g    eÍÍÁre  r  )r   r    r   r!   r$   )$rd   r/  r¬   r  r  r™   rÓ  rH  rÆ   rB  rP   rr   rk   r)   rË   rN  r÷  r  Úpredictr¡   r¸   r¿  Úinterpolater
  rü   r  rÏ   r?  Ú_disable_attention_maskrX  rŠ   Úmasked_fillr¹   r  r  r   )re   r,  rg   rh   r$   r=   Úmasks_queries_logits_per_layerÚclass_queries_logits_per_layerrI  r"   rÐ   Úlayer_modulerH  Únorm_hidden_statesr    r   Úinterpolated_logitsr  r  Úsequence_outputr   r
  s                         r0   r‰   z$EomtForUniversalSegmentation.forward<  sÅ  € ð* JPÑFÐ&Ð(FØˆàÐÜÐ?Ó@Ð@àŸ™¨Ó5ˆä!*¨4¯;©;Ó!7ó .	HÑˆC�Ø�d×,Ñ,¨t¯{©{×/EÑ/EÑEÒEØŸ
™
×)Ñ)¨$²²1¨*Ñ5×<Ñ<¸]×=PÑ=PÐQRÑ=SÐUWÐY[Ó\×_Ñ_Ð`m×`tÑ`tÓu�Ü %§	¡	¨5°-Ð*@ÀaÔ H�à�d×,Ñ,¨t¯{©{×/EÑ/EÑEÓEØ—’ ×!5Ñ!5°c¸D×<RÑ<RÑ6RÐUY×U`ÑU`×UkÑUkÑ6kÑ!lÐopÓ!pà%)§^¡^°MÓ%BÐ"Ø=A¿\¹\ÐJ\Ó=]Ñ:Ð$Ð&:à.Ð3GÐ2IÑIÐ.Ø.Ð3GÐ2IÑIÐ.ä!&§¡Ø!×'Ñ'¨Ñ*Ø!×'Ñ'¨Ñ*Ø!×'Ñ'¨Ñ*Ø(×/Ñ/ÜŸ*™*ô"�ô '(§m¡mÐ4HÈtÏ~É~ÐdnÔ&oÐ#Ø&9×&>Ñ&>Ø'×,Ñ,¨QÓ/Ð1D×1IÑ1IÈ!Ó1LÈbó'Ð#ð $(§;¡;×#:Ñ#:Ð Ø'7¸$¿/¹/×:[Ñ:[Ñ'[Ð$ð ObÐdeÑNe�šqÐ"3Ð#3Ð"3Ð5IÑ5JÐJÑKð "&×!=Ñ!=Ø"Ø×-Ñ-¨c°D×4JÑ4JÑ.JÈTÏ[É[×McÑMcÑ.cÑdØ%5Ø)=Ø)×0Ñ0ð ">ó "�ð "0²°4¸°Ñ!=×!DÑ!DÀRÈÏÉ×IhÑIhÐjlÐnpÓ!q�Ø!/×!5Ñ!5Ó!7×!CÑ!CÀ^ÀOÐUYÓ!Z�á(¨¸ÓGŠMð].	Hð` Ÿ.™.¨Ó7ˆà59·\±\À/Ó5RÑ2ÐÐ2Ø&Ð+?Ð*AÑAÐ&Ø&Ð+?Ð*AÑAÐ&àˆØÐ" |Ð'?ØˆDÜ>AØ.Ð0Nó?ò 
1Ñ:Ð$Ð&:ð !×.Ñ.Ø)=Ø)=Ø +Ø!-Ø*.ð /ó �	ð ˜Ÿ™ iÓ0Ñ0‘ð
1ô 2ØØ!5Ø!5Ø-Ø'ô
ð 	
r/   c                 ó.   — | j                   j                  S rÚ   )r/  r;  r   s    r0   Úget_input_embeddingsz1EomtForUniversalSegmentation.get_input_embeddings¦  s   € Ø�‰×/Ñ/Ð/r/   rÜ   c                 óÎ  — |d d …d | j                   j                  …d d …f   }| j                  |«      }|d d …| j                   j                  | j                  j                  z   d …d d …f   }|j                  dd«      } |j                  |j                  d   dg| j                  ¢­Ž }| j                  |«      }| j                  |«      }t        j                  d||«      }||fS )Nr   r5   r   rC   zbqc, bchw -> bqhw)r™   rÏ   r	  r/  r?  rÍ   rh  rP   r
  r  r  r)   Úeinsum)re   rÜ   Úquery_tokensÚclass_logitsÚprefix_tokensÚmask_logitss         r0   r  z$EomtForUniversalSegmentation.predict©  s×   € ØšaÐ!: 4§;¡;×#:Ñ#:Ð!:ºAÐ=Ñ>ˆØ×+Ñ+¨LÓ9ˆàšq $§+¡+×"9Ñ"9¸D¿O¹O×<]Ñ<]Ñ"]Ñ"_ÒabÐbÑcˆØ%×/Ñ/°°1Ó5ˆà-˜×-Ñ-¨m×.AÑ.AÀ!Ñ.DÀbÐZÈ4Ï>É>ÒZˆà—~‘~ lÓ3ˆØ×*Ñ*¨=Ó9ˆä—l‘lÐ#6¸ÀmÓTˆà˜LÐ(Ð(r/   c                 ó‚   — |dk  r9t        j                  | j                  d   ||¬«      |kD  }d| d d …d |…|d …f   |<   | S )Nr   r   rj   )r)   rs   rP   )Ú	attn_maskr  r  r  rk   Úrandom_queriess         r0   r  z4EomtForUniversalSegmentation._disable_attention_mask¹  sW   € à�!Š8ä"ŸZ™Z¨	¯©¸Ñ(:Ð<LÐU[Ô\Ð_cÑcˆNð VWˆI’aÐ*Ð*Ð*Ð,@Ñ,AÐAÑBÀ>ÑRàÐr/   )NNN)r%   r&   r'   rû  r   rc   r   r  r  r  r  r   r   r   r-   r   r   r   r‰   r$  r)   r  Ústaticmethodr  r�   rŽ   s   @r0   rõ  rõ  ý  s?  ø„ ð %€Oð˜zõ ð8à$ðð %ðð ð	ð
 ðð  $ C¨ KÑ0ðð 
ˆc�6ˆkÑ	óð0' $ s¨F {Ñ"3ð '¸ó 'ð  ØØð ,0Ø,0Ø-1ñe
àðe
ð ˜&‘\ DÑ(ðe
ð ˜6‘l TÑ)ð	e
ð
 ˜F‘| dÑ*ðe
ð Ð+Ñ,ðe
ð 
,òe
ó ó ó  ðe
òN0ð)˜eŸl™ló )ð  ñó ôr/   rõ  )Fr¢  )JÚcollections.abcr&  rê  r   Údataclassesr   Únumpyr  r)   Útorch.nn.functionalr   r9   r¿  r   Ú r   rè  Úactivationsr	   Ú
file_utilsr
   r   r   Úmodeling_layersr   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   Úutils.genericr   Úutils.output_capturingr   Úconfiguration_eomtr   Úscipy.optimizer   Ú
accelerater   Úaccelerate.utilsr   r   r?   rK   rZ   r  r\   r‹   r’   r–   r˜   r  r1  rŠ   rS  rU  rp  rz  r‰  r–  r¤  r¨  r¹  rÃ  rÐ  rÙ  rÞ  rõ  Ú__all__r.   r/   r0   ú<module>rA     sò  ðó* Û Ý $Ý !ã Û ß Ð ß å &Ý !ß LÑ LÝ 9ß FÝ &ß PÑ PÝ 7Ý 5Ý *ñ ÔÝ4áÔÝ'Ý'ñ ðô	ð ô4¨ó 4ó ó	ð4ðB LQñØ—L‘LðØ5:·\±\ðà
‡\�\óð@ ð °ð ¸6ó ð,°·±ð ÀuÇ|Á|ð ÐX]×XdÑXdó ô8g˜2Ÿ9™9ô gðT�fð  fð ¸ð Àó ð< u§|¡|ð ¸U¿\¹\ð ÐVYð Ð^c×^jÑ^jó ô(uˆr�y‰yô uôp	˜"Ÿ)™)ô ôB"�R—Y‘Yô "ðX ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð ð%ð ó%ô.8)�B—I‘Iô 8)ôv+�R—Y‘Yô +ôˆb�i‰iô ô&(�B—I‘Iô (ô"%�2—9‘9ô %ô0'Ð*ô 'ôT�b—l‘lô ô�R—Y‘Yô ô2	�R—Y‘Yô 	ô�2—9‘9ô ð" ô-/˜/ó -/ó ð-/ñ` ðôô
@Ð#6ó @óð
@ðF !Ð"@Ð
A�r/   