
    קg9                       U d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlZddlmZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZ g d	Zd
ZdZ dZ!dZ"dZ#ej$        j%        eej$        j&        j'        ededeej$        j(        eej)        ededeej$        j*        eej$        j+        eej,        ededeiZ-de.d<   d,dZ/d-d.d!Z0d/d.d"Z1d.d#Z2d.d$Z3d0d'Z4g d(Z5d)e.d*<    G d+ d          Z6dS )1    )annotations)OrderedDict)AnyCallableDictListTupleUnionN   )default_weight_fake_quantFixedQParamsFakeQuantize)_PartialWrapper)default_fixed_qparams_range_0to1_observer,default_fixed_qparams_range_neg1to1_observerdefault_placeholder_observerdefault_weight_observer)default_quint8_weight_qconfigdefault_reuse_input_qconfig%default_symmetric_qnnpack_qat_qconfig!default_symmetric_qnnpack_qconfigget_default_qat_qconfigget_default_qconfigQConfig
QConfigAny)get_default_qconfig_mappingget_default_qat_qconfig_mappingQConfigMapping object_typemodule_name_regexmodule_namemodule_name_object_type_orderhardsigmoidhardsigmoid_sigmoidsigmoid_tanhtanh_z+Dict[Union[Callable, str], _PartialWrapper]_FIXED_QPARAMS_OP_TO_OBSERVERis_qatboolbackendstrversionintreturnr   c                   | rt          ||          }nt          ||          }| rt          nt          }|dv rt	          |j        |          }n|}t	          |j        t                    }t                                          |          	                    dt                    	                    t          j        j        |          	                    t          j        j        |          	                    t          j        j        |          	                    t          j        j        j        |          	                    t          j        j        j        |          	                    t          j        j        j        |          	                    t          j        j        j        |          	                    t          j        j        |          	                    t          j        j        t.                    }i }t0                                          D ]X\  }	}
|
|v r	||
         }n0| rt5          j        |
          }n|
}t	          ||          }|||
<   |	                    |	|           Y|S )zX
    Return the default QConfigMapping for the given quantization type and backend.
    )fbgemmx86)
activationweightreshape)observer)r   r   r   r   r   r4   r   r   
set_globalset_object_typer   torchnnConvTranspose1dConvTranspose2dConvTranspose3d
functionalconv_transpose1dconv_transpose2dconv_transpose3d
layer_norm	LayerNormPReLUr   r)   itemsr   	with_args)r*   r,   r.   qconfigdefault_weightqconfig_transposeqconfig_layernormqconfig_mapping!fixed_qparams_observer_to_qconfigfixed_qparams_opr7   fixed_qparams_qconfigr4   s                a/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/ao/quantization/qconfig_mapping.py_get_default_qconfig_mappingrQ   =   s<     8)'7;;%gw7728U..>UN ####).
 
 
 $  %.J  
 		G			$?	@	@	13D	E	E	13D	E	E	13D	E	E	,=?P	Q	Q	,=?P	Q	Q	,=?P	Q	Q	,79J	K	K	+->	?	?	)F	G	G  @B%&C&I&I&K&K Q Q"(888$Eh$O!! &5?RRR

%
$+%n% % %! ;P-h7''(8:OPPPP
     r3   c                $    t          d| |          S )a?  
    Return the default QConfigMapping for post training quantization.

    Args:
      * ``backend`` (str) : the quantization backend for the default qconfig mapping, should be
         one of ["x86" (default), "fbgemm", "qnnpack", "onednn"]
      * ``version`` (int) : the version for the default qconfig mapping
    FrQ   r,   r.   s     rP   r   r   ~   s     (w@@@rR   c                $    t          d| |          S )a@  
    Return the default QConfigMapping for quantization aware training.

    Args:
      * ``backend`` (str) : the quantization backend for the default qconfig mapping, should be
         one of ["x86" (default), "fbgemm", "qnnpack", "onednn"]
      * ``version`` (int) : the version for the default qconfig mapping
    TrT   rU   s     rP   r   r      s     (gw???rR   c                 2    t           } t          dd|           S )z
    Return a QConfigMapping that uses `torch.ao.quantization.default_symmetric_qnnpack_qconfig`
    as the default QConfig.
    Fqnnpack)r   1_get_default_qconfig_mapping_with_default_qconfigdefault_qconfigs    rP   &_get_symmetric_qnnpack_qconfig_mappingr\      s"    
 8O<y/  rR   c                 2    t           } t          dd|           S )z
    Return a QConfigMapping that uses `torch.ao.quantization.default_symmetric_qnnpack_qat_qconfig`
    as the default QConfig.
    TrX   )r   rY   rZ   s    rP   *_get_symmetric_qnnpack_qat_qconfig_mappingr^      s"    
 <O<i  rR   r[   r   c                    | rt          |          }nt          |          }|                    |           |j                                        D ]!}|t
          vr|                    ||           "|S )zX
    Return a QConfigMapping that uses the provided qconfig as the default QConfig.
    )r   r   r8   object_type_qconfigskeysr)   r9   )r*   r,   r[   rL   patterns        rP   rY   rY      s      ?9'BB5g>>///"7<<>> F F777++G_EEErR   )global_qconfigr`   module_name_regex_qconfigsmodule_name_qconfigs&module_name_object_type_order_qconfigsz	List[str]_QCONFIG_STYLE_ORDERc                  j    e Zd ZdZddZddZddZddZddZd dZ	d!dZ
d"dZed#d            ZdS )$r   a  
    Mapping from model ops to :class:`torch.ao.quantization.QConfig` s.

    The user can specify QConfigs using the following methods (in increasing match priority):

        ``set_global`` : sets the global (default) QConfig

        ``set_object_type`` : sets the QConfig for a given module type, function, or method name

        ``set_module_name_regex`` : sets the QConfig for modules matching the given regex string

        ``set_module_name`` : sets the QConfig for modules matching the given module name

        ``set_module_name_object_type_order`` : sets the QConfig for modules matching a combination
        of the given module name, object type, and the index at which the module appears

    Example usage::

        qconfig_mapping = QConfigMapping()
            .set_global(global_qconfig)
            .set_object_type(torch.nn.Linear, qconfig1)
            .set_object_type(torch.nn.ReLU, qconfig1)
            .set_module_name_regex("foo.*bar.*conv[0-9]+", qconfig1)
            .set_module_name_regex("foo.*", qconfig2)
            .set_module_name("module1", qconfig1)
            .set_module_name("module2", qconfig2)
            .set_module_name_object_type_order("foo.bar", torch.nn.functional.linear, 0, qconfig3)

    r0   Nonec                    d | _         t                      | _        t                      | _        t                      | _        t                      | _        d S )N)rc   r   r`   rd   re   rf   selfs    rP   __init__zQConfigMapping.__init__   sN    *. MM 	! IT'BM--! MM 	333rR   rc   r   c                    || _         | S )z3
        Set the global (default) QConfig.
        )rc   )rl   rc   s     rP   r8   zQConfigMapping.set_global   s     -rR   r   Union[Callable, str]rH   c                    || j         |<   | S )z
        Set the QConfig for a given module type, function, or method name.
        If the QConfig for an existing object type was already set, the new QConfig will override the old one.
        )r`   )rl   r   rH   s      rP   r9   zQConfigMapping.set_object_type   s     29!+.rR   r    r-   c                    || j         |<   | S )aO  
        Set the QConfig for modules matching the given regex string.

        Regexes will be matched in the order in which they are registered through this method.
        Thus, the caller should register more specific patterns first, e.g.::

            qconfig_mapping = QConfigMapping()
                .set_module_name_regex("foo.*bar.*conv[0-9]+", qconfig1)
                .set_module_name_regex("foo.*bar.*", qconfig2)
                .set_module_name_regex("foo.*", qconfig3)

        In this example, "foo.bar.conv0" would match qconfig1, "foo.bar.linear" would match qconfig2,
        and "foo.baz.relu" would match qconfig3.

        If the QConfig for an existing module name regex was already set, the new QConfig will override the
        old one while preserving the order in which the regexes were originally registered.
        )rd   )rl   r    rH   s      rP   set_module_name_regexz$QConfigMapping.set_module_name_regex  s    ( >E'(9:rR   r!   c                    || j         |<   | S )z
        Set the QConfig for modules matching the given module name.
        If the QConfig for an existing module name was already set, the new QConfig will override the old one.
        )re   )rl   r!   rH   s      rP   set_module_namezQConfigMapping.set_module_name  s    
 29!+.rR   r   indexr/   c                     || j         |||f<   | S )a-  
        Set the QConfig for modules matching a combination of the given module name, object type,
        and the index at which the module appears.

        If the QConfig for an existing (module name, object type, index)  was already set, the new QConfig
        will override the old one.
        )rf   )rl   r!   r   ru   rH   s        rP   !set_module_name_object_type_orderz0QConfigMapping.set_module_name_object_type_order$  s%      	3+u-	
 rR   c                   | j         j        dz   }t          D ]p}|d| z  }t          | |          }t	          |t
                    r9t          |          dk    r&|                                D ]\  }}|d| d| z  }h|d| z  }q|dz   S )Nz (z
 r   z
  z: z
))	__class____name__rg   getattr
isinstancer   lenrF   )rl   output
style_nameqconfigskeyrH   s         rP   __repr__zQConfigMapping.__repr__3  s    (4/. 	, 	,J(J(((FtZ00H(K00 ,S]]Q5F5F$,NN$4$4 6 6LC5S55G555FF6 ++++~rR   Dict[str, Any]c                p   t           | j        t          t          | j                                                  t          t          | j                                                  t          t          | j	                                                  t          d | j                                        D             iS )a?  
        Convert this ``QConfigMapping`` to a dictionary with the following keys:

            "" (for global QConfig)

            "object_type"

            "module_name_regex"

            "module_name"

            "module_name_object_type_order"

        The values of this dictionary are lists of tuples.
        c                "    g | ]\  }}g ||R S  r   ).0kvs      rP   
<listcomp>z*QConfigMapping.to_dict.<locals>.<listcomp>U  s3     6 6 6 Aq!Q6 6 6rR   )_GLOBAL_DICT_KEYrc   _OBJECT_TYPE_DICT_KEYlistr`   rF   _MODULE_NAME_REGEX_DICT_KEYrd   _MODULE_NAME_DICT_KEYre   '_MODULE_NAME_OBJECT_TYPE_ORDER_DICT_KEYrf   rk   s    rP   to_dictzQConfigMapping.to_dict@  s    " d1!4(A(G(G(I(I#J#J'd.M.S.S.U.U)V)V!4(A(G(G(I(I#J#J3 6 6$($O$U$U$W$W6 6 6
 	
rR   qconfig_dictc                ,    |             }t           |v r |                    |t                               |                    t          g           D ]\  }}|                    ||           |                    t
          g           D ]\  }}|                    ||           |                    t          g           D ]\  }}|                    ||           |                    t          g           D ]\  }}}}|
                    ||||            |S )a[  
        Create a ``QConfigMapping`` from a dictionary with the following keys (all optional):

            "" (for global QConfig)

            "object_type"

            "module_name_regex"

            "module_name"

            "module_name_object_type_order"

        The values of this dictionary are expected to be lists of tuples.
        )r   r8   getr   r9   r   rr   r   rt   r   rw   )clsr   confr   rH   r    r!   ru   s           rP   	from_dictzQConfigMapping.from_dict[  sR   " suu|++OOL)9:;;;$0$4$45JB$O$O 	7 	7 K  g6666*6*:*:'+
 +
 	C 	C&w &&'8'BBBB$0$4$45JB$O$O 	7 	7 K  g66668D8H8H3R9
 9
 	 	4KeW 22[%    rR   N)r0   ri   )rc   r   r0   r   )r   ro   rH   r   r0   r   )r    r-   rH   r   r0   r   )r!   r-   rH   r   r0   r   )
r!   r-   r   r   ru   r/   rH   r   r0   r   )r0   r-   )r0   r   )r   r   r0   r   )rz   
__module____qualname____doc__rm   r8   r9   rr   rt   rw   r   r   classmethodr   r   rR   rP   r   r      s         <
 
 
 
         .      
 
 
 

 
 
 
6 ! ! ! [! ! !rR   )r*   r+   r,   r-   r.   r/   r0   r   )r3   r   )r0   r   )r3   r   )r*   r+   r,   r-   r[   r   r0   r   )7
__future__r   collectionsr   typingr   r   r   r   r	   r
   r:   fake_quantizer   r   r7   r   r   r   r   r   rH   r   r   r   r   r   r   r   r   __all__r   r   r   r   r   r;   Hardsigmoidr?   r#   Sigmoidr%   SoftmaxTanhr'   r)   __annotations__rQ   r   r   r\   r^   rY   rg   r   r   rR   rP   <module>r      s   " " " " " " " # # # # # # : : : : : : : : : : : : : : : :  N N N N N N N N             	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    % 1 % *I ' 
HC	H#%N<=	H?	M<89	H?	HM?	J<
89N     "> > > >B
A 
A 
A 
A 
A	@ 	@ 	@ 	@ 	@         &# # #     t t t t t t t t t trR   