
    ёi                        S r SSKJr  SSKJr  SSKJr  \(       a  SSKJr  SSK	J
r
  SSKJr   " S	 S
\5      r " S S\5      rg)z!Define stub used in quantization.    )annotations)TYPE_CHECKING   )Layer)Tensor)QuantConfig)QuanterFactoryc                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	Stub   a  
The stub is used as placeholders that will be replaced by observers before PTQ or QAT.
It is hard to assign a quantization configuration to a functional API called in
the forward of a layer. Instead, we can create a stub and add it to the sublayers of the layer.
And call the stub before the functional API in the forward. The observer held by the
stub will observe or quantize the inputs of the functional API.

Args:
    observer(QuanterFactory): The configured information of the observer to be inserted.
        It will use a global configuration to create the observers if the 'observer' is none.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.nn.quant import Stub
        >>> from paddle.quantization.quanters import FakeQuanterWithAbsMaxObserver
        >>> from paddle.nn import Conv2D
        >>> from paddle.quantization import QAT, QuantConfig

        >>> quanter = FakeQuanterWithAbsMaxObserver(moving_rate=0.9)
        >>> class Model(paddle.nn.Layer):
        ...     def __init__(self, num_classes=10):
        ...         super().__init__()
        ...         self.conv = Conv2D(3, 6, 3, stride=1, padding=1)
        ...         self.quant = Stub(quanter)
        ...
        ...     def forward(self, inputs):
        ...         out = self.conv(inputs)
        ...         out = self.quant(out)
        ...         return paddle.nn.functional.relu(out)

        >>> model = Model()
        >>> q_config = QuantConfig(activation=quanter, weight=quanter)
        >>> qat = QAT(q_config)
        >>> quant_model = qat.quantize(model)
        >>> print(quant_model)
        Model(
            (conv): QuantedConv2D(
                (weight_quanter): FakeQuanterWithAbsMaxObserverLayer()
                (activation_quanter): FakeQuanterWithAbsMaxObserverLayer()
            )
            (quant): QuanterStub(
                (_observer): FakeQuanterWithAbsMaxObserverLayer()
            )
        )
c                .   > [         TU ]  5         Xl        g N)super__init__	_observer)selfobserver	__class__s     T/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/quant/stub.pyr   Stub.__init__N   s    !    c                    U$ r    r   inputs     r   forwardStub.forwardR   s    r   r   r   )r   zQuanterFactory | NonereturnNoner   r   r   r   	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classcell__r   s   @r   r   r      s    .`" " r   r   c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )QuanterStubV   a  
It is an identity layer with an observer observing the input.
Before QAT or PTQ, the stub in the model will be replaced with an instance of QuanterStub.
The user should not use this class directly.

Args:
    layer(paddle.nn.Layer): The stub layer with an observer configure factory. If the observer
    of the stub layer is none, it will use 'q_config' to create an observer instance.
    q_config(QuantConfig): The quantization configuration for the current stub layer.
c                   > [         TU ]  5         S U l        UR                  b!  UR                  R                  U5      U l        g UR                  b!  UR                  R                  U5      U l        g g r   )r   r   r   	_instance
activation)r   layerq_configr   s      r   r   QuanterStub.__init__b   s^    ??&"__66u=DN  ,%00::5ADN -r   c                B    U R                   b  U R                  U5      $ U$ r   r   r   s     r   r   QuanterStub.forwardj   s    (,(Bt~~e$MMr   r   )r1   r   r2   r   r   r    r!   r"   r*   s   @r   r,   r,   V   s    	BN Nr   r,   N)r'   
__future__r   typingr   layer.layersr   paddler   paddle.quantizationr   paddle.quantization.factoryr	   r   r,   r   r   r   <module>r<      s:    ( "    /:65 6rN% Nr   