Skip to main content

Phantom ML compiler: declarative model definitions compiled to native code via LLVM. pip install phantom-lang; import phantom.

Project description

phantom — Python bindings for Phantom

Idiomatic Python bindings for the Phantom compiler. Calls JIT-compiled Phantom shapes against numpy arrays in one function call — no ctypes, no manual buffer management, no dylib path juggling.

Install

pip install maturin
git clone https://github.com/menezzo/phantom
cd phantom/crates/phantom-pyo3
maturin develop --release

A pip install phantom wheel is the next ship; until then, maturin develop builds the extension module in-place against your active Python interpreter.

Quickstart

import numpy as np
import phantom

# Run a Phantom shape from a .ph file with a numpy float32 input.
out = phantom.run(
    "examples/two_host_pipeline.ph",
    np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32),
)
print(out)  # numpy array

# With pre-loaded weights from a directory of .npy files.
out = phantom.run(
    "examples/gpt2.ph",
    np.load("/tmp/phantom-gpt2-golden/input.npy"),
    weights_dir="/tmp/phantom-gpt2-golden",
)
print(out.shape)  # (32, 256)

# Or compile from a source string (for inline notebook use).
src = """
shape add_one =
  it ~> add 1.0
"""
out = phantom.run_source(src, np.array([1.0, 2.0, 3.0], dtype=np.float32))
print(out)  # [2., 3., 4.]

Contract

  • f32 only: numpy arrays must be dtype=np.float32. Other dtypes raise TypeError at the boundary. Cast explicitly with .astype(np.float32) if you need to.
  • C-contiguous only: non-contiguous arrays raise ValueError. Use np.ascontiguousarray(arr) to convert.
  • Errors are Python exceptions: parse/resolve/typecheck/lower failures raise RuntimeError with the rendered diagnostic; runtime errors do the same. No status codes, no thread-local error strings.

What this wraps

phantom.run and phantom.run_source are thin wrappers over phantom_driver::run_pipeline::compile_and_invoke. The same JIT pipeline that powers phantom-driver run from the CLI also powers these calls, so the numerical results match bit-identical (or within floating-point noise) between the Python API and the CLI.

The Phantom GPT-2 model that matches PyTorch within 1.79e-07 via the C FFI also matches PyTorch via this API — same JIT, same MLIR, same LLVM codegen, just a different ergonomic shell.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

phantom_lang-0.2.0-cp39-abi3-manylinux_2_35_x86_64.whl (56.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.35+ x86-64

phantom_lang-0.2.0-cp39-abi3-macosx_11_0_arm64.whl (39.6 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file phantom_lang-0.2.0-cp39-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for phantom_lang-0.2.0-cp39-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 07d64f04035edda6b63695eec35cd7c2b6a4cccdb543d9090e743a3fb6ed91f1
MD5 68900e3f1c9230bfad90cc0b45d7b184
BLAKE2b-256 f03f8ac2786cf419301496b720c38174e5e419ed5711cf0fae8bdf17e1f26f5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for phantom_lang-0.2.0-cp39-abi3-manylinux_2_35_x86_64.whl:

Publisher: pypi-publish.yml on menezzo/phantom

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

File details

Details for the file phantom_lang-0.2.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phantom_lang-0.2.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 498c11e84ea11eae95c9b07f8361ba8817dc1e817382495726e1dda433c4b4c2
MD5 ca32abb7eed9d72a56f6b73639afe40c
BLAKE2b-256 8d533212d242cfbcbcfef1c2db988bf8ac951b92954f6899b6854c34b6b8fc8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for phantom_lang-0.2.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: pypi-publish.yml on menezzo/phantom

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