o
    ôjú1  ã                   @   s¸   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lZdd„ ZG dd„ de
ƒZG dd„ deje	ƒZdd„ Zd
S )é    )Útypeof)Útypes)ÚGUFuncBuilder)Úparse_signature)Ú	UfuncBaseÚUfuncLowererBase)Úufunc_find_matching_loop)Ú	serializeÚerrors)Únpydecl)Ú	signatureÚAbstractTemplateNc                    s6   ddl m} G ‡ fdd„d|jƒ}| jˆ j7  _|S )Nr   ©Únpyimplc                       s<   e Zd ZdZ”Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )z(make_gufunc_kernel.<locals>.GUFuncKernelzÊ
        npyimpl._Kernel subclass responsible for lowering a gufunc kernel
        (element-wise function) inside a broadcast loop (which is
        generated by npyimpl.numpy_gufunc_kernel()).
        c                    s6   t ƒ  |||¡ | j |j¡}| j |¡\| _| _d S ©N)ÚsuperÚ__init__ÚdufuncÚ_get_ewise_dtypesÚargsÚfind_ewise_functionÚ	inner_sigÚcres)ÚselfÚcontextÚbuilderÚ	outer_sigÚewise_types©Ú	__class__© úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/numba/np/ufunc/gufunc.pyr      s
   ÿz1make_gufunc_kernel.<locals>.GUFuncKernel.__init__c                    s:   t |tjƒrt |tjƒstƒ  ||j|¡S tƒ  |||¡S r   )Ú
isinstancer   ÚArrayr   ÚcastÚdtype)r   ÚvalÚfromtyÚtotyr   r    r!   r$      s
   
ÿz-make_gufunc_kernel.<locals>.GUFuncKernel.castc                    s4   | j jrd}t |¡‚| j | j jf¡ tƒ j|Ž S )NzCCalling a guvectorize function in object mode is not supported yet.)	r   Ú
objectmoder
   ÚNumbaRuntimeErrorr   Úadd_linking_libsÚlibraryr   Úgenerate)r   r   Úmsgr   r    r!   r-   %   s
   
z1make_gufunc_kernel.<locals>.GUFuncKernel.generate)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r-   Ú__classcell__r    ©Ú_dufuncr   r!   ÚGUFuncKernel   s    r6   )Únumba.npr   Ú_Kernelr/   )r5   r   r6   r    r4   r!   Úmake_gufunc_kernel   s   r9   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚGUFuncLowererzHCallable class responsible for lowering calls to a specific gufunc.
    c                    s"   ddl m} tƒ  |t|j¡ d S )Nr   r   )r7   r   r   r   r9   Znumpy_gufunc_kernel)r   Úgufuncr   r   r    r!   r   4   s
   þzGUFuncLowerer.__init__)r/   r0   r1   r2   r   r3   r    r    r   r!   r:   1   s    r:   c                   @   sª   e Zd ZdZ		d'dd„Zdd„ Zd	d
„ Zedd„ ƒZdd„ Z	d(dd„Z
dd„ Zdd„ Zdd„ Zdd„ Zd(dd„Zdd„ Zedd„ ƒZdd „ Zd!d"„ Zd#d$„ Zd%d&„ ZdS ))ÚGUFunczÊ
    Dynamic generalized universal function (GUFunc)
    intended to act like a normal Numpy gufunc, but capable
    of call-time (just-in-time) compilation of fast loops
    specialized to inputs.
    NFr    c                 C   sv   |d u ri }d | _ d| _|| _|| _t||||||ƒ| _| jjj| _| jjj| _| jj	| _
|  | j
¡ t | |¡ d S )NF)ÚufuncÚ_frozenÚ_is_dynamicÚ	_identityr   Úgufunc_builderÚpy_funcr/   r2   Únb_funcÚ_dispatcherÚ_initializeÚ	functoolsÚupdate_wrapper)r   rB   r   ÚidentityÚcacheÚ
is_dynamicÚtargetoptionsÚwritable_argsr    r    r!   r   C   s   ÿ
zGUFunc.__init__c                 C   s&   |   ¡  |  ¡  t| ƒ| _|  ¡  d S r   )Úbuild_ufuncÚ_install_typer:   Ú	_lower_meÚ_install_cg)r   Ú
dispatcherr    r    r!   rE   X   s   
zGUFunc._initializec                 C   s6   | j }t|j|j| j|j| j|j|j|j	| j
d�	}|S )N)	rB   r   rH   rI   rJ   rK   rL   ÚtypesigsÚfrozen)rA   ÚdictrB   r   r@   rI   r?   rK   rL   Ú_sigsr>   )r   ZgbÚdctr    r    r!   Ú_reduce_states^   s   ÷zGUFunc._reduce_statesc
              	   C   s<   | |||||||d�}
|D ]}|
  |¡ q|
 ¡  |	|
_|
S )N)rB   r   rH   rI   rJ   rK   rL   )ÚaddrM   r>   )ÚclsrB   r   rH   rI   rJ   rK   rL   rR   rS   r   Úsigr    r    r!   Ú_rebuildm   s   þzGUFunc._rebuildc                 C   s   d| j › d�S )Nz<numba._GUFunc 'z'>)r/   ©r   r    r    r!   Ú__repr__y   s   zGUFunc.__repr__c                 C   s@   |du r	| j jj}td| j tft| | jd�ƒ}| | |¡ dS )a*  Constructs and installs a typing class for a gufunc object in the
        input typing context.  If no typing context is given, then
        _install_type() installs into the typing context of the
        dispatcher object (should be same default context used by
        jit() and njit()).
        NZGUFuncTyping_)ÚkeyÚgeneric)	rD   ÚtargetdescrÚtyping_contextÚtyper/   r   rT   Ú_type_meÚinsert_user_function)r   Ú	typingctxÚ_ty_clsr    r    r!   rN   |   s   

þzGUFunc._install_typec                 C   s   | j  |¡ d S r   )rA   rX   )r   Úftyr    r    r!   rX   Š   s   z
GUFunc.addc                 C   s   | j  ¡ | _| S r   )rA   rM   r=   r\   r    r    r!   rM   �   s   zGUFunc.build_ufuncc                 C   s0   t | jjƒ}ttt|d ƒƒttt|d ƒƒfS ©Nr   é   )r   rA   r   ÚtupleÚmapÚlen)r   Ú
parsed_sigr    r    r!   Úexpected_ndims‘   s   $zGUFunc.expected_ndimsc                 C   s€  |rJ ‚| j }| jj}|  ¡ \}}|| }t|ƒsJ t|ƒƒ‚t|ƒD ]F\}}	t|	tjƒri|	j	|| k ri|t|ƒk r<dnd}
|t|ƒk rF|n|t|ƒ }| j
› d|
› d|› d|	j	› d|› d|| › d�}t |¡‚q#tj |||¡}|\}}}}|  |¡\}}|d	u r·| jr“d
| › d|› �}t |¡‚|  |¡ |  |¡\}}|dkr±d| j
› d|› �}t |¡‚|d	us·J ‚ttjg|¢R Ž S )zì
        Implement AbstractTemplate.generic() for the typing class
        built by gufunc._install_type().

        Return the call-site signature after either validating the
        element-wise signature or compiling for it.
        ÚInputZOutputz: z	 operand z& does not have enough dimensions (has z, gufunc core with signature z
 requires ú)Nzcannot call z with types ©NNzFail to compile )r=   rA   r   rn   rl   Ú	enumerater"   r   r#   Úndimr/   r
   ÚTypingErrorr   ÚNumpy_rules_ufuncÚ_handle_inputsr   r>   Ú_compile_for_argtysÚnone)r   ÚargtysÚkwsr=   rZ   Z	inp_ndimsZ	out_ndimsÚndimsÚidxÚargÚkindÚir.   Ú_handle_inputs_resultr   Ú_r    r    r!   rc   •   sH   ÿþþÿ
€ÿ


zGUFunc._type_mec                 C   s   | j |Ž }| j |¡ d S r   )Ú_get_function_typerA   rX   )r   ry   Úreturn_typeÚfntyr    r    r!   rw   Ä   s   
zGUFunc._compile_for_argtysc                 C   s   |   |j¡}t|ƒt|ƒkS r   )r   r   rj   )r   r   rZ   Údtypesr    r    r!   Úmatch_signatureË   s   zGUFunc.match_signaturec                 C   s   | j S r   )r?   r\   r    r    r!   rJ   Ï   s   zGUFunc.is_dynamicc                 C   sD   t dd„ |ƒ}g }|D ]}t|tjƒr| |j¡ q| |¡ q|S )Nc                 S   s   t | tjƒr| S t| ƒS r   )r"   r   ÚTyper   )r}   r    r    r!   Ú<lambda>Ô   s    z*GUFunc._get_ewise_dtypes.<locals>.<lambda>)rk   r"   r   r#   Úappendr%   )r   r   ry   ÚtysÚargtyr    r    r!   r   Ó   s   ÿzGUFunc._get_ewise_dtypesc                 G   s,   t | jjƒ}t|ƒt|d ƒt|d ƒ kS rh   )r   rA   r   rl   )r   r   rm   r    r    r!   Ú_num_args_matchÞ   s    zGUFunc._num_args_matchc                 G   sÀ   t | jjƒ}|  |¡}g }t|d ƒD ] \}}t|ƒ}|dkr'| || ¡ q| t || |d¡¡ qt|d ƒ}t|d ƒD ]\}}|||  }	t|ƒpOd}
| t |	|
d¡¡ q@tj	|Ž S )Nr   ÚAri   )
r   rA   r   r   rr   rl   r‰   r   r#   rx   )r   r   rm   r   Úlr|   Zsig_dimrs   ÚoffsetÚrettyZret_ndimr    r    r!   r‚   â   s   

zGUFunc._get_function_typec                 O   sÜ   | j s| js%|rt|d ƒr|d j| dg|¢R i |¤ŽS | j|i |¤ŽS d|v r1|| d¡f7 }| j|Ž du rCd| j› d�}t|ƒ‚|  	|¡}| jrQt
| j|ƒsf|  |¡dksb| j|Ž }|  |¡ |  ¡  | j|i |¤ŽS )Nr   Ú__call__ÚoutFz Too few arguments for function 'z'. Note that the pattern `out = gufunc(Arg1, Arg2, ..., ArgN)` is not allowed. Use `gufunc(Arg1, Arg2, ..., ArgN, out) instead.rq   )r>   rJ   Ú_is_array_wrapperÚ__array_ufunc__r=   ÚpoprŒ   r/   Ú	TypeErrorr   r   r   r‚   rX   rM   )r   r   Úkwargsr.   ZewiserZ   r    r    r!   r‘   ú   s.   ÿÿÿÿ


zGUFunc.__call__)NNFNr    r   )r/   r0   r1   r2   r   rE   rW   Úclassmethodr[   r]   rN   rX   rM   rn   rc   rw   r†   ÚpropertyrJ   r   rŒ   r‚   r‘   r    r    r    r!   r<   ;   s.    
ÿ


/
r<   c                 C   s    t | tƒ ot| dƒot| dƒS )ad  Return True if obj wraps around numpy or another numpy-like library
    and is likely going to apply the ufunc to the wrapped array; False
    otherwise.

    At the moment, this returns True for

    - dask.array.Array
    - dask.dataframe.DataFrame
    - dask.dataframe.Series
    - xarray.DataArray
    - xarray.Dataset
    - xarray.Variable
    - pint.Quantity
    - other potential wrappers around dask array or dask dataframe

    We may need to add other libraries that pickle ufuncs from their
    __array_ufunc__ method in the future.

    Note that the below test is a lot more naive than
    `dask.base.is_dask_collection`
    (https://github.com/dask/dask/blob/5949e54bc04158d215814586a44d51e0eb4a964d/dask/base.py#L209-L249),  # noqa: E501
    because it doesn't need to find out if we're actually dealing with
    a dask collection, only that we're dealing with a wrapper.
    Namely, it will return True for a pint.Quantity wrapping around a plain float, a
    numpy.ndarray, or a dask.array.Array, and it's OK because in all cases
    Quantity.__array_ufunc__ is going to forward the ufunc call inwards.
    Z__dask_graph__r”   )r"   rb   Úhasattr)Úobjr    r    r!   r“   %  s
   ÿýr“   )Únumbar   Ú
numba.corer   Znumba.np.ufunc.ufuncbuilderr   Únumba.np.ufunc.sigparser   Únumba.np.ufunc.ufunc_baser   r   Únumba.np.numpy_supportr   r	   r
   Únumba.core.typingr   Únumba.core.typing.templatesr   r   rF   r9   r:   ÚReduceMixinr<   r“   r    r    r    r!   Ú<module>   s    $
 k