# hkproj/torchfeather architecture

> Architecture diagram of the GitHub repository hkproj/torchfeather, 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/hkproj/torchfeather
- Repository: https://github.com/hkproj/torchfeather

## Overview

#### Purpose and entry points
TorchFeather is a PyTorch distributed-training framework derived from TorchTitan, configured by default to train a DeepSeek-style MoE model. Cluster execution enters through `launch.sh` and `trainer.slurm`; Python training orchestration begins at `torchfeather/train.py`. Small parallelism demonstrations live under `minimal_examples/`.

#### Job configuration and assembly
`torchfeather/config/job_config.py` defines the training-job configuration surface, while `torchfeather/config/default_configs.py` supplies defaults. `torchfeather/train.py` consumes this configuration to construct the model, data pipeline, optimizer, scheduler, metrics, checkpointing, and distributed execution topology.

#### Model stack
The transformer implementation is centered in `torchfeather/model/model.py`, with model shape/options in `torchfeather/model/model_args.py`. Attention and rotary position encoding are isolated in `torchfeather/model/attention.py` and `torchfeather/model/rope.py`. `torchfeather/model/parallelize.py` is the model-to-distributed-runtime boundary, applying supported parallelization strategies to model modules.

#### Mixture-of-Experts path
MoE layers are implemented in `torchfeather/model/moe/moe.py`, with routing/dispatch support in `torchfeather/model/moe/utils.py` and specialized operations in `torchfeather/model/moe/kernels.py`. Expert execution integrates with distributed expert placement through `torchfeather/distributed/expert_parallel.py`; this is a key boundary between model-local routing and cross-rank communication.

#### Distributed execution topology
`torchfeather/distributed/parallel_dims.py` defines the multidimensional parallel layout. Runtime strategies include tensor/model parallelism in `torchfeather/distributed/model_parallel.py`, pipeline execution in `torchfeather/distributed/pipeline_parallel.py`, and activation rematerialization in `torchfeather/distributed/activation_checkpoint.py`. Shared process-group and rank utilities reside in `torchfeather/distributed/utils.py`. The trainer selects and composes these mechanisms rather than treating distribution as an external wrapper.

#### Training data and optimization flow
Training data is sourced through Hugging Face dataset integration in `torchfeather/datasets/hf_datasets.py`, then tokenized via `torchfeather/components/tokenizer.py` and batched by `torchfeather/components/dataloader.py`. Each training step in `torchfeather/train.py` flows through forward/loss, backward/distributed synchronization, optimizer update, and learning-rate scheduling. Those stages are encapsulated by `torchfeather/components/loss.py`, `torchfeather/components/optimizer.py`, and `torchfeather/components/lr_scheduler.py`.

#### State, observability, and infrastructure
Checkpoint save/load behavior is owned by `torchfeather/components/checkpoint.py`; metrics reporting is in `torchfeather/components/metrics.py`, with profiling/device support in `torchfeather/tools/profiling.py` and `torchfeather/tools/device_utils.py`. The intended infrastructure is a Slurm-managed multi-node GPU cluster (`trainer.slurm`, `launch.sh`). External dependencies include PyTorch distributed runtime, Hugging Face assets/datasets, and a DeepSeek-MoE tokenizer downloaded by `scripts/download_hf_assets.py` into the default-configured local assets path.

Main entry points: [`torchfeather/train.py`](https://github.com/hkproj/torchfeather/blob/main/torchfeather/train.py) (Training orchestrator), [`torchfeather/model/parallelize.py`](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/parallelize.py) (Model parallelization), [`torchfeather/model/model.py`](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/model.py) (DeepSeek MoE transformer), [`torchfeather/distributed/parallel_dims.py`](https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/parallel_dims.py) (Parallel dimensions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cluster["Cluster execution"]
  node_launcher["Launch scripts<br/>Slurm launcher<br/>[launch.sh]"]
  node_slurm["Slurm job spec<br/>Slurm configuration<br/>[trainer.slurm]"]
end

subgraph group_config["Job assembly"]
  node_train{{"Training orchestrator<br/>Python entry point<br/>[train.py]"}}
  node_job_config["Job configuration<br/>configuration schema<br/>[job_config.py]"]
  node_defaults["Default configuration<br/>configuration defaults<br/>[default_configs.py]"]
end

subgraph group_model["Model and MoE"]
  node_model_args["Model arguments<br/>model schema<br/>[model_args.py]"]
  node_transformer["DeepSeek MoE transformer<br/>model implementation<br/>[model.py]"]
  node_attention["Attention and RoPE<br/>transformer layers<br/>[attention.py]"]
  node_moe["MoE layers and routing<br/>expert routing<br/>[moe.py]"]
  node_model_parallelize{{"Model parallelization<br/>model/runtime boundary<br/>[parallelize.py]"}}
end

subgraph group_distributed["Distributed runtime"]
  node_parallel_dims["Parallel dimensions<br/>topology layout<br/>[parallel_dims.py]"]
  node_model_parallel["Tensor/model parallelism<br/>distributed strategy<br/>[model_parallel.py]"]
  node_pipeline_parallel["Pipeline execution<br/>distributed strategy"]
  node_expert_parallel["Expert parallelism<br/>cross-rank expert execution<br/>[expert_parallel.py]"]
end

subgraph group_data["Data pipeline"]
  node_hf_data[("Hugging Face datasets<br/>external data integration<br/>[hf_datasets.py]")]
  node_tokenizer["Tokenizer<br/>tokenization<br/>[tokenizer.py]"]
  node_dataloader["Data loader<br/>batching<br/>[dataloader.py]"]
end

subgraph group_training["Training state"]
  node_loss["Loss computation<br/>training component<br/>[loss.py]"]
  node_optimizer["Optimizer<br/>training component<br/>[optimizer.py]"]
  node_lr_scheduler["Learning-rate scheduler<br/>training component<br/>[lr_scheduler.py]"]
  node_checkpoint[("Checkpointing<br/>training state<br/>[checkpoint.py]")]
  node_metrics["Metrics and profiling<br/>observability<br/>[metrics.py]"]
end

node_slurm -->|"defines job"| node_launcher
node_launcher -->|"starts"| node_train
node_defaults -->|"supplies defaults"| node_job_config
node_job_config -->|"configures"| node_train
node_train -->|"selects shape"| node_model_args
node_model_args -->|"parameterizes"| node_transformer
node_transformer -->|"uses"| node_attention
node_transformer -->|"contains"| node_moe
node_train -->|"applies strategies"| node_model_parallelize
node_model_parallelize -->|"transforms modules"| node_transformer
node_parallel_dims -->|"drives layout"| node_model_parallelize
node_model_parallelize -->|"enables"| node_model_parallel
node_model_parallelize -->|"enables"| node_pipeline_parallel
node_moe -->|"dispatches experts"| node_expert_parallel
node_parallel_dims -->|"places experts"| node_expert_parallel
node_hf_data -->|"provides samples"| node_tokenizer
node_tokenizer -->|"produces tokens"| node_dataloader
node_dataloader -->|"feeds batches"| node_train
node_train -->|"computes loss"| node_loss
node_loss -->|"backpropagates gradients"| node_optimizer
node_optimizer -->|"updates with schedule"| node_lr_scheduler
node_train -->|"saves and restores state"| node_checkpoint
node_train -->|"reports progress"| node_metrics

click node_launcher "https://github.com/hkproj/torchfeather/blob/main/launch.sh"
click node_slurm "https://github.com/hkproj/torchfeather/blob/main/trainer.slurm"
click node_train "https://github.com/hkproj/torchfeather/blob/main/torchfeather/train.py"
click node_job_config "https://github.com/hkproj/torchfeather/blob/main/torchfeather/config/job_config.py"
click node_defaults "https://github.com/hkproj/torchfeather/blob/main/torchfeather/config/default_configs.py"
click node_model_args "https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/model_args.py"
click node_transformer "https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/model.py"
click node_attention "https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/attention.py"
click node_moe "https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/moe/moe.py"
click node_model_parallelize "https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/parallelize.py"
click node_parallel_dims "https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/parallel_dims.py"
click node_model_parallel "https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/model_parallel.py"
click node_pipeline_parallel "https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/pipeline_parallel.py"
click node_expert_parallel "https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/expert_parallel.py"
click node_hf_data "https://github.com/hkproj/torchfeather/blob/main/torchfeather/datasets/hf_datasets.py"
click node_tokenizer "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/tokenizer.py"
click node_dataloader "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/dataloader.py"
click node_loss "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/loss.py"
click node_optimizer "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/optimizer.py"
click node_lr_scheduler "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/lr_scheduler.py"
click node_checkpoint "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/checkpoint.py"
click node_metrics "https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/metrics.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_launcher,node_slurm toneBlue
class node_train,node_job_config,node_defaults toneAmber
class node_model_args,node_transformer,node_attention,node_moe,node_model_parallelize toneMint
class node_parallel_dims,node_model_parallel,node_pipeline_parallel,node_expert_parallel toneRose
class node_hf_data,node_tokenizer,node_dataloader toneIndigo
class node_loss,node_optimizer,node_lr_scheduler,node_checkpoint,node_metrics toneTeal
```

## Components

### Cluster execution

- [Launch scripts](https://github.com/hkproj/torchfeather/blob/main/launch.sh): Slurm launcher · Starts multi-node GPU training jobs. · `launch.sh`
- [Slurm job spec](https://github.com/hkproj/torchfeather/blob/main/trainer.slurm): Slurm configuration · `trainer.slurm`

### Job assembly

- [Training orchestrator](https://github.com/hkproj/torchfeather/blob/main/torchfeather/train.py): Python entry point · Assembles and runs the end-to-end training loop. · `torchfeather/train.py`
- [Job configuration](https://github.com/hkproj/torchfeather/blob/main/torchfeather/config/job_config.py): configuration schema · `torchfeather/config/job_config.py`
- [Default configuration](https://github.com/hkproj/torchfeather/blob/main/torchfeather/config/default_configs.py): configuration defaults · `torchfeather/config/default_configs.py`

### Model and MoE

- [Model arguments](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/model_args.py): model schema · `torchfeather/model/model_args.py`
- [DeepSeek MoE transformer](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/model.py): model implementation · `torchfeather/model/model.py`
- [Attention and RoPE](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/attention.py): transformer layers · `torchfeather/model/attention.py`
- [MoE layers and routing](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/moe/moe.py): expert routing · Routes tokens to local expert computation. · `torchfeather/model/moe/moe.py`
- [Model parallelization](https://github.com/hkproj/torchfeather/blob/main/torchfeather/model/parallelize.py): model/runtime boundary · Applies selected distributed strategies to model modules. · `torchfeather/model/parallelize.py`

### Distributed runtime

- [Parallel dimensions](https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/parallel_dims.py): topology layout · Defines the multidimensional parallel layout. · `torchfeather/distributed/parallel_dims.py`
- [Tensor/model parallelism](https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/model_parallel.py): distributed strategy · `torchfeather/distributed/model_parallel.py`
- [Pipeline execution](https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/pipeline_parallel.py): distributed strategy · `torchfeather/distributed/pipeline_parallel.py`
- [Expert parallelism](https://github.com/hkproj/torchfeather/blob/main/torchfeather/distributed/expert_parallel.py): cross-rank expert execution · Connects routed experts with distributed placement and communication. · `torchfeather/distributed/expert_parallel.py`

### Data pipeline

- [Hugging Face datasets](https://github.com/hkproj/torchfeather/blob/main/torchfeather/datasets/hf_datasets.py): external data integration · `torchfeather/datasets/hf_datasets.py`
- [Tokenizer](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/tokenizer.py): tokenization · `torchfeather/components/tokenizer.py`
- [Data loader](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/dataloader.py): batching · `torchfeather/components/dataloader.py`

### Training state

- [Loss computation](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/loss.py): training component · `torchfeather/components/loss.py`
- [Optimizer](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/optimizer.py): training component · `torchfeather/components/optimizer.py`
- [Learning-rate scheduler](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/lr_scheduler.py): training component · `torchfeather/components/lr_scheduler.py`
- [Checkpointing](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/checkpoint.py): training state · `torchfeather/components/checkpoint.py`
- [Metrics and profiling](https://github.com/hkproj/torchfeather/blob/main/torchfeather/components/metrics.py): observability · `torchfeather/components/metrics.py`

## Connections

- Slurm job spec → Launch scripts: defines job
- Launch scripts → Training orchestrator: starts
- Default configuration → Job configuration: supplies defaults
- Job configuration → Training orchestrator: configures
- Training orchestrator → Model arguments: selects shape
- Model arguments → DeepSeek MoE transformer: parameterizes
- DeepSeek MoE transformer → Attention and RoPE: uses
- DeepSeek MoE transformer → MoE layers and routing: contains
- Training orchestrator → Model parallelization: applies strategies
- Model parallelization → DeepSeek MoE transformer: transforms modules
- Parallel dimensions → Model parallelization: drives layout
- Model parallelization → Tensor/model parallelism: enables
- Model parallelization → Pipeline execution: enables
- MoE layers and routing → Expert parallelism: dispatches experts
- Parallel dimensions → Expert parallelism: places experts
- Hugging Face datasets → Tokenizer: provides samples
- Tokenizer → Data loader: produces tokens
- Data loader → Training orchestrator: feeds batches
- Training orchestrator → Loss computation: computes loss
- Loss computation → Optimizer: backpropagates gradients
- Optimizer → Learning-rate scheduler: updates with schedule
- Training orchestrator → Checkpointing: saves and restores state
- Training orchestrator → Metrics and profiling: reports progress
