Skip to content
JChemseddinePublic

About

Spherical Flows for Sampling Categorical Data

Resources

Stars

6 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spherical Flows for Sampling Categorical Data

Jannis Chemseddine, Gregor Kornhardt, Gabriele Steidl

arXiv Hugging Face

Reference implementation of Spherical Flows for Sampling Categorical Data. Tokens are embedded as unit vectors and generated in parallel by learning to reverse a vMF based noising process. The conditional score is closed-form on the sphere, so one cross-entropy-trained model supports the ODE, predictor–corrector (PC) and SDE samplers.

Also included:

  • the geodesic (SLERP), Euclidean VP/VE, and masked-token (MDLM-style) baselines
  • a CDCD-style learned time warp
  • self-conditioning
  • the spherical Cauchy path (cauchy)
  • SDE sampling on the sphere
Dataset Tokenizer / seq. length Metric
Sudoku (sudoku-extreme) — / 81 grid validity, non-clue accuracy
LM1B BERT / 128 gen-PPL + entropy
OpenWebText (OWT) GPT-2 / 1024 gen-PPL + entropy
TinyGSM (GSM8K) SmolLM-135M / 512 answer accuracy

Repository layout

src/
├── config.py            # typed config, JSON-backed
├── configs/paper/       # experiment configs, one folder per dataset
│   └── {lm1b,owt,tinygsm,sudoku}/
├── data/                # dataset loaders: sudoku, lm1b, owt, tinygsm
├── methods/             # noise processes + samplers (spherical, Euclidean, masked)
├── model/               # DiT-style backbone
├── schedule/            # CDCD learned time warp
├── eval/                # gen-PPL / entropy / GSM8K grader internals
├── utils/               # checkpointing, validation, training loop helpers
├── scripts/
│   ├── data/            # dataset preparation
│   ├── train/           # train.py (single GPU), train_ddp.py (DDP)
│   ├── eval/            # eval_{lm1b,owt,gsm8k,sudoku}.py, sample.py
│   ├── viz/             # figure scripts
│   └── slurm/           # cluster launchers + replicate_*_grid.sh
└── tests/               # CPU tests: samplers, SDE paths, cross-path knobs, masked samplers

Sampler smoke test (CPU, no checkpoints or data, a few seconds):

PYTHONPATH=$PWD/src uv run python src/tests/test_samplers.py

Getting started

Requires Python ≥ 3.10 and uv; Torch is resolved from the CUDA 12.4 index.

uv sync
export PYTHONPATH=$PWD/src

Run all commands below from the repository root.

Data preparation

The shipped configs carry placeholder absolute paths (/scratch/htc/USER/...). Point data.data_dir (plus data.cache_dir for TinyGSM) and checkpoint_dir at your own storage first.

Then one script per dataset:

uv run python src/scripts/data/build_sudoku_dataset.py --output-dir data/sudoku-extreme-full
uv run python src/scripts/data/prepare_lm1b.py --output_dir data/lm1b
uv run python src/scripts/data/prepare_owt.py  --output_dir data/owt
uv run python src/scripts/data/prep_tinygsm.py --config src/configs/paper/tinygsm/vmf_tc.json

Training

The configs in src/configs/paper/ are the exact settings behind the paper's experiments (typed schema in src/config.py). Each config is the final paper configuration for its cell.

Naming follows the paper: flow.noise_process is one of vmf, geodesic, vp, ve, cauchy, masked, and flow.sampler_method one of ode, pc, sde. Time-conditioned models end in _tc, self-conditioned ones in _selfcond_scp025.

Dataset Configs Budget
Sudoku src/configs/paper/sudoku/*.json 1M steps
LM1B src/configs/paper/lm1b/*.json 1M steps
OWT src/configs/paper/owt/*.json 1M steps
TinyGSM src/configs/paper/tinygsm/*.json 250K steps
# single GPU
uv run python src/scripts/train/train.py \
    --config src/configs/paper/sudoku/vmf_d11.json --no-wandb

# multi GPU (DDP) — used for all LM1B/OWT/TinyGSM paper runs
uv run torchrun --nproc_per_node=4 src/scripts/train/train_ddp.py \
    --config src/configs/paper/lm1b/vmf_tc.json

Checkpoints go to the config's checkpoint_dir (step_*.pt, plus final.pt at the end). train.py additionally takes --smoke (10-step pipeline check) and --max-steps N; both scripts take --resume <ckpt> and --no-wandb.

SLURM launchers for the paper runs are in src/scripts/slurm/:

  • They locate the checkout themselves (override with SFCD_SRC) and take CUDA_HOME, HF_HOME, WANDB_DIR, and UV_CACHE_DIR from the environment.
  • Only the #SBATCH headers — partition, GPU type, walltime — need adapting.
  • They run from src/, so their config argument is relative to that:
sbatch src/scripts/slurm/run_lm1b.sh configs/paper/lm1b/vmf_tc.json

Reproducing the paper

The trained paper checkpoints for LM1B, OWT, and TinyGSM are hosted at huggingface.co/Jugc/SFCD, laid out as <dataset>/<model>/final.pt alongside the config.json each was trained with. Fetch one without leaving the shell (huggingface_hub comes with transformers):

from huggingface_hub import hf_hub_download

ckpt = hf_hub_download("Jugc/SFCD", "lm1b/vmf_tc_selfcond_scp025/final.pt")
cfg  = hf_hub_download("Jugc/SFCD", "lm1b/vmf_tc_selfcond_scp025/config.json")

or the whole repository at once:

uv run hf download Jugc/SFCD --local-dir checkpoints

Model names match the config names: {lm1b,owt,tinygsm}/<path>[_tc][_selfcond_scp025], with <path> one of vmf, geo, vp, ve. Any downloaded final.pt works as <ckpt.pt> below; the downloaded config.json works as --config.

Prepare the dataset first (Data preparation).

One script per dataset fires the canonical evaluation grid for a (config, checkpoint) pair, at the paper's exact-NFE schedules (NFE = S for the ODE, S·(1 + cs/ci) for PC). Pass the NFE budgets you want:

bash src/scripts/slurm/replicate_lm1b_grid.sh \
    configs/paper/lm1b/vmf_tc_selfcond_scp025.json <ckpt.pt> 128
bash src/scripts/slurm/replicate_owt_grid.sh \
    configs/paper/owt/vmf_tc_selfcond_scp025.json <ckpt.pt> 128,1024
bash src/scripts/slurm/replicate_gsm8k_grid.sh \
    configs/paper/tinygsm/vmf_tc.json <ckpt.pt> 1024
  • Each script sweeps the samplers, corrector splits, and step sizes behind its dataset's tables, submitting one job per cell. Each job prints its gen-PPL and entropy (or accuracy) to stdout, so a whole grid is read back from the job logs.
  • DRYRUN=1 prints the job matrix without submitting anything.
  • Run one per path you want to reproduce (vMF, geodesic, VP, VE, masked).
  • Their headers document which paper tables they cover.

Sampling and evaluation

For single runs rather than a whole grid.

Sampler methods:

  • ode is the predictor-only Euler sampler, pc the predictor–corrector; a _warp suffix (e.g. pc_warp) selects warp-aware step spacing. These two are the samplers behind the paper's main tables.
  • sde is Euler–Maruyama on the flow SDE, available on vmf, vp and ve. Geodesic admits no closed-form score and so has neither pc nor sde.
  • The masked path selects its sampler with flow.masked_sampler: mdlm (the ancestral absorbing-state sampler behind the paper's masked baseline), remdm_cap / remdm_rescale (ReMDM remasking), or confidence (LLaDA 2.0 confidence-threshold decoding, adaptive NFE).

Every sampler, every path it runs on, and every inference knob with its default and the paper table it backs are documented in SAMPLERS.md.

Text results are generative perplexity (1024 generated sequences scored with GPT-2 Large) reported together with the sample entropy H. TinyGSM reports answer accuracy on the full GSM8K test set, executing the generated Python to check it.

# LM1B / OWT / TinyGSM
uv run python src/scripts/eval/eval_lm1b.py \
    --config src/configs/paper/lm1b/vmf_tc.json --checkpoint <ckpt.pt> \
    --method pc_warp --sampling-steps 64 --num-gen-ppl-samples 1024 \
    --corrector-steps 1 --corrector-interval 1 --corrector-epsilon 1e-3 \
    --corrector-scaling on

# Sudoku — prints grids, checks validity, scores non-clue accuracy
uv run python src/scripts/eval/sample.py \
    --checkpoint checkpoints/sudoku/vmf_d11/final.pt \
    --num-samples 4 --use-val-puzzles

eval_owt.py follows the same pattern; eval_gsm8k.py uses --num-steps instead of --sampling-steps. See --help for the full flag lists.

For a self-conditioned checkpoint, --sc-onset τ feeds a null self-conditioning input while step/S < τ and resumes the warm chain afterwards, at no extra NFE. The paper reports τ ∈ {0, 1/3, 1/2}; τ = 0 is the plain warm chain.

Acknowledgements

This codebase adapts parts of prior open-source work:

  • Model backbone, masked-diffusion baseline, generative-perplexity harness, and LM1B/OWT preprocessing — adapted from MDLM (Sahoo et al., 2024, Apache-2.0) and SEDD (Lou et al., 2023, MIT), which build on DiT (Peebles & Xie, 2023, MIT).
  • Learned time warp — our own implementation of CDCD (Dieleman et al., 2022).
  • Masked inference samplers — remdm_cap / remdm_rescale implement ReMDM (Wang et al., 2025, arXiv:2503.00307); confidence implements the confidence-threshold parallel decoder of LLaDA 2.0 (arXiv:2512.15745). Both are our own implementations, and neither is used for the paper's masked baseline, which is the mdlm ancestral sampler.
  • Sudoku data preparation — adapted from HRM (Wang et al., 2025); data from sapientinc/sudoku-extreme.
  • TinyGSM/GSM8K pipeline — derives from S-FLM (Deschenaux & Gulcehre, 2026); the GSM8K grader is adapted from PUMA.
  • Datasets — LM1B; OpenWebText; GSM8K test set (OpenAI, MIT); TinyGSM.

Citation

@misc{chemseddine2026spherical,
  title         = {Spherical Flows for Sampling Categorical Data},
  author        = {Jannis Chemseddine and Gregor Kornhardt and Gabriele Steidl},
  year          = {2026},
  eprint        = {2605.05629},
  archivePrefix = {arXiv},
  primaryClass  = {stat.ML}
}

About

Spherical Flows for Sampling Categorical Data

Resources

Stars

6 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages