
    bIgD                        d dl mZ d dlZd dlmZ d Z G d de      Z G d de      Z G d	 d
e      Z	 G d de      Z
y)    )annotationsN)OpRunc                   t        |t        j                        r#|j                  dk7  rt	        d|d      |d   }t        |      }t        | j                        dk(  r|dk(  rt        j                  | j                  d         dddf   }|dk(  rFt        j                  | ||dz
        }|ddd|f   }||t        j                  | ||f         f   }nGt        j                  |  ||dz
        }|ddd|f   }||t        j                  | ||f          f   }| ||f   }||fS t        j                  | |      }t        j                  | |      }|r.t        j                  ||      }t        j                  ||      }t        j                  |      }t        j                  |||      }	t        j                  |||      }
|
|	fS )	zSee 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              W/var/www/html/answerous/venv/lib/python3.12/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<__QTq1uEN+ArrE2N+bjj<+G)HIIN  __aRdAFN+ArrE2N+bjj!L.,H*I)IJJN \>9://ZZ-NGGAD)Md;D9
))A,C''.#DA$?222    c                      e Zd ZddZy)_CommonTopKc                    |d   }|dk\  r|n|t        |j                        z   }t        ||||      \  }}||j                  t        j
                        fS )  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_runz_CommonTopK._common_run5   sS     Fqyttc$**o'=0q$Heell288,--r&   N)r   )__name__
__module____qualname__r1    r&   r$   r(   r(   4   s    .r&   r(   c                      e Zd ZddZy)TopK_1Nc                6    t         j                  | ||g|      S r*   r   r(   r1   )r-   r.   r   r	   s       r$   _runzTopK_1._runH   s      &&tTA3T&BBr&   )NNr2   r3   r4   r;   r5   r&   r$   r7   r7   G       Cr&   r7   c                      e Zd ZddZy)TopK_10Nc                4    t         j                  | |||      S r9   r:   )r-   r.   r/   r	   s       r$   r;   zTopK_10._runX   s     &&tT3T&BBr&   )Nr<   r5   r&   r$   r?   r?   W   r=   r&   r?   c                      e Zd Z	 	 	 ddZy)TopK_11Nc                T    |dvrt        d      t        j                  | ||||      S )a  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>`_.
        )Tr   z.TopK does not implement anything for sorted=0.)r	   r   )r   r(   r1   )r-   r.   r/   r	   r   sorteds         r$   r;   zTopK_11._runh   s4    ( "OPP&&tT3T7&SSr&   )NNNr<   r5   r&   r$   rB   rB   g   s    
 Tr&   rB   )
__future__r   numpyr   onnx.reference.op_runr   r%   r(   r7   r?   rB   r5   r&   r$   <module>rH      sL    #  '&3R.% .&C[ C Ck C Tk Tr&   