# chaidiscovery/chai-lab architecture

> Architecture diagram of the GitHub repository chaidiscovery/chai-lab, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chaidiscovery/chai-lab
- Repository: https://github.com/chaidiscovery/chai-lab

## Overview

#### Purpose and entry points
Chai-1 is a GPU/CUDA Python inference package for multimodal molecular structure prediction: proteins, small molecules, nucleic acids, glycans, covalent complexes, templates, MSAs, and experimental restraints. Primary APIs are `chai_lab.chai1.run_inference` and advanced context-based folding through `run_folding_on_context` in `chai_lab/chai1.py`. CLI packaging/commands are rooted in `chai_lab/main.py`; example invocation is in `examples/predict_structure.py`.

#### Input parsing and validation
FASTA and auxiliary inputs are parsed into typed molecular entities and constraints before inference. The parser handles protein/nucleic-acid sequences, ligand SMILES, glycans, structure data, MSA formats, templates, and restraint specifications. Key boundaries: user files become normalized internal entity/context representations.
Paths: `chai_lab/data/parsing/fasta.py`, `chai_lab/data/parsing/input_validation.py`, `chai_lab/data/parsing/restraints.py`.

#### Structural context assembly
`AllAtomFeatureContext` is the central inference payload, combining tokenized all-atom structure, optional embeddings, MSAs, template information, and restraint/covalent-bond data. `inference_dataset` builds model-ready examples from parsed inputs; atom tokenization establishes a shared representation across molecular modalities.
Paths: `chai_lab/data/dataset/all_atom_feature_context.py`, `chai_lab/data/dataset/inference_dataset.py`, `chai_lab/data/dataset/structure/all_atom_residue_tokenizer.py`.

#### Enrichment sources: MSA, templates, embeddings
Optional enrichments are separate from core sequence input. MSAs can be supplied as aligned parquet or obtained through a ColabFold/MMseqs2 server, then preprocessed into MSA context. Templates are selected from m8 hits and loaded from local CIFs or downloaded RCSB structures. ESM embedding support is represented as an embedding context.
Paths: `chai_lab/data/dataset/msas/colabfold.py`, `chai_lab/data/dataset/templates/load.py`, `chai_lab/data/dataset/embeddings/esm.py`.

#### Feature generation and batching
The feature factory converts assembled contexts into model tensors via specialized generators for residue/atom identity, relative positions, bonds, MSA/template features, docking, and distance/pocket restraints. Collation batches examples while preserving the model’s structured atom/token/pair inputs.
Paths: `chai_lab/data/features/feature_factory.py`, `chai_lab/data/features/generators/`, `chai_lab/data/collate/collate.py`.

#### Model execution and sampling
Inference orchestration in `chai1.py` downloads/locates weights, applies model and diffusion schedule utilities, generates multiple predicted structures, and writes output artifacts. The runtime requires Linux, Python 3.10+, CUDA, and bfloat16-capable GPUs; weight storage can be redirected with `CHAI_DOWNLOADS_DIR`.
Paths: `chai_lab/chai1.py`, `chai_lab/model/diffusion_schedules.py`, `chai_lab/model/utils.py`.

#### Output, ranking, and external boundaries
Predicted structures are serialized as PDB/CIF-compatible outputs and ranked using confidence and geometry signals including pLDDT, pTM, frames, and clash detection. External-service boundaries are ColabFold/MMseqs2 for automatic MSAs and RCSB for template CIF retrieval; local template overrides use `CHAI_TEMPLATE_CIF_FOLDER`.
Paths: `chai_lab/ranking/rank.py`, `chai_lab/ranking/clashes.py`, `chai_lab/data/io/rcsb.py`.

Main entry points: [`chai_lab/data/dataset/all_atom_feature_context.py`](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/all_atom_feature_context.py) (AllAtomFeatureContext), [`chai_lab/chai1.py`](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/chai1.py) (Inference orchestration), [`chai_lab/data/parsing/input_validation.py`](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/input_validation.py) (Input validation), [`chai_lab/data/dataset/inference_dataset.py`](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/inference_dataset.py) (Inference dataset)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_api["Inference APIs<br/>Python API<br/>[chai1.py]"]
  node_cli["CLI commands<br/>[main.py]"]
  node_example["Prediction example<br/>example script"]
end

subgraph group_input["Input and enrichment"]
  node_fasta["FASTA parser<br/>input parser<br/>[fasta.py]"]
  node_validation["Input validation<br/>validator"]
  node_restraints["Restraint parser<br/>constraint parser<br/>[restraints.py]"]
  node_msa["MSA acquisition<br/>MSA source<br/>[colabfold.py]"]
  node_templates["Template loader<br/>template source<br/>[load.py]"]
  node_embeddings["ESM embeddings<br/>embedding context<br/>[esm.py]"]
  node_colabfold{{"ColabFold/MMseqs2<br/>external service"}}
  node_rcsb{{"RCSB<br/>external service<br/>[rcsb.py]"}}
end

subgraph group_assembly["Context and features"]
  node_tokenizer["All-atom tokenizer<br/>structure tokenizer"]
  node_dataset["Inference dataset<br/>example builder"]
  node_context["AllAtomFeatureContext<br/>inference payload"]
  node_features["Feature factory<br/>tensor features<br/>[feature_factory.py]"]
  node_generators["Feature generators<br/>feature layers"]
  node_collate["Structured collation<br/>batch builder<br/>[collate.py]"]
end

subgraph group_runtime["Inference runtime"]
  node_orchestrator["Inference orchestration<br/>CUDA runtime<br/>[chai1.py]"]
  node_diffusion["Diffusion schedules<br/>sampling schedule"]
  node_model_utils["Model utilities<br/>model runtime<br/>[utils.py]"]
end

subgraph group_output["Outputs"]
  node_ranking["Confidence ranking<br/>structure ranking<br/>[rank.py]"]
  node_clashes["Clash detection<br/>geometry check<br/>[clashes.py]"]
  node_structures["Structure artifacts<br/>PDB/CIF outputs"]
end

node_cli -->|"invokes"| node_api
node_example -->|"uses"| node_api
node_api -->|"parses inputs"| node_fasta
node_fasta -->|"typed entities"| node_validation
node_restraints -->|"constraints"| node_validation
node_colabfold -.->|"alignments"| node_msa
node_rcsb -.->|"template CIFs"| node_templates
node_validation -->|"normalized inputs"| node_dataset
node_tokenizer -->|"atom tokens"| node_dataset
node_msa -->|"MSA context"| node_context
node_templates -->|"template context"| node_context
node_embeddings -->|"embedding context"| node_context
node_dataset -->|"assembled example"| node_context
node_context -->|"feature inputs"| node_features
node_generators -->|"specialized features"| node_features
node_features -->|"model tensors"| node_collate
node_collate -->|"batched inputs"| node_orchestrator
node_diffusion -->|"sampling schedule"| node_orchestrator
node_model_utils -->|"model support"| node_orchestrator
node_orchestrator -->|"predicted structures"| node_ranking
node_clashes -->|"geometry signal"| node_ranking
node_ranking -->|"ranked PDB/CIF"| node_structures

click node_api "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/chai1.py"
click node_cli "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/main.py"
click node_example "https://github.com/chaidiscovery/chai-lab/blob/main/examples/predict_structure.py"
click node_fasta "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/fasta.py"
click node_validation "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/input_validation.py"
click node_restraints "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/restraints.py"
click node_msa "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/msas/colabfold.py"
click node_templates "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/templates/load.py"
click node_embeddings "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/embeddings/esm.py"
click node_rcsb "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/io/rcsb.py"
click node_tokenizer "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/structure/all_atom_residue_tokenizer.py"
click node_dataset "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/inference_dataset.py"
click node_context "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/all_atom_feature_context.py"
click node_features "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/features/feature_factory.py"
click node_collate "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/collate/collate.py"
click node_orchestrator "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/chai1.py"
click node_diffusion "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/model/diffusion_schedules.py"
click node_model_utils "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/model/utils.py"
click node_ranking "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/ranking/rank.py"
click node_clashes "https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/ranking/clashes.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api,node_cli,node_example toneBlue
class node_fasta,node_validation,node_restraints,node_msa,node_templates,node_embeddings,node_colabfold,node_rcsb toneAmber
class node_tokenizer,node_dataset,node_context,node_features,node_generators,node_collate toneMint
class node_orchestrator,node_diffusion,node_model_utils toneRose
class node_ranking,node_clashes,node_structures toneIndigo
```

## Components

### Entry points

- [Inference APIs](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/chai1.py): Python API · run_inference and run_folding_on_context. · `chai_lab/chai1.py`
- [CLI commands](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/main.py): CLI · `chai_lab/main.py`
- [Prediction example](https://github.com/chaidiscovery/chai-lab/blob/main/examples/predict_structure.py): example script · `examples/predict_structure.py`

### Input and enrichment

- [FASTA parser](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/fasta.py): input parser · Parses molecular sequence inputs into typed entities. · `chai_lab/data/parsing/fasta.py`
- [Input validation](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/input_validation.py): validator · Normalizes supported molecular and auxiliary inputs. · `chai_lab/data/parsing/input_validation.py`
- [Restraint parser](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/parsing/restraints.py): constraint parser · `chai_lab/data/parsing/restraints.py`
- [MSA acquisition](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/msas/colabfold.py): MSA source · Uses supplied alignments or ColabFold/MMseqs2. · `chai_lab/data/dataset/msas/colabfold.py`
- [Template loader](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/templates/load.py): template source · Loads local CIFs or retrieved template structures. · `chai_lab/data/dataset/templates/load.py`
- [ESM embeddings](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/embeddings/esm.py): embedding context · `chai_lab/data/dataset/embeddings/esm.py`
- **ColabFold/MMseqs2**: external service · Automatic MSA service boundary.
- [RCSB](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/io/rcsb.py): external service · Template CIF retrieval boundary. · `chai_lab/data/io/rcsb.py`

### Context and features

- [All-atom tokenizer](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/structure/all_atom_residue_tokenizer.py): structure tokenizer · Shared token representation across molecular modalities. · `chai_lab/data/dataset/structure/all_atom_residue_tokenizer.py`
- [Inference dataset](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/inference_dataset.py): example builder · Builds model-ready examples from parsed inputs. · `chai_lab/data/dataset/inference_dataset.py`
- [AllAtomFeatureContext](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/dataset/all_atom_feature_context.py): inference payload · Central context for structure, MSA, templates, embeddings, and constraints. · `chai_lab/data/dataset/all_atom_feature_context.py`
- [Feature factory](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/features/feature_factory.py): tensor features · Generates atom, token, pair, MSA, template, and restraint features. · `chai_lab/data/features/feature_factory.py`
- **Feature generators**: feature layers
- [Structured collation](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/data/collate/collate.py): batch builder · Batches structured atom, token, and pair inputs. · `chai_lab/data/collate/collate.py`

### Inference runtime

- [Inference orchestration](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/chai1.py): CUDA runtime · Locates weights, executes sampling, and writes artifacts. · `chai_lab/chai1.py`
- [Diffusion schedules](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/model/diffusion_schedules.py): sampling schedule · `chai_lab/model/diffusion_schedules.py`
- [Model utilities](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/model/utils.py): model runtime · `chai_lab/model/utils.py`

### Outputs

- [Confidence ranking](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/ranking/rank.py): structure ranking · Ranks predictions using confidence and geometry signals. · `chai_lab/ranking/rank.py`
- [Clash detection](https://github.com/chaidiscovery/chai-lab/blob/main/chai_lab/ranking/clashes.py): geometry check · `chai_lab/ranking/clashes.py`
- **Structure artifacts**: PDB/CIF outputs · Serialized predicted structures and ranked results.

## Connections

- CLI commands → Inference APIs: invokes
- Prediction example → Inference APIs: uses
- Inference APIs → FASTA parser: parses inputs
- FASTA parser → Input validation: typed entities
- Restraint parser → Input validation: constraints
- ColabFold/MMseqs2 → MSA acquisition: alignments
- RCSB → Template loader: template CIFs
- Input validation → Inference dataset: normalized inputs
- All-atom tokenizer → Inference dataset: atom tokens
- MSA acquisition → AllAtomFeatureContext: MSA context
- Template loader → AllAtomFeatureContext: template context
- ESM embeddings → AllAtomFeatureContext: embedding context
- Inference dataset → AllAtomFeatureContext: assembled example
- AllAtomFeatureContext → Feature factory: feature inputs
- Feature generators → Feature factory: specialized features
- Feature factory → Structured collation: model tensors
- Structured collation → Inference orchestration: batched inputs
- Diffusion schedules → Inference orchestration: sampling schedule
- Model utilities → Inference orchestration: model support
- Inference orchestration → Confidence ranking: predicted structures
- Clash detection → Confidence ranking: geometry signal
- Confidence ranking → Structure artifacts: ranked PDB/CIF
