U
    qhD                     @  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dkr|dkrt| jd dddf }|dkrtj	| ||d d}|ddd|f }||t
| ||f f }nFtj	|  ||d d}|ddd|f }||t
| ||f  f }| ||f }||fS tj
| |d}tj| |d}|rPt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      N)axisZkthr   )
isinstancenpZndarraysizeRuntimeErrorintlenshapeZarangeZargpartitionZargsortsortZflipZtake)Xkr   largestZsample_rangeZsorted_indicesZsorted_distancesZsorted_valuesZarkZtopk_sorted_indicesZtopk_sorted_values r   >/tmp/pip-unpacked-wheel-xnis5xre/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   )r   r   r   Zastyper
   Zint64)selfdatainkr   r   r   r   Zsortir   r   r   _common_run5   s    z_CommonTopK._common_runN)r   )__name__
__module____qualname__r   r   r   r   r   r   4   s   r   c                   @  s   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   _runH   s    zTopK_1._run)NNr   r   r   r#   r   r   r   r   r    G   s   r    c                   @  s   e Zd ZdddZdS )TopK_10Nc                 C  s   t j| |||dS r!   r"   )r   r   r   r   r   r   r   r#   X   s    zTopK_10._run)Nr$   r   r   r   r   r%   W   s   r%   c                   @  s   e Zd ZdddZdS )TopK_11Nc                 C  s$   |dk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   r#   h   s    zTopK_11._run)NNNr$   r   r   r   r   r&   g   s      r&   )
__future__r   Znumpyr
   Zonnx.reference.op_runr   r   r   r    r%   r&   r   r   r   r   <module>   s   )