Skip to main content

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.

Metadata

Release files for py2wgsl 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py2wgsl 0.1.0
File Size Uploaded Provenance
py2wgsl-0.1.0.tar.gz 20.1 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for py2wgsl 0.1.0
File Interpreter ABI Platform
py2wgsl-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 39.0 kB

Release files / py2wgsl-0.1.0.tar.gz

Download URL py2wgsl-0.1.0.tar.gz
Size 20.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a8d4b691024ecaabb50696a98683d21e4e4d469efb0f872ed79c43dd0059de0c
BLAKE2b-256 checksum
How to use checksums
e7b65242d7140ea6802fbba7cfa2b9f43f0553311cdc5c116bf5c73910129db4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

Release files / py2wgsl-0.1.0-py3-none-any.whl

Download URL py2wgsl-0.1.0-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5617b6a121da0af1f4dab37e5cd56cb25cc116886925d4d1174f1f01ac9c586b
BLAKE2b-256 checksum
How to use checksums
6994055cb0775b9e7b2135e0e44b19d388877f6dfc03bf10530668de531e500c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

Security for py2wgsl 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for py2wgsl-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page