U
    Kc,%                 v   @   s  U d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZmZmZmZmZmZmZmZ d dlZd dlmZ d dlZd dlmZ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&m'Z'm(Z( d d	l)m*Z* d d
l+m,Z,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4 ej56ddZ7ej56dddZ8ej56dddZ9ej56dddZ:ej56dddZ;ddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~dddddddddddgvZ<ddddddeee"ejf  ee$ ee% eej= eeej>e?f  dddZ@e?ee#ee#df f eee?dddZAG dd deZBddeBee'df e,dddZCdd ZDe?eBdddZEe?eBdddZFdd ZGeEdejHdeBjIdZHeEdejJdeBjKdZJeEdejLdeBjKdZLeEdejMdeBjKdZMeEdejNdeBjKdZNeEdejOdeBjKdZOeEdejPdeBjKdZPeEdejQdeBjKdZQeEdejRdeBjKdZReEd"ejSjTdeBjKdZTeEd#ejSjUdeBjKdZUeEdejVdeBjKdZWeEdejSjXdeBjKdZYeEd ejSjZdeBjKdZ[eEd!ejSj\deBjKdZ]eEd$ej^deBjKdZ^ejedddZ_eEd%e_deBjKdZ`eEd&ejadeBjKdZae'e'dddZbeAdebejcde#jddZeeEd(ejfdeBjKdZfeEd)ejgdeBjKdZgeEd*ejSjhdeBjKdZieEd+ejSjjdeBjKdZjeEd,ejkdeBjKdZkeEd-ejSjldeBjKdZleEd.ejSjmdeBjKdZme'e"e'dddZnee"edddZoeAde#jdeneoddZpeEd0ejqdeBjKdZqeAdeeDeBjIde#jrejsddZseEd2ejtdeBjudZteEd3ejvdeBjKdZveEd4ejwdeBjKdZweEd5ejxdeBjKdZxeEd6ejydeBjKdZyeEd7ejzdeBjKdZzeAdeeDeBjIde#jrej{ddZ{eEd:ej|deBjKdZ|eEd8ej}deBjKdZ}eEd;ej~deBjKdZ~eEd^ejdeBjKdZeEd<ejdeBjKdZeEd=ejdeBjKdZeEd>ejdeBjKdZeEd?ejdeBjKdZeEd@ejSjdeBjKdZeEdAejdeBjKdZeEdBejdeBjKdZeEdCejdeBjKdZeEdDejdeBjKdZeFdEejdeBjKdZeFdFejdeBjKdZeFdGejdeBjKdZeFdHejdeBjKdZeFdIejdeBjKdZdd ZeFdJedeBjKdZeFdKejdeBjudZeFdLejdeBjKdZeFdMejdeBjKdZeFdNejdeBjKdZeFdOejdeBjKdZeFdPejdeBjudZeFdQejdeBjudZeFdRejdeBjKdZeFdSejSjdeBjKdZeFdTejSjdeBjKdZeFdUejdeBjudZeFdVejdeBjudZee'e"f ee'e"f e'dddZeFdWedeBjKdZee'e"f ee'e"f e'dddZeFdXedeBjKdZeFdYejdeBjKdZeFdZejdeBjudZeFd[ejdeBjKdZeFd\ejdeBjKdZeFd]ejdeBjKdZeFd_ejdeBjKdZeFd`ejdeBjKdZeGZeFdbejdeBjKdZeFdcejSjdeBjKdZe'e$e%ee'dddZee$e%eedddZdZeAdeee#jredōZe'e$ee dƜddȄZddʄ ZdZeAdeee#jredōZe'eeeee$ ee% f d͜ddτZe'eee'd͜ddфZeeeed͜ddӄZdZeAdeee#jredōZe'e'dddׄZdZeAdeeje#jredōZde'ee'dڜddhZeZee ed< de'eeee% e'dݜdd߄ZÐdeeeee% edݜddZdZeAdeee#jredōZde'eeeee'dddZƐdeeeeeedddZdZeAdeee#jredōZe'eee'dddZeeeedddZdZeAdeee#jredōZe'ee'dڜddZeeedڜddZdZeAdeee#jredōZe'ee'dddZeeedddZdZeAdeee#jredōZe'e'dd dZeedddZאdZeAdeee#jredōZeeeed͜ddoZee' ee'ddd	Zeeedf ee f eedd
dZܐdZeAdeee#jdedōZe'e$dddZee$edddZdZeAdeee#jdedōZe'ee'dddZdZeAdeeje#jdedōZe'e'e'e'dddZdZeAdeeje#jdedōZe'ej=e'd d!d"Zeej=ed d#d$Zd%ZeAd&eee#jdedōZe'ee?ej>f e'd'd(d)Zeee?ej>f ed'd*d+Zd,ZeAd-eee#jdedōZe'e,dd.d/Zd0ZeAd1eejje#jdedōZej=e,d2d3d4Zej=d5d6d7Zd8ZeAd9eee#jdedōZej=e,d2d:d;Zej=d5d<d=Zd>ZeAd?eee#jdedōZe'ej=e'd d@dAZeej=ed dBdCZdDZeAdEeee#jdedōZ e'e'dFdGdHZeeeddIdJZdKZeAdLeee#jedōZe'e$ddMdNZee$eddOdPZdQZeAdReee#jedōZ	ddSdTdUZ
dVdW ZdXZdYZdZZd[Zd\Ze?d]d^d_Ze?d]d`daZedejedbZdd5e'ee eej= edcdddeZed~eedbZedejedbZed|ejedbZed}ejedbZdfZe"e"e"eej= eej> ee'dgdhdiZe"e"e"eej= eej> ee'dgdjdkZeAdle#jdeeedZe$ej=ej>ee'dmdndoZe$ej=ej>eedmdpdqZdrZ eAdseee#jde dōZ!e$e%ej=ej>ee'dtdudvZ"dwZ#eAdxe#jde"ej$e#dZ$e$e"ej=ej>ee'dydzd{Z%e$e"ej=ej>eedyd|d}Z&d~Z'eAde%e&e#jde'dōZ(e'e"ej=ej>ee'dddZ)ee"ej=ej>eedddZ*dZ+eAde)e*e#jde+dōZ,e"ej=ej>e'dddZ-e"ej=ej>edddZ.dZ/eAde-e.e#jde/dōZ0e'eee'e'e'f dddZ1e'eeeeef dddZ2dZ3eAde1e2e#jde#jde#jdfe3dōZ4e$ee5e6f e5ej=ej>ee'dddZ7e$ee5e6f e5ej=ej>eedddZ8dZ9eAde#jde7e8e9dZ:e$e5e5ej=ej>e'dddZ;e$e5e5ej=ej>edddZ<dZ=eAde#jde;e<e=dZ>e&eee'dddZ?e&eee'dddZ@dZAeAde?e@e#jdeAdōZBe&eee'dddZCe&eee'dddZDdZEeAdeCeDe#jdeEdōZFe&eee'dddZGe&eee'dddZHdZIeAdeGeHe#jdeIdōZJe  dS (      N)Enum)partialreduce)AnyCallableListOptionalSequenceTupleTypeUnion)TensorTypedStorage)_get_default_device)register_nvprims)checkDimsSequenceTypeDimsTypeNumber
NumberTypeRETURN_TYPE	ShapeType
StrideType
TensorLikeTensorLikeTypetype_to_dtypebackwards_not_supported)
FakeTensorFakeTensorMode)handle_torch_functionhas_torch_function)tree_flattentree_maptree_unflattenprimsZDEFZIMPLZCompositeExplicitAutogradZBackendSelectZAutogradZMetar   absacosacoshasinasinhatanatanhcoscosh	bessel_i0
bessel_i0e	bessel_i1
bessel_i1e	bessel_j0	bessel_j1bitwise_notcbrtceilconj_physicaldigammaerferf_inverfcexpexpm1exp2fillfloorimagisfinitelgammaloglog1plog2log10negreal
reciprocalroundsignsignbitsinsinhspherical_bessel_j0sqrttantanhtruncaddatan2bitwise_and
bitwise_orbitwise_xordiveqfmaxfminfmodgcdgegthypotigammaigammacleltmaximumminimummulne	nextafterpow	remainderrsqrt
shift_leftshift_right_arithmeticshift_right_logicalsubzeta
as_stridedbroadcast_in_dimcollapse_viewconjexpand_dimssliceslice_in_dim	split_dimsqueeze	transposeview_ofcollapsecatreshaperevwhereconvert_element_type
device_putitemmaximum_valueminimum_valueto_dtypecopy_toresizeamaxaminprodsumvarempty_stridedscalar_tensorarangesvdnormaluniformfft_r2cfft_c2cfft_c2rshapestridesdtypedevice)
tensorliker   r   r   r   c          	      C   s<  t | tr^|s |d ks$t |ts$t|s:|d ks>t |ts>td}d}tt| }td}nl| d k	rt | tjsvtt	| j
}t	|  }| j}| j}n0|d k	st|d k	st|d k	st|d k	st|d kr|nt	|}|d kr|nt	|}|d kr|n|}|d kr|n|}t |tr*t|}tj||||dS )N cpur   r   )
isinstancer   r	   AssertionErrorr   typetorchr   r   tupler   strider   strr   )	r   r   r   r   r   Zinferred_shapeZinferred_stridesZinferred_dtypeZinferred_devicer   r   9/tmp/pip-unpacked-wheel-gikjz4vx/torch/_prims/__init__.py
TensorMeta   s0    


r   .)schemareturn_typemeta	impl_atendocc           
         s   t |  fdd fdd}fdd}| dd }t| t|| t| ttj	j
|}|j dd	lm tfd
d jjD st|| | fD ]"}	||	_||	_| |	_|	_|	_q S )z)
    Creates a primitive operation.

    c                     s   | |  | |S Nr   argskwargs)r   r   r   r   
_prim_impl  s    
z_make_prim.<locals>._prim_implc                     s   t  | |S r   r   r   )_primr   r   _autograd_impl  s    z"_make_prim.<locals>._autograd_implc                     s0   | dr"|d jdkr"| |S  | |S d S )Nr   r   )getr   r   )r   r   r   r   _backend_select_impl  s    
z(_make_prim.<locals>._backend_select_impl(r   contains_tensor_typesc                 3   s   | ]} |j V  qd S r   )r   ).0ar   r   r   	<genexpr>&  s     z_make_prim.<locals>.<genexpr>)primZdefinesplit	prim_implimplprim_autograd_implprim_meta_implgetattrr   opsr%   defaulttorch._subclasses.fake_tensorr   anyZ_schema	argumentsprim_backend_select_impl__doc__r   r   )
r   r   r   r   r   r   r   nameZ_prim_packetpr   )r   r   r   r   r   r   
_make_prim   s(    
r   c                   @   s   e Zd ZdZdZdZdS )$ELEMENTWISE_PRIM_TYPE_PROMOTION_KIND)r   )   )   N)__name__
__module____qualname__DEFAULTALWAYS_BOOLCOMPLEX_TO_FLOATr   r   r   r   r   4  s   r   )args_with_fixed_dtypes)type_promotionr   returnc                 G   s  t |dksttj|  t|}|dk	r4|| tj|ddi tj|ddi tj| }tj	|ddi}d}d}|D ]>}t
|trt|s|j} qq|j}qzt
|trzt|}qz|dkr|dk	rt|}d}	d}
|D ]2}t
|tr|j}	 qqt
|tr|
dkr|}
q|	dk	r|dk	s,t| tjkr>|}n<| tjkrRtj}n(| tjkrzt|rvt|}n|}t|	|||dS t
|
tjrtdt
|
tjrtdt|
S )z
    Meta function for elementwise operations that produce outputs in the same dtype
    as their inputs.

    Stride logic is currently incorrect.
    r   NZallow_cpu_scalar_tensorsT)r   r   r   r   ZNYI)lenr   utilsZcheck_same_dtypelistextendZcheck_same_deviceZcheck_same_shape"compute_elementwise_output_stridesZextract_shaper   r   Zis_cpu_scalar_tensorr   r   r   r   r   r   r   r   r   boolr   is_complex_dtypecorresponding_real_dtyper   Z
SymIntNodeZSymFloatNode)r   r   r   Zargs_r   r   r   Zscalar_typeargr   numberr   r   r   _elementwise_meta;  sX    










r   c                  O   s&   t t | d jdd  t| |S )Nr   c                   S   s   dS )NzOnly complex dtype is supportedr   r   r   r   r   <lambda>      z0_complex_only_elementwise_meta.<locals>.<lambda>)r   r   r   r   r   r   r   r   r   _complex_only_elementwise_meta  s
     r   )r   r   c                K   s&   t f |  dtt|dtjd|S )z,
    Creates an elementwise unary prim.
    z(Tensor self) -> Tensorr   r   r   r   r   r   r   r   NEWr   r   r   r   r   r   _make_elementwise_unary_prim  s    
r   c                K   s&   t f |  dtt|dtjd|S )z-
    Creates an elementwise binary prim.
    z%(Tensor self, Tensor other) -> Tensorr   r   r   r   r   r   r   _make_elementwise_binary_prim  s    
r   c                  O   s   t d S r   )NotImplementedErrorr   r   r   r   	_not_impl  s    r    )r   r   r   )r   r   c                 C   s.   t |   dd  tt|  d| S )Nc                   S   s   dS )NzJcbrt: Complex inputs not supported. Consider calling torch.pow(a, 1.0/3.0)r   r   r   r   r   r   ,  r   z_cbrt_aten.<locals>.<lambda>gUUUUUU?)r   r   
is_complexr   copysignrm   r&   r   r   r   r   
_cbrt_aten)  s
    
r   )inputr   c                 C   s&   | j jstdt| }t| |dS )Nz6prims.conj_physical is only defined for complex dtypes)r   )r   r   RuntimeErrorr   r   r   )r   r   r   r   r   _conj_physical_metaF  s    
r   z$conj_physical(Tensor self) -> Tensorz4Returns the physical conjugation of a complex tensor)r   r   r   r   r   )r   valuer   c                 C   s   t | tjdS )Nr   r   r   r   )r   r   r   r   r   
_fill_meta  s     r   c              	   C   s*   | d }t   || W 5 Q R X |S NF)r   no_gradZfill_)r   r   tr   r   r   
_fill_aten  s    
r  z)fill(Tensor self, Scalar value) -> Tensorzimag(Tensor self) -> Tensorr   )r   r   r   r   r   zreal(Tensor self) -> Tensor)r   r   r   r   c                 C   sJ   t | ttfp$t | tjo$t| j}|r:tj| |ddS t	| |S d S )NrU   )Zrounding_mode)
r   r   intr   r   r   Zis_integer_dtyper   r[   Ztrue_divide)r   bZis_integralr   r   r   	_div_atenp  s    r  )r   r  r   c                 C   sZ   t | tr(t |tr(t|| j| jd}n&t |trNt | trNt| |j|jd} t| |S Nr   )r   r   r   r   r   r   r   rh   r   r  r   r   r   _maximum_aten  s
    r	  c                 C   sZ   t | tr(t |tr(t|| j| jd}n&t |trNt | trNt| |j|jd} t| |S r  )r   r   r   r   r   r   r   ri   r  r   r   r   _minimum_aten  s
    r
  )r   sizer   storage_offsetr   c                 C   st   t |t |kst|dks tt| t| ttj|dkrFn t| t	j
rft|  ||| t| ||dS )Nr   r   r   )r   r   r   Zvalidate_stridesvalidate_shaper   operatorrj   r   r   r   Zcheck_in_bounds_for_storageZstorager   r   r  r   r  r   r   r   _as_strided_metaF  s    

r  c                 C   s   t | |||S r   )r   ru   r  r   r   r   _as_strided_atenX  s    r  zy
    Creates a view of the tensor with the given shape (size), strides (stride) and
    storage offset (storage_offset).
z]as_strided(Tensor(a!) a, SymInt[] size, SymInt[] stride, SymInt storage_offset) -> Tensor(a!)r   r   r   r   r   )r   r   broadcast_dimensionsc                    s  t | tstt tstt |ts*t| jt|ks<tt| jksNtfdd t fdd|d t|D ],\}}| j| dksv| j| | ksvtqvg }d}t	tD ]P}||kr| j| | kr|
d n|
|  |  |d }q|
d qt| |dS )	Nc                    s.   t |tst|| kst|t k s*t|S r   )r   r  r   r   accx)r   r   r   _greater_than_reduce}  s    z4_broadcast_in_dim_meta.<locals>._greater_than_reducec                    s
    | |S r   r   r  )r  r   r   r     r   z(_broadcast_in_dim_meta.<locals>.<lambda>   r   r  )r   r   r   r	   ndimr   r   	enumerater   rangeappendr   r   )r   r   r  idxZnew_idxnew_stridesZoriginal_idxr   )r  r   r   _broadcast_in_dim_metal  s&    &
r!  c                 C   sL   t |}|D ]}d||< q| }t|D ]\}}|dkr&||}q&||S Nr  )r   r  Z	unsqueezeexpand)r   r   r  sZbroadcast_dimensionvr  r  r   r   r   _broadcast_in_dim_aten  s    
r&  aD  
  Creates a view of a with the specified shape.

  Allows adding dimensions of any length and broadcasting
  dimensions of length one in a to any length.

  The location of the broadcast dimensions must be specified
  using the broadcast_dimensions argument. Changing the
  relative order of dimensions is not supported.
  zVbroadcast_in_dim(Tensor(a) a, SymInt[] shape, int[] broadcast_dimensions) -> Tensor(a))r   startendr   c                 C   s  t | tst| jdkr"d}d}n| j}|  }tt|| t	t|| ||krld
||}t|| jdks|d |kr||fS ||d  }||d  }tt||d D ]}|| dks||d  dkrd}d} qN|| dkrq|||  }t||| }|  dkr||d  dkr|| ||d  ||d   ks dS q|d | |f ||d   }	|d | |f ||d   }
|  dkrt|	}
|	|
fS )Nr   r  zIAttempting to collapse but end, {0}, is less than or equal to start, {1}!r  )NN)r   r   r   r  r   r   r   validate_idxr   Zvalidate_exclusive_idxformat
ValueErrorreversedr  minnumelmake_contiguous_strides_for)r   r'  r(  r   r   msglengthr   r  	new_shaper   r   r   r   _collapse_view_helper  sN    
 

r4  c                 C   s2   t | ||\}}|d kr$d}t|t| ||dS )Nz?Attempting to view a collapsed tensor, but no such view exists!r  )r4  r,  r   )r   r'  r(  r3  r   r1  r   r   r   _collapse_view_meta  s
    r5  c                 C   s^   | j dkrd}n| j}d}t||D ]}|||  }q$|d| |f ||d   }| |S )Nr   r)  r  )r  r   r  view)r   r'  r(  r   
dim_lengthr  r3  r   r   r   _collapse_view_aten   s    
r8  a  
  Creates a view of a with the dimensions between
  start (inclusive) and end (exclusive) merged into a
  single dimension.

  If it's not possible to take such a view then an error
  is thrown. See collapse instead.

  The dimensions can be merged if and only if
  they are all "nested" with each other. That is, they all
  have the property that

  stride[i] = stride[i+1] * shape[i+1]

  for all i in [start, end - 1).
  z;collapse_view(Tensor(a) a, int start, int end) -> Tensor(a)c                 C   s   | j jstdt| S )Nz$Expected complex dtype in prims.conj)r   r   r   r   r   r   r   r   
_conj_meta*  s    r9  z2
Returns a conjugated view of the original tensor
zconj(Tensor(a) a) -> Tensor(a))r   
dimensionsr   c                    s   |dk	rt t| }nt t| j }tt|t|krVdt }t|t	| j
}|D ]}||d qd fddtt|D }t| ||S )z
    Creates a view of a with a.ndim + len(dimensions) dimensions, with new
    dimensions of length one at the dimensions specified by dimensions.
    Nz.Received duplicate dimensions to expand in {0}r  c                    s   g | ]}| kr|qS r   r   )r   r  r:  r   r   
<listcomp>Q  s     zexpand_dims.<locals>.<listcomp>)sortedr   canonicalize_dimsr  r   setr+  r   r,  r   r   insertr  rv   )r   r:  r  dimsr1  r3  r  r  r   r;  r   ry   =  s    


pyslice)r   start_indiceslimit_indicesr   r   c                 C   s  |d k	r|ndgt | }| jt |krBd| jt |}t|| jt |krjd| jt |}t|| jt |krd| jt |}t|t|| jD ]@\}}|dk rd|}t|||krd|| j}t|qt|| j|D ]Z\}}}|dk rd|}t|||kr4d	|| j}t|||k rd
||}q|D ]"}|dkrNd|}t|qNg }	t|||D ]$\}}}|	t|| |  qg }
t| 	 |D ]\}}|
||  qt
| |	|
dS )Nr  zHAttempting to slice tensor of rank {0} with start_indices of length {1}!zHAttempting to slice tensor of rank {0} with limit_indices of length {1}!zBAttempting to slice tensor of rank {0} with strides of length {1}!r   z@Attempting to slice a tensor with a negative start index of {0}!z|Attempting to slice a tensor but a start index in {0} is greater than the length of its corresponding dimension in shape {1}z?Attempting to slice a tensor with a negative stop index of {0}!z|Attempting to slice a tensor but a stop index in {0} is greater than the length of  its corresponding dimension in shape {1}zgAttempting to slice a tensor but a start index in {0} is greater than  its corresponding stop index {1}z=Attempting to slice a tensor with a non-positive step of {0}!r  )r   r  r+  r,  zipr   r  mathrA   r   r   )r   rC  rD  r   _stridesr1  r  yzr3  r   r   r   r   _slice_meta[  s        


  
rJ  c           	      C   sT   |d k	r|ndgt | }g }t|||D ]\}}}|t||| q*t| |S )Nr  )r   rE  r  rB  r  getitem)	r   rC  rD  r   rG  Zslicesr'  stopstepr   r   r   _slice_aten  s
    rN  a  
    Creates a view of a "bounding box" within the tensor.

    The bounding box is specified independently in each of the tensor's dimensions.
    start_indices and limit_indices describe the box's boundaries for their corresponding
    dimensions. If strides is specified then they specify the step size between elements
    in their corresponding dimension.

    This operation is analogous to slicing in NumPy, but does not permit slices where
    the stop indices are less than the start indices.
    zgslice(Tensor(a) a, SymInt[] start_indices, SymInt[] limit_indices, SymInt[]? strides=None) -> Tensor(a)r  )r   start_indexlimit_indexr   axisr   c           	      C   s  |dk rd |}t||| jkr:d || j}t||dk rTd |}t||| j| krvd ||}t||| j| krd ||}t|||k rd ||}t||dk rd |}t|dg| j }t| j}d	g| j }|||< |||< |||< t| |||S )
Nr   z*slice_in_dim: received a negative axis {0}zHslice_in_dim: axis {0} is greater or equal to the rank {1} of the tensorz1slice_in_dim: received a negative start_index {0}zIslice_in_dim: start_index is greater than the length {0} of dimension {1}zIslice_in_dim: limit_index is greater than the length {0} of dimension {1}zFslice_in_dim: received a limit_index {0} less than the start_index {1}z4slice_in_dim: received a non-positive stride of {0}!r  )r+  r,  r  r   r   rJ  )	r   rO  rP  r   rQ  r1  rC  rD  r   r   r   r   _slice_in_dim_meta  sP    

 
   

rR  c                 C   sH   dg| j  }t| j}dg| j  }|||< |||< |||< t| |||S Nr   r  )r  r   r   rz   )r   rO  rP  r   rQ  rC  rD  r   r   r   r   _slice_in_dim_aten  s    
rT  zI
    Convenience wrapper for slicing just one dimension using slice.
    zhslice_in_dim(Tensor(a) a, SymInt start_index, SymInt limit_index, int stride=1, int axis=0) -> Tensor(a))r   dimouter_lengthr   c                 C   s   t | tstt| j| t| | j| | }| j| | dkr`d| j| |}t	|g }g }t
| jD ]`}||kr|||f ||  | | |  | f qr|| j|  ||  |  qrt| ||dS )Nr   zaAttempting to split dimension of length {0}, but outer length of {1} divides it with a remainder!r  )r   r   r   r   r*  r  Zvalidate_dim_lengthr   r+  r,  r  r   r   r  r   )r   rU  rV  inner_lengthr1  r3  r   r  r   r   r   _split_dim_meta'  s&    
 $rX  c                 C   s@   | j | | }| j d| ||f | j |d d   }| |S rS  )r   r6  )r   rU  rV  rW  r3  r   r   r   _split_dim_atenB  s    (rY  z
  Creates a view of a with the given dimension (of length l) split
  into two dimensions, with the outer of the two having
  length outer_length and the inner of the two having computed
  length inner_length such outer_length * inner_length = l.
  zAsplit_dim(Tensor(a) a, int dim, SymInt outer_length) -> Tensor(a)c                 C   s   t | tst|D ]$}t| j| | j| dkstqg }g }tt| jD ]0}||kr\qN|	| j|  |	| 
 |  qNt| ||dS )Nr  r  )r   r   r   r   r*  r  r   r  r   r  r   r   )r   r:  r  r3  r   r   r   r   _squeeze_metaZ  s    rZ  c                 C   s$   t t|D ]}tj| |d} q| S )N)rU  )r-  r=  r   r}   )r   r:  r  r   r   r   _squeeze_atenm  s    r[  z~
  Creates a view of the tensor with the specified dimensions removed.

  The removed dimensions must each have length one.
  z3squeeze(Tensor(a) a, int[] dimensions) -> Tensor(a))r   permutationr   c                 C   s   | j t|kr(d| j t|}t|t| j |sHd|}t|dg| j  }dg| j  }t|D ]&\}}| j| ||< |  | ||< qht	| t
|t
|dS )NzUAttempting to permute a tensor of rank {0}, but received a permutation of length {1}!z%Received an invalid permutation, {0}!r   r  )r  r   r+  r,  r   Zis_valid_permutationr  r   r   r   r   )r   r\  r1  r3  r   r  rU  r   r   r   _transpose_meta  s     
r]  c                 C   s   t | |S r   )r   Zpermute)r   r\  r   r   r   _transpose_aten  s    r^  z
    Creates a view of the tensor with its dimensions permuted.

    The length of the permutation must be the rank of the tensor,
    and each element of the permutation specifies the new order
    for the corresponding dimension.
    z6transpose(Tensor(a) a, int[] permutation) -> Tensor(a)c                 C   s   t | S r   r   r   r   r   r   _view_of_meta  s    r`  c                 C   s   |  | jS r   )r6  r   r   r   r   r   _view_of_aten  s    ra  z'
    Creates a view of the tensor.
    zview_of(Tensor(a) a) -> Tensorc                 C   sN   d}t ||D ]}|| j|  }q| jd| |f | j|d  }t| |S )z
    Wrapper around reshape that collapses a span of dimensions.

    See collapse_view for the corresponding view operation.
    r  r   N)r  r   r   )r   r'  r(  r7  r  r3  r   r   r   r     s
    ")tensorsrU  r   c           
      C   s   | d j }d}t| D ]P\}}tt||j D ]6\}\}}||krL|| }q.||kr.td| dq.qt| d j  }	||	|< t| d |	t|	dS )Nr   z0Sizes of tensors must match except in dimension zV. Expected {common_length} but got {length} for tensor number {tensor_idx} in the listr  )	r   r  rE  r   r   copyr   r   r0  )
rb  rU  r   Zconcat_lengthZ
tensor_idxZtensorr  Zcommon_lengthr2  r3  r   r   r   	_cat_meta  s"    


rd  c                 C   s   t | |S r   )r   r   )rb  rU  r   r   r   	_cat_aten  s    re  z
  Concatenates tensors along the specified dimension.

  The tensors' shapes must have the same rank and same length for other dimensions.
  z(cat(Tensor[] tensors, int dim) -> Tensorr   r   c                 C   s\   t | tstt| ttj|}||  krHd	|  |}t
|t| |t|dS )NzNAttempting to reshape a tensor with {0} elements to a shape with {1} elements!r  )r   r   r   r   r  r   r  rj   r/  r+  r,  r   r0  )r   r   r/  r1  r   r   r   _reshape_meta  s    
 rg  )r   r   r   c                 C   s   |  |  S r   )r   
contiguousclonerf  r   r   r   _reshape_aten  s    rj  z`
  Creates a contiguous tensor with the specified shape
  containing a copy of the data in a.
  z+reshape(Tensor a, SymInt[] shape) -> Tensor)r   rA  r   c                 C   s   t | j| t| S r   )r   Zvalidate_dimension_indicesr  r   )r   rA  r   r   r   	_rev_meta  s    rk  zD
    Reverses the order of elements along the given dimensions.
    z#rev(Tensor a, int[] dims) -> Tensor)predr   r  r   c                 C   s   t ||tj| fdS )N)r   r   r   )rl  r   r  r   r   r   _where_meta5  s    rm  z
  Selects elements from a and b according to pred.

  Where pred is true the result contains the element from a, and
  where pred is false the result contains the element from b.
  z0where(Tensor pred, Tensor a, Tensor b) -> Tensor)r   r   r   c                 C   s6   t | tstt |tjstt| }t| ||dS N)r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   _convert_element_type_metaS  s    
rp  c              
   C   s   t |sd}n2z
| j}W n& tk
r@ } zd}W 5 d }~X Y nX tj| | j||d}t  t|| W  5 Q R  S Q R X d S )NF)r   r   requires_grad)	r   Zis_grad_dtyperq  	Exceptionr   Z
empty_liker   r  r   )r   r   rq  eresultr   r   r   _convert_element_type_aten]  s    

   
ru  z6
  Creates a copy of a tensor with the given dtype.
  z:convert_element_type(Tensor a, ScalarType dtype) -> Tensor)r   r   r   c                 C   s4   t | tstt |ttjfs"tt| t|dS )N)r   )	r   r   r   r   r   r   r   r   Zcanonicalize_devicer   r   r   r   r   _device_put_meta}  s    rw  c                 C   s
   |  |S r   torv  r   r   r   _device_put_aten  s    rz  z5
  Creates a copy of a tensor on the given device.
  z-device_put(Tensor a, Device device) -> Tensorc                 C   s   t | j}t|dS r"  )r   dtype_to_typer   r   )r   number_typer   r   r   
_item_meta  s    r}  z<
    Converts a tensor with one element to a Python number.
zitem(Tensor a) -> Scalar)r   r   c                 C   s   t | }t|dS r"  r   r{  r   r   r|  r   r   r   _maximum_value_meta  s    
r  r   c                 C   s6   | t jkrdS | js| jr&t | jS t | jS d S )NT)r   r   r   is_floating_pointfinfomaxiinfor  r   r   r   _maximum_value_aten  s
    
r  z2
    Return the maximum finite value for a dtype.
z)maximum_value(ScalarType dtype) -> Scalarc                 C   s   t | }t|dS r"  r~  r  r   r   r   _minimum_value_meta  s    
r  c                 C   s6   | t jkrdS | js| jr&t | jS t | jS d S r   )r   r   r   r  r  r.  r  r  r   r   r   _minimum_value_aten  s
    
r  z2
    Return the mimimum finite value for a dtype.
z(minium_value(ScalarType dtype) -> Scalarc                 C   s   t | j}t| ||dS rn  )r   r0  r   r   ro  r   r   r   _to_dtype_meta  s    r  c                 C   s
   |  |S r   rx  )r   r   r   r   r   _to_dtype_aten  s    r  zA
    Creates a contiguous copy of a tensor with the given dtype.
z.to_dtype(Tensor a, ScalarType dtype) -> Tensorr  c                 C   sL   t | tstt |tst|  | krHd| |  }t|| S )Nz>Attempting to copy {0} elements to a tensor with {1} elements!)r   r   r   r/  r+  r   )r   r  r1  r   r   r   _copy_to_meta  s    
 r  c                 C   s
   |  |S r   )Zcopy_r  r   r   r   _copy_to_aten  s    r  z;
  Copies the data in b to a and returns the modified a.
  z-copy_to(Tensor(a!) a, Tensor b) -> Tensor(a!)c                 C   s
   |  |S r   Zresize_rf  r   r   r   _resize_meta-  s    r  c                 C   s
   |  |S r   r  rf  r   r   r   _resize_aten1  s    r  z
  Gives a tensor with no elements a new shape, returning the modified tensor.

  The tensor's strides are contiguous and its values are unitialized.
  z2resize(Tensor(a!) a, SymInt[] shape) -> Tensor(a!)output_dtypec                C   sB   t | tst|dkr| j}t| j|}t|t||| j	dS )z\
    Meta function for single output reduction operations
    Stride logic is incorrect
    Nr   )
r   r   r   r   r   Zcompute_reduction_output_shaper   r   r0  r   )inprA  r  Zoutput_shaper   r   r   _reduction_metaE  s    r  c                C   s.   t | jrt | j}n| j}t| ||dS )Nr  )r   r   r   r   r  )r  rA  Z
correctionr  r   r   r   _var_reduction_metaV  s    r  zx
    Computes the sum of elements in the input tensor over the list of dimensions
    specified in the dim argument
    z|
    Computes the product of elements in the input tensor over the list of dimensions
    specified in the dim argument
    z
    Computes the maximum value of elements in the input tensor over the list of dimensions
    specified in the dim argument
    z
    Computes the minimum value of elements in the input tensor over the list of dimensions
    specified in the dim argument
    ze
    Computes the biased variance of x over the list of dimensions specified in the dim argument
    )r   c                 C   s   t |  dt|tj|dS )Creates a reduction prim.zE(Tensor inp, int[]? dims, *, ScalarType? output_dtype=None) -> Tensorr  )r   r  r   r   r   r   r   r   r   r   _make_reduction_prims  s    r  c                 C   s   t |  dt|tj|dS )r  zU(Tensor inp, int[]? dims, *, int correction, ScalarType? output_dtype=None) -> Tensorr  )r   r  r   r   r  r   r   r   _make_var_reduction_prim~  s    r  r  )r  rA  r   r   c                C   sN   |d k	r:t |ddD ] }|dks$ttj| ||d} q| S tj| ||dS d S )NT)reverser   r  )r=  r   r   r   )r  rA  r   dr   r   r   
_prod_aten  s    r  z
    Constructs a 1-D tensor with values from the interval [start, end) taken
    with common difference `step` beginning from `start`.
)r'  r(  rM  r   r   rq  r   c                   s   t tr"t  tr"t |tr"tt|dkdd  ttoLt  fdd t|dkrp kp~|dk o~ kdd  |d k	rn(tdd  |fD rtj	}nt
 }|d krt n|}t  | f}t|}t||||dS )	Nr   c                   S   s   dS )Nzstep must be nonzeror   r   r   r   r   r     r   z_arange_meta.<locals>.<lambda>c                      s   d d  S )Nzunsupported range: z -> r   r   r(  r'  r   r   r     r   c                   S   s   dS )Nz7upper bound and lower bound inconsistent with step signr   r   r   r   r   r     r   c                 s   s   | ]}t |tV  qd S r   )r   r  )r   r   r   r   r   r     s     z_arange_meta.<locals>.<genexpr>r   )r   complexr   r   r   rF  rC   allr   Zint64Zget_default_dtyper   r7   r0  r   )r'  r(  rM  r   r   rq  r   r   r   r  r   _arange_meta  s6    

r  c             
   C   s   t j| ||||t jd|dS )NF)r   r   ZlayoutZ
pin_memoryrq  )r   r   Zstrided)r'  r(  rM  r   r   rq  r   r   r   _arange_aten  s    
r  zqarange(Scalar start, Scalar end, Scalar step, *, ScalarType? dtype, Device? device, bool requires_grad) -> Tensor)r   r   r   rq  r   c                C   s   t | }t| |||dS Nr   r   r0  r   )r   r   r   rq  r   r   r   r   _empty_meta		  s    
r  c                C   s   t j| |||dS N)r   r   rq  )r   empty)r   r   r   rq  r   r   r   _empty_aten	  s    r  z\
    Creates a tensor with uninitialized values and the specified shape, dtype, and device.
zWempty(SymInt[] shape, *, ScalarType dtype, Device device, bool requires_grad) -> Tensor)r   r   r   r   rq  r   c                C   s   t | |||dS r  r_  )r   r   r   r   rq  r   r   r   _empty_strided_meta#	  s    r  z1
    Creates a tensor with uninitialized values.
zqempty_strided(SymInt[] shape, SymInt[] strides, *, ScalarType dtype, Device device, bool requires_grad) -> Tensor)r   
fill_valuer   r   rq  r   c                C   s   t | }t| |||dS r  r  )r   r  r   r   rq  r   r   r   r   
_full_meta<	  s    
r  c                C   s   t j| ||||dS r  )r   full)r   r  r   r   rq  r   r   r   
_full_atenH	  s    	    r  zi
    Creates a tensor filled with the given fill value, and with the specified shape, dtype, and device.
zifull(SymInt[] shape, Scalar fill_value, *, ScalarType dtype, Device device, bool requires_grad) -> Tensor)r   r  r   r   rq  r   c                C   s.   t | }|  dkr|  }t| |||dS )Nr   )r   r   r   )r   r   r/  r   r   )r   r  r   r   rq  r   r   r   r   _full_like_metad	  s    
r  c                C   s   t j| ||||dS r  )r   	full_like)r   r  r   r   rq  r   r   r   _full_like_atens	  s    	    r  z
    Creates a tensor filled with the given fill value, and the same shape, dtype, and device as the
    given tensor by default. The dtype and device settings can be overridden
    by specifying them explicitly.
zhfull_like(Tensor a, Scalar fill_value, *, ScalarType dtype, Device device, bool requires_grad) -> Tensor)scalarr   r   r   c                C   s    g }t |}t| ||||dS r  r  )r  r   r   r   r   r   r   r   _scalar_tensor_meta	  s    
r  c                C   s4   t | tr$|d kst|s$tdtj| ||dS )Nz-Complex scalar requires complex tensor dtype.r   )r   r  r   r   	TypeErrorr   r   )r  r   r   r   r   r   _scalar_tensor_aten	  s    
r  zG
    Wraps a Number into a Tensor with the specified dtype and device.
zQscalar_tensor(Scalar s, *, ScalarType? dtype=None, Device? device=None) -> Tensor)Afull_matricesr   c                C   s  t | d t j| jddd | j}|d d }|dd  \}}t||}|||rV|n|f }t j|dd}t||| j| jd}	||f }
t |
}t|
|| 	 rt 
| jn| j| jd}||r|n||f }| jjdk}t j||d}t||| j| jd}|	||fS )Nz
linalg.svdF)Zallow_low_precision_dtypes)Z	row_majorr   Zcuda)r   Zcheck_is_matrixZcheck_fp_or_complexr   r   r.  r0  r   r   r   r   r   )r  r  ZA_shapebatchmnkZshape_UZ	strides_UUZshape_SZ	strides_SSZshape_VhZis_cudaZ
strides_VhZVhr   r   r   	_svd_meta	  s,    


r  c                C   s   t jj| |dS )N)r  )r   Zlinalgr   )r  r  r   r   r   	_svd_aten	  s    r  z
    Returns the SVD of a matrix or batch of matrices.

    The `full_matrices` flag controls whether the full or reduced SVD decomposition is returned.
zGsvd(Tensor A, *, bool full_matrices) -> (Tensor U, Tensor S, Tensor Vh))r   meanstdr   r   rq  r   c                   sV   t dkfdd t t  p.t   fdd t | }t| | |dS )Ng        c                      s
   d  S )Nz6expected non-negative standard deviation, but got std=r   r   )r  r   r   r   
  r   z_normal_meta.<locals>.<lambda>c                      s
   d  S )Nz:expected a floating-point or complex dtype, but got dtype=r   r   r  r   r   r   
  r   r   )r   r   Zis_float_dtyper   r0  r   )r   r  r  r   r   rq  r   r   )r   r  r   _normal_meta	  s    	


r  c             	   C   s6   t j| |||d}t   ||| W 5 Q R X |S r  )r   r  r  Znormal_)r   r  r  r   r   rq  r   r   r   r   _normal_aten
  s    	
r  z
    Constructs a tensor filled with values drawn from a normal distribution with the specified mean
    and standard deviation.

    Only supports floating-point types.
zrnormal(SymInt[] shape, *, Scalar mean, Scalar std, ScalarType dtype, Device device,  bool requires_grad) -> Tensor)r   lowhighr   r   r   c                C   s   t | }t| |||dS r  r  )r   r  r  r   r   r   r   r   r   _uniform_meta1
  s    
r  c                C   s    t j| ||d}||| |S r  )r   r  Zuniform_)r   r  r  r   r   r   r   r   r   _uniform_aten=
  s    r  zN
    Constructs a tensor filled with values drawn uniformly from low to high.
z^uniform(SymInt[] shape, *, Scalar low, Scalar high, ScalarType dtype, Device device) -> Tensor)r   rU  onesidedr   c                C   sj   t | j|}t | t| j}|rB|d }|| d d ||< t | j}t |}t	|||| j
dS )Nr  r   r  r   )r   r>  r  validate_no_repeating_dimsr   r   Zcorresponding_complex_dtyper   r0  r   r   )r   rU  r  r   Zlast_dimr   r   r   r   r   _fft_r2c_meta^
  s    


r  c                C   s   d}t | |||S Nr   )r   Z_fft_r2c)r   rU  r  normalizationr   r   r   _fft_r2c_atenq
  s    r  z7
    Performs a real to complex Fast Fourier Transform
z;fft_r2c(Tensor self, *, int[] dim, bool onesided) -> Tensor)r   rU  forwardr   c                C   s<   t | j|}t | | j}t |}t||| j| jdS r  )	r   r>  r  r  r   r0  r   r   r   )r   rU  r  r   r   r   r   r   _fft_c2c_meta
  s    

   r  c                C   s   d}t | |||S r  )r   Z_fft_c2c)r   rU  r  r  r   r   r   _fft_c2c_aten
  s    r  z>
    Performs either a Fast Fourier Transform, or its inverse
z:fft_c2c(Tensor self, *, int[] dim, bool forward) -> Tensor)r   rU  last_dim_sizer   c                C   sV   t | j|}t | t| j}|||d < t | j}t |}t	|||| j
dS )Nr  r   )r   r>  r  r  r   r   r   r   r0  r   r   )r   rU  r  r   r   r   r   r   r   _fft_c2r_meta
  s    


r  c                C   s   d}t | |||S r  )r   Z_fft_c2r)r   rU  r  r  r   r   r   _fft_c2r_aten
  s    r  z?
    Performs a complex to real Inverse Fast Fourier Transform
zBfft_c2r(Tensor self, *, int[] dim, SymInt last_dim_size) -> Tensor)N)N)N)N)r  r   )r  r   (K  
contextlib	itertoolsrF  r  weakrefenumr   	functoolsr   r   typingr   r   r   r   r	   r
   r   r   r   Ztorch._prims_commonZ_prims_commonr   Ztorch.libraryr   r   Ztorch._Cr   Ztorch._prims.nvfuser_primsr   r   r   r   r   r   r   r   r   r   r   r   Ztorch._prims_common.wrappersr   r   r   r   Ztorch.overridesr    r!   Ztorch.utils._pytreer"   r#   r$   ZlibraryLibraryr   r   r   r   r   __all__r   r   r   r   r   r   r   r   r   r   r   r&   r   r'   r   r(   r)   r*   r+   r,   r-   r.   specialr3   r4   Zi0r/   Zi0er0   i1r1   Zi1er2   r5   r   r6   r7   r   Z_conj_physicalr   r8   r9   r:   Zerfinvr;   r<   r=   r>   r?   r   r  r@   rA   ZVIEWrB   rC   r   rD   rE   rF   rG   rH   rJ   rK   rI   rL   ro   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r  r[   r\   r]   r^   r_   r`   ra   rb   rc   Zgammaincrd   Z	gammainccre   rf   rg   r	  rh   r
  ri   rj   rk   rl   rm   rn   Zbitwise_left_shiftrp   Zbitwise_right_shiftrq   rr   rs   rt   r  r  r  Z_as_strided_docru   r!  r&  Z_broadcast_in_dim_docrv   r4  r5  r8  Z_collapse_view_docrw   r9  Z	_conj_docrx   ry   rz   rB  __annotations__rJ  rN  Z
_slice_docrR  rT  Z_slice_in_dim_docr{   rX  rY  Z_split_dim_docr|   rZ  r[  Z_squeeze_docr}   r]  r^  Z_transpose_docr~   r`  ra  Z_view_of_docr   r   rd  re  Z_cat_docr   rg  rj  Z_reshape_docr   rk  Z_rev_docZflipr   rm  Z
_where_docr   rp  ru  Z_convert_element_type_docr   rw  rz  Z_device_put_docr   r}  Z	_item_docr   r  r  Z_maximum_value_docr   r  r  Z_minimum_value_docr   r  r  Z_to_dtype_docr   r  r  Z_copy_to_docZINPLACEr   r  r  Z_resize_docr   r  r  Z_sum_docZ	_prod_docZ	_amax_docZ	_amin_docZ_var_docr  r  r   r  r   r   r   r   Z_arange_docr   r  r  r   r  r  Z
_empty_docr  r  Z_empty_strided_docr   r  r  Z	_full_docr  r  r  Z_full_like_docr  r  r  Z_scalar_tensor_docr   r  r  Z_svd_docr   floatr  r  r  Z_normal_docr   r  r  Z_uniform_docr   r  r  Z_fft_r2c_docr   r  r  Z_fft_c2c_docr   r  r  Z_fft_c2r_docr   r   r   r   r   <module>   s	   (4  %-;

O  	

 
	
 
      
  /
  :
	
 W 9 				,		  

 	"

					




	'      



 " 




