def __init__(self, quantizable_layer_type=['Conv2D', 'Linear', 'Conv2DTranspose'], weight_quantize_type='abs_max', activation_quantize_type='moving_average_abs_max', weight_bits=8, activation_bits=8, moving_rate=0.9, weight_preprocess_layer=None, act_preprocess_layer=None, weight_quantize_layer=None, act_quantize_layer=None):
    '\n        The constructor for ImperativeQuantAware.\n\n        Args:\n            quantizable_layer_type(list[str | layer]): List the type of\n                layers that will be quantized. Default is [\'Conv2D\', \'Linear\'].\n            weight_quantize_type(str): quantization type for weights,\n                which supports \'abs_max\' and \'channel_wise_abs_max\'.\n            activation_quantize_type(str): quantization type for activations,\n                which supports \'abs_max\' and \'moving_average_abs_max\' now.\n                If using \'abs_max\' mode, the quantization scale will be\n                calculated dynamically each step in both training and testing\n                period. If using \'moving_average_abs_max\', the static\n                quantization scale will be calculated during training and\n                used in inference.\n            weight_bits(int): quantization bit number for weights, whereas\n                the bias is not quantized.\n            activation_bits(int): quantization bit number for activations.\n            moving_rate(float): the parameter for \'moving_average_abs_max\'\n                quantization.\n            weight_preprocess_layer(paddle.nn.Layer, optional): A paddle\n                Layer that defines how to preprocess weight before quantization.\n                Using this can quickly test if user\'s preprocess method works\n                or not. The input is non-quantized weight and function returns\n                processed weight to be quantized.\n                If None, the weight will be quantized directly.\n                Default is None.\n            act_preprocess_layer(paddle.nn.Layer, optional): A paddle Layer\n                that defines how to preprocess activation before quantization.\n                Using this can quickly test if user\'s preprocess method works\n                or not. The input is non-quantized activation and function returns\n                processed activation to be quantized.\n                If None, the activation will be quantized directly.\n                Default is None.\n            weight_quantize_layer(paddle.nn.Layer, optional): A paddle Layer that\n                defines how to quantize weight.\n                Using this can quickly test if user\'s quantization method works or not.\n                In this layer, user should both define quantization method and\n                dequantization method, that is, the function\'s input is non-quantized\n                weight and returns dequantized weight.\n                If None, will use uantization op defined by \'weight_quantize_type\'.\n                Default is None.\n            act_quantize_layer(paddle.nn.Layer, optional): A paddle Layer that defines\n                how to quantize activation.\n                Using this can quickly test if user\'s quantization method works or not.\n                In this layer, user should both define quantization method and\n                dequantization method, that is, the function\'s input is non-quantized\n                activation and returns dequantized activation. \n                If None, will use quantization op defined by \'activation_quantize_type\'.\n                Default is None.\n\n        Note:\n            If user sets attribute \'skip_quant\' to a Layer that support dynamic\n            quantization and sets it to true, the layer would not be quantized\n            during training. If this attribute is not sets or the attribute is\n            false, the Layer would be qunatized in training.\n\n        Examples 1:\n        .. code-block:: python\n\n            import paddle\n            from paddle.fluid.contrib.slim.quantization                 import ImperativeQuantAware\n            from paddle.vision.models                 import resnet\n            \n            model = resnet.resnet50(pretrained=True)\n\n            imperative_qat = ImperativeQuantAware(\n                weight_quantize_type=\'abs_max\',\n                activation_quantize_type=\'moving_average_abs_max\')\n            \n            # Add the fake quant logical.\n            # The original model will be rewrite.\n            # The outscale of outputs in supportted layers would be calculated.\n            imperative_qat.quantize(model)\n\n            # Fine-tune the quantized model\n            # ...\n            \n            # Save quant model for the inference.\n            imperative_qat.save_quantized_model(\n                layer=model,\n                model_path="./resnet50_qat",\n                input_spec=[\n                    paddle.static.InputSpec(\n                    shape=[None, 3, 224, 224], dtype=\'float32\')])\n\n        Examples 2:\n        .. code-block:: python\n\n            import paddle\n            from paddle.fluid.contrib.slim.quantization                 import ImperativeQuantAware\n\n            class ImperativeModel(paddle.nn.Layer):\n                def __init__(self):\n                    super(ImperativeModel, self).__init__()\n                    # self.linear_0 would skip the quantization.\n                    self.linear_0 = paddle.nn.Linear(784, 400)\n                    self.linear_0.skip_quant = True\n\n                    # self.linear_1 would not skip the quantization.\n                    self.linear_1 = paddle.nn.Linear(400, 10)\n                    self.linear_1.skip_quant = False\n\n                def forward(self, inputs):\n                    x = self.linear_0(inputs)\n                    x = self.linear_1(inputs)\n                    return x\n\n            model = ImperativeModel()\n            imperative_qat = ImperativeQuantAware(\n                weight_quantize_type=\'abs_max\',\n                activation_quantize_type=\'moving_average_abs_max\')\n\n            # Add the fake quant logical.\n            # The original model will be rewrite.\n            #\n            # There is only one Layer(self.linear1) would be added the\n            # fake quant logical.\n            imperative_qat.quantize(model)\n\n            # Fine-tune the quantized model\n            # ...\n\n            # Save quant model for the inference.\n            imperative_qat.save_quantized_model(\n                layer=model,\n                model_path="./imperative_model_qat")\n        '
    super(ImperativeQuantAware, self).__init__()
    kwargs = {'quantizable_layer_type': quantizable_layer_type, 'weight_quantize_type': weight_quantize_type, 'activation_quantize_type': activation_quantize_type, 'weight_bits': weight_bits, 'activation_bits': activation_bits, 'moving_rate': moving_rate, 'weight_preprocess_layer': weight_preprocess_layer, 'act_preprocess_layer': act_preprocess_layer, 'weight_quantize_layer': weight_quantize_layer, 'act_quantize_layer': act_quantize_layer}
    self._quantize_inputs = ImperativeQuantizeInputs(**kwargs)
    self._quantize_outputs = ImperativeQuantizeOutputs(moving_rate)
The constructor for ImperativeQuantAware.

Args:
    quantizable_layer_type(list[str | layer]): List the type of
        layers that will be quantized. Default is ['Conv2D', 'Linear'].
    weight_quantize_type(str): quantization type for weights,
        which supports 'abs_max' and 'channel_wise_abs_max'.
    activation_quantize_type(str): quantization type for activations,
        which supports 'abs_max' and 'moving_average_abs_max' now.
        If using 'abs_max' mode, the quantization scale will be
        calculated dynamically each step in both training and testing
        period. If using 'moving_average_abs_max', the static
        quantization scale will be calculated during training and
        used in inference.
    weight_bits(int): quantization bit number for weights, whereas
        the bias is not quantized.
    activation_bits(int): quantization bit number for activations.
    moving_rate(float): the parameter for 'moving_average_abs_max'
        quantization.
    weight_preprocess_layer(paddle.nn.Layer, optional): A paddle
        Layer that defines how to preprocess weight before quantization.
        Using this can quickly test if user's preprocess method works
        or not. The input is non-quantized weight and function returns
        processed weight to be quantized.
        If None, the weight will be quantized directly.
        Default is None.
    act_preprocess_layer(paddle.nn.Layer, optional): A paddle Layer
        that defines how to preprocess activation before quantization.
        Using this can quickly test if user's preprocess method works
        or not. The input is non-quantized activation and function returns
        processed activation to be quantized.
        If None, the activation will be quantized directly.
        Default is None.
    weight_quantize_layer(paddle.nn.Layer, optional): A paddle Layer that
        defines how to quantize weight.
        Using this can quickly test if user's quantization method works or not.
        In this layer, user should both define quantization method and
        dequantization method, that is, the function's input is non-quantized
        weight and returns dequantized weight.
        If None, will use uantization op defined by 'weight_quantize_type'.
        Default is None.
    act_quantize_layer(paddle.nn.Layer, optional): A paddle Layer that defines
        how to quantize activation.
        Using this can quickly test if user's quantization method works or not.
        In this layer, user should both define quantization method and
        dequantization method, that is, the function's input is non-quantized
        activation and returns dequantized activation. 
        If None, will use quantization op defined by 'activation_quantize_type'.
        Default is None.

Note:
    If user sets attribute 'skip_quant' to a Layer that support dynamic
    quantization and sets it to true, the layer would not be quantized
    during training. If this attribute is not sets or the attribute is
    false, the Layer would be qunatized in training.

Examples 1:
.. code-block:: python

    import paddle
    from paddle.fluid.contrib.slim.quantization                 import ImperativeQuantAware
    from paddle.vision.models                 import resnet
    
    model = resnet.resnet50(pretrained=True)

    imperative_qat = ImperativeQuantAware(
        weight_quantize_type='abs_max',
        activation_quantize_type='moving_average_abs_max')
    
    # Add the fake quant logical.
    # The original model will be rewrite.
    # The outscale of outputs in supportted layers would be calculated.
    imperative_qat.quantize(model)

    # Fine-tune the quantized model
    # ...
    
    # Save quant model for the inference.
    imperative_qat.save_quantized_model(
        layer=model,
        model_path="./resnet50_qat",
        input_spec=[
            paddle.static.InputSpec(
            shape=[None, 3, 224, 224], dtype='float32')])

Examples 2:
.. code-block:: python

    import paddle
    from paddle.fluid.contrib.slim.quantization                 import ImperativeQuantAware

    class ImperativeModel(paddle.nn.Layer):
        def __init__(self):
            super(ImperativeModel, self).__init__()
            # self.linear_0 would skip the quantization.
            self.linear_0 = paddle.nn.Linear(784, 400)
            self.linear_0.skip_quant = True

            # self.linear_1 would not skip the quantization.
            self.linear_1 = paddle.nn.Linear(400, 10)
            self.linear_1.skip_quant = False

        def forward(self, inputs):
            x = self.linear_0(inputs)
            x = self.linear_1(inputs)
            return x

    model = ImperativeModel()
    imperative_qat = ImperativeQuantAware(
        weight_quantize_type='abs_max',
        activation_quantize_type='moving_average_abs_max')

    # Add the fake quant logical.
    # The original model will be rewrite.
    #
    # There is only one Layer(self.linear1) would be added the
    # fake quant logical.
    imperative_qat.quantize(model)

    # Fine-tune the quantized model
    # ...

    # Save quant model for the inference.
    imperative_qat.save_quantized_model(
        layer=model,
        model_path="./imperative_model_qat")
def __init__(self, quantizable_layer_type=['Conv2D', 'Linear', 'Conv2DTranspose'], weight_quantize_type='abs_max', activation_quantize_type='moving_average_abs_max', weight_bits=8, activation_bits=8, moving_rate=0.9, weight_preprocess_layer=None, act_preprocess_layer=None, weight_quantize_layer=None, act_quantize_layer=None):
    '\n        The constructor for ImperativeQuantAware.\n\n        Args:\n            quantizable_layer_type(list[str | layer]): List the type of\n                layers that will be quantized. Default is [\'Conv2D\', \'Linear\'].\n            weight_quantize_type(str): quantization type for weights,\n                which supports \'abs_max\' and \'channel_wise_abs_max\'.\n            activation_quantize_type(str): quantization type for activations,\n                which supports \'abs_max\' and \'moving_average_abs_max\' now.\n                If using \'abs_max\' mode, the quantization scale will be\n                calculated dynamically each step in both training and testing\n                period. If using \'moving_average_abs_max\', the static\n                quantization scale will be calculated during training and\n                used in inference.\n            weight_bits(int): quantization bit number for weights, whereas\n                the bias is not quantized.\n            activation_bits(int): quantization bit number for activations.\n            moving_rate(float): the parameter for \'moving_average_abs_max\'\n                quantization.\n            weight_preprocess_layer(paddle.nn.Layer, optional): A paddle\n                Layer that defines how to preprocess weight before quantization.\n                Using this can quickly test if user\'s preprocess method works\n                or not. The input is non-quantized weight and function returns\n                processed weight to be quantized.\n                If None, the weight will be quantized directly.\n                Default is None.\n            act_preprocess_layer(paddle.nn.Layer, optional): A paddle Layer\n                that defines how to preprocess activation before quantization.\n                Using this can quickly test if user\'s preprocess method works\n                or not. The input is non-quantized activation and function returns\n                processed activation to be quantized.\n                If None, the activation will be quantized directly.\n                Default is None.\n            weight_quantize_layer(paddle.nn.Layer, optional): A paddle Layer that\n                defines how to quantize weight.\n                Using this can quickly test if user\'s quantization method works or not.\n                In this layer, user should both define quantization method and\n                dequantization method, that is, the function\'s input is non-quantized\n                weight and returns dequantized weight.\n                If None, will use uantization op defined by \'weight_quantize_type\'.\n                Default is None.\n            act_quantize_layer(paddle.nn.Layer, optional): A paddle Layer that defines\n                how to quantize activation.\n                Using this can quickly test if user\'s quantization method works or not.\n                In this layer, user should both define quantization method and\n                dequantization method, that is, the function\'s input is non-quantized\n                activation and returns dequantized activation. \n                If None, will use quantization op defined by \'activation_quantize_type\'.\n                Default is None.\n\n        Note:\n            If user sets attribute \'skip_quant\' to a Layer that support dynamic\n            quantization and sets it to true, the layer would not be quantized\n            during training. If this attribute is not sets or the attribute is\n            false, the Layer would be qunatized in training.\n\n        Examples 1:\n        .. code-block:: python\n\n            import paddle\n            from paddle.fluid.contrib.slim.quantization                 import ImperativeQuantAware\n            from paddle.vision.models                 import resnet\n            \n            model = resnet.resnet50(pretrained=True)\n\n            imperative_qat = ImperativeQuantAware(\n                weight_quantize_type=\'abs_max\',\n                activation_quantize_type=\'moving_average_abs_max\')\n            \n            # Add the fake quant logical.\n            # The original model will be rewrite.\n            # The outscale of outputs in supportted layers would be calculated.\n            imperative_qat.quantize(model)\n\n            # Fine-tune the quantized model\n            # ...\n            \n            # Save quant model for the inference.\n            imperative_qat.save_quantized_model(\n                layer=model,\n                model_path="./resnet50_qat",\n                input_spec=[\n                    paddle.static.InputSpec(\n                    shape=[None, 3, 224, 224], dtype=\'float32\')])\n\n        Examples 2:\n        .. code-block:: python\n\n            import paddle\n            from paddle.fluid.contrib.slim.quantization                 import ImperativeQuantAware\n\n            class ImperativeModel(paddle.nn.Layer):\n                def __init__(self):\n                    super(ImperativeModel, self).__init__()\n                    # self.linear_0 would skip the quantization.\n                    self.linear_0 = paddle.nn.Linear(784, 400)\n                    self.linear_0.skip_quant = True\n\n                    # self.linear_1 would not skip the quantization.\n                    self.linear_1 = paddle.nn.Linear(400, 10)\n                    self.linear_1.skip_quant = False\n\n                def forward(self, inputs):\n                    x = self.linear_0(inputs)\n                    x = self.linear_1(inputs)\n                    return x\n\n            model = ImperativeModel()\n            imperative_qat = ImperativeQuantAware(\n                weight_quantize_type=\'abs_max\',\n                activation_quantize_type=\'moving_average_abs_max\')\n\n            # Add the fake quant logical.\n            # The original model will be rewrite.\n            #\n            # There is only one Layer(self.linear1) would be added the\n            # fake quant logical.\n            imperative_qat.quantize(model)\n\n            # Fine-tune the quantized model\n            # ...\n\n            # Save quant model for the inference.\n            imperative_qat.save_quantized_model(\n                layer=model,\n                model_path="./imperative_model_qat")\n        '
    super(ImperativeQuantAware, self).__init__()
    kwargs = {'quantizable_layer_type': quantizable_layer_type, 'weight_quantize_type': weight_quantize_type, 'activation_quantize_type': activation_quantize_type, 'weight_bits': weight_bits, 'activation_bits': activation_bits, 'moving_rate': moving_rate, 'weight_preprocess_layer': weight_preprocess_layer, 'act_preprocess_layer': act_preprocess_layer, 'weight_quantize_layer': weight_quantize_layer, 'act_quantize_layer': act_quantize_layer}
    self._quantize_inputs = ImperativeQuantizeInputs(**kwargs)
    self._quantize_outputs = ImperativeQuantizeOutputs(moving_rate)