# md-mushfiqur123/m2lrf architecture

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

- Interactive diagram: https://gitdiagram.com/md-mushfiqur123/m2lrf
- Repository: https://github.com/md-mushfiqur123/m2lrf

## Overview

M-2LRF is an LLM quantization, fine-tuning, and inference engine. Its main workflow loads a pretrained model, applies architecture patches and low-bit conversion, then trains or exports the result; serving is a separate inference path. The README also documents adapter merging and serving features. Relationships below use the sampled source and SOURCE INDEX references; several documented capabilities have unsampled implementations, so their connections are marked unverified rather than inferred.

Main entry points: [`m2lrf/cli/main.py`](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/cli/main.py) (Command line), [`m2lrf/models/loader.py`](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/models/loader.py) (Model loader), [`m2lrf/unified_layer.py`](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/unified_layer.py) (Unified linear layer), [`m2lrf/serving/engine.py`](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/engine.py) (Generation engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_model["Model preparation"]
  node_loader["Model loader<br/>[loader.py]"]
  node_patch["Architecture patchers<br/>[base_patcher.py]"]
  node_convert["Model conversion<br/>[trainer_eval.py]"]
end

subgraph group_quant["Quantization runtime"]
  node_hadamard["Hadamard rotation"]
  node_quantizer["Dual-basis quantizer<br/>[quantizer.py]"]
  node_codec["Packed weight codec<br/>[packed_codec.py]"]
  node_mixed["Mixed precision<br/>[mixed_precision.py]"]
  node_layer["Unified linear layer<br/>[unified_layer.py]"]
  node_kernels["GPU kernels"]
end

subgraph group_training["Training and data"]
  node_sft["SFT trainer<br/>[sft_trainer.py]"]
  node_reasoning["Reasoning data"]
  node_grpo["GRPO trainer<br/>[grpo_trainer.py]"]
end

subgraph group_serving["Inference serving"]
  node_serveapi["OpenAI API server<br/>[openai_server.py]"]
  node_engine["Generation engine<br/>[engine.py]"]
  node_blocks["KV block manager<br/>[block_manager.py]"]
  node_attention["Paged attention<br/>[paged_attention.py]"]
  node_prefix["Prefix cache<br/>[radix_cache.py]"]
end

subgraph group_delivery["Export and interfaces"]
  node_cli["Command line<br/>[main.py]"]
  node_hfexport["Hugging Face export<br/>[hf_export.py]"]
  node_gguf["GGUF export<br/>[gguf_export.py]"]
end

node_user(("Engineer"))
node_externalmodel[("Model source")]
node_client(("Inference client"))

node_user -->|"runs commands"| node_cli
node_cli -->|"loads model"| node_loader
node_loader -->|"loads weights"| node_externalmodel
node_loader -->|"selects patcher"| node_patch
node_loader -->|"prepares model"| node_convert
node_patch -->|"patches norms"| node_kernels
node_patch -->|"delegates conversion"| node_convert
node_convert -->|"replaces modules"| node_layer
node_convert -->|"uses rotation"| node_hadamard
node_layer -->|"quantizes weights"| node_quantizer
node_layer -->|"packs weights"| node_codec
node_layer -->|"uses precision"| node_mixed
node_hadamard -->|"quantizes rotated weights"| node_quantizer
node_hadamard -->|"packs weights"| node_codec
node_sft -->|"uses loss"| node_kernels
node_grpo -->|"uses samples"| node_reasoning
node_cli -->|"starts training"| node_sft
node_cli -->|"dispatches export"| node_hfexport
node_cli -->|"dispatches export"| node_gguf
node_client -.->|"sends requests"| node_serveapi
node_serveapi -.->|"routes generation"| node_engine
node_engine -->|"allocates blocks"| node_blocks
node_engine -->|"matches prefixes"| node_prefix
node_engine -.->|"runs attention"| node_attention

click node_cli "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/cli/main.py"
click node_loader "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/models/loader.py"
click node_patch "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/models/base_patcher.py"
click node_convert "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/trainer_eval.py"
click node_hadamard "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/hadamard_transform.py"
click node_quantizer "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/quantizer.py"
click node_codec "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/packed_codec.py"
click node_mixed "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/mixed_precision.py"
click node_layer "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/unified_layer.py"
click node_kernels "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/kernels/fast_cross_entropy.py"
click node_sft "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/trainers/sft_trainer.py"
click node_reasoning "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/data/synthetic_reasoning.py"
click node_grpo "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/trainers/grpo_trainer.py"
click node_serveapi "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/openai_server.py"
click node_engine "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/engine.py"
click node_blocks "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/block_manager.py"
click node_attention "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/paged_attention.py"
click node_prefix "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/radix_cache.py"
click node_hfexport "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/export/hf_export.py"
click node_gguf "https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/export/gguf_export.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_loader,node_patch,node_convert,node_client toneBlue
class node_hadamard,node_quantizer,node_codec,node_mixed,node_layer,node_kernels,node_externalmodel toneAmber
class node_sft,node_reasoning,node_grpo toneMint
class node_serveapi,node_engine,node_blocks,node_attention,node_prefix toneRose
class node_cli,node_hfexport,node_gguf,node_user toneIndigo
```

## Components

### Model preparation

- [Model loader](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/models/loader.py): `m2lrf/models/loader.py`
- [Architecture patchers](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/models/base_patcher.py): `m2lrf/models/base_patcher.py`
- [Model conversion](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/trainer_eval.py): `m2lrf/trainer_eval.py`

### Quantization runtime

- [Hadamard rotation](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/hadamard_transform.py): `m2lrf/hadamard_transform.py`
- [Dual-basis quantizer](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/quantizer.py): `m2lrf/quantizer.py`
- [Packed weight codec](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/packed_codec.py): `m2lrf/packed_codec.py`
- [Mixed precision](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/mixed_precision.py): `m2lrf/mixed_precision.py`
- [Unified linear layer](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/unified_layer.py): `m2lrf/unified_layer.py`
- [GPU kernels](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/kernels/fast_cross_entropy.py): `m2lrf/kernels/fast_cross_entropy.py`

### Training and data

- [SFT trainer](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/trainers/sft_trainer.py): `m2lrf/trainers/sft_trainer.py`
- [Reasoning data](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/data/synthetic_reasoning.py): `m2lrf/data/synthetic_reasoning.py`
- [GRPO trainer](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/trainers/grpo_trainer.py): `m2lrf/trainers/grpo_trainer.py`

### Inference serving

- [OpenAI API server](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/openai_server.py): `m2lrf/serving/openai_server.py`
- [Generation engine](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/engine.py): `m2lrf/serving/engine.py`
- [KV block manager](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/block_manager.py): `m2lrf/serving/block_manager.py`
- [Paged attention](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/paged_attention.py): `m2lrf/serving/paged_attention.py`
- [Prefix cache](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/serving/radix_cache.py): `m2lrf/serving/radix_cache.py`

### Export and interfaces

- [Command line](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/cli/main.py): `m2lrf/cli/main.py`
- [Hugging Face export](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/export/hf_export.py): `m2lrf/export/hf_export.py`
- [GGUF export](https://github.com/md-mushfiqur123/m2lrf/blob/main/m2lrf/export/gguf_export.py): `m2lrf/export/gguf_export.py`

### Other components

- **Engineer**
- **Model source**
- **Inference client**

## Connections

- Engineer → Command line: runs commands (evidence: `m2lrf/cli/main.py`)
- Command line → Model loader: loads model (evidence: `m2lrf/cli/main.py`)
- Model loader → Model source: loads weights (evidence: `m2lrf/models/loader.py`)
- Model loader → Architecture patchers: selects patcher (evidence: `m2lrf/models/loader.py`)
- Model loader → Model conversion: prepares model (evidence: `m2lrf/models/loader.py`)
- Architecture patchers → GPU kernels: patches norms (evidence: `m2lrf/models/base_patcher.py`)
- Architecture patchers → Model conversion: delegates conversion (evidence: `m2lrf/models/base_patcher.py`)
- Model conversion → Unified linear layer: replaces modules (evidence: `m2lrf/trainer_eval.py`)
- Model conversion → Hadamard rotation: uses rotation (evidence: `m2lrf/trainer_eval.py`)
- Unified linear layer → Dual-basis quantizer: quantizes weights (evidence: `m2lrf/unified_layer.py`)
- Unified linear layer → Packed weight codec: packs weights (evidence: `m2lrf/unified_layer.py`)
- Unified linear layer → Mixed precision: uses precision (evidence: `m2lrf/unified_layer.py`)
- Hadamard rotation → Dual-basis quantizer: quantizes rotated weights (evidence: `m2lrf/hadamard_transform.py`)
- Hadamard rotation → Packed weight codec: packs weights (evidence: `m2lrf/hadamard_transform.py`)
- SFT trainer → GPU kernels: uses loss (evidence: `m2lrf/trainers/sft_trainer.py`)
- GRPO trainer → Reasoning data: uses samples (evidence: `m2lrf/trainers/grpo_trainer.py`)
- Command line → SFT trainer: starts training (evidence: `m2lrf/cli/main.py`)
- Command line → Hugging Face export: dispatches export (evidence: `m2lrf/cli/main.py`)
- Command line → GGUF export: dispatches export (evidence: `m2lrf/cli/main.py`)
- Inference client → OpenAI API server: sends requests
- OpenAI API server → Generation engine: routes generation
- Generation engine → KV block manager: allocates blocks (evidence: `m2lrf/serving/engine.py`)
- Generation engine → Prefix cache: matches prefixes (evidence: `m2lrf/serving/engine.py`)
- Generation engine → Paged attention: runs attention (evidence: `m2lrf/serving/engine.py`)
