
    ^j                     r   d dl m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 d dlmZmZ g dZ ed	
      dedededededdfd       Z ed	
       G d de             Z ed	
      	 ddedeej*                     dz  ddfd       Z ed	
      dedefd       Z ed	
      dededefd       Zy)    )
NamedTupleN)compatibility)Graph)GraphModule)map_argNodeTarget)	ShapePropTensorMetadata)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_targetreturnc                   	 t               }i 	| j                  j                  D ]  }|j                  |k(  r|j                  |k(  rt        |j                  	fd      }t        |j                  	fd      }t        |t              st        dt        |             t        |t              st        dt        |             |j                  |||||j                        	|<   |j                  |	fd      	|<    || _        y)z
    Modifies 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 target.
    c                     |    S N nval_maps    m/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/graph_manipulation.py<lambda>z+replace_target_nodes_with.<locals>.<lambda>$   s    
     c                     |    S r   r   r   s    r   r    z+replace_target_nodes_with.<locals>.<lambda>%   s    GAJ r!   zExpected tuple, got zExpected dict, got c                     |    S r   r   r   s    r   r    z+replace_target_nodes_with.<locals>.<lambda>.   s    
 r!   N)r   graphnodesoptargetr   argskwargs
isinstancetupleAssertionErrortypedictcreate_nodename	node_copy)
r   r   r   r   r   	new_graphnoder(   r)   r   s
            @r   r   r      s     I "G%% L77f
!:499&:;DT[[*>?FdE*$';DJ<%HIIfd+$':4<.%IJJ%11
D&$))GDM &//6JKGDML  IOr!   c                   "    e Zd ZU eed<   eed<   y)r   output_size
total_sizeN)__name__
__module____qualname__int__annotations__r   r!   r   r   r   2   s    Or!   r   r(   c                     | t        |       j                  |  | j                  j                  D ]$  }|j                  dk(  r yt        | |      |_        & y)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(   r3   s      r   r   r   8   s_     &	)&&-%% <77h
 +9d;< r!   r3   c                 ^    | j                   j                  d      }|st        d|  d      |S )Ntensor_metazNode zQ has no tensor metadata associated with it! Check that shape propagation has run.)metagetRuntimeError)r3   r@   s     r   r   r   J   s=    ))--.KD6 4 5
 	

 r!   c                 $   d}|j                   dk(  rUt        | j                               }||j                     }|j	                         }|D ]  \  }}||j                         z  } t        |      }|j                  j                         }	||	z  }|j                  r0t        j                  g |j                        j                         }
n/t        j                  g |j                        j                         }
|
|z  }|
|	z  }t        ||      S )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_quantizedrF   element_sizetensorr   )r   r3   total_num_of_elemssubmodule_dict	submodule
parameters_namepr@   output_elemsize_per_elem_bytesr6   r5   s                r   r   r   W   s    ww-i5578"4;;/	//1
" 	,HE1!'')+	, "$'K##))+K+%#;;k''

,. 	 $ll2[5F5FGTTV$'99J%3Kk:..r!   r   )typingr   rL   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.graph_moduler   torch.fx.noder   r   r	   torch.fx.passes.shape_propr
   r   __all__strr   r   listTensorr   r   r   r   r!   r   <module>rb      s,     1   - / / @ e,      	 
   
  - : e,  -
 e,>B"&u||"4t";	 -" e,	$ 	> 	 -	 e,/ /4 /J / -/r!   