U
    ©Ê¹j�  ã                   @   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rˆt|j‡ fdd„ƒ}t|j‡ fdd„ƒ}t|t	ƒs`t
‚t|tƒsnt
‚| |||||j¡ˆ |< q| |‡ fdd„¡ˆ |< q|| _dS )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 targetc                    s   ˆ |  S ©N© ©Ún©Zval_mapr   úa/var/www/html/bwave/python/venv/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 )zÈGiven 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 )zŠGiven 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:   ÿû ÿ þ