> ## 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.

# Bringing your own weights

> The artifact format your training run needs to produce.

Khaos does not train models; it serves them. Training and experiments happen in
your own environment, in PyTorch, TensorFlow or any other library that can export
ONNX. The result is an **artifact directory** that Khaos can load, serve and
publish.

## Artifact layout

```text theme={null}
my-artifact/
├── manifest.json
└── model.onnx
```

```json manifest.json theme={null}
{
  "name": "pde_darcy_flow_2d",
  "vertical": "physics",
  "version": "v1",
  "framework": "onnx",
  "files": { "model": "model.onnx" },
  "meta": { "trained_on": "PDEBench", "input_shape": [1, 128, 128] }
}
```

| Key | Required | Meaning |
| - | - | - |
| `name` | yes | Must match a registered artifact name (`khaos info`) |
| `vertical` | yes | The domain subpackage the artifact belongs to |
| `version` | no | Release label such as `v1`, shown by `/v1/artifacts`. Defaults to `v1` |
| `framework` | no | Only `onnx` is supported today |
| `files.model` | no | Path to the model file, defaults to `model.onnx` |
| `meta` | no | Anything you want recorded with the weights |

## Export from other libraries

> [tab] PyTorch
>
> ```python theme={null}
> import torch
>
> model.eval()
> torch.onnx.export(
>     model,
>     torch.zeros(1, 1, 128, 128),
>     "my-artifact/model.onnx",
>     input_names=["x"],
>     output_names=["y"],
>     dynamic_axes={"x": {0: "batch"}, "y": {0: "batch"}},
> )
> ```

> [tab] TensorFlow
>
> ```bash theme={null}
> pip install tf2onnx
> ```
>
> ```python theme={null}
> import tensorflow as tf
> import tf2onnx
>
> spec = (tf.TensorSpec((None, 128, 128, 1), tf.float32, name="x"),)
> tf2onnx.convert.from_keras(
>     model,
>     input_signature=spec,
>     opset=17,
>     output_path="my-artifact/model.onnx",
> )
> ```

> [tab] scikit-learn
>
> ```bash theme={null}
> pip install skl2onnx
> ```
>
> ```python theme={null}
> from skl2onnx import to_onnx
>
> onnx_model = to_onnx(model, X_train[:1].astype("float32"))
> with open("my-artifact/model.onnx", "wb") as f:
>     f.write(onnx_model.SerializeToString())
> ```
>
> Anything else that can produce an ONNX file works too. Gradient-boosted models
> (XGBoost, LightGBM) export through `onnxmltools`, and for other frameworks you
> can look for a converter in the
> [ONNX ecosystem](https://onnx.ai/supported-tools.html).

## Versions

Khaos is on version v1. The first release of every artifact is `v1`, the next is
`v2`, and so on. There are no `v0.x` versions. Set it with
`export_artifact(..., version="v1")`, or in the manifest.

## Check it locally

```bash theme={null}
khaos serve ./my-artifact
```

## Publish

Push the directory to the Hugging Face Hub as `<KHAOS_HUB_ORG>/khaos-<name>`. After that,
`khaos download <name>` and `khaos.load("<name>")` fetch it on demand.

## Pre- and post-processing

The ONNX file holds the network only. Turning raw input into tensors and tensors
into a result container is the job of the artifact's pipeline, a subclass of
`khaos.pipeline.OnnxPipeline` registered under the artifact name:

```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])
```


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