
    sg=                         d dl Z 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
mZmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d dlZd Z ed	 d
D              Z	 	 ddZ G d de	      Zy)    N)floating_and_complex_types_and)TestCasefreeze_rng_stateTemporaryFileName)enable_profiling_mode_for_profiling_testsis_iterable_of_tensors)enable_profiling_mode)chain)ListUnion)
TensorTypec                    t        |dd       }|j                         D cg c]  }|j                          }}| j                  t	        |      dk(         |d   }t
        j                  j                  ||       y c c}w )N
last_graph   r   )getattroutputstype
assertTruelentorch_C_jit_assert_is_instance)	selffuncref_outputsargskwargsgraphotypests	            U/var/www/html/venv/lib/python3.12/site-packages/torch/testing/_internal/common_jit.pycheck_output_typesr#      se    D,-E$}}/!QVVX/E/OOCJ!O$aA	HH$$[!4 0s   A<c              #   &   K   | ]	  }d |z     yw)test_nn_N ).0names     r"   	<genexpr>r)       s      	&Dj4&7 	&s   )pdistmultilabel_margin_lossmax_unpool3dmulti_margin_lossbinary_cross_entropy!binary_cross_entropy_size_averagectc_lossgrid_samplec
                    |r|ni }d }
d dt         ffd}d } |d      }| j                  |||      }t               5  | j                  |||      }ddd       | j                  |       |rt	        | ||||       |ryt               5   |d	      } ||      } || j                  |||            }t
        j                  j                   |
|      ||
      } || j                  |||            }t
        j                  j                   |
|      ||
      }| j                  ||       | j                  ||       | j                  t        v s|	r
	 ddd       y || j                  |||            } |
|      }t
        j                  j                  ||d	|      } |
|      |z  }t
        j                  j                  |||
      } |d	      } ||      } || j                  |||            } |
|      }t
        j                  j                  ||d	|      } |
|      |z  }t
        j                  j                  |||
      }| j                  ||       | j                  ||       t        ||      D ]  \  }}||| j                  ||dd       ! 	 ddd       y# 1 sw Y   _xY w# 1 sw Y   yxY w)zVerifies a function performs identically to some reference implementation.

    Commonly, this is used to verify that a JIT implementation
    (output_func) matches the behavior of the eager implementation
    (reference_func).
    c                 r    t        | t        j                        r| f} t        d t	        |       D              S )Nc              3   .  K   | ]  \  }}||j                   t        t        j                  t        j                        v rR|j                   j
                  r$|dz   |j                         j                         z  n|dz   |j                         z    y w)Nr   )dtyper   r   halfbfloat16
is_complexsumabs)r'   ivs      r"   r)   z:check_against_reference.<locals>.allSum.<locals>.<genexpr>8   s}      oq!m3QRWR\R\^c^l^l3m(m /0gg.@.@AEQUUW[[]*q1uPQPUPUPWFWW os   BB)
isinstancer   Tensorr9   	enumerate)vss    r"   allSumz'check_against_reference.<locals>.allSum5   s9    b%,,'B o(}o o 	o    c                 |    |xr | j                   }| j                         j                         j                  |      S N)requires_graddetachclonerequires_grad_)r!   preserve_requires_gradrequire_grads      r"   clone_tensorz-check_against_reference.<locals>.clone_tensor<   s0    -A!//xxz!00>>rB   rI   c           
         g }D ]x  }t        |t        j                        r|j                   ||              6t	        |      r'|j                  |D cg c]  } ||        c}       h|j                  |       z |S c c}w rD   )r=   r   r>   appendr   )rI   inputsargr!   r   rK   s       r"   clone_inputsz-check_against_reference.<locals>.clone_inputs@   sz    @B 	#C#u||,l30FGH',PST1|A/EFTUc"	# 	 Us   B
c                     g }| D ]b  }t        |t        j                        r|j                  r|j	                  |       ;t        |      sG|j                  t        d |             d |S )Nc                     | j                   S rD   )rE   )r!   s    r"   <lambda>zHcheck_against_reference.<locals>.get_recording_tensors.<locals>.<lambda>U   s
    !// rB   )r=   r   r>   rE   rM   r   extendfilter)r   recording_tensorsrO   s      r"   get_recording_tensorsz6check_against_reference.<locals>.get_recording_tensorsN   sc    02 	QC#u||,1B1B!((-',!((0I3)OP		Q ! rB   F)rI   NT)allow_unused)create_graphrX   gMb@?g-C6?)atolrtol)boolrunAndSaveRNGr   assertEqualr#   r   autogradgrad_testMethodNamenn_functional_single_gradzip)r   r   reference_funcoutput_funcr   r   rX   check_typesno_gradno_gradgradrA   rP   rW   nograd_inputsr   outputs_testrecording_inputsrV   grads
grads_testl1l2grads2l1_testl2_testgrads2_testg2g2_testrK   s       `                       @r"   check_against_referencerv   +   s    V2Fo?T 	! !>M  GG	2	4 G))$vFGWl+4|]FK	2	4 &@'tD12BCd00AQSYZ[##F7O5F1= $ ?"4#5#5d<Lf#UV^^(()=?P6B ) D
,/
+#<<&@ &@  d00AQSYZ[G_##B(91= $ ? Umb $$R):$V'tD12BC"4#5#5d<Lf#UV&^^((&T ) V
 *%/nn))'3DS_)`,/
+v{3 	@KBzgoRt$?	@G&@ &@G G&@ &@s    J6CK'EK6K Kc                   f    e Zd Zd Zd Zd ZddZddZd Zd Z		 dd	e
ee   eee      f   fd
Zy)JitCommonTestCasec                     t        |t        j                  j                        r|n|j	                         }t        j                  j                  d|      S )Nforward)r=   r   r   Graphr   _create_function_from_graph)r   tracer   s      r"   createFunctionFromGraphz)JitCommonTestCase.createFunctionFromGraph   s7    #E588>>:xx33IuEErB   c                 J    | j                  |      }| j                  ||       y rD   )r~   assertExportImportModule)r   r}   rN   ms       r"   assertExportImportz$JitCommonTestCase.assertExportImport   s"    ((/%%a0rB   c                     | j                  |      }| j                  ||      }| j                  ||      }| j                  ||d       y )NzIResults of original model and exported/imported version of model differed)getExportImportCopyr]   r^   )r   r   rN   m_importabs         r"   r   z*JitCommonTestCase.assertExportImportModule   sO    ++A.q&)x0A  M 	NrB   Nc                 b    |r|ni }t               5   ||i |}d d d        |S # 1 sw Y   S xY wrD   )r   )r   r   rN   r   resultss        r"   r]   zJitCommonTestCase.runAndSaveRNG   s:    !r 	.F-f-G	.	.s   	$.c                    t        j                         }t        j                  j	                  ||       |j                  d       t        j                  j                  ||      }|s|S t               5 }t        j                  j	                  ||       t        j                  j                  ||      cd d d        S # 1 sw Y   y xY w)Nr   )map_location)ioBytesIOr   jitsaveseekloadr   )r   r   also_test_filer   bufferimportedfnames          r"   r   z%JitCommonTestCase.getExportImportCopy   s    		q&!A99>>&|>DO  	DEIINN8U+99>>%l>C	D 	D 	Ds   5AC  C	c                    d}|r|dz  }g }g }	g }
g }|D ])  }||v r|	j                  |       |j                  |       + |D ])  }||v r|j                  |       |
j                  |       + t        |      dkD  r	|d| dz  }t        |	      dkD  r	|d|	 dz  }t        |
      dkD  r	|d|
 dz  }t        |      dkD  r	|d| dz  }|S |d	z  }t        |      dkD  r	|d| d
z  }t        |      dkD  r	|d| dz  }|S )Nz0
Failure in testing nodes' autodifferentiation. zOne or more nodes were expected to be autodiffed, but were not found in specified fusible/nonfusible DifferentiableGraph groups. 
Specifically:r   z
  z were not in one of the DifferentiableGraphs when they were expected to be. Did you intend for these nodes to be autodiffed? If not, remove them from the list of nonfusible nodes.a#   were found in one of the FusionGroups when they were expected to be just in a DifferentiableGraph. If it was intended for these nodes to be in FusionGroups, reclassify these nodes as fusible nodes. If these nodes were not intended to be fused, your autodifferentiation logic might be wrong.aP   were not in one of the FusionGroups of the DifferentiableGraphs when they were expected to be. They were also not found in an outer DifferentiableGraph. Did you intend for these nodes to be autodifferentiated? If not, you should remove these nodes from the test's fusible nodes. Otherwise your autodifferentiation logic might be wrong.aH   were not in one of the FusionGroups of the DifferentiableGraphs when they were expected to be, instead they were found just in an outer DifferentiableGraph. Did you intend for these nodes to be fused? If not, you should move these nodes into the test's nonfusible nodes. Otherwise your autodifferentiation logic might be wrong.a  One or more nodes were not expected to be autodiffed but were found in a DifferentiableGraph or in a FusionGroup of a DifferentiableGraph. Did you intend for these nodes to be autodiffed? If so, change this test to expect autodifferentiation. 
Specifically:zu were not expected to be in one of the DifferentiableGraphs, but appeared in a FusionGroup of a DifferentiableGraph. zE were not expected to be in one of the DifferentiableGraphs but were.)rM   r   )r   should_autodiff_nodenodes_not_in_diff_graphfusion_nodes_not_foundnon_fusible_nodes_being_fusedfusion_nodes_foundnodes_in_diff_grapherr_msgdiff_nodes_missingdiff_nodes_in_fusionfusion_nodes_missingfusion_nodes_in_diffnodes                r"   autoDiffErrorMessagez&JitCommonTestCase.autoDiffErrorMessage   s    F > >G "$ $& #%  $& / 488(//5&--d3	4
 / 6..(//5(//5	6
 %&*T"4!5 6M M M '(1,T"6!7 8@ @ @
 '(1,T"6!7 8@ @ @ '(1,T"6!7 8@ @ @&   " "G
 %&*T"4!5 61 1 1 &'!+T"5!6 7F F FrB   c           
      H   |j                  d      }|D cg c]  }|j                  d       }}t        t        j                  |D cg c]  }|j                  d       c}            }	|	D cg c]  }|j                  d       }
}g }g }g }|D ]_  t        fd|D              r|j                         n|j                         t        fd|
D              sO|j                         a t        |      t        |      k(  }g }g }|D ]9  t        fd|
D              r|j                         )|j                         ; t        |      t        |      k(  }|.| j                  ||||||      }| j                  ||xr ||       y y c c}w c c}w c c}w )Nzprim::DifferentiableGraphSubgraphzprim::FusionGroupc              3   D   K   | ]  }|j                        d u  y wrD   findNoder'   gr   s     r"   r)   z7JitCommonTestCase.assertAutodiffNode.<locals>.<genexpr>  s     HA1::d#4/H    c              3   D   K   | ]  }|j                        d u  y wrD   r   r   s     r"   r)   z7JitCommonTestCase.assertAutodiffNode.<locals>.<genexpr>       JA1::d#4/Jr   c              3   D   K   | ]  }|j                        d u  y wrD   r   r   s     r"   r)   z7JitCommonTestCase.assertAutodiffNode.<locals>.<genexpr>  r   r   )
findAllNodesr   listr
   from_iterableanyrM   r   r   r^   )r   r   r   nonfusible_nodesfusible_nodes
diff_nodesr   diff_subgraphsr   fusion_nodesfusion_subgraphsr   r   r   found_all_nonfusible_nodesr   r   found_all_fusible_nodesr   s         `            r"   assertAutodiffNodez$JitCommonTestCase.assertAutodiffNode   s   ''(CD
9CD$&&,DD E//^l0mYZ@S1T0mno;GH4DFF:.HH !"$(*%$ 	;DHHH#**40'..t4J9IJJ-44T:	; &))<%=EUAV%V"  !#! 	4DJ9IJJ"))$/&--d3		4
 #&&8"9S=O"O+//0D0G0F0M0B0CEG 17S<SU\^ ,; E 1nHs   F	F0F	out_sizesc                     t         j                  j                         }dD ]'  }t         j                  j                  |       t         j                  j	                  |       |rt         j                  j                  |       t         j                  j                  |       t        |j                               j                         } fd}|j                  t         j                  j                  j                               r
 |||       |j                         }	t        t        |	            D ]  }
 ||	|
   ||
           * t         j                  j                  |       y )N)TFc                 8   | j                         }t        j                         j                  |      }t        j                         j                  |      }j	                  |j                  |             r!j                  |j                         |       y y rD   )symbolic_sizesr   get
with_sizesr   isSubtypeOfr^   sizes)r   actual_sizer   out_typeactual_typeassert_propagationr   s        r"   	test_typez7JitCommonTestCase.checkShapeAnalysis.<locals>.test_type/  sy    ++-%>>+66u=(nn.99+F  7 7 AB &$$X^^%5{C &rB   )r   r   &_jit_symbolic_shapes_test_mode_enabled"_jit_set_symbolic_shapes_test_mode&_jit_erase_non_input_shape_information_jit_pass_constant_propagation#_jit_pass_propagate_shapes_on_graphnextr   r   r   r   r   elementsranger   )r   r   traced_graphr   constant_prop!prev_symbolic_shapes_test_enabledenable_test_modeoutputr   tuple_elementsr;   s   `  `       r"   checkShapeAnalysisz$JitCommonTestCase.checkShapeAnalysis  s    -2HH,[,[,]) - 	? HH778HIHH;;LI77EHH88F ,..01668FD !!%(("5"5"9"9";<&),!'!2s>23 ?AnQ/1>?;	?@ 	334UVrB   rD   )TN)T)__name__
__module____qualname__r~   r   r   r]   r   r   r   r   r   intr   r&   rB   r"   rx   rx      sW    F1NDBH'^T LP$WE$s)T$s)_2L,M $WrB   rx   )NTTFF)r   
torch.cuda	torch.jittorch.jit._loggingtorch.jit.frontendtorch.jit.quantized$torch.testing._internal.common_dtyper   $torch.testing._internal.common_utilsr   r   r   r   r   r	   	itertoolsr
   typingr   r   torch._Cr   r   r#   	frozensetrb   rv   rx   r&   rB   r"   <module>r      s          Pk k F    	5 & 	& 	E 	& 	  SW\ab@HtW tWrB   