Skip to content

Quantization

QuantBackboneInternalSkipConnection

Bases: QuantSkipConnection

This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between blocks inside the backbone.

Source code in latest/src/super_gradients/modules/quantization/quantized_skip_connections.py
43
44
45
46
47
48
@register_quantized_module(float_source=BackboneInternalSkipConnection)
class QuantBackboneInternalSkipConnection(QuantSkipConnection):
    """
    This is a placeholder module used by the quantization engine only.
    The module is to be used as a quantized substitute to a skip connection between blocks inside the backbone.
    """

QuantCrossModelSkipConnection

Bases: QuantSkipConnection

This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between backbone and the head.

Source code in latest/src/super_gradients/modules/quantization/quantized_skip_connections.py
59
60
61
62
63
64
@register_quantized_module(float_source=CrossModelSkipConnection)
class QuantCrossModelSkipConnection(QuantSkipConnection):
    """
    This is a placeholder module used by the quantization engine only.
    The module is to be used as a quantized substitute to a skip connection between backbone and the head.
    """

QuantHeadInternalSkipConnection

Bases: QuantSkipConnection

This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between blocks inside the head.

Source code in latest/src/super_gradients/modules/quantization/quantized_skip_connections.py
51
52
53
54
55
56
@register_quantized_module(float_source=HeadInternalSkipConnection)
class QuantHeadInternalSkipConnection(QuantSkipConnection):
    """
    This is a placeholder module used by the quantization engine only.
    The module is to be used as a quantized substitute to a skip connection between blocks inside the head.
    """

QuantResidual

Bases: SGQuantMixin

This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a residual skip connection within a single block.

Source code in latest/src/super_gradients/modules/quantization/quantized_skip_connections.py
13
14
15
16
17
18
19
20
21
22
23
24
25
@register_quantized_module(float_source=Residual)
class QuantResidual(SGQuantMixin):
    """
    This is a placeholder module used by the quantization engine only.
    The module is to be used as a quantized substitute to a residual skip connection within a single block.
    """

    if _imported_pytorch_quantization_failure is not None:
        raise _imported_pytorch_quantization_failure

    @classmethod
    def from_float(cls, float_instance: Residual, **kwargs):
        return quant_nn.TensorQuantizer(kwargs.get("quant_desc_input"))

QuantSkipConnection

Bases: SGQuantMixin

This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between blocks.

Source code in latest/src/super_gradients/modules/quantization/quantized_skip_connections.py
28
29
30
31
32
33
34
35
36
37
38
39
40
@register_quantized_module(float_source=SkipConnection)
class QuantSkipConnection(SGQuantMixin):
    """
    This is a placeholder module used by the quantization engine only.
    The module is to be used as a quantized substitute to a skip connection between blocks.
    """

    if _imported_pytorch_quantization_failure is not None:
        raise _imported_pytorch_quantization_failure

    @classmethod
    def from_float(cls, float_instance: SkipConnection, **kwargs):
        return quant_nn.TensorQuantizer(kwargs.get("quant_desc_input"))

QuantAttentionRefinementModule

Bases: SGQuantMixin, AttentionRefinementModule

AttentionRefinementModule to apply on the last two backbone stages.

Source code in latest/src/super_gradients/modules/quantization/quantized_stdc_blocks.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@register_quantized_module(float_source=AttentionRefinementModule, action=QuantizedMetadata.ReplacementAction.REPLACE_AND_RECURE)
class QuantAttentionRefinementModule(SGQuantMixin, AttentionRefinementModule):
    """
    AttentionRefinementModule to apply on the last two backbone stages.
    """

    def __init__(self, in_channels: int, out_channels: int):
        super(QuantAttentionRefinementModule, self).__init__(in_channels=in_channels, out_channels=out_channels)
        self.q_x = Residual()
        self.q_y = Residual()

    def forward(self, x):
        x = self.conv_first(x)
        y = self.attention_block(x)
        return torch.mul(self.q_x(x), self.q_y(y))

QuantBottleneck

Bases: SGQuantMixin

we just insert quantized tensor to the shortcut (=residual) layer, so that it would be quantized NOTE: we must quantize the float instance, so the mode should be QuantizedMetadata.ReplacementAction.RECURE_AND_REPLACE

Source code in latest/src/super_gradients/modules/quantization/resnet_bottleneck.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@register_quantized_module(float_source=Bottleneck, action=QuantizedMetadata.ReplacementAction.RECURE_AND_REPLACE)
class QuantBottleneck(SGQuantMixin):
    """
    we just insert quantized tensor to the shortcut (=residual) layer, so that it would be quantized
    NOTE: we must quantize the float instance, so the mode should be
          QuantizedMetadata.ReplacementAction.RECURE_AND_REPLACE
    """

    if _imported_pytorch_quantization_failure is not None:
        raise _imported_pytorch_quantization_failure

    @classmethod
    def from_float(cls, float_instance: Bottleneck, **kwargs):
        float_instance.shortcut.add_module("residual_quantizer", quant_nn.TensorQuantizer(kwargs.get("quant_desc_input")))
        return float_instance