Metadata-Version: 2.5
Name: palingenesis
Version: 0.3.0
Summary: Fire-and-forget LLM fine-tuning with state-of-the-art defaults.
Project-URL: Repository, https://github.com/mii-llm/palingenesis
Project-URL: Documentation, https://mii-llm.github.io/palingenesis/
Author: palingenesis contributors
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: finetuning,llm,palingenesis,pytorch,sft,training
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.11
Requires-Dist: accelerate>=1.10
Requires-Dist: datasets>=4.0
Requires-Dist: numpy>=1.26
Requires-Dist: pyyaml>=6.0
Requires-Dist: safetensors>=0.4.0
Requires-Dist: torch>=2.11
Requires-Dist: transformers<5.13,>=5.12
Provides-Extra: agent-sandbox
Requires-Dist: k8s-agent-sandbox[async]>=1.0; extra == 'agent-sandbox'
Provides-Extra: all
Requires-Dist: bitsandbytes>=0.49; extra == 'all'
Requires-Dist: cut-cross-entropy>=25.1.0; extra == 'all'
Requires-Dist: flash-linear-attention>=0.4; (sys_platform == 'linux') and extra == 'all'
Requires-Dist: liger-kernel>=0.5.0; (sys_platform == 'linux') and extra == 'all'
Requires-Dist: math-verify>=0.8; extra == 'all'
Requires-Dist: mcp>=2.2; extra == 'all'
Requires-Dist: schedulefree>=1.4.0; extra == 'all'
Requires-Dist: sentence-transformers>=3.0.0; extra == 'all'
Requires-Dist: torchao>=0.9.0; extra == 'all'
Requires-Dist: torchvision>=0.26; extra == 'all'
Requires-Dist: trackio>=0.25.0; extra == 'all'
Requires-Dist: wandb>=0.19.0; extra == 'all'
Provides-Extra: dev
Requires-Dist: bitsandbytes>=0.49; extra == 'dev'
Requires-Dist: cut-cross-entropy>=25.1.0; extra == 'dev'
Requires-Dist: flash-linear-attention>=0.4; (sys_platform == 'linux') and extra == 'dev'
Requires-Dist: liger-kernel>=0.5.0; (sys_platform == 'linux') and extra == 'dev'
Requires-Dist: math-verify>=0.8; extra == 'dev'
Requires-Dist: mcp>=2.2; extra == 'dev'
Requires-Dist: pytest>=8.0.0; extra == 'dev'
Requires-Dist: ruff>=0.8.0; extra == 'dev'
Requires-Dist: schedulefree>=1.4.0; extra == 'dev'
Requires-Dist: sentence-transformers>=3.0.0; extra == 'dev'
Requires-Dist: torchao>=0.9.0; extra == 'dev'
Requires-Dist: torchvision>=0.26; extra == 'dev'
Requires-Dist: trackio>=0.25.0; extra == 'dev'
Requires-Dist: wandb>=0.19.0; extra == 'dev'
Provides-Extra: float8
Requires-Dist: torchao>=0.9.0; extra == 'float8'
Provides-Extra: hybrid
Requires-Dist: causal-conv1d>=1.5; (sys_platform == 'linux') and extra == 'hybrid'
Provides-Extra: logging
Requires-Dist: trackio>=0.25.0; extra == 'logging'
Requires-Dist: wandb>=0.19.0; extra == 'logging'
Provides-Extra: loss
Requires-Dist: cut-cross-entropy>=25.1.0; extra == 'loss'
Provides-Extra: mcp
Requires-Dist: mcp>=2.2; extra == 'mcp'
Provides-Extra: multimodal
Requires-Dist: torchvision>=0.26; extra == 'multimodal'
Provides-Extra: optim
Requires-Dist: schedulefree>=1.4.0; extra == 'optim'
Provides-Extra: prepare
Requires-Dist: sentence-transformers>=3.0.0; extra == 'prepare'
Provides-Extra: rl
Requires-Dist: math-verify>=0.8; extra == 'rl'
Provides-Extra: train
Requires-Dist: bitsandbytes>=0.49; extra == 'train'
Requires-Dist: flash-linear-attention>=0.4; (sys_platform == 'linux') and extra == 'train'
Requires-Dist: liger-kernel>=0.5.0; (sys_platform == 'linux') and extra == 'train'
Provides-Extra: vllm
Requires-Dist: torchaudio==2.11.0; (sys_platform == 'linux') and extra == 'vllm'
Requires-Dist: torchcodec>=0.14; (sys_platform == 'linux') and extra == 'vllm'
Requires-Dist: torchvision==0.26.0; (sys_platform == 'linux') and extra == 'vllm'
Requires-Dist: vllm==0.26.0; (sys_platform == 'linux') and extra == 'vllm'
Provides-Extra: vllm-server
Requires-Dist: ray>=2.40; (sys_platform == 'linux') and extra == 'vllm-server'
Requires-Dist: torchaudio==2.11.0; (sys_platform == 'linux') and extra == 'vllm-server'
Requires-Dist: torchcodec>=0.14; (sys_platform == 'linux') and extra == 'vllm-server'
Requires-Dist: torchvision==0.26.0; (sys_platform == 'linux') and extra == 'vllm-server'
Requires-Dist: vllm==0.26.0; (sys_platform == 'linux') and extra == 'vllm-server'
Description-Content-Type: text/markdown

# palingenesis

<p align="center">
  <img src="https://raw.githubusercontent.com/mii-llm/palingenesis/main/assets/pgs.webp" alt="palingenesis" width="100%">
</p>

**Post-train LLMs and agents from one config: SFT, multi-turn RL in tool environments, on-policy distillation, DPO and
merging, with state-of-the-art defaults.**

[Documentation](https://mii-llm.github.io/palingenesis/) · [Quickstart](https://mii-llm.github.io/palingenesis/getting-started/quickstart/) · [Configs](https://github.com/mii-llm/palingenesis/tree/main/configs)

From PyPI (Linux + NVIDIA GPU):

```bash
pip install "torch>=2.11" --index-url https://download.pytorch.org/whl/cu129   # FIRST: the CUDA 12.9 build (driver >= 575)
pip install "palingenesis[train,logging]"
pgs init sft                     # writes ./sft.yaml: set your model and dataset
pgs train --config sft.yaml
```

> **Install torch from PyTorch's CUDA index first.** Plain `pip install palingenesis` on Linux otherwise pulls PyPI's
> default torch, built for the newest CUDA (13.x, driver >= 580): on an older driver it installs fine and then sees no
> GPU. Other CUDA versions: [Installation](https://mii-llm.github.io/palingenesis/getting-started/install/).

From source (the locked, tested versions; the CUDA torch index is configured for you):

```bash
git clone https://github.com/mii-llm/palingenesis.git && cd palingenesis
uv sync --extra train --extra logging     # .venv with CUDA-enabled torch (+ --extra hybrid for Qwen3.5: faster, needed to pack)
source .venv/bin/activate
pgs train --config configs/quickstart.yaml
```

In a notebook or a script: `palingenesis.train.train(config)` on one GPU, see
[Training from a notebook](https://mii-llm.github.io/palingenesis/getting-started/notebook/).
AMD GPUs (ROCm): [Training on AMD GPUs](https://mii-llm.github.io/palingenesis/getting-started/amd/).

## What it does

| | Command | Guide |
|---|---|---|
| **Supervised fine-tuning**, including agentic traces with tool calls and reasoning | `pgs train --config configs/quickstart.yaml` | [Data](https://mii-llm.github.io/palingenesis/guides/data/), [Agentic](https://mii-llm.github.io/palingenesis/guides/agentic-training/) |
| **Reinforcement learning**: verifiable rewards, multi-turn tool use, GRPO-style groups, vLLM rollouts | `pgs rl --config configs/rl_math.yaml` | [RL](https://mii-llm.github.io/palingenesis/guides/rl/) |
| **Agent environments**: Python classes, MCP servers, OpenEnv, NeMo Gym, Harbor tasks in Docker, with emulated harness dialects (opencode, pi, OpenHands, …) | `pgs rl --config configs/rl_harbor.yaml` | [Harbor](https://mii-llm.github.io/palingenesis/guides/harbor/), [Harness profiles](https://mii-llm.github.io/palingenesis/guides/harness-profiles/) |
| **On-policy distillation**: the student samples, one or more teachers score those tokens, reverse KL | `pgs distill --config configs/distill_math.yaml` | [Distillation](https://mii-llm.github.io/palingenesis/guides/distillation/) |
| **Off-policy distillation**: fixed data against one or more teachers' distributions (online, or a cached top-k), context distillation, hidden-state targets, multi-GPU, plugins for QAT | `pgs init kd && pgs train --config kd.yaml` | [Off-policy distillation](https://mii-llm.github.io/palingenesis/guides/offpolicy-distillation/) |
| **Preference optimization**: DPO, IPO, SLiC-HF, robust and length-normalised DPO, SFT anchor | `pgs train --config configs/dpo_qwen35_08b.yaml` | [DPO](https://mii-llm.github.io/palingenesis/guides/dpo/) |
| **FP4 / FP8 quantisation-aware training** (plugin): NVFP4, MXFP4, FP8 fake quantisation while distilling from the bf16 model (SFT, KD, OPD, RL); packed checkpoints vLLM serves; `pgs quantize` for PTQ | `pgs train --config configs/qat_nvfp4_qwen35_08b.yaml` | [QAT](https://mii-llm.github.io/palingenesis/guides/qat/) |
| **LoRA** (plugin): frozen base, rank-r adapters (rsLoRA, DoRA, LoRA+), merged export and PEFT adapter; full fine-tuning stays the default | `pgs init lora && pgs train --config lora.yaml` | [LoRA](https://mii-llm.github.io/palingenesis/guides/lora/) |
| **Model merging**: interpolation, soups, task arithmetic | `pgs merge interpolate START END --lam 0.3 -o OUT` | [Merging](https://mii-llm.github.io/palingenesis/guides/merging/) |
| **Data preparation**: drop broken rows and duplicates, hold out an eval set, with the same config you train with | `pgs prepare --config configs/qwen35_4b/a100_80gb.yaml` | [Data](https://mii-llm.github.io/palingenesis/guides/data/) |
| **Autopilot**: profiles the GPU, sweeps the learning rate, trains to completion | `pgs autopilot --model Qwen/Qwen3.5-4B --dataset data.jsonl` | [Autopilot](https://mii-llm.github.io/palingenesis/guides/autopilot/) |

## Highlights

- **Correct by construction.** The loss path is checked step by step against a plain reference loop; packed conversations
  never see each other; multi-GPU gradients equal single-GPU ones; saved models load with plain `from_pretrained`.
- **Masking from the chat template itself.** Whole assistant turns are trained as the template renders them, tool calls and
  end-of-turn tokens included; `tools` are rendered into the prompt; `"loss": false` keeps a message as context only.
- **Token-exact RL.** What the trainer scores is exactly what the sampler produced, tool results included; nothing is
  re-tokenized. Rewards are checked answers, hidden tests, LLM judges or any Python function.
- **Memory.** Chunked losses (the full logits are never materialised), selective activation checkpointing, gradient
  release, 8-bit optimizers. A Qwen3.5-4B fine-tune (attention pathway, 36% of the weights) fits in about 16 GiB.
  `pgs fit` measures which memory settings are the fastest that fit your GPU.
- **Long sequences on one GPU (SeCO).** Chunk-by-chunk training with a cache gives the exact gradient, verified against full
  backpropagation on 23 architectures, including the Qwen3.5 and Qwen3-Next hybrids.
- **Mixture-of-Experts models.** Routers are balanced in every trainer (load-balancing loss with the expert load measured
  over the global batch, router z-loss, DeepSeek-V3's selection-bias update), with per-layer load and entropy in the log;
  RL replays the experts the rollout engine chose. Gradients tested against the written-out losses on five families.
- **Training you can inspect.** wandb and trackio with grad norm, spike and clip counters, gradient noise scale, entropy and
  the generalisation gap on one step axis; best-checkpoint tracking on eval loss.
- **Evaluation that agrees with training.** `pgs eval --model X --suite core` runs tasks defined in YAML (GSM8K,
  MATH-500, MMLU, MMLU-Pro, IFEval, BBH, HumanEval, MBPP, function calling, Italian sets, your own, agentic
  environments, or task files of lm-evaluation-harness as they are) with the trainer's own prompt rendering, tool
  parsing and rewards, in each thinking mode: one engine per run, scoring next to generation, multiple choice with
  one prefill per question; `pgs compare` gives paired tests between two checkpoints.
- **Crash-safe.** Re-run the same command to resume from the last complete checkpoint, in the same tracker run.
- **Scale out.** FSDP across GPUs, multi-node SLURM with sharded checkpoints, and a container image for Kubernetes.
- **Research options, one line each.** DEFT / DFT / InfoSFT token weighting, Hyperball, power-decay schedules: implemented to
  their papers' definitions; the benefits are as reported by the papers, not reproduced here.

## Hardware

| GPU | Model | Config |
|-----|-------|--------|
| A100 40 GB | Qwen3.5-4B, attention pathway (36% of weights) | `configs/qwen35_4b/a100_40gb.yaml` |
| A100 80 GB | Qwen3.5-4B, batch 4 | `configs/qwen35_4b/a100_80gb.yaml` |
| H100 80 GB | Qwen3.5-4B, FP8 | `configs/qwen35_4b/h100_80gb.yaml` |
| 8× A100 80 GB | Qwen3.5-35B MoE | `configs/qwen35_35b_moe/a100_80gb_multigpu.yaml` |

```bash
./scripts/train_multi_gpu.sh configs/qwen35_4b/a100_80gb_multigpu.yaml           # one node, all GPUs
sbatch scripts/train_slurm.sh configs/qwen35_35b_moe/a100_80gb_multigpu.yaml     # SLURM
```

Containers and Kubernetes: [docs/containers.md](https://github.com/mii-llm/palingenesis/blob/main/docs/containers.md).

## Development

```bash
pytest tests/                 # test suite
mkdocs serve                  # docs locally (pip install mkdocs-material)
```

## License

[Apache-2.0](https://github.com/mii-llm/palingenesis/blob/main/LICENSE).

*A new form emerging from what came before.*
