o
    toi                     @  sl   d dl mZ d dlZd dlmZ dd ZG dd deZG dd	 d	eZG d
d deZ	G dd deZ
dS )    )annotationsN)OpRunc           
      C  s   t |tjr|jdkrtd|d|d }t|}t| j| }|r(| }tj|| f|d}tj	| ||d}|rItj
||d}tj
||d}t|}tj|||d}tj|||d}	|	|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   axis)
isinstancenpndarraysizeRuntimeErrorintindicesshapelexsorttake_along_axisfliparangetake)
Xkr   largestind_axissorted_indicessorted_valuesarktopk_sorted_indicestopk_sorted_values r   N/home/ubuntu/.local/lib/python3.10/site-packages/onnx/reference/ops/op_topk.pytopk_sorted_implementation   s"   

r    c                   @  s   e Zd ZdddZdS )_CommonTopKr   c                 C  sD   |d }|dkr
|n|t |j }t||||\}}||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   )lenr   r    astyper	   int64)selfdatainkr   r   r   sortsortir   r   r   _common_run&   s   z_CommonTopK._common_runN)r   )__name__
__module____qualname__r+   r   r   r   r   r!   %       r!   c                   @     e Zd ZdddZdS )TopK_1Nc                 C  s   t j| ||g|dS r"   r   r!   r+   )r&   r'   r   r   r   r   r   _run9   s   zTopK_1._run)NNr,   r-   r.   r4   r   r   r   r   r1   8   r/   r1   c                   @  r0   )TopK_10Nc                 C  s   t j| |||dS r2   r3   )r&   r'   r(   r   r   r   r   r4   I   s   zTopK_10._run)Nr5   r   r   r   r   r6   H   r/   r6   c                   @  s   e Zd Z			dddZdS )TopK_11Nc                 C  s$   |dvrt dtj| ||||d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!   r+   )r&   r'   r(   r   r   sortedr   r   r   r4   Y   s   zTopK_11._run)NNNr5   r   r   r   r   r7   X   s
    r7   )
__future__r   numpyr	   onnx.reference.op_runr   r    r!   r1   r6   r7   r   r   r   r   <module>   s   