a
    ÞñhÏ  ã                   @   sT   d dl Z d dlZdd„ Zdedœdd„Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )é    Nc                 C   s8   |   dd¡}t|ƒdkr$d|d fS |d |d fS d S )NÚ.é   Ú r   )ÚrsplitÚlen)ÚtargetÚr© r	   ú[/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/quantization/fx/utils.pyÚ_parent_name   s    r   T)Úreturnc                 C   s¢  t  d¡}t  d¡}ddddddœ}i }d	}|D ]}t|ƒ||< q0g }| jD �]}	t|	jƒ}
|rl|
 d
d¡}
t|	jƒ}|rŠ||v rŠ|| }t|	jƒ}|rä| 	|¡}|r¸d| 
d¡› d�}| 	|¡}|rØd| 
d¡› d�}| d
d¡}t|	jƒ}|rþ| d
d¡}t|	jƒ}t||
||||fƒD ] \}}t|| t|ƒƒ||< �q| |
||||g¡ qLd}d}||j|i |¤Ž7 }|D ]}||j|i |¤Ž7 }�qt|�rž|d7 }|S )zyReturns a printable representation of the ops in the graph of g.
    If shorten is True, tries to abbreviate fields.
    z<built-in function (.*)>z <built-in method (.*) of type.*>ZplchdrZgt_prmZcl_funZcl_modZcl_meth)ÚplaceholderÚget_attrÚcall_functionZcall_moduleZcall_method)ÚnameÚopr   ÚargsÚkwargsÚactivation_post_processZobsz<bi_fun r   ú>z	<bi_meth r   z9{:<{name}} {:<{op}} {:<{target}} {:<{args}} {:<{kwargs}}
z'*obs_{n} = activation_post_process_{n}
)ÚreÚcompiler   ÚnodesÚstrr   Úreplacer   r   ÚsearchÚgroupr   r   ÚzipÚmaxÚappendÚformat)ÚgÚshortenZbuilt_in_func_reZbuilt_in_meth_reZop_dictZmax_lensZ	col_namesÚsÚresultsÚnr   r   r   Zbuilt_in_funcZbuilt_in_methr   r   ÚkÚvÚres_strZ
format_strÚresultr	   r	   r
   Úgraph_pretty_str   sX    

û






r*   c                 C   s   | t jkp| t jkS ©N)ÚtorchZper_tensor_affineZper_tensor_symmetric©Úqschemer	   r	   r
   Úis_per_tensorN   s    
ÿr/   c                 C   s   | t jt jt jfv S r+   )r,   Zper_channel_affineZ per_channel_affine_float_qparamsZper_channel_symmetricr-   r	   r	   r
   Úis_per_channelR   s    þr0   c                 C   s>   t | jƒsJ dƒ‚|  ¡ \}}t|ƒ}t|ƒ}| j}|||fS )Nz)Only per tensor quantization is supported)r/   r.   Úcalculate_qparamsÚfloatÚintÚdtype)r   ÚscaleÚ
zero_pointr4   r	   r	   r
   Úget_per_tensor_qparamsW   s    r7   c                 C   sf   |   ¡ \}}| j}t| jƒr<t| jƒ}||||dœ}tj}n"t|ƒ}t|ƒ}|||dœ}tj	}||fS )z™ Given an activation_post_process module,
    return quantize op(e.g. quantize_per_tensor) and a dictionary
    of extracted qparams from the module
    )Ú_scale_Ú_zero_point_Z_axis_Ú_dtype_)r8   r9   r:   )
r1   r4   r0   r.   r3   Úch_axisr,   Zquantize_per_channelr2   Zquantize_per_tensor)r   r5   r6   r4   r;   ÚqparamsÚquantize_opr	   r	   r
   Úget_quantize_op_and_qparams_   s    

r>   c                    sˆ   dd„ ‰ ‡ fdd„}t |ƒ\}}|| |ƒ}|g}| ¡ D ]:\}	}
t| |	t|ƒ |
ƒ |	t|ƒ }| | d|¡¡ q8| d|t|ƒi ¡S )a�   Add quantization nodes for given node to graph
    with the qparams calculated from activation_post_process module
    e.g. Given input `node` in `node = self.conv(x)`, insert node:
    `quantized_node = torch.quantize_per_tensor(x, self._scale_0, self._zer_point_0, self._dtype_0)`
    where self._scale_0, self._zero_point_0 and self._dtype_0 are
    calculated from `activation_post_process`
    c                 S   s*   |  ¡ D ]}t| |t|ƒ ƒr dS qdS )NTF)ÚkeysÚhasattrr   )Úmoduler<   Úir   r	   r	   r
   Ú"module_has_qparams_attr_with_indexy   s    z9quantize_node.<locals>.module_has_qparams_attr_with_indexc                    s   d}ˆ | ||ƒr|d7 }q|S )Nr   r   r	   )rA   r<   Úidx©rC   r	   r
   Úget_next_qparams_idx   s    
z+quantize_node.<locals>.get_next_qparams_idxr   r   )r>   ÚitemsÚsetattrr   r   Zcreate_nodeÚtuple)Zroot_moduleÚgraphÚnoder   rF   r=   r<   rD   ÚinputsÚkeyÚvalueZqparam_full_pathr	   rE   r
   Úquantize_nodeq   s    
rO   )T)
r   r,   r   r   r*   r/   r0   r7   r>   rO   r	   r	   r	   r
   Ú<module>   s   B