
    wi                        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
dS )    )annotationsN)OpRunc                .   t          |t          j                  r&|j        dk    rt	          d|d          |d         }t          |          }t          j        | j                  |         }|r| }t          j        || f|          }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   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_valuess
             j/root/.openclaw/workspace/chatterbox_venv_py311/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z!'""4(H 9Z1D999N&q.tDDDM :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   )lenr   r!   astyper   int64)selfdatainkr	   r   r   sortsortis           r    _common_runz_CommonTopK._common_run&   s[     Fqyytttc$*oo'=0q$HHeell28,,--r"   N)r   )__name__
__module____qualname__r/    r"   r    r$   r$   %   s(        . . . . . .r"   r$   c                      e Zd ZddZdS )TopK_1Nc                @    t                               | ||g|          S r&   r   r$   r/   )r*   r+   r   r	   s       r    _runzTopK_1._run9   s#     &&tTA3T&BBBr"   )NNr0   r1   r2   r9   r3   r"   r    r5   r5   8   .        C C C C C Cr"   r5   c                      e Zd ZddZdS )TopK_10Nc                >    t                               | |||          S r7   r8   )r*   r+   r,   r	   s       r    r9   zTopK_10._runI   s!     &&tT3T&BBBr"   )Nr:   r3   r"   r    r=   r=   H   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$   r/   )r*   r+   r,   r	   r   sorteds         r    r9   zTopK_11._runY   s=    ( ""OPPP&&tT3T7&SSSr"   )NNNr:   r3   r"   r    r@   r@   X   s9        
 T T T T T Tr"   r@   )
__future__r   numpyr   onnx.reference.op_runr   r!   r$   r5   r=   r@   r3   r"   r    <module>rF      s   # " " " " "     ' ' ' ' ' '3 3 34. . . . .% . . .&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"   