o
    #i                     @   s  d dl Z d dlZd dlZd dl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 d dlZd dlm  mZ d dlm  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# d d	l$m%Z% d d
lm&Z&m'Z' g dZ(G dd deZ)	dMdeej*ej+f dej,j-de.de)de/f
ddZ0G dd dej,j-Z1G dd de j2Z3G dd dej,j-Z4	dNdedee3 de4fdd Z5d!ejj6ddfd"d#Z7d$d% Z8d&e.d'e.de.fd(d)Z9d*ej,j-d+ej,j-fd,d-Z:d.ej,j-de.fd/d0Z;d.ej,j-dejj<fd1d2Z=d.ej,j-dejj<fd3d4Z>d5ej,j-de.fd6d7Z?d5ej,j-de.d8ej,j-fd9d:Z@G d;d< d<ZAd=ejj6d>e4fd?d@ZB	AdOdBej,j-dCee.eCf dDe.fdEdFZDdej,j-dGee.ee. f dHee. fdIdJZEdKdL ZFdS )P    N)defaultdict)deepcopy)Enum)AnycastDictListOptionalSetTupleUnion)FakeScriptObject)reorder_kwargs)ConstantArgumentExportedProgram	InputKindModuleCallSignatureSymIntArgumentTensorArgument)is_fx_tracing)
GetAttrKeySequenceKey)InterpreterModuleUnflattenedModule	unflattenFlatArgsAdapterc                   @   s   e Zd ZdZdZdZdS )	_AttrKind	parameterbufferconstantN)__name__
__module____qualname__	PARAMETERBUFFERCONSTANT r&   r&   T/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/export/unflatten.pyr      s    r   Tfrom_obj	to_moduletarget	attr_kind
persistentc           	      C   s   | d^ }}|D ]}t||d }|d u r!tj }t||| |}q
|tjkr:t| tjj	s2J |
||  d S |tjkrQt| tjsGJ |j|| |d d S |tjkrrt| tr_J dt| tjtjfsjJ t|||  d S d S )N.)r,   z2FakeScriptObject should only exist during tracing.)splitgetattrtorchnnModulesetattrr   r#   
isinstance	Parameterregister_parameterr$   Tensorregister_bufferr%   r   ScriptObject)	r(   r)   r*   r+   r,   prefixfielditemtr&   r&   r'   _assign_attr%   s6   



r>   c                       s:   e Zd ZdZdejjf fddZdd Zdd Z	  Z
S )	r   zA module that uses torch.fx.Interpreter to execute instead of the usual
    codegen that GraphModule uses. This provides better stack trace information
    and makes it easier to debug execution.
    graphc                    s   t    || _| | j_d S N)super__init__r?   owning_module)selfr?   	__class__r&   r'   rB   O   s   
zInterpreterModule.__init__c                 O   s   | j d us	J dtj r| j |i |S |rNt|}| jt|d  }|D ]}||v r4|||  q't|t|ks?J t|t| jksJJ t|}tj	j
| | jdj|ddiS )Nz&Didn't finalize this InterpreterModuler?   enable_io_processingF)graph_moduler0   compileris_dynamo_compilinglist	arg_nameslenappendtuplefxInterpreterr?   run)rD   argskwargsarg_listkwarg_names
kwarg_namer&   r&   r'   forwardW   s$   
zInterpreterModule.forwardc                 C   sP   t j| | j| jd< | j  g | _| jjD ]}|jdkr%| j	|j
 qd S )NrI   placeholder)r0   rQ   GraphModuler?   __dict__lintrM   nodesoprO   r*   rD   noder&   r&   r'   finalizet   s   

zInterpreterModule.finalize)r    r!   r"   __doc__r0   rQ   GraphrB   rY   rb   __classcell__r&   r&   rE   r'   r   I   s    r   c                
   @   s<   e Zd ZdZejdejdejdee	 dee	 fddZ
dS )	r   zN
    Adapts input arguments with ``input_spec`` to align ``target_spec``.
    target_spec
input_spec
input_argsreturnc                 C   s   dS )z=NOTE: This adapter may mutate given ``input_args_with_path``.Nr&   )rD   rf   rg   rh   r&   r&   r'   adapt   s   zFlatArgsAdapter.adaptN)r    r!   r"   rc   abcabstractmethodpytreeTreeSpecr   r   rj   r&   r&   r&   r'   r      s    r   c                       s>   e Zd Z	d
dedee f fddZdd Zdd	 Z  Z	S )r   Nexport_moduleflat_args_adapterc           !         s  t    |jjd urtddd |jD }|d dksJ t|j}t|j| _tj	
 | _t|j| _|| _d| _t|| j t||  |j| _g | _|j}t }i }| jjD ]*}|| }	t|	|vrrtj|	 |t|	< t|t|	 | |tjd || qZt| jj}
t }i }| jjD ]9}||
v rd}|j| }nd}|| }t||vr| |f|t|< t|t| d | |tj|d	 || q|  D ][\}}||v s||v rqd}t||v st!|tjjsd}|rt||vr|df|t|< t|t| d | |tjdd	 qt||vr ||t|< t|t| | |tjd qi }|j  D ],\}}t||vrQt!|tj"rK| }||t|< |t| }t|| |tj#d q5t$t%t }fd
d}t }| jj&D ]}|j't(jks|j't(jkr|j)rt*|j+dsJ t!|j,t-sJ |t|j|j, |j+j.|j, ||j, ||j, qv|j't(jkr|j)r|j't(j/ks|j't(j0krt*|j+dsJ t!|j,t-sJ |t|j|j, |j+j.|j, ||j, qv|j  D ]-\}}||vr,t|v sJ dt| d \}}|t||| ||j, q |j  D ]%\}}||vrWt|vrEq3t| d \}}|t||| q3i 1 D ]}dd |D }|D ]	\}}||< qjq_t2| g   fdd  | g  dd | jj3D | _4d| _5dd t6|D } | j7ddD ]\}}|| vrt8| | |< qt9| |  dd | j7ddD t%| : ksJ d S )Nz%Unflattening on JointExportModule NYIc                 S   s   g | ]}|j qS r&   )fqn.0entryr&   r&   r'   
<listcomp>       z.UnflattenedModule.__init__.<locals>.<listcomp>r    F)r+   T)r+   r,   c                    s    |  }| ||f d S r@   )rO   )obj_id	node_nametarget_name	name_list)
consts_mapr&   r'   add_to_consts_map  s   z5UnflattenedModule.__init__.<locals>.add_to_consts_mapnamez?Constants should be either aliased or appear in graph signaturec                 S      g | ]}|d  qS    r&   )rs   r=   r&   r&   r'   ru   P      c                    s   t | dr2| jjD ](}|jdkr1|jv r1t fdd|j D r1t|j d  d| j q	|  D ]\}} | |  q6d S )Nr?   rZ   c                 3   s*    | ]}| d dt   kV  qdS )r-   N)r.   rN   )rs   rq   scoper&   r'   	<genexpr>c  s
    
zJUnflattenedModule.__init__.<locals>.check_module_inputs.<locals>.<genexpr>z was not sunk into the module z which has the graph: )	hasattrr?   r^   r_   r~   anyAssertionErrornamed_childrenrO   )moduler   ra   r~   submod)check_module_inputsinputs_to_stater   r'   r   W  s    



z7UnflattenedModule.__init__.<locals>.check_module_inputsc                 S   s   g | ]	}|j d kr|qS rZ   r_   rs   ra   r&   r&   r'   ru   v  s    c                 S      i | ]\}}||qS r&   r&   )rs   irq   r&   r&   r'   
<dictcomp>{      z.UnflattenedModule.__init__.<locals>.<dictcomp>)remove_duplicatec                 S      g | ]\}}|qS r&   r&   )rs   rq   _r&   r&   r'   ru     r   );rA   rB   graph_signaturebackward_signature
ValueErrormodule_call_graphr   r?   r0   rQ   rd   rp   adapted_inplace_buffer_mutations_outline_submodulesrange_constraintsequality_constraints
state_dictset
parametersidr1   r5   cloner>   r   r#   addnon_persistent_buffersbuffers	constantsr$   itemsr4   r7   r%   r   rL   input_specskindr   r,   r   argr*   strr~   CONSTANT_TENSOR
CUSTOM_OBJvalues_sink_paramsr^   input_placeholderscheck_input_constraints	enumeratenamed_modulesrN   _reorder_submoduleskeys)!rD   ro   rp   fqn_listexport_graphr   assigned_paramsid_to_paramr~   paramr   assigned_buffersid_to_bufferr,   r   tensor	is_bufferid_to_constrq   r   	_constantconsts_targetsr}   added_params_bufferss
const_nameconstph_namer   node_targettargetsn	fqn_orderrE   )r   r|   r   r'   rB      s0  





	




zUnflattenedModule.__init__c                 C   sF   |   D ]\}}t|d  t|dr t|jtjjr t|j qd S )N:r?   )r   printr   r4   r?   r0   rQ   rd   )rD   rq   modr&   r&   r'   _print_graph  s   
zUnflattenedModule._print_graphc                 O   sz  | j d j}t||j}t||f\}}dd |D }t r?tjj	| | j
dj|ddi}t|tr=t|dkr=|d S |S ||jkr| jsTtd| d	d
|j  | jd u r]td| jsdtd | jj|j||d}d| _t||jjkrtdt| d|jj | jrddlm}	 | jdu rdd |D }
n|}
|	| j|
| j tjj	| | j
dj|ddi}t||jS )Nr   c                 S   r   r   r&   )rs   xr&   r&   r'   ru     r   z-UnflattenedModule.forward.<locals>.<listcomp>rG   rH   Fr   zGInput treespec does not match with exported module's: 
Input treespec: z. zExported module treespec: zeThere is no flat args adapter sepcified. Are you sure you are calling this with the right arguments? z5Adapting flat arg to match exported module's treespec)rf   rg   rh   Tz<Flat args adaption failed, number of args mismatch Adatped: z 
Exported module: )"_check_input_constraints_for_graphc                 S   s$   g | ]}t d dtddf|fqS )r   )idxz<unknown location>)r~   )r   r   rs   r   r&   r&   r'   ru     s    )r   	signaturer   in_specrm   tree_flatten_with_pathr   r0   rQ   rR   r?   rS   r4   rP   rN   r   r   rp   	TypeErrorrj   
num_leavesr   torch._export.utilsr   r   r   tree_unflattenout_spec)rD   rT   rU   r   reordered_kwargsflat_args_with_pathr   	flat_args
return_valr   new_flat_args_with_pathtree_outr&   r&   r'   rY     sv   




zUnflattenedModule.forwardr@   )
r    r!   r"   r   r	   r   rB   r   rY   re   r&   r&   rE   r'   r      s     or   r   rp   ri   c                 C   s
   t | |S )a  Unflatten an ExportedProgram, producing a module with the same module
    hierarchy as the original eager module. This can be useful if you are trying
    to use :mod:`torch.export` with another system that expects a module
    hierachy instead of the flat graph that :mod:`torch.export` usually produces.

    .. note:: The args/kwargs of unflattened modules will not necessarily match
        the eager module, so doing a module swap (e.g. :code:`self.submod =
        new_mod`) will not necessarily work. If you need to swap a module out, you
        need to set the :code:`preserve_module_call_signature` parameter of
        :func:`torch.export.export`.

    Args:
        module (ExportedProgram): The ExportedProgram to unflatten.
        flat_args_adapter (Optional[FlatArgsAdapter]): Adapt flat args if input TreeSpec does not match with exported module's.

    Returns:
        An instance of :class:`UnflattenedModule`, which has the same module
        hierarchy as the original eager module pre-export.
    )r   )r   rp   r&   r&   r'   r     s   
r   r?   c              	      s*  t tt| j}|jdkrt|jdksJ |jd }|j}|dt| }dd |j	 D }dd | jD }|D ]I}||j
 }	||	 }
||
 }| |# | dtjjj||f |j	 D ]	\}}| j|< qaW d   n1 suw   Y  |  fd	d
 q;t|t|d }|f|_dS )a  Transform buffer mutations from their functionalized form into a copy_
    node in the graph.

    Functionalization represents buffer mutation by passing the buffer as an input and output. So for example, the eager code:
        def forward(self, x):
            self.buffer += x
            return x * x

    Will become a graph that looks like:
        def forward(self, buffer, x):
            mutated_buffer = aten.add(buffer, x)
            mul = aten.mul(x, x)
            return (mutated_buffer, mul)

    We want to inplace this into something that looks like the original eager code:
        def forward(self, buffer, x):
            mutated_buffer = aten.add(buffer, x)
            buffer.copy_(mutated_buffer)
            mul = aten.mul(x, x)
            return (mul,)
    outputr   r   Nc                 S   r   r&   r&   )rs   kvr&   r&   r'   r     r   z-_inplace_buffer_mutations.<locals>.<dictcomp>c                 S   s   i | ]}|j d kr|j|qS r   )r_   r~   r   r&   r&   r'   r     s    call_functionc                    s   |  uS r@   r&   )r   new_noder&   r'   <lambda>  s    z+_inplace_buffer_mutations.<locals>.<lambda>)nextiterreversedr^   r_   rN   rT   buffers_to_mutateinputs_to_buffersr   r~   inserting_aftercreate_noder0   opsatencopy_metareplace_all_uses_withrP   )r?   r   output_nodereturn_argsmutation_node_to_buffer	mutationsbuffers_to_inputsinput_name_to_nodemutationbuffer_name
input_name
input_noder   r   user_outputsr&   r   r'   r     s4   

r   c                 C   s$   t | t |k o|dt |  | kS )z2Check whether `candidate` is a prefix of `target`.N)rN   )	candidater*   r&   r&   r'   
_is_prefix#  s   $r  
parent_fqn	child_fqnc                 C   s`   | dkr|S |  d}| d}|d t| |ks%J d| d|  dd|t|d  S )Nrw   r-   zChild module 'z(' is not a descendant of parent module '')r.   rN   join)r  r  parent_splitchild_splitr&   r&   r'   _compute_accessor(  s   

r
  r   yc                 C   s2   dt jjdtfdd}|| j||jksJ d S )Nr?   ri   c                    s   g }i  dt f fdd}t| jD ]E\}}dd t||jD }|dd t||j D 7 }|jdkr:|j	nd}|
| d	|j d
| dd| d | t|< qd|S )Nri   c                    s*   t | tjjrdt t|   S t| S )N%)r4   r0   rQ   Noder   r   )r   	nodes_idxr&   r'   arg_dump;  s   z?_verify_graph_equivalence.<locals>.graph_dump.<locals>.arg_dumpc                 S   s   g | ]}t |qS r&   )r   r   r&   r&   r'   ru   A  r   zA_verify_graph_equivalence.<locals>.graph_dump.<locals>.<listcomp>c                 S   s   g | ]\}}| d | qS )=r&   )rs   keyvaluer&   r&   r'   ru   B  s    r   rw   z: [z](z, )
)r   r   r^   rm   tree_maprT   rU   r   r_   r*   rO   r  r   )r?   retr  r   ra   	args_dumpr*   r&   r  r'   
graph_dump7  s   *
z-_verify_graph_equivalence.<locals>.graph_dump)r0   rQ   rd   r   r?   )r   r  r  r&   r&   r'   _verify_graph_equivalence6  s   r  gmc                 C   sF   d}t | d| r|d7 }t | d| s
d| }t| || |S )Nr   _spec_r   )r   r3   )r  specr   r~   r&   r&   r'   	_add_specN  s   
r  c                 C   *   t | |}| j|}| jtj||fS r@   )r  r?   get_attrr   	fx_pytreetree_flatten_spec)r  ra   r  r~   	spec_noder&   r&   r'   _generate_flattenW     
r%  c                 C   r   r@   )r  r?   r!  r   rm   r   )r  r^   r  r~   r$  r&   r&   r'   _generate_unflatten]  r&  r'  r   c                 C   sX   | d^ }}|D ]}t| |d }|d u r d S t|tjjs# d S |} q
t| |d S )Nr-   )r.   r/   r4   r0   r1   r2   )r   r*   r:   r;   r<   r   r&   r&   r'   _get_submodulec  s   r(  module_to_addc                 C   sl   | d^ }}|D ]#}t| |d }|d u r!tj }t| || t|tjjs+ dS |} q
| || d S )Nr-   F)r.   r/   r0   r1   r2   r3   r4   
add_module)r   r*   r)  r:   r;   r<   r   r&   r&   r'   _add_submodulet  s   
r+  c                   @   sj   e Zd Z	ddeeef deejj	 fddZ
dd Zdd	 Zd
d Zdd Zdd Zdd Zdd ZdS )_ModuleFrameNr   r   c
              	      s  |_ |_|_|_|_|_|_|_d_jd _	|	d ur)|	_
nttj _
jjv r?jj _n
d _j
jj< j
j_i _i _d _|d uryt|j	j	}
t|j
|
jd u rnj
nj |j|
_|j	}|d urɈjd ur|jjdksJ |jjd }|jjd }|jd u sJ |jd usJ jd w g }t|jD ]}|jd|  qi }|jD ]
}j|||< qt j
t!||f|j}t"|j#D ]7\}}jj$dt%j&||ft'|t(s|j)nd| d	}t'|t(rqt**j|j) j+|_+|jj|j) < qW d    n	1 s-w   Y  jj,jr g }|j#D ]-}t'|t(rU|j-d u rU|d  q@t'|t.t/fs_J |j0j|j)  q@t1jj
||j}jj2t%j&|df jj2t%j&|df fd
dt|jD }fdd|jD }W d    n	1 sw   Y  jd usJ t!|j_3|j_4d S d S d S )NF   r   r   _positional_arg_r   
_constant_)r_   r*   rT   r~   c                    s"   g | ]}j jtj |fqS r&   parentr?   r   operatorgetitem)rs   r   )	args_noderD   r&   r'   ru     s    z)_ModuleFrame.__init__.<locals>.<listcomp>c                    s$   i | ]}|j jtj |fqS r&   r1  )rs   r   )kwargs_noderD   r&   r'   r     s    

z)_ModuleFrame.__init__.<locals>.<dictcomp>)5
flat_graphr^   
seen_nodesseen_modulesr2  module_stack	module_idr   verboserq   r   r   r0   rQ   rd   cached_graph_moduler?   node_mapnode_to_placeholderparent_call_moduler
  r+  call_modulegetr   num_childrenchildren_specscontextr   rangerO   rZ   r%  rP   r   inputsr   r3  r4  r4   r   r~   copyr   inserting_beforer  r   r   remap_inputr'  r   rT   rU   )rD   r7  r^   r8  r9  r2  r:  r;  r   r   accessorr   	args_speckwargs_spec	arg_nodesr   kwarg_nodesr~   r   r   flat_arg_nodeinput_nodesinputinputs_noder&   )r5  r6  rD   r'   rB     s   

	





!z_ModuleFrame.__init__c                 C   s   | j dksJ d| d|j| ju sJ | jd  | jj|j|jd}W d    n1 s0w   Y  t|j|_|| j	|< d S )Nrw   zCannot add placeholder z to root module)	type_expr)
rq   r?   r7  rI  rZ   r~   typerH  r   r?  )rD   r   placeholder_noder&   r&   r'   add_placeholder  s   z_ModuleFrame.add_placeholderc                 C   sb   |j | ju sJ || jv r| j| S || jvr,| | | jd ur,| jd| j| | j| S )Nr   )	r?   r7  r>  r?  rW  r@  
insert_argr2  rJ  )rD   r   r&   r&   r'   rJ    s   





z_ModuleFrame.remap_inputc                    s  g } j  j}|d urM jd urM|jD ]}t|ttfr(| j	|j
  qtd| t jt fdd|D |j}t jj j|j}|}n2g } j D ]}|jD ]}|j
 j	vrp|| | j|   nqYqT j}t|dkr|d }t|ttjjfsJ  j| |d u rd S t|tjjr|jdndd |D |jd< t|dkr|d u r| jj|d < n!t|D ]\}	}
tj||	 j}|
jd|jd< | jj|
< qĈ j d urt! j  j d S d S )	Nz'Unsupported data type for output node: c                 3   s"    | ]} j  j|j  V  qd S r@   )r>  r8  r~   )rs   r   rD   r&   r'   r   ,  s
    
z0_ModuleFrame.finalize_outputs.<locals>.<genexpr>r   r   valc                 S   s   g | ]}|j d qS )rZ  )r   rB  )rs   or&   r&   r'   ru   P  s    z1_ModuleFrame.finalize_outputs.<locals>.<listcomp>)"r   rB  rq   r2  outputsr4   r   r   rO   r8  r~   RuntimeErrorr'  r   rP   r   r%  r@  r>  r   usersrN   rL   r0   rQ   r  r?   r   r   r   Proxyra   r=  r  )rD   orig_outputsr   r   tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outr&   rY  r'   finalize_outputs  sb   



z_ModuleFrame.finalize_outputsc                 C   s6   |  d|  | j|| j| j|< || j|j< d S )Ncopying)r   format_noder?   	node_copyrJ  r>  r8  r~   r`   r&   r&   r'   	copy_node_  s   z_ModuleFrame.copy_nodec                 C   s   d}| j jD ]}| ||jd|  |d7 }qd}| j| }|jdkr9| | |d7 }| j| }|jdks&| | | j jD ]}|jdkrN| | qBd S )Nr   nn_module_stackr   rZ   r   )	r7  r^   r   r   rB  rj  r_   rl  run_from)rD   r   ra   node_idxr&   r&   r'   	run_outerd  s"   








z_ModuleFrame.run_outerc                 O   s   | j rt|i | d S d S r@   )r<  r   )rD   rT   rU   r&   r&   r'   r   y  s   z_ModuleFrame.printc              
   C   s  d}|t | jk r| j| }|jdksJ |   | d||  | | j |jdkr<t | jdkr6|S |   |S t |jdi dkrNt	d| |jd }ddl
m} t |dkrg||v rg| j}nd	d
 |jd  D }|d t | j | jkr|   | d| j | | j |S |d usJ t| j|r|t | j }| d| t| j| j| j| j| | j|g t|jd  t | j | j|}|d7 }q|| jksJ | | |d7 }|t | jk s	d S d S )Nr   rZ   STEPr   r   rm  z(Unable to find nn_module_stack for node )_EMPTY_NN_MODULE_STACK_KEYc                 S   r   r&   r&   )rs   pathtyr&   r&   r'   ru     s    z)_ModuleFrame.run_from.<locals>.<listcomp>	outliningzCreating new stack frame for)rN   r^   r_   r   rj  r:  rh  r   rB  r]  (torch._export.passes._node_metadata_hookrr  r   rq   r?   r  r,  r7  r8  r9  rL   r   r   rn  rl  )rD   ro  
module_idxra   rm  rr  node_module_stacknext_moduler&   r&   r'   rn  }  sb   



	

z_ModuleFrame.run_fromr@   )r    r!   r"   r   r   r   r	   r0   r1   r2   rB   rW  rJ  rh  rl  rp  r   rn  r&   r&   r&   r'   r,    s    
	


Br,  
orig_graphroot_modulec                 C   s>   i }i }t | t| j||d dgddd |jD |d	  d S )Nrw   c                 S   s   i | ]
}|j r|j|j qS r&   )r   rq   rr   r&   r&   r'   r     s    z'_outline_submodules.<locals>.<dictcomp>)r   )r,  rP   r^   r   rp  )rz  r{  r8  r9  r&   r&   r'   r     s    
r   rw   r2  r   r:   c                 C   s   |dkr!t | dd  D ]}t| |d u r t| |tj  qg }t | j D ]%\}}|d u r3q*|| }t	|||d d t
| | ||| ||f q*|jtdd |D ]\}}}| || q[d S )Nrw   r   r-   )r:   r   )r  )rL   r   r(  r+  r0   r1   r2   _modulesr   r   delattrrO   sortr3  
itemgetterregister_module)r2  r   r:   rq   childrenr~   childr   r&   r&   r'   r     s"   
r   r   r   c              	      s  t t | j D ]"\}tttjj|||g }| D ]\}} | 	| qq	t
| ds3 S | j}ttdd |j}|d }	tdd |j}
|
D ]#}t| |jddurqt v rqtt fdd|j|_qNi }|D ].}|j|vr~qvd}||j D ]}|d}|dt| |kr|} nq|du rqv|||< qvg }| D ]S\}}t|jd	kr|t|d }t| |}t|tjtjfsJ ||	 |d
d|}W d   n1 sw   Y  |j|dd || ||j qt| t r	| !  t| |iS )aT  Sink params, buffers, and constants from graph inputs into get_attr nodes.

    Exported modules are purely functional, so they pass their parameters and
    buffers in as inputs to the graph.

    To replicate eager's semantics, we need to get them from the module state
    via get_attr instead.

    module: GraphModule, potentially containining nested submodules.
    inputs_to_state: mapping graph input names to the corresponding key in the state_dict.
    scope: tracks where we are in the module hierarchy, so that we can emit the
        right `getattr(self, "foo.bar")` calls, etc.
    r?   c                 S   
   | j dkS )NrZ   r   r   r&   r&   r'   r        
 z_sink_params.<locals>.<lambda>r-  c                 S   r  )NrA  r   r  r&   r&   r'   r      r  r-   Nc                    s   | j  t vS r@   )r~   r   r  module_id_to_inputs_removed	submoduler&   r'   r   (  rv   r   r!  T)propagate_meta)"r   rL   r|  r   r   r   r0   r1   r2   extendr   r?   filterr^   _recursive_getattrr*   r.   r   rP   rT   r~   rN   r^  r4   r7   r9   r   r   r  r   
erase_noderO   r   rb   )r   r   r   r~   submod_id_to_inputs_removedr   r   r?   rG  the_last_inputcall_module_nodesra   inputs_to_state_of_scope
state_namesnsn_splitinputs_removed	attr_path
state_attrr   r&   r  r'   r     sj   





r   c                 C   s(   |D ]}t | |s d S t| |} q| S r@   )r   r/   )objr  attrr&   r&   r'   r  `  s
   
r  )Tr@   )rw   )Grk   rH  r3  collectionsr   r   enumr   typingr   r   r   r   r	   r
   r   r   r0   torch.fx._pytreerQ   _pytreer"  torch.utils._pytreeutilsrm   "torch._library.fake_class_registryr   torch.export._tree_utilsr   torch.export.exported_programr   r   r   r   r   r   torch.fx._symbolic_tracer   r   r   __all__r   r7   r9   r1   r2   r   boolr>   r   ABCr   r   r   rd   r   r  r
  r  r  r  r%  r'  r(  r+  r,  r   intr   r   r  r&   r&   r&   r'   <module>   s   ( 
$=  ?
8	  I


i