API Reference¶
Auto-generated from source docstrings via mkdocstrings.
High-level API¶
The dantinox package exposes five classes and two functions that cover the full lifecycle — training, generation, benchmarking, plotting, and Hub sharing — without touching internal modules.
Trainer¶
dantinox.trainer.Trainer ¶
High-level training interface for DantinoX.
Parameters¶
config : Config Model and training configuration.
Examples¶
trainer = Trainer(Config.from_yaml("configs/default_config.yaml")) run_dir = trainer.fit("data/corpus.txt")
Source code in dantinox/trainer.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
Functions¶
__init__ ¶
fit ¶
fit(
data_path: str | None = None,
*,
run_dir: str | None = None,
wandb_project: str | None = None,
resume: bool = False,
) -> str
Train a model and save the checkpoint.
Parameters¶
data_path : str, optional Path to the training corpus. Falls back to config.dataset_name. run_dir : str, optional Directory to write the checkpoint and logs. Auto-generated if omitted. wandb_project : str, optional If provided, metrics are logged to Weights & Biases. resume : bool If True and a previous checkpoint exists in run_dir, training resumes from the saved step. Optimizer state is not preserved.
Returns¶
str Path to the run directory containing the saved checkpoint.
Raises¶
ConfigError If the data path is missing or the file cannot be found.
Source code in dantinox/trainer.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
find_lr ¶
find_lr(
data_path: str | None = None,
*,
min_lr: float = 1e-07,
max_lr: float = 1.0,
num_steps: int = 100,
smoothing: float = 0.9,
) -> tuple[float, list[float], list[float]]
LR range test (Smith 2015).
Trains for num_steps steps while exponentially increasing the learning rate from min_lr to max_lr. Returns a tuple of (suggested_lr, lr_history, loss_history).
Parameters¶
data_path : str, optional Path to the training corpus. min_lr : float Starting learning rate (default 1e-7). max_lr : float Maximum learning rate (default 1.0). num_steps : int Number of steps in the sweep (default 100). smoothing : float Exponential smoothing factor for the loss curve (default 0.9).
Returns¶
tuple[float, list[float], list[float]] (suggested_lr, lr_history, loss_history)
Source code in dantinox/trainer.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
Generator¶
dantinox.generator.Generator ¶
Loads a trained DantinoX checkpoint and generates text.
Accepts either a local run directory or a HuggingFace Hub repo ID — the checkpoint is downloaded automatically when needed.
Parameters¶
run_dir : str Local path produced by Trainer.fit() or a Hub repo ID such as "my-org/dantinox-dante". seed : int RNG seed used for sampling (default 42). token : str, optional HuggingFace access token for private repositories. revision : str, optional Branch, tag, or commit SHA to download from the Hub.
Raises¶
CheckpointError If the checkpoint cannot be found locally or downloaded from the Hub.
Examples¶
gen = Generator("runs/run_20260101_120000") # local gen = Generator("my-org/dantinox-dante") # HF Hub gen = Generator("my-org/private-model", token="hf_…") # private Hub text = gen.generate("Nel mezzo del cammin ") print(text)
Source code in dantinox/generator.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
Functions¶
__init__ ¶
__init__(
run_dir: str,
*,
seed: int = 42,
token: str | None = None,
revision: str | None = None,
) -> None
Source code in dantinox/generator.py
generate ¶
generate(
prompt: str,
*,
max_new_tokens: int = 150,
greedy: bool = False,
top_k: int | None = None,
top_p: float | None = None,
temperature: float = 1.0,
use_cache: bool = True,
) -> str
Generate text continuing from prompt.
Parameters¶
prompt : str The input prefix. max_new_tokens : int Number of tokens to generate (default 150). greedy : bool Use greedy decoding instead of sampling (default False). top_k : int, optional Keep only the top-k logits before sampling. top_p : float, optional Nucleus sampling threshold. temperature : float Softmax temperature (default 1.0). use_cache : bool Enable KV-cache for faster generation (default True).
Returns¶
str The full generated string (prompt + continuation).
Source code in dantinox/generator.py
generate_batch ¶
generate_batch(
prompts: list[str],
*,
max_new_tokens: int = 150,
greedy: bool = False,
top_k: int | None = None,
top_p: float | None = None,
temperature: float = 1.0,
use_cache: bool = True,
) -> list[str]
Generate text for multiple prompts in a single batched forward pass.
Shorter prompts are left-padded with zeros so all share the same sequence length. This runs a true batch through the model, so throughput scales with GPU parallelism.
Parameters¶
prompts : list[str] Input prefixes to generate from. max_new_tokens : int Tokens to generate per prompt (default 150). greedy : bool Greedy decoding (default False). top_k : int, optional Top-k filtering before sampling. top_p : float, optional Nucleus sampling threshold. temperature : float Softmax temperature (default 1.0). use_cache : bool Enable KV-cache (default True).
Returns¶
list[str] Generated strings (prompt + continuation) in the same order as prompts.
Source code in dantinox/generator.py
stream ¶
stream(
prompt: str,
*,
max_new_tokens: int = 150,
greedy: bool = False,
top_k: int | None = None,
top_p: float | None = None,
temperature: float = 1.0,
) -> Iterator[str]
Stream generated tokens one at a time as they are produced.
Uses the KV-cache path: the prompt is prefilled in one forward pass, then each new token is decoded individually. Each yield returns the string for one generated token (may be a character or a BPE subword).
Parameters¶
prompt : str The input prefix. max_new_tokens : int Maximum number of tokens to generate (default 150). greedy : bool Greedy decoding (default False). top_k : int, optional Top-k filtering. top_p : float, optional Nucleus sampling threshold. temperature : float Softmax temperature (default 1.0).
Yields¶
str Decoded string for each generated token.
Examples¶
gen = Generator("runs/my_run") for chunk in gen.stream("Nel mezzo", max_new_tokens=50): ... print(chunk, end="", flush=True)
Source code in dantinox/generator.py
BenchmarkRunner¶
dantinox.bench.BenchmarkRunner ¶
Benchmarks one or more DantinoX run directories.
Parameters¶
runs_dir : str Directory containing run sub-directories (default "runs"). seq_lens : list[int], optional Sequence lengths to test for throughput scaling. batch_sizes : list[int], optional Batch sizes to test for memory/throughput scaling.
Examples¶
runner = BenchmarkRunner("runs") df = runner.run() df.to_csv("results.csv", index=False)
Source code in dantinox/bench.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
Functions¶
__init__ ¶
__init__(
runs_dir: str = "runs",
*,
seq_lens: Sequence[int] | None = None,
batch_sizes: Sequence[int] | None = None,
) -> None
Source code in dantinox/bench.py
run ¶
Run benchmarks and return a DataFrame.
Parameters¶
run_names : list[str], optional Subset of run names to evaluate. Benchmarks all runs if omitted. out_csv : str, optional Write results to this CSV path.
Returns¶
pandas.DataFrame
Raises¶
BenchmarkError If the runs directory does not exist.
Source code in dantinox/bench.py
Plotter¶
dantinox.plotting.Plotter ¶
Generates all DantinoX benchmark plots from a CSV produced by :meth:~dantinox.BenchmarkRunner.run.
Runs the four bundled plot modules (perf, insights, 3d, 3d_dkv) and writes 16 PNG files to out_dir.
Parameters¶
in_csv : str Path to benchmark_results.csv. out_dir : str Directory where PNGs are written (created if absent). batch_csv : str, optional Path to batch_sweep_results.csv for the batch-throughput plot. If omitted, that figure is replaced with a placeholder.
Raises¶
PlotError If the CSV is missing or a group name is invalid.
Examples¶
from dantinox import BenchmarkRunner, Plotter BenchmarkRunner("runs").run(out_csv="benchmark_results.csv") Plotter("benchmark_results.csv").run()
Source code in dantinox/plotting.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
Functions¶
__init__ ¶
run ¶
Generate plots and save them as PNGs.
Parameters¶
groups : list[str], optional Subset of ["perf", "insights", "3d", "3d_dkv"]. Generates all four if omitted.
Returns¶
dict[str, list[str]] Mapping of group name → list of figure function names that ran.
Raises¶
PlotError If the benchmark CSV is not found or a group name is invalid.
Source code in dantinox/plotting.py
Hub¶
Push, pull, and directly load checkpoints from HuggingFace Hub.
Optional dependency
Install with pip install "dantinox[hub]" or pip install huggingface-hub.
Direct loading — no pull step needed
dantinox.hub.resolve_checkpoint ¶
resolve_checkpoint(
path_or_repo: str,
*,
token: str | None = None,
revision: str | None = None,
) -> str
Return a local directory path for path_or_repo.
If path_or_repo is an existing local directory it is returned unchanged. Otherwise it is treated as a HuggingFace Hub repo ID (e.g. "my-org/dantinox-dante") and the checkpoint is downloaded via :func:pull before returning the local cache path.
Parameters¶
path_or_repo: Local run directory or HuggingFace Hub repo ID. token: HuggingFace access token for private repositories. revision: Branch, tag, or commit SHA to download.
Returns¶
str Absolute path to a local directory suitable for passing to Generator(), Transformer.from_pretrained(), etc.
Source code in dantinox/hub.py
dantinox.hub.push ¶
push(
run_dir: str,
repo_id: str,
*,
private: bool = False,
token: str | None = None,
commit_message: str | None = None,
) -> str
Upload a run directory to a HuggingFace Hub model repository.
Creates the repository if it does not exist. Only the core checkpoint files are uploaded (config.yaml, tokenizer.json, model_weights.msgpack, best_model_weights.msgpack, model_summary.json). Log files are excluded.
Parameters¶
run_dir : str Local path to a DantinoX run directory. repo_id : str Hub repository in the form "owner/repo-name". private : bool Create the repository as private (default False). token : str, optional HuggingFace access token. Falls back to the HF_TOKEN environment variable or the cached login token. commit_message : str, optional Commit message for the upload (auto-generated if omitted).
Returns¶
str URL of the Hub repository after the upload.
Raises¶
ImportError If huggingface_hub is not installed.
Source code in dantinox/hub.py
dantinox.hub.pull ¶
pull(
repo_id: str,
*,
local_dir: str | None = None,
token: str | None = None,
revision: str | None = None,
) -> str
Download a DantinoX checkpoint from HuggingFace Hub.
Parameters¶
repo_id : str Hub repository in the form "owner/repo-name". local_dir : str, optional Where to store the downloaded files. Defaults to the HuggingFace cache directory (~/.cache/huggingface/hub/...). token : str, optional HuggingFace access token for private repositories. revision : str, optional Git revision (branch, tag, or commit SHA) to download.
Returns¶
str Path to the local directory containing the checkpoint. Pass this directly to Generator(run_dir).
Raises¶
ImportError If huggingface_hub is not installed.
Source code in dantinox/hub.py
Core Modules¶
Internal implementation. Import directly when you need low-level access.
Model Architecture¶
Core Transformer components — Transformer, Block, Attention (MHA/GQA/MLA), MoE, and MLP.
core.model ¶
Classes¶
Transformer ¶
Bases: Module
Source code in core/model.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
Functions¶
from_pretrained classmethod ¶
from_pretrained(
path_or_repo: str,
rngs: Rngs | None = None,
*,
best: bool = True,
token: str | None = None,
revision: str | None = None,
) -> Transformer
Load a trained Transformer from a local directory or HuggingFace Hub.
Parameters¶
path_or_repo: Local path produced by Trainer.fit() or a Hub repo ID such as "my-org/dantinox-dante". The checkpoint is downloaded automatically when a Hub ID is given. rngs: PRNG state for initialisation. Defaults to nnx.Rngs(0). best: When True (default), loads best_model_weights.msgpack if it exists, otherwise falls back to model_weights.msgpack. token: HuggingFace access token for private repositories. revision: Branch, tag, or commit SHA to download from the Hub.
Source code in core/model.py
Normalisation¶
RMSNorm is the alternative to nnx.LayerNorm selected when norm_type = "rmsnorm".
core.block.RMSNorm ¶
Bases: Module
Root Mean Square Layer Normalisation (Zhang & Sennrich, 2019).
Faster than LayerNorm — no mean subtraction, no bias — with identical empirical performance on modern LLMs (LLaMA, Mistral, Gemma, …).
Source code in core/block.py
Model Output¶
Transformer.__call__ returns a ModelOutput NamedTuple — supports both attribute access and positional unpacking.
core.output.ModelOutput ¶
Bases: NamedTuple
Named return type for Transformer.__call__.
Supports both attribute access and positional unpacking so existing code that destructures the tuple continues to work unchanged::
# Named (preferred)
out = model(x, ...)
loss = cross_entropy(out.logits, targets) + cfg.alpha * out.aux_loss
# Positional (backward-compatible)
logits, kv_caches, aux_loss = model(x, ...)
Source code in core/output.py
LoRA Adapters¶
LoRAParam is a distinct NNX variable type that freezes base weights at the type level. LoRALinear is a drop-in replacement for nnx.Linear with a trainable low-rank delta.
core.lora.LoRAParam ¶
core.lora.LoRALinear ¶
Bases: Module
Drop-in replacement for nnx.Linear with frozen base weight and trainable low-rank delta.
The effective weight is W_eff = W_base + (alpha / rank) * A @ B, where A is initialised with scaled Gaussian noise and B with zeros, so the adapter contributes nothing at initialisation.
Source code in core/lora.py
Functions¶
__init__ ¶
__init__(
in_features: int,
out_features: int,
*,
rank: int = 8,
alpha: float = 16.0,
dropout_rate: float = 0.0,
use_bias: bool = False,
rngs: Rngs,
) -> None
Source code in core/lora.py
__call__ ¶
Source code in core/lora.py
merge_weights ¶
Sharding Utilities¶
SPMD data-parallel helpers built on jax.sharding. Pass n_devices in Config to activate automatically, or call these directly for custom sharding strategies.
core.sharding ¶
Functions¶
make_mesh ¶
Create a 1-D data-parallel mesh.
Parameters¶
n_devices: Number of devices to use. 0 (default) means all available local devices.
Source code in core/sharding.py
replicate ¶
shard_batch ¶
Shard pytree along its leading (batch) axis across all devices in mesh.
Configuration¶
The Config dataclass is the single source of truth for all architectural and training hyperparameters.
core.config ¶
Classes¶
Config dataclass ¶
Source code in core/config.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
Generation Engine¶
Autoregressive inference with static KV-cache management, jax.lax.fori_loop token loop, and sampling strategies (greedy, Top-K, Top-P).
core.generation ¶
Tokenizers¶
Character-level and Byte-Level BPE tokenizers with save/load support.
utils.tokenizer ¶
Classes¶
Tokenizer ¶
Bases: Protocol
Source code in utils/tokenizer.py
CharTokenizer ¶
Source code in utils/tokenizer.py
BPETokenizer ¶
Source code in utils/tokenizer.py
Functions¶
get_tokenizer ¶
load_tokenizer_from_file ¶
Load a tokenizer that was previously saved with tokenizer.save().
Source code in utils/tokenizer.py
CLI Reference¶
The dantinox command provides eight subcommands:
| Subcommand | Description |
|---|---|
train | Train a model from a config and corpus |
generate | Generate text from a checkpoint |
find-lr | Run the LR range test and suggest a learning rate |
push | Upload a checkpoint to HuggingFace Hub |
pull | Download a checkpoint from HuggingFace Hub |
sweep | Run a W&B Bayesian hyperparameter sweep |
benchmark | Benchmark throughput and FLOPs for run directories |
plot | Generate figures from benchmark results |
dantinox --version
dantinox --help
dantinox train --help
dantinox find-lr --help
dantinox push --help
train¶
dantinox train
--config PATH YAML config file (default: configs/default_config.yaml)
--data_path PATH Training corpus
--run_dir PATH Output directory (auto-generated if omitted)
--wandb_project NAME W&B project for logging
--resume Resume from last checkpoint in --run_dir
--<field> VALUE Override any Config field (e.g. --lr 3e-4 --use_bf16 True)
generate¶
dantinox generate
--run_dir PATH Run directory with config + weights (required)
--prompt TEXT Input prefix (default: "Nel mezzo del cammin ")
--max_new_tokens N Tokens to generate (default: 150)
--greedy Greedy decoding
--temperature FLOAT Softmax temperature (default: 1.0)
--top_k INT Top-k sampling
--top_p FLOAT Nucleus sampling threshold
--no_cache Disable KV cache
--seed INT RNG seed (default: 42)
find-lr¶
dantinox find-lr
--config PATH YAML config file
--data_path PATH Training corpus (required)
--min_lr FLOAT Start LR (default: 1e-7)
--max_lr FLOAT End LR (default: 1.0)
--num_steps INT Sweep steps (default: 100)
--plot Save a lr_finder.png loss curve
--plot_out PATH Custom output path for the PNG
--<field> VALUE Override any Config field
push¶
dantinox push
--run_dir PATH Local run directory to upload (required)
--repo NAME Hub repo id, e.g. my-org/my-model (required)
--private Create a private repository
--token TOKEN HuggingFace access token
--message TEXT Commit message