# nabin2004/dhant architecture

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

- Interactive diagram: https://gitdiagram.com/nabin2004/dhant
- Repository: https://github.com/nabin2004/dhant

## Overview

1. **Repository purpose**
   - `dhant` is a lightweight Python package for learning post-training workflows. It is not a full production trainer stack yet; most components are educational placeholders that simulate training behavior while preserving a clean architecture.

2. **Core subsystems**
   - The package is split into three main runtime areas: `trainer/` for training orchestration, `rewards/` for reusable reward functions, and `cli/` for command-line entrypoints. `scripts/` contains runnable examples and quickstart flows.

3. **Trainer layer as the main orchestration boundary**
   - `src/dhant/trainer/` is the central subsystem. It contains a shared `base.py`, per-task config modules, and trainer implementations for:
     - SFT (`sft_trainer.py`, `sft_config.py`)
     - DPO (`dpo_trainer.py`, `dpo_config.py`)
     - GRPO (`grpo_trainer.py`, `grpo_config.py`)
     - Reward model training (`reward_trainer.py`, `reward_config.py`)
   - This layer is the primary place where datasets, models, reward functions, logging, and run outputs are coordinated.

4. **Task-specific training flows**
   - The trainers represent separate post-training pipelines:
     - **SFT** consumes a model and training dataset.
     - **DPO** is intended for preference-pair optimization.
     - **GRPO** connects model generation with reward scoring and aggregation.
     - **Reward training** is the dedicated path for fitting reward models.
   - The README indicates the current implementation is simulated, so these flows are architectural templates rather than fully connected ML pipelines.

5. **Reward subsystem**
   - `src/dhant/rewards/` defines a small reward-function boundary:
     - `base_reward.py` establishes the interface
     - `accuracy_reward.py`, `format_reward.py`, and `reasoning_reward.py` provide concrete reusable reward functions
   - These rewards are especially relevant to GRPO-style training, where trainer logic consumes one or more reward functions to score outputs.

6. **Adapters and parameter-efficient tuning**
   - `trainer/adapters.py` contains boilerplate configs for memory- and parameter-efficient fine-tuning, including quantization, LoRA, and QLoRA.
   - These are exposed both through Python APIs and CLI flags, making them a cross-cutting concern that affects trainer setup before any loop begins.

7. **CLI as the user-facing entry boundary**
   - `src/dhant/cli/main.py` is the operational entrypoint for end users.
   - The CLI maps subcommands to trainer workflows: `sft`, `dpo`, `grpo`, and `reward`.
   - It also carries run metadata and logging flags, so the CLI is the boundary where user intent becomes a configured training run.

8. **Run management, logging, and monitoring**
   - The project writes each run into an isolated experiment folder under `outputs/experiments/...`.
   - Each run contains logs, TensorBoard data, checkpoints, and artifacts such as `run_summary.json`.
   - This means the architecture includes a persistent run-recording layer, not just in-memory training output.
   - TensorBoard is an optional monitoring integration, enabled via the package’s monitoring extras.

9. **Error handling and execution semantics**
   - Training failures are wrapped in `TrainingExecutionError`.
   - The architecture explicitly separates user-facing failure reporting from detailed traceback capture: concise errors go to the CLI, while full diagnostics are stored per run in log files.
   - This suggests the trainer layer owns execution robustness, while the CLI owns exit status and user feedback.

10. **Supporting scripts and learning path**
   - `src/dhant/scripts/quickstart.py` is a lightweight runnable example that demonstrates the intended package usage outside the CLI.
   - The README’s learning path shows the project is designed for incremental replacement: start with simulated trainers, then add real loops, dataset adapters, preference handling, generation/reward aggregation, and evaluation.

11. **Validation and test coverage**
   - Tests focus on the architectural seams: `tests/test_adapters.py` and `tests/test_template_trainers.py`.
   - That indicates the repository is testing configuration and template behavior more than heavy model execution, which matches its template-oriented design.

12. **Important architecture diagram boundaries**
   - A good diagram should show:
     - external user input through CLI or Python API
     - trainer selection by task
     - optional adapter/config preprocessing
     - reward function injection into GRPO/reward workflows
     - run outputs flowing to experiment directories, logs, checkpoints, and TensorBoard
   - The major boundary is between orchestration/template code in this repo and the real ML backend that a future implementation would plug in.

Main entry points: [`src/dhant/trainer/grpo_trainer.py`](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/grpo_trainer.py) (GRPO Trainer), [`src/dhant/trainer/base.py`](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/base.py) (Trainer Base), [`src/dhant/cli/main.py`](https://github.com/nabin2004/dhant/blob/main/src/dhant/cli/main.py) (CLI), [`src/dhant/trainer/reward_trainer.py`](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/reward_trainer.py) (Reward Trainer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["Interface"]
  node_user(("User<br/>external actor"))
  node_cli_main["CLI<br/>entrypoint<br/>[main.py]"]
  node_quickstart["Quickstart<br/>example script<br/>[quickstart.py]"]
end

subgraph group_training["Training"]
  node_trainer_base["Trainer Base<br/>orchestration core<br/>[base.py]"]
  node_adapters["Adapters<br/>setup config<br/>[adapters.py]"]
  node_sft_trainer["SFT Trainer<br/>trainer template<br/>[sft_trainer.py]"]
  node_sft_config["SFT Config<br/>trainer config<br/>[sft_config.py]"]
  node_dpo_trainer["DPO Trainer<br/>trainer template<br/>[dpo_trainer.py]"]
  node_dpo_config["DPO Config<br/>trainer config<br/>[dpo_config.py]"]
  node_grpo_trainer["GRPO Trainer<br/>trainer template<br/>[grpo_trainer.py]"]
  node_grpo_config["GRPO Config<br/>trainer config<br/>[grpo_config.py]"]
  node_reward_trainer["Reward Trainer<br/>trainer template<br/>[reward_trainer.py]"]
  node_reward_config["Reward Config<br/>trainer config<br/>[reward_config.py]"]
end

subgraph group_rewards["Rewards"]
  node_reward_base["Reward Base<br/>reward interface<br/>[base_reward.py]"]
  node_accuracy_reward["Accuracy Reward<br/>reward function<br/>[accuracy_reward.py]"]
  node_format_reward["Format Reward<br/>reward function<br/>[format_reward.py]"]
  node_reasoning_reward["Reasoning Reward<br/>reward function"]
end

subgraph group_runtime["Runtime"]
  node_run_outputs[("Run Outputs<br/>artifact store")]
  node_tensorboard{{"TensorBoard<br/>monitoring integration"}}
end

node_user -->|"runs"| node_cli_main
node_user -->|"uses"| node_quickstart
node_cli_main -->|"selects"| node_sft_trainer
node_cli_main -->|"selects"| node_dpo_trainer
node_cli_main -->|"selects"| node_grpo_trainer
node_cli_main -->|"selects"| node_reward_trainer
node_quickstart -->|"demonstrates"| node_trainer_base
node_sft_trainer -->|"extends"| node_trainer_base
node_dpo_trainer -->|"extends"| node_trainer_base
node_grpo_trainer -->|"extends"| node_trainer_base
node_reward_trainer -->|"extends"| node_trainer_base
node_sft_trainer -->|"uses"| node_sft_config
node_dpo_trainer -->|"uses"| node_dpo_config
node_grpo_trainer -->|"uses"| node_grpo_config
node_reward_trainer -->|"uses"| node_reward_config
node_trainer_base -->|"prepares"| node_adapters
node_grpo_trainer -->|"scores with"| node_reward_base
node_grpo_trainer -->|"can use"| node_accuracy_reward
node_grpo_trainer -->|"can use"| node_format_reward
node_grpo_trainer -->|"can use"| node_reasoning_reward
node_reward_trainer -->|"trains against"| node_reward_base
node_sft_trainer -->|"writes"| node_run_outputs
node_dpo_trainer -->|"writes"| node_run_outputs
node_grpo_trainer -->|"writes"| node_run_outputs
node_reward_trainer -->|"writes"| node_run_outputs
node_run_outputs -->|"feeds"| node_tensorboard

click node_cli_main "https://github.com/nabin2004/dhant/blob/main/src/dhant/cli/main.py"
click node_quickstart "https://github.com/nabin2004/dhant/blob/main/src/dhant/scripts/quickstart.py"
click node_trainer_base "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/base.py"
click node_adapters "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/adapters.py"
click node_sft_trainer "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/sft_trainer.py"
click node_sft_config "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/sft_config.py"
click node_dpo_trainer "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/dpo_trainer.py"
click node_dpo_config "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/dpo_config.py"
click node_grpo_trainer "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/grpo_trainer.py"
click node_grpo_config "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/grpo_config.py"
click node_reward_trainer "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/reward_trainer.py"
click node_reward_config "https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/reward_config.py"
click node_reward_base "https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/base_reward.py"
click node_accuracy_reward "https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/accuracy_reward.py"
click node_format_reward "https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/format_reward.py"
click node_reasoning_reward "https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/reasoning_reward.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_user,node_cli_main,node_quickstart toneBlue
class node_trainer_base,node_adapters,node_sft_trainer,node_sft_config,node_dpo_trainer,node_dpo_config,node_grpo_trainer,node_grpo_config,node_reward_trainer,node_reward_config toneAmber
class node_reward_base,node_accuracy_reward,node_format_reward,node_reasoning_reward toneMint
class node_run_outputs,node_tensorboard toneRose
```

## Components

### Interface

User-facing entrypoints and example flow

- **User**: external actor · Invokes the package through the CLI or Python API
- [CLI](https://github.com/nabin2004/dhant/blob/main/src/dhant/cli/main.py): entrypoint · Maps subcommands into trainer workflows and run metadata · `src/dhant/cli/main.py`
- [Quickstart](https://github.com/nabin2004/dhant/blob/main/src/dhant/scripts/quickstart.py): example script · Runnable learning example outside the CLI · `src/dhant/scripts/quickstart.py`

### Training

Core orchestration and task-specific trainer templates

- [Trainer Base](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/base.py): orchestration core · Shared training boundary for execution, coordination, and errors · `src/dhant/trainer/base.py`
- [Adapters](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/adapters.py): setup config · Quantization, LoRA, and QLoRA setup helpers · `src/dhant/trainer/adapters.py`
- [SFT Trainer](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/sft_trainer.py): trainer template · Supervised fine-tuning orchestration template · `src/dhant/trainer/sft_trainer.py`
- [SFT Config](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/sft_config.py): trainer config · Configuration for SFT runs · `src/dhant/trainer/sft_config.py`
- [DPO Trainer](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/dpo_trainer.py): trainer template · Preference optimization orchestration template · `src/dhant/trainer/dpo_trainer.py`
- [DPO Config](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/dpo_config.py): trainer config · Configuration for DPO runs · `src/dhant/trainer/dpo_config.py`
- [GRPO Trainer](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/grpo_trainer.py): trainer template · Generation, reward scoring, and aggregation template · `src/dhant/trainer/grpo_trainer.py`
- [GRPO Config](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/grpo_config.py): trainer config · Configuration for GRPO runs · `src/dhant/trainer/grpo_config.py`
- [Reward Trainer](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/reward_trainer.py): trainer template · Dedicated reward model training template · `src/dhant/trainer/reward_trainer.py`
- [Reward Config](https://github.com/nabin2004/dhant/blob/main/src/dhant/trainer/reward_config.py): trainer config · Configuration for reward model runs · `src/dhant/trainer/reward_config.py`

### Rewards

Reusable reward function boundary

- [Reward Base](https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/base_reward.py): reward interface · Common reward-function contract · `src/dhant/rewards/base_reward.py`
- [Accuracy Reward](https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/accuracy_reward.py): reward function · Reusable accuracy-oriented scoring · `src/dhant/rewards/accuracy_reward.py`
- [Format Reward](https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/format_reward.py): reward function · Reusable formatting and structure scoring · `src/dhant/rewards/format_reward.py`
- [Reasoning Reward](https://github.com/nabin2004/dhant/blob/main/src/dhant/rewards/reasoning_reward.py): reward function · Reusable reasoning-quality scoring · `src/dhant/rewards/reasoning_reward.py`

### Runtime

Run outputs and monitoring artifacts

- **Run Outputs**: artifact store · Per-run experiment folders with checkpoints, logs, and summaries
- **TensorBoard**: monitoring integration · Optional experiment monitoring channel

## Connections

- User → CLI: runs · User launches task-specific workflows through the CLI
- User → Quickstart: uses · User can also explore the package through the example script
- CLI → SFT Trainer: selects · CLI subcommand routes to the SFT workflow
- CLI → DPO Trainer: selects · CLI subcommand routes to the DPO workflow
- CLI → GRPO Trainer: selects · CLI subcommand routes to the GRPO workflow
- CLI → Reward Trainer: selects · CLI subcommand routes to the reward-model workflow
- Quickstart → Trainer Base: demonstrates · Example flow exercises the shared orchestration boundary
- SFT Trainer → Trainer Base: extends · SFT is built on the shared trainer boundary
- DPO Trainer → Trainer Base: extends · DPO is built on the shared trainer boundary
- GRPO Trainer → Trainer Base: extends · GRPO is built on the shared trainer boundary
- Reward Trainer → Trainer Base: extends · Reward training is built on the shared trainer boundary
- SFT Trainer → SFT Config: uses · SFT flow is parameterized by its config
- DPO Trainer → DPO Config: uses · DPO flow is parameterized by its config
- GRPO Trainer → GRPO Config: uses · GRPO flow is parameterized by its config
- Reward Trainer → Reward Config: uses · Reward training flow is parameterized by its config
- Trainer Base → Adapters: prepares · Shared setup applies tuning and memory-efficiency options before execution
- GRPO Trainer → Reward Base: scores with · GRPO consumes reward functions for output scoring
- GRPO Trainer → Accuracy Reward: can use · GRPO may include accuracy scoring
- GRPO Trainer → Format Reward: can use · GRPO may include format scoring
- GRPO Trainer → Reasoning Reward: can use · GRPO may include reasoning scoring
- Reward Trainer → Reward Base: trains against · Reward-model workflow aligns to the reward-function contract
- SFT Trainer → Run Outputs: writes · SFT run artifacts are recorded per experiment
- DPO Trainer → Run Outputs: writes · DPO run artifacts are recorded per experiment
- GRPO Trainer → Run Outputs: writes · GRPO run artifacts are recorded per experiment
- Reward Trainer → Run Outputs: writes · Reward-training run artifacts are recorded per experiment
- Run Outputs → TensorBoard: feeds · Stored metrics can be surfaced in TensorBoard
