Book contents +

XVII · Python C-ABI (welvet on PyPI) / CHAPTER 26

Python C-ABI: install and Dense forward

Samuel Watson · Welvet feature bookOriginal source ↗

welvet (PyPI)linux · macos arm64


Why it exists

Use the same Welvet engine from Python via ctypes over a prebuilt C ABI: no Go toolchain required for day-to-day scripts.

What it is

Install welvet==1.1.2, assert the CABI contract, create a CPU grid, place a Dense layer, and run one forward.

Package vs engine

Python package / CABI contract is 1.1.2. The Go engine behind the shared library is Welvet v1.7.3. Wheels ship the matching welvet.so / welvet.dylib (Linux x86_64 manylinux includes CUDA/ROCm/Vulkan/cpp siblings; macOS arm64 includes Metal).

pip install welvet==1.1.2

Replaces the Loom-era PyPI line (welvet==0.80.0). Prefer create_grid + place_* over legacy Loom network helpers. Sibling docs: w2a/python/README.md. Backends: §115. Train + entity: §116. Layers: §117.

Python example

examples/114-python-install/main.py

Run:cd welvet/examples/114-python-install && python3 main.py
"""114. Python C-ABI: install and Dense forward."""
from __future__ import annotations

import welvet as w


def main() -> None:
    w.assert_engine_version()
    print("python_package", w.__version__)
    print("cabi_contract", w.engine_version())
    print("go_engine", "v1.7.3")  # engine behind this CABI wheel

    g = w.create_grid(backend="cpu_tiled")
    try:
        w.place_dense(
            g,
            {"in": 8, "out": 4, "act": "relu", "dtype": w.DType.FLOAT32},
        )
        x = [0.05 * (i + 1) for i in range(8)]
        y = w.forward(g, x)
        print("input", [round(v, 4) for v in x])
        print("output", [round(v, 4) for v in y])
    finally:
        w.free_grid(g)


if __name__ == "__main__":
    main()

Output

exit 0 · last run via python3 main.py

python_package 1.1.2
cabi_contract 1.1.2
go_engine v1.7.3
input [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4]
output [0.0, 0.0, 0.0, 0.0]

Validate (harness)

cd welvet/examples/114-python-install && PYTHONPATH=../../../w2a/python/src python3 main.py