
    9i%              
          S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	J
r
JrJrJrJrJr  S SKJrJr  S SKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJrJrJr  SSK J!r!  SSK"J#r#  / SQr$\ RJ                  " \#5      S\S\S\4S j5       r&\ RJ                  " \#5      S\S\S\4S j5       r'\RP                  RR                  RT                  RV                  \RP                  RR                  RT                  RX                  \RP                  RR                  RZ                  RV                  \RP                  R\                  R^                  /r0S\S\14S jr2\ RJ                  " \#5        SS\S\1S\1S\4S jj5       r3g)    N)constant_fold)DuplicateDQPass)PortNodeMetaForQDQ)DerivedQuantizationSpecFixedQParamsQuantizationSpecQuantizationAnnotationQuantizationSpecQuantizationSpecBase	QuantizerSharedQuantizationSpec)GraphModuleNode)PassManager   )prepare)_fold_conv_bn_qat_fuse_conv_bn_qat) reference_representation_rewrite)_disallow_eval_train_fuse_conv_bn__get_node_name_to_scope)#_convert_to_reference_decomposed_fx)DEPRECATION_WARNING)prepare_pt2eprepare_qat_pt2econvert_pt2emodel	quantizerreturnc                 f   [         R                  R                  S5        U R                  n[	        U 5      n[        U 5        UR                  U 5      n UR                  U 5        UR                  U 5        [        U USUR                  S9n U R                  R                  U5        [        U 5      n U $ )a  Prepare a model for post training quantization

Args:
  * `model` (torch.fx.GraphModule): a model captured by `torch.export.export_for_training` API.
  * `quantizer`: A backend specific quantizer that conveys how user want the
    model to be quantized. Tutorial for how to write a quantizer can be found here:
    https://pytorch.org/tutorials/prototype/pt2e_quantizer.html

Return:
  A GraphModule with observer (based on quantizer annotation), ready for calibration

Example::

    import torch
    from torch.ao.quantization.quantize_pt2e import prepare_pt2e
    from torch.ao.quantization.quantizer import (
        XNNPACKQuantizer,
        get_symmetric_quantization_config,
    )

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

       def forward(self, x):
           return self.linear(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)

    # Step 1. program capture
    # NOTE: this API will be updated to torch.export API in the future, but the captured
    # result should mostly stay the same
    m = torch.export.export_for_training(m, *example_inputs).module()
    # we get a model with aten ops

    # Step 2. quantization
    # backend developer will write their own Quantizer and expose methods to allow
    # users to express how they
    # want the model to be quantized
    quantizer = XNNPACKQuantizer().set_global(get_symmetric_quantization_config())
    m = prepare_pt2e(m, quantizer)

    # run calibration
    # calibrate(m, sample_inference_data)
z+quantization_api.quantize_pt2e.prepare_pt2eFis_qatobs_or_fq_callback)torch_C_log_api_usage_oncemetar   r   transform_for_annotationannotatevalidater   prepare_obs_or_fq_callbackupdater   r   r   original_graph_metanode_name_to_scopes       c/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/ao/quantization/quantize_pt2e.pyr   r   "   s    v 
HH  !NO**07 5..u5Euu$??	E 
JJ)* 'EL    c                 f   [         R                  R                  S5        U R                  n[	        U 5      nUR                  U 5      n UR                  U 5        UR                  U 5        [        U 5        [        U USUR                  S9n U R                  R                  U5        [        U 5      n U $ )a  Prepare a model for quantization aware training

Args:
  * `model` (torch.fx.GraphModule): see :func:`~torch.ao.quantization.quantize_pt2e.prepare_pt2e`
  * `quantizer`: see :func:`~torch.ao.quantization.quantize_pt2e.prepare_pt2e`

Return:
  A GraphModule with fake quant modules (based on quantizer annotation), ready for
  quantization aware training

Example::
    import torch
    from torch.ao.quantization.quantize_pt2e import prepare_qat_pt2e
    from torch.ao.quantization.quantizer import (
        XNNPACKQuantizer,
        get_symmetric_quantization_config,
    )

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

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

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

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

    # Step 1. program capture
    # NOTE: this API will be updated to torch.export API in the future, but the captured
    # result should mostly stay the same
    m = torch.export.export_for_training(m, *example_inputs).module()
    # we get a model with aten ops

    # Step 2. quantization
    # backend developer will write their own Quantizer and expose methods to allow
    # users to express how they
    # want the model to be quantized
    quantizer = XNNPACKQuantizer().set_global(get_symmetric_quantization_config())
    m = prepare_qat_pt2e(m, quantizer)

    # run quantization aware training
    train_loop(prepared_model, train_loop)

z/quantization_api.quantize_pt2e.prepare_qat_pt2eTr!   )r$   r%   r&   r'   r   r(   r)   r*   r   r   r+   r,   r   r-   s       r0   r   r   r   s    r 
HH  !RS**07..u5Euu e$??	E 
JJ)* 'ELr1   nc                 T    U R                   S:H  =(       a    U R                  [        ;   $ )a@  If there is any pure ops between get_attr and quantize op they will be const propagated
e.g. get_attr(weight) -> transpose -> quantize -> dequantize*
(Note: dequantize op is not going to be constant propagated)

This filter is added because we don't want to constant fold the things that are not
related to quantization
call_function)optarget
_QUANT_OPS)r3   s    r0   _quant_node_constraintr9      s!     44?"=qxx:'==r1   use_reference_representationfold_quantizec                    [         R                  R                  S5        [        U[        5      (       d  [        SU S35      eU R                  n[        U 5      n [        U 5      n [        [        5       /5      nU" U 5      R                  n [        [        5       /5      nU" U 5      R                  n U(       a  [        U [        5        U(       a  [        U 5      n U R                  R!                  U5        [#        U 5      n U $ )a  Convert a calibrated/trained model to a quantized model

Args:
  * `model` (torch.fx.GraphModule): calibrated/trained model
  * `use_reference_representation` (bool): boolean flag to indicate whether to produce reference representation or not
  * `fold_quantize` (bool): boolean flag for whether fold the quantize op or not

Returns:
    quantized model, either in q/dq representation or reference representation

Example::

    # prepared_model: the model produced by `prepare_pt2e`/`prepare_qat_pt2e` and calibration/training
    # `convert_pt2e` produces a quantized model that represents quantized computation with
    # quantize dequantize ops and fp32 ops by default.
    # Please refer to
    # https://pytorch.org/tutorials/prototype/pt2e_quant_ptq_static.html#convert-the-calibrated-model-to-a-quantized-model
    # for detailed explanation of output quantized model
    quantized_model = convert_pt2e(prepared_model)

z+quantization_api.quantize_pt2e.convert_pt2ezjUnexpected argument type for `use_reference_representation`, please make sure you intend to pass argument z to convert_pt2e)r$   r%   r&   
isinstancebool
ValueErrorr'   r   r   r   r   graph_moduler   r   r9   r   r,   r   )r   r:   r;   r.   pms        r0   r   r      s    6 
HH  !NO2D99<<X;YYik
 	
  **/6Ee$E	o'(	)BuI""E	(*+	,BuI""Ee34#07	JJ)* 'ELr1   )FT)4typing_extensionsr$   %torch._export.passes.constant_foldingr   ,torch.ao.quantization.pt2e.duplicate_dq_passr   -torch.ao.quantization.pt2e.port_metadata_passr   torch.ao.quantization.quantizerr   r   r   r	   r
   r   r   torch.fxr   r   "torch.fx.passes.infra.pass_managerr   pt2e.preparer   pt2e.qat_utilsr   r   pt2e.representationr   
pt2e.utilsr   r   r   quantize_fxr   utilsr   __all__
deprecatedr   r   opsquantized_decomposedquantize_per_tensordefaulttensorquantize_per_channel
pt2e_quantquantize_affiner8   r>   r9   r    r1   r0   <module>rZ      s     ? H L   ' : ! @ A U U < & 12LLL L 3L^ 12JJJ J 3J\ 
II""66>>	II""66==	II""77??	II((	
>d >t > 12 */22"&2 2 	2 32r1   