AI युग के लिए GPU सर्वाइवल टूलकिट
(journal.hexmos.com)- AI development अब सिर्फ CPU-स्टाइल sequential execution से पूरा नहीं चल सकता; training और inference performance को सही तरह संभालने के लिए GPU के बड़े पैमाने वाले parallel processing model को समझना ज़रूरी है
- Consumer-grade CPU में आम तौर पर 2~16 cores होते हैं और वे single-thread व conditional-branching tasks में मजबूत होते हैं, जबकि GPU हजारों छोटे cores के साथ matrix operations, image processing और deep learning के लिए बेहतर होते हैं
- AWS P3/P4, P5/Inf1, G4 और Amazon SageMaker जैसे GPU execution environments देता है; p3.2xlarge करीब $3.06 प्रति घंटा, p5.48xlarge $98.32, और g4dn.xlarge $0.526 के स्तर पर है
- NVIDIA CUDA developers को GPU memory allocation, data copying, kernel execution और compilation तक की parallel execution flow को सीधे संभालने देता है
- Array addition, Mandelbrot generation और cats-vs-dogs classification CNN examples दिखाते हैं कि sequential loops को GPU threads में कैसे बांटा जाता है; Mandelbrot में समय CPU पर 4.07 सेकंड से घटकर GPU पर 0.0046 सेकंड हो गया
सिर्फ CPU knowledge क्यों पर्याप्त नहीं है
- कई developers ने learning और problem solving को CPU-centric approach से सीखा है, लेकिन CPU मूल रूप से sequential architecture पर आधारित होकर काम करता है
- पारंपरिक CPU instructions को linearly execute करता है और कुछ शक्तिशाली cores को single-thread performance के लिए optimize करता है
- जब कई tasks को एक साथ process करना होता है, तो sequential execution की वजह से हर task को बारी-बारी संभालने की लागत बढ़ जाती है
- Multithreading से performance बढ़ाई जा सकती है, लेकिन CPU की basic design philosophy अब भी sequential execution के करीब है
AI models और parallel processing
- Transformer जैसे modern AI architectures training performance बढ़ाने के लिए parallel processing का इस्तेमाल करते हैं
- RNN sequentially काम करते हैं, लेकिन GPT जैसे Transformer कई words को एक साथ process कर सकते हैं, जिससे training efficiency और model capability बढ़ती है
- Parallel training बड़े models को संभव बनाती है, और बड़े models बेहतर output देने की बुनियाद बन सकते हैं
- Parallelism natural language processing के अलावा image recognition पर भी लागू होता है
- AlexNet image के अलग-अलग हिस्सों को एक साथ process कर patterns identify करने का उदाहरण है
- Single-thread performance-centric design के कारण CPU complex AI models के लिए जरूरी बड़े पैमाने की parallel calculations को efficiently distribute और execute करने में कठिनाई महसूस करता है
GPU bottleneck कैसे कम करता है
- GPU को CPU के बड़े और शक्तिशाली cores की जगह बहुत सारे छोटे और specialized cores इस्तेमाल करने वाली structure के रूप में design किया गया है
- Graphics rendering और complex mathematical calculations जैसे workloads में, जहां एक ही तरह के operations बड़े पैमाने पर repeat होते हैं, GPU की parallelism साफ दिखती है
- TensorFlow जैसे deep learning frameworks GPU performance का इस्तेमाल करके model training और inference को accelerate करने के लिए optimized होते हैं
- Neural network training में बहुत सारे matrix operations शामिल होते हैं, और GPU अपनी बड़ी core count के कारण इन operations को parallelize करने में मजबूत होता है
CPU और GPU की roles में अंतर
-
CPU
- CPU को sequential processing पर focus करके design किया गया है, इसलिए यह एक instruction flow को linearly execute करने वाले tasks में मजबूत होता है
- General-purpose computing, system tasks और conditional branching वाले complex algorithms को process करने के लिए उपयुक्त है
- Consumer-grade CPU में आम तौर पर 2~16 cores की range में अपेक्षाकृत कम cores होते हैं
- हर core अपना instruction set independently process कर सकता है
-
GPU
- GPU को parallel architecture के रूप में design किया गया है, इसलिए यह एक साथ बहुत सारे sub-tasks process करने में efficient है
- Graphics rendering, complex mathematical calculations और parallelizable algorithms execute करने में फायदेमंद है
- Task को छोटे parallel units में बांटकर कई operations को एक साथ process करता है
- GPU cores अक्सर हजारों की संख्या में होते हैं और streaming multiprocessors(SMs) या इसी तरह की structures से बने होते हैं
- Image/video processing, deep learning और scientific simulation जैसे बहुत सारा data एक साथ संभालने वाले tasks के लिए उपयुक्त है
AWS पर चुने जा सकने वाले GPU environments
- AWS machine learning जैसे tasks के लिए कई GPU instances देता है
-
General-purpose GPU instances
-
Inference-optimized instances
- Inference वह process है जिसमें trained AI model में real-time data डालकर predictions किए जाते हैं या tasks हल किए जाते हैं
- P5 और Inf1 low latency और cost efficiency महत्वपूर्ण होने वाली machine learning inference के लिए tuned हैं
- p5.48xlarge $98.32 प्रति घंटा है और 80GB memory वाले 8 NVIDIA H100 GPUs, कुल 640GB video memory देता है
-
Graphics-optimized instances
- G4 instances graphics-intensive tasks को संभालने के लिए design किए गए हैं
- Video game developers G4 instances से games के लिए 3D graphics render कर सकते हैं
- g4dn.xlarge $0.526 प्रति घंटा है और 16GB memory वाले 1 NVIDIA T4 GPU का इस्तेमाल करता है
-
Managed machine learning service
- Amazon SageMaker machine learning के लिए managed service है और P3, P4, P5 जैसे GPU-based instances तक access देता है
- SageMaker उन organizations के लिए उपयुक्त है जो underlying infrastructure को सीधे manage किए बिना machine learning शुरू करना चाहती हैं
- Amazon SageMaker pricing documentation अलग से उपलब्ध है
NVIDIA CUDA का basic usage
- CUDA NVIDIA द्वारा विकसित parallel computing platform और programming model है, जो GPU accelerators का इस्तेमाल करके applications को तेजी से चलाने देता है
- Example CUDA development flow दिखाता है: GPU memory allocation, data copy, kernel execution और result retrieval
-
Installation flow
- CUDA से base installer और driver installer download करें
- Home folder की
.bashrcमें नीचे दिए environment variables जोड़ेंexport PATH="/usr/local/cuda-12.3/bin:$PATH"export LD_LIBRARY_PATH="/usr/local/cuda-12.3/lib64:$LD_LIBRARY_PATH"
- नीचे दिए commands चलाएं
sudo apt-get install cuda-toolkitsudo apt-get install nvidia-gds
- बदलाव लागू करने के लिए system reboot करें
-
उपयोगी check commands
lspci | grep VGA: system के GPUs को identify और list करता हैnvidia-smi: NVIDIA GPU की utilization, temperature, memory usage आदि detailed information देता हैsudo lshw -C display: graphics card जैसे display controller information देता हैinxi -G: GPU और display सहित graphics subsystem की जानकारी दिखाता हैsudo hwinfo --gfxcard: system graphics card की detailed information जांचने के लिए इस्तेमाल होता है
CUDA से array addition parallelize करना
- Array addition GPU parallelization समझाने के लिए उपयुक्त problem है
- Example arrays
A = [1,2,3,4,5,6],B = [7,8,9,10,11,12]हैं, और resultC = [8,10,12,14,16,18]है - CPU approach array elements को एक-एक करके traverse करते हुए addition करता है
- Data बढ़ने पर sequential approach का समय बढ़ता है, और GPU
1+7,2+8,3+9जैसे operations को एक साथ कर सकता है - CUDA example
.cukernel file का इस्तेमाल करता है__global__GPU पर call होने वाले kernel function को दर्शाता हैvectorAddतीन integer pointersa,b,cलेता है और vector addition करता हैthreadIdx.xcurrent thread का index लाता है- हर thread संबंधित element का sum
c[i]में store करता है
mainfunction GPU memory allocation, data copy, kernel execution और result copy के क्रम में चलता हैcudaMallocसेcudaA,cudaB,cudaCmemory GPU पर allocate की जाती हैcudaMemcpyसेa,bको host से GPU पर copy किया जाता हैvectorAdd <<<1, sizeof(a) / sizeof(a[0])>>>से kernel execute किया जाता है- Result vector
cudaCको GPU से host पर copy किया जाता है
- Compile और run करने के लिए
nvcccommand का इस्तेमाल होता है - पूरा code उपलब्ध है
Python image generation में GPU का इस्तेमाल
- Mandelbrot set generation एक ऐसा task है जो किसी खास equation में numbers के behavior के आधार पर complex visual patterns बनाता है और resource-intensive है
- CPU-based Python example हर pixel पर loop चलाकर Mandelbrot value calculate करता है, और 1024×1536 image generation में 4.07 सेकंड लगते हैं
- GPU-accelerated version Numba library का इस्तेमाल करता है
@jitdecorator Python code को machine code में बदलने वाली Just-In-Time compilation करता हैcuda.jitसेmandel_gpuबनाया जाता है, औरdevice=Truespecify करके उसे GPU पर execute होने दिया जाता हैmandel_kernelCUDA GPU पर execute होता है और Mandelbrot generation task को GPU threads में बांटता है
create_fractal_gpuGPU memory allocation, thread/block setup, GPU kernel execution, synchronization और result copy करता हैthreadsperblock = (16, 16)का इस्तेमाल करता हैcuda.synchronize()से GPU task पूरा होने का इंतजार करता हैd_image.copy_to_host(image)से result CPU side पर copy करता है
- GPU execution time 0.0046 सेकंड है, जो CPU-based code से कहीं ज्यादा तेज है
- पूरा code उपलब्ध है
GPU से cats-vs-dogs classification neural network training
- AI में GPU कैसे इस्तेमाल होता है यह दिखाने के लिए cats और dogs में फर्क करने वाला neural network example इस्तेमाल किया गया है
- Prerequisites CUDA और TensorFlow हैं
- TensorFlow को
pip install tensorflow[and-cuda]से install किया जा सकता है - Dataset Kaggle Dogs vs. Cats का इस्तेमाल करता है
- Download के बाद cats और dogs images को training folder के अलग-अलग subfolders में organize करें
- TensorFlow को
- Model convolutional neural network(CNN) का इस्तेमाल करता है
- pandas और numpy data manipulation के लिए इस्तेमाल होते हैं
Sequentialneural network layers को linearly stack करने के लिए इस्तेमाल होता हैConvolution2D,MaxPooling2D,Dense,FlattenCNN component layers हैंImageDataGeneratortraining के दौरान real-time data augmentation में इस्तेमाल होता है
- Training data
ImageDataGeneratorसे load होता है- Training data पर
rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=Trueapply होता है - Input images
(64, 64)size, batch size 32 और binary classification mode पर set होते हैं
- Training data पर
- CNN structure convolution, max pooling, flattening, Dense layer और sigmoid output से बना है
- Model
adamoptimizer,binary_crossentropyloss औरaccuracymetric के साथ compile होता है - Training
epochs=25,validation_steps=2000से execute होती है, औरclassifier.save('trained_model.h5')से.h5file में save होती है - Inference code
trained_model.h5load करके image को(64, 64)में convert करता है, और predicted value 0.5 या उससे ज्यादा हो तोdog, नहीं तोcatoutput करता है - पूरा code उपलब्ध है
GPU usage का दायरा
- AI युग में GPU capabilities को ignore करना मुश्किल है, और developers को GPU की ताकत को बेहतर समझना चाहिए
- Sequential algorithms से parallelized algorithms की ओर बढ़ते हुए, GPU complex calculations को accelerate करने का tool बनता है
- GPU की parallel processing capability AI और machine learning tasks में बड़े datasets और complex neural network architectures को process करने के लिए खास तौर पर फायदेमंद है
- GPU traditional machine learning domain से आगे scientific research, simulation और data-intensive tasks में भी इस्तेमाल होता है
- Parallel processing capability drug discovery, climate modeling और financial simulation जैसे अलग-अलग क्षेत्रों की problems solve करने में इस्तेमाल होती है
1 टिप्पणियां
Hacker News की राय
इस लेख का code गलत है। CUDA kernel बिल्कुल call नहीं किया गया: https://github.com/RijulTP/GPUToolkit/blob/f17fec12e008d0d37...
JIT-compiled code से Mandelbrot set को “compute” करने में लगने वाले समय का 90% असल computation में नहीं, बल्कि function compile करने में जाता है
अगर आप CUDA सही तरह से सीखना चाहते हैं, तो matrix multiplication implementation अच्छा अभ्यास है, और देखने लायक tutorials ये हैं: https://cnugteren.github.io/tutorial/pages/page1.html और https://siboehm.com/articles/22/CUDA-MMM
यह 32-bit floating-point vectors X और Y, और scalar A लेता है, हर X[i] को A से multiply करता है और फिर Y[i] में जोड़ता है: https://developer.nvidia.com/blog/six-ways-saxpy/
दावा है कि “हर developer को यह जानना चाहिए”, लेकिन असल में यह लेख AI में GPU कैसे इस्तेमाल होते हैं इस बारे में ज्यादा लगता है। ज्यादातर developers AI developers नहीं हैं, और वे AI से सीधे interact भी नहीं करते या GPU को सीधे use भी नहीं करते
इसके अलावा GPU के अस्तित्व का मुख्य कारण, यानी 3D graphics, इसमें लगभग cover नहीं किया गया
basic knowledge हो तो manager को बेची जाने वाली “AI” वाली बातों को भी बेहतर समझ सकते हैं
“adjacent fields की जरूरत नहीं” वाला attitude मैंने school में अक्सर देखा था। system administration side में मेरे batchmates कहते थे कि programming जानना जरूरी नहीं, लेकिन scripting की जरूरत पड़ी; software development school में कहते थे कि networks जानना जरूरी नहीं, लेकिन कुछ साल बाद job postings में DevOps खूब दिखने लगा
अगर लेख लगभग 1500 words का है तो पढ़ाई की तरह पढ़ने पर भी करीब 12 minutes लगेंगे, और code examples चला कर देखने में 2 घंटे भी लगें तो यह कोई बड़ा investment नहीं है। बेशक, यह मानना होगा कि वह लेख अच्छा introduction है
curlसे JSON request भेजना नहीं आता। मैं अब भी embedded developer हूं, लेकिन उसके बाद backend, frontend और infra काफी सीखा, और लगता है अगले कुछ सालों में पूरी industry में AI को लेकर कुछ ऐसा ही होने की संभावना काफी हैमुझे लगता है कि AI में Python के dominant होने की वजह यह है कि Python-C relationship, CPU-GPU relationship जैसी है
GPU की performance बहुत अच्छी है, लेकिन direct code करना मुश्किल है, इसलिए लोग PyTorch जैसे high-level API calls से GPU को handle करते हैं
C की performance भी अच्छी है, लेकिन coding मुश्किल है, इसलिए Python को C के ऊपर abstraction layer की तरह इस्तेमाल किया जाता है
यह साफ नहीं है कि लोगों को GPU को इतना गहराई से समझना चाहिए या नहीं। अगर आप AI training या operations के काफी अंदर तक नहीं जा रहे, तो और भी कम; और अगर Moore’s law खत्म हो जाए और multithreading speed improvements का मुख्य तरीका बन जाए, तो parallel programming paradigm के हिसाब से नई languages आने की संभावना काफी है। Mojo उस शुरुआत जैसा लगता है
simple iterative computations से लेकर हर instruction तक, सब कुछ पीछे से intelligently सभी CPU cores को parallel में इस्तेमाल करे, और जो काम संभव हों उन्हें GPU को सौंप दे—ऐसे design के साथ
जिज्ञासा है कि क्या ऐसा प्रयास पहले हो चुका है, या शुरुआत से ही यह possible है या नहीं
“कई tasks मिलने पर CPU resources को इस तरह allocate करता है कि हर task को एक-एक करके process करे” यह explanation बहुत oversimplified है। कभी-कभी तो लगता है काश CPU अब भी इतना simple होता
लेख का programming model पर focus करना ठीक है, लेकिन performance perspective से “CPU instructions sequentially execute करता है” कहना मूल रूप से गलत है। pipelines instructions को parallel execute करती हैं, SIMD भी है, और कई cores एक ही problem को साथ मिलकर handle कर सकते हैं
बड़ा फर्क यह है कि CPU एक thread को efficiently execute करने के लिए control-flow handling पर बहुत silicon और power खर्च करता है, जबकि GPU वही resources ज्यादा compute units पर लगाता है और ढेर सारे threads चलाकर control flow और memory latency को hide करता है
यह कहना कि CPU serial code के लिए अच्छा है और GPU parallel code के लिए अच्छा है, कुछ हद तक सही है, लेकिन यह काफ़ी मोटा approximation है। अगर कुछ सौ watts के समान power budget को मानें, तो CPU में independent tasks को एक-एक करके चलाने वाले लगभग 100 “cores” होते हैं, hyperthreading सहित, और यह branch prediction व pipelining से memory latency छिपाता है
GPU में लगभग 100 “compute units” होते हैं, और हर unit करीब 80 independent tasks को interleave करके चलाता है; दूसरे task की अगली instruction चलाकर memory latency छिपाता है
terminology काफ़ी confusing है, और CPU में 256-bit चौड़ी vector unit, जबकि GPU में 2048-bit चौड़ी vector unit होने की संभावना ज़्यादा है, लेकिन थोड़ा दूर से देखें तो दोनों architectures काफ़ी मिलती-जुलती दिखती हैं
नाम Xeon Chi रख सकते हैं
ज़्यादातर programming languages CPU की तरह sequential processing के हिसाब से design की गई हैं, जबकि Erlang/Elixir को GPU की तरह parallelism के लिए design किया गया मानें, तो सोचता हूँ कि क्या Nx / Axon उभरेंगे: https://github.com/elixir-nx/
buying guide चाहिए। जानना चाहता हूँ कि कम-से-कम कितना खर्च करना होगा, और budget range के हिसाब से best choice क्या है। समस्या यह है कि यह जानकारी कभी-कभी बदलती रहती है, और पता नहीं कोई लगातार updated resource है या नहीं
https://colab.google/
https://www.kaggle.com/docs/notebooks
https://www.paperspace.com/gradient/free-gpu
क्या हम फिर से “हर developer को जानना चाहिए” किस्म के clickbait लेखों पर लौट आए हैं
मुझे complexity को सीधे address करना पसंद है, और computer hardware जैसे क्षेत्रों में quantitative methods और qualitative details दोनों की कुछ समझ है, इसलिए किसी field की details ठीक से समझाने वाले लेख अच्छे लगते हैं
मसलन “What every programmer should know about memory” क्या हर programmer को जानना ही चाहिए, यह अलग बात है, लेकिन अच्छे programmer को computer असल में कैसे काम करता है इसका अंदाज़ा होना चाहिए। उस लेख से लेने लायक मुख्य बात locality है, जो तेज़, follow करने में आसान और problem-fit अच्छे code में अक्सर स्वाभाविक रूप से आ जाती है
अच्छा लेख है, लेकिन AWS P5 instances P4d, P4de के साथ inference के बजाय साफ़ तौर पर training के लिए ज्यादा tuned हैं। inference के लिए ज्यादा उपयुक्त instance types G4dn और G5 हैं, जो क्रमशः T4 और A10G GPU इस्तेमाल करते हैं
GPU programming में मैं लगभग नया हूँ, लेकिन यह लेख पढ़ने में मज़ेदार लगा। यह देखकर हैरानी हुई कि technology इतनी आगे बढ़ गई है कि simple “कुत्ता या बिल्ली” neural network को इतनी आसानी से train किया जा सकता है