Live data from Hacker News

Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

gimletlabs.ai

21–30 of 35 posts

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#21

This is gonna be a silly question but what does “kernel” mean in this context. I thought it meant like a Linux kernel module but doesn’t seem to be?

A kernel is low level function that is going to run in parallel on your accelerator (hopefully efficiently). You will have various matmuls, convolutions, etc.

If you search CUDA kernel you’d find examples but the term was used in HPC before as well.

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#23

They are comparing unoptimized PyTorch inference, something you would never deploy on a device, to a model with custom kernels. Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI. Generally, PyTorch inference is meant to be used during the training process, and when running metrics, not when deploying. When deployed, you should export to ONNX, and then compile the ONNX to…

[deleted]

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#24

They are comparing unoptimized PyTorch inference, something you would never deploy on a device, to a model with custom kernels. Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI. Generally, PyTorch inference is meant to be used during the training process, and when running metrics, not when deploying. When deployed, you should export to ONNX, and then compile the ONNX to…

> and then compile the ONNX to the native format of the device.

I'm assuming you are talking about https://github.com/onnx/onnx-mlir?

In your experience, how much faster is a "compiled" onnx model vs. using an onnx runtime?

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#25

I work on PyTorch and there are many things that make me suspicious about these results. My TL;DR is unless we get a zip file of all the kernels with how they're benchmarked results like this are almost impossible to verify 1. I don't have an M4 but I have an M1 Pro and I tried running the claimed 18x speedup VisionAttention attention example and I get close to identical runtimes. This example has more issues the mai…

Hey, thanks for the thoughtful comments. A lot of big claims have been made in this area so skepticism is the right default reaction. tl;dr: agree that we should provide the kernels and benchmark suite so this can be evaluated by others, will follow up with that.

A few clarifications:

1. Baselines - We didn't compare to torch.compile because as of PyTorch 2.7, torch.compile doesn't support the MPS backend, and we ran into some issues on many of the problems when using it. GitHub issue: https://github.com/pytorch/pytorch/issues/150121. Once it's supported, it will be the obvious baseline.

2. Methodology - We followed KernelBench’s protocol to establish a baseline on Metal, adding more correctness checks. Warmup and synchronization were done. We recognize the limitations here and are expanding the validation suite.

3. Optimizations - Right now most of the optimizations are fusions, but there is some use of Metal-specific primitives/optimizations. We expect as we make the supervisor more sophisticated, the novelty of the optimized kernels will also increase.

Overall the goal here is to get some % of the benefit of a human expert in kernel engineering, without developer effort. Compiler-based optimizations are great, but hand-tuned implementations are still common for performance-critical models. The hope is that we can automate some of that process.

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#26

They are comparing unoptimized PyTorch inference, something you would never deploy on a device, to a model with custom kernels. Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI. Generally, PyTorch inference is meant to be used during the training process, and when running metrics, not when deploying. When deployed, you should export to ONNX, and then compile the ONNX to…

> and then compile the ONNX to the native format of the device. I'm assuming you are talking about https://github.com/onnx/onnx-mlir ? In your experience, how much faster is a "compiled" onnx model vs. using an onnx runtime?

For other people reading this:

Back in the day TensorFlow had tfdeploy which compiled TensorFlow terms into NumPy matrix operations. Our synthetic tests saw speedups of factor 50.

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#27

They are comparing unoptimized PyTorch inference, something you would never deploy on a device, to a model with custom kernels. Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI. Generally, PyTorch inference is meant to be used during the training process, and when running metrics, not when deploying. When deployed, you should export to ONNX, and then compile the ONNX to…

> Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI.

But that’s the thing, I wouldn’t write a custom kernel before AI

I don't do that level of development or operate at that part of the stack but I’m very experienced in software development

AI significantly augments my skillsets in this area

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#28

It'd be curious to see how those AI generated kernels compare to kernels generated by https://github.com/tinygrad/tinygrad

As they wrote most of the wins are because of fusion and TimyGrad started to have fusion optimizations in the last few weeks.

GeoHot didn't want to make it only FlashAttention specific, he worked on FlashAttenrion being automatically generated by the optimizer. It's going well

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#29

They are comparing unoptimized PyTorch inference, something you would never deploy on a device, to a model with custom kernels. Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI. Generally, PyTorch inference is meant to be used during the training process, and when running metrics, not when deploying. When deployed, you should export to ONNX, and then compile the ONNX to…

> Yes, of course the model with custom kernels is faster, whether it's written by a human or an AI. But that’s the thing, I wouldn’t write a custom kernel before AI I don't do that level of development or operate at that part of the stack but I’m very experienced in software development AI significantly augments my skillsets in this area

The point is those kernels exist already, you can just use them off the shelf. In the case where you're trying to write a production grade kernel without operating at that part of the stack... well good luck with that.

Re: Speeding up PyTorch inference on Apple devices with AI-generated Metal kernels

#30
post #9

> non 100% correctness of kernels wouldn't model not work properly if kernels are even slightly off? wasn't kernels a part of training stack for models? am I missing anything?

The article is referring to GPU compute kernel ( https://en.wikipedia.org/wiki/Compute_kernel ), not the term kernel used in ML/NN/etc.

…aren't they the same thing
Post reply on HN