
    Vh                      U d dl mZ d dlZd dlZd dlZ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mZmZmZmZmZmZmZ d dlmZmZ d dlZd d	lmZmZmZ er#d dlZ G d
 dej<                        Z ede      Z eejB                  e"e#   e$e#df   f   Z%de&d<   ee"e#   e$e#df   f   Z'de&d<   ee#e"e#   e$e#df   f   Z(de&d<   ee"e#   e$e#df   f   Z)de&d<   ee*e+   e*e#   e*e,   e*e-   f   Z.de&d<   ee+e#e,e-f   Z/de&d<   ee+e#e,f   Z0de&d<   e+e#e,e-ejb                  ejd                  ejf                  fZ4e#Z5e#ejb                  fZ6e,ejd                  fZ7e+ejf                  fZ8e#Z9e,Z:ee;ejx                  e#f   Z=de&d<   ej|                  Z>ejx                  ej~                  ej4                  ej6                  ej8                  ej                  ej                  ej                  ej|                  j                  ej|                  j                  j                  ej|                  j                  ej|                  j                  ej|                  j                  ej|                  j                  ej|                  j                  j                  ej|                  j                  j                  ej|                  j                  j                  ej|                  jx                  j                  ej|                  j                  j                  ej|                  j                  j                  ej|                  j                  ej|                  j                  ej|                  j                  ej|                  j                  j                  ej|                  j                  hZSej|                  ZTej|                  ZUee"eT   e$eTdf   f   ZVde&d<   eeTe/f   ZWde&d<   dZXddddZYd ZZ	 	 dddd 	 	 	 dd!Z[dddd"	 	 	 	 	 dd#Z\ddd$	 	 	 	 	 dd%Z]dd&	 	 	 	 	 dd'Z^dd(ZOdd)Z_dd*Z`ej                  ej                  ej                  ej                  hZedd+Zf	 	 	 	 	 	 dd,Zgdd-Zhdd.Zidd/	 dd0Zjdd1Zkd2 Zld3 Zmdd4Zndd5Zodd6Zpdd7Zqdd8Zrdd9Zsddd:Zte	 d	 	 	 	 	 	 	 dd;       Zueddd<       Zudd=Zudd>Zvdd?Zwdd@ZxdA ZyddBZzddCZ{ddDZ|	 	 	 	 ddEZ}	 d	 	 	 ddFZ~ddGZddHZej                  ej                  ej                  ej                  ej
                  ej                  ej                  ej                  fZej                  ej                  ej                  fZej                  ej                  ej                  fZddIZddJZddKZddLZddMZddNZej                  ej.                  ej                  ej0                  ej                  ej                  iZej                  ej                  ej                  ej                  ej0                  ej                  ej.                  ej                  iZddOZddPZddQZddRZddSZddTZe+e#e,e-fZ	 d	 	 	 	 	 ddUZdddVZddWZ	 	 	 	 	 	 ddXZddYZddZZdd[Zdd\Zd] Zej                  ej0                  ej                  ej0                  ej                  ej                  iZdd^Zej                  ej.                  ej                  ej.                  ej0                  ej.                  ej                  ej                  ej                  ej                  iZdd_Z G d` dae
      Z G db dce
      Z G dd dee
      Z	 	 	 	 ddfZddgZ	 	 	 	 ddhZ	 d	 	 	 	 	 ddiZ	 d	 	 	 	 	 ddjZ	 	 	 	 ddkZ	 	 	 	 ddlZ	 	 	 	 ddmZ	 	 	 	 ddnZ	 	 	 	 	 	 ddoZddpZddqZ	 	 d	 	 	 	 	 ddrZ	 	 	 	 	 	 	 	 ddsZ	 	 	 	 	 	 	 	 ddtZ eduev      ef	 	 	 	 	 	 	 ddw       Z	 	 	 	 	 	 ddxZddyZddzZdd{Zdd|Zdd}Zdd~ZddZddZd ZddZ G d d      Zy)    )annotationsN)Sequence)nullcontext)Enum)reduce)	AnyCallablecast
NamedTupleOptionaloverloadTYPE_CHECKINGTypeVarUnion)
deprecated	TypeAlias)	sym_floatsym_intsym_maxc                  ,    e Zd ZddZddZddZddZy)_WorksWithIntc                     y N selfothers     L/home/dcms/DCMS/lib/python3.12/site-packages/torch/_prims_common/__init__.py__add__z_WorksWithInt.__add__$           c                     y r   r   r   s     r   __radd__z_WorksWithInt.__radd__'   r    r!   c                     y r   r   r   s     r   __mul__z_WorksWithInt.__mul__*   r    r!   c                     y r   r   r   s     r   __rmul__z_WorksWithInt.__rmul__-   r    r!   N)r   r   returnztyping.Self)__name__
__module____qualname__r   r#   r%   r'   r   r!   r   r   r   #   s    				r!   r   	_IntLikeT)bound.r   	ShapeType
StrideTypeDimsTypeDimsSequenceTypeNumberTypeType
NumberTypeRealNumberTypeDeviceLikeTypeTensorSequenceTypeTensorOrNumberLikeType__custom_out_param__Fallow_rhs_unbackedc                   ddl m} t        |       t        |      k7  ryt        | |      D ]/  \  }}|rt	        |t
        j                        r# |||k7        s/ y y)Nr   guard_size_obliviousFT)%torch.fx.experimental.symbolic_shapesr=   lenzip
isinstancetorchSymInt)abr:   r=   xys         r   
same_shaperH   q   s]    J
1vQAq	 1 !U\\*  Q' r!   c                    | t         j                  u rt        S | t         j                  u rt        S | t         j
                  u rt        S | S r   )rB   SymFloatfloatrC   intSymBoolbool)ts    r   _maybe_get_pytyperP      s:    ENN	
ell	
	
emm	r!   T)r:   
check_conjc                  ddl m} t        | t              sJ t        |t              sJ |rGt	        | j
                  |j
                  |      s%d| j
                   d|j
                   d} ||      | j                  |j                  k7  r%d| j                   d|j                   d} ||      | j                  |j                  k7  rt        | j                        dk(  st        | j                        d	k(  r1t        |j                        dk(  st        |j                        d	k(  rn%d
| j                   d|j                   d} ||      |rt        | ||      \  }}	|s0d| j                          d|j                          d|	 d} ||      | j                         |j                         k7  r-d| j                          d|j                          d} ||      |rK| j                         |j                         k7  r* |d| j                          d|j                                | j                         |j                         k7  r* |d| j                          d|j                                y)z
    Checks that two tensor likes have the same shape,
    dtype and device.

    In the future this will validate additional metadata, like
    strides.
    r   )MetadataMismatchErrorr9   zShapes z and z are not equal!zDtypes zcuda:0cudazDevices zStride mismatch! Strides are z (mismatched at z)!z-Storage offset mismatch! Storage offsets are !z!Conj mismatch! is_conj is set to zNeg mismatch! is_neg is set to N)torch._subclasses.fake_tensorrS   rA   
TensorLikerH   shapedtypedevicestrcheck_significant_stridesstridestorage_offsetis_conjis_neg)
rD   rE   check_sizescheck_stridesr:   rQ   rS   msgsame_stridesidxs
             r   compare_tensor_metarf      sF     Da$$$a$$$:	-? yaggYo>#C((ww!''yaggYo>#C((xx188 MX%QXX&)@MX%QXX&)@QXXJeAHH:_EC',, 5q%7
c 1!((*U188:,N^_b^ccefC',,!1!1!33A!BRBRBTAUUZ[\[k[k[mZnnopC',,99;!))+%'3AIIK=aiik]S  	xxzQXXZ#-ahhj\qxxzlK
 	
  r!   	only_cudasignificant_onlyr:   c                  |r2| j                   j                  dk(  s|j                   j                  dk(  r| j                         dkD  rt        | j                        D ]u  }| xs | j
                  |   dkD  }t        |j                         |   t        j                        rF| j                         |   |j                         |   k7  sn|sqd|fc S  y)NrT   r      F)TN)
rZ   typenumelrangendimrX   rA   r]   rB   rC   )rD   rE   rh   ri   r:   re   checks          r   _check_strides_helperrq      s     &0AHHMMV4K
'')a-= 	"C((<AGGCL1,<E!((*S/5<<8xxz#!((*S/1ecz!	" r!   )rh   r:   c               "    t        | ||d|      S )NTrg   rq   )rD   rE   rh   r:   s       r   r\   r\      s      !		- r!   )rh   c                    t        | ||d      S )NF)rh   ri   rs   )rD   rE   rh   s      r   check_all_stridesru      s     !AUSSr!   c           	         ddl m}  || j                         dk        ryd}t        t	        t        | j                  | j                                           D ]#  \  }} ||dk(        r |||k7        r y||z  }% y)z
    Tests whether a tensor is contiguous or not.

    Tensors are contiguous when they have no elements,
    one element, or when they have "nested" strides.
    r   r<      Trk   F)r>   r=   rm   reversedtupler@   rX   r]   )rD   r=   expected_striderF   rG   s        r   is_contiguousr{     s~     KAGGIM*Os177AHHJ789 .1Q'_ 45)A-. r!   c                    | j                   dk7  ryddlm} d}dD ]B  }| j                  |   } ||dk(        r| j	                         |   } |||k7        r y||z  }D y)N   Fr   r<   rk   rk      rw   r   Tro   r>   r=   rX   r]   rD   r=   rz   re   lengthr]   s         r   is_channels_last_contiguous_2dr      sq    vv{JO 	"!,C/ 9:6!	" r!   c                    | j                   dk7  ryddlm} d}dD ]B  }| j                  |   } ||dk(        r| j	                         |   } |||k7        r y||z  }D y)N   Fr   r<   rk   rk   r}   r   rw   r   Tr   r   s         r   is_channels_last_contiguous_3dr   6  sq    vv{JO 	"!,C/ 9:6!	" r!   c                D     t        j                   t        v  fd       y )Nc                     d  dS )NzReceived unknown memory format rU   r   memory_formats   r   <lambda>z(validate_memory_format.<locals>.<lambda>W  s    1-B r!   )rB   _check_memory_formatsr   s   `r   validate_memory_formatr   T  s    	LL(Br!   c                  t               t        j                  k(  rt        |       S t        j                  k(  rt        |       S t        j                  k(  rt        |       S t        j                  dfd       y )NFc                     d  S )Nz1is_contiguous received unsupported memory format r   r   s   r   r   z1is_contiguous_for_memory_format.<locals>.<lambda>i  s    CM?S r!   )	r   rB   contiguous_formatr{   channels_lastr   channels_last_3dr   r   )rD   r   s    `r   is_contiguous_for_memory_formatr   [  sj     =)///Q+++-a00...-a00	LLSr!   c                2    t        |       xs t        |       S )a  
    True when a tensor is channels-last contiguous.

    This requires that:

      - the tensor is conceptually either 4 (NHWC) or 5 (NDHWC) dimensions
      - if we name the tensor's dimensions NCHW or NCDHW, then the strides are such that the
        stride of the 'C' dimension (Cs) is 1 and the strides corresponding to
        each dimension (Xs) can be ordered Cs <= Ws <= Hs <= (Ds) <= Ns and are
        "nested" -- so Ws = Cs * Cl, where Cl is the length of the 'C' dimension,
        for example.
    )r   r   rD   s    r   is_channels_last_contiguousr   n  s     *!,Q0Nq0QQr!   c                v   ddl m | j                  ryt        |       st	        |       ry| j
                  dk(  r| j                         d   dk(  S  G fddt              }t        t        || j                  | j                                     }d}|D ]#  \  }} |dk(        r ||k7        r y||z  }% y)z
    True when a tensor is non-overlapping and dense.

    A tensor is non-overlapping and dense when there exists a permutation of
    its dimensions that is contiguous.
    r   r<   FTrk   c                  V    e Zd ZU ded<   ded<    fdZ fdZ fdZ fdZ fdZy	)
'is_non_overlapping_and_dense.<locals>.KrL   sizer]   c                B     | j                   |j                   k        S r   r]   r   r   r=   s     r   __lt__z.is_non_overlapping_and_dense.<locals>.K.__lt__      'ell(BCCr!   c                B     | j                   |j                   kD        S r   r   r   s     r   __gt__z.is_non_overlapping_and_dense.<locals>.K.__gt__  r   r!   c                B     | j                   |j                   k        S r   r   r   s     r   __le__z.is_non_overlapping_and_dense.<locals>.K.__le__      'u||(CDDr!   c                B     | j                   |j                   k\        S r   r   r   s     r   __ge__z.is_non_overlapping_and_dense.<locals>.K.__ge__  r   r!   c                B     | j                   |j                   k(        S r   r   r   s     r   __eq__z.is_non_overlapping_and_dense.<locals>.K.__eq__  r   r!   N)	r)   r*   r+   __annotations__r   r   r   r   r   r<   s   r   Kr     s+    		D	D	E	E	Er!   r   )r>   r=   	is_sparser{   r   ro   r]   r   sortedmaprX   )rD   r   lengths_and_stridesrz   r   r]   r=   s         @r   is_non_overlapping_and_denser   ~  s     K{{ Q6q9 	vv{xxz!}!!EJ E& !Q!<=O- "!,/ 9:6!" r!   )_skip_checksc                B   ddl m | st              dk(  rd}t        |      | st	        ddi | st        d D              t              dk(  rg S d   j                  }|dk(  rg S |dk(  rdgS d}d}D ]J  }|xr  |j                  t        j                        }|xr  |j                  t        j                        }L |r|st        t        |            S |r|sdgt        t        d	|            dS d   j                  fd
}t        t        t        |                  }t        d|      D ]L  }	|	}
t        t        |	            D ]1  } |||   ||
         }|dkD  r||
   ||   c||<   ||
<   |}
+|dk  s1 L N t        t        |            S )Nr   r<   :Can't compute elementwise output strides for zero tensors!allow_cpu_scalar_tensorsTc              3  V   K   | ]!  }t        |t              rt        |      s| # y wr   rA   rW   is_cpu_scalar_tensor.0rD   s     r   	<genexpr>zFcompute_elementwise_output_logical_to_physical_perm.<locals>.<genexpr>  s*      
!Z(1Ea1H 
s   ')rk   r   rw   c                    D ]l  }|j                         |    }|j                         |   } |dk(        s |dk(        r@ ||k        r y ||kD        r y |    |   kD        sl y y)Nr   rk   r   )idx_aidx_btensorstride_astride_br=   rX   tensorss        r   should_swapzHcompute_elementwise_output_logical_to_physical_perm.<locals>.should_swap  s     	F}}u-H}}u-H#HM26JA7 #Hx$78#Hx$78 $E%L5<$?@#	* r!   )r>   r=   r?   
ValueErrorcheck_same_shapery   ro   r{   rB   r   r   listrn   rX   rx   )r   r   rc   ro   r{   is_channels_lastrO   r   permidim1dim0
comparisonr=   rX   s    `           @@r   3compute_elementwise_output_logical_to_physical_permr     s    KCLA-Jo'ADA  

 
 7|q	 1:??Dqy	qys
 M 
% 
!//11 +: +
 , 
-- 1@ 1
	
 -E$K  ,Dq$(,!,,AJE: t%&D 1d^ U1X& 	D$T$Zd<JA~)-dT$Z&T
DJa	 r!   c                 h   t        |       dk(  rd}t        |      t        | ddi t        d | D              } t        |       dk(  ry| d   j                  }| d   j
                  }|dk(  ry|dk(  ryt        | d	di}t        ||      }t        |      }t        |t        |            }t        |      S )
zA
    Computes the output strides for elementwise operations.
    r   r   r   Tc              3  Z   K   | ]#  }t        |t              st        |      r | % y wr   r   r   s     r   r   z5compute_elementwise_output_strides.<locals>.<genexpr>2  s'      jJ7@TUV@Ws   +++r   rk   rk   r   )
r?   r   r   ry   ro   rX   r   
apply_permmake_contiguous_strides_forinvert_perm)r   rc   ro   rX   logical_to_physical_permpermuted_shapenew_stridespermuted_stridess           r   "compute_elementwise_output_stridesr   '  s     7|qJog==   G
 7|q1:??DAJEqyqyR	 #   '?@N-n=K![!9: !""r!   c                ^    t        |       }dg|z  }t        |      D ]  \  }}| |   ||<    |S Nr   r?   	enumerate)inpr   ro   permuted_inpre   rF   s         r   r   r   P  sA    s8D4$;LD/ #QFS#r!   c                X    t        |       }dg|z  }t        |       D ]
  \  }}|||<    |S r   r   )r   ro   new_permre   rF   s        r   r   r   X  s;    t9Dtd{HD/ QOr!   c                ~    t        | t        t        j                  f      rt        j                  |        y| dk\  sJ y)zK
    Validates that an object represents a valid
    dimension length.
    r   N)rA   rL   rB   rC   _check_is_size)r   s    r   validate_dim_lengthr   e  s3     &3-.V$ {{r!   c                h    t        | t              sJ t        |              | D ]  }t        |        y)z=
    Validates that a sequence represents a valid shape.
    N)rA   r   rl   r   )rX   ls     r   validate_shaper   r  s4    
 eX&3U3& Ar!   c                D    t        | t              sJ | D ]	  }|dk\  r	J  y)z6
    Verifies the object specifies valid strides.
    r   N)rA   r   )stridesr]   s     r   validate_stridesr   |  s/    
 gx((( {{r!   c                p    t        |t              sJ t        | t              sJ |dk\  r|| k  s|dk(  sJ yy)zr
    Validates that idx is a valid index for the given shape.
    Assumes the index is already canonicalized.
    r   NrA   Dim)rankre   s     r   validate_idxr     sA     c3dC   !8d
cQh..h
r!   c                *    |D ]  }t        | |        y r   )r   )r   indicesre   s      r   validate_dimension_indicesr     s      T3 r!   c                d    t        |t              sJ t        | t              sJ |dkD  r|| k  sJ y)zS
    Validates that ex_idx is a valid exclusive index
    for the given shape.
    r   Nr   )r   ex_idxs     r   validate_exclusive_idxr     s9     fc"""dC   A:&D.((.r!   c                    | dk  rd|  }t        |      | dk(  r|sd| d}t        |      d} |dk\  r|| k  r|S |dk  r|| z   }n|}|dk  s|| k\  rd|   d| dz
   d| d	}t        |      |S )
Nr   z Rank cannot be negative but got zDimension specified as z but tensor has no dimensionsrk   z4Dimension out of range (expected to be in range of [, z], but got ))
IndexError)r   re   wrap_scalarrc   _idxs        r   canonicalize_dimr     s    ax07oqy+C50MNCS/!
axC$J

QwTzax44<DdUG2dUVhZWbcfbgghioKr!   c                     y r   r   r   r   r   s      r   canonicalize_dimsr     s     	r!   c                     y r   r   r   s      r   r   r     s    r!   c                j     t        |t              rt         |      S t         fd|D              S )Nc              3  8   K   | ]  }t        |        y wr   )r   )r   rF   r   r   s     r   r   z$canonicalize_dims.<locals>.<genexpr>  s     IA!$;7Is   )rA   r   r   ry   r   s   ` `r   r   r     s-    '3g{;;IIIIr!   c                f    t        |t              xr  t        |      t        t	        |             k(  S )z>
    Validates that perm is a permutation of length rank.
    )rA   r   r   r   rn   )r   r   s     r   is_valid_permutationr    s(    
 dH%K&,$uT{:K*KKr!   c                0    t        |       t        |      k(  S )z
    Compares two shapes a and b, returning True if they are the same
    (their ranks and corresponding lengths match) and False otherwise.
    )ry   )rD   rE   s     r   is_same_shaper    s     8uQxr!   c                z    t        | t              xr* | j                  dk(  xr | j                  j                  dk(  S )Nr   cpu)rA   rW   ro   rZ   rl   r   s    r   r   r     s.    a$O1O%9OOr!   c                   t        |      dk  ryd}|D ]  }t        |t              rt        |t              ra| rt	        |      r2||j
                  }||j
                  k7  sPdt        |j
                        z   dz   t        |      z   dz   }t        |      dt        t        |            z   dz   }t        |       y)a%  
    Checks that all Tensors in args have the same device.

    Raises a RuntimeError when:
      - args contains an object whose type is not Tensor or Number
      - two Tensor objects in args have different devices, unless one is a CPU scalar tensor and allow_cpu_scalar_tensors is True
    rk   NzTensor on device z is not on the expected device rU   z/Unexpected type when checking for same device, )	r?   rA   NumberrW   r   rZ   r[   RuntimeErrorrl   )r   argsrZ   argrc   s        r   check_same_devicer    s     4yA~ F $c6"Z(',@,E~#'#**o&78 &k" 	  #3'' BCS	NRUXX  s##/$r!   c                    t        | t        j                        r| S t        | t              sJ t        j                  |       S r   )rA   rB   rZ   r[   rZ   s    r   canonicalize_devicer    s4    &%,,'fc"""<<r!   c                B   d}|D ]  }t        |t              rt        |t              rQ| rt        |      r2||j                  }t        ||j                        rWd|j                   d| d}t        |      dt        t        |            z   dz   }t        |       y)z
    Checks that all Tensors in args have the same shape.

    Raises a RuntimeError when:
      - args contains an object whose type is not Tensor or Number
      - two Tensor objects in args have different devices
    NzShape z is not the expected shape rU   z.Unexpected type when checking for same shape, )	rA   r
  rW   r   rX   r  r  r[   rl   )r   r  rX   r  rc   s        r   r   r     s     E $c6"Z(',@,E}		 		2syyk)DUG1M"3'' A3tCy>QTWW  s###$r!   c                    d }d }|D ]e  }t        |t              rt        |t              rA| rt        |      r|j                  }>||j                  }t        ||j                        rc y  y  ||S |S r   )rA   r
  rW   r   rX   r  )r   r  rX   scalar_shaper  s        r   extract_shaper  >  s}    EL c6"Z(',@,E"yy}		 		2  %57<7r!   c                    | r?t        | d   t              r,t        |       dk(  sJ t        t        t
           |       } | d   S t        t
        |       S )Nr   rk   )rA   r   r?   r
   ry   r1   dimss    r   extract_dims_from_varargsr  W  sM     
47H-4yA~~E*+T2Aw$d++r!   c                l    t        |       dk(  rt        | d   t              r| d   } |rt        |        | S )a  
    Returns a shape from varargs.

    In PyTorch, operations that accept shapes often accept them as varargs, like
    foo(*shape). However a user can pass the shape as a sequence of integers,
    like this:

      foo(1, 2, 3)

    or as a sequence of integers

      foo((1, 2, 3))

    In the first case shape will be a tuple of integers, and in the second case it's a tuple
    containing a tuple of integers. This validates those inputs and canonicalizes them
    to a tuple of integers.
    rk   r   )r?   rA   r   r   )rX   validates     r   extract_shape_from_varargsr  b  s5    . 5zQ:eAh9auLr!   c                  	 t        t        |       t        |            }dg|z  }t        |dz
  dd      D ]|  |dz
  z
  }t        |       dz
  |z
  }t        |      dz
  |z
  }|dk\  r| |   nd|dk\  r||   nd	t        j                  	k(  xs dk(  xs 	dk(  	fd       dk(  r	n|<   ~ t        |      S )Nr   rk   r   c                     d d d  S )NzThe size of tensor a (z#) must match the size of tensor b (z) at non-jagged dimension r   )r   sizeAsizeBs   r   r   z#infer_size_shapes.<locals>.<lambda>  s&    ( 0"G#=aSB r!   )maxr?   rn   rB   r   ry   )
rD   rE   ro   expandedSizesoffsetdimAdimBr   r  r   s
          @@@r   infer_size_shapesr&    s    s1vs1vDC$JM4!8R$ :A1vzF"1vzF"19$!19$!e^<!<!	
 %*QJ5Ea!:$ r!   c                
    d}d}t               D ]J  \  }dk(  rt        j                  |du d        |}'dk\  r|z  }2t        j                  dfd       L |(t        j                  |k(   fd       t               S dd	lm t        j                  |dk7   fd
       t        j                  |z  dk(   fd       t                |z   |<   t        j                   |          t               S )z
    Infers the size of a dim with size -1, if it exists.
    Also checks that new shape is compatible with the number of elements.
    Nrk   r   c                      y)Nz"only one dimension can be inferredr   r   r!   r   r   zinfer_size.<locals>.<lambda>      r!   r   Fc                     d  S )Nzinvalid shape dimension r   )ds   r   r   zinfer_size.<locals>.<lambda>  s    *B1#(F r!   c                 &    dt               d  S Nshape '' is invalid for input of size r   rm   rX   s   r   r   zinfer_size.<locals>.<lambda>      gd5k]*I%Q r!   )definitely_truec                 Z      dk(        rdt               dS dt               d S )Nr   z/cannot reshape tensor of 0 elements into shape zL because the unspecified dimension size -1 can be any value and is ambiguousr.  r/  r0  )r3  rm   rX   s   r   r   zinfer_size.<locals>.<lambda>  sM     #5A:. B$u+ OR S  tE{m+J5'R	 r!   c                 &    dt               d  S r-  r0  r1  s   r   r   zinfer_size.<locals>.<lambda>  r2  r!   )r   rB   r   r>   r3  r   r   ry   )rX   rm   dimnewsizer   r+  r3  s   ``   @@r   
infer_sizer8    s   
 CG%  H17LL&RSC!VqLGLL FGH {WQ	
> <5 	JqL	
 	GOq Q	
 Ug%c
 	U3Z(<r!   c                ^    t        | t        j                        sJ | t        j                  u S r   )rA   rB   rY   rN   rY   s    r   is_boolean_dtyper;    s%    eU[[)))EJJr!   c                J    t        | t        j                        sJ | t        v S r   )rA   rB   rY   _integer_dtypesr:  s    r   is_integer_dtyper>    !    eU[[)))O##r!   c                J    t        | t        j                        sJ | t        v S r   )rA   rB   rY   _low_precision_dtypesr:  s    r   is_low_precision_dtyperB    s"    eU[[))))))r!   c                R    t        | t        j                        sJ | j                  S r   )rA   rB   rY   is_floating_pointr:  s    r   is_float_dtyperE    s"    eU[[)))"""r!   c                J    t        | t        j                        sJ | t        v S r   )rA   rB   rY   _complex_dtypesr:  s    r   is_complex_dtyperH    r?  r!   c                4    | j                   xs t        |       S )z5
    Checks if the dtype can require a gradient.
    )rD  rH  r:  s    r   is_grad_dtyperJ    s     ""=&6u&==r!   c                    t         |    S r   )_complex_to_real_dtype_mapr:  s    r   corresponding_real_dtyperM        %e,,r!   c                    t         |    S r   )_real_to_complex_dtype_mapr:  s    r   corresponding_complex_dtyperQ    rN  r!   c                    t        | t        j                        sJ | t        j                  u rt        S | t        v rt
        S | j                  rt        S | t        v rt        S t        d      )z[
    Computes the corresponding Python type (AKA "type kind") for the
    given dtype.
    Invalid dtype!)rA   rB   rY   rN   r=  rL   rD  rK   rG  complexr   r:  s    r   dtype_to_typerU    s]    
 eU[[)))



%
&&r!   c                    t        | t        j                        sJ | t        j                  u rd S | t        v rt
        S | j                  rt        S | t        v rd S t        d      )zU
    Computes the corresponding Python type constructor for the
    given dtype.
    c                    t        |       S r   )rN   rF   s    r   r   z$dtype_to_type_ctor.<locals>.<lambda>+  s
    a r!   c                    t        |       S r   )rT  rX  s    r   r   z$dtype_to_type_ctor.<locals>.<lambda>2  s
     r!   rS  )
rA   rB   rY   rN   r=  r   rD  r   rG  r   r:  s    r   dtype_to_type_ctorrZ  #  s`    
 eU[[)))

  ##
%
&&r!   c                   t        | t              sJ | t        t        j                  fv rt        j                  S | t
        t        j                  fv rt        j                  S | t        t        j                  fv rt        j                         S | t        u rt        t        j                               S t        d|  d      )z=
    Computes the corresponding dtype for a Number type.
    zInvalid type rU   )rA   rl   rN   rB   rM   rL   rC   longrK   rJ   get_default_dtyperT  rQ  r   )typs    r   type_to_dtyper_  7  s    
 c4   
tU]]##zz
sELL!!zz
uenn%%&&((
g~*5+B+B+DEE
}SE+
,,r!   c                v    t        | t        j                        r| j                  S t	        t        |             S r   )rA   rB   TensorrY   r_  rl   rX  s    r   	get_dtyperb  K  s(    !U\\"wwT!W%%r!   c                     t        j                  t               xs t                fd       t        j                  |xs t	                 fd       y)z
    Checks whether the input is floating point or complex.
    If allow_low_precision_dtypes is True, it allows having float16, bfloat16, and complex32
    c                      d  S )Nz<: Expected a floating point or complex tensor as input. Got r   rY   fn_names   r   r   z%check_fp_or_complex.<locals>.<lambda>^  s    7)WX]W^_ r!   c                      d  S )Nz+: Half precision dtypes not supported. Got r   re  s   r   r   z%check_fp_or_complex.<locals>.<lambda>b  s    7)FugN r!   N)rB   r   rE  rH  rB  )rY   rf  allow_low_precision_dtypess   `` r   check_fp_or_complexri  U  sI     
LLu8!1%!8_ 
LL"G*@*G&GNr!   c                h    t        j                  t        | j                        dk\  fd       y )Nrw   c                      d  dS )Nz: The input tensor z! must have at least 2 dimensions.r   )arg_namef_names   r   r   z!check_is_matrix.<locals>.<lambda>i  s    6(-hZ7XY r!   )rB   r   r?   rX   )Arm  rl  s    ``r   check_is_matrixro  f  s"    	LLAGGYr!   c                    t        |       t        |      }} | t        vs|t        vrt        d|  d|       | |u r| S t        D ]  }| |u r|c S ||u s| c S  t        d      )zw
    Returns the higher of the two given Number types.

    The types are ordered bool -> int -> float -> complex.
    &Expected builtin numeric types, found r   zUnknown Python scalar type!)rP   _ordered_typesr  r   rD   rE   r^  s      r   get_higher_typert  m  s     Q!21!5qA!>"9CA3bLMMAv 8H8H	 2
33r!   c                   | 't        | t        j                  t        t        f      sJ |'t        |t        j                  t        t        f      sJ 	 	 	 	 dd} ||        ||      }} | |u r| S | |S || S t        j
                  ft        j                  t        j                  ft        j                  ft        j                  ft        j                  ft        j                  t        j                  ft        j                  ft        j                  ft        j                  ft        j                   ft        j"                  ff}t%        |      D ]*  \  }}| |v r||v r||dz      d   c S | |v r|c S ||v s(| c S  t'        d      )zW
    Computes the "lowest" datatype that is weakly
    "higher" than both a and b.
    c                    | y t        | t        j                        r| S t        | t              r| j                  S t        | t              rt        t        |             S t        d      )Nz(Unexpected type given to _extract_dtype!)rA   rB   rY   rW   r
  r_  rl   r  rX  s    r   _extract_dtypez(get_higher_dtype.<locals>._extract_dtype  sW     9a%Ha$77Na  a))EFFr!   rk   r   Unexpected termination!)rF   8Optional[Union[torch.dtype, TensorLikeType, NumberType]]r(   Optional[torch.dtype])rA   rB   rY   rW   r
  rN   uint8int8int16int32int64float16bfloat16float32float64	complex32	complex64
complex128r   r  )rD   rE   rw  ordered_datatypesre   dtypess         r   get_higher_dtyper    ss    9
1u{{J&GHHH9
1u{{J&GHHHGCG	G !nQ/qAAvyy 
	ejj!				'							 !!23 V;1;$S1W-a00;H;H 0
11r!   c                4    t        j                  |  d        y )Nc                      y)Nz(PrimTorch does not support pinned memoryr   r   r!   r   r   z"check_pin_memory.<locals>.<lambda>  r)  r!   )rB   _check_not_implemented)
pin_memorys    r   check_pin_memoryr    s    	  Jr!   c                Z     t        j                   t         j                  k(   fd       y )Nc                     d  S )Nz!PrimTorch doesn't support layout=r   layouts   r   r   zcheck_layout.<locals>.<lambda>  s    +LVH)U r!   )rB   r  stridedr  s   `r   check_layoutr    s     	  %--!Ur!   c                    t        |       t        |      }} | t        vs|t        vrt        d|  d|       t        D ]  }| |k(  r y||k(  s y t        d      )z
    Compares two types, a and b, returning True if a is weakly "less" than b.

    The comparison is determined by the following type ordering: bool, int, float, complex.
    rq  r   TFrx  )rP   rr  r  rs  s      r   is_weakly_lesser_typer    sq     Q!21!5qA!>"9CA3bLMM 88	 0
11r!   c                    t         t        t        t        fD ]  } ||       r y ||      s y t	        d|  d| d      )NTFzReceived unknown dtypes r   rU   )rH  rE  r>  r;  r   )cast_to	cast_fromfns      r   can_safe_cast_tor    sM    1ACST g;i=	 /y9+QG
HHr!   c                    d}d}| D ]  }t        |t              rt        |t              r||j                  }|t	        |j                        }||j                  ur5dt        |j                        z   dz   t        |      z   dz   }t        |      t	        |j                        }||usdt        |      z   dz   t        |      z   dz   }t        |      dt        t        |            z   dz   }t        |       y)a  
    Checks that all Tensors in args have the same device and that all Numbers have the
    same corresponding Python type.

    Raises a RuntimeError when:
      - args contains an object whose type is not Tensor or Number
      - two Tensors objects in args have different dtypes
      - two Number objects in args have different types
      - there are Tensors and Numbers in args, and one of those Tensors corresponding
          Python types is different from the type of one of those Numbers
    NzTensor with dtype z is not the expected dtype of rU   z&Tensor with corresponding Python type z is not the expected type of z.Unexpected type when checking for same dtype, )rA   r
  rW   rY   rU  r[   r  rl   )r  
full_dtypescalar_typer  rc   arg_types         r   check_same_dtyper    s.    JK .$c6" Z(! YY
"+CII6*(#))n%67 *o& 	  #3''$SYY/H{*<(m$56 +&' 	  #3'' A3tCy>QTWW  s##].$r!   c                .    t         j                  | |       S r   )_computation_dtype_mapgetr:  s    r   get_computation_dtyper  :  s    !%%eU33r!   c                b    |j                   dk(  rt        j                  | |       S t        |       S Nr  )rl   _cpu_acc_type_mapr  r  )rY   rZ   s     r   get_acc_typer  G  s,    {{e $$UE22$U++r!   c                  $    e Zd ZdZdZdZdZdZdZy)ELEMENTWISE_TYPE_PROMOTION_KINDr   r   rw   r   )r}   )r   N)	r)   r*   r+   DEFAULT	NO_OPMATHINT_TO_FLOATALWAYS_BOOLCOMPLEX_TO_FLOATBOOL_TO_LONGr   r!   r   r  r  O  s!    GILKLr!   r  c                      e Zd ZdZdZdZdZy)REDUCTION_OUTPUT_TYPE_KINDr  r   r  r  N)r)   r*   r+   SAMEr  KEEP_PROMOTED_TYPEr  r   r!   r   r  r  X  s    DKr!   r  c                      e Zd ZdZdZdZdZy)RETURN_TYPEr  r   r  r  N)r)   r*   r+   NEWVIEWINPLACENONEr   r!   r   r  r  f  s    
CDGDr!   r  c                    t        | t        j                        rt        S t        | t        j                        rt
        S t        | t        j                        rt        S t        |       S r   )	rA   rB   rC   rL   rJ   rK   rM   rN   rl   rX  s    r   number_typer  n  sF     !U\\"
	Au~~	&	Au}}	%Awr!   c                    dd l }| j                  |j                  j                  j                  u rt        S | j
                  rt        S t        S Nr   )sympykindcoreBooleanKindrN   
is_integerrL   rK   )rF   r  s     r   	expr_typer  {  s7    vv,,,	

 r!   c                   t        d |D              }t        }ddl}|D ]  }t        |t        t
        |j                  f      s#dt        t        |             d}t        |      t        |t              rt        |t        |            }mt        ||j                        rt        |t        |            }t        |t        |j                              } d}dd	 dd}|t        u r& ||t               }|t#        j$                         n|}nn|t&        u r, ||d	 d
      }|Xt)        t#        j$                               }n:|t*        u r" ||t,              }|t"        j.                  n|}nt"        j                  }| t0        j2                  u rt5        |      |fS | t0        j6                  u r||fS | t0        j8                  u r7t-        |      st;        |      rt#        j$                         }t5        |      |fS | t0        j<                  u r%t5        |      }	t?        |      rtA        |      }|	|fS | t0        jB                  u r8t;        |      r t"        j.                  t"        j.                  fS t5        |      |fS | t0        jD                  u rt5        |      t"        j                  fS t        dt        |              )a  
    Computes the computation and result dtypes for elementwise type promotion
    on the given arguments and with the given elementwise type promotion kind.

    Note that not all inputs to an elementwise operation necessarily participate in type promotion.
    For example, the "alpha" parameter of torch.add does not participate in type promotion,
    although it may be cast to the Python type corresponding to the computation dtype that
    the type promotion algorithm determines.

    Default elementwise type promotion, which all other type promotion kinds tweak (see below),
    first decides which of four ordered types to use:

    bool -> integer -> floating point -> complex

    The selected type is the "lowest" type in the above list such that all number arguments
    have a weakly "lower" type and all tensor arguments have a weakly lower corresponding
    type for their dtype.

    Once the type is determined, the particular result dtype is found. The dtypes are
    partially ordered as follows:

    bool -> uint8, int8 -> int16 -> int32 -> int64 ->
      float16, bfloat16 -> float32 -> float64 -> complex32 -> complex64 -> complex128

    The result dtype is selected by:
      - if no tensor's dtype has the same corresponding type as the one selected,
          then the result dtype is the (default) dtype corresponding to the selected type
          (for example, 1.5 + an integer tensor has a result dtype of the default floating point dtype)
      - if the result type is complex then the dtype is:
        -  the default complex dtype if there are no floating point or complex tensors
        -  if there are floating point or complex tensors with one or more dimensions, then
            the complex dtype corresponding to the highest corresponding complex dtype among those tensors
            (for example, double + cfloat -> cdouble)
        -  if there are only floating point or complex tensors with zero dimensions, then
            the complex dtype corresponding to the highest corresponding complex dtype among those tensors
      - if the first two cases do not apply, the result dtype is the highest dtype among
          all tensors with one or more dimensions of the output type, and if there are no such
          tensors then it's the highest dtype among all tensors with zero dimensions of the output type
          (for example, long + half -> half, even if the half tensor has zero dimensions)

    The "corresponding complex dtypes" are:
      float16    -> complex32
      bfloat16   -> complex64
      float32    -> complex64
      float64    -> complex128
      complex32  -> complex32
      complex64  -> complex64
      complex128 -> complex128

    The DEFAULT type promotion kind computes per above, and then uses the result dtype to pick a computation
    dtype by mapping low precision floating point and complex dtypes as follows:

      float16   -> float32
      bfloat16  -> float32
      complex32 -> complex64

    This is referred to as "op math", and the NO_OPMATH type promotion kind disables this mapping, making the
    computation dtype the same as the result dtype when it's selected. NO_OPMATH is appropriate for kernels
    which perform no mathematical operations on their tensors (see below for examples).

    The INT_TO_FLOAT type promotion kind maps boolean and integer result dtypes to the default floating point dtype,
    and computation dtypes to the appropriate op math dtype.

    The COMPLEX_TO_FLOAT type promotion kind maps complex result dtypes to the corresponding float dtype, following this
    mapping:

        complex32  -> float16
        complex64  -> float32
        complex128 -> float64

    Note that COMPLEX_TO_FLOAT derives the computation dtype as the DEFAULT setting does.

    The BOOL_TO_LONG type promotion kind maps boolean computation and result dtypes to long.

    The ALWAYS_BOOL type promotion kind always sets the result dtype to bool.

    Example operators for each type promotion option:
      DEFAULT                 : add
      NO_OPMATH               : where, nextafter, cat
      INT_TO_FLOAT            : sin
      COMPLEX_TO_FLOAT        : abs
      BOOL_TO_LONG            : pow
      ALWAYS_BOOL             : eq

    c              3  &   K   | ]	  }||  y wr   r   )r   rF   s     r   r   z%elementwise_dtypes.<locals>.<genexpr>  s     3qQ]3s   r   NzUnexpected type z+ when computing elementwise type promotion!F)float_as_complexc                  d }d }| D ]r  }t        |t              s ||j                        s'|j                  }|rt        |      rt	        |      }|j
                  dk(  rt        ||      }gt        ||      }t ||S |S r  )rA   rW   rY   rE  rQ  ro   r  )r  filterr  zero_dim_tensor_dtypeone_plus_dim_tensor_dtyperF   _dtypes          r   _find_highest_dtype_filteredz8elementwise_dtypes.<locals>._find_highest_dtype_filtered  s     !%$(! 	A!Z(VAGG_#v(>8@F66Q;,<-v-)
 1A161-	  %0,,$$r!   c                2    t        |       xs t        |       S r   )rE  rH  rX  s    r   r   z$elementwise_dtypes.<locals>.<lambda>  s    nQ'>+;A+> r!   TzUnknown type promotion kind )r(   rz  )#ry   rN   r  rA   r
  rW   Basicr[   rl   r   rt  r  r  rU  rY   rK   rE  rB   r]  rT  rQ  rL   r>  r\  r  r  r  r  r  r;  r  rH  rM  r  r  )
type_promotion_kind_argsr  highest_typer  rF   rc   result_dtyper  computation_dtypes
             r   elementwise_dtypesr    s   t 3E33DL  Q!fj%++>?$Sa\N2]^CS/!a *<QHL5;;'*<1FL +<qww9OPLQ L +0%	%4 u3D.I)5)=E##%< 	 
	 3>!

 6u7N7N7PQL		3D:JK%1%9uzz| zz=EEE$\2L@@	 ? I I	I\))	 ? L L	LL)-=l-K 224L$\2L@@	 ? P P	P1,?L)3LAL ,..	 ? L L	LL)::uzz))$\2L@@	 ? K K	K$\2EJJ>>7<O8P7QRSSr!   c                X   ||n| j                   }t        |      }|t        j                  k(  s|t        j                  k(  r=|r|n| j                   }|t        j                  k(  rt        |      rt        |      }||fS |t        j                  k(  rd }||fS t        j                  }||fS r   )
rY   r  r  r  r  rH  rM  r  rB   rN   )r  output_dtype_kindrY   	inp_dtyper  r  s         r   reduction_dtypesr  ?  s     *		I-i87<<< : K KK %u399!;!L!LL .3LAL
 l**	 
8KK	K l** zzl**r!   c                $   t        |        | syddlm} d}g }t        |       D ],  }|j	                  |       | ||      r|nt        |d      z  }. t        t        |            }|r|S t        |       dk  r|S |dd dt        | d   d      fz   S )z
    Returns the strides of a contiguous tensor if row_major
    If row_major=True, it returns the strides of a contiguous batch of Fortran-contiguous matrices
    This is often used when calling external libraries like BLAS/LAPACK/cuSolver...
    r   r   )is_nested_intrk   rw   N)	r   r>   r  rx   appendr   ry   r?   r!  )rX   	row_majorr  
multiplierr   r   results          r   r   r   \  s     5C()JGe_ 
z"q!Awq!}	


 8G$%F u:>Mcr{aU2Y!2333r!   c                    t        j                  t        |       dk(  d        d}dgdz  }dD ]  }|||<   || |   z  } t        |      S )Nr   c                      y)NzAOnly tensors of rank 3 can use the channels_last_1d memory formatr   r   r!   r   r   z3make_channels_last_1d_strides_for.<locals>.<lambda>  r)  r!   rk   r   )rk   r   r   rB   r   r?   ry   rX   r  r   re   s       r   !make_channels_last_1d_strides_forr  ~  sd     
LLE
aS
 )*J,-37G ! "eCj 
	! >r!   c                    t        j                  t        |       dk(  d        d}dgdz  }dD ]  }|||<   || |   z  } t        |      S )Nr}   c                      y)Nz>Only tensors of rank 4 can use the channels_last memory formatr   r   r!   r   r   z3make_channels_last_2d_strides_for.<locals>.<lambda>  r)  r!   rk   r   )rk   r   r  r   r  r  s       r   !make_channels_last_2d_strides_forr    sd     
LLE
aP
 )*J,-37G ! "eCj 
	! >r!   c                    t        j                  t        |       dk(  d        d}dgdz  }dD ]  }|||<   || |   z  } t        |      S )Nr   c                      y)NzAOnly tensors of rank 5 can use the channels_last_3d memory formatr   r   r!   r   r   z3make_channels_last_3d_strides_for.<locals>.<lambda>  r)  r!   rk   r   )rk   r   r  r   r  r  s       r   !make_channels_last_3d_strides_forr    sd     
LLE
aS
 )*J,-37G! ! "eCj 
	! >r!   c                    t        | t              rt        |       nd}|dk(  rt        |       S |dk(  rt	        |       S |dk(  rt        |       S t        d| d      )Nrk   r   r}   r   z)no channels last format strides exist in z dimensions)rA   r   r?   r  r  r  r  )rX   ro   s     r   make_channels_last_strides_forr    sg     $E843u:!Dqy077	077	0777v[I
 	
r!   c                    |D ]  }t        t        |       |        g }t        t        |             D ]  }||v r|j                  | |           t	        |      S r   )r   r?   rn   r  ry   )rX   
dimensionsre   	new_shapes       r   compute_reduction_output_shaper    sj      &SZ%& ISZ  %*s$	% r!   c                Z    t        |       t        t        |             k7  rt        d      y )Nz#duplicate value in the list of dims)r?   setr  r  s    r   validate_no_repeating_dimsr    s'    
4yCD	N"@AA #r!   c                     |t        t        t                           S t         fd|D              }t        |       |S )Nc              3  H   K   | ]  }t        t              |        y wr   )r   r?   )r   re   rX   s     r   r   z!reduction_dims.<locals>.<genexpr>  s     Cs!#e*c2Cs   ")ry   rn   r?   r  )rX   r  s   ` r   reduction_dimsr    s9    |U3u:&''CdCCDt$Kr!   c                    || t        d      || d}n|
| | du rdnd}t        |t        t        f      st	        d      |dk  rt	        d      t        |      S )Nz5cannot specify both correction and unbiased argumentsg      ?Fg        z.correction argument should be integer or floatr   z*correction argument should be non-negative)r  rA   IntLike	FloatLiker   r   )unbiased
corrections     r   set_correctionr    s}     ("6RSS		 0
		 4$-S3
j7I"67IJJA~EFFZ  r!   c                    ddl m}  |t        t        j                  | d      dk(        ryt        d t        | |      D              }d|z   |z   S )a  Computes the minimum storage size to hold the given tensor geometry.

    Example
    =======

    This is the size of a newly allocated tensor's storage, in units of elements

    >>> t = torch.empty((10, 20))
    >>> compute_required_storage_length(t.shape, t.stride(), t.storage_offset())
    200

    >>> # xdoctest: +SKIP(failing)
    >>> t2 = torch.empty_strided((1, 2, 3), (5, 7, 11))
    >>> size = compute_required_storage_length(t2.shape, t2.stride(), t2.storage_offset())
    >>> size == t.storage().size()
    True

    A valid tensor may have a larger storage size, but never smaller

    >>> slice = torch.empty(100)[20:40]
    >>> slice.storage().size()
    100

    >>> compute_required_storage_length(slice.shape, slice.stride(), slice.storage_offset())
    40

    r   r<   rk   c              3  2   K   | ]  \  }}|d z
  |z    yw)rk   Nr   )r   rF   rG   s      r   r   z2compute_required_storage_length.<locals>.<genexpr>  s     ATQa!eq[As   )r>   r=   r   operatormulsumr@   )rX   r   r^   r=   
max_offsets        r   compute_required_storage_lengthr    sN    < K F8<<:a?@AS-@AAJ~
**r!   c           
         t        |||      }| j                         |k  r<d| j                          d| dt        |       dt        |       d| 
}t        |      y)z]
    Determines if the given shape, strides, and offset are valid for the given storage.
    zCan't view a storage of size z with an offset of z, shape of z, and strides of z#, which requires a storage of size N)r  r   r[   r   )rD   rX   r   r^   required_lengthrc   s         r   check_in_bounds_for_storager     sw     6eWnUOvvx/!+AFFH:5HHX YE
|#4S\N C00?/@B 	
 o "r!   zz`torch._prims_common.check` is deprecated and will be removed in the future. Please use `torch._check*` functions instead.)categoryc                2    t        j                  || |       y)a  
    Helper function for raising an error_type (default: RuntimeError) if a boolean condition fails.
    Error message is a callable producing a string (to avoid wasting time
    string formatting in non-error case, and also to make it easier for torchdynamo
    to trace.)

    .. note:: This function is planned for removal in the future. Please use
        `torch._check*` functions instead.
    N)rB   _check_with)rE   sexc_types      r   rp   rp   4  s    " 
h1%r!   c                   ddl m} t        |       }|dk(  rg d}n|dk(  rg d}ny ||d   dk(        ryd}|D ]M  } || |   dk(        r y |||   |k        r y|dk(  r
||d   k(  r y||   } |||   dkD        sF|| |   z  }O y	)
Nr   r<   r}   r~   r   r   Frk   T)r>   r=   r?   )rX   r   r=   ro   	dim_orderminr+  s          r   are_strides_like_channels_lastr  J  s     Ku:Dqy 		#	GAJ!O,
C 	aA.
S 016cWQZ'aj
Q/58OC	 r!   c                "   | j                   t        j                  k7  rt        j                  S t	        | j
                  | j                               r/| j                  dk(  rt        j                  S t        j                  S t        j                  S )Nr}   )
r  rB   r  r   r  rX   r]   ro   r   r   rX  s    r   suggest_memory_formatr  k  s_    xx5== &&&%aggqxxz:&'ffku""Mu7M7MM"""r!   c                8    t        t        j                  | d      S )zCProduct of elements in input sequence. Returns 1 for empty sequencerk   )r   r  r  )xss    r   prodr  u  s    (,,A&&r!   c                    t        |       t        |      kD  ryt        t        |             D ]$  }| | dz
     || dz
     k7  s| | dz
     dk7  s$ y y)zChecks if a shape can be expanded to another shape.
    This is equivalent to checking if the two shapes are broadcastable.
    Frk   T)r?   rn   )rX   desiredr   s      r   is_expandable_tor  z  sd     5zCL 3u: !a=GQBFO+qb1f0B r!   c                    |j                   t        j                  u r| j                  |      S t        j                  | |d      S )zu
    Similar to torch.where(mask, t, 0) but if t is boolean,
    result is also boolean and not promoted to int.
    r   )rY   rB   rN   logical_andwhere)maskrO   s     r   mask_tensorr!    s8     	ww%**""{{4A&&r!   c                    | j                   }d}|j                  |      sJ |t        |      d }|r|dd }|j                  dd      }|dz   }t	        t
        j                  j                  j                  | |       S )a  
    Given the __module__ of reference and its name, it returns
    (our best guess of) the ATen name of the associated operation

    Note: In ATen, the __name__ of a function within a module often
    starts by the module name. E.g. linalg_eigh, or special_zeta
    ztorch._refsNrk   ._)	r*   
startswithr?   replacegetattrrB   _opsopsaten)r  namemoduleprefixs       r   get_aten_opr.    s     ]]FFV$$$CKM"F S)#5::>>&&6(4&(9::r!   c                2    | | S t        j                         S r   )rB   r]  r:  s    r   dtype_or_defaultr0    s    %5D5+B+B+DDr!   c                4    | | S t        j                  d      S r  )rB   rZ   r  s    r   device_or_defaultr2    s    '6@U\\%-@@r!   c                *    | | S t         j                  S r   )rB   r  r  s    r   layout_or_defaultr4    s    '6:U]]:r!   c                \   t        | j                         | j                         | j                               }	 t        j
                  j                  t        j
                  j                  j                        }t        j
                  j                  t        j
                  j                  j                  d       t	        j                  | |fdd      j                         }t	        j                  || j                         | j                         | j                               t        j
                  j                  t        j
                  j                  j                  |       S # t        j
                  j                  t        j
                  j                  j                         w xY w)NTr   r   )r  r   r]   r^   rB   _C&_dispatch_tls_is_dispatch_key_excludedDispatchKeyADInplaceOrView'_dispatch_tls_set_dispatch_key_excluded
as_stridedclone)rF   needed_sizeoldbuffers       r   clone_preserve_stridesr@    s   1	!((*a..0K
hh==HH  00
 	88HH  00$	
 !!!k^T1=CCE!((*a>N>N>PQ88HH  00#	
88HH  00#	
s   C+E' 'AF+c                     t        j                         rGt        j                         rt        j                    d       y t        j
                  d fd       y y )Na    does not have a deterministic implementation, but you set 'torch.use_deterministic_algorithms(True, warn_only=True)'. You can file an issue at https://github.com/pytorch/pytorch/issues to help us prioritize adding deterministic support for this operation.Fc                       dS )Na   does not have a deterministic implementation, but you set 'torch.use_deterministic_algorithms(True)'. You can turn off determinism just for this operation, or you can use the 'warn_only=True' option, if that's acceptable for your application. You can also file an issue at https://github.com/pytorch/pytorch/issues to help us prioritize adding deterministic support for this operation.r   callers   r   r   z)alert_not_deterministic.<locals>.<lambda>  s    h ] ^ r!   )rB   $are_deterministic_algorithms_enabled-is_deterministic_algorithms_warn_only_enabledwarningswarnr   rC  s   `r   alert_not_deterministicrI    sS    113>>@MM( Y Z LL
 4r!   c                  J    e Zd Ze e       fd       Zed        Zed        Zy)CUDARngStateHelperc                P   t         j                  j                         st        d      | 5  t        j                  t         j                  j                               }t        j                  t         j                  j                               }||fcd d d        S # 1 sw Y   y xY w)NzCUDA not available)rB   rT   is_availabler  r   initial_seed_get_rng_state_offset)	fake_modeseedr#  s      r   get_torch_state_as_tuplez+CUDARngStateHelper.get_torch_state_as_tuple  ss    zz&&(344 	 <<

 7 7 9:D\\%**"B"B"DEF<	  	  	 s   A&BB%c                ,   | j                  dg      j                  t        j                        }|j                  dg      j                  t        j                        }t        j                  ||g      }t        j
                  j                  |       y )Nrk   )reshapeviewrB   r{  catrT   set_rng_state)rQ  r#  seed_portionoffset_portion	new_states        r   set_torch_state_tensorz)CUDARngStateHelper.set_torch_state_tensor  sg     ||QC(--ekk:,11%++>II|^<=	

  +r!   c                ^    t         j                  j                  | j                                y r   )rB   rT   _set_rng_state_offsetitem)relative_offsets    r   set_new_offsetz!CUDARngStateHelper.set_new_offset  s    

(()=)=)?@r!   N)r)   r*   r+   staticmethodr   rR  r[  r`  r   r!   r   rK  rK    sC    +6=     , , A Ar!   rK  )rD   r.   rE   r.   r(   rN   )TF)rD   TensorLikeTyperE   rb  )rD   rb  rE   rb  r(   ztuple[bool, Optional[int]])rD   rb  r(   rN   )rD   ra  r(   rN   )r   torch.memory_format)rD   ra  r   rc  r(   rN   )r(   z	list[int])r(   tuple[int, ...])r   rL   )rX   r.   )r   r/   )r   rL   re   rL   )r   rL   r   r1   )r   rL   r   rL   )T)r   rL   re   rL   r   rN   r(   rL   )r   rL   r   Sequence[int]r   rN   r(   rd  )r   rL   r   rL   r   rN   r(   rL   )r   rL   r   r1   r(   rN   )rD   r   rE   r   r(   rN   )rD   r   r(   rN   )rZ   r5   r(   torch.device)r   rN   )r   rN   r(   zOptional[ShapeType])r  z5Union[DimsSequenceType, tuple[DimsSequenceType, ...]]r(   r1   )rX   z"Union[ShapeType, tuple[ShapeType]]r(   rd  )rD   r.   rE   r.   r(   rd  )rX   r.   rm   rL   r(   rd  )rY   torch.dtyper(   rN   )rY   rg  r(   rg  )rY   rg  r(   rl   )rY   rg  r(   z"Callable[[NumberType], NumberType])r^  rl   r(   rg  )rF   zUnion[torch.Tensor, NumberType])rY   rg  rf  r[   rh  rN   )rn  )rn  rb  rm  r[   rl  r[   )rD   rl   rE   rl   r(   rl   )rD   ry  rE   ry  r(   rz  )r  rN   )r  torch.layout)rD   rl   rE   rl   r(   rN   )r  rg  r  rg  r(   rN   )rY   rg  rZ   rf  r(   rg  )rF   z>Union[NumberType, torch.SymInt, torch.SymFloat, torch.SymBool]r(   rl   )rF   zsympy.Basicr(   rl   )r  r  r(   ztuple[torch.dtype, torch.dtype]r   )r  r  rY   rz  r(   z)tuple[torch.dtype, Optional[torch.dtype]])rX   r.   r  rN   r(   !tuple[Union[_IntLikeT, int], ...])rX   zSequence[_IntLikeT]r(   ri  )rX   r.   r  r   r(   rd  )r  r   )rX   r.   r  zOptional[Sequence]r(   rd  )NN)r  zOptional[bool]r   zOptional[NumberType]r(   rK   )rX   r.   r   r/   r^   rL   r(   rL   )rD   ztorch.TypedStoragerX   r.   r   r/   r^   rL   )rE   rN   r  zCallable[[], str]r  ztype[Exception]r(   None)rX   re  r   re  r(   rN   )rF   rb  r(   rc  )r  zSequence[NumberType]r(   r3   )rX   r.   r  r.   r(   rN   )r   rb  rO   rb  )r  r	   r+  r[   )rY   rz  r(   rg  )rZ   zOptional[DeviceLikeType]r(   r5   )r  zOptional[torch.layout]r(   rh  )rD  r[   )
__future__r   r  typingrG  collections.abcr   
contextlibr   enumr   	functoolsr   r   r	   r
   r   r   r   r   r   r   typing_extensionsr   r   rB   r   r   r   r  Protocolr   r,   Sizer   rL   ry   r.   r   r/   r0   r1   rl   rN   rK   rT  r2   r3   r4   rC   rJ   rM   r
  r   r  r  BoolLikeIntWithoutSymIntFloatWithoutSymFloatr[   rZ   r5   ra  sym_notsym_min	_sym_sqrtsym_iter6  ro   __get__rm   r   r^   r]   rY   r   rX   requires_gradr  r{   
__format____repr____getitem__torch_function_passthroughrb  rW   r6   r7   CustomOutParamAnnotationrH   rP   rf   rq   r\   ru   r   r   r   preserve_formatr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r   r  r  r  r&  r8  r{  uint16uint32uint64r|  r}  r~  r  r=  r  r  r  rA  r  r  rG  r;  r>  rB  rE  rH  rJ  r  r  rL  rP  rM  rQ  rU  rZ  r_  rb  rr  ri  ro  rt  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  FutureWarningr  rp   r  r  r  r  r!  r.  r0  r2  r4  r@  rI  rK  r   r!   r   <module>r     s	   "    $ "  
 
 
 4  - - 
   =9I UZZcE#s(OCD	9 Dd3isCx89
I 9CcE#s(O;<) <#DIuS#X$>? ) ?!$t*d3ied7m"ST	 T dC78
I 8!$U"23	 3
UGU\\5>>5==	Q 

ENN#	%--   !#u||S"89	 9	 
LL	MM	OO	MM	MM	MM	OO	MM	LL	LL	LL	LL	LL	LL	LL	LL""	LL	LL	LL&&	LL	LL	LL	LL	LL&&	LL5 < \\
 %d>&:E.RUBU<V&V W I W$).**D$E 	 E1  BG 0  	@
 @
@
@
N   6 8<PU		(		 8<TT(TT4,. 
				!4	&R =R !^ ^ B%#R
	/ 
): 
;?	
	%	48		 
	 
	 
	JL P%$P $@82,
?,, - > 2/f 
KK	LL	LL	LL	JJ	KK	KK	KK	 H ??EOOU5E5EF
$
*
#
$
> 
emm	OOU]]	OOU]]  
MM5??	NNEOO	MM5??	MM5##	 --'&'(-(& UG, JN!$BF"44<2?<2?<2 <2~2*I=$D 
NNEMM	MM5==	OOU__ 4
 
NNEMM	MM5==	MM5==	OOU%%	OOU%% ,d  $ 
E
	
	tT8tT %tTt $(+1+ !+ /	+< )-44!%4&4D&&&(&&

&
 "* B
  $'+!!$! !$&+&+)&+;>&+&+R"+6@RU( 4 @L&&!&-<&	&
&"#0	B#'

';*EA;
2.A Ar!   