o
    #j                     @   s  d dl Zd dlZd dlmZ i dejjdejjdejjdejj	dejj
dejjd	ejjd
ejjdejjdejjdejjdejjdejjdejjdejjdejjdejjejjejjdZejjejjejjgZejjjejjjejjjejjjejjj gZ!ej"ej#ej$ej%gZ&ej'ej(ej)ej*ej+gZ,ejjejjgZ-g dZ.g dZ/dd Z0dd Z1dd Z2dd Z3dd  Z4d!d" Z5d#d$ Z6dS )%    N)quant_layersConv2DTransposeConv2DLinearAdaptiveAvgPool2DAdaptiveMaxPool2D	AvgPool2D	MaxPool2D	Hardswish	LeakyReLUPReLUReLUReLU6SigmoidSoftmaxSwishTanh	BatchNorm)	GroupNorm	LayerNorm)Zconv2dZdepthwise_conv2dmatmulZconv2d_transposeZdepthwise_conv2d_transpose)Z fake_quantize_dequantize_abs_maxZ-fake_channel_wise_quantize_dequantize_abs_maxZ/fake_quantize_dequantize_moving_average_abs_maxc                 C   s0   |  |}|dusJ d| d t| S )z(
    Load variable value from scope
    NzCan not find z in the scope.)Zfind_varnparrayZ
get_tensor)scopevar_nameZvar_node r   e/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/quantization/imperative/utils.pyload_variable_data]   s   
r   c                 C   s"   | j D ]}||jv r|  S qdS )z6
    Find the previous op for the input variable.
    N)opsZoutput_arg_names)blockr   opr   r   r   find_previous_opf   s
   

r!   c                 C   s(   g }| j D ]}||jv r|| q|S )z7
    Find all followed ops for the input variable.
    )r   Zinput_arg_namesappend)r   r   Zres_opsr    r   r   r   find_next_opsp   s   


r#   c                 C   s   t | tjjsJ dt|dksJ dd}d}| }|t|k rE|| dkr;||| }t||r;t||}|d }|d7 }|t|k s!||| }||fS )a  
    Given the model and the name of a layer, find the parent layer and
    the sub_name of the layer.
    For example, if name is 'block_1/convbn_1/conv_1', the parent layer is
    'block_1/convbn_1' and the sub_name is `conv_1`.
    Args:
        model(paddle.nn.Layer): the model to be quantized.
        name(string): the name of a layer

    Returns:
        parent_layer, subname
    z2The model must be the instance of paddle.nn.Layer.r   z%The input (name) should not be empty..   )
isinstancepaddlennLayerlenhasattrgetattr)modelnameZlast_idxidxZparent_layerZsub_namer   r   r   find_parent_layer_and_sub_name{   s&   

r0   c                 C   s*   g }| j D ]}|jD ]}|| q
q|S )z/
    Return all ops for the input program.
    )blocksr   r"   )programZall_opsr   r    r   r   r   program_all_ops   s   

r3   c                 C   s   t | tjjot|  dkS )z*
    Whether the layer is leaf layer.
    r   )r&   r'   r(   r)   r*   Z	sublayers)layerr   r   r   is_leaf_layer   s   r5   c                 C   s   | j dkr	t| S |  S )z)
    Convert numpy to float or list.
    r%   )sizefloattolist)Zx_npr   r   r   fp_numpy_to_naive   s   
r9   )7numpyr   r'   Zpaddle.nn.quantr   r(   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Zlayer_name_mapZfake_quant_input_layersZquantaddsubtractmultiplydivider   Zfake_quant_output_layersZFakeQuantAbsMaxZFakeQuantChannelWiseAbsMaxZFakeQuantMovingAverageAbsMaxZMovingAverageAbsMaxScaleZfake_quant_leaf_layersZQuantizedConv2DZQuantizedLinearZQuantizedConv2DTransposeZQuantizedColumnParallelLinearZQuantizedRowParallelLinearZfake_quant_wrap_layersZspec_channel_axis_layersZweight_op_typesZ!fake_quantize_dequantize_op_typesr   r!   r#   r0   r3   r5   r9   r   r   r   r   <module>   s   	

			
 