
    
Ng                         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dS )    N)OpRunc                    t          |t          j                  r&|j        dk    rt	          d|d          |d         }t          |          }t          | j                  dk    r|dk    rt          j        | j        d                   dddf         }|dk    rMt          j	        | ||dz
            }|ddd|f         }||t          j
        | ||f                   f         }nNt          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              V/var/www/html/ai-engine/env/lib/python3.11/site-packages/onnx/reference/ops/op_topk.pytopk_sorted_implementationr$      s   
 !RZ   6Q;;A1AAABBBaDAA
17||qTQYYy,,QQQW5a<<_QTq1uEEEN+AAArrE2N+bj<+G)HIIINN  _aRdAFFFN+AAArrE2N+bj!L.,H*I)IJJJN \>9://Z---NGAD)))M :d;;;D999
)A,,C'.#DAAA$???222    c                       e Zd ZddZdS )_CommonTopKr   c                     |d         }|dk    r|n|t          |j                  z   }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$   astyper   int64)selfdatainkr   r   r   r   sortis           r#   _common_runz_CommonTopK._common_run5   s[     Fqyytttc$*oo'=0q$HHeell28,,--r%   N)r   )__name__
__module____qualname__r0    r%   r#   r'   r'   4   s(        . . . . . .r%   r'   c                       e Zd ZddZdS )TopK_1Nc                 @    t                               | ||g|          S r)   r
   r'   r0   )r,   r-   r   r   s       r#   _runzTopK_1._runH   s#     &&tTA3T&BBBr%   )NNr1   r2   r3   r:   r4   r%   r#   r6   r6   G   .        C C C C C Cr%   r6   c                       e Zd ZddZdS )TopK_10Nc                 >    t                               | |||          S r8   r9   )r,   r-   r.   r   s       r#   r:   zTopK_10._runX   s!     &&tT3T&BBBr%   )Nr;   r4   r%   r#   r>   r>   W   r<   r%   r>   c                       e Zd Z	 ddZdS )TopK_11Nc                 f    |dvrt          d          t                              | ||||          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'   r0   )r,   r-   r.   r   r   sorteds         r#   r:   zTopK_11._runh   s=     ""OPPP&&tT3T7&SSSr%   )NNNr;   r4   r%   r#   rA   rA   g   s1        9=T T T T T Tr%   rA   )	numpyr   onnx.reference.op_runr   r$   r'   r6   r>   rA   r4   r%   r#   <module>rF      s       ' ' ' ' ' '&3 &3 &3R. . . . .% . . .&C C C C C[ C C C C C C C Ck C C C T T T T Tk T T T T Tr%   