في السعي الدائم نحو نشر نماذج الذكاء الاصطناعي بكفاءة، أصبح تكميم ما بعد التدريب (PTQ) تقنية قياسية لضغط النماذج. ومع ذلك، عندما يقصر PTQ في الحفاظ على الدقة في الدقة المنخفضة، فإن التدريب المدرك للكمية (QAT) والتقطير المدرك للكمية (QAD) يقدمان بدائل واعدة. تتعمق مشاركة المدونة هذه في QAT و QAD، وتوضح كيف يمكن الاستفادة منهما مع مُحسِّن نموذج TensorRT لإعداد النماذج للاستدلال منخفض الدقة، مع التركيز بشكل خاص على مهام سير العمل المتوافقة مع Hugging Face و PyTorch. كما يسلط الضوء على نشر هذه النماذج المحسنة باستخدام TensorRT-LLM.
فهم التدريب المدرك للكمية (QAT)
QAT هي تقنية يتم فيها تدريب النموذج لاستيعاب العمليات الحسابية منخفضة الدقة بعد التدريب المسبق. على عكس PTQ، الذي يكمم نموذجًا مدربًا بالكامل، يحاكي QAT التكميم أثناء عملية التدريب، مما يسمح للنموذج بتكييف أوزانه وتنشيطاته مع قيود أنواع البيانات منخفضة الدقة.
- كيف يعمل: يستخدم QAT “التكميم الوهمي” أثناء التمرير الأمامي، ويمثل قيم الدقة المنخفضة داخل نوع بيانات أعلى باستخدام عوامل تشغيل التكميم / إلغاء التكميم. يتيح ذلك التدريب على الأجهزة القياسية دون دعم أصلي للدقة المنخفضة.
- الهدف: الهدف الأساسي هو الاستدلال الكمي عالي الدقة، وتمييزه عن التدريب الكمي الذي يركز على كفاءة التدريب.
- التنفيذ: أثناء QAT، تتعرض أخطاء التقريب والاقتطاع لوظيفة الخسارة، مما يسمح للنموذج بالتعافي والتكيف.
- دعم مُحسِّن النموذج: يدعم مُحسِّن النموذج تنسيقات التكميم مثل FP8 و NVFP4 و MXFP4 و INT8 و INT4. يتطلب تطبيق QAT مع مُحسِّن النموذج قدرًا صغيرًا فقط من التعليمات البرمجية الإضافية عن طريق إضافة حلقة تدريب إلى سير عمل PTQ. عادةً ما يكون الضبط الدقيق للنموذج لحوالي 10٪ من فترات التدريب الأولية، أو حتى أقل بالنسبة لـ LLMs، كافيًا لاستعادة جودة النموذج.
استكشاف التقطير المدرك للكمية (QAD)
يهدف QAD إلى استعادة الدقة بعد التكميم، مع إجراء تقطير المعرفة في نفس الوقت. الفرق الرئيسي عن تقطير المعرفة التقليدي هو أن نموذج “الطالب” يستخدم التكميم الوهمي أثناء التمرير الأمامي.
- إعداد المعلم والطالب: يستخدم QAD نموذج المعلم (النموذج الأصلي عالي الدقة) لتوجيه نموذج الطالب (النموذج الكمي).
- خسارة التقطير: تُستخدم وظيفة خسارة التقطير لقياس الانحراف بين مخرجات الطالب الكمي ومخرجات المعلم كامل الدقة.
- العملية: يتم تكميم حسابات الطالب بشكل وهمي أثناء التقطير، مما يعرض حالات عدم التطابق الناتجة عن التكميم لخسارة التقطير. يتيح ذلك لأوزان وتنشيطات الدقة المنخفضة التكيف مع سلوك المعلم.
- الفوائد: يؤدي هذا إلى استعادة دقة أعلى مقارنة بـ PTQ أو QAT وحدهما، دون التأثير على أداء الاستدلال (حيث يظل البناء والدقة كما هما).
- واجهات برمجة تطبيقات مُحسِّن النموذج: يوفر مُحسِّن نموذج TensorRT واجهات برمجة تطبيقات تجريبية لـ QAD. تتضمن العملية تطبيق وصفة تكميم على نموذج الطالب، وتحديد تكوين تقطير (نموذج المعلم، وحجج التدريب، وخسارة التقطير)، ثم تنفيذ عملية التقطير.
التأثير الحقيقي والمعايير
تختلف فعالية QAT و QAD عبر النماذج. تحتفظ بعض النماذج بدقة عالية (> 99.5٪) مع PTQ وحده. ومع ذلك، فقد أظهرت نماذج مثل Llama Nemotron Super مكاسب كبيرة في الدقة من QAD (4-22٪ عبر المعايير).
- البيانات واعتماد المعلمات الفائقة: يعتمد نجاح QAT و QAD بشكل كبير على جودة بيانات التدريب واختيار المعلمات الفائقة وبنية النموذج.
- NVFP4 مقابل MXFP4: بالنسبة للتكميم 4 بت، يمكن أن يتفوق NVFP4 على MXFP4 نظرًا لعوامل القياس الدقيقة، خاصة في المهام التي تتضمن تفاصيل دقيقة، مثل الإجابة على الأسئلة المرئية على الرسوم البيانية والرسومات المعقدة. يترجم هذا إلى محاذاة أكثر ثباتًا بين المكونات المرئية والنصية للنموذج، مما يقلل من أخطاء التقريب / الاقتطاع.
- أمثلة الإجابة على الأسئلة المرئية: تكون الاختلافات بين NVFP4 و MXFP4 أكثر وضوحًا في مجموعات البيانات مثل InfoVQA، والتي تتطلب اكتشاف تفاصيل صغيرة، مقارنة بـ DocVQA، التي تركز على المستندات المنظمة حيث يكون العثور على الحقل الصحيح أمرًا بالغ الأهمية.
خاتمة
يعزز QAT و QAD PTQ من خلال تمكين النماذج من التكيف مع البيئات منخفضة الدقة، واستعادة الدقة حيث يفشل PTQ. تمكن هذه التقنيات المطورين من تسخير كفاءة تنفيذ FP4 مع قوة عالية الدقة تقريبًا. يوفر مُحسِّن نموذج TensorRT الوصول إلى مهام سير العمل هذه من خلال واجهات برمجة تطبيقات PyTorch و Hugging Face المألوفة، مما يبسط التجريب مع تنسيقات مثل NVFP4 و MXFP4. سواء كان المشروع يتطلب سرعة PTQ أو مرونة QAT أو مكاسب دقة QAD، فهناك مجموعة أدوات شاملة متاحة لضغط النماذج الذكاء الاصطناعي وضبطها بدقة ونشرها على وحدات معالجة الرسومات NVIDIA، مما يؤدي إلى عمليات نشر أسرع وأصغر وأكثر دقة للذكاء الاصطناعي على نطاق واسع.
المصدر: NVIDIA Developer Blog
In the relentless pursuit of efficient AI model deployment, post-training quantization (PTQ) has become a standard technique for compressing models. However, when PTQ falls short in maintaining accuracy at lower precisions, Quantization Aware Training (QAT) and Quantization Aware Distillation (QAD) offer promising alternatives. This blog post dives into QAT and QAD, showcasing how they can be leveraged with the TensorRT Model Optimizer to prepare models for low-precision inference, specifically focusing on workflows compatible with Hugging Face and PyTorch. It also highlights the deployment of these optimized models using TensorRT-LLM.
Understanding Quantization Aware Training (QAT)
QAT is a technique where the model is trained to accommodate low-precision arithmetic after pre-training. Unlike PTQ, which quantizes a fully trained model, QAT simulates quantization during the training process, allowing the model to adapt its weights and activations to the limitations of lower precision data types.
- How it works: QAT uses “fake quantization” during the forward pass, representing lower precision values within a higher data type using quantize/dequantize operators. This allows training on standard hardware without native low-precision support.
- The Goal: The primary goal is high accuracy quantized inference, differentiating it from quantized training focused on training efficiency.
- Implementation: During QAT, rounding and clipping errors are exposed to the loss function, allowing the model to recover and adapt.
- Model Optimizer Support: Model Optimizer supports quantization formats such as FP8, NVFP4, MXFP4, INT8, and INT4. Applying QAT with Model Optimizer requires only a small amount of extra code by adding a training loop to the PTQ workflow. Fine-tuning the model for about 10% of the initial training epochs, or even less for LLMs, is generally sufficient to restore model quality.
Exploring Quantization Aware Distillation (QAD)
QAD aims to recover accuracy post-quantization, while simultaneously performing knowledge distillation. The key difference from traditional knowledge distillation is that the “student” model utilizes fake quantization during its forward pass.
- Teacher-Student Setup: QAD uses a teacher model (the original higher precision model) to guide a student model (the quantized model).
- Distillation Loss: A distillation loss function is used to measure the deviation between the quantized student’s outputs and the full-precision teacher’s outputs.
- The Process: The student’s computations are fake quantized during the distillation, exposing quantization-induced mismatches to the distillation loss. This allows the low-precision weights and activations to adapt to the teacher’s behavior.
- Benefits: This leads to higher accuracy recovery compared to PTQ or QAT alone, without impacting inference performance (as the architecture and precision remain the same).
- Model Optimizer APIs: TensorRT Model Optimizer offers experimental APIs for QAD. The process involves applying a quantization recipe to the student model, defining a distillation configuration (teacher model, training arguments, and distillation loss), and then executing the distillation process.
Real-World Impact and Benchmarking
The effectiveness of QAT and QAD varies across models. Some models retain high accuracy (>99.5%) with PTQ alone. However, models like Llama Nemotron Super have shown significant accuracy gains from QAD (4-22% across benchmarks).
- Data and Hyperparameter Dependency: The success of QAT and QAD depends heavily on training data quality, hyperparameter selection, and model architecture.
- NVFP4 vs MXFP4: For 4-bit quantization, NVFP4 can outperform MXFP4 due to finer-grained scaling factors, particularly in tasks involving fine details, like visual question answering on complex charts and graphics. This translates to a steadier alignment between the visual and text components of the model, reducing rounding/clipping errors.
- Visual Question Answering Examples: Differences between NVFP4 and MXFP4 are more prominent in datasets like InfoVQA, which requires detecting small details, compared to DocVQA, which focuses on structured documents where finding the correct field is paramount.
Conclusion
QAT and QAD enhance PTQ by enabling models to adapt to low-precision environments, recovering accuracy where PTQ falls short. These techniques enable developers to harness the efficiency of FP4 execution with near high-precision robustness. The TensorRT Model Optimizer provides access to these workflows through familiar PyTorch and Hugging Face APIs, simplifying experimentation with formats like NVFP4 and MXFP4. Whether the project requires the speed of PTQ, the resilience of QAT, or the accuracy gains of QAD, there is a comprehensive toolkit available to compress, fine-tune, and deploy AI models on NVIDIA GPUs, leading to faster, smaller, and more accurate AI deployments at scale.
Source: NVIDIA Developer Blog
جاري تحميل التعليقات...