Skip to content

Training internals

The estimator covers normal use. These parts are exported for custom training and may change between versions.

TrainConfig

A dataclass with every setting of a training run. FloDR(advanced=...) overrides its fields by name.

@dataclass
class TrainConfig:
iters: int = 700
n_layers: int = 4
hid: int = 256
lr: float = 1e-3
# negative samples per edge
neg: int = 12
min_dist: float = 0.01
w_rep: float = 5.0
w_recon: float = 2.0
# mid-near attraction
w_global: float = 0.6
w_stress: float = 0.0
# densMAP's density term: the correlation of each cell's log neighbour radius, input
# vs layout; trades local recall for density preservation, so off by default
w_dens: float = 0.0
# 1 = ordinal hinge on ranks, 0 = log-ratio stress
stress_ordinal: int = 1
# "euclid" | "jaccard" (needs stress_X in {0, 1})
stress_metric: str = "euclid"
w_nll: float = 0.5
# keep recon from reshaping the layout
recon_detach: bool = True
# ramp recon/nll/global in over this fraction of iters
warmup_frac: float = 0.3
# rows per iter for recon + NLL, 0 = full batch
aux_batch: int = 512
# trailing fraction of iters run full-batch
aux_full_frac: float = 0.2
# kNN edges per iter (0 = full batch, -1 = full batch up to EDGE_BATCH_CAP edges and
# capped above)
edge_batch: int = 0
# couplings of the post-hoc conditional density tail
cond_tail: int = 2
cond_iters: int = 1500
# input noise for the density, norm about this times the median 1-NN distance
# (0 = none); with it the density cannot gain by putting cells on a sheet
density_noise: float = 0.5
# "energy": tail chosen by the energy certificate on held-out cells; "coverage": by
# l-rank coverage, which keeps draws too wide; "auto": energy only with w_lin
tail_select: str = "auto"
# >0 restricts the sketch to the first N dims (the PCA head)
sketch_head: int = 0
# fixed k-dim sketch conditioner (0 = off)
sketch: int = 0
device: str = field(default_factory=default_device)
# the step as one CUDA graph: None = wherever supported, False = eager; not bitwise
# equal to eager. FLODR_NO_GRAPHS=1 turns it off too
cudagraphs: bool = None
# torch.compile the full-batch loss inside the graph
graph_compile: bool = True
# checkpoint the recon-inverse pass, less memory, slower
checkpoint: bool = False
# embedding dimension
k: int = 2
# nested-dropout cuts
cuts: Tuple[int, ...] = (2, 2, 2, 4, 8, 16, 24, 32)
# torch CPU threads (0 = leave untouched)
threads: int = 0
# OneCycleLR at max_lr = 3 * lr
onecycle: bool = False
# torch.compile(step) on CPU, not for edge_batch
cpu_compile: bool = False
# torch.compile the edge-batched loss on CUDA; the draws stay eager
compile_edges: bool = False
# "bf16" or "fp16" hidden layers on the edge-batched step, output layers and Fourier
# phase in fp32; fp16 with loss scaling
half_edges: str = ""
# bf16 matmuls while training, fp32 everywhere else
bf16: bool = False
# "adam" | "nadam" | "muon" (lr ~0.02)
optim: str = "adam"
# "tanh" | "relu" | "silu"
act: str = "tanh"
# 1-hidden-layer conditioners
shallow: bool = False
# >0 bounds |log scale| per coupling
gate_max: float = 0.0
# >0 makes the bound per dim, this on the display and gate_max on the residual
gate_display: float = 0.0
# FloDR pads narrower inputs with unit-variance noise columns up to this width, the
# graph and the stress stay on X
pad: int = 0
# >0 adds a FourierShift to the couplings that write the display, at this weight
fine_eps: float = 0.0
# its own Adam step size, and the fraction of iters before it switches on
fine_lr: float = 1e-3
fine_start: float = 0.5
# log(1 + rho^2), rho = |F^-1(F(x) + J e) - (x + e)| / |e|, e the density's noise
# times h, so the draws are that noise; opt-in as it costs recall; needs noise_sd
w_lin: float = 0.0
# off until the layout has formed, then a linear ramp over lin_ramp of the iters
lin_start: float = 0.5
lin_ramp: float = 0.1
lin_h: Tuple[float, ...] = (0.5, 1.0)
lin_batch: int = 1024
# every lin_every-th step at lin_every times the weight: the same push through the
# momentum for a fraction of the kernel launches
lin_every: int = 2
# log(1 + rho^2 / lin_tau^2)
lin_tau: float = 1.0
seed: int = 0

raw_recipe

raw_recipe(seed=0, threads=None, **overrides)

Returns the TrainConfig the estimator starts from. Overriding iters without lr rescales the learning rate so that their product stays at 16, with the rate capped at 0.02.

train_flodr

train_flodr(
Xp, ei, ej, cfg: TrainConfig, rng, return_model=False, w=None,
stress_X=None, progress=False, noise_sd=None,
)

Trains a flow on whitened coordinates Xp and graph edges ei, ej. Returns the layout, the round-trip error and reconstruction errors, and the flow when return_model=True.

Flow

Flow(
d, n_layers=4, hid=256, sketch=0, act="tanh", shallow=False, sketch_head=0,
gate_max=0.0, gate_display=0.0, k=2,
)

The normalising flow, a stack of affine couplings. flow(x) maps whitened inputs to latent coordinates and flow.inverse(z) maps them back. flow.log_prob(x) gives the log density once the density is fitted.

Coupling

Coupling(
d, mask, hid, sketch=0, act="tanh", shallow=False, sketch_head=0,
gate_max=0.0, gate_display=0.0, k=2,
)

One affine coupling layer of the flow.

Data helpers

raw_coords(X, n_components=50, seed=0)
fuzzy_knn_graph(
Xp, k, n_iter=64, precomputed=None, return_dist=False, device=None,
info=None,
)
knn_search(Xp, k, exact=None, device=None, info=None)
global_pairs(Xp, count, rng)
find_ab(min_dist=0.1, spread=1.0)
preprocess(
X, n_components=50, seed=0, return_inverse=False, return_transform=False,
whiten=1.0,
)
FunctionDescription
raw_coordsthe PCA whitening, with its forward and inverse maps
fuzzy_knn_graphthe fuzzy neighbour graph used for training
knn_searchnearest neighbours of every point, exact on the GPU
global_pairsrandom pairs for the global terms
find_abthe parameters of the layout kernel
preprocessPCA to at most n_components components scaled to unit variance, optionally with its inverse and transform

Device helpers

default_device() -> str
native_bf16(device=None) -> bool
perf_cores() -> int
FunctionDescription
default_device"cuda" when PyTorch sees a GPU, otherwise "cpu"
native_bf16whether the device computes in bfloat16 natively
perf_coresthe number of performance cores, which FloDR uses on the CPU