# replicate/flux-fine-tuner architecture

> Architecture diagram of the GitHub repository replicate/flux-fine-tuner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/replicate/flux-fine-tuner
- Repository: https://github.com/replicate/flux-fine-tuner

## Overview

flux-fine-tuner provides Cog-based FLUX LoRA training and inference. The training workflow accepts images and configuration, optionally captions the images, configures and runs ai-toolkit training, then returns weights and can upload them to Hugging Face or log training activity to Weights & Biases. Inference loads model pipelines, resolves and caches LoRA weights, generates or edits images, and applies safety checks. The README documents these major capabilities; some connections are supported only by its statements or the sampled excerpts.

Main entry points: [`train.py`](https://github.com/replicate/flux-fine-tuner/blob/main/train.py) (Training API), [`predict.py`](https://github.com/replicate/flux-fine-tuner/blob/main/predict.py) (Inference API), [`ai-toolkit`](https://github.com/replicate/flux-fine-tuner/tree/main/ai-toolkit) (Training job), [`wandb_client.py`](https://github.com/replicate/flux-fine-tuner/blob/main/wandb_client.py) (W&B logging)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_training["LoRA training"]
  node_train_api["Training API<br/>[train.py]"]
  node_image_dataset["Image dataset<br/>[train.py]"]
  node_toolkit_job["Training job"]
  node_trained_weights["Trained LoRA<br/>[train.py]"]
end

subgraph group_training_support["Training support"]
  node_captioner["Auto-captioner<br/>[caption.py]"]
  node_layer_selection["Layer selection<br/>[layer_match.py]"]
  node_runtime_patches["Runtime patches"]
  node_wandb_client["W&amp;B logging<br/>[wandb_client.py]"]
end

subgraph group_inference["Image inference"]
  node_inference_api["Inference API<br/>[predict.py]"]
  node_flux_pipelines["FLUX pipelines<br/>[predict.py]"]
  node_generated_images["Generated images<br/>[predict.py]"]
end

subgraph group_inference_support["Inference support"]
  node_weight_cache[("Weight download cache<br/>[weights.py]")]
  node_lora_loader["LoRA loader patch"]
  node_safety_checks["Safety checks<br/>[predict.py]"]
end

node_user(("Model user"))
node_hugging_face["Hugging Face"]
node_replicate["Replicate"]

node_user -->|"submits training"| node_train_api
node_train_api -->|"extracts images"| node_image_dataset
node_train_api -->|"captions images"| node_captioner
node_train_api -->|"selects layers"| node_layer_selection
node_train_api -->|"runs job"| node_toolkit_job
node_train_api -.->|"applies patches"| node_runtime_patches
node_toolkit_job -.->|"trains on"| node_image_dataset
node_toolkit_job -.->|"saves weights"| node_trained_weights
node_train_api -->|"logs training"| node_wandb_client
node_wandb_client -->|"saves weights"| node_trained_weights
node_train_api -->|"optionally uploads"| node_hugging_face
node_train_api -->|"returns weights"| node_user
node_user -->|"requests generation"| node_inference_api
node_inference_api -->|"ensures weights"| node_weight_cache
node_inference_api -->|"selects pipeline"| node_flux_pipelines
node_inference_api -->|"patches loading"| node_lora_loader
node_inference_api -->|"checks images"| node_safety_checks
node_inference_api -->|"returns images"| node_generated_images
node_inference_api -.->|"serves predictions"| node_replicate
node_generated_images -->|"delivers images"| node_user

click node_train_api "https://github.com/replicate/flux-fine-tuner/blob/main/train.py"
click node_image_dataset "https://github.com/replicate/flux-fine-tuner/blob/main/train.py"
click node_captioner "https://github.com/replicate/flux-fine-tuner/blob/main/caption.py"
click node_layer_selection "https://github.com/replicate/flux-fine-tuner/blob/main/layer_match.py"
click node_toolkit_job "https://github.com/replicate/flux-fine-tuner/tree/main/ai-toolkit"
click node_runtime_patches "https://github.com/replicate/flux-fine-tuner/blob/main/submodule_patches.py"
click node_wandb_client "https://github.com/replicate/flux-fine-tuner/blob/main/wandb_client.py"
click node_trained_weights "https://github.com/replicate/flux-fine-tuner/blob/main/train.py"
click node_inference_api "https://github.com/replicate/flux-fine-tuner/blob/main/predict.py"
click node_weight_cache "https://github.com/replicate/flux-fine-tuner/blob/main/weights.py"
click node_flux_pipelines "https://github.com/replicate/flux-fine-tuner/blob/main/predict.py"
click node_lora_loader "https://github.com/replicate/flux-fine-tuner/blob/main/lora_loading_patch.py"
click node_safety_checks "https://github.com/replicate/flux-fine-tuner/blob/main/predict.py"
click node_generated_images "https://github.com/replicate/flux-fine-tuner/blob/main/predict.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_train_api,node_image_dataset,node_toolkit_job,node_trained_weights,node_user toneBlue
class node_captioner,node_layer_selection,node_runtime_patches,node_wandb_client toneAmber
class node_inference_api,node_flux_pipelines,node_generated_images toneMint
class node_weight_cache,node_lora_loader,node_safety_checks toneRose
class node_hugging_face,node_replicate toneIndigo
```

## Components

### LoRA training

- [Training API](https://github.com/replicate/flux-fine-tuner/blob/main/train.py): `train.py`
- [Image dataset](https://github.com/replicate/flux-fine-tuner/blob/main/train.py): `train.py`
- [Training job](https://github.com/replicate/flux-fine-tuner/tree/main/ai-toolkit): `ai-toolkit`
- [Trained LoRA](https://github.com/replicate/flux-fine-tuner/blob/main/train.py): `train.py`

### Training support

- [Auto-captioner](https://github.com/replicate/flux-fine-tuner/blob/main/caption.py): `caption.py`
- [Layer selection](https://github.com/replicate/flux-fine-tuner/blob/main/layer_match.py): `layer_match.py`
- [Runtime patches](https://github.com/replicate/flux-fine-tuner/blob/main/submodule_patches.py): `submodule_patches.py`
- [W&B logging](https://github.com/replicate/flux-fine-tuner/blob/main/wandb_client.py): `wandb_client.py`

### Image inference

- [Inference API](https://github.com/replicate/flux-fine-tuner/blob/main/predict.py): `predict.py`
- [FLUX pipelines](https://github.com/replicate/flux-fine-tuner/blob/main/predict.py): `predict.py`
- [Generated images](https://github.com/replicate/flux-fine-tuner/blob/main/predict.py): `predict.py`

### Inference support

- [Weight download cache](https://github.com/replicate/flux-fine-tuner/blob/main/weights.py): `weights.py`
- [LoRA loader patch](https://github.com/replicate/flux-fine-tuner/blob/main/lora_loading_patch.py): `lora_loading_patch.py`
- [Safety checks](https://github.com/replicate/flux-fine-tuner/blob/main/predict.py): `predict.py`

### Other components

- **Model user**
- **Hugging Face**
- **Replicate**

## Connections

- Model user → Training API: submits training (evidence: `README.md`)
- Training API → Image dataset: extracts images (evidence: `train.py`)
- Training API → Auto-captioner: captions images (evidence: `train.py`)
- Training API → Layer selection: selects layers (evidence: `train.py`)
- Training API → Training job: runs job (evidence: `train.py`)
- Training API → Runtime patches: applies patches (evidence: `train.py`)
- Training job → Image dataset: trains on
- Training job → Trained LoRA: saves weights
- Training API → W&B logging: logs training (evidence: `train.py`)
- W&B logging → Trained LoRA: saves weights (evidence: `wandb_client.py`)
- Training API → Hugging Face: optionally uploads (evidence: `README.md`)
- Training API → Model user: returns weights (evidence: `train.py`)
- Model user → Inference API: requests generation (evidence: `README.md`)
- Inference API → Weight download cache: ensures weights (evidence: `predict.py`)
- Inference API → FLUX pipelines: selects pipeline (evidence: `predict.py`)
- Inference API → LoRA loader patch: patches loading (evidence: `predict.py`)
- Inference API → Safety checks: checks images (evidence: `predict.py`)
- Inference API → Generated images: returns images (evidence: `predict.py`)
- Inference API → Replicate: serves predictions
- Generated images → Model user: delivers images (evidence: `README.md`)
