
    &Vji                        d dl Z d dlmZmZmZ d dlZd dlmZmZ e	edf         Z
eeeef         ge	edf         f         Zg dZdedefdZd	ej        j        deeef         fd
Zdee         deeef         fdZdedefdZdS )    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                 .    t          j        dd|           S )Nz\.([a-zA-Z_][a-zA-Z0-9_]*)z['\1'])resub)r   s    Y/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/fx/experimental/_dynamism.pyr   r      s    6/DAAA    modulec                    i }i |d<   i |d<   t          |           D ]}	 |                    d          st          t          | |                    sqt          | |          }t	          |t
          j        j                  sBt	          |t          t          t
          j
        f          rt          |          t          ur|||<   # t          $ r Y w xY w|                     d          D ]\  }}||d         |<   |                     d          D ]\  }}||d         |<   |                                 D ]\  }}t#          |          |d         |<   |S )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NotImplementedErrornamed_parametersnamed_buffersnamed_childrenr	   )r   	self_dict	attr_name
attr_valuer   parambuffer	submodules           r   r	   r	      s    "I!ImIj[[  		'',, 	6X	**6 6 	6 %VY77
":ux??6":UEL/IJJ6 Z((44+5Ii(" 	 	 	 H	
 ..u.== / /e).	- &&,,U,;; 0 0f)/	- &&!0022 G Gi&;I&F&F	*d##s   B#C
CCexample_inputsc                 B   i }| D ]}d|v r=t          |d         t          j        j                  rt	          |d                   |d<   t          |          \  }}|D ]C\  }}t          |t          t          t          j        f          s.t          |t          j                  rt          |j
                  }d}n|f}d}||vr,d t          t          |                    D             |f||<   ns||         \  }	}
|
|k    r	 t          |	          t          |          k     rA|	                    t                                 t          |	          t          |          k     At          |          D ],\  }}||         d         |                             |           -Ei }|                                D ]d\  }\  }	}t          d |	D                       }dd                    d	 |D                       z   }|d         j        }||vri ||<   |||         |<   e|S )
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.
    selfTFc                 *    g | ]}t                      S  )set).0r   s     r   
<listcomp>z2track_dynamism_across_examples.<locals>.<listcomp>V   s    &H&H&Hsuu&H&H&Hr   r   c              3   <   K   | ]}t          |          d k    V  dS )   N)len)r6   ss     r   	<genexpr>z1track_dynamism_across_examples.<locals>.<genexpr>b   s,      77#a&&1*777777r   L c              3   6   K   | ]}t          |           V  d S )N)str)r6   ks     r   r<   z1track_dynamism_across_examples.<locals>.<genexpr>c   s*      >>3q66>>>>>>r   )r   r   r   r    r	   r   r!   r"   r#   tupleshaperanger:   appendr5   	enumerateadditemsjoinkey)r0   trackingexleaves_with_pathsr   key_pathvaluerC   	is_tensordim_setsflagidimoutput
_is_tensor	final_dynkey_strrJ   s                     r   r
   r
   ;   sL    <>H 2 2R<<Jr&z58?CC<.r&z::BvJ5b9910 	2 	2OHeec5%,%?@@ %.. "16u{1C1C 		!	x''&H&HeCJJ6G6G&H&H&H)%T""!)(!3$9$$(mmc%jj00OOCEE*** (mmc%jj00#E** 2 23"1%a(,,S11112#	2(  F,4NN,<,< ) )((8Z77h77777	>>X>>>>>>qkofF3K(sGMr   example_inputc                 F    dt           dt           fd}t          ||           S )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.
    rO   r   c                     t          | t          j                  r(|                                                     d          S | S )Nmeta)device)r   r   r#   cloneto)rO   s    r   transform_fnz/clone_and_convert_to_meta.<locals>.transform_fnq   s:    eU\** 	3;;==##6#222r   )r   r   )rY   r`   s     r   r   r   k   s6    C C    
 L-000r   )r   typingr   r   r   r   torch.utils._pytreer   r   rB   KeyPathr!   r"   NonTensorShapeFn__all__r@   r   r   r    dictr	   listr
   r   r4   r   r   <module>rh      sE   				 ' ' ' ' ' ' ' ' ' '  @ @ @ @ @ @ @ @ S/U3:./sCx@A   B B B B B B
 %(/  d38n        F-I-	#s(^- - - -`1S 1S 1 1 1 1 1 1r   