Metadata-Version: 2.4
Name: metal-kernels
Version: 0.1.1
Summary: Metal kernel sources for Apple silicon. The collection currently has FlashAttention for head dim 32.
Author: John Amodeo
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/jmamodeo/metal-kernels
Project-URL: Repository, https://github.com/jmamodeo/metal-kernels
Project-URL: Issues, https://github.com/jmamodeo/metal-kernels/issues
Keywords: metal,gpu,apple,apple-silicon,mlx,pytorch-mps
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: MacOS
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Environment :: MacOS X
Classifier: Environment :: GPU
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Provides-Extra: mlx
Requires-Dist: mlx; extra == "mlx"
Provides-Extra: torch
Requires-Dist: torch; extra == "torch"
Provides-Extra: reference
Requires-Dist: numpy>=1.24; extra == "reference"
Provides-Extra: dev
Requires-Dist: numpy>=1.24; extra == "dev"
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: build; extra == "dev"
Provides-Extra: mac
Requires-Dist: torch; extra == "mac"
Requires-Dist: mlx; extra == "mac"
Requires-Dist: numpy>=1.24; extra == "mac"
Requires-Dist: pytest>=7; extra == "mac"
Dynamic: license-file

# metal-kernels

Python package for [metal-kernels](https://github.com/jmamodeo/metal-kernels), a collection of Apple Metal GPU kernels for Apple silicon. The collection currently has FlashAttention for head dim 32.

```bash
python3 -m pip install metal-kernels
```

The base package has no required dependencies. Optional extras (`mlx`, `torch`, `reference`, `dev`, `mac`) are listed in the [repository README](https://github.com/jmamodeo/metal-kernels#install). From a checkout, the development install is `python3 -m pip install -e "./python[dev]"` at the repository root.

The `.metal` file in [`kernels/`](https://github.com/jmamodeo/metal-kernels/tree/main/kernels) is the only copy of each shader. The package vendors that source and returns its text on import. It does not import MLX or PyTorch. A kernel's launch module compiles the source on first launch, through MLX or PyTorch MPS.

```python
from metal_kernels.flash_attention import flash_attention_hd32

out = flash_attention_hd32(q, k, v, bias=None, scale=None, backend="mlx")  # or backend="mps"
```

## SAM 3 on MPS

`metal_kernels.integrations.sam3` patches the pinned SAM 3 checkout (`31cb7e624e4fe95301b1be1d4e809111593d2e45`) at import time so the image model and the tracker can run without CUDA. Call it before importing SAM 3:

```python
from metal_kernels.integrations.sam3 import build_image_model, install

install()  # cuda if present, else mps, else cpu
model = build_image_model(load_from_HF=False)
```

The ViT MLP uses linear plus GELU on CPU and MPS. On CUDA the original fused op is left in place. MPS itself is not executed by the tests in this repository. The command that times the built-in kernels on a Mac is in [`bench/sam3_shapes/README.md`](https://github.com/jmamodeo/metal-kernels/blob/main/bench/sam3_shapes/README.md).
