o
    i                     @   s&  d dl Z d dlmZ 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mZ eddd	G d
d dZe jee df Zde_G dd deZdee fddZdee fddZdee fddZdee fddZdedefddZdefddZdefdd Zd!ed"efd#d$ZdS )%    N)	dataclass)Callable
NamedTupleOptional)Node)Q_ANNOTATION_KEY   )QuantizationAnnotationQuantizationSpecT)eqfrozenc                   @   sJ   e Zd ZU ee ed< ee ed< ee ed< ee ed< dZeed< dS )QuantizationConfiginput_activationoutput_activationweightbiasFis_qatN)__name__
__module____qualname__r   r
   __annotations__r   bool r   r   ]/home/ubuntu/.local/lib/python3.10/site-packages/torchao/quantization/pt2e/quantizer/utils.pyr      s   
 r   z)torchao.quantization.pt2e.quantizer.utilsc                   @   s"   e Zd ZU eed< ee ed< dS )OperatorConfigconfig	operatorsN)r   r   r   r   r   listOperatorPatternTyper   r   r   r   r   %   s   
 	r   quantization_configc                 C   :   | d u rd S | j d u rd S | j }|jtjtjfv sJ |S N)r   qschemetorchper_tensor_affineper_tensor_symmetricr   quantization_specr   r   r   get_input_act_qspec2      

r(   c                 C   r    r!   )r   r"   r#   r$   r%   r&   r   r   r   get_output_act_qspec?   r)   r*   c                 C   sT   | d u rd S | d usJ | j d u rd S | j }|jtjtjd fvr(td| d|S )NzUnsupported quantization_spec z for weight)r   r"   r#   r%   per_channel_symmetric
ValueErrorr&   r   r   r   get_weight_qspecL   s   

r-   c                 C   sD   | d u rd S | d usJ | j d u rd S | j }|jtjks J d|S )Nz9Only float dtype for bias is supported for bias right now)r   dtyper#   floatr&   r   r   r   get_bias_qspec^   s   
r0   node
input_nodec                 C   s8   | j tt }|jd u ri |_||j|< || j t< d S r!   )metagetr   r	   input_qspec_map)r1   r2   qspecquantization_annotationr   r   r   annotate_input_qspec_mapk   s
   

r8   c                 C   s$   | j tt }||_|| j t< d S r!   )r3   r4   r   r	   output_qspec)r1   r6   r7   r   r   r   annotate_output_qspecs   s   r:   module_namec                    s   dt dtf fdd}|S )a  Get the module_name_filter function for a given module name, the filter accepts
    a node and checks if the node comes from a module that has certain module name

    For example:
        node: linear_op = call_function[...](...)  # comes from a module with name blocks.sub.linear1


    >> module_name_filter = _get_module_name_filter("blocks.sub")
    >> print(module_name_filter(node))
    True  # the node is from "blocks.sub" based on the fully qualified name "blocks.sub.linear1"
    nreturnc                    s4   | j di }dd   fdd| D }|v S )Nnn_module_stackc                 S   s"   d}|  drtd}| |d  S )Nr   z
L['self'].)
startswithlen)r<   prefixr   r   r   _normalize_path   s   
zKget_module_name_filter.<locals>.module_name_filter.<locals>._normalize_pathc                    s   g | ]\}} |qS r   r   ).0r<   _rB   r   r   
<listcomp>   s    zFget_module_name_filter.<locals>.module_name_filter.<locals>.<listcomp>)r3   r4   values)r<   r>   namesr;   rE   r   module_name_filter   s   z2get_module_name_filter.<locals>.module_name_filter)r   r   )r;   rJ   r   rI   r   get_module_name_filtery   s   rK   
annotationr=   c                 C   s4   | d u rdS | j }| j}t|dkr|d u rdS dS )NFr   T)r5   r9   r@   )rL   r5   r9   r   r   r   is_valid_annotation   s   rM   )typingdataclassesr   r   r   r   r#   torch.fxr   -torchao.quantization.pt2e.quantizer.quantizerr   	quantizerr	   r
   r   	Annotatedr   r   r   r   r(   r*   r-   r0   r8   r:   strrK   r   rM   r   r   r   r   <module>   s(   

"