Skip to main content

Compile a restricted subset of Python into WGSL compute shaders that run on WebGPU

Project description

py2wgsl — Python → WGSL compiler

Compiles a restricted subset of Python into complete WGSL compute shader modules that run on WebGPU. Kernels are ordinary Python functions parsed with the ast module — the Python is never executed; it is translated.

Built for warppool, the distributed WebGPU compute pool, and usable anywhere WGSL runs.

Install

pip install py2wgsl        # zero runtime dependencies
pip install py2wgsl[gpu]   # + wgpu, to execute kernels from Python

Usage

from py2wgsl import Array, f32, compile_kernel, global_id, array_length

def scale_add(scale: f32, offset: f32, values: Array[f32], result: Array[f32]):
    i = global_id()
    if i < array_length(result):
        result[i] = values[i] * scale + offset

kernel = compile_kernel(scale_add, workgroup_size=64)
print(kernel.wgsl)         # full WGSL module, ready for createShaderModule()
print(kernel.buffers)      # binding layout metadata for setting up bind groups

There is also compile_kernel_source(source_str) for compiling from a source string, and python -m py2wgsl.demo prints two example kernels (including a Monte Carlo pi kernel like this project's hand-written shader).

Parameter → binding mapping

Python annotation WGSL
f32, i32, u32 (or float, int) field in a Params uniform struct at @binding(0)
Array[f32] / Array[i32] / Array[u32] storage buffer at the next binding, in declaration order

Buffers the kernel writes to (via buf[i] = ...) get read_write access; the rest are read. The entry point is always fn main with @builtin(global_invocation_id).

Supported subset

  • Assignments (first assignment declares a typed var), annotated assignments, augmented assignments
  • if / elif / else, while, for i in range(...) with constant step, break, continue, bare return
  • Arithmetic, comparison, boolean, and bitwise operators; a if c else b (compiles to select)
  • Math builtins bare or as math.*: sqrt, sin, cos, floor, min, max, clamp, pow, abs, ... and math.pi / math.e / math.tau
  • Casts: f32(x), i32(x), u32(x)
  • GPU builtins: global_id(), local_id(), workgroup_id(), array_length(buf), barrier(), storage_barrier()

Semantics: WGSL wins over Python

These are deliberate; the compiler errors on the ambiguous cases it can catch:

  • Integers are 32-bit and wrap; they are not Python bignums
  • / on two integers is a compile error — use // or cast with f32()
  • // on negative integers truncates toward zero (Python floors)
  • No implicit int/float conversions — cast explicitly (bare literals are fine: x * 2 works when x: f32, matching WGSL abstract literals)
  • Conditions must be bool — no truthiness
  • Variables first assigned inside a block are scoped to that block
  • No atomics, vectors, textures, or user-defined function calls (yet)

Verified on GPU

The generated WGSL is validated by tests/test_py2wgsl.py (structure and error handling, no GPU needed) and has been executed end-to-end on WebGPU via wgpu-py (Metal): elementwise results match a Python reference and the compiled Monte Carlo kernel estimates pi correctly.

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

py2wgsl-0.1.0.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

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

py2wgsl-0.1.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for py2wgsl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a8d4b691024ecaabb50696a98683d21e4e4d469efb0f872ed79c43dd0059de0c
MD5 40299253ede49dc18901dff5053e6436
BLAKE2b-256 e7b65242d7140ea6802fbba7cfa2b9f43f0553311cdc5c116bf5c73910129db4

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on warppool/py2wgsl

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

File details

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

File metadata

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

File hashes

Hashes for py2wgsl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5617b6a121da0af1f4dab37e5cd56cb25cc116886925d4d1174f1f01ac9c586b
MD5 f000d481c0c740bbe5cf754a4bc26978
BLAKE2b-256 6994055cb0775b9e7b2135e0e44b19d388877f6dfc03bf10530668de531e500c

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on warppool/py2wgsl

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