U
    ©Ê¹j'4  ã                   @   s†  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlZd dlmZ d dlmZmZmZmZmZmZ d dlmZmZ e
eejedœdd	„ƒZe
eejedœd
d	„ƒZe
eejedœdd	„ƒZe
dejddœdd	„ƒZdd	„ Zeeedœdd„Zdd„ ZG dd„ dƒZeedœdd„Z eedœdd„Z!ddœeee"dœdd„Z#ddœe$e"dœd d!„Z%d"d#„ Z&eed$œd%d&„Z'dS )'é    N)Úwraps)Úchain)ÚCallableÚ
NamedTupleÚOptionalÚoverloadÚSequenceÚTuple)ÚELEMENTWISE_TYPE_PROMOTION_KINDÚNumberÚ
NumberTypeÚ	ShapeTypeÚ
TensorLikeÚTensorLikeType)Útree_flattenÚtree_unflatten)ÚaÚdtypeÚreturnc                 C   s   d S ©N© ©r   r   r   r   ú[/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/_prims_common/wrappers.pyÚ_maybe_convert_to_dtype   s    r   c                 C   s   d S r   r   r   r   r   r   r      s    c                 C   s   d S r   r   r   r   r   r   r      s    c                 C   s   d S r   r   r   r   r   r   r   $   s    c                    s~   t | tƒr"| jˆ kr|  ˆ ¡S | S t | tƒr:t ˆ ¡| ƒS t | tƒrZt‡ fdd„| D ƒƒS | d krfd S t	dt
| ƒ› d�ƒ‚d S )Nc                 3   s   | ]}t |ˆ ƒV  qd S r   ©r   ©Ú.0Úx©r   r   r   Ú	<genexpr>2   s     z*_maybe_convert_to_dtype.<locals>.<genexpr>zReceived type z& that is neither a tensor or a number!)Ú
isinstancer   r   Útor   ÚutilsZdtype_to_type_ctorr   ÚtupleÚ
ValueErrorÚtyper   r   r   r   r   *   s    




)r   Útypr   c                 C   s^   t | tƒs"dt| ƒ› d�}t|ƒ‚t t| ƒ|¡sVd| › dt| ƒ› d|› d�}t|ƒ‚|| ƒS )NzFound unknown type z  when trying to convert scalars!zScalar z	 of type z cannot be safely cast to type ú!)r    r   r%   r$   r"   Zis_weakly_lesser_type)r   r&   Úmsgr   r   r   Ú_maybe_convert_to_type;   s    
r)   c                 C   s4   t |dƒr,|jD ]}t| |d�r dS qdS | |kS )NÚ__args__)r&   Ú
annotationTF)Úhasattrr*   Ú_annotation_has_type)r&   r+   r   r   r   r   r-   F   s    

r-   c                   @   s>   e Zd ZdZddœeeee  dœdd„Ze	e	dœdd	„Z
dS )
Ú"elementwise_type_promotion_wrappera°  
    Adds elementwise type promotion to a Python reference implementation.

    Takes two kwargs, type_promoting_args and type_promotion_kind.

    type_promoting_args must be a string Sequence specifiying the argument names of all
    arguments that participate in type promotion (and should be type promoted). If the
    arg specifies a Sequence-type then every element of the Sequence will participate in
    type promotion.

    type_promotion_kind must be one of the kinds specified by ELEMENTWISE_TYPE_PROMOTION_KIND.
    See its documentation for details.

    Other type promotion behavior, like validating the Python type of scalar arguments, must
    be handled separately.
    N)Útype_promoting_args)Útype_promotion_kindr/   c                C   s   || _ || _d S r   )Útype_promoting_arg_namesr0   )Úselfr0   r/   r   r   r   Ú__init__b   s    z+elementwise_type_promotion_wrapper.__init__©Úfnr   c                    s,   t  ˆ ¡‰tˆ ƒ‡ ‡‡fdd„ƒ}ˆ|_|S )Nc                     s¾   ˆj | |Ž‰ t‡ fdd„ˆjD ƒƒ}t|ƒd }tj|dˆjiŽ\‰‰‡ ‡fdd„ˆjD ƒ}ˆ j |¡ ˆf ˆ jŽ}t	|t
ƒrˆt|ˆƒS t	|tƒr¨t‡fdd„|D ƒƒS tdt|ƒ› �ƒ‚d S )	Nc                 3   s&   | ]}|ˆ j  ¡ krˆ j | V  qd S r   )Ú	argumentsÚkeysr   )Úboundr   r   r   q   s   þzKelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<genexpr>r   r0   c                    s,   i | ]$}|ˆ j  ¡ kr|tˆ j | ˆƒ“qS r   )r6   r7   r   r   )r8   Úcompute_dtyper   r   Ú
<dictcomp>}   s   þ zLelementwise_type_promotion_wrapper.__call__.<locals>._fn.<locals>.<dictcomp>c                 3   s   | ]}t |ˆ ƒV  qd S r   r   r   )Úresult_dtyper   r   r   ‰   s     zUnhandled result type: )Úbindr#   r1   r   r"   Zelementwise_dtypesr0   r6   Úupdater    r   r   r   ÚAssertionErrorr%   )ÚargsÚkwargsr/   Zflattened_type_promoting_argsZpromoted_argsÚresult©r5   r2   Úsig)r8   r9   r;   r   Ú_fnn   s(    þÿþ
þ


z8elementwise_type_promotion_wrapper.__call__.<locals>._fn©ÚinspectÚ	signaturer   Ú__signature__)r2   r5   rD   r   rB   r   Ú__call__k   s
    
z+elementwise_type_promotion_wrapper.__call__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   Ústrr3   r   rI   r   r   r   r   r.   P   s   ü
ü	r.   ©ÚoutÚshapec                 C   s>   t  | j|¡rdS |  ¡ dkr:dt| jƒ› d�}t |¡ dS )NFr   zCAn output with one or more elements was resized since it had shape a   which does not match the required output shape {str(shape)}. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0).T)r"   Z
same_shaperQ   ZnumelrN   ÚwarningsÚwarn)rP   rQ   r(   r   r   r   Ú_resize_output_check‘   s    ÿ
rT   c                 C   s   t | |ƒr|  |¡S | S d S r   )rT   Zresize_rO   r   r   r   Ú_maybe_resize_out¢   s    

rU   F)Úexact_dtype©Ú	copy_fromÚcopy_torV   c                    sv   ˆ j ˆj kr$d ˆ j ˆj ¡}t|ƒ‚|rHt ˆ jˆjk‡ ‡fdd„¡ n$t tjˆ jˆjd�‡ ‡fdd„¡ ˆ ˆ ¡S )NzXAttempting to copy from device {} to device {}, but cross-device copies are not allowed!c                      s   dˆ j › dˆj › d�S )Nz"Expected out tensor to have dtype z	 but got z insteadr   r   ©rX   rY   r   r   Ú<lambda>·   ó    z _safe_copy_out.<locals>.<lambda>)Z	cast_fromZcast_toc                      s   dˆ j › dˆj › d�S )NzAttempting to cast from z to out tensor with dtype z0, but this can't be cast because it is not safe!r   r   rZ   r   r   r[   ½   r\   )	ÚdeviceÚformatÚRuntimeErrorÚtorchÚ_checkr   r"   Zcan_safe_cast_toZcopy_)rX   rY   rV   r(   r   rZ   r   Ú_safe_copy_out©   s      ÿ
þþrb   )Ú	out_namesrV   c                    s<   t ˆƒdk‰ˆs t ˆƒdks t‚ttdœ‡ ‡‡fdd„}|S )Nr   é   r4   c              	      s   ˆrt nttdd„ ttˆƒƒD ƒƒ }ˆr.t ntdˆj› �dd„ ˆD ƒƒ‰t ˆ¡‰d‰ t	‡fdd„ˆ D ƒƒ‰t
ˆƒdd	œ‡‡ ‡‡‡‡‡fd
d„
ƒ}tjdtjjd|d�}ˆjˆj|fks¾t‚tˆj ¡ |fƒ}tj|ˆd�|_ˆj|_||jd< ˆ|jd< |S )z?
        Adds the out parameter to a Python reference.
        c                 s   s   | ]
}t V  qd S r   ©r   )r   Ú_r   r   r   r   Ï   s     z4out_wrapper.<locals>._out_wrapper.<locals>.<genexpr>Zreturn_types_c                 S   s   g | ]}|t f‘qS r   re   )r   Úor   r   r   Ú
<listcomp>Õ   s     z5out_wrapper.<locals>._out_wrapper.<locals>.<listcomp>)r]   r   c                 3   s   | ]}|ˆ j kV  qd S r   )Ú
parameters)r   Úp)rC   r   r   r   Û   s     N)rP   c                    s  ˆr0ˆ d k	r0ˆD ]}t ˆ |ƒ}||kr|||< qˆ||Ž‰tˆtƒrHˆsftˆtƒrbtˆƒtˆƒksft‚ˆ d k	rüˆrœtˆ tƒs€t‚tˆ ˆjƒ tˆˆ ˆd� n^tˆ tƒsªt‚t	 
tˆ ƒtˆƒk‡ ‡fdd„¡ tˆˆ ƒD ]"\}}t||jƒ t||ˆd� qÖnˆ‰ ˆ�r
ˆ S ˆˆ Ž S )NrW   c                      s   dt ˆƒ› dt ˆ ƒ› �S )Nzexpected tuple of z elements but got )Úlenr   ©rP   rA   r   r   r[     r\   z@out_wrapper.<locals>._out_wrapper.<locals>._fn.<locals>.<lambda>)Úgetattrr    r   r	   rk   r>   rU   rQ   rb   r`   Ú_check_typeÚzip)rP   r?   r@   ÚkZout_attrÚrrg   )rV   Úfactory_kwargsr5   Úis_factory_fnÚ	is_tensorrc   Úreturn_typerl   r   rD   Ý   s:    


ÿþýüþz.out_wrapper.<locals>._out_wrapper.<locals>._fnrP   )ÚkindÚdefaultr+   )ri   Úreturn_annotationr   )r   r	   r#   Úrangerk   r   rJ   rF   rG   Úallr   Ú	ParameterÚKEYWORD_ONLYrx   Úemptyr>   r   ri   ÚvaluesÚ	SignaturerH   Ú__annotations__)r5   Zout_typerD   Z	out_paramÚparams©rV   rt   rc   )rr   r5   rs   ru   rC   r   Ú_out_wrapperÈ   s@    ÿýÿ
 ÿý
 3ü ÿ

z!out_wrapper.<locals>._out_wrapper)rk   r>   r   )rV   rc   rƒ   r   r‚   r   Úout_wrapperÄ   s    Zr„   c                    s>   ‡fdd„‰G ‡fdd„dt jjƒ‰ tˆƒ‡ ‡fdd„ƒ}|S )Nc              
      s>   t j ¡ �* t j t jjj¡}ˆ | |ŽW  5 Q R £ S Q R X d S r   )r`   Z_CZ_AutoDispatchBelowAutogradZ&_dispatch_tls_is_dispatch_key_excludedZDispatchKeyZADInplaceOrView)r?   r@   Úold)Úprimr   r   Úredispatch_prim&  s
    ÿz0backwards_not_supported.<locals>.redispatch_primc                       s(   e Zd Ze‡ fdd„ƒZedd„ ƒZdS )z6backwards_not_supported.<locals>.BackwardsNotSupportedc                    s   t ||ƒ\}}ˆ ||ƒS r   )r   )ÚctxÚ	args_specÚ	flat_argsr?   r@   ©r‡   r   r   Úforward.  s    z>backwards_not_supported.<locals>.BackwardsNotSupported.forwardc                 W   s   t dƒ‚d S )Nzbackwards not supported on prim)r_   )rˆ   r?   r   r   r   Úbackward3  s    z?backwards_not_supported.<locals>.BackwardsNotSupported.backwardN)rJ   rK   rL   ÚstaticmethodrŒ   r�   r   r‹   r   r   ÚBackwardsNotSupported-  s   r�   c                     sH   t | |fƒ\}}t ¡ r:tdd„ |D ƒƒr:ˆ j|f|žŽ S ˆ| |ƒS d S )Nc                 s   s    | ]}t |tjƒr|jV  qd S r   )r    r`   ÚTensorZrequires_grad)r   r   r   r   r   r   :  s     zBbackwards_not_supported.<locals>._autograd_impl.<locals>.<genexpr>)r   r`   Zis_grad_enabledÚanyÚapply)r?   r@   rŠ   r‰   )r�   r‡   r   r   Ú_autograd_impl7  s    ÿz/backwards_not_supported.<locals>._autograd_impl)r`   ZautogradÚFunctionr   )r†   r“   r   )r�   r†   r‡   r   Úbackwards_not_supported%  s
    
r•   r4   c                    s(   t  ˆ ¡}tˆ ƒ‡ fdd„ƒ}||_|S )zQ
    Allows unary operators that accept tensors to work with Python numbers.
    c                     sv   t | ƒdkrlt| d tƒrlt t| d ƒ¡}t| ƒ}tj| d |d�|d< ˆ ||Ž}t|tj	ƒsdt
‚| ¡ S ˆ | |ŽS )Nr   r   )rk   r    r   r"   Ztype_to_dtyper%   Úlistr`   Ztensorr�   r>   Úitem)r?   r@   r   Zargs_rA   ©r5   r   r   rD   W  s    
z-elementwise_unary_scalar_wrapper.<locals>._fnrE   )r5   rC   rD   r   r˜   r   Ú elementwise_unary_scalar_wrapperQ  s
    
r™   )(rF   rR   Ú	functoolsr   Ú	itertoolsr   Útypingr   r   r   r   r   r	   r`   Ztorch._prims_commonZ_prims_commonr"   r
   r   r   r   r   r   Ztorch.utils._pytreer   r   r   r   r%   r)   r-   r.   rT   rU   Úboolrb   rN   r„   r•   r™   r   r   r   r   Ú<module>   s>     
Aÿ  ÿa,