Training on industrial meshes
Point subsampling, batching of uneven meshes, mixed precision and several GPUs.
Outcome: Train on runs with millions of points by sampling a subset per step; batch runs with different point counts with a collate_fn; choose between sampling, mixed precision, checkpointing and DDP for a given memory problem; weight a sampled point by its measure over its probability of being kept.
Builds on Normalising and the training loop, Attention at scale: Transolver
The aircraft’s 2,000 surface points per run train on a laptop. An industrial car run can have millions of surface points and more than a hundred million volume cells (about 8.8 million and 160 million per car in DrivAerML), and a dataset can hold hundreds of runs. Often one run no longer fits in the memory of a GPU (the graphics processor that trains the network), let alone a batch. The linear-cost models of the previous unit keep one forward pass affordable; this lesson is about the training around them.
Sampling points within runs
At each training step, a random subset of points is drawn from each run and the loss is computed on those, so each step fits in memory. AB-UPT, for example, trains on 16,000 surface and 16,000 volume points per run of DrivAerML; sizes vary with the model and the GPU. Over many steps the samples spread across the mesh, though on the largest meshes many points may never be drawn. Sampling suits models with query decoders, which do not need the whole mesh to make a prediction at a point.
A sum over mesh points stands for an integral only if each point carries the area or volume it represents (attention at scale). With a sample, a point kept with probability is weighted by : the Horvitz-Thompson estimator of survey sampling (Horvitz and Thompson 1952), whose expected sum over the kept points is the full mesh’s weighted sum, whatever the sampling law, provided every point can be drawn.
Drawing of points uniformly, without replacement, gives . Drawn with replacement, as in Example 7-1, a point is counted each time it is drawn, is its expected number of draws, and the same weight keeps the sum unbiased. Keeping each cell with probability proportional to its area, capped at 1, keeps every large cell and stops a finely meshed region from crowding out the rest. A weighted mean is a ratio of two such sums (the Hájek estimator) and is not unbiased in general, though its bias shrinks as the sample grows.
Sampling is for training only. Held-out runs are evaluated on all points, in chunks, so the metric matches what is reported (measuring error). The latent surrogate of Example 6-4 already did both: 512 of each run’s 2,000 points per step, all 2,000 on the validation runs.
Batching runs of different sizes
A PyTorch DataLoader stacks the items of a batch into one tensor, which fails when runs have different numbers of points. A collate_fn decides how a batch is assembled; here it draws the same number of points from every run.
import torch
from torch import nn
from torch.utils.data import DataLoader
# Stand-in for 20 runs with different point counts.
# Columns: x, y, z, nx, ny, nz, Cp. In practice a Dataset
# would read each run from disk (Zarr, HDF5).
runs = [torch.randn(200_000 + 10_000 * i, 7)
for i in range(20)]
def sample_points(batch, k=16_384):
# The same number k of random points from every run.
picked = torch.stack(
[run[torch.randint(len(run), (k,))] for run in batch])1
# Inputs (B, k, 6), targets (B, k, 1).
return picked[..., :6], picked[..., 6:]
loader = DataLoader(runs, batch_size=4, shuffle=True,
collate_fn=sample_points)2
dev = "cuda" if torch.cuda.is_available() else "cpu"
model = nn.Sequential(nn.Linear(6, 128), nn.SiLU(),
nn.Linear(128, 1)).to(dev)
opt = torch.optim.AdamW(model.parameters(), lr=1e-3)
for x, y in loader: # one epoch
x, y = x.to(dev), y.to(dev)
# Mixed precision: matrix products in bfloat16.
with torch.autocast(device_type=dev, dtype=torch.bfloat16):3
pred = model(x)
loss = nn.functional.mse_loss(pred.float(), y)4
opt.zero_grad()
loss.backward()
opt.step()
print(x.shape) # (4, 16384, 6)5
torch.randintdrawskindices with replacement, so a point can be drawn twice in one step. Every run now giveskrows, sotorch.stackcan stack them.- The
DataLoaderhands each list of four runs tosample_pointsinstead of trying to stack runs of different sizes. - Inside
torch.autocastthe matrix products of the forward pass run in bfloat16; the weights stay in 32-bit. pred.float()brings the prediction back to 32-bit, so the loss is computed in full precision.- The shape is (runs in the batch, sampled points, input channels).
Example 7-1 prints torch.Size([4, 16384, 6]) for the last batch: four runs of between 200,000 and 390,000 points each, cut to the same 16,384.
Memory and several GPUs
Mixed precision. Inside torch.autocast, matrix products run in bfloat16, a 16-bit format with the same range as 32-bit floats. Those activations take half the memory and GPUs with bf16 support can run faster; the weights and optimiser state stay in 32-bit.
Activation checkpointing. Backpropagation stores every layer's intermediate results. torch.utils.checkpoint keeps only some and recomputes the rest during the backward pass: less memory, more compute.
Distributed data parallel (DDP) puts a copy of the model on each GPU, gives each a different batch, and averages the gradients at every step: more runs per second and, at the same batch per GPU, the same memory per GPU. When a single run is too large even for sampling, the points of one mesh can be split across GPUs, as Transolver++ does.
DrivAerML shows the scale: 500 car variants with surface and volume fields from scale-resolving CFD. AB-UPT trains on it with surface and volume points sampled per step, and its authors report training on a single GPU in less than a day.
With several outputs (surface and volume, pressure and velocity), the weight of each in the loss is a hyperparameter, tuned on validation like the learning rate.
A training job whose validation error has stopped improving is unlikely to gain from more steps. In the learning curves (overfitting), a growing gap asks for more designs or regularisation, two high curves usually for a larger model.
More designs are the expensive remedy. The next lesson is about getting them at a lower cost.