U
    *-e                     @   s  d dl mZmZ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 dd	d
ddgZeddeeeeedddZeddG dd	 d	eZedddeeeej  dddd
ZeddeedddZeddeeedddZdS )    )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)Zis_backward_compatible)	fx_moduleold_op
old_targetnew_op
new_targetc           	         s   t  }i  | jjD ]}|j|kr|j|krt|j fdd}t|j fdd}t|t	s`t
t|tsnt
||||||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    |  S N nZval_mapr   c/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/torch/fx/passes/graph_manipulation.py<lambda>       z+replace_target_nodes_with.<locals>.<lambda>c                    s    |  S r   r   r   r   r   r   r       r   c                    s    |  S r   r   r   r   r   r   r   '   r   N)r   graphnodesoptargetr
   argskwargs
isinstancetupleAssertionErrordictZcreate_nodenameZ	node_copy)	r   r   r   r   r   Z	new_graphnoder$   r%   r   r   r   r      s"    
    
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   sF   |dk	rt | j|  d}| jjD ]}|jdkr4 qBt| ||_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 sizeNg        output)r   	propagater    r!   r"   r   r   )r   r$   Ztotal_size_of_graphr+   r   r   r   r   1   s    
)r+   r3   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+   r6   r   r   r   r   D   s    
)r   r+   r3   c                 C   s   d}|j dkrFt|  }||j }| }|D ]\}}|| 7 }q0t|}|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   Zcall_module)dtype)r"   r)   Znamed_modulesr#   Znamed_parametersZnumelr   shapeZis_quantizedtorchZ_empty_affine_quantizedr:   Zelement_sizeZtensorr   )r   r+   Ztotal_num_of_elemsZsubmodule_dict	submodule
parametersr*   pr6   Zoutput_elemZsize_per_elem_bytesr-   r,   r   r   r   r   Q   s&    


 )N)typingr   r   r   r   r   r<   Ztorch.fx._compatibilityr   Ztorch.fx.graphr   Ztorch.fx.graph_moduler	   Ztorch.fx.noder
   r   r   Ztorch.fx.passes.shape_propr   __all__strr   r   ZTensorr   r   r   r   r   r   r   <module>   s:     