
    Pi                        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	           G d
 d                      Ze j        ee         df         Zde_         G d de          Zdee         fdZdee         fdZdee         fdZdee         fdZdedefdZdefdZdefdZdedefdZdS )    N)	dataclass)Callable
NamedTupleOptional)Node)Q_ANNOTATION_KEY   )QuantizationAnnotationQuantizationSpecT)eqfrozenc                   v    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     }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchao/quantization/pt2e/quantizer/utils.pyr   r      sj         /0000 01111%&&&&
#
$$$$FDr   r   z)torchao.quantization.pt2e.quantizer.utilsc                   0    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   %   s2          '((((((r   r   quantization_configc                 p    | d S | j         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_specs     r   get_input_act_qspecr,   2   sW    "t+3t*=*N$")     r   c                 p    | d S | j         d S | j         }|j        t          j        t          j        fv sJ |S r%   )r   r&   r'   r(   r)   r*   s     r   get_output_act_qspecr.   ?   sW    "t,4t*=*O$")     r   c                     | d S | J | j         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*   s     r   get_weight_qspecr2   L   s    "t***!)t*=*D "#)  
 K->KKK
 
 	
 r   c                 t    | d S | J | j         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*   s     r   get_bias_qspecr6   ^   sY    "t***'t*=*B"ek111C 211 r   node
input_nodec                     | j                             t          t                                }|j        i |_        ||j        |<   || j         t          <   d S r%   )metagetr   r
   input_qspec_map)r7   r8   qspecquantization_annotations       r   annotate_input_qspec_mapr?   k   sS    "imm,<>T>V>VWW.624/:?+J7"9DIr   c                     | j                             t          t                                }||_        || j         t          <   d S r%   )r:   r;   r   r
   output_qspec)r7   r=   r>   s      r   annotate_output_qspecrB   s   s<    "imm,<>T>V>VWW+0("9DIr   module_namec                 0     dt           dt          f f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                     | j                             di           }d fd|                                D             }|v S )Nnn_module_stackc                 b    d}|                      d          rt          d          }| |d          S )Nr   z
L['self'].)
startswithlen)rE   prefixs     r   _normalize_pathzKget_module_name_filter.<locals>.module_name_filter.<locals>._normalize_path   s6    F||L)) +\**VWW:r   c                 ,    g | ]\  }} |          S r   r   ).0rE   _rM   s      r   
<listcomp>zFget_module_name_filter.<locals>.module_name_filter.<locals>.<listcomp>   s'    III1##IIIr   )r:   r;   values)rE   rH   namesrM   rC   s      @r   module_name_filterz2get_module_name_filter.<locals>.module_name_filter   s^     &**%6;;	 	 	 JIII0F0F0H0HIIIe##r   )r   r   )rC   rT   s   ` r   get_module_name_filterrU   y   s7    $d $t $ $ $ $ $ $$ r   
annotationrF   c                 X    | dS | j         }| j        }t          |          dk    r|dS dS )NFr   T)r<   rA   rK   )rV   r<   rA   s      r   is_valid_annotationrX      s?    u 0O*L
?q  \%9u4r   )typingdataclassesr   r   r   r   r'   torch.fxr   -torchao.quantization.pt2e.quantizer.quantizerr   	quantizerr
   r   r   	Annotatedr!   r"   r   r   r,   r.   r2   r6   r?   rB   strrU   r   rX   r   r   r   <module>r`      s    ! ! ! ! ! ! 1 1 1 1 1 1 1 1 1 1        J J J J J J ? ? ? ? ? ? ? ? d4          !  &tH~t';< !L  
) 
) 
) 
) 
)Z 
) 
) 
)
X6H-I 
 
 
 

h7I.J 
 
 
 
(3E*F    $
1C(D 
 
 
 
:4 :T : : : :: : : : :    D$: t      r   