§
    ­¸ja  ã                  ó:   — d Z ddlmZ ddlZddlmZ dZefdd„ZdS )aH  Torch-free multi-class top-k selection shared by the export inference helpers.

RF-DETR uses independent per-class sigmoids, not a mutually exclusive softmax, so a single query can legitimately score
above threshold on more than one class at once (e.g. "car" and "truck"). ``PostProcess._select_topk``
(``rfdetr/models/postprocess.py``) accounts for this: it flattens the ``(Q, C)`` score grid to ``Q * C`` query/class
pairs and takes the top ``num_select`` scoring pairs *before* any threshold is applied, so a query can contribute more
than one detection. Selecting the single highest-scoring class per query (``argmax`` over the class axis) silently drops
the rest.

The export inference helpers must reproduce that exact selection so exported ONNX/TFLite models match
``RFDETR.predict()`` detection-for-detection, not just box-for-box.
é    )ÚannotationsN)ÚNDArrayi,  Ú
scores_allúNDArray[np.floating]Ú	thresholdÚfloatÚ
num_selectÚintÚreturnúAtuple[NDArray[np.floating], NDArray[np.int64], NDArray[np.int64]]c                ó
  — | j         dk    rt          d| j        › �¦  «        ‚|dk     rt          d|› �¦  «        ‚| j        \  }}|                      d¦  «        }|dk    s|j        dk    rI|dd…         t          j        dt
          j        ¬¦  «        t          j        dt
          j        ¬¦  «        fS t          ||j        d         ¦  «        }t          j	        |j        d         t
          j        ¬¦  «        }t          j
        t          j        |¦  «        t
          j        |¦  «        }|j        t
          j        k    �ru||j        d         k     �rc|dz  |j        d         k    �rN|j        d         t          j        t
          j        ¦  «        j        k    �r|                     t
          j        ¦  «        }	t          j        d	¦  «        }
|	t          j
        |	|
z  dk    t          j        d
¦  «        |
¦  «        z  }|
||	t          j        d¦  «        z  dk    <   |                      t
          j        ¦  «        t          j        d¦  «        z  |                     t
          j        ¦  «        z  }t          j        ||dz
  ¦  «        d|…         }|t          j        ||         d¬¦  «                 }nt          j        || f¦  «        d|…         }||         }||z  }||z  }||k    }||         ||         ||         fS )uà  Select the top ``num_select`` query/class pairs, then threshold.

    Mirrors ``PostProcess._select_topk`` (flatten ``(Q, C)`` to ``Q * C``, take the top
    ``num_select`` scoring pairs in deterministic descending-score order) followed by the
    caller's own ``scores > threshold`` filter â€” ``PostProcess`` never bakes thresholding into
    ``_select_topk`` itself, it is always applied by the caller afterwards.

    Uses a deterministic lexicographic order: descending score, then ascending flattened
    query/class index. ``PostProcess._select_topk`` uses the same stable tie rule so exported
    inference remains reproducible when scores are equal.

    Args:
        scores_all: Per-query, per-class sigmoid probabilities, shape ``(Q, C)``.
        threshold: Confidence threshold; pairs at or below this score are dropped.
        num_select: Maximum number of query/class pairs to consider before thresholding.

    Returns:
        A ``(scores, labels, query_indices)`` tuple, each 1-D and sorted by descending score,
        containing only pairs that cleared ``threshold``. ``query_indices`` selects rows from
        box/mask outputs and, unlike a per-query argmax, can repeat when a query has more than one
        detection.
    é   z'scores_all must have shape (Q, C); got r   z%num_select must be non-negative; got éÿÿÿÿN)Údtypeé   l        l   ÿÿ iÿÿÿé    é   Ústable)Úkind)ÚndimÚ
ValueErrorÚshapeÚreshapeÚsizeÚnpÚemptyÚint64ÚminÚarangeÚwhereÚisnanÚinfr   Úfloat32ÚiinfoÚuint32ÚmaxÚviewÚastypeÚuint64ÚargpartitionÚargsortÚlexsort)r   r   r	   Únum_queriesÚnum_classesÚflat_scoresÚnum_to_selectÚflat_idxÚsort_scoresÚ
score_bitsÚsign_bitÚordered_scoresÚselection_keysÚpartition_idxÚtop_idxÚtopk_scoresÚ
topk_queryÚtopk_labelsÚkeeps                      úc/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/export/_topk.pyÚ_select_topk_multiclassr>      sÒ  € ð2 „˜!ÒÐÝÐUÀ:ÔCSÐUÐUÑVÔVÐVØ�A‚~€~ÝÐMÀÐMÐMÑNÔNÐNà)Ô/Ñ€K�Ø×$Ò$ RÑ(Ô(€KØ�Q‚€˜+Ô*¨aÒ/Ð/à˜˜˜ŒOÝŒH�Q�bœhÐ'Ñ'Ô'ÝŒH�Q�bœhÐ'Ñ'Ô'ð
ð 	
õ ˜
 KÔ$5°aÔ$8Ñ9Ô9€MÝŒy˜Ô*¨1Ô-µR´XÐ>Ñ>Ô>€Hõ ”(�2œ8 KÑ0Ô0µ"´&¸+ÑFÔF€Kð 	Ô�RœZÒ'Ñ'Ø˜KÔ-¨aÔ0Ò0Ñ0Ø˜AÑ Ô!2°1Ô!5Ò5Ñ5ØÔ˜aÔ ¥B¤H­R¬YÑ$7Ô$7Ô$;Ò;Ñ;ð
 !×%Ò%¥b¤iÑ0Ô0ˆ
Ý”9˜ZÑ(Ô(ˆØ#¥b¤hØ˜(Ñ" qÒ(ÝŒI�jÑ!Ô!Øñ'
ô '
ñ 
ˆð
 EMˆ˜
¥R¤Y¨zÑ%:Ô%:Ñ:¸qÒ@ÑAð
 +˜?×2Ò2µ2´9Ñ=Ô=ÅÄÈ2ÁÄÑNÐRZ×RaÒRaÕbdÔbkÑRlÔRlÑlˆÝœ¨¸ÈÑ8IÑJÔJÈ>ÈMÈ>ÔZˆØ¥¤
¨>¸-Ô+HÈxÐ XÑ XÔ XÔYˆˆå”*˜h¨¨Ð5Ñ6Ô6°~¸°~ÔFˆà˜gÔ&€KØ˜KÑ'€JØ˜KÑ'€Kà˜Ò"€DØ�tÔ˜k¨$Ô/°¸DÔ1AÐAÐAó    )r   r   r   r   r	   r
   r   r   )	Ú__doc__Ú
__future__r   Únumpyr   Únumpy.typingr   ÚDEFAULT_NUM_SELECTr>   © r?   r=   ú<module>rF      s�   ððð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  ð
 Ð ð K]ðOBð OBð OBð OBð OBð OBð OBr?   