{ "$schema": "https://raw.githubusercontent.com/guitaripod/wwdc-sessions/master/schema/session.schema.json", "schemaVersion": 1, "id": "wwdc2026-330", "event": "wwdc2026", "eventName": "WWDC26", "year": 2026, "eventContentId": 330, "title": "Optimize custom machine learning operations with Metal tensors", "description": "Unlock powerful machine learning performance with the Metal Tensor API and Metal Performance Primitives (MPP) Tensor Ops library. Discover how to create portable operations that take advantage of Neural Accelerators in Apple M5 and A19 GPUs. Learn to build custom machine learning kernels for your Core AI applications, and find out how to work effectively with quantized data formats and GPU memory optimization.", "url": "https://developer.apple.com/videos/play/wwdc2026/330", "sosumiURL": "https://sosumi.ai/videos/play/wwdc2026/330", "type": "Video", "publishedAt": "2026-06-08T14:00:00-07:00", "platforms": [ "iOS", "iPadOS", "macOS", "tvOS", "visionOS", "watchOS" ], "keywords": [], "topics": [ "Graphics & Games", "AI & Machine Learning" ], "primaryTopic": "AI & Machine Learning", "duration": 973, "hasTranscript": true, "transcriptWordCount": 2116, "availableLanguages": [ "eng", "fra", "jpn", "kor", "por", "spa", "zho" ], "media": { "hls": "https://devstreaming-cdn.apple.com/videos/wwdc/2026/330/4/0ff2c290-e47b-4d88-8a8f-0634e11506a4/cmaf.m3u8", "downloadHLS": "https://devstreaming-cdn.apple.com/videos/wwdc/2026/330/4/0ff2c290-e47b-4d88-8a8f-0634e11506a4/cmaf-download.m3u8", "downloadSD": "https://devstreaming-cdn.apple.com/videos/wwdc/2026/330/4/0ff2c290-e47b-4d88-8a8f-0634e11506a4/downloads/wwdc2026-330_sd.mp4", "downloadHD": "https://devstreaming-cdn.apple.com/videos/wwdc/2026/330/4/0ff2c290-e47b-4d88-8a8f-0634e11506a4/downloads/wwdc2026-330_hd.mp4", "duration": 973 }, "codeSnippets": [ { "title": "Create a quantized MTLTensor", "language": "objectivec", "startTimeSeconds": 233, "code": "// Creating a tensor with a quantized data type from device\n\n#define RANK 2\n\nMTLTensorDescriptor *tensorDesc = [MTLTensorDescriptor new];\n\ntensorDesc.dataType = MTLTensorDataTypeMetalFloat8E4M3;\ntensorDesc.usage = MTLTensorUsageCompute;\n\nNSInteger dimensions[RANK] = {NumCols, NumRows};\ntensorDesc.dimensions = [[MTLTensorExtents alloc] initWithRank:RANK values:dimensions];\n\nNSError *err = nil;\nid tensor = [device newTensorWithDescriptor:tensorDesc error:&err];" }, { "title": "Declare a multi-plane tensor with scale factors", "language": "objectivec", "startTimeSeconds": 288, "code": "// Creating a tensor with a scales auxiliary plane from device\n\n#define RANK 2\n\nMTLTensorAuxiliaryPlaneDescriptor *planeDesc = [MTLTensorAuxiliaryPlaneDescriptor new];\nplaneDesc.dataType = MTLTensorDataTypeMetalFloat8UE8M0;\n\nNSInteger blockFactors[RANK] = {32, 1};\nplaneDesc.blockFactors = [[MTLTensorExtents alloc] initWithRank:RANK values:blockFactors];\n\nMTLTensorAuxiliaryPlaneDescriptorMap *auxiliaryPlanes =\n [MTLTensorAuxiliaryPlaneDescriptorMap new];\n[auxiliaryPlanes setDescriptor:planeDesc forPlane:MTLTensorPlaneTypeScales];\n\nMTLTensorDescriptor *tensorDesc = [MTLTensorDescriptor new];\ntensorDesc.dataType = MTLTensorDataTypeMetalFloat8E4M3;\ntensorDesc.usage = MTLTensorUsageCompute;\n\nNSInteger dimensions[RANK] = {NumCols, NumRows};\ntensorDesc.dimensions = [[MTLTensorExtents alloc] initWithRank:RANK values:dimensions];\ntensorDesc.auxiliaryPlanes = auxiliaryPlanes;\n\nNSError *err = nil;\nid tensor = [device newTensorWithDescriptor:tensorDesc error:&err];" }, { "title": "MSL type aliases for an MXFP8 tensor handle", "language": "objectivec", "startTimeSeconds": 367, "code": "// Type aliases for a MXFP8 multi-plane tensor handle\n\n#include \n\nusing namespace metal;\n\nusing scales_plane = tensor_blockwise;\n\nusing mxfp8_tensor = tensor,\n tensor_handle,\n scales_plane>;\n\nkernel void matmul(mxfp8_tensor matrixA [[buffer(0)]],\n mxfp8_tensor matrixB [[buffer(1)]],\n tensor> matrixC [[buffer(2)]])\n{\n // ...\n}" }, { "title": "Declare an inline MXFP8 tensor on the stack", "language": "objectivec", "startTimeSeconds": 411, "code": "// Type aliases for a MXFP8 multi-plane tensor inline\n\n#include \n\nusing namespace metal;\n\nusing scales_plane = tensor_blockwise;\n\nusing mxfp8_tensor_inline = tensor,\n tensor_inline,\n scales_plane>;\n\n// Construct tensor on the stack from buffer pointers\nmxfp8_tensor_inline matrixA(dataBufferA,\n dextents(K, M),\n array({ 1, K }),\n scales_plane(scalesBufferA));" }, { "title": "Slice tensors and run a quantized matmul", "language": "objectivec", "startTimeSeconds": 439, "code": "// Slice the tensors to extract the relevant tile\nauto tA = matrixA.slice(0, tgid.y * TILEM);\nauto tB = matrixB.slice(tgid.x * TILEN, 0);\nauto tC = matrixC.slice(tgid.x * TILEN, tgid.y * TILEM);\n\n// Set up the matmul descriptor\nconstexpr auto descriptor = matmul2d_descriptor(TILEM, // M\n TILEN, // N\n dynamic_length_v, // K\n false, // Left matrix transposed\n false); // Right matrix transposed\n\nmatmul2d> op;\n\n// Run the op — TensorOps handles dequantization automatically\nop.run(tA, tB, tC);" }, { "title": "Set up simdgroup-scoped QxK multiplication", "language": "objectivec", "startTimeSeconds": 627, "code": "// Setup QxK matrix multiplication op\nconstexpr auto mul_qk_op_desc = matmul2d_descriptor(/* ... */);\nmatmul2d mul_qk_op;\n\n// Slice Q, K, V\nauto tQSlice = tQ.slice(0, sgid * ROWS_PER_SIMD);\nauto tKSlice = tK.slice(0, k);\nauto tVSlice = tV.slice(0, k);\n\n// Create cooperative tensor to store tile of QxK\nauto ctQK = mul_qk_op.get_destination_cooperative_tensor();\n\n// Multiply QxK\nmul_qk_op.run(tQSlice, tKSlice, ctQK);" }, { "title": "Compute row-wise reduction for SoftMax", "language": "objectivec", "startTimeSeconds": 678, "code": "// Create a cooperative tensor to store row reduction output\nauto ctTileRowMax = mul_qk_op.get_row_reduction_destination_cooperative_tensor<\n decltype(tQSlice),\n decltype(tKSlice),\n float>();\n\n// Compute max over each row of QxK tile\nreduce_rows(ctQK, ctTileRowMax, reduction_operation::max, -INFINITY);" }, { "title": "Compute element-wise SoftMax with map_iterator", "language": "objectivec", "startTimeSeconds": 716, "code": "// Iterate over elements of QxK tile\n#pragma clang loop unroll(full)\nfor (auto it = ctQK.begin(); it != ctQK.end(); it++) {\n // Fetch row max corresponding to this element\n auto row_it = ctRowMax.map_iterator(it);\n\n // Subtract row max from each element and compute exponent\n *it = exp(*it - *row_it);\n}" }, { "title": "Reuse cooperative tensor as matmul input", "language": "objectivec", "startTimeSeconds": 753, "code": "constexpr auto mul_sv_op_desc = matmul2d_descriptor(/* ... */);\nmatmul2d mul_sv_op;\n\nif (mul_sv_op.is_compatible_as_left_input(ctQK)) {\n // Directly reuse cooperative tensor as input\n auto ctQKIn = mul_sv_op.get_left_input_cooperative_tensor(ctQK);\n mul_sv_op.run(ctQKIn, tVSlice, ctO);\n} else {\n // Store and reload through threadgroup memory if layout is not compatible\n ctQK.store(tgTensor);\n simdgroup_barrier(mem_flags::mem_threadgroup);\n\n auto ctQKIn = mul_sv_op.get_left_input_cooperative_tensor();\n ctQKIn.load(tgTensor);\n mul_sv_op.run(ctQKIn, tVSlice, ctO);\n}" } ], "resources": [ { "title": "Running inline ML operations in a shader with Metal 4", "url": "https://developer.apple.com/documentation/Metal/running-inline-ml-operations-in-a-shader-with-metal-4", "type": "samplecode", "description": "Multiply matrices across multiple GPU cores with inline tensor operations.", "sosumiURL": "https://sosumi.ai/documentation/Metal/running-inline-ml-operations-in-a-shader-with-metal-4", "doccJSON": "https://developer.apple.com/tutorials/data/documentation/Metal/running-inline-ml-operations-in-a-shader-with-metal-4.json" }, { "title": "Machine learning passes", "url": "https://developer.apple.com/documentation/Metal/machine-learning-passes", "type": "documentation", "description": "Add machine learning model inference to your Metal app’s GPU workflow.", "sosumiURL": "https://sosumi.ai/documentation/Metal/machine-learning-passes", "doccJSON": "https://developer.apple.com/tutorials/data/documentation/Metal/machine-learning-passes.json" }, { "title": "Download the Metal Performance Primitives (MPP) Programming Guide", "url": "https://developer.apple.com/download/files/Metal-Performance-Primitives-Programming-Guide.pdf", "type": "download", "description": null, "sosumiURL": "https://sosumi.ai/download/files/Metal-Performance-Primitives-Programming-Guide.pdf" }, { "title": "Metal Performance Shaders", "url": "https://developer.apple.com/documentation/MetalPerformanceShaders", "type": "documentation", "description": "Optimize graphics and compute performance with kernels that are fine-tuned for the unique characteristics of each Metal GPU family.", "sosumiURL": "https://sosumi.ai/documentation/MetalPerformanceShaders", "doccJSON": "https://developer.apple.com/tutorials/data/documentation/MetalPerformanceShaders.json" } ], "path": "sessions/wwdc2026/330-optimize-custom-machine-learning-operations-with-metal-tenso/", "files": { "metadata": "sessions/wwdc2026/330-optimize-custom-machine-learning-operations-with-metal-tenso/metadata.json", "readme": "sessions/wwdc2026/330-optimize-custom-machine-learning-operations-with-metal-tenso/README.md", "transcript": "sessions/wwdc2026/330-optimize-custom-machine-learning-operations-with-metal-tenso/transcript.md", "transcriptJSON": "sessions/wwdc2026/330-optimize-custom-machine-learning-operations-with-metal-tenso/transcript.json" } }