
    Vh                         d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
 d Zd Zd Zd Z	 ddZdd	Zdd
Zd Zd Zd Zd Zd Zed        Z	 ddZd Zd Zd ZddZd Zd Zd ZddZ ddZ!y)    N)make_tensor)BinaryUfuncInfo
ErrorInput#generate_elementwise_binary_tensorsReductionOpInfosample_inputs_reductionSampleInputc                 \      fd} fd}t        |t              r	 ||       y  ||       y )Nc                    	  | j                   j                  g| j                   j                  i | j                   j                   t	        j
                   d| j                   d| j                   d| j                   d      # | j                  $ r Y y t        $ r\}t	        j
                   d| j                   d| j                   d| j                   dt        |      j                   d|       d }~ww xY w)Nz on sample.sample_input=z expected exception : z, got none.z, got )sample_inputinputargskwargsAssertionErrorname
error_typeerror_regex	Exceptiontype__name__samplemsgop_infos     a/home/dcms/DCMS/lib/python3.12/site-packages/torch/testing/_internal/opinfo/definitions/sparse.py_check_failz$_check_validate.<locals>._check_fail   s   	##))$$)) %%,, !<<. 9V%8%8$::N$$%R(:(:';;H     	 	 <<. 9V%8%8$::N$$%R(:(:';6$s)BTBTAUUWX[W\^ 	s   AB
 
C?C?#AC::C?c           
          	  | j                   g| j                  i | j                   y # t        $ r8}t	        j
                   d| dt        |      j                   d|       d }~ww xY w)Nz on sample=z expected to succeed , got r   )r   r   r   r   r   r   r   r   r   s     r   _check_successz'_check_validate.<locals>._check_success&   sr    	FLL@6;;@&--@ 	 <<.VI .c++,Bse5 	s   *. 	A/3A**A/)
isinstancer   )r   r   r   r   s   `   r   _check_validater!      s)    ( &*%Fv    c              /     K   t         j                  j                  dd      dk(  } | |g|i |D ]"  } ||||      }t        |t              s| $  ||g|i |D ]"  } ||||      }t        |t              s| $ y wN*PYTORCH_TEST_CHECK_VALIDATE_SPARSE_SAMPLES01check_validate)osenvirongetr    r	   )sample_inputsmaybe_failing_sample_inputsvalidate_sample_inputr   r   r   r)   r   s           r   _sample_inputs_sparser0   5   s      	

CSISP   9$9&9 &w~Vfk*L .gGGG &w~Vfk*Ls   AB	0B	B	c              /      K   t         j                  j                  dd      dk(  } | |g|i |D ]"  } ||||      }t        |t              s| $ y wr$   )r*   r+   r,   r    r   )r.   r/   r   r   r   r)   r   s          r   _error_inputs_sparser2   L   sb      	

CSISP  .gGGG &w~Vfj)Ls   AAAc                       fd}|S )zDecorator to _maybe_failing_sample_inputs_... generator functions
    that clones and sets requires_grad argument to tensors in sample
    input arguments. This is needed when the generated samples share
    tensor instances.
    c              ;      K   fd}r& | |||fi |D ]  }|j                  |        y  | |||fi |E d {    y 7 w)Nc                     t        | t        j                        r.| j                  s"r | j	                         s| j                         s| S | j                         j                         j                        S N)	r    torchTensorrequires_gradis_floating_point
is_complexdetachclonerequires_grad_)xr9   s    r   apply_requires_gradzM_apply_requires_grad_to_samples.<locals>.wrapper.<locals>.apply_requires_grada   sT    q%,,/??$++-88:##%44]CCr"   )	transform)	r   devicedtyper9   layoutr   r@   r   r-   s	      `    r   wrapperz0_apply_requires_grad_to_samples.<locals>.wrapper`   sv     	D  -v!AG! B #,,-@AAB
 %vAG  s   >A
AA
 )r-   rE   s   ` r   _apply_requires_grad_to_samplesrG   Y   s    * Nr"   c           	   +   N  K   t        |      j                  dd      d   j                  dd      d   }t        | d|z         }|syt	        | |||fi |D ]  }	|	j
                  j                  dk(  r|t        j                  t        j                  t        j                  t        j                  hv rQ|	j
                  j                  dk  rx|	j
                  j                  dkD  r|	j
                  dk(  j                         r|t        j                  t        j                  hv r|d	}t        |	j
                  j                         j                  ||
      j!                  |      |	j"                  |	j$                         |t        j&                  u r|j(                  s|j*                  r|	j
                  j                         j                  |      }
t        j,                  |
j/                         j1                  dd      |
j3                         j1                  d      |
j4                  |
j6                  |
j8                        }
|
j;                         rJ t        |
j!                  |      |	j"                  |	j$                         |	j
                  j                  dkD  sXt        |	j
                  j                         j                  |||	j
                  j                  dz
        j!                  |      |	j"                  |	j$                          yw)z9Sample inputs for reduction operations on sparse tensors..   _coor   	supports_N   rJ   rJ   rD   	blocksizer   r   rD   rC   rB   rD   rQ   	dense_dim)strsplitrsplitgetattrr   r   ndimr7   
sparse_csr
sparse_csc
sparse_bsr
sparse_bscanyr	   r<   	to_sparser>   r   r   
sparse_coor:   r;   sparse_coo_tensorindicesrepeatvaluesshaperC   rB   is_coalesced)r   rB   rC   r9   rD   rQ   r   layout_nameop_supports_layoutr   inps              r   sample_inputs_sparse_reductionrl   x   s     f+##C+B/66vqA!DK +*CD/17 @ ""a'	
 
 !!&&* !!&&*0B0Ba0G/L/L/N
 e&&(8(899i>OI%%'Yf	Y:^M*""&&
 	
 U%%%5+B+BeFVFV$$++-77v7FC))$$Q*

##A&		iizzC '')))""=1!&&#**  ""Q&""))+!'*00559  
  .!&&#**
 
m@s   J)L%-A8L%c                    t               }| j                  dk(  rt        |      }| j                  dv r|j                  j	                  d|      }|d|hvrL|j
                  dkD  r=|j                  t        j                  u r!|dk(  j                         rt        |d      }nq|j                  j	                  d	      st        |t        t        fd
      }n=||u rt        |t        d      }n&|j                  j
                  dkD  rt        |d      }| j                  dv rR|j                  }|j                  j	                  d      }|M|j
                  dkD  r>|j                  t        j                  u r"|dk(  j                         rt        |d      }n|t        |t        d      }n|j                  |j                  j                  u r2|j
                  dkD  r#| j                  dk(  rt        |t        d      }nr|j                  j	                  d	      st        |t        t        fd
      }n=|j                  j
                  dkD  r|j                  j	                  d      dvr|j
                  dkD  rx|j                  t        j                  ur\|j                  j	                  d      dk(  rt        |d      }n| j                  dk(  rt        |d      }nt        |t        d      }n|j                  j
                  dkD  rt        |d      }nY|j                  |j                  u rA|j!                         |j!                         k7  r |j#                         dkD  rt        |d      }|rt%        | |       |S )Return the specified sample when it is valid and supported by the
    operation. Otherwise, return the sample as ErrorInput instance.

    When check_validate is True, the result is validated against
    calling the op on the sample.
    sum>   
masked.summaskNrN   r   z7Expect the same number of specified elements per batch.r   keepdimzKreduction operations on (CSR|CSC) tensors with keepdim=False is unsupported)r   r   z=masked (.*) expects explicit mask for sparse_csr tensor inputzFcrow_indices is supposed to be a vector, but got 3 dimensional tensor.>   masked.amaxmasked.aminmasked.meanmasked.prodrv   z]where[(][)] received an invalid combination of arguments - got [(]Tensor, Tensor, NoneType[)]dim>   r   rJ   )r   rK   zLtensor dimensionality must be sum of batch, base, and dense dimensionalitiesrw   z(input_dim == 2 INTERNAL ASSERT FAILED atzFSparse CSR tensors are 2D and only support reduction along dim 0 or 1.zBIndex tensor must have the same number of dimensions as src tensor)objectr   +_validate_sample_input_sparse_reduction_sumr   r,   r[   rD   r7   stridedr`   r   r   RuntimeError
ValueErrorr   	TypeError_nnzrV   r!   )r   r   r)   UNSPECIFIEDrq   t_inps         r   '_validate_sample_input_sparse_reductionr      s    (K||u<VD||~%}}  5{++		Au}},!  UF ""9-*L9iF
 [ %[F
 \\"dF
 ||SS}}  (		Au}},!  UF \%[F KK6<<...		A-$<	F ""9-*L9iF LL!""5)7		A5==0}}  '72# n .# J
 $- h
 \\"dF
 KK5<<'		uzz|+!A%`F
 (Mr"   c                 6   | j                   | j                  }}|j                  d      }|j                  d      }|j                  }t	        |t
        t        t        f      r6|t        j                  t        j                  t        j                  t        j                  hv r|t        j                  t        j                  t        j                  hv rt        | d      S |t        j                  t        j                  hv r|st        | d      S |j                         dk7  rt        | d      S |t        j                  k(  rT|j                  t        j                   k(  rt        | d      S |j                  t        j"                  k(  rt        | d	      S | S )
Nrx   rs   z`Currently the only compressed sparse format supported for sum.dim_IntList is CSR, but got layoutrr   zEreduction operations on CSR tensors with keepdim=False is unsupportedrN   zinput_dim == 2 INTERNAL ASSERTz._sparse_csr_sum_cpu not implemented for 'Bool'z6_sparse_csr_sum_cuda not implemented for 'ComplexHalf')r   r   r,   rD   r    intlisttupler7   r\   r]   r^   r_   r   rx   rC   bool	complex32)r   r)   r   t_kwargsrx   rs   rD   s          r   rz   rz   @  sd    llFMM8E
,,u
Cll9%G\\F#T5)*	
 
 %**E,<,<e>N>NOO!z  %**E,<,<==g!_  yy{a!!A  )));;%**,%%U  ;;%//1%T  Mr"   c              +     K   |t         j                  t         j                  fv rt        t        j                  ddgddgg|      j                  |      j                  |      t        dd      	       t        t        j                  ddggddggg|      j                  |d
      j                  |      t        d      	       t        t        j                  ddgddgg|      j                  |      j                  |      t        d      	       t        t        j                  ddgddgg|      j                  |      j                  |      t        dd      	       t        t        j                  ddggddggg|      j                  |d
      j                  |      t        d      	       t        t        j                  ddgddgg|      j                  |      j                  |      t        d      	       |t         j                  t         j                  fv rt        t        j                  ddgddgg|      j                  |d      j                  |      t        dd      	       t        t        j                  ddggddggg|      j                  |dd      j                  |      t        d      	       t        t        j                  ddgddgg|      j                  |d      j                  |      t        d      	       t        t        j                  ddgddgg|      j                  |d      j                  |      t        dd      	       t        t        j                  ddggddggg|      j                  |dd      j                  |      t        d      	       t        t        j                  ddgddgg|      j                  |d      j                  |      t        d      	       yyw)IGenerator of samples that are known to fail or that were failing in past.r   rJ   rN      rC   rS   T)rx   rs   r   )rD   rV   )rx   )r   )rN   rN   rP   rO   rD   rV   rQ   rU   N)
r7   r\   r]   r	   tensorra   r>   dictr^   r_   r   rB   rC   r9   rD   r   s         r   1_maybe_failing_sample_inputs_sparse_reduction_sumr   q  s      
 LL1a&1a&)7YfY%^M*At,	
 	
 LLAq6(aVH-U;YfY2^M*A;	
 	
 LL1a&1a&)7YfY%^M*D>	
 	
 LL1a&1a&)7YfY%^M*D$/	
 	
 LLAq6(aVH-U;YfY2^M*D>	
 	
 LL1a&1a&)7YfY%^M*A;	
 	
  
 LL1a&1a&)7YfY7^M*At,	
 	
 LLAq6(aVH-U;YfVYD^M*A;	
 	
 LL1a&1a&)7YfY7^M*D>	
 	
 LL1a&1a&)7YfY7^M*D$/	
 	
 LLAq6(aVH-U;Yf!YD^M*D>	
 	
 LL1a&1a&)7YfY7^M*A;	
 	
Ks   QQc           
   +   ^   K   t        t        t        t        | ||||fi |E d{    y7 w)z(Sample inputs for sum on sparse tensors.N)r0   rl   r   r   r   s         r   "sample_inputs_sparse_reduction_sumr     s<      %&9/
 
 
 
   #-+-c           	   +   x   K   t         j                  }d}t        t        t        | ||||fi |E d{    y7 w)z'Error inputs for sum on sparse tensors.FN)r7   float64r2   r   r   r   rB   rD   r   rC   r9   s         r   !error_inputs_sparse_reduction_sumr     sE     MMEM#9/	 	 	 	   0:8:c              +     K   fd}t        | f||dd|D ]  }|j                  |j                  d   }	}d}
|j                  dz
  }|t        j
                  t        j                  t        j                  t        j                  hv r|j                  dk  r|j                  dz
  }t        |
|dz         D ]  }|t        j                  t        j                  hv rddg}|j                         dkD  rQ|j                  |j                  |j                  dz
  |z
     |j                  |j                  dz
  |z
     f       ndg}|D ]  }t        |||	      } ||fi |} ||	fi |}t        ||g|j                  dd |j                  
       t        |t!        d||      g|j                  dd |j                  
          yw)zSample inputs for elementwise binary operations on sparse tensors.

    The samples include regular, zero-sized, batched, and hybrid
    sparse tensors as well as rhs scalars. All tensors are full tensors.
    c                 b     | j                         j                  di |j                        S )NrF   )r<   ra   r>   )r   r   r9   s     r   
_to_sparsezEsample_inputs_sparse_elementwise_binary_operation.<locals>._to_sparse  s*    (v}}((262AA-PPr"   T)rB   rC   r9   exclude_zeror   rJ   rN   rO   Nr   rR   rF   )rC   rB   r9   )r   r   r   r[   r7   r\   r]   r^   r_   rangenumelappendrg   r   r	   r   r   )r   rB   rC   r9   rD   r   r   r   lhsrhsmin_dense_dimmax_dense_dimrV   
blocksizesrQ   to_sparse_kwargs
lhs_sparse
rhs_sparses      `              r   1sample_inputs_sparse_elementwise_binary_operationr     s    Q <#  8  %%|'8'8';S1	
 
 xx!|HHqLM}ma.?@ "	I%**E,<,<==$X
99;?%%IIchhlY&>?IIchhlY&>? #V
' 	#'!Y)$  (@/?@
'@/?@
!$=|'8'8'<='..  "#eF- &**12.	 (..	 	"	-8s   GGc                    | j                   | j                  }}|j                         |j                         z
  |j	                         z
  }|j
                  }|j                  }|t        j                  u r$|dkD  r|d   j                  dkD  rt        | d      S |t        j                  u r|d   j                  dkD  rt        | d      S |t        j                  u r|d   j                  dkD  rt        | d      S |t        j                  u r|d   j                  dkD  rt        | d      S |t        j                  u rc|t        j                  u rQ|d   j                  dkD  r?|j                   r3|j#                         dkD  r |j                         dkD  rt        | d      S |t        j                  t        j                  hv rc|t        j                  u rQ|j%                         dkD  r>|d   j                  dkD  r,|j                   r |j#                         dkD  rt        | d      S |t        j                  u rZ|d   j
                  t        j&                  u r;d|d   j                  k  r)|d   j                  |j                  k  rt        | d	      S |t        j                  u rk|d   j
                  t        j&                  u rd|d   j                  k  s-|d   j
                  |u r)|j(                  |d   j(                  k7  rt        | d
      S |t        j                  u rc|j                         dkD  rP|j%                         dkD  r=|j                   r1|t        j*                  u r|d   j                  dkD  rt        | d      S | S )Nr   zrcoo_to_sparse_csr: conversion from Sparse to SparseCsr for input tensors with sparse_dim[(][)]!=2 is not supportedrr   z*Expected result Tensor to be of format CSRzbempty_sparse_compressed expected sparse compressed [(]non-block[)] tensor layout but got SparseBsrzbempty_sparse_compressed expected sparse compressed [(]non-block[)] tensor layout but got SparseBscz,"addcmul_cpu_out" not implemented for 'Bool'z+"mul_out_sparse" not implemented for 'Bool'z,sparse_mask_sparse_csr expects self to be 2Dzlexpects sparse inputs with equal dimensionality, number of sparse dimensions, and shape of sparse dimensionsz,"addcmul_cpu_out" not implemented for 'Half')r   r   rx   rV   
sparse_dimrD   rC   r7   r\   r[   r   r]   r^   r_   rb   r   is_cpur   r   r{   rg   float16)r   r   t_args	batch_dimrD   rC   s         r   4_validate_sample_input_elementwise_binary_sparse_mulr   8  s!    LL&++6E		eoo//%2B2B2DDI\\FKKE!!!i!mq	8JE
 	
 
5##	#q	(: L
 	
 
5##	#q	(:|
 	
 
5##	#q	(:|
 	

 	%"""UZZ1INNQLLKKMAOO! P
 	
 	5##U%5%566UZZJJL11INNQLLKKMA O
 	
 	%"""1I-q	1INNUZZ' N
 	
 
5##	#			U]]	*q6!9>>/A1I&5;;&)//+I2
 	
 	%"""OO!JJL1LLU]]"1INNQ P
 	
 Mr"   c              +   .  K   |t         j                  t         j                  hv rdnd}t        j                  ddgddgg||      j	                  |d|	      }t        j                  ddgddggdd
gddggg||      j	                  |d|	      }t        j                  dgdggdgdggg||      j	                  |d|	      }	|t         j
                  u rt        ||f       t        t        j                  |	      j                  |      t        j                  |	      j                  |      f       |t         j                  u rt        ||f       |t         j                  u r|j                  rt        ||f       |t         j                  u rt        ||f       |t         j                  u rt        ||f       |t         j                  u rt        ||f       |t         j                  u r|t         j                  u rt        ||f       |t         j                  u r|j                  rt        ||f       |t         j                  t         j                  hv r|j                  rt        |	|	f       yyyyw)r   rO   NrJ   rN   r      )rB   rC   r   r            )r   )r7   r^   r_   r   ra   r\   r	   
zeros_liker>   r   r   r   r]   rb   r   )
r   rB   rC   r9   rD   r   rQ   regularbatchhybrids
             r   :_maybe_failing_sample_inputs_sparse_elementwise_binary_mulr     s`     !U%5%5u7G7G$HHdIllQFQF+F%HRRi S G LLa&1a&	QFQF+,V5ivii@ 
 \\#sqcA3Z uivii@  !!!%uh// V$33MB""6*99-HJ
 	
 EOO#gWJ77EJJ7>>gWJ77!!!'
33!!!'
33!!!'
33!!!EOO#gWJ77EJJ7>>gWJ77UZZ//GNNfF955 5C/ "s   JJc                 V    | j                   dk(  rt        |      }|rt        | |       |S )Nmul)r   r   r!   r   r   r)   s      r   :_validate_sample_input_sparse_elementwise_binary_operationr     s,     ||uEfM(Mr"   c           
   +   ^   K   t        t        t        t        | ||||fi |E d{    y7 w)z2Sample inputs for mul operation on sparse tensors.N)r0   r   r   r   r   s         r   sample_inputs_sparse_mulr     s:     $9BB
 
 
 
r   c           	   +   x   K   t         j                  }d}t        t        t        | ||||fi |E d{    y7 w)z1Error inputs for mul operation on sparse tensors.FN)r7   r   r2   r   r   r   s         r   error_inputs_sparse_mulr     sE     MMEM#BB	 	 	 	r   c           
   +     K   ddl m}  |       j                  |||ddddd      D ]  }t        |di        t        |dt	        |||             |t
        j                  ur(t        |dt	        t
        j                  	             t
        j                  j                         r7|j                  j                  d
k(  rdnd
}t        |dt	        |             |t
        j                  u rt
        j                  }	ny|t
        j                  u rt
        j                  }	nV|t
        j                  u rt
        j                  }	n3|t
        j                  u rt
        j                  }	nt
        j                  }	t        |dt	        |	             |t
        j                   ust        |dt	        t
        j                                 y w)Nr   )TestCaseTF)rB   rC   enable_batchenable_hybridenable_zero_sizedenable_non_contiguous_indicesenable_non_contiguous_valuesrF   rR   )rB   rC   rD   r   cpucudarB   rS   )$torch.testing._internal.common_utilsr   generate_simple_inputsr	   r   r7   r   r   is_availablerB   r   r\   r]   r^   r_   r{   rb   )
r   rB   rC   r9   rD   r   r   r   other_deviceother_layouts
             r   _sample_inputs_sparse_like_fnsr     s     >*33&+%* 4 	 #U &r"55DeF$S
 	
 %f2d6OPP::""$%+]]%7%75%@6eLf2d,6OPPU%%% ++Lu''' ++Lu''' ++Lu''' ++L ==L&r$l2KLL)))f2d%BRBR6STTG#Us   F,G0,Gc                    |j                   j                  t        j                  t        j                  t        j
                  t        j                  hv rb| j                  dvrT|j                  j                  d|j                   j                        |j                   j                  k7  rt        |d      S |j                   j                  t        j                  u rt        |d      S |rt        | |       |S )N>   r   rD   zempty_like with different sparse layout is not supported \(self is Sparse(Csc|Csr|Bsc|Bsr) but you requested Sparse(Csr|Csc|Bsr|Bsc)\)rr   zQCould not run 'aten::normal_' with arguments from the 'Sparse(CPU|CUDA)' backend.)r   rD   r7   r\   r]   r^   r_   r   r   r,   r   rb   r!   r   s      r   &_validate_sample_input_sparse_like_fnsr     s    ||	 
 ,,n
,==Xv||':':;v||?R?RRg  ||e...c
 	
 (Mr"   c              +   \  K   t         j                  j                         r|t         j                  urut        j                  |      j
                  dk(  rdnd}|t         j                  u rt         j                  }ny|t         j                  u rt         j                  }nV|t         j                  u rt         j                  }n3|t         j                  u rt         j                  }nt         j                  }|t         j                  t         j                  hv rdnd }t        t        j                  ddgddgg||      j                  ||	      t        |
             t        t        j                  ddgddgg||      j                  ||	      t        |             y y y w)Nr   r   rO   r   rJ   rN   r   rT   rP   r   r   rS   )r7   r   r   rb   rB   r   r\   r]   r^   r_   r{   r	   r   ra   r   )	r   rB   rC   r9   rD   r   r   r   rQ   s	            r   ,_maybe_failing_sample_inputs_sparse_like_fnsr   5  sn     zz V53C3C%C!&f!5!:!:e!CvU%%% ++Lu''' ++Lu''' ++Lu''' ++L ==L$)9)95;K;K(LLFRV	LL1a&1a&)vFPP Q  |,	
 	
 LL1a&1a&)vFPP Q  |,	
 	
- &D s   F*F,c           
   +   ^   K   t        t        t        t        | ||||fi |E d{    y7 w)z3Sample inputs for like-functions on sparse tensors.N)r0   r   r   r   r   s         r   sample_inputs_sparse_like_fnsr   V  s<      %&4.
 
 
 
r   c           	   +   x   K   t         j                  }d}t        t        t        | ||||fi |E d{    y7 w)z2Error inputs for like-functions on sparse tensors.FN)r7   r   r2   r   r   r   s         r   error_inputs_sparse_like_fnsr   g  sE     MMEM#4.	 	 	 	r   c                    | j                   dk(  r|j                  j                  t        j                  t        j
                  t        j                  t        j                  hv rTt        |j                        dk(  r<t        |j                  d   t              r|j                  d   dk7  rt        |d      }|rt        | |       |S )Nra   rJ   r   rN   z=sparse dim argument must be 2 for sparse_compressed_to_sparserr   )r   r   rD   r7   r\   r]   r^   r_   lenr   r    r   r   r!   r   s      r   %_validate_sample_input_sparse_defaultr   w  s    ||{"LL  %"2"2E4D4DeFVFVWXFKK A%6;;q>3/A!#[F
 (Mr"   c                     t        | t              rt        | ||      S t        | t              rt	        | ||      S t        | ||      S )rn   r(   )r    r   r   r   r   r   r   s      r   validate_sample_input_sparser     sX     '?+6VN
 	
 
G_	-IVN
 	
 5VN
 	
r"   r6   )F)"r*   r7   torch.testingr   #torch.testing._internal.opinfo.corer   r   r   r   r   r	   r!   r0   r2   rG   rl   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   r"   r   <module>r      s    
  % !H.	@ >BIXyx.b\
~" DNRj !46 !46p %* (UV6
B" &
r"   