
    [Th                         S SK r S SKJrJrJr  S SKrS SKJrJr  \	\S4   r
\\\\4   /\	\S4   4   r/ SQrS\S\4S jrS	\R"                  R$                  S\\\4   4S
 jrS\\   S\\\4   4S jrS\S\4S jrg)    N)AnyCallableUnion)tree_flatten_with_pathtree_map.)normalize_source_namemodule_to_nested_dicttrack_dynamism_across_examplesclone_and_convert_to_metanamereturnc                 2    [         R                  " SSU 5      $ )Nz\.([a-zA-Z_][a-zA-Z0-9_]*)z['\1'])resub)r   s    W/var/www/auris/envauris/lib/python3.13/site-packages/torch/fx/experimental/_dynamism.pyr   r      s    66/DAA    modulec                 l   0 n0 US'   0 US'   [        U 5       H  nUR                  S5      (       a  M  [        [        X5      5      (       a  M6  [        X5      n[	        U[
        R                  R                  5      (       a  Ml  [	        U[        [        [
        R                  45      (       d  M  [        U5      [        Ld  M  X1U'   M     U R                  SS9 H  u  pEXQS   U'   M     U R                  SS9 H  u  pFXaS   U'   M     U R                  5        H  u  pG[!        U5      US   U'   M     U$ )ziRecursively converts an nn.Module into a nested dictionary with explicit 'parameters' and 'modules' keys._parameters_modules_F)recurse)dir
startswithcallablegetattr
isinstancetorchnnModuleintfloatTensortypeboolnamed_parametersnamed_buffersnamed_childrenr	   )r   	self_dict	attr_name
attr_valuer   parambuffer	submodules           r   r	   r	      s    "I!ImIj[	##C(('&:T1U1U 3Jz588??;;zC+EFF$D0'1)$ ! ..u.=).- & >,,U,;)/- & < "002&;I&F	*d# 3 r   example_inputsc                    0 nU  GH  nSU;   a=  [        US   [        R                  R                  5      (       a  [	        US   5      US'   [        U5      u  p4U GH)  u  pV[        U[        [        [        R                  45      (       d  M2  [        U[        R                  5      (       a  [        UR                  5      nSnOU4nSnXQ;  a2  [        [        U5      5       Vs/ s H  n[        5       PM     snU4X'   OWX   u  pX:w  a   [        U	5      [        U5      :  a3  U	R                  [        5       5        [        U	5      [        U5      :  a  M3  [        U5       H  u  pX   S   U   R!                  U5        M      GM,     GM     0 nUR#                  5        HV  u  nu  p[        S U	 5       5      nSSR%                  S U 5       5      -   nUS   R&                  nUU;  a  0 UU'   XU   U'   MX     U$ s  snf )	a  
This function analyzes a list of example inputs to determine the dynamism of their shapes.
It tracks whether the dimensions of tensors or non-tensor values change across
different examples. The function returns a dictionary where each key represents
a path to a value in the input examples, and the corresponding value is a tuple
indicating which dimensions are dynamic (i.e., change across examples). This
helps in understanding how the structure of data varies across different instances.
selfTFr   c              3   >   #    U  H  n[        U5      S :  v   M     g7f)   N)len).0ss     r   	<genexpr>1track_dynamism_across_examples.<locals>.<genexpr>[   s     7h#a&1*hs   L c              3   :   #    U  H  n[        U5       v   M     g 7f)N)str)r5   ks     r   r7   r8   \   s     >X3q6(Xs   )r   r   r   r    r	   r   r!   r"   r#   tupleshaperanger4   setappend	enumerateadditemsjoinkey)r/   trackingexleaves_with_pathsr   key_pathvaluer?   	is_tensordim_setsflagidimoutput
_is_tensor	final_dynkey_strrG   s                     r   r
   r
   4   s    <>HR<Jr&z588??CC.r&z:BvJ5b90OHec5%,,%?@@%..16u{{1C 	!	'6;CJ6G&H6Gsu6G&H)%T"!)!3$(mc%j0OOCE* (mc%j0#E*"1%a(,,S1 +#  1	 0  F,4NN,<((87h77	>X>>>qkoofF3K(sG -= M% 'Is   $G<example_inputc                 :    S[         S[         4S jn[        X5      $ )z
This function takes a list of example inputs and for each tensor, clones it and converts it to device=meta.
For non-tensor values, it keeps the reference. It uses pytree to handle nested structures recursively.
rL   r   c                 ~    [        U [        R                  5      (       a  U R                  5       R	                  SS9$ U $ )Nmeta)device)r   r   r#   cloneto)rL   s    r   transform_fn/clone_and_convert_to_meta.<locals>.transform_fnj   s2    eU\\**;;=##6#22r   )r   r   )rV   r]   s     r   r   r   d   s"    C C 
 L00r   )r   typingr   r   r   r   torch.utils._pytreer   r   r>   KeyPathr!   r"   NonTensorShapeFn__all__r<   r   r   r    dictr	   listr
   r    r   r   <module>rg      s    	 ' '  @ S/U3:./sCx@A B B B
%((// d38n 8-I-	#s(^-`1S 1S 1r   