यह लेख C++ inference software डिज़ाइन करने की अवधारणाएँ और कुछ implementation भाग समझाता है। नीचे का कोड केवल छोटा memory-arena उदाहरण है; इसमें model loader, tokenizer, सभी Transformer layers, sampling या पूर्ण CUDA/Metal backends नहीं हैं। इसे LLM engine या performance comparison न समझें।
C++ क्यों और इसकी सीमाएँ
जहाँ memory layout, CPU instructions या device APIs पर नियंत्रण चाहिए वहाँ C++ उपयोगी है, लेकिन केवल भाषा बदलने से workload अपने आप तेज़ नहीं होता। PyTorch जैसे frameworks अधिकांश गणना optimized C++ और GPU kernels को सौंपते हैं, इसलिए Python calls हमेशा bottleneck नहीं होते। Deployment भी हमेशा एक self-contained binary नहीं होता; GPU runtimes और shared libraries निर्भरता रह सकते हैं।
Inference engine के घटक
graph TD
A["Model and input"] --> B["Tokenizer and model loader"]
B --> C["Tensor operations and graph"]
C --> D["Memory planning and scheduler"]
D --> E["CPU backend"]
D --> FGPU["Optional GPU backend"]
E --> G["Sampling and output"]
FGPU --> G
Matrix operations और memory estimate
Transformers में कई matrix multiplications होती हैं, लेकिन runtime में उनका हिस्सा model, batch, sequence length और hardware पर निर्भर करता है। 7,000,000,000 FP32 weights केवल weights के लिए 28,000,000,000 bytes (28 GB decimal, लगभग 26.1 GiB) लेते हैं। FP16/INT8/INT4 का सैद्धांतिक raw payload FP32 का लगभग आधा, चौथाई और आठवाँ भाग है; quantization metadata, KV cache और workspace अतिरिक्त memory लेते हैं और गुणवत्ता बदल सकते हैं।
C++17 arena उदाहरण
यह उदाहरण C++17 में aligned byte क्षेत्र का पुनः उपयोग करता है। reset केवल cursor पीछे करता है; इसमें रखे objects के destructors नहीं चलाता। Object lifetime अलग से संभालें और allocation का आकार जाँचें।
| |
CPU और GPU backends
CPU SIMD और GPU paths को instruction support, drivers, runtimes, data layout और transfers के साथ design व measure करना पड़ता है। CUDA में cuBLAS और Apple में Metal Performance Shaders matrix APIs देते हैं, लेकिन छोटा code snippet पूरा backend नहीं बनाता। Apple silicon की shared memory से synchronization या resource management की ज़रूरत समाप्त नहीं होती।
वास्तविक implementations देखें
वास्तविक inference implementations के लिए GGML और llama.cpp का source तथा build guide देखें। छोटे tensor operations और tests से शुरू करें; model format, supported operations, numerical precision, memory use और target hardware लिखें। Performance result में model, input, build settings, device और measurement method बताएँ।
