Quantization वास्तव में क्या है?
Quantization एक important optimization technique है, जिसका use Large Language Models (LLMs) और deep learning models में किया जाता है। इसमें high-precision numerical data जैसे FP32 (32-bit floating point) और FP16 (16-bit floating point) को convert करके lower-precision formats जैसे INT8 या INT4 में बदला जाता है।
High-precision formats models को better accuracy देते हैं क्योंकि उनमें ज्यादा bits होती हैं। लेकिन downside यह है कि computation heavy हो जाती है और inference slow पड़ता है। Quantization इसी problem को solve करता है, जहाँ थोड़ी-सी accuracy sacrifice करके model को ज्यादा fast और efficient बनाया जाता है।
Quantization को आप एक तरह का smart compression समझ सकते हैं, जहाँ data का size कम होता है लेकिन model का behavior लगभग वैसा ही बना रहता है।
Activation Values क्या होती हैं?
Neural network में हर artificial neuron को जो numeric input मिलता है, उसे activation value कहते हैं। यह value आमतौर पर 0 और 1 के बीच होती है (हालाँकि architecture पर depend करता है)। Quantization के दौरान weights के साथ-साथ इन activation values को भी lower-precision में convert किया जा सकता है।
आज के समय में 8-bit quantization सबसे common है, लेकिन research में 4-bit (INT4) और उससे भी कम bit-width पर successful results देखे जा चुके हैं।
LLMs में Quantization की ज़रूरत क्यों पड़ती है?
जब कोई LLM FP32 में run होता है, तो उसकी computational requirements बहुत ज्यादा होती हैं। इसका असर inference speed, cost और power consumption पर पड़ता है।
Quantization का primary goal होता है:
- Computation cost को reduce करना
- Inference speed को improve करना
- Model को low-resource devices पर usable बनाना
Lower-precision weights को process करने में hardware को कम operations perform करनी पड़ती हैं। इसी वजह से model हर user query को ज्यादा तेज़ी से handle कर पाता है।
Quantization implement क्यों किया जाता है?
1. Faster Inference
Floating-point operations को integer operations में convert करने से calculations काफी तेज़ हो जाती हैं। इससे model latency कम होती है और real-time applications के लिए performance improve होती है।
यह trade-off (speed vs accuracy) खासतौर पर mobile CPUs और edge devices के लिए बहुत important होता है।
2. Better Efficiency
हर device heavy computation handle नहीं कर सकता। Quantization machine learning models को lightweight बनाता है, जिससे वे laptops, tablets और smartphones जैसे limited-resource devices पर भी smoothly run कर सकते हैं।
3. Lower Power Consumption
Quantized models को run करने में कम computation चाहिए, इसलिए energy usage भी कम होती है। Battery-powered devices के लिए यह एक major advantage है।
4. Platform Compatibility
Quantization integer-based operations को enable करता है, जिससे models उन platforms पर भी run हो पाते हैं जहाँ floating-point support limited या unavailable होता है। इससे older hardware और consumer-grade GPUs पर भी ML models accessible बनते हैं।
Quantization कैसे काम करता है?
FP32 में possible values का range लगभग -3.4 × 1038 से +3.4 × 1038 तक होता है, यानी करीब 4 billion possible values।
वहीं दूसरी तरफ INT8 में सिर्फ 256 possible values होती हैं, जो आमतौर पर -128 से 127 तक रहती हैं।
Values की range छोटी होने की वजह से matrix multiplication जैसी operations बहुत तेज़ हो जाती हैं, जो deep learning models का core हिस्सा होती हैं।
Quantization process में सबसे पहले यह तय किया जाता है कि FP32 values को INT8 space में कैसे project किया जाए। इसके लिए अलग-अलग algorithms मौजूद हैं।
मुख्य Quantization Methods
- Absolute Max Quantization
- Affine Quantization
Absolute Max Quantization
इस method में floating-point values को उनके absolute maximum के आधार पर scale किया जाता है।
Steps simplified रूप में:
- Tensor का absolute maximum value निकालना
- INT8 range (-127 से 127) के अनुसार scaling factor calculate करना
- Original values को scale और round करना
उदाहरण के लिए, अगर vector है:
[1.6, -0.7, -3.4, 1.7, -2.9, 0.5, 2.3, 6.2]
इसका absolute maximum = 6.2
Scaling factor ≈ 127 / 6.2
Scaled और rounded values से INT8 representation मिलती है। Rounding की वजह से थोड़ी precision loss होती है, जिसे quantization error कहा जाता है।
Affine Quantization
Affine quantization में floating-point range को explicitly define किया जाता है और zero को represent करने के लिए एक zero-point use किया जाता है।
xq = round((1 / S) × x + Z)
जहाँ:
- xq → Quantized INT8 value
- x → Original FP32 value
- S → Scaling factor
- Z → Zero-point (INT8 में zero का equivalent)
Accuracy बनाए रखने के लिए outliers को handle करना जरूरी होता है। इसके लिए weights को छोटे blocks (जैसे 64 या 128 values) में divide करके quantize किया जाता है।
Quantization के प्रकार
Post-Training Quantization (PTQ)
PTQ में quantization training के बाद apply की जाती है। Existing trained model को directly lower-precision format में convert कर दिया जाता है।
Advantages:
- Fast process
- Extra training data की जरूरत नहीं
- Cost-effective
Limitation यह है कि accuracy में कुछ degradation हो सकता है, खासकर large models के case में।
Quantization-Aware Training (QAT)
QAT में quantization को training या fine-tuning stage के दौरान simulate किया जाता है।
Iska फायदा:
- Higher accuracy
- Quantization errors के लिए model पहले से adapt हो जाता है
लेकिन यह approach expensive होती है और ज्यादा computation power व training data मांगती है।
Dynamic vs Static Quantization
Dynamic quantization में activation ranges runtime पर calculate होती हैं, जिससे accuracy बेहतर मिलती है।
Static quantization में clipping range पहले से fixed होती है और calibration data की जरूरत पड़ती है। यह approach ज्यादा common है लेकिन setup cost बढ़ा सकती है।
Risks और Limitations
Lower Precision का Risk
Quantization के दौरान accuracy loss inevitable होता है। बहुत बड़े LLMs में यह error accumulate होकर noticeable impact डाल सकता है।
Cost Considerations
QAT highly expensive हो सकता है, जबकि PTQ ज्यादा cost-effective है। Choice इस बात पर depend करती है कि speed, cost और accuracy में से किसे ज्यादा priority दी जा रही है।
Conclusion: Quantization एक powerful optimization technique है, लेकिन इसे blindly apply करने के बजाय model size, use-case और hardware constraints को ध्यान में रखकर implement करना चाहिए।







