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_time_taken"].mean().plot.bar()

Pass env= to control the variable sets yourself instead of letting them be generated:

# template dict: pin k at 5, sweep x (1.0–10.0, seeded)
rows = bench("x / k", backend="cpu", n=100, repeat=5, seed=7,
             env={"k": 5})

# full list: every instance supplied outright, n becomes informational
rows = batch_bench("x * k", backend="vulkan", n=3, repeat=2,
                   env=[{"x": 1.0, "k": 4}, {"x": 2.0, "k": 4},
                        {"x": 3.0, "k": 4}])

Every supplied env must define all variables the program uses (checked up front — same condition the backends raise NameError for), and pinned names are never re-randomized.

Rows carry backend, program, mode, n, repeat, total, total_time_taken, per_eval, check_err + per-stage columns. Single rows hold one scalar in result; batch rows hold the first instance's value (same instance check_err validates). CPU batch rows add threads (workers used). GPU batch rows come one per chunk and add num_batches (chunks in the run), batch_size (instances per chunk), batch_index (which chunk, 0-based) and chunk_n (instances in it). summarize(rows) collapses to one row per (backend, program).

CPU batch runs multithreaded automatically (batch_bench(..., threads=None) = all CPUs, 1 = sequential), and Cpu.run_batch(prog, envs, workers=…) / Compiler.compile_many( items, workers=…) expose the same control directly. Note: CPython threads share the GIL, so threaded CPU suits big batches and typically gains far less than the worker count — measure, don't assume.

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.12

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.12
File Size Uploaded Provenance Actions
toyc-0.2.12.tar.gz 49.2 kB Without provenance Details for toyc-0.2.12.tar.gz

Built distribution (wheel)

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

Total release size: 99.2 kB

Release files / toyc-0.2.12.tar.gz

Download URL toyc-0.2.12.tar.gz
Size 49.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9563165b8890b37e1ea873876a5ab926acfb9af59dcd1c22854d5c2e3948d650
BLAKE2b-256 checksum
How to use checksums
4c0f5a9709d2f0c590297743a0bbdbb06d46b931f0b4ff5b553b789f18a003d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.17

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

Download URL toyc-0.2.12-py3-none-any.whl
Size 50.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e0b93e2673b08e1450a5b08b51a1fff50fd0cb4c19e785e9b8d451f75337773
BLAKE2b-256 checksum
How to use checksums
15eccc23841ac3ac644bef4c0f5d5ce444c66fbf1e8acb838ed4549caf971fd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.17

Security for toyc 0.2.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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