Skip to main content

Graphite

Graphite is a Python toolkit for describing, validating, matching, and rewriting Heterograph-based intermediate representations (IRs). Graphite is independent of workflow orchestration frameworks.

For pattern matching, rewrites, and examples, see the GraphProcessor guide.

Installation

Conda (development)

From the repository root, create an environment with Graphite installed in editable mode. Changes to the source take effect immediately:

conda env create -f environment.yml
conda activate graphite
pytest

pip

Graphite requires Python 3.10 or newer and graph-tool. For Ubuntu:

apt-get install python3-graph-tool

For other graph-tool installation options, see the graph-tool installation documentation.

Then install graphite-ir from PyPI:

python -m pip install graphite-ir

Examples

The runnable examples are in examples/. The validation, processor, and lowering examples use Graphite directly; see the examples README for commands and descriptions.

IR schemas

GraphSchema defines the accepted graph, vertex, and edge metadata with Pydantic models. It constructs correctly typed graph elements and returns a ValidationReport instead of failing at the first invalid element. Each graph element uses _type to select its model.

from typing import Literal

from pydantic import BaseModel, Field
from graphite import GraphSchema


class ConstantProps(BaseModel):
    type_: Literal["Constant"] = Field(alias="_type")
    value: float


class NegateProps(BaseModel):
    type_: Literal["Negate"] = Field(alias="_type")


class DataEdgeProps(BaseModel):
    type_: Literal["data"] = Field(alias="_type")


ir = GraphSchema(
    name="ArithmeticIR",
    vertex={"Constant": ConstantProps, "Negate": NegateProps},
    edge={"data": DataEdgeProps},
)

graph = ir.new_graph()
constant = ir.new_vertex(graph, "Constant", value=3.0)
negate = ir.new_vertex(graph, "Negate")
ir.new_edge(graph, constant, negate, "data")

report = ir.validate(graph)
assert report.ok

Pass rules to GraphSchema for structural validation such as arity, acyclicity, or required inputs and outputs. A rule receives the HGraph and returns a list of Issue values. update_vertex() and update_edge() apply the same Pydantic validation when metadata changes.

Graph processor

GraphProcessor finds AQL patterns and applies one in-place rewrite pass. A result contains matches, the parsed pattern, and a modified flag.

See GraphProcessor.md for the AQL grammar, matching semantics, rewrites, rewiring, and a complete Dense-lowering walkthrough.

from heterograph import HGraph
from graphite import GraphProcessor

graph = HGraph()
graph.add_vx(3)
graph.add_edge(0, 1)
graph.add_edge(1, 2)

processor = GraphProcessor(snapshot=False)
result = processor.run(
    graph,
    select="a => b => c",
    rewrite="a => c",
)

assert result["modified"]
assert set(graph.edges) == {(0, 2)}

Use where(graph, **match) to filter matches and finalize(graph, **match) to initialize created vertices or metadata. finalize must return a boolean. Rewrites require disjoint matches; Graphite rejects overlapping matches before mutating the graph. Rewire annotations preserve boundary connections when a matched vertex is replaced:

processor.run(
    graph,
    select="a => b => c",
    rewrite="a => x {rewire: b} => c",
)

GraphProcessor(snapshot=True) is the default and records graphs in a Heterograph WebView. Use snapshot=False for scripts and tests without a viewer.

The standalone lowering example is in examples/lowering.

Metadata

Release files for graphite-ir 0.2.0

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

Source distribution (sdist)

Source distribution for graphite-ir 0.2.0
File Size Uploaded Provenance Actions
graphite_ir-0.2.0.tar.gz 12.7 kB Without provenance Details for graphite_ir-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for graphite-ir 0.2.0
File Interpreter ABI Platform Actions
graphite_ir-0.2.0-py3-none-any.whl Python 3 none any Details for graphite_ir-0.2.0-py3-none-any.whl

Total release size: 24.4 kB

Release files / graphite_ir-0.2.0.tar.gz

Download URL graphite_ir-0.2.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
75fdaf81cff43173f6517a5e1b19703d51ed3d6da1267e2b4b19ab4762d61f52
BLAKE2b-256 checksum
How to use checksums
c7380823efcb5ada612f7f5f17f47e1c760a91d01b4aec491b330efeb2ca7e23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / graphite_ir-0.2.0-py3-none-any.whl

Download URL graphite_ir-0.2.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
911281a4079cc51bab348a9be72f68569aecacc460800df2bcb8b2482f618ede
BLAKE2b-256 checksum
How to use checksums
e14a3950a46e19728ad3b759d4b78dca27a9ba7f7a0ac9f90dcbff7927154d94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for graphite-ir 0.2.0

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