
    IЦi                     f   S SK JrJrJrJrJr  S SKrS SKJr  S SK	J
r
  S SKJr  S SKJrJrJr  S SKJr  / SQr\" S	S
9S\S\S\S\S\4
S j5       r\" S	S
9 " S S\5      5       r\" S	S
9 SS\S\\\R.                        SS4S jj5       r\" S	S
9S\S\4S j5       r\" S	S
9S\S\S\4S j5       rg)    )AnyDictList
NamedTupleOptionalN)compatibility)Graph)GraphModule)map_argNodeTarget)	ShapeProp)replace_target_nodes_with
size_bytesget_size_of_all_nodesget_tensor_metaget_size_of_nodeF)is_backward_compatible	fx_moduleold_op
old_targetnew_op
new_targetc                   ^	 [        5       n0 m	U R                  R                   H  nUR                  U:X  a  UR                  U:X  a  [        UR                  U	4S j5      n[        UR                  U	4S j5      n[        U[        5      (       d   e[        U[        5      (       d   eUR                  X4XxUR                  5      T	U'   M  UR                  UU	4S j5      T	U'   M     XPl        g)zModifies all nodes in fx_module.graph.nodes which match the specified op code and target,
and updates them to match the new op code and targetc                    > TU    $ N nval_maps    a/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/fx/passes/graph_manipulation.py<lambda>+replace_target_nodes_with.<locals>.<lambda>#   s	    
    c                    > TU    $ r   r   r   s    r!   r"   r#   $   s	    GAJr$   c                    > TU    $ r   r   r   s    r!   r"   r#   +   s	    
r$   N)r	   graphnodesoptargetr   argskwargs
isinstancetupledictcreate_nodename	node_copy)
r   r   r   r   r   	new_graphnoder+   r,   r    s
            @r!   r   r      s     I "G%%77f
!:499&:;DT[[*>?FdE****fd++++%11D$))GDM &//6JKGDM &  Or$   c                   *    \ rS rSr% \\S'   \\S'   Srg)r   /   output_size
total_sizer   N)__name__
__module____qualname____firstlineno__int__annotations____static_attributes__r   r$   r!   r   r   /   s    Or$   r   r+   returnc                     Ub  [        U 5      R                  " U6   U R                  R                   H%  nUR                  S:X  a    g[        X5      Ul        M'     g)zGiven a fx graph module, update each node with its total size (weights + bias + output)
and its output_size(output). For a non-module node, the total size is the output size.
return total sizeNoutput)r   	propagater'   r(   r)   r   r   )r   r+   r4   s      r!   r   r   5   sV     )&&-%%77h
 +9; & r$   r4   c                 h    U R                   R                  S5      nU(       d  [        SU  S35      eU$ )Ntensor_metazNode zQ has no tensor metadata associated with it! Check that shape propagation has run.)metagetRuntimeError)r4   rE   s     r!   r   r   G   s=    ))--.KD6 4 5
 	

 r$   c                    SnUR                   S:X  aT  [        U R                  5       5      nX1R                     nUR	                  5       nU H  u  pgX'R                  5       -  nM     [        U5      nUR                  R                  5       n	X)-  nUR                  (       a.  [        R                  " / UR                  S9R                  5       n
O-[        R                  " / UR                  S9R                  5       n
X-  nX-  n[        X5      $ )zGiven a node with node.dtype and node.shape, return its total size and its output size.
total_size = weights + bias + output_size
r   call_module)dtype)r)   r/   named_modulesr*   named_parametersnumelr   shapeis_quantizedtorch_empty_affine_quantizedrK   element_sizetensorr   )r   r4   total_num_of_elemssubmodule_dict	submodule
parameters_nameprE   output_elemsize_per_elem_bytesr8   r7   s                r!   r   r   T   s     ww-i5578";;/	//1
"HE'')+ # "$'K##))+K%#;;k''

,. 	 $ll2[5F5FGTTV$9J%3Kk..r$   r   )typingr   r   r   r   r   rQ   torch.fx._compatibilityr   torch.fx.graphr	   torch.fx.graph_moduler
   torch.fx.noder   r   r   torch.fx.passes.shape_propr   __all__strr   r   Tensorr   r   r   r   r$   r!   <module>rf      s%   8 8  1   - / / 0 e,      	 
   - 2 e,  -
 e,AE"*4+=">	 -" e,	$ 	3 	 -	 e,/ /4 /J / -/r$   