o
    Hh|                     @   s  d dl mZmZ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 g dZed	d
de
dedededef
ddZed	d
G dd deZed	d
	dde
deeej  ddfddZed	d
dedefddZed	d
de
dedefddZdS )    )Any
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           	         s   t  }i  | jjD ]G}|j|krD|j|krDt|j fdd}t|j fdd}t|t	s0J t|t
s7J ||||||j |< q	|| fdd |< q	|| _dS )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                        |  S N nval_mapr   V/var/www/vscode/kcb/lib/python3.10/site-packages/torch/fx/passes/graph_manipulation.py<lambda>#       z+replace_target_nodes_with.<locals>.<lambda>c                    r   r   r   r   r   r   r   r   $   r    c                    r   r   r   r   r   r   r   r   +   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   r   r   r      s   


r   c                   @   s   e Zd ZU eed< eed< dS )r   output_size
total_sizeN)__name__
__module____qualname__int__annotations__r   r   r   r   r   /   s   
 r   r%   returnc                 C   sD   |durt | j|  | jjD ]}|jdkr dS t| ||_qdS )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%   r.   r   r   r   r   5   s   
r   r.   c                 C   s$   | 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)r.   r9   r   r   r   r   G   s   
r   c                 C   s   d}|j dkr#t|  }||j }| }|D ]
\}}|| 7 }qt|}|j }	||	7 }|jr>t	j
g |jd }
n
t	jg |jd }
|
| }|
|	 }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_quantizedr>   element_sizetensorr   )r   r.   total_num_of_elemssubmodule_dict	submodule
parameters_namepr9   output_elemsize_per_elem_bytesr0   r/   r   r   r   r   T   s(   



r   r   )typingr   r   r   rD   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.graph_moduler   torch.fx.noder   r	   r
   torch.fx.passes.shape_propr   __all__strr   r   listTensorr   r   r   r   r   r   r   <module>   sF   	