TorchLean API

FloatLib.Numerics.Quantization.Affine

Executable affine rational quantization #

For a positive rational scale s, zero point z, and integer code interval [qmin, qmax], this module executes nearest-even affine quantization:

quantize x   = clamp (roundEven (x / s) + z)
dequantize k = s * (k - z)

Exact rational parameters keep the scalar kernel executable and make its mathematical denotation independent of host floating-point behavior. Downstream tensor libraries can lift the scalar operation pointwise without introducing a second quantization semantics.

The equations follow the integer-arithmetic quantization scheme used by common neural-network runtimes. See Jacob et al., "Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference," CVPR 2018, doi:10.1109/CVPR.2018.00286.

Parameters of a bounded nearest-even affine quantizer.

  • scale :

    Exact distance between adjacent reconstructed values.

  • zeroPoint :

    Integer code representing zero when it lies in the code range.

  • qmin :

    Smallest stored code.

  • qmax :

    Largest stored code.

  • scale_pos : 0 < self.scale

    A quantization scale is strictly positive.

  • codeRange : self.qmin self.qmax

    The code interval is nonempty.

Instances For
    @[inline]

    Saturate an integer to the quantizer's code interval, using the shared saturating clamp.

    Instances For
      @[inline]

      Integer code before saturation.

      Instances For
        @[inline]

        Quantize an exact rational and saturate the result to the stored code interval.

        Instances For
          @[inline]

          Reconstruct the exact rational denoted by an integer code.

          Instances For
            @[inline]

            Mathematical value produced by executable affine quantization.

            Instances For

              Saturation always returns a valid code.

              @[simp]
              theorem FloatLib.Numerics.Quantization.AffineQuantizer.clampCode_eq_self (q : AffineQuantizer) {code : } (hlo : q.qmin code) (hhi : code q.qmax) :
              q.clampCode code = code

              Saturation fixes a code already inside the representable interval.

              Every quantized scalar lies in the declared code interval.

              @[simp]

              Dequantizing the zero point gives exact zero.

              @[simp]
              theorem FloatLib.Numerics.Quantization.AffineQuantizer.quantize_dequantize (q : AffineQuantizer) {code : } (hlo : q.qmin code) (hhi : code q.qmax) :
              q.quantize (q.dequantize code) = code

              Every in-range integer code survives an executable quantize/dequantize round trip.

              Before saturation, reconstruction differs from the input by at most half a quantization step.

              The half-step bound survives saturation whenever clipping is inactive.

              General numerical-system view #

              @[reducible, inline]

              An integer code in the quantizer's closed storage interval.

              Using Mathlib's interval subtype keeps range reasoning interoperable and avoids a project-local proof-carrying record for the standard qmin ≤ value ≤ qmax invariant.

              Instances For
                @[inline]

                Store the executable quantization result together with its erased range proof.

                Instances For

                  Numerical system whose codes denote their exact affine reconstruction.

                  Instances For
                    @[reducible, inline]

                    An affine-quantized code with an erased proof of its complete denotation.

                    Instances For
                      @[reducible, inline]

                      An affine-quantized code with an erased proof of its reconstructed rational value.

                      Instances For
                        @[simp]

                        Representation in the affine system is equality with exact dequantization.

                        Executable quantization is a quantizer onto its own reconstructed value.

                        Since roundedValue is dequantize after quantize, this is the definitional contract used by generic QuantizerOn automation. The error bound when clipping is inactive is dequantize_quantize_error_le.

                        theorem FloatLib.Numerics.Quantization.AffineQuantizer.numericalSystem_quantize_error_le (q : AffineQuantizer) (x : ) (hlo : q.qmin q.rawCode x) (hhi : q.rawCode x q.qmax) :
                        ∃ (reconstructed : ), q.numericalSystem.Represents (q.quantizeCode x) reconstructed |reconstructed - x| q.scale / 2

                        The numerical-system view inherits the inactive-saturation half-step guarantee.