
    h                     v    S SK rS SKJr  S r " S S\5      r " S S\5      r " S S	\5      r " S
 S\5      rg)    N)OpRunc                    [        U[        R                  5      (       a%  UR                  S:w  a  [	        SU< S35      eUS   n[        U5      n[        U R                  5      S:X  a  US:X  a  [        R                  " U R                  S   5      SS2S4   nUS:X  aC  [        R                  " XUS-
  S9nUSS2SU24   nUU[        R                  " XU4   5      4   nOD[        R                  " U * X!S-
  S9nUSS2SU24   nUU[        R                  " XU4   * 5      4   nXU4   nXe4$ [        R                  " XS9n[        R                  " XS9nU(       a(  [        R                  " XRS9n[        R                  " XrS9n[        R                  " U5      n[        R                  " XXUS9n	[        R                  " XxUS9n
X4$ )	z
See function `_kneighbors_reduce_func
<https://github.com/scikit-learn/scikit-learn/blob/main/
sklearn/neighbors/_base.py#L304>`_.
   zk must be an integer not .r      N)axiskthr   )
isinstancenpndarraysizeRuntimeErrorintlenshapearangeargpartitionargsortsortfliptake)Xkr   largestsample_rangesorted_indicessorted_distancessorted_valuesarktopk_sorted_indicestopk_sorted_valuess              T/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/reference/ops/op_topk.pytopk_sorted_implementationr$      s    !RZZ  66Q;!:1%qABBaDAA
177|qTQYyy,QW5a<__Qq1uEN+ArrE2N+bjj+G)HIIN  __aRdAFN+ArrE2N+bjj!.,H*I)IJJN >9://ZZ-NGGA)M;9
))A,C''.DA$?22    c                       \ rS rSrSS jrSrg)_CommonTopK5   c                     US   nUS:  a  UOU[        UR                  5      -   n[        XX45      u  pgXgR                  [        R
                  5      4$ )  
Runtime for operator *TopK*.
The implementation is not the most efficient
as it sorts everything then extracts the top *k*
values.

.. warning::
    ONNX specifications may be imprecise in case of negative value
    for axis. The implementation follows what `onnxruntime`
    does in `top_k.cc
    <https://github.com/Microsoft/onnxruntime/blob/main/onnxruntime/core/providers/cpu/math/top_k.cc#L63>`_.
r   )r   r   r$   astyper   int64)selfdatainkr   r   r   r   sortis           r#   _common_run_CommonTopK._common_run6   sK     Fqyttc$**o'=0$Hll288,--r%    N)r   )__name__
__module____qualname____firstlineno__r1   __static_attributes__r3   r%   r#   r'   r'   5   s    .r%   r'   c                       \ rS rSrSS jrSrg)TopK_1I   Nc                 .    [         R                  XU/US9$ r*   r
   r'   r1   )r-   r.   r   r   s       r#   _runTopK_1._runJ   s     &&tA3T&BBr%   r3   )NNr4   r5   r6   r7   r?   r8   r3   r%   r#   r:   r:   I       Cr%   r:   c                       \ rS rSrSS jrSrg)TopK_10Z   Nc                 *    [         R                  XX#S9$ r=   r>   )r-   r.   r/   r   s       r#   r?   TopK_10._run[   s     &&t3&BBr%   r3   )NrA   r3   r%   r#   rD   rD   Z   rB   r%   rD   c                       \ rS rSrSS jrSrg)TopK_11k   Nc                 N    US;  a  [        S5      e[        R                  XX#US9$ )r*   )Tr   z.TopK does not implement anything for sorted=0.)r   r   )r   r'   r1   )r-   r.   r/   r   r   sorteds         r#   r?   TopK_11._runl   s0     "OPP&&t37&SSr%   r3   )NNNrA   r3   r%   r#   rI   rI   k   s    Tr%   rI   )	numpyr   onnx.reference.op_runr   r$   r'   r:   rD   rI   r3   r%   r#   <module>rP      sI     ''3T.% .(C[ C"Ck C"Tk Tr%   