Skip to main content

A tracing Python DSL for writing Metal compute kernels for Apple GPUs.

Project description

Spork: A Tracing DSL for Apple GPUs

Spork is a DSL for writing kernels for Apple GPUs. You can think of it as a Python wrapper on top of Metal, Apple's GPU language. This makes development more convenient, since it makes the kernels live in the same source language as most ML code, and makes it easy to verify correctness against NumPy.

Unlike Triton, Spork is a tracing DSL rather than a parsing-based DSL. This means that a Spork program is fundamentally a Python program that creates a Metal program. Spork can use any Python libraries to aid in metaprogramming.

Installation

Spork runs on Apple Silicon (or any Mac with Metal). Install it from PyPI:

pip install spork-metal

Or with uv:

uv add spork-metal

The package is published as spork-metal on PyPI; the import name is spork:

import spork as sk

Example: Matrix Addition

One of the most basic kernels is a matrix-addition kernel. In Numpy, you could write

shape = (1024, 1024)
A = np.random.randn(*shape).astype(np.float32)
B = np.random.randn(*shape).astype(np.float32)
out = A + B

A Metal kernel to perform this operation would look like this:

#include <metal_stdlib>
using namespace metal;

kernel void matrix_add(
    device float *out [[buffer(0)]],
    device const float *A [[buffer(1)]],
    device const float *B [[buffer(2)]],
    uint index [[thread_position_in_grid]])
{
    out[index] = A[index] + B[index];
}

Notice that it exists in a separate source file and requires special code to compile, link to, and invoke. An equivalent Spork kernel looks like this:

@sk.jit
def matrix_add(
    out   : sk.DevicePointer[sk.dt.float32],
    A     : sk.DevicePointer[sk.dt.float32],
    B     : sk.DevicePointer[sk.dt.float32],
    index : sk.Uint[sk.ThreadPositionInGrid],
):
    out[index] = A[index] + B[index]

Notice that we have a direct correspondance here between the Spork kernel and the Metal kernel. We declare the types of our inputs, and we can take the attribute parameters that we take in Metal.

Then to actually invoke and run kernel, we simply call it from Python with Numpy arrays, and like magic it runs your kernel!

matrix_add[
    (int(np.prod(shape)) // 128, 1, 1),
    (128, 1, 1),
](
    C,
    A,
    B,
)

Notice that in the brackets, we provide two parameters. The first is the Grid size, and the second is the Warpgroup size. In the parentheses, we supply the Numpy tensors we wish to use. Notice that here we're taking a pointer to C, which is allocated via Numpy but written to from the Spork kernel.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spork_metal-0.3.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spork_metal-0.3.0-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file spork_metal-0.3.0.tar.gz.

File metadata

  • Download URL: spork_metal-0.3.0.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spork_metal-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2950d385a19eaf3d8db3c350938458b68b56b947abd03c7af1272455b49406fa
MD5 da679f2f7eee38f29308bfbd859fc16d
BLAKE2b-256 e9edd7bf15d2ef141015f785eefa40a62f44c3f00a95b3edd04e3f7fb53f0e93

See more details on using hashes here.

Provenance

The following attestation bundles were made for spork_metal-0.3.0.tar.gz:

Publisher: publish.yml on save-buffer/spork

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spork_metal-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: spork_metal-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spork_metal-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 69ba898d26d0e9ab7a621f428e7f069151d3272fb519399d7bedbe9f9a98f48a
MD5 084f9035c28e25ab18a4e4bce2561cd8
BLAKE2b-256 cdc26c0f2293d06fc9253bf494653e153d19eb90b79b9fb53c3e63ddcf24af3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for spork_metal-0.3.0-py3-none-any.whl:

Publisher: publish.yml on save-buffer/spork

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page