Metal Performance Shaders (MPS) کیا ہے؟ MPS kernels اور MPSGraph میں انتخاب کیسے کریں
Metal Performance Shaders (MPS) تیار kernels کی لائبریری ہے جس کی مدد سے Apple پلیٹ فارمز پر Metal apps، image filters اور matrix operations جیسے بار بار ہونے والے کام GPU کے سپرد کر سکتے ہیں۔ ہر GPU کے لیے الگ shader لکھے بغیر app کسی 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 apps، image filters اور matrix operations جیسے بار بار ہونے والے کام GPU کے سپرد کر سکتے ہیں۔ ہر GPU کے لیے الگ shader لکھے بغیر app کسی MPS operation کو موجودہ Metal resources اور command buffers سے جوڑ سکتی ہے۔ درست انتخاب اس بات پر منحصر ہے کہ آپ کو ایک kernel، tensor operations کا graph، یا اپنا shader درکار ہے۔
MPS کون سا کام انجام دیتا ہے؟
Metal گرافکس اور compute کے لیے کم سطح کا API ہے جو app کو GPU کے کام اور data flow پر اختیار دیتا ہے۔ MPS عام operations کے kernels فراہم کرنے والا framework ہے۔ Apple کی فہرست میں image filters اور histograms، neural network operations، matrix factorization اور multiplication، اور ray اور geometry کے intersection شامل ہیں۔ یہ kernels ان objects کے ساتھ کام کرتے ہیں جو Metal app پہلے سے استعمال کرتی ہے، مثلاً MTLTexture، MTLBuffer اور MTLCommandBuffer۔ Apple کا MPS تعارف
Kernel حساب کی وہ اکائی ہے جو GPU پر متوازی طور پر چلتی ہے۔ مثال کے طور پر blur filter قریبی pixels کو استعمال کرتے ہوئے بہت سے pixels کا حساب کرتا ہے۔ خود implementation لکھنے پر GPU threads کی ترتیب، کنارے کے pixels اور memory access سنبھالنا پڑتا ہے۔ MPS کے ساتھ app input اور output textures اور parameters تیار کرنے پر توجہ دے سکتی ہے، پھر kernel call کو command buffer میں encode کر سکتی ہے۔ Apple کہتا ہے کہ MPS kernels مختلف Metal GPU families کی خصوصیات کے مطابق tune کیے گئے ہیں، لیکن اس سے یہ ضمانت نہیں ملتی کہ کوئی خاص operation ہر device پر ہمیشہ تیز ہوگا۔ اصل device، data format، image size اور اردگرد کے کام سمیت پیمائش کریں۔
کسی تصویر کو blur کرنے پر کیا ہوتا ہے؟
فرض کریں app کو camera frame blur کرنا ہے۔ وہ پہلے MPSSupportsMTLDevice چلا کر دیکھتی ہے کہ Metal device MPS کو support کرتا ہے یا نہیں۔ یہ function framework کی device-level support بتاتا ہے۔ یہ ہر kernel، pixel format یا precision level کی تصدیق نہیں کرتا، اس لیے متعلقہ 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 استعمال کریں۔ تصویر دیکھیں اور ضرورت پڑنے پر pixel differences اور precision کو reference implementation سے ملائیں۔
Filter کے نام میں “Gaussian” کو ریاضیاتی طور پر مکمل Gaussian blur کی ضمانت نہ سمجھیں۔ Apple MPSImageGaussianBlur کو تیز approximation قرار دیتا ہے جو تقریباً 10 bit یا اس سے کم precision مانگنے والے عام image processing کے لیے موزوں ہے۔ اگر تجزیاتی طور پر درست Gaussian filter درکار ہو تو مناسب weights کے ساتھ MPSImageConvolution پر غور کریں۔ بصری اثر اور زیادہ سخت error limits والے تجزیے کے درمیان انتخاب اس فرق سے بدل سکتا ہے۔ MPSImageGaussianBlur کی دستاویز
MPS kernels اور MPSGraph میں کیا فرق ہے؟
MPS kernel کسی تیار operation—جیسے blur، convolution یا matrix multiplication—کو command buffer میں شامل کرنے کے لیے موزوں ہے۔ MPSGraph بڑے computation کو graph کی شکل میں دکھاتا ہے: nodes، addition یا convolution جیسے operations ہوتے ہیں، جبکہ edges کئی جہتی data رکھنے والے tensors ہوتے ہیں۔ Graph بنانے کے بعد app inputs اور outputs طے کرتی ہے، executable compile کرتی ہے اور input data پر اسے چلا سکتی ہے۔ Apple کے مطابق MPSGraph پلیٹ فارم کے compute devices، بشمول GPU، CPU اور Neural Engine، استعمال کر سکتا ہے۔ اصل placement اور support graph، device اور operating system پر منحصر ہیں؛ کسی خاص 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 لکھیں۔ اس صورت میں threads اور memory design، درستگی کی جانچ اور ہر device پر profiling ٹیم کی ذمہ داری ہوگی۔
| صورت حال | پہلے کس انتخاب پر غور کریں | کیا جانچیں |
|---|---|---|
| ایک تیار operation، جیسے image filter یا matrix 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، درستگی اور 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 بنائے جا سکتے ہیں۔ یہ راستہ Metal 4 اور Apple GPU کی نئی صلاحیتوں کے لیے ہے؛ پرانے devices میں وہی خصوصیات فرض نہ کریں۔ موجود image filter صرف چلانے والی app کی ضرورت مختلف ہے، اور اس کے لیے MPS اب بھی مناسب ہے۔ Apple MPP Programming Guide WWDC26: Metal tensors کے ساتھ custom machine-learning operations کی بہتری
MPS اپنانے سے پہلے کیا تصدیق کریں؟
پہلے target operating system اور device پر framework اور متعلقہ operation کی دستیابی دیکھیں۔ MPSSupportsMTLDevice شروعات ہے؛ Apple کی feature table اور API کی availability اور input-format requirements بھی پڑھیں۔ MPS support کا مطلب یہ نہیں کہ تمام MPS classes اور pixel formats ایک جیسی شرائط پر چلتے ہیں۔
دوسرے، optimization سے پہلے قابل قبول error طے کریں۔ Input/output formats اور کم precision کے options قدروں اور رفتار دونوں کو بدل سکتے ہیں۔ دیکھنے میں اچھا blur اس کام کے لیے کافی نہیں ہو سکتا جسے pixel-level reproducibility یا analytical precision درکار ہو؛ اسے دوسرا kernel چاہیے ہو سکتا ہے۔
تیسرے، صرف kernel runtime نہیں بلکہ app کا پورا راستہ ناپیں۔ Textures بنانا یا نقل کرنا، عارضی buffers، encoder بدلنا، اور CPU-GPU انتظار حساب سے زیادہ مہنگے ہو سکتے ہیں۔ Apple کی tuning رہنمائی resources دوبارہ استعمال کرنے اور GPU کے کام کے دوران اگلے commands queue کرنے کا مشورہ دیتی ہے۔ MPS tuning رہنمائی
آخر میں concurrent command encoding دیکھیں۔ MPS kernel object ایک خاص Metal device سے منسلک ہوتا ہے۔ کئی threads کو ایک ہی kernel object بیک وقت تبدیل یا ایک ہی command buffer میں بیک وقت تحریر نہیں کرنی چاہیے۔ اگر کئی threads کام encode کریں تو kernel objects اور command buffers کی ملکیت واضح رکھیں اور کام مکمل ہونے تک resources برقرار رکھیں۔ target devices پر حقیقی models یا frame sizes کا موازنہ کرکے طے کریں کہ MPS، MPSGraph یا custom Metal میں سے کس راستے کو برقرار رکھنا ہے۔
حوالہ جات
[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