RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Custom Quantization and Kernels
  6. /Ch. 1
Custom Quantization and Kernels

01. Quantization Theory

Chapter 1 of 18 · 15 min
KEY INSIGHT

Quantization is fundamentally a lossy compression problem where scale and zero-point parameters control the mapping between continuous float values and discrete integer representations.

Quantization transforms continuous floating-point values into discrete representations that require less memory and compute. In local AI inference, this process determines how model weights and activations are stored and processed, directly impacting inference speed, memory consumption, and model accuracy.

The fundamental challenge of quantization lies in mapping a range of floating-point values to a limited set of discrete values. Consider a weight matrix containing values ranging from -2.5 to 3.8. Quantization must compress this continuous distribution into, for example, 256 unique values (for int8) while preserving the semantic meaning of the original numbers.

Quantization operates through two primary parameters: scale and zero-point. The scale factor (typically a float32 value) maps the quantization domain to real values, while the zero-point (zero-point offset) ensures that zero maps exactly, which matters for representing padding and bias terms correctly.

class Quantizer:
    def __init__(self, bits=8, scheme="asymmetric"):
        self.bits = bits
        self.scheme = scheme
        self.scale = None
        self.zero_point = None
    
    def quantize(self, tensor):
        qmin, qmax = 0, (1 << self.bits) - 1
        
        if self.scheme == "asymmetric":
            min_val = tensor.min()
            max_val = tensor.max()
            self.scale = (max_val - min_val) / (qmax - qmin)
            self.zero_point = torch.round(qmin - min_val / self.scale)
        else:  # symmetric
            max_val = tensor.abs().max()
            self.scale = max_val / (qmax // 2)
            self.zero_point = 0
        
        quantized = torch.clamp(
            torch.round(tensor / self.scale) + self.zero_point,
            qmin, qmax
        ).to(torch.int8)
        
        return quantized

Two quantization approachs dominate practical implementations. Symmetric quantization uses the same scale for positive and negative ranges, simplifying computation but wasting one quantization level. Asymmetric quantization separately handles the positive and negative ranges, preserving precision near zero while using scale factors that match the data distribution more accurately.

The quantization error, the difference between original and dequantized values, compounds through neural network layers. A layer operating on already-quantized inputs further propagates this error. Understanding this accumulation effect explains why careful calibration and per-tensor versus per-channel quantization strategies diverge in practice.

EXERCISE

Implement both symmetric and asymmetric quantization for a random float32 tensor and measure the mean squared error for each approach at different bit widths (4, 6, 8 bits).

← Overview
Custom Quantization and Kernels
Chapter 2 →
Weight Quantization