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.1.0-cp39-abi3-manylinux_2_39_x86_64.whl (56.2 MB view details)

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

phantom_lang-0.1.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.1.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.1.0-cp39-abi3-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for phantom_lang-0.1.0-cp39-abi3-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 101bfe00694c8030f307eb15123e288d7b6ba6424828c6471f4d3b398cc37e8a
MD5 0b67e2282146447ecbb8cf3a9141d5ad
BLAKE2b-256 9e6d09bd283af8471303170f6ca2a3ac464cf20a699ee9137f9863de49d4a147

See more details on using hashes here.

Provenance

The following attestation bundles were made for phantom_lang-0.1.0-cp39-abi3-manylinux_2_39_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.1.0-cp39-abi3-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for phantom_lang-0.1.0-cp39-abi3-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 77f95047d3cbf8c170bcec1f81d4f5edcd95bab3817e3f5962e31fe542e6b00c
MD5 b867423b652c397566bdbebf6a16d2af
BLAKE2b-256 fbeab2c27243af21844474d8ed7bc47afc90c2bdd8ed87e05104fdae473021cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for phantom_lang-0.1.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.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for phantom_lang-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d66bdf080cc11d97938b0a538050c7c0f9641603ba1d741ececff6b4c492db0c
MD5 80c1ca938a933497ad20690dbbec888a
BLAKE2b-256 595e47212129eb4eae7d7c699e9b0e229795bee26a69a816a706e08bdcd37eb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for phantom_lang-0.1.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