Skip to main content

JSON format for composable model graphs

Project description

Trama

Trama is a standalone Python library that defines a JSON schema, validator, and export/import framework for hierarchical model graphs in Static Single Assignment (SSA) style. It is framework-agnostic and is designed to be used by domain-specific modeling libraries that inject their own op registries. Models are serialized to self-contained .trama.json files capturing both topology and parameter values, and can be reconstructed back into runtime modules via registered factory functions.

Installation

# With pip:
pip install trama

# Or with uv:
uv add trama

Quick-start

import trama

# Export a module that implements ExportableOp + ImportableOp.
model = trama.export_model(module, inputs=["x"], outputs=["y"], registry=registry)

# Serialize to JSON and save.
trama.dump(model, "my_model.trama.json")

# Later: load and reconstruct the runtime module.
model = trama.load("my_model.trama.json")
instance = trama.import_model(model, registry=registry)

JSON Schema

Trama exposes a JSON Schema for .trama.json files. Generate it with the tramaschema command:

tramaschema > trama.schema.json

The schema validates format structure: required fields, field types, and identifier patterns. Registry conformance — whether op names resolve and parameter types match — is not expressible in JSON Schema and is enforced at load time in Python by trama.load() or trama.validate().

Example

{
  "format_version": "0.2",
  "inputs": ["x"],
  "outputs": [{ "node": "root", "output": "y" }],
  "nodes": [
    {
      "id": "root", "parent": null, "op": "Sequential",
      "inputs": { "x": { "node": "%input", "output": "x" } },
      "outputs": ["y"],
      "parameters": {},
      "output_bindings": { "y": { "node": "root.linear_1", "output": "y" } }
    },
    {
      "id": "root.linear_0", "parent": "root", "op": "Linear",
      "inputs": { "x": { "node": "%input", "output": "x" } },
      "outputs": ["y"],
      "parameters": {
        "in_features":  { "name": "root.linear_0.in_features" },
        "out_features": { "name": "root.linear_0.out_features" },
        "weight":       { "name": "root.linear_0.weight" }
      },
      "output_bindings": null
    },
    {
      "id": "root.linear_1", "parent": "root", "op": "Linear",
      "inputs": { "x": { "node": "root.linear_0", "output": "y" } },
      "outputs": ["y"],
      "parameters": {
        "in_features":  { "name": "root.linear_0.out_features" },
        "out_features": { "name": "root.linear_1.out_features" },
        "weight":       { "name": "root.linear_1.weight" }
      },
      "output_bindings": null
    }
  ],
  "parameters": {
    "root.linear_0.in_features":  { "value": 8,  "learnable": false },
    "root.linear_0.out_features": { "value": 16, "learnable": false },
    "root.linear_1.out_features": { "value": 4,  "learnable": false },
    "root.linear_0.weight": { "value": { "dtype": "float32", "shape": [16, 8],  "data": "..." }, "learnable": true },
    "root.linear_1.weight": { "value": { "dtype": "float32", "shape": [4,  16], "data": "..." }, "learnable": true }
  }
}

Note that root.linear_0.out_features is referenced by both root.linear_0 and root.linear_1 (the dimension is the same physical value). This is the parameter sharing mechanism: a single entry in the table, multiple refs.

The trama format is inspired by SSA form from compiler design. The tramassa command prints any .trama.json file in this human-readable form:

# trama SSA  (format 0.2)

model(x) -> %root.y

params
  root.linear_0.in_features   : int         = 8
  root.linear_0.out_features  : int         = 16
  root.linear_1.out_features  : int         = 4
  root.linear_0.weight        : f32[16, 8]  learnable
  root.linear_1.weight        : f32[4, 16]  learnable

scope root [Sequential]
  y = %root.linear_1.y

%root.linear_0.y = Linear(
    x            = x,
    in_features  = root.linear_0.in_features,
    out_features = root.linear_0.out_features,
    weight       = root.linear_0.weight
)

%root.linear_1.y = Linear(
    x            = %root.linear_0.y,
    in_features  = root.linear_0.out_features,
    out_features = root.linear_1.out_features,
    weight       = root.linear_1.weight
)

Design

For detailed description of the design of Trama please see the design document.

Compared to other model export formats

Trama operates at a fundamentally different abstraction level than ONNX, torch.export, and similar formats. Those tools target deployment: they decompose user-authored modules into low-level tensor operations (aten.matmul, aten.conv2d, ONNX Gemm) so a runtime can execute them efficiently. The user's Sequential(Encoder(), Decoder()) becomes a flat sequence of primitive ops with the original module boundaries dissolved. Trama instead targets composition: it preserves the user-authored module hierarchy as first-class structure, captures only the wiring between the building blocks the consumer library defines, and treats those blocks as opaque ops whose semantics live in the registry. This makes trama files dramatically smaller and more readable than ONNX graphs of equivalent models, diffable in code review, and meaningful to humans inspecting "what does this model architecture actually look like" — at the cost of being unsuitable for cross-framework deployment, since the ops are defined by the consumer library rather than a standardized vocabulary. Trama is complementary to ONNX, not a replacement: a natural pipeline is to author and review models in trama, then lower to ONNX or torch.export when shipping to inference.

Project details


Download files

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

Source Distribution

trama-0.2.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

trama-0.2.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file trama-0.2.0.tar.gz.

File metadata

  • Download URL: trama-0.2.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for trama-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ce882fc3cb96b77d4c2e0c25adddc35b91ff5c237b042452b3cb73fa344de7cf
MD5 6f5d4599d66d39291a6c6fdc8055c66d
BLAKE2b-256 ccd217d96cd320f533699a86fd2d19ee63df8339235a1b9fcd4fef03c944c16d

See more details on using hashes here.

File details

Details for the file trama-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: trama-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for trama-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11b8e444a2a025a8c521167b5439bbb7ba078d861600c822b19104c9ebbac031
MD5 1fa5e101be8b185a3b85621e92e1450d
BLAKE2b-256 d6834d9477cb6a63e1cc033e4e3ee4cfe9d2ca47f16c6956902cbca698efe5d7

See more details on using hashes here.

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