Skip to main content

Python metaprogramming for GPU compute, with runtime-generated kernels, FFTs, and reductions.

Project description

vkdispatch

vkdispatch is a Python GPU computing framework for writing single-source kernels in Python and dispatching them across multiple runtime backends.

It combines runtime code generation, execution helpers, and FFT/reduction utilities in one package. The default PyPI install ships with the Vulkan backend. CUDA and OpenCL backends can be enabled with optional runtime dependencies.

Highlights

  • Single-source Python shaders via @vd.shader and vkdispatch.codegen
  • Multiple runtime backends: Vulkan, CUDA, OpenCL, and a dummy codegen-only backend
  • Backend-aware code generation: GLSL for Vulkan, CUDA source for CUDA, and OpenCL C for OpenCL
  • Native FFT workflows through vd.fft, including mapping hooks for fusion and custom I/O
  • VkFFT-backed transforms through vd.vkfft on the Vulkan backend
  • Reductions through vd.reduce
  • Batched submission and deferred execution through vd.CommandGraph
  • CUDA interop through __cuda_array_interface__ and CUDA Graph capture helpers

Installation

Default Vulkan Install

To install vkdispatch with the Vulkan backend, run:

pip install vkdispatch

This installs the core library, the code generation system, and the Vulkan runtime backend. The Vulkan backend is designed to run on systems supporting Vulkan 1.2 or higher, including macOS via a statically linked MoltenVK. Alternate backends can be added with optional dependencies as described below.

On mainstream platforms - Windows (x86_64), macOS (x86_64 and Apple Silicon/arm64), and Linux (x86_64) - pip will usually download a prebuilt wheel, so no compiler is needed.

On less common platforms, pip may fall back to a source build, which takes a few minutes. See Building From Source for toolchain requirements and developer-oriented instructions.

Core package

For cases where only the codegen component is needed, or in environments where only the CUDA or OpenCL backends are needed, install the core package:

pip install vkdispatch-core

This installs the core library and codegen components, but not the Vulkan runtime backend. To enable runtime features beyond pure codegen, install the optional dependencies below.

Optional components

  • Optional CLI: pip install vkdispatch-core[cli]
  • CUDA runtime backend: pip install vkdispatch-core[cuda]
  • OpenCL runtime backend: pip install vkdispatch-core[opencl]

Runtime backends

vkdispatch currently supports these runtime backends:

  • vulkan
  • cuda
  • opencl
  • dummy

If you do not explicitly select a backend, vkdispatch prefers Vulkan. When the Vulkan backend cannot be imported because it is not installed, initialization falls back to CUDA and then OpenCL.

You can select a backend explicitly in Python:

import vkdispatch as vd

vd.initialize(backend="vulkan")
# vd.initialize(backend="cuda")
# vd.initialize(backend="opencl")
# vd.initialize(backend="dummy")

You can also select the backend with an environment variable:

export VKDISPATCH_BACKEND=vulkan

The dummy backend is useful for codegen-only workflows, source inspection, and development environments where no GPU runtime is available.

There are two intended shader-generation modes:

  • Default mode: generate for the current machine/runtime. This is the normal path and is how vkdispatch picks backend-specific defaults and limits.
  • Custom mode: initialize with backend="dummy" and optionally tune the dummy device limits when you want controlled codegen without relying on the current runtime.

Verifying your installation

If you installed the optional CLI, you can list devices with:

vdlist

# Explicit backend selection can be done with cmdline flags:
vdlist --vulkan
vdlist --cuda
vdlist --opencl

You can always inspect devices from Python:

import vkdispatch as vd

for device in vd.get_devices():
    print(device.get_info_string())

The reported version label depends on the active backend:

  • Vulkan devices show a Vulkan version
  • CUDA devices show CUDA compute capability
  • OpenCL devices show an OpenCL version

Quick start

The example below defines a simple in-place compute kernel in Python:

import numpy as np
import vkdispatch as vd
import vkdispatch.codegen as vc
from vkdispatch.codegen.abbreviations import Buff, Const, f32

# @vd.shader(exec_size=lambda args: args.buff.size)
@vd.shader("buff.size")
def add_scalar(buff: Buff[f32], bias: Const[f32]):
    tid = vc.global_invocation_id().x
    buff[tid] = buff[tid] + bias

arr = np.arange(8, dtype=np.float32)
buff = vd.asbuffer(arr)

# If you want a non-default backend, call vd.initialize(backend=...) first.
add_scalar(buff, 1.5)

print(buff.read(0))

String launch sizing is the shortest form and is kept for convenience. If you want the launch rule to be more explicit and deterministic, use the equivalent lambda form instead: @vd.shader(exec_size=lambda args: args.buff.size).

In normal usage, vkdispatch initializes itself and creates a default context on first runtime use. Call vd.initialize() and vd.make_context() manually only when you want non-default settings such as backend selection, custom device selection, debug logging, or multi-device Vulkan contexts.

Codegen-Only Workflows

If you want generated source without compiling or dispatching it on the current machine, use the dummy backend explicitly:

import vkdispatch as vd
import vkdispatch.codegen as vc
from vkdispatch.codegen.abbreviations import Buff, Const, f32

vd.initialize(backend='dummy')
vd.set_dummy_context_params(
    subgroup_size=32,
    max_workgroup_size=(128, 1, 1),
    max_workgroup_count=(65535, 65535, 65535),
)
vc.set_codegen_backend('cuda')

# @vd.shader(exec_size=lambda args: args.buff.size)
@vd.shader('buff.size')
def add_scalar(buff: Buff[f32], bias: Const[f32]):
    tid = vc.global_invocation_id().x
    buff[tid] = buff[tid] + bias

src = add_scalar.get_src(line_numbers=True)
print(src)

In this mode, vkdispatch uses the dummy device model for launch/layout defaults and emits source for the backend selected with vc.set_codegen_backend(...).

Documentation

The docs site is still under active development, but the main entry points are here:

Some especially useful tutorials:

Happy GPU programming!

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

vkdispatch-0.1.0.tar.gz (4.6 MB view details)

Uploaded Source

Built Distribution

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

vkdispatch-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file vkdispatch-0.1.0.tar.gz.

File metadata

  • Download URL: vkdispatch-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vkdispatch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c633ae9475a3a8e84d740f41fe549d19c7d3be53eccd43651f77529e77cd747b
MD5 77b47f9b252896e99a059f25ba303d27
BLAKE2b-256 41f61917db55b70717fb8d9e73e8c5225bb8c9cb4251d7ad257b3b470a8e4d0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for vkdispatch-0.1.0.tar.gz:

Publisher: python-publish.yml on sharhar/vkdispatch

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

File details

Details for the file vkdispatch-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: vkdispatch-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for vkdispatch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df94443d2bc6dfd806f8050987b1bf5c7a1016ee5be3101a2270ad8d1919c00d
MD5 4b93a353aa783fc3f6407820105e3a92
BLAKE2b-256 1260837b3a749a71a9da0519c21e3b6a5206d5c528032182ece23bd0020ca4dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for vkdispatch-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on sharhar/vkdispatch

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