traingen¶
traingen contains shared training utilities for design-generators packages that train models locally. It keeps reusable PyTorch Lightning CLI integration points out of individual model packages while leaving model-specific data modules, losses, and training loops in models/*.
Use traingen for generic training infrastructure such as LightningCLI helpers. Use traingen-parity for deterministic trace capture and reference/target comparison reports.
traingen.optim.KerasAdam implements the update order and epsilon placement used by Keras 2 Adam. CanvasVAE and PixelVAE use it when comparing TensorFlow training updates with their PyTorch models.
Install¶
uv sync --package traingen
Install from outside the workspace with pip's direct-reference subdirectory form:
pip install "traingen @ git+https://github.com/creative-graphic-design/design-generators.git#subdirectory=lib/traingen"
API Entry Points¶
Run a model-agnostic LightningCLI from a training YAML that declares
model.class_path and data.class_path.
uv run --package layout-flow --extra training \
traingen fit \
--config models/layout-flow/configs/training/smoke.yaml
Create package-local LightningCLI subclasses only when a model package needs custom CLI arguments or defaults that cannot live in YAML.
uv run --package traingen python
from traingen.lightning.cli import lightning_cli_class
class MyLightningCLI(lightning_cli_class()):
pass
Use traingen.lightning.steps for generic Lightning loss reduction and logging in model training steps.
from traingen.lightning.steps import finish_training_step, log_validation_loss
def training_step(self, batch, batch_idx):
losses = self.compute_losses(batch)
total, self.latest_step_trace = finish_training_step(self, losses, {})
return total
def validation_step(self, batch, batch_idx):
total = self.compute_validation_loss(batch)
log_validation_loss(self, total)
return total
Scope¶
- Keep reusable training utilities here only when more than one package that trains models locally can use them.
- Keep dataset-specific modules, model losses, condition policies, and checkpoint conversion in each model package.
- Keep deterministic comparison logic in
traingen-parityso regular training helpers do not depend on parity-only behavior.
Pointers¶
API Reference¶
Shared training utilities for design generator packages.
lightning ¶
Optional Lightning helpers for train-ourselves packages.
cli ¶
Shared LightningCLI entry point for train-ourselves packages.
lightning_cli_class ¶
lightning_cli_class() -> type[LightningCLI]
Return LightningCLI without importing Lightning at package import time.
Source code in lib/traingen/src/traingen/lightning/cli.py
11 12 13 14 15 | |
main ¶
main(args: list[str] | None = None) -> None
Run a model-agnostic LightningCLI from YAML class_path entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str] | None
|
Optional CLI arguments for tests. When omitted, LightningCLI reads
|
None
|
Raises:
| Type | Description |
|---|---|
SystemExit
|
If LightningCLI argument parsing fails. |
Source code in lib/traingen/src/traingen/lightning/cli.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
steps ¶
Generic Lightning training-step logging and reduction helpers.
ScalarLogger ¶
Bases: Protocol
Minimal scalar logging protocol implemented by Lightning modules.
Source code in lib/traingen/src/traingen/lightning/steps.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
log ¶
log(
name: str,
value: Float[Tensor, ""],
*,
prog_bar: bool = False,
on_step: bool | None = None,
on_epoch: bool | None = None,
batch_size: int | None = None,
) -> None
Log a scalar training metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric name. |
required |
value
|
Float[Tensor, '']
|
Scalar tensor value. |
required |
prog_bar
|
bool
|
Whether to show the value in the progress bar. |
False
|
on_step
|
bool | None
|
Whether to aggregate the value per step. |
None
|
on_epoch
|
bool | None
|
Whether to aggregate the value per epoch. |
None
|
batch_size
|
int | None
|
Batch size used by Lightning metric aggregation. |
None
|
Source code in lib/traingen/src/traingen/lightning/steps.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
sum_loss_values ¶
sum_loss_values(
losses: Mapping[str, Float[Tensor, ""]],
) -> Float[torch.Tensor, ""]
Sum scalar loss values with the canonical training reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
losses
|
Mapping[str, Float[Tensor, '']]
|
Mapping from metric names to scalar loss tensors. |
required |
Returns:
| Type | Description |
|---|---|
Float[Tensor, '']
|
Scalar tensor containing the sum of all loss values. |
Examples:
>>> import torch
>>> sum_loss_values({"a": torch.tensor(1.0), "b": torch.tensor(2.0)})
tensor(3.)
Source code in lib/traingen/src/traingen/lightning/steps.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
log_training_losses ¶
log_training_losses(
logger: ScalarLogger,
losses: Mapping[str, Float[Tensor, ""]],
total: Float[Tensor, ""],
*,
batch_size: int = 1,
) -> None
Log per-component and total training losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
ScalarLogger
|
Object exposing Lightning-compatible |
required |
losses
|
Mapping[str, Float[Tensor, '']]
|
Per-component scalar loss values. |
required |
total
|
Float[Tensor, '']
|
Total scalar training loss. |
required |
batch_size
|
int
|
Batch size used by Lightning metric aggregation. |
1
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in lib/traingen/src/traingen/lightning/steps.py
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 | |
finish_training_step ¶
finish_training_step(
logger: ScalarLogger,
losses: Mapping[str, Float[Tensor, ""]],
trace: Mapping[str, Shaped[Tensor, "..."]],
*,
batch_size: int = 1,
) -> tuple[
Float[torch.Tensor, ""],
dict[str, Shaped[torch.Tensor, "..."]],
]
Reduce losses, log training metrics, and append train_loss to a trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
ScalarLogger
|
Object exposing Lightning-compatible |
required |
losses
|
Mapping[str, Float[Tensor, '']]
|
Per-component scalar loss values. |
required |
trace
|
Mapping[str, Shaped[Tensor, '...']]
|
Training trace entries produced before the optimizer step. |
required |
batch_size
|
int
|
Batch size used by Lightning metric aggregation. |
1
|
Returns:
| Type | Description |
|---|---|
tuple[Float[Tensor, ''], dict[str, Shaped[Tensor, '...']]]
|
Total scalar loss and an updated detached trace mapping. |
Source code in lib/traingen/src/traingen/lightning/steps.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
log_validation_loss ¶
log_validation_loss(
logger: ScalarLogger,
total: Float[Tensor, ""],
*,
batch_size: int = 1,
) -> None
Log the canonical validation loss metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
ScalarLogger
|
Object exposing Lightning-compatible |
required |
total
|
Float[Tensor, '']
|
Scalar validation loss. |
required |
batch_size
|
int
|
Batch size used by Lightning metric aggregation. |
1
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in lib/traingen/src/traingen/lightning/steps.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
optim ¶
Optimizers with update semantics shared by training reproduction runs.
KerasAdam ¶
Bases: Optimizer
Adam with the update order and epsilon placement of Keras 2 Adam.
For step t the update is m += (g - m) * (1 - beta1),
v += (g**2 - v) * (1 - beta2), and
p -= m * alpha / (sqrt(v) + epsilon) with
alpha = lr * sqrt(1 - beta2**t) / (1 - beta1**t); all scalars are
float32. torch.optim.Adam instead adds epsilon after bias-correcting
sqrt(v).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Iterable[Parameter]
|
Parameters to optimize. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
tuple[float, float]
|
First and second moment decay rates. |
(0.9, 0.999)
|
eps
|
float
|
Denominator epsilon. |
1e-07
|
Examples:
>>> weight = nn.Parameter(torch.ones(2))
>>> optimizer = KerasAdam([weight], lr=0.1)
>>> weight.grad = torch.ones(2)
>>> optimizer.step()
>>> weight.detach().tolist()
[0.8999999761581421, 0.8999999761581421]
Source code in lib/traingen/src/traingen/optim.py
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 | |
__init__ ¶
__init__(
params: Iterable[Parameter],
lr: float = 0.001,
betas: tuple[float, float] = (0.9, 0.999),
eps: float = 1e-07,
) -> None
Register parameter groups and hyperparameters.
Source code in lib/traingen/src/traingen/optim.py
36 37 38 39 40 41 42 43 44 | |
step ¶
step(
closure: Callable[[], float | Float[Tensor, ""]]
| None = None,
) -> float | Float[torch.Tensor, ""] | None
Apply one update to every parameter with a gradient.
Source code in lib/traingen/src/traingen/optim.py
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 | |