Skip to main content

toyc

Toy expression compiler (+ - * /, parens, variables) with CPU and Vulkan GPU backends. Both execute the same AST — compare the results.

from toyc import Cpu, GpuVulkan

Cpu.run("1 + 2")          # 3   (ints stay ints)
GpuVulkan.run("1 + 2")    # 3.0 (GPU is float32)

Install

pip install toyc
sudo apt install glslang-tools   # builds the bundled shaders on first run

Python 3.10+. Needs a Vulkan driver.

Usage

Cpu.run("program.toy")                          # file
Cpu.run("program.toy", env={"a": 1})            # variables
Cpu.run("program.toy", silent=True)             # capture, don't print
Cpu.run("out.toyc")                             # compiled file
Compiler.compile("program.toy")                 # write program.toyc

GpuVulkan.run("program.toy", env={"a": 10.0, "b": 5.0})
GpuVulkan.run("program.toy", env=[{...}, {...}])  # auto-batch → [..]
GpuVulkan.run("program.toy", cache=False)         # skip .toyc write
GpuVulkan.run("program.toyc")                     # cached result, no dispatch

run() takes an inline string, a .toy path, or a .toyc path. .toyc caches hold a scalar (single) or a float list (batch).

GPU session

Setup costs ~15–40 ms once; a dispatch ~0.5 ms. One shared session per process, thread-safe, never torn down per call:

GpuVulkan.startup()    # optional warmup; first call does it lazily
GpuVulkan.run(...)
GpuVulkan.shutdown()   # also automatic at process exit

Batch (where the GPU wins)

One expression, many variable sets, one dispatch per chunk:

results = GpuVulkan.run_batch("prog.toy", [{"a": 1.0}, {"a": 2.0}])

Chunking at the recommended batch size is automatic. Singles → CPU (~0.1 ms vs ~1 ms); bulk → GPU (~23× at N=2000 on RX 580).

Timing & bench

timed=True prints a report and returns (result, timing) (seconds):

cpu_val, cpu_t = Cpu.run("program.toy", timed=True)
gpu_val, gpu_t = GpuVulkan.run("program.toy", timed=True)

toyc.bench needs no loops from you — one equation + knobs, test values auto-generated:

from toyc.bench import bench, batch_bench, summarize, to_csv

rows = bench("a + b * 2", backend="cpu", n=100, repeat=5)
rows = batch_bench("a + b * 2", backend="vulkan", n=2000, repeat=3)
to_csv(rows, "timings.csv")

import pandas as pd                       # your plotting, your deps
pd.DataFrame(rows).groupby("backend")["total"].mean().plot.bar()

Rows carry backend, program, mode, n, repeat, total, total_time_taken, per_eval, check_err + per-stage columns. Batch rows additionally carry num_batches (dispatch chunks used) and batch_size (instances per chunk). summarize(rows) collapses to one row per (backend, program).

Semantics (CPU ≡ GPU)

Case Behavior
Value Numerically equal (15 vs 15.0 — GPU is float32, ~1e-6 error)
1/0, 1/(a-a), 0/0 ZeroDivisionError("Division by zero in VM")
Missing variable NameError("Undefined variable: 'q'")

Pipeline: Lexer → Parser → AST → Evaluator/Cpu, or Flattener → compute shader (toyc/gpu/vulkan/comp*.glsl).

License

See LICENSE.

Metadata

Release files for toyc 0.2.7

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

Source distribution (sdist)

Source distribution for toyc 0.2.7
File Size Uploaded
toyc-0.2.7.tar.gz 42.5 kB Details

Built distribution (wheel)

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

Total release size: 88.8 kB

Release files / toyc-0.2.7.tar.gz

Download URL toyc-0.2.7.tar.gz
Size 42.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d9f694c07b6514cd5c22af8b64d9f737533d07e779eaa77a12e0c0a42f372cf9
BLAKE2b-256 checksum
How to use checksums
46d465aa8840ac9179b5e8f9e59fce8f63dd728d72328dc61bdb7d1249297f14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release files / toyc-0.2.7-py3-none-any.whl

Download URL toyc-0.2.7-py3-none-any.whl
Size 46.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
275bf99c12cc2713395acdcd5ccaa0abf321e71579f7c4add62cff37a2e8a81b
BLAKE2b-256 checksum
How to use checksums
9a18a15633a12a1faf11806cd815b44c4a11f34215fdc24165b29491b9535cb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

0.2.9

2 release files

0.2.8

2 release files

This release

0.2.7 This release

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.4

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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