Metal Performance Shaders (MPS) क्या है? MPS kernels और MPSGraph में कैसे चुनें
Metal Performance Shaders (MPS) तैयार kernels की एक लाइब्रेरी है, जिसकी मदद से Apple प्लेटफ़ॉर्म के Metal ऐप image filters और matrix operations जैसे बार-बार होने वाले काम GPU को सौंप सकते हैं। ऐप हर GPU के लिए अलग shader लिखे बिना MPS operation को मौजूदा Metal resources और command buffers से जोड़ सकता है। चुनाव इस बात पर निर्भर करता है कि एक kernel चाहिए, tensor operations का graph चाहिए या अपना shader लिखना है।
Metal Performance Shaders (MPS) क्या है? MPS kernels और MPSGraph में कैसे चुनें
Metal Performance Shaders (MPS) तैयार kernels की एक लाइब्रेरी है, जिसकी मदद से Apple प्लेटफ़ॉर्म के Metal ऐप image filters और matrix operations जैसे बार-बार होने वाले काम GPU को सौंप सकते हैं। ऐप हर GPU के लिए अलग shader लिखे बिना MPS operation को मौजूदा Metal resources और command buffers से जोड़ सकता है। चुनाव इस बात पर निर्भर करता है कि एक kernel चाहिए, tensor operations का graph चाहिए या अपना shader लिखना है।
MPS कौन-सा काम करता है?
Metal graphics और compute के लिए low-level API है, जो ऐप को GPU के काम और data flow पर नियंत्रण देता है। MPS आम operations के kernels देने वाला framework है। Apple image filters और histograms, neural-network operations, matrix factorization और multiplication, तथा ray और geometry के intersection को इसकी सुविधाओं में गिनता है। ये kernels उन Metal objects के साथ काम करते हैं जिन्हें ऐप पहले से इस्तेमाल करता है, जैसे MTLTexture, MTLBuffer और MTLCommandBuffer। Apple का MPS परिचय
Kernel गणना की वह इकाई है जो GPU पर parallel चलती है। उदाहरण के लिए, blur filter आसपास के pixels की मदद से बहुत से pixels की गणना करता है। इसे हाथ से लिखने पर GPU threads का बँटवारा, किनारे के pixels और memory access संभालना पड़ता है। MPS के साथ ऐप input और output textures तथा parameters तैयार करने और kernel call को command buffer में encode करने पर ध्यान दे सकता है। Apple बताता है कि MPS kernels अलग-अलग Metal GPU परिवारों की विशेषताओं के अनुसार tune किए गए हैं, लेकिन इससे यह गारंटी नहीं मिलती कि कोई खास operation हर device पर तेज़ ही होगा। वास्तविक device, data format, image size और आसपास के काम को मापें।
किसी image को blur करने पर क्या होता है?
मान लें कि ऐप को camera frame blur करना है। वह पहले MPSSupportsMTLDevice से जाँचता है कि Metal device MPS का समर्थन करता है या नहीं। यह function framework के device-level support की जानकारी देता है। यह हर kernel, pixel format या precision की पुष्टि नहीं करता, इसलिए संबंधित API documentation और device feature table भी देखें। MPS device-support function
Kernel commands को इस क्रम में रिकॉर्ड करें। MPS kernels का उपयोग
`MTLDevice`, command queue और command buffer तैयार करें और उस device पर MPS support जाँचें।
Pixels वाली input texture और परिणाम के लिए output texture तैयार करें। पुष्टि करें कि दोनों के dimensions और pixel formats चुने हुए filter की शर्तें पूरी करते हैं।
`MPSImageGaussianBlur(device:sigma:)` से blur kernel बनाएँ। `sigma` का मान x और y दिशाओं में blur की तीव्रता तय करता है।
कोई Metal encoder खुला हो तो उसे समाप्त करें, फिर commands दर्ज करने के लिए kernel का `encode` method बुलाएँ। उसी command buffer में आगे GPU काम जोड़ने से पहले नया encoder बनाएँ।
Command buffer submit करें और completion notification मिलने के बाद output texture इस्तेमाल करें। Image देखें और ज़रूरत पर pixel differences तथा precision की तुलना reference implementation से करें।
Filter के नाम में “Gaussian” होने का अर्थ यह न लें कि परिणाम गणितीय रूप से सटीक Gaussian blur है। Apple MPSImageGaussianBlur को तेज़ approximation बताता है, जो आम image-processing कामों में लगभग 10 bits या उससे कम precision की ज़रूरत के लिए है। यदि विश्लेषणात्मक रूप से सटीक Gaussian filter चाहिए, तो उचित weights के साथ MPSImageConvolution पर विचार करें। दृश्य प्रभाव और कड़े error limits वाले analysis में यही अंतर चुनाव बदल सकता है। MPSImageGaussianBlur दस्तावेज़
MPS kernels और MPSGraph में क्या अंतर है?
MPS kernel किसी तैयार operation—जैसे blur, convolution या matrix multiplication—को command buffer में जोड़ने के लिए उपयुक्त है। MPSGraph बड़े computation को graph के रूप में दिखाता है: nodes addition या convolution जैसे operations हैं, जबकि edges बहुआयामी data वाले tensors हैं। Graph बनाने के बाद ऐप inputs और outputs तय करता है, executable compile करता है और input data पर उसे चला सकता है। Apple के अनुसार MPSGraph प्लेटफ़ॉर्म के compute devices, जिनमें GPU, CPU और Neural Engine शामिल हैं, का उपयोग कर सकता है। वास्तविक placement और support graph, device और OS पर निर्भर करते हैं; किसी खास device के इस्तेमाल की गारंटी न मानें। MPSGraph परिचय MPSGraph class और compilation API
एक image blur के लिए MPS image kernel स्वाभाविक विकल्प है। कई जुड़े tensor operations वाले model—जैसे resize, normalization और कई convolutions—में संबंधों को MPSGraph में व्यक्त करके देखें कि graph compilation मदद करता है या नहीं। ज़रूरी operation उपलब्ध न हो या data flow अथवा operation fusion पर सीधा नियंत्रण चाहिए, तो Metal shader लिखें। तब team को thread और memory design, correctness checks और हर device पर profiling की जिम्मेदारी लेनी होगी।
| स्थिति | पहले किस पर विचार करें | क्या जाँचें |
|---|---|---|
| Image filter या matrix operation जैसा तैयार एकल operation | MPS kernel | Pixel formats, parameters, precision और device support |
| Input से output तक कई tensor operations जोड़ने वाला computation | MPSGraph | Operator support, graph compilation cost, वास्तविक execution device और बार-बार चलाने का प्रदर्शन |
| ऐसा विशेष processing जिसे तैयार operations व्यक्त नहीं कर सकते, या मजबूत fusion की ज़रूरत | Custom Metal shader | Implementation और maintenance cost, memory access, correctness और device-specific tuning |
| Metal 4 tensor operations से custom ML kernel बनाना | Metal Performance Primitives (TensorOps) भी देखें | Metal 4 और target GPU support जाँचें। यह MPS kernel library या MPSGraph वाली परत नहीं है |
आखिरी पंक्ति का MPP, MPS का नया नाम नहीं है। Apple की 2026 MPP सामग्री mpp::tensor_ops को Metal shaders के भीतर matrix multiplication जैसे tensor operations जोड़कर custom ML kernels बनाने का low-level तरीका बताती है। यह रास्ता Metal 4 और नए Apple GPU capabilities के लिए है; पुराने devices में वही सुविधाएँ होंगी, ऐसा न मानें। केवल मौजूदा image filter बुलाने वाले ऐप की ज़रूरत अलग है, और उसके लिए MPS अब भी उपयुक्त है। Apple MPP Programming Guide WWDC26: Metal tensors से custom machine-learning operations को optimize करना
MPS अपनाने से पहले क्या जाँचें?
पहले target OS और device पर framework तथा चुने हुए operation की उपलब्धता जाँचें। MPSSupportsMTLDevice शुरुआत है; Apple की feature table और API की availability तथा input-format requirements भी पढ़ें। MPS support का अर्थ यह नहीं कि हर MPS class और pixel format समान शर्तों में समर्थित है।
दूसरे, optimization से पहले स्वीकार्य error तय करें। Input/output formats और reduced-precision options values और speed दोनों बदल सकते हैं। देखने में अच्छा blur उस काम के लिए पर्याप्त नहीं हो सकता जिसे pixel-level reproducibility या analytical precision चाहिए; वहाँ दूसरा kernel लग सकता है।
तीसरे, केवल kernel runtime नहीं, ऐप का पूरा path मापें। Textures बनाना या copy करना, temporary buffers, encoder switches और CPU-GPU waits की लागत गणना से अधिक हो सकती है। Apple की tuning सलाह resources reuse करने और GPU के काम करते समय अगले commands queue करने की है। MPS tuning guidance
अंत में concurrent command encoding जाँचें। MPS kernel object एक Metal device से जुड़ा होता है। कई threads को एक ही kernel object को एक साथ बदलना या एक ही command buffer में एक साथ लिखना नहीं चाहिए। यदि कई threads काम encode करते हैं, तो kernel objects और command buffers का ownership स्पष्ट रखें और completion तक resources जीवित रखें। Target devices पर वास्तविक models या frame sizes की तुलना करके तय करें कि MPS, MPSGraph या custom Metal में से किस path को maintain करना है।
स्रोत
[Apple, Metal Performance Shaders](https://developer.apple.com/documentation/metalperformanceshaders)
[Apple, The MPSKernel Class](https://developer.apple.com/documentation/metalperformanceshaders/the-mpskernel-class)
[Apple, MPSSupportsMTLDevice(_:)](https://developer.apple.com/documentation/metalperformanceshaders/mpssupportsmtldevice%28_%3A%29)
[Apple, MPSImageGaussianBlur](https://developer.apple.com/documentation/metalperformanceshaders/mpsimagegaussianblur)
[Apple, Metal Performance Shaders Graph](https://developer.apple.com/documentation/metalperformanceshadersgraph)
[Apple, MPSGraph](https://developer.apple.com/documentation/metalperformanceshadersgraph/mpsgraph)
[Apple, Tuning Hints](https://developer.apple.com/documentation/metalperformanceshaders/tuning-hints)
[Apple, Metal Performance Primitives Programming Guide](https://developer.apple.com/download/files/Metal-Performance-Primitives-Programming-Guide.pdf)
[Apple, WWDC26: Optimize custom machine learning operations with Metal tensors](https://developer.apple.com/videos/play/wwdc2026/330/)
संदर्भ
- Apple Metal Performance Shaders
- Apple The MPSKernel Class
- Apple MPSSupportsMTLDevice
- Apple MPSImageGaussianBlur
- Apple Metal Performance Shaders Graph
- Apple MPSGraph
- Apple Tuning Hints
- Apple Metal Performance Primitives Programming Guide
- Apple WWDC26 Optimize custom machine learning operations with Metal tensors