
    sg!                        d dl Z d dlZd dlmZmZ d dlmZ d dl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mZmZmZmZmZmZmZmZ d dlmZ eeeeeeeefZeeeeefZdgerdgng z   Z G d	 d
e      Z d Z!d Z"d Z#d Z$d Z%ddZ&d Z'y)    N)partialwraps)Sequence)	TEST_CUDA)_dispatch_dtypes	all_typesall_types_andall_types_and_complexall_types_and_complex_andall_types_and_halfcomplex_typesfloating_and_complex_typesfloating_and_complex_types_andfloating_typesfloating_types_andfloating_types_and_halfintegral_typesintegral_types_and)torch_to_numpy_dtype_dictcpucudac                       e Zd Zy)_dynamic_dispatch_dtypesN)__name__
__module____qualname__     W/var/www/html/venv/lib/python3.12/site-packages/torch/testing/_internal/opinfo/utils.pyr   r   6   s    r   r   c                 4   |dv sJ t         s%|dk(  r t        j                  d       t        d      S t	               }t        t        j                  t        j                  t        j                        D ]V  }	  || ||d      }d}|D ]-  }	  | |j                  g|j                  i |j                   / |sF|j                  |       X t        |      S # t        $ r t        j                  d| d|        Y w xY w# t        $ r}d}Y d }~ \d }~ww xY w)	N)r   r   r   zGWARNING: CUDA is not available, empty_dtypes dispatch will be returned!r   Fz.WARNING: Unable to generate sample for device:z and dtype:T)r   warningswarnr   setr   torchboolbfloat16halfRuntimeErrorinputargskwargsadd)	opsample_inputs_fndevice_typesupported_dtypesdtypesamples	supportedsampleres	            r   get_supported_dtypesr6   ;   s    /))).U	
 (++u*5::u~~uzzR (
	&r;uEG 	 	F6<<?&++??	   '3(6 $$4551  	
 MM@[Y^X_` 	    !	s*   7C
*D$C>=C>	D
DDc                 p   t        j                  dd      }t        |       dk(  r	 |dd      S t        |       }t        D ]-  }t         |             |k(  s |||j
                  dz         c S  d }d}t        D ]9  }t         |             }|j                  |      s%t        |      }||kD  s6|}|}; | |dt        |             S  |t        gt        t        |       t         |             z
         |j
                  t        t        t        |       t         |             z
              z         S )Nreturn_typezdispatch_fn dispatch_fn_strr   r   z()g        )collections
namedtuplelenr#   COMPLETE_DTYPES_DISPATCHr   EXTENSIBLE_DTYPE_DISPATCHissubsetstrr   tuple)dtypesr8   
set_dtypesdispatchchosen_dispatchchosen_dispatch_scoredispatch_dtypesscores           r   dtypes_dispatch_hintrH   c   s6    ((8UVK 6{a2t$$VJ, Cxz?j(x):):T)ABBC
 O- 'hj/''
3O$(($)!&O' 2s6{++@5Vs8:!>?@Cc&kC
O&C DEE r   c                     | j                   S N)dynamic_dtypes)r-   s    r   is_dynamic_dtype_setrL      s     r   c                     d| j                    dt        | j                        j                   dt        | j                        j                   d}|S )Nz
        OpInfo(z,
               dtypes=z,
               dtypesIfCUDA=z,
        )
        )namerH   rA   dispatch_fn_strdtypesIfCUDA)r-   fmt_strs     r   str_format_dynamic_dtyperR      sY    y +BII6FFG H1"//BRRS T	G Nr   c                 8     d t                fd       }|S )Nc                     | t         j                  t        t         j                  t         j                  t         j
                  t         j                  t         j                  fv S rJ   )npbool_r%   uint8int8int16int32int64)r1   s    r   is_integralz=np_unary_ufunc_integer_promotion_wrapper.<locals>.is_integral   sA    HHHHGGHHHHHH
 
 	
r   c                     t         t        j                            } | j                        r | j	                  |            S  |       S rJ   )r   r$   get_default_dtyper1   astype)xnp_dtypefnr\   s     r   
wrapped_fnz<np_unary_ufunc_integer_promotion_wrapper.<locals>.wrapped_fn   sA     -U-D-D-FGqwwahhx())!ur   )r   )rb   rc   r\   s   ` @r   (np_unary_ufunc_integer_promotion_wrapperrd      s(    	
 2Y  r   c                 T     t               dt        j                  f fd       }|S )aA  Wraps a NumPy reduction operator.

    The wrapper function will forward dim, keepdim, mask, and identity
    kwargs to the wrapped function as the NumPy equivalent axis,
    keepdims, where, and initiak kwargs, respectively.

    Args:
        f: NumPy reduction operator to wrap
        supports_keepdims (bool, optional): Whether the NumPy operator accepts
            keepdims parameter. If it does not, the wrapper will manually unsqueeze
            the reduced dimensions if it was called with keepdim=True. Defaults to True.

    Returns:
        Wrapped function

    r`   c                 z   t        |j                               }|j                  dd       }|j                  dd      }d|v r;t        |t              rt        |      n|}| j                  dk(  r
|dv rd |d<   n||d<   d|v r
r||d<   d|v rS|j                  d      }|@|j                  t        j                  k(  sJ |j                         j                         |d	<   d
|v r|j                  d
      }|m|j                  t        j                  u r.|j                         j                  t        j                        }n|j                         }|j                         |d<    	| g|i |}|rI
sG| j                  dkD  r8|t!        t#        | j                              n|}t%        j&                  ||      }|S )NdimkeepdimFr   >   r   r   rk   axiskeepdimsmaskwhereidentityinitial)r#   keyspop
isinstancer   r@   ndimlayoutr$   stridedr   numpyr1   r&   tofloat32listrangerU   expand_dims)r`   r*   r+   rr   rg   rh   rn   rp   resultfsupports_keepdimss            r   wrapperz*reference_reduction_numpy.<locals>.wrapper   s    6;;=!jj%**Y.D= *3 9%*sC vv{s&::!%v!$v!2!(F:T>::f%D{{emm333"&((*"2"2"4wzz*-H#>>U^^3'||~00?H'||~H$,NN$4y!1&t&v& ,!),$uQVV}%#C^^FC0Fr   )r   rU   ndarray)r   r   r   s   `` r   reference_reduction_numpyr      s.    $ 1X*2:: * *X Nr   c                    d|vrt        j                  | j                  t         j                        r | j	                  t         j
                        } nMt        j                  | j                  t         j                        r| j	                  t         j                        } t        t         j                        } || g|i |S )aR  
    The function will call np.prod with type as np.int64 if the input type
    is int or uint64 if is uint. This is necessary because windows np.prod uses by default
    int32 while on linux it uses int64.
    This is for fixing integer overflow https://github.com/pytorch/pytorch/issues/77320

    Returns:
        np.prod of input
    r1   )
rU   
issubdtyper1   signedintegerr_   r[   unsignedintegeruint64r   prod)ar*   r+   rb   s       r   
prod_numpyr      s     f==""2"23"A]]177B$6$67#A	"277	+Ba!$!&!!r   )T)(r9   r!   	functoolsr   r   typingr   rx   rU   r$   #torch.testing._internal.common_cudar   $torch.testing._internal.common_dtyper   r   r	   r
   r   r   r   r   r   r   r   r   r   r   $torch.testing._internal.common_utilsr   r<   r=   DEVICESr   r6   rH   rL   rR   rd   r   r   r   r   r   <module>r      s      $    9     K 	  "  'fX
3	/ 	
%6P#L!H?D"r   