> ## Documentation Index
> Fetch the complete documentation index at: https://khaos-ml.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Architecture

> How the repo and runtime fit together.

```text theme={null}
khaos/
├── pyproject.toml
├── Dockerfile          # container image for `khaos serve`
├── docs/               # this documentation (Mintlify)
├── khaos/              # importable package
│   ├── registry.py     # artifact registry, load(), download()
│   ├── manifest.py     # artifact manifest (training -> runtime contract)
│   ├── pipeline.py     # BasePipeline, OnnxPipeline, ModalityProcessor
│   ├── modeling.py     # khaos.modeling.build(artifact)
│   ├── export.py       # export_artifact(): model -> manifest + ONNX
│   ├── layers.py       # ONNX-friendly transformer blocks
│   ├── serve/          # HTTP server (FastAPI)
│   ├── cli/            # `khaos` command
│   └── <domain>/       # finance, geometry, tabular, telemetry,
│       │               # graph, chem, physics, robotics
│       ├── adapter.py      # raw input -> model tensors
│       ├── containers.py   # this domain's own result types
│       └── models/         # network architectures
├── artifacts/          # artifact definitions, one dir per domain
├── training/           # per-artifact training and evaluation configs
└── tests/
```

## Life of an artifact

Your training code`khaos.modeling.build("<artifact>")`, then train however you like

↓

export\_artifact`manifest.json` and `model.onnx`

↓

Hugging Face Hub, or a local directory`<org>/<artifact>`

↓

khaos.load() or khaos serveadapter, then ONNX Runtime, then a result container

1. **Build.** `khaos.modeling.build("<artifact>")` gives the architecture for an
   artifact, defined in `khaos/<domain>/models/`.
2. **Train.** Your own loop, in your own environment. Khaos does not train.
3. **Export.** `export_artifact` writes `manifest.json` and `model.onnx`.
4. **Publish.** Push the directory to the Hugging Face Hub, or keep it local.
5. **Run.** `khaos.load()` and `khaos serve` read the manifest, find the registered
   pipeline for that artifact, and run it on ONNX Runtime.

## Design

* **Code and weights are separate.** This repository holds the runtime and the
  architectures. Weights are published to the Hugging Face Hub and cached locally.
* **ONNX is the contract.** Whatever you train with, the runtime only sees an ONNX
  file and a manifest. The architectures are written to export cleanly with dynamic
  batch and length, and the tests check that ONNX Runtime matches PyTorch.
* **One subpackage per domain.** Each holds its adapter, containers and
  architectures, so optional dependencies stay isolated behind extras.
* **Domain-owned containers.** Each domain defines its own result types. There is
  deliberately no shared base class: a filing, a field grid and a trajectory have
  nothing meaningful in common.
* **Registry-driven loading.** `@register("artifact_name")` binds a pipeline class to
  a name; `load()` imports only the domain it needs.
* **Light runtime, heavy training.** Serving needs ONNX Runtime and FastAPI.
  PyTorch and transformers live behind the `train` extra.
* **Local-first.** Inference runs on the user's machine through ONNX Runtime.

Structure was informed by [spaCy](https://github.com/explosion/spaCy): library and
models are distributed separately, and components are resolved through a registry.

## Adding a pipeline

A pipeline connects an exported model to the rest of the runtime. Subclass
`OnnxPipeline`, turn raw input into named arrays, and build the domain's container
from the outputs:

```python theme={null}
from khaos.pipeline import OnnxPipeline
from khaos.physics.containers import FieldResult
from khaos.registry import register


@register("pde_darcy_flow_2d")
class DarcyFlow(OnnxPipeline):
    def preprocess(self, raw):
        return {"x": raw.astype("float32")[None, None]}

    def postprocess(self, outputs):
        return FieldResult(prediction=outputs[0][0, 0])
```

Once registered, the name works everywhere: `khaos.load("pde_darcy_flow_2d")`,
`khaos serve pde_darcy_flow_2d`, and `khaos download pde_darcy_flow_2d`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.