|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Build & Development Commands |
| 6 | + |
| 7 | +```bash |
| 8 | +# Install in dev mode (editable + test deps) |
| 9 | +pip install -e ".[dev]" |
| 10 | + |
| 11 | +# Run all tests |
| 12 | +pytest tests/ -v --tb=short |
| 13 | + |
| 14 | +# Run a single test file |
| 15 | +pytest tests/test_config.py -v |
| 16 | + |
| 17 | +# Run a single test |
| 18 | +pytest tests/test_data.py::test_detect_alpaca_format -v |
| 19 | + |
| 20 | +# Lint |
| 21 | +ruff check soup_cli/ tests/ |
| 22 | + |
| 23 | +# Lint with auto-fix |
| 24 | +ruff check --fix soup_cli/ tests/ |
| 25 | +``` |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +Soup is a CLI-first tool for LLM fine-tuning. The core flow: |
| 30 | + |
| 31 | +``` |
| 32 | +soup train --config soup.yaml |
| 33 | + → config/loader.py (YAML → Pydantic SoupConfig) |
| 34 | + → utils/gpu.py (detect CUDA/MPS/CPU, estimate batch size) |
| 35 | + → data/loader.py (load file or HF dataset → normalize format) |
| 36 | + → trainer/sft.py (load model → quantize → apply LoRA → train) |
| 37 | + → monitoring/callback.py + display.py (live Rich dashboard) |
| 38 | + → save LoRA adapter to output/ |
| 39 | +``` |
| 40 | + |
| 41 | +**Config system:** `config/schema.py` is the single source of truth. All YAML fields are validated by Pydantic models (`SoupConfig` → `TrainingConfig` → `LoraConfig`, `DataConfig`). Templates (chat/code/medical) live as YAML strings in this file. |
| 42 | + |
| 43 | +**Data pipeline:** `data/loader.py` handles local files (JSONL/JSON/CSV/Parquet) and HuggingFace datasets. `data/formats.py` auto-detects and normalizes alpaca/sharegpt/chatml formats into a unified `{"messages": [...]}` structure. |
| 44 | + |
| 45 | +**Trainer:** `trainer/sft.py` (`SFTTrainerWrapper`) wraps HuggingFace's SFTTrainer with auto quantization (BitsAndBytes), LoRA (PEFT), and batch size estimation. Heavy ML imports are lazy (inside methods) so CLI stays fast for non-training commands. |
| 46 | + |
| 47 | +**Monitoring:** `monitoring/callback.py` is a HuggingFace `TrainerCallback` that streams metrics to `monitoring/display.py` (Rich Live panel at 2Hz). |
| 48 | + |
| 49 | +## Code Conventions |
| 50 | + |
| 51 | +- **Line length:** 100 chars (ruff enforced) |
| 52 | +- **Linter:** ruff with E, F, I, N, W rules |
| 53 | +- **Config validation:** Always Pydantic v2 (BaseModel + Field) |
| 54 | +- **CLI framework:** Typer with `rich_markup_mode="rich"` |
| 55 | +- **Output:** Use `rich.console.Console` — never bare `print()` |
| 56 | +- **Lazy imports:** Heavy deps (torch, transformers, peft) are imported inside functions, not at module level |
| 57 | +- **Variable naming:** Avoid single-letter names (ruff E741) — use `entry`, `part`, `length` instead of `l` |
| 58 | + |
| 59 | +## Git Workflow |
| 60 | + |
| 61 | +- Repo: https://github.com/MakazhanAlpamys/Soup |
| 62 | +- Branch: `main` |
| 63 | +- CI: GitHub Actions runs ruff lint + pytest on Python 3.9/3.11/3.12 |
| 64 | +- Always run `ruff check soup_cli/ tests/` before committing |
| 65 | +- Always run `pytest tests/ -v` before committing |
0 commit comments