
    sgW                     B   U d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZ g dZeZ e       Zeeef   ed<   d	 Zd
eeef   fdZ e       Zeeef   ed<   i Zeeef   ed<   i Zeeef   ed<   ddZd
eeef   fdZd
eee	f   fdZdeeef   d
eeef   fdZy)    N)OrderedDict)AnyDict)FixedQParamsFakeQuantize)ObserverBase)Pattern)get_default_fusion_patternsget_default_quant_patterns.get_default_output_activation_post_process_map_DEFAULT_FUSION_PATTERNSc                       fd}|S )Nc                     | t         <   | S N)r   )fnpatterns    Y/var/www/html/venv/lib/python3.12/site-packages/torch/ao/quantization/fx/pattern_utils.pyinsertz(_register_fusion_pattern.<locals>.insert   s    ,. )	     )r   r   s   ` r   _register_fusion_patternr      s     Mr   returnc                  4    t        j                   t              S r   )copyr   r   r   r   r	   r	       s    99-..r   _DEFAULT_QUANTIZATION_PATTERNS!_DEFAULT_OUTPUT_FAKE_QUANTIZE_MAP_DEFAULT_OUTPUT_OBSERVER_MAPc                       fd}|S )Nc                 j    | t         <   &t        j                        t        <   t        <   | S )N)observer)r   r   	with_argsr   r   )r   fixed_qparams_observerr   s    r   r   z'_register_quant_pattern.<locals>.insert/   sE    24&w/!- )22<RS . 5K(1	r   r   )r   r!   r   s   `` r   _register_quant_patternr"   .   s     Mr   c                  4    t        j                   t              S r   )r   r   r   r   r   r
   r
   <   s    99344r   c                 j    | rt        j                   t              S t        j                   t              S r   )r   r   r   )is_trainings    r   r   r   B   s'     yy:;;yy566r   patterns_dictc                 \    fdt        t        | j                         fd            S )a  
    Return a sorted version of the patterns dictionary such that longer patterns are matched first,
    e.g. match (F.relu, F.linear) before F.relu.
    This works for current use cases, but we may need to have a more clever way to sort
    things to address more complex patterns
    c                 ^    d}t        | t              r| D ]  }| |      z  } |S |dz  }|S )a   this will calculate the length of the pattern by counting all the entries
        in the pattern.
        this will make sure (nn.ReLU, (nn.BatchNorm, nn.Conv2d)) comes before
        (nn.BatchNorm, nn.Conv2d) so that we can match the former first
        r      
isinstancetuple)r   lenitemget_lens      r   r/   z&_sorted_patterns_dict.<locals>.get_len]   sH     gu% %wt}$% 
 1HC
r   c                 F    t        | d   t              r | d          S dS )Nr   r)   r*   )kvr/   s    r   <lambda>z'_sorted_patterns_dict.<locals>.<lambda>n   s"    jA.FGBqEN? A r   )key)r   sorteditems)r&   r/   s    @r   _sorted_patterns_dictr6   S   s.     !M	
 r   r   )r   collectionsr   typingr   r   #torch.ao.quantization.fake_quantizer   torch.ao.quantization.observerr   torch.ao.quantization.utilsr   __all__QuantizeHandlerr   __annotations__r   r	   r   r   r   r"   r
   r   r6   r   r   r   <module>r?      s     #  H 7 /  <G= $w78 H/T'?*B%C / BM Wo%= > N
 EG !4(@#A F?A d7O#;< A
5D/)A$B 57	'<
 7"01	'?
"#r   