# answerdotai/fsdp_qlora architecture

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

- Interactive diagram: https://gitdiagram.com/answerdotai/fsdp_qlora
- Repository: https://github.com/answerdotai/fsdp_qlora

## Overview

#### Purpose and Entry Points
`fsdp_qlora` is an alpha CLI training system for fine-tuning Hugging Face LLMs with PyTorch Fully Sharded Data Parallel (FSDP), including full tuning, LoRA/DoRA adapters, and 4-bit bitsandbytes or HQQ quantization. The primary entry point is `train.py`; launch wrappers target local, benchmark, and SLURM/multi-node execution: `train.sh`, `fsdp_multi_node.sh`, and `benchmarking/large_gpu_benchmarking.sh`. `hf_train.py` is a separate TRL SFTTrainer comparison path.

#### Distributed Training Orchestration
`train.py` owns argument-driven selection of model, dataset, precision, train type, checkpointing, CPU offload, logging, and world size. It initializes distributed GPU workers and applies FSDP sharding/wrapping around supported Transformer layers. FSDP is the central distributed boundary: model states, gradients, and optimizer work are partitioned across CUDA ranks, with optional CPU offload and synchronized module state.
Paths: `train.py`, `fsdp_multi_node.sh`, `train.sh`.

#### Model Loading and Quantized Initialization
For QLoRA/HQQ modes, the system bypasses ordinary Transformers pretrained loading limitations with custom low-memory loading: weights are loaded iteratively, quantized on GPU, then returned to CPU or meta-device while FSDP initialization synchronizes quantization state. This avoids materializing a full quantized model on one GPU before sharding. Bitsandbytes supplies NF4 `Linear4bit`; HQQ is an optional alternative with custom CUDA kernels.
Paths: `train.py`, `nbs/02-qlora-memeff-loading.ipynb`, `train_hqq_bench.sh`.

#### Adapter and Fine-Tuning Strategies
Training mode dispatch distinguishes full parameter updates from PEFT LoRA, custom LoRA, custom DoRA, bitsandbytes QLoRA/DoRA, HQQ LoRA/DoRA, and Llama-Pro variants. Custom adapter implementations are repo-owned and are the extension boundary for injected trainable layers over frozen/quantized base weights.
Paths: `scripts/lora.py`, `scripts/dora.py`, `train.py`.

#### Llama-Pro Model Expansion
Llama-Pro support is a preprocessing stage rather than a training runtime feature: it expands a base model’s blocks and writes derived weights, which `train.py` later receives through `--llama_pro_path` for BnB or HQQ training modes.
Paths: `scripts/block_expansion.py`, `train.py`, `tests/test_block_expansion.py`.

#### Training Data, Execution, and Persistence Flow
The main flow is CLI options → distributed/model setup → dataset tokenization and batched training → forward/backward/optimizer under selected precision and FSDP → metrics logging and optional adapter-state save. Saved custom quantized modes contain trainable LoRA weights only; inference must reload and quantize the base model separately. Conversion toward standard Hugging Face artifacts is documented in a notebook.
Paths: `train.py`, `Converting the State Dict.ipynb`, `profiling_utils.py`.

#### Runtime and External Dependencies
The runtime is Python with PyTorch 2.2+ on CUDA, FSDP, Transformers, and Hugging Face model/dataset access (including authenticated Llama access). Optional integrations are PEFT, bitsandbytes, HQQ/CUDA kernels, Weights & Biases, TRL (`hf_train.py`), and SLURM. Native FlashAttention 2 is available through recent PyTorch CUDA builds.
Paths: `hf_train.py`, `fsdp_multi_node.sh`, `profile.sh`.

Main entry points: [`train.py`](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py) (Training CLI), [`nbs/02-qlora-memeff-loading.ipynb`](https://github.com/answerdotai/fsdp_qlora/blob/main/nbs/02-qlora-memeff-loading.ipynb) (Memory-efficient quantization), [`train.sh`](https://github.com/answerdotai/fsdp_qlora/blob/main/train.sh) (Local launcher), [`fsdp_multi_node.sh`](https://github.com/answerdotai/fsdp_qlora/blob/main/fsdp_multi_node.sh) (Multi-node launcher)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_launch["Launch &amp; orchestration"]
  node_local_launcher["Local launcher<br/>shell wrapper<br/>[train.sh]"]
  node_multinode_launcher["Multi-node launcher<br/>SLURM launcher<br/>[fsdp_multi_node.sh]"]
  node_benchmark_launcher["Large GPU benchmark<br/>benchmark wrapper"]
  node_train_cli["Training CLI<br/>Python entry point<br/>[train.py]"]
end

subgraph group_training["Distributed training"]
  node_distributed_setup(("CUDA rank setup<br/>distributed runtime<br/>[train.py]"))
  node_fsdp{{"FSDP boundary<br/>PyTorch FSDP<br/>[train.py]"}}
  node_model_loading["Model loading<br/>Transformers loading<br/>[train.py]"]
  node_quantized_loading["Memory-efficient quantization<br/>QLoRA/HQQ loader"]
  node_data_training["Tokenization &amp; training loop<br/>training execution<br/>[train.py]"]
  node_metrics["Metrics &amp; profiling<br/>observability<br/>[profiling_utils.py]"]
  node_checkpoint[("Adapter state save<br/>persistence")]
end

subgraph group_extensions["Training extensions"]
  node_adapter_dispatch["Fine-tuning mode dispatch<br/>strategy selection<br/>[train.py]"]
  node_lora["Custom LoRA<br/>adapter layers<br/>[lora.py]"]
  node_dora["Custom DoRA<br/>adapter layers<br/>[dora.py]"]
  node_llama_pro["Llama-Pro expansion<br/>model preprocessing<br/>[block_expansion.py]"]
  node_trl_path["TRL comparison path<br/>SFTTrainer entry point<br/>[hf_train.py]"]
end

node_local_launcher -->|"launches"| node_train_cli
node_multinode_launcher -->|"launches"| node_train_cli
node_benchmark_launcher -->|"launches"| node_train_cli
node_train_cli -->|"initializes"| node_distributed_setup
node_distributed_setup -->|"forms ranks"| node_fsdp
node_train_cli -->|"configures"| node_model_loading
node_model_loading -->|"for QLoRA/HQQ"| node_quantized_loading
node_quantized_loading -->|"synchronizes state"| node_fsdp
node_train_cli -->|"selects mode"| node_adapter_dispatch
node_adapter_dispatch -->|"custom LoRA"| node_lora
node_adapter_dispatch -->|"custom DoRA"| node_dora
node_llama_pro -->|"derived model path"| node_model_loading
node_model_loading -->|"wrapped and sharded"| node_fsdp
node_adapter_dispatch -->|"defines trainable layers"| node_data_training
node_fsdp -->|"distributed execution"| node_data_training
node_data_training -->|"emits metrics"| node_metrics
node_data_training -->|"saves adapter state"| node_checkpoint
node_trl_path -.->|"alternative SFT path"| node_data_training

click node_local_launcher "https://github.com/answerdotai/fsdp_qlora/blob/main/train.sh"
click node_multinode_launcher "https://github.com/answerdotai/fsdp_qlora/blob/main/fsdp_multi_node.sh"
click node_benchmark_launcher "https://github.com/answerdotai/fsdp_qlora/blob/main/benchmarking/large_gpu_benchmarking.sh"
click node_train_cli "https://github.com/answerdotai/fsdp_qlora/blob/main/train.py"
click node_distributed_setup "https://github.com/answerdotai/fsdp_qlora/blob/main/train.py"
click node_fsdp "https://github.com/answerdotai/fsdp_qlora/blob/main/train.py"
click node_model_loading "https://github.com/answerdotai/fsdp_qlora/blob/main/train.py"
click node_quantized_loading "https://github.com/answerdotai/fsdp_qlora/blob/main/nbs/02-qlora-memeff-loading.ipynb"
click node_adapter_dispatch "https://github.com/answerdotai/fsdp_qlora/blob/main/train.py"
click node_lora "https://github.com/answerdotai/fsdp_qlora/blob/main/scripts/lora.py"
click node_dora "https://github.com/answerdotai/fsdp_qlora/blob/main/scripts/dora.py"
click node_llama_pro "https://github.com/answerdotai/fsdp_qlora/blob/main/scripts/block_expansion.py"
click node_data_training "https://github.com/answerdotai/fsdp_qlora/blob/main/train.py"
click node_metrics "https://github.com/answerdotai/fsdp_qlora/blob/main/profiling_utils.py"
click node_checkpoint "https://github.com/answerdotai/fsdp_qlora/blob/main/Converting%20the%20State%20Dict.ipynb"
click node_trl_path "https://github.com/answerdotai/fsdp_qlora/blob/main/hf_train.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_local_launcher,node_multinode_launcher,node_benchmark_launcher,node_train_cli toneBlue
class node_distributed_setup,node_fsdp,node_model_loading,node_quantized_loading,node_data_training,node_metrics,node_checkpoint toneAmber
class node_adapter_dispatch,node_lora,node_dora,node_llama_pro,node_trl_path toneMint
```

## Components

### Launch & orchestration

- [Local launcher](https://github.com/answerdotai/fsdp_qlora/blob/main/train.sh): shell wrapper · `train.sh`
- [Multi-node launcher](https://github.com/answerdotai/fsdp_qlora/blob/main/fsdp_multi_node.sh): SLURM launcher · `fsdp_multi_node.sh`
- [Large GPU benchmark](https://github.com/answerdotai/fsdp_qlora/blob/main/benchmarking/large_gpu_benchmarking.sh): benchmark wrapper · `benchmarking/large_gpu_benchmarking.sh`
- [Training CLI](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py): Python entry point · Selects model, data, precision, training mode, and checkpoint behavior. · `train.py`

### Distributed training

- [CUDA rank setup](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py): distributed runtime · `train.py`
- [FSDP boundary](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py): PyTorch FSDP · Shards model, gradients, and optimizer state across ranks. · `train.py`
- [Model loading](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py): Transformers loading · `train.py`
- [Memory-efficient quantization](https://github.com/answerdotai/fsdp_qlora/blob/main/nbs/02-qlora-memeff-loading.ipynb): QLoRA/HQQ loader · Iteratively quantizes weights without materializing a full model on one GPU. · `nbs/02-qlora-memeff-loading.ipynb`
- [Tokenization & training loop](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py): training execution · Batches data and runs forward, backward, and optimizer steps. · `train.py`
- [Metrics & profiling](https://github.com/answerdotai/fsdp_qlora/blob/main/profiling_utils.py): observability · `profiling_utils.py`
- [Adapter state save](https://github.com/answerdotai/fsdp_qlora/blob/main/Converting%20the%20State%20Dict.ipynb): persistence · Custom quantized modes save trainable LoRA weights only. · `Converting the State Dict.ipynb`

### Training extensions

- [Fine-tuning mode dispatch](https://github.com/answerdotai/fsdp_qlora/blob/main/train.py): strategy selection · Chooses full tuning, PEFT, custom, BnB, HQQ, or Llama-Pro modes. · `train.py`
- [Custom LoRA](https://github.com/answerdotai/fsdp_qlora/blob/main/scripts/lora.py): adapter layers · Repo-owned trainable layers over frozen base weights. · `scripts/lora.py`
- [Custom DoRA](https://github.com/answerdotai/fsdp_qlora/blob/main/scripts/dora.py): adapter layers · `scripts/dora.py`
- [Llama-Pro expansion](https://github.com/answerdotai/fsdp_qlora/blob/main/scripts/block_expansion.py): model preprocessing · Expands model blocks and writes derived weights before training. · `scripts/block_expansion.py`
- [TRL comparison path](https://github.com/answerdotai/fsdp_qlora/blob/main/hf_train.py): SFTTrainer entry point · `hf_train.py`

## Connections

- Local launcher → Training CLI: launches
- Multi-node launcher → Training CLI: launches
- Large GPU benchmark → Training CLI: launches
- Training CLI → CUDA rank setup: initializes
- CUDA rank setup → FSDP boundary: forms ranks
- Training CLI → Model loading: configures
- Model loading → Memory-efficient quantization: for QLoRA/HQQ
- Memory-efficient quantization → FSDP boundary: synchronizes state
- Training CLI → Fine-tuning mode dispatch: selects mode
- Fine-tuning mode dispatch → Custom LoRA: custom LoRA
- Fine-tuning mode dispatch → Custom DoRA: custom DoRA
- Llama-Pro expansion → Model loading: derived model path
- Model loading → FSDP boundary: wrapped and sharded
- Fine-tuning mode dispatch → Tokenization & training loop: defines trainable layers
- FSDP boundary → Tokenization & training loop: distributed execution
- Tokenization & training loop → Metrics & profiling: emits metrics
- Tokenization & training loop → Adapter state save: saves adapter state
- TRL comparison path → Tokenization & training loop: alternative SFT path
