§
    ŠŠtj´~  ã                   ó²  — d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dlm	Z	 ddl
mZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZ ddlmZmZ ddl m!Z! ddl"m#Z# deej$        j%        z  de&e'ef         ddfd„Z(dej$        j%        ddfd„Z)deddfd„Z*dej$        j%        ddfd„Z+	 	 d3dede,dee&e'ef         z  dz  dee&e'ef         z  dz  def
d„Z-	 	 	 	 d4dej$        j%        de!e&e'ef         z  de,de.edf         dee&e'ef         z  dz  d e!e&e'ef         z  dz  dee&e'ef         z  dz  d!e,defd"„Z/	 	 d3dej$        j%        de!e&e'ef         z  de,de.edf         dee&e'ef         z  dz  dee&e'ef         z  dz  defd#„Z0	 	 d3dej$        j%        dee&e'ef         z  dz  dee&e'ef         z  dz  defd$„Z1 ej2        e#¦  «        	 	 	 d5dej$        j%        de!e&e'ef         z  de.edf         dee&e'ef         z  dz  d e!e&e'ef         z  dz  dee&e'ef         z  dz  defd%„¦   «         Z3 ej2        e#¦  «        	 	 d3dej$        j%        de!e&e'ef         z  de.edf         dee&e'ef         z  dz  dee&e'ef         z  dz  defd&„¦   «         Z4	 	 	 	 	 	 	 d6d(ed)e,d*ee&e'ef         z  dz  d!e,d+e,de!e&e'ef         z  dz  dee&e'ef         z  dz  d,e,d-e,defd.„Z5 ej2        e#¦  «        	 	 	 	 	 d7d(ed*ee&e'ef         z  dz  d+e,de!e&e'ef         z  dz  dee&e'ef         z  dz  d-e,defd/„¦   «         Z6	 	 	 	 d8d(ed*ee&e'ef         z  dz  d+e,de!e&e'ef         z  dz  dee&e'ef         z  dz  defd0„Z7	 	 	 d5d(ed*ee&e'ef         z  dz  de!e&e'ef         z  dz  dee&e'ef         z  dz  def
d1„Z8	 	 d9d(ed)e,d*ee&e'ef         z  dz  defd2„Z9dS ):é    N)ÚAny)ÚGraphModule)Ú_USER_PRESERVED_ATTRIBUTES_KEYé   )ÚBackendConfigÚget_tensorrt_backend_config)Úconvert)ÚConvertCustomConfigÚFuseCustomConfigÚPrepareCustomConfig)Úfuse)ÚObservedGraphModule)Úprepare)ÚQuantizationTracerÚScopeÚScopeContextManager)Úget_custom_module_class_keysÚ#get_skipped_module_name_and_classes)ÚQConfigMapping)ÚDEPRECATION_WARNINGÚmodelÚpreserved_attrsÚreturnc                 ó¾   — t          j         |¦  «        | j        t          <   | j        t                                        ¦   «         D ]\  }}t	          | ||¦  «         ŒdS )zXStore preserved attributes to the model.meta so that it can be preserved during deepcopyN)ÚcopyÚmetar   ÚitemsÚsetattr)r   r   Ú	attr_nameÚattrs       ú_/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/ao/quantization/quantize_fx.pyÚattach_preserved_attrs_to_modelr"      sc   € õ
 26´¸?Ñ1KÔ1K€E„JÕ-Ñ.ð !œ:Õ&DÔE×KÒKÑMÔMð (ð (‰ˆ	�4Ý��y $Ñ'Ô'Ð'Ð'ð(ð (ó    c                 ó”   — t          | t          ¦  «        s2t          dt          t	          | ¦  «        ¦  «        z   dz   dz   ¦  «        ‚d S )Nz,input model must be a GraphModule, Got type:z Please make zsure to follow the tutorials.)Ú
isinstancer   Ú
ValueErrorÚstrÚtype)r   s    r!   Ú_check_is_graph_moduler)   %   s_   € Ý�e�[Ñ)Ô)ð 
Ýðå•$�u‘+”+ÑÔñð ñð .ñ	.ñ
ô 
ð 	
ð
ð 
r#   c                 óR   — | j         j        D ]}t          |d¦  «        si |_        ŒdS )a¾  Attach meta field to all nodes of the graph if it does not exist,
    meta field is a field stores some meta information about the node, such
    as dtype and shape information for output of the node, this only exists
    if the program is captured by make_fx (used in quantize_pt2e flow), if
    the program is captured by torch.fx symbolic tracing, this field may not exist,
    so we add it here to avoid checking this all over the places
    r   N)ÚgraphÚnodesÚhasattrr   )r   Únodes     r!   Ú!_attach_meta_to_node_if_not_existr/   0   s<   € ð ”Ô!ð ð ˆÝ�t˜VÑ$Ô$ð 	ØˆDŒIøðð r#   c                 óT  — g }|                       ¦   «         D ]S\  }}t          |t          j        j        j        j        ¦  «        r|                     |¦  «         ŒDt          |¦  «         ŒT|D ]:}| j	        |= t          j        j        j         
                    ¦   «         | j	        |<   Œ;dS )z+Swap FloatFunctional with FXFloatFunctionalN)Únamed_childrenr%   ÚtorchÚaoÚnnÚ	quantizedÚFloatFunctionalÚappendÚ_swap_ff_with_fxffÚ_modulesÚFXFloatFunctional)r   Úmodules_to_swapÚnameÚmodules       r!   r8   r8   =   s³   € à€OØ×,Ò,Ñ.Ô.ð 'ð '‰ˆˆfÝ�f�eœhœkÔ3ÔCÑDÔDð 	'Ø×"Ò" 4Ñ(Ô(Ð(Ð(å˜vÑ&Ô&Ð&Ð&àð Ið IˆØŒN˜4Ð Ý$œxœ{Ô4×FÒFÑHÔHˆŒ�tÑÐðIð Ir#   Úis_qatÚfuse_custom_configÚbackend_configc                 óD   — t          | ¦  «         t          | |||¦  «        S )zªInternal helper function to fuse modules in preparation for quantization

    Args:
        model: GraphModule object from symbolic tracing (torch.fx.symbolic_trace)
    )r)   r   )r   r>   r?   r@   s       r!   Ú_fuse_fxrB   K   s'   € õ ˜5Ñ!Ô!Ð!Ý��vÐ1°>ÑBÔBÐBr#   FÚqconfig_mappingÚexample_inputs.Úprepare_custom_configÚ_equalization_configÚis_standalone_modulec                 óŽ  ‡ — |€t          ¦   «         }|€t          ¦   «         }t          |t          ¦  «        r0t	          j        dt          d¬¦  «         t          j        |¦  «        }t          ‰ ¦  «         t          ||¦  «        \  }}	|j
        }
ˆ fd„|
D ¦   «         }t          ||	¦  «        }t          ‰ |                     ‰ ¦  «        ¦  «        }t          |¦  «         t          ¦   «                              |j
        ¦  «        }t#          ||||¦  «        }t%          ||||j        |||||¬¦	  «	        }t)          ||¦  «         |S )aZ  Internal helper function for prepare_fx
        Args:
          `model`, `qconfig_mapping`, `prepare_custom_config`, `_equalization_config`:
          see docs for :func:`~torch.ao.quantization.prepare_fx`
          `is_standalone_module`: a boolean flag indicates whether we are
          quantizing a standalone module or not, a standalone module
          is a submodule of the parent module that is not inlined in the
    forward graph of the parent module,
          the way we quantize standalone module is described in:
          :func:`~torch.ao.quantization._prepare_standalone_module_fx`
    NzšPassing a prepare_custom_config_dict to prepare is deprecated and will not be supported in a future version. Please pass in a PrepareCustomConfig instead.é   ©Ú
stacklevelc                 óR   •— i | ]#}t          ‰|¦  «        ¯|t          ‰|¦  «        “Œ$S © ©r-   Úgetattr©Ú.0r    r   s     €r!   ú
<dictcomp>z_prepare_fx.<locals>.<dictcomp>„   óE   ø€ ð ð ð àÝ�5˜$ÑÔðØ�g�e˜TÑ"Ô"ðð ð r#   )rD   rE   rF   r@   rG   )r   r   r%   ÚdictÚwarningsÚwarnÚFutureWarningÚ	from_dictr8   r   Úpreserved_attributesr   r   Útracer/   r   Úset_preserved_attributesrB   r   Únode_name_to_scoper"   )r   rC   r>   rD   rE   rF   r@   rG   Úskipped_module_namesÚskipped_module_classesÚpreserved_attr_namesr   ÚtracerÚgraph_moduler?   Úprepareds   `               r!   Ú_prepare_fxrc   Z   s’  ø€ ð* Ð$Ý 3Ñ 5Ô 5ÐØÐ#Ý-Ñ/Ô/ÐåÐ'­Ñ.Ô.ð UÝŒðQåØð		
ñ 	
ô 	
ð 	
õ !4Ô =Ð>SÑ TÔ TÐõ �uÑÔÐå3VØÐ3ñ4ô 4Ñ0ÐÐ0ð 1ÔEÐðð ð ð à(ðñ ô €Oõ  Ð 4Ð6LÑMÔM€FÝ˜u f§l¢l°5Ñ&9Ô&9Ñ:Ô:€LÝ% lÑ3Ô3Ð3å)Ñ+Ô+×DÒDØÔ2ñô Ðõ ˜L¨&Ð2DÀnÑUÔU€LÝØØØØÔ!Ø%Ø3Ø1Ø%Ø1ð
ñ 
ô 
€Hõ $ H¨oÑ>Ô>Ð>Ø€Or#   c           	      ó.   — t          | |||||d¬¦  «        S )a  [Internal use only] Prepare a standalone module, so that it can be used when quantizing the
    parent module.
    standalone_module means it a submodule that is not inlined in parent module,
    and will be quantized separately as one unit.

    How the standalone module is observed is specified by `input_quantized_idxs` and
    `output_quantized_idxs` in the prepare_custom_config for the standalone module

    Returns:

        * model(GraphModule): prepared standalone module. It has these attributes in
          model.meta:

            * `standalone_module_input_quantized_idxs(List[Int])`: a list of
              indexes for the graph input that is expected to be quantized,
              same as input_quantized_idxs configuration provided
              for the standalone module
            * `standalone_module_output_quantized_idxs(List[Int])`: a list of
              indices for the graph output that is quantized
              same as input_quantized_idxs configuration provided
              for the standalone module

    T)r@   rG   )rc   )r   rC   r>   rD   rE   r@   s         r!   Ú_prepare_standalone_module_fxre   ¢   s0   € õ> ØØØØØØ%Ø!ðñ ô ð r#   c                 óº  ‡ — |€t          ¦   «         }t          |t          ¦  «        r0t          j        dt
          d¬¦  «         t          j        |¦  «        }t          j         	                    d¦  «         |j
        }ˆ fd„|D ¦   «         }t          j                             ‰ ¦  «        }t          |¦  «         t          |d||¦  «        }t          ||¦  «         |S )a  Fuse modules like conv+bn, conv+bn+relu etc, model must be in eval mode.
    Fusion rules are defined in torch.ao.quantization.fx.fusion_pattern.py

    Args:

        * `model` (torch.nn.Module): a torch.nn.Module model
        * `fuse_custom_config` (FuseCustomConfig): custom configurations for fuse_fx.
            See :class:`~torch.ao.quantization.fx.custom_config.FuseCustomConfig` for more details
    Example::

        from torch.ao.quantization import fuse_fx

        m = Model().eval()
        m = fuse_fx(m)

    Nz‘Passing a fuse_custom_config_dict to fuse is deprecated and will not be supported in a future version. Please pass in a FuseCustomConfig instead.é   rJ   z$quantization_api.quantize_fx.fuse_fxc                 óR   •— i | ]#}t          ‰|¦  «        ¯|t          ‰|¦  «        “Œ$S rM   rN   rP   s     €r!   rR   zfuse_fx.<locals>.<dictcomp>ï   rS   r#   F)r   r%   rT   rU   rV   rW   rX   r2   Ú_CÚ_log_api_usage_oncerY   ÚfxÚsymbolic_tracer/   rB   r"   )r   r?   r@   r_   r   ra   s   `     r!   Úfuse_fxrm   Ì   sû   ø€ ð* Ð!Ý-Ñ/Ô/ÐåÐ$¥dÑ+Ô+ð LÝŒðNåØð		
ñ 	
ô 	
ð 	
õ .Ô7Ð8JÑKÔKÐå	„H× Ò Ð!GÑHÔHÐHØ-ÔBÐðð ð ð à(ðñ ô €Oõ ”8×*Ò*¨5Ñ1Ô1€LÝ% lÑ3Ô3Ð3Ý˜L¨%Ð1CÀ^ÑTÔT€Lå# L°/ÑBÔBÐBØÐr#   c           	      ój   — t           j                             d¦  «         t          | |d||||¦  «        S )aÜ   Prepare a model for post training quantization

    Args:
      * `model` (torch.nn.Module): torch.nn.Module model

      * `qconfig_mapping` (QConfigMapping): QConfigMapping object to configure how a model is
         quantized, see :class:`~torch.ao.quantization.qconfig_mapping.QConfigMapping`
         for more details

      * `example_inputs` (Tuple[Any, ...]): Example inputs for forward function of the model,
         Tuple of positional args (keyword args can be passed as positional args as well)

      * `prepare_custom_config` (PrepareCustomConfig): customization configuration for quantization tool.
          See :class:`~torch.ao.quantization.fx.custom_config.PrepareCustomConfig` for more details

      * `_equalization_config`: config for specifying how to perform equalization on the model

      * `backend_config` (BackendConfig): config that specifies how operators are quantized
         in a backend, this includes how the operators are observed,
         supported fusion patterns, how quantize/dequantize ops are
         inserted, supported dtypes etc. See :class:`~torch.ao.quantization.backend_config.BackendConfig` for more details

    Return:
      A GraphModule with observer (configured by qconfig_mapping), ready for calibration

    Example::

        import torch
        from torch.ao.quantization import get_default_qconfig_mapping
        from torch.ao.quantization.quantize_fx import prepare_fx

        class Submodule(torch.nn.Module):
            def __init__(self) -> None:
                super().__init__()
                self.linear = torch.nn.Linear(5, 5)
            def forward(self, x):
                x = self.linear(x)
                return x

        class M(torch.nn.Module):
            def __init__(self) -> None:
                super().__init__()
                self.linear = torch.nn.Linear(5, 5)
                self.sub = Submodule()

            def forward(self, x):
                x = self.linear(x)
                x = self.sub(x) + x
                return x

        # initialize a floating point model
        float_model = M().eval()

        # define calibration function
        def calibrate(model, data_loader):
            model.eval()
            with torch.no_grad():
                for image, target in data_loader:
                    model(image)

        # qconfig is the configuration for how we insert observers for a particular
        # operator
        # qconfig = get_default_qconfig("fbgemm")
        # Example of customizing qconfig:
        # qconfig = torch.ao.quantization.QConfig(
        #    activation=MinMaxObserver.with_args(dtype=torch.qint8),
        #    weight=MinMaxObserver.with_args(dtype=torch.qint8))
        # `activation` and `weight` are constructors of observer module

        # qconfig_mapping is a collection of quantization configurations, user can
        # set the qconfig for each operator (torch op calls, functional calls, module calls)
        # in the model through qconfig_mapping
        # the following call will get the qconfig_mapping that works best for models
        # that target "fbgemm" backend
        qconfig_mapping = get_default_qconfig_mapping("fbgemm")

        # We can customize qconfig_mapping in different ways.
        # e.g. set the global qconfig, which means we will use the same qconfig for
        # all operators in the model, this can be overwritten by other settings
        # qconfig_mapping = QConfigMapping().set_global(qconfig)
        # e.g. quantize the linear submodule with a specific qconfig
        # qconfig_mapping = QConfigMapping().set_module_name("linear", qconfig)
        # e.g. quantize all nn.Linear modules with a specific qconfig
        # qconfig_mapping = QConfigMapping().set_object_type(torch.nn.Linear, qconfig)
        # for a more complete list, please see the docstring for :class:`torch.ao.quantization.QConfigMapping`
        # argument

        # example_inputs is a tuple of inputs, that is used to infer the type of the
        # outputs in the model
        # currently it's not used, but please make sure model(*example_inputs) runs
        example_inputs = (torch.randn(1, 3, 224, 224),)

        # TODO: add backend_config after we split the backend_config for fbgemm and qnnpack
        # e.g. backend_config = get_default_backend_config("fbgemm")
        # `prepare_fx` inserts observers in the model based on qconfig_mapping and
        # backend_config. If the configuration for an operator in qconfig_mapping
        # is supported in the backend_config (meaning it's supported by the target
        # hardware), we'll insert observer modules according to the qconfig_mapping
        # otherwise the configuration in qconfig_mapping will be ignored
        #
        # Example:
        # in qconfig_mapping, user sets linear module to be quantized with quint8 for
        # activation and qint8 for weight:
        # qconfig = torch.ao.quantization.QConfig(
        #     observer=MinMaxObserver.with_args(dtype=torch.quint8),
        #     weight=MinMaxObserver.with-args(dtype=torch.qint8))
        # Note: current qconfig api does not support setting output observer, but
        # we may extend this to support these more fine grained control in the
        # future
        #
        # qconfig_mapping = QConfigMapping().set_object_type(torch.nn.Linear, qconfig)
        # in backend config, linear module also supports in this configuration:
        # weighted_int8_dtype_config = DTypeConfig(
        #   input_dtype=torch.quint8,
        #   output_dtype=torch.quint8,
        #   weight_dtype=torch.qint8,
        #   bias_type=torch.float)

        # linear_pattern_config = BackendPatternConfig(torch.nn.Linear) \
        #    .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) \
        #    .add_dtype_config(weighted_int8_dtype_config) \
        #    ...

        # backend_config = BackendConfig().set_backend_pattern_config(linear_pattern_config)
        # `prepare_fx` will check that the setting requested by suer in qconfig_mapping
        # is supported by the backend_config and insert observers and fake quant modules
        # in the model
        prepared_model = prepare_fx(float_model, qconfig_mapping, example_inputs)
        # Run calibration
        calibrate(prepared_model, sample_inference_data)
    z'quantization_api.quantize_fx.prepare_fxF©r2   ri   rj   rc   )r   rC   rD   rE   rF   r@   s         r!   Ú
prepare_fxrp   ý   sC   € õX 
„H× Ò Ð!JÑKÔKÐKÝØØØØØØØñô ð r#   c                 ój   — t           j                             d¦  «         t          | |d|||¬¦  «        S )aþ  Prepare a model for quantization aware training

    Args:
      * `model` (torch.nn.Module): torch.nn.Module model
      * `qconfig_mapping` (QConfigMapping): see :func:`~torch.ao.quantization.prepare_fx`
      * `example_inputs` (Tuple[Any, ...]): see :func:`~torch.ao.quantization.prepare_fx`
      * `prepare_custom_config` (PrepareCustomConfig): see :func:`~torch.ao.quantization.prepare_fx`
      * `backend_config` (BackendConfig): see :func:`~torch.ao.quantization.prepare_fx`

    Return:
      A GraphModule with fake quant modules (configured by qconfig_mapping and backend_config), ready for
      quantization aware training

    Example::

        import torch
        from torch.ao.quantization import get_default_qat_qconfig_mapping
        from torch.ao.quantization.quantize_fx import prepare_qat_fx


        class Submodule(torch.nn.Module):
            def __init__(self) -> None:
                super().__init__()
                self.linear = torch.nn.Linear(5, 5)

            def forward(self, x):
                x = self.linear(x)
                return x


        class M(torch.nn.Module):
            def __init__(self) -> None:
                super().__init__()
                self.linear = torch.nn.Linear(5, 5)
                self.sub = Submodule()

            def forward(self, x):
                x = self.linear(x)
                x = self.sub(x) + x
                return x


        # initialize a floating point model
        float_model = M().train()
        # (optional, but preferred) load the weights from pretrained model
        # float_model.load_weights(...)


        # define the training loop for quantization aware training
        def train_loop(model, train_data):
            model.train()
            for image, target in data_loader:
                ...


        # qconfig is the configuration for how we insert observers for a particular
        # operator
        # qconfig = get_default_qconfig("fbgemm")
        # Example of customizing qconfig:
        # qconfig = torch.ao.quantization.QConfig(
        #    activation=FakeQuantize.with_args(observer=MinMaxObserver.with_args(dtype=torch.qint8)),
        #    weight=FakeQuantize.with_args(observer=MinMaxObserver.with_args(dtype=torch.qint8)))
        # `activation` and `weight` are constructors of observer module

        # qconfig_mapping is a collection of quantization configurations, user can
        # set the qconfig for each operator (torch op calls, functional calls, module calls)
        # in the model through qconfig_mapping
        # the following call will get the qconfig_mapping that works best for models
        # that target "fbgemm" backend
        qconfig_mapping = get_default_qat_qconfig_mapping("fbgemm")

        # We can customize qconfig_mapping in different ways, please take a look at
        # the docstring for :func:`~torch.ao.quantization.prepare_fx` for different ways
        # to configure this

        # example_inputs is a tuple of inputs, that is used to infer the type of the
        # outputs in the model
        # currently it's not used, but please make sure model(*example_inputs) runs
        example_inputs = (torch.randn(1, 3, 224, 224),)

        # TODO: add backend_config after we split the backend_config for fbgemm and qnnpack
        # e.g. backend_config = get_default_backend_config("fbgemm")
        # `prepare_qat_fx` inserts observers in the model based on qconfig_mapping and
        # backend_config, if the configuration for an operator in qconfig_mapping
        # is supported in the backend_config (meaning it's supported by the target
        # hardware), we'll insert fake_quantize modules according to the qconfig_mapping
        # otherwise the configuration in qconfig_mapping will be ignored
        # see :func:`~torch.ao.quantization.prepare_fx` for a detailed explanation of
        # how qconfig_mapping interacts with backend_config
        prepared_model = prepare_qat_fx(float_model, qconfig_mapping, example_inputs)
        # Run training
        train_loop(prepared_model, train_loop)

    z+quantization_api.quantize_fx.prepare_qat_fxT)r@   ro   )r   rC   rD   rE   r@   s        r!   Úprepare_qat_fxrr   •  sE   € õL 
„H× Ò Ð!NÑOÔOÐOÝØØØØØØ%ðñ ô ð r#   Tra   Úis_referenceÚconvert_custom_configÚ_remove_qconfigÚis_decomposedÚkeep_original_weightsc	                 óJ  ‡ — |€t          ¦   «         }t          |t          ¦  «        r0t          j        dt
          d¬¦  «         t          j        |¦  «        }t          ‰ ¦  «         |j        }	ˆ fd„|	D ¦   «         }
t          ‰ ||||||||¬¦	  «	        }t          ||
¦  «         |S )z_`is_standalone_module`: see docs in :func:`~torch.ao.quantization.prepare_standalone_module_fx`NzšPassing a convert_custom_config_dict to convert is deprecated and will not be supported in a future version. Please pass in a ConvertCustomConfig instead.rI   rJ   c                 óR   •— i | ]#}t          ‰|¦  «        ¯|t          ‰|¦  «        “Œ$S rM   rN   )rQ   r    ra   s     €r!   rR   z_convert_fx.<locals>.<dictcomp>   sE   ø€ ð ð ð àÝ�< Ñ&Ô&ðØ�g�l DÑ)Ô)ðð ð r#   )Ú_remove_qconfig_flagrC   r@   rv   rw   )r
   r%   rT   rU   rV   rW   rX   r)   rY   r	   r"   )ra   rs   rt   rG   ru   rC   r@   rv   rw   r_   r   r5   s   `           r!   Ú_convert_fxr{     sì   ø€ ð Ð$Ý 3Ñ 5Ô 5ÐåÐ'­Ñ.Ô.ð UÝŒðQåØð		
ñ 	
ô 	
ð 	
õ !4Ô =Ð>SÑ TÔ TÐå˜<Ñ(Ô(Ð(Ø0ÔEÐðð ð ð à(ðñ ô €Oõ ØØØØØ,Ø'Ø%Ø#Ø3ð
ñ 
ô 
€Iõ $ I¨Ñ?Ô?Ð?ØÐr#   c           	      ól   — t           j                             d¦  «         t          | d|||||¬¦  «        S )a�
  Convert a calibrated or trained model to a quantized model

    Args:
        * `graph_module` (torch.fx.GraphModule): A prepared and calibrated/trained model (GraphModule)

        * `convert_custom_config` (ConvertCustomConfig): custom configurations for convert function.
            See :class:`~torch.ao.quantization.fx.custom_config.ConvertCustomConfig` for more details

        * `_remove_qconfig` (bool): Option to remove the qconfig attributes in the model after convert.

        * `qconfig_mapping` (QConfigMapping): config for specifying how to convert a model for quantization.

           The keys must include the ones in the qconfig_mapping passed to `prepare_fx` or `prepare_qat_fx`,
           with the same values or `None`. Additional keys can be specified with values set to `None`.

          For each entry whose value is set to None, we skip quantizing that entry in the model::

            qconfig_mapping = QConfigMapping
                .set_global(qconfig_from_prepare)
                .set_object_type(torch.nn.functional.add, None)  # skip quantizing torch.nn.functional.add
                .set_object_type(torch.nn.functional.linear, qconfig_from_prepare)
                .set_module_name("foo.bar", None)  # skip quantizing module "foo.bar"

         * `backend_config` (BackendConfig): A configuration for the backend which describes how
            operators should be quantized in the backend, this includes quantization
            mode support (static/dynamic/weight_only), dtype support (quint8/qint8 etc.),
            observer placement for each operators and fused operators.
            See :class:`~torch.ao.quantization.backend_config.BackendConfig` for more details

    Return:
        A quantized model (torch.nn.Module)

    Example::

        # prepared_model: the model after prepare_fx/prepare_qat_fx and calibration/training
        # convert_fx converts a calibrated/trained model to a quantized model for the
        # target hardware, this includes converting the model first to a reference
        # quantized model, and then lower the reference quantized model to a backend
        # Currently, the supported backends are fbgemm (onednn), qnnpack (xnnpack) and
        # they share the same set of quantized operators, so we are using the same
        # lowering procedure
        #
        # backend_config defines the corresponding reference quantized module for
        # the weighted modules in the model, e.g. nn.Linear
        # TODO: add backend_config after we split the backend_config for fbgemm and qnnpack
        # e.g. backend_config = get_default_backend_config("fbgemm")
        quantized_model = convert_fx(prepared_model)

    z'quantization_api.quantize_fx.convert_fxF)rs   rt   ru   rC   r@   rw   ©r2   ri   rj   r{   )ra   rt   ru   rC   r@   rw   s         r!   Ú
convert_fxr~   6  sH   € õt 
„H× Ò Ð!JÑKÔKÐKÝØØØ3Ø'Ø'Ø%Ø3ðñ ô ð r#   c                 ój   — t           j                             d¦  «         t          | d||||¬¦  «        S )a}  Convert a calibrated or trained model to a reference quantized model,
    see https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md for more details,
    reference quantized model is a standard representation of a quantized model provided
    by FX Graph Mode Quantization, it can be further lowered to run on the target
    hardware, like accelerators

    Args:
        * `graph_module` (GraphModule): A prepared and calibrated/trained model (GraphModule)

        * `convert_custom_config` (ConvertCustomConfig): custom configurations for convert function.
            See :func:`~torch.ao.quantization.quantize_fx.convert_fx` for more details.

        * `_remove_qconfig` (bool): Option to remove the qconfig attributes in the model after convert.

        * `qconfig_mapping` (QConfigMapping): config for specifying how to convert a model for quantization.
            See :func:`~torch.ao.quantization.quantize_fx.convert_fx` for more details.

         * `backend_config` (BackendConfig): A configuration for the backend which describes how
            operators should be quantized in the backend. See
            :func:`~torch.ao.quantization.quantize_fx.convert_fx` for more details.

    Return:
        A reference quantized model (GraphModule)

    Example::

        # prepared_model: the model after prepare_fx/prepare_qat_fx and calibration/training
        # TODO: add backend_config after we split the backend_config for fbgemm and qnnpack
        # e.g. backend_config = get_default_backend_config("fbgemm")
        reference_quantized_model = convert_to_reference_fx(prepared_model)

    z4quantization_api.quantize_fx.convert_to_reference_fxT)rs   rt   ru   rC   r@   r}   )ra   rt   ru   rC   r@   s        r!   Úconvert_to_reference_fxr€   |  sE   € õN 
„H× Ò Ð!WÑXÔXÐXÝØØØ3Ø'Ø'Ø%ðñ ô ð r#   c           	      ól   — t           j                             d¦  «         t          | d|d||d¬¦  «        S )a  Convert a calibrated or trained model to a reference quantized model, with
    decomposed representation for quantized Tensor
    see https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md for more details,
    reference quantized model is a standard representation of a quantized model provided
    by FX Graph Mode Quantization, it can be further lowered to run on the target
    hardware, like accelerators

    Note: this is not public API

    Args:
        * `graph_module` (GraphModule): A prepared and calibrated/trained model (GraphModule)

        * `convert_custom_config` (ConvertCustomConfig): custom configurations for convert function.
            See :func:`~torch.ao.quantization.quantize_fx.convert_fx` for more details.

        * `_remove_qconfig` (bool): Option to remove the qconfig attributes in the model after convert.

        * `qconfig_mapping` (QConfigMapping): config for specifying how to convert a model for quantization.
            See :func:`~torch.ao.quantization.quantize_fx.convert_fx` for more details.

         * `backend_config` (BackendConfig): A configuration for the backend which describes how
            operators should be quantized in the backend. See
            :func:`~torch.ao.quantization.quantize_fx.convert_fx` for more details.

    Return:
        A reference quantized model (GraphModule) with operators working with decomposed quantized Tensor

    Example::

        # prepared_model: the model after prepare_fx/prepare_qat_fx and calibration/training
        # TODO: add backend_config after we split the backend_config for fbgemm and qnnpack
        # e.g. backend_config = get_default_backend_config("fbgemm")
        reference_quantized_model = _convert_to_reference_decomposed_fx(prepared_model)

    z@quantization_api.quantize_fx._convert_to_reference_decomposed_fxTF)rs   rt   ru   rC   r@   rv   r}   )ra   rt   rC   r@   s       r!   Ú#_convert_to_reference_decomposed_fxr‚   ®  sP   € õR 
„H× Ò ØJñô ð õ ØØØ3ØØ'Ø%Øðñ ô ð r#   c                 ó(   — t          | ||d¬¦  «        S )av  [Internal use only] Convert a model produced by :func:`~torch.ao.quantization.prepare_standalone_module_fx`
    and convert it to a quantized model

    Returns a quantized standalone module, whether input/output is quantized is
    specified by prepare_custom_config, with
    input_quantized_idxs, output_quantized_idxs, please
    see docs for prepare_fx for details
    T)rG   )r{   )ra   rs   rt   s      r!   Ú_convert_standalone_module_fxr„   å  s'   € õ ØØØØ!ð	ñ ô ð r#   )NN)NNNF)NNN)NFTNNFF)NTNNF)NTNN)FN):r   Útyping_extensionsrU   Útypingr   r2   Útorch.fxr   Útorch.fx.graph_moduler   r@   r   r   Ú
fx.convertr	   Úfx.custom_configr
   r   r   Úfx.fuser   Úfx.graph_moduler   Ú
fx.preparer   Ú	fx.tracerr   r   r   Úfx.utilsr   r   rC   r   Úutilsr   r4   ÚModulerT   r'   r"   r)   r/   r8   ÚboolrB   Útuplerc   re   rm   Ú
deprecatedrp   rr   r{   r~   r€   r‚   r„   rM   r#   r!   ú<module>r•      s(	  ðØ €€€Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø @Ð @Ð @Ð @Ð @Ð @à FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ Ð Ð Ð Ð Ð Ø XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ Ð Ð Ð Ð Ð Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø Ð Ð Ð Ð Ð Ø EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ Eðð ð ð ð ð ð ð ð ,Ð +Ð +Ð +Ð +Ð +Ø &Ð &Ð &Ð &Ð &Ð &ð	(Ø˜œœÑ(ð	(à˜#˜s˜(”^ð	(ð 
ð	(ð 	(ð 	(ð 	(ð
 %¤(¤/ð 
°dð 
ð 
ð 
ð 
ð
¨[ð 
¸Tð 
ð 
ð 
ð 
ðI˜eœhœoð I°$ð Ið Ið Ið Ið" DHØ<@ð	Cð CØðCàðCð )¨4°°S°¬>Ñ9¸DÑ@ðCð " D¨¨c¨¤NÑ2°TÑ9ð	Cð
 ðCð Cð Cð Cð( JNØCGØ<@Ø!&ðEð EØŒ8Œ?ðEà# d¨3°¨8¤nÑ4ðEð ðEð ˜#˜s˜(”Oð	Eð
 /°°c¸3°h´Ñ?À$ÑFðEð )¨4°°S°¬>Ñ9¸DÑ@ðEð " D¨¨c¨¤NÑ2°TÑ9ðEð ðEð ðEð Eð Eð EðZ JNØ<@ð'ð 'ØŒ8Œ?ð'à# d¨3°¨8¤nÑ4ð'ð ð'ð ˜#˜s˜(”Oð	'ð
 /°°c¸3°h´Ñ?À$ÑFð'ð " D¨¨c¨¤NÑ2°TÑ9ð'ð ð'ð 'ð 'ð 'ðX DHØ<@ð.ð .ØŒ8Œ?ð.à(¨4°°S°¬>Ñ9¸DÑ@ð.ð " D¨¨c¨¤NÑ2°TÑ9ð.ð ð	.ð .ð .ð .ðb ÐÔÐ1Ñ2Ô2ð
 JNØCGØ<@ðTð TØŒ8Œ?ðTà# d¨3°¨8¤nÑ4ðTð ˜#˜s˜(”OðTð /°°c¸3°h´Ñ?À$ÑFð	Tð
 )¨4°°S°¬>Ñ9¸DÑ@ðTð " D¨¨c¨¤NÑ2°TÑ9ðTð ðTð Tð Tñ 3Ô2ðTðn ÐÔÐ1Ñ2Ô2ð
 JNØ<@ðmð mØŒ8Œ?ðmà# d¨3°¨8¤nÑ4ðmð ˜#˜s˜(”Oðmð /°°c¸3°h´Ñ?À$ÑFð	mð
 " D¨¨c¨¤NÑ2°TÑ9ðmð ðmð mð mñ 3Ô2ðmðf JNØ!&Ø Ø>BØ<@ØØ"'ð-ð -Øð-àð-ð /°°c¸3°h´Ñ?À$ÑFð-ð ð	-ð
 ð-ð $ d¨3°¨8¤nÑ4°tÑ;ð-ð " D¨¨c¨¤NÑ2°TÑ9ð-ð ð-ð  ð-ð ð-ð -ð -ð -ð` ÐÔÐ1Ñ2Ô2ð JNØ Ø>BØ<@Ø"'ðBð BØðBà.°°c¸3°h´Ñ?À$ÑFðBð ðBð $ d¨3°¨8¤nÑ4°tÑ;ð	Bð
 " D¨¨c¨¤NÑ2°TÑ9ðBð  ðBð ðBð Bð Bñ 3Ô2ðBðN JNØ Ø>BØ<@ð/ð /Øð/à.°°c¸3°h´Ñ?À$ÑFð/ð ð/ð $ d¨3°¨8¤nÑ4°tÑ;ð	/ð
 " D¨¨c¨¤NÑ2°TÑ9ð/ð ð/ð /ð /ð /ðh JNØ>BØ<@ð	4ð 4Øð4à.°°c¸3°h´Ñ?À$ÑFð4ð $ d¨3°¨8¤nÑ4°tÑ;ð4ð " D¨¨c¨¤NÑ2°TÑ9ð	4ð
 ð4ð 4ð 4ð 4ðr ØIMðð Øðàðð /°°c¸3°h´Ñ?À$ÑFðð ð	ð ð ð ð ð r#   