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

# Models

> Building architectures and the layers they share.

Install the training extra to use these: `pip install "khaos-ml[train]"`.

## `khaos.modeling`

```python theme={null}
from khaos.modeling import build, build_from, Spec
```

### `build`

```python theme={null}
build(artifact: str, **overrides) -> torch.nn.Module
```

Builds the default architecture of any artifact. Keyword arguments override the defaults.
It imports only the artifact's own domain package.

```python theme={null}
from khaos.modeling import build

model = build("graph_link_prediction")                       # defaults
model = build("graph_link_prediction", in_dim=32, hidden=64)  # overrides
```

Raises `KeyError` for an unknown artifact.

### `Spec` and `build_from`

```python theme={null}
Spec(cls, kwargs)                        # a factory plus its default keyword arguments
build_from(architectures, artifact, **overrides)
```

Each domain keeps a `khaos.<domain>.models.ARCHITECTURES` dict of `Spec`s and exposes its own
`build(artifact, **overrides)`, which calls `build_from`.

## Architectures

Defaults for every artifact. Trained artifacts record the settings they actually used in
`manifest.json`; for example the chemistry models use the vocabulary size of their tokenizer.

| Artifact | Domain | Class | Default settings |
| - | - | - | - |
| `sec_10k_core`, `sec_8k_events`, `sec_form4_insider`, `sec_s1_ipo` | finance | `FilingNER` | `distilbert-base-uncased` encoder, label set per artifact |
| `pde_darcy_flow_2d` | physics | `FNO2d` | in\_channels=4, out\_channels=1, resolution=(64, 64), modes=(16, 16), width=32 |
| `pde_navier_stokes_steady_2d` | physics | `FNO2d` | in\_channels=3, out\_channels=2, modes=(16, 16), width=32, param\_dim=1 |
| `pde_heat_diffusion_2d` | physics | `TimeConditionedFNO2d` | in\_channels=4, out\_channels=1, modes=(16, 16), width=32, param\_dim=1 |
| `pde_wave_evolution_2d` | physics | `TimeConditionedFNO2d` | in\_channels=4, out\_channels=2, modes=(32, 32), width=48, param\_dim=1 |
| `robo_kinematics_sm` | robotics | `TrajectoryTransformer` | state\_dim=14, dim=128, depth=4, max\_len=1024 |
| `robo_skill_segmentation` | robotics | `SkillSegmenter` | state\_dim=14, dim=128, depth=4, num\_skills=6, max\_len=1024 |
| `robo_failure_detection` | robotics | `FailureDetector` | state\_dim=14, dim=128, depth=4, max\_len=1024 |
| `geo_part_classifier` | geometry | `DGCNNClassifier` | num\_classes=40, num\_points=512, k=16 |
| `geo_mesh_embedding` | geometry | `MeshEncoder` | num\_points=512, k=16, dim=256 |
| `geo_pointcloud_embedding` | geometry | `PointCloudEncoder` | num\_points=512, k=16, dim=256 |
| `tab_column_types` | tabular | `ColumnTypeCNN` | num\_types=91 |
| `tab_key_matching` | tabular | `ColumnPairMatcher` | dim=64 |
| `tab_entity_dedupe` | tabular | `RecordMatcher` | `distilbert-base-uncased` encoder |
| `tab_anomaly_scoring` | tabular | `TabularAnomalyTransformer` | max\_columns=64, dim=128, depth=4 |
| `tele_sensor_vibration` | telemetry | `SensorTransformer` | in\_channels=1, length=3200, patch\_len=64, dim=128, depth=4 |
| `tele_bio_ecg` | telemetry | `SensorTransformer` | in\_channels=1, length=2048, patch\_len=32, dim=128, depth=4, score\_head=True |
| `tele_regime_shift` | telemetry | `RegimeShiftTransformer` | in\_channels=8, length=512, patch\_len=8, dim=128, depth=4 |
| `graph_node_embedding` | graph | `GCNAutoencoder` | in\_dim=64, hidden=128, out\_dim=128, layers=3 |
| `graph_subgraph_embedding` | graph | `GraphTransformerEncoder` | in\_dim=64, dim=128, depth=4, heads=4 |
| `graph_fraud_rings` | graph | `NodeAnomalyGCN` | in\_dim=64, hidden=128, layers=3 |
| `graph_link_prediction` | graph | `LinkPredictor` | in\_dim=64, hidden=128, layers=3 |
| `chem_mol_embedding` | chem | `HybridMoleculeEncoder` | vocab\_size=128, atom\_dim=32, dim=256 |
| `chem_logp`, `chem_solubility` | chem | `MoleculePropertyModel` | vocab\_size=128, atom\_dim=32, dim=256, n\_out=1 |
| `chem_toxicity` | chem | `MoleculePropertyModel` | n\_out=13, activation="sigmoid" |

For the inputs and outputs of each model, see [Model architectures](https://khaos-ml.mintlify.site/usage/architectures.md).

## Classes by domain

Each `khaos.<domain>.models` package also exports the model classes, `ARCHITECTURES`, `build`
and `export_artifact`.

| Package | Exports |
| - | - |
| `khaos.finance.models` | `FilingNER`, `DEFAULT_ENCODER`, `ENTITY_TYPES`, `bio_labels`, `chunk_ids`, `decode_entities` |
| `khaos.physics.models` | `FNO2d`, `TimeConditionedFNO2d`, `SpectralConv2d`, `Normalized` |
| `khaos.robotics.models` | `TrajectoryTransformer`, `SkillSegmenter`, `FailureDetector`, `NormalizedTraj` |
| `khaos.telemetry.models` | `SensorTransformer`, `SensorLSTMAutoencoder`, `RegimeShiftTransformer` |
| `khaos.graph.models` | `GCNEncoder`, `GCNAutoencoder`, `GraphTransformerEncoder`, `NodeAnomalyGCN`, `LinkPredictor`, `normalize_adj` |
| `khaos.chem.models` | `SmilesTransformer`, `MolGraphEncoder`, `HybridMoleculeEncoder`, `MoleculePropertyModel` |
| `khaos.geometry.models` | `DGCNNClassifier`, `MeshEncoder`, `PointCloudEncoder`, `EdgeConv`, `knn_indices`, `edge_features` |
| `khaos.tabular.models` | `ColumnEncoder`, `ColumnTypeCNN`, `ColumnPairMatcher`, `RecordMatcher`, `TabularAnomalyTransformer`, `DEFAULT_ENCODER` |

### Normalization wrappers

Trained physics and robotics artifacts wrap the network so serving takes raw values:

| Class | Wraps | What it does inside the exported model |
| - | - | - |
| `khaos.physics.models.Normalized(model, x_mean, x_std, y_mean, y_std, log_channels=(), p_mean=None, p_std=None, t_scale=1.0)` | `FNO2d`, `TimeConditionedFNO2d` | Log-transforms the listed input channels, standardizes inputs, parameters and time, and rescales the output. |
| `khaos.robotics.models.NormalizedTraj(model, mean, std)` | the three trajectory models | Standardizes joint positions. |

## `khaos.layers`

Transformer parts that keep batch size and sequence length dynamic when exported to ONNX.
`torch.nn.MultiheadAttention` bakes the traced sequence length into the graph; these do not.

| Name | Description |
| - | - |
| `SelfAttention(dim, heads)` | Multi-head self-attention taking an optional additive bias. |
| `TransformerBlock(dim, heads, ff_mult=4, dropout=0.0)` | Pre-norm block: attention plus a feed-forward layer. `forward(x, bias=None)`. |
| `padding_bias(mask)` | `(B, T)` mask with 1 for real tokens to an additive bias `(B, 1, 1, T)`. |
| `causal_bias(mask)` | Causal plus padding bias `(B, 1, T, T)`, built from the mask so it stays shape-dynamic. |
| `NEG` | The large negative number used to mask attention (`-1e9`). |


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