A 32x32 RGB diffusion model written from scratch in PyTorch: no diffusers, no pretrained weights, no Stable Diffusion. A small U-Net, a 300-step DDPM schedule and 2,655 sprites extracted from four generated texture sheets.
It trains from scratch in 24 minutes on one consumer GPU and produces sprites that are recognisably items. It also has a specific, measurable failure, described below, which is the most interesting part of the project.
| Model | two-stage U-Net, 3,435,395 parameters, 32x32x3 in and out |
| Diffusion | DDPM, epsilon prediction, linear beta 1e-4 to 0.02, 300 steps |
| Training | 1,000 epochs x 41 batches = 41,000 steps, AdamW, batch 64, ≈0.02 MSE |
| Data | 2,655 sprites cut from four texture-sheet images, no copyrighted game assets |
| Hardware | one AMD RX 9070 XT, 16 GB |
Not a sprite generator you should use for a game. The output is limited by a 2,655-image dataset, a 32x32 canvas and a class-agnostic model with no text conditioning, and none of that is hidden below.
process_sheets.py thresholds each sheet, erodes to break diagonal contacts, takes external
contours, rejects anything outside 10 to 55 pixels, then centres every crop on a 32x32 black
canvas. It produced 2,655 sprites, and measuring them explains most of what the model does
later:
| Measurement | Value |
|---|---|
| sprites | 2,655 (664 / 665 / 663 / 663 across the four sheets) |
| bounding box, median | 20 wide x 23 high, inside a 32x32 canvas |
| aspect w/h | median 0.875, p10 0.60, p90 1.04 |
| fill extent (ink ÷ bounding box) | median 0.724 |
| ink mass (ink ÷ canvas) | median 27.9%, maximum 49.5% |
| downscaled by the extractor | 4 of 2,655 (0.2%) |
Forty-nine of them at random, which is where the class mix becomes obvious: about a third are chibi characters, the rest are swords, potions, shields, armour, bows, coins and keys.
Two consequences, both measurable:
- Position carries no information. Every sprite sits at the centre of its canvas, so the dataset's pixel-wise mean is a soft centred rectangle and an unconditional model can fit it without ever looking at the noise for placement.
- What varies is coverage, not elongation. Splitting the set by how much of its bounding box each sprite fills gives the shape of the problem:
| group | share | aspect median | ink median |
|---|---|---|---|
| thin (fill < 0.55) | 24.3% | 1.00 | 16.2% |
| middle | 51.3% | 0.83 | 28.9% |
| solid (fill > 0.80) | 24.4% | 0.78 | 33.0% |
corr(aspect, extent) = -0.450: the sparser a sprite is, the more square its bounding box.
44.5% of sprites have a near-square bounding box with a median fill of 0.553. That is what a
sword drawn at 45 degrees looks like once it is cropped and centred: it spans the same width and
height as a chest does.
So at 32x32 a thin diagonal and a book occupy the same square envelope, and the only thing separating them is which pixels inside that envelope are lit. That is high-frequency information, and it is the first thing an MSE-trained model at this resolution averages away.
Trained on that dataset, the model produces one kind of object. Counting families across 256 draws: shields with crests about 45%, books and tomes 18%, armour pieces 14%, chests 12%, unreadable plaques 10%, and the remaining ~1% is everything else, which is where the few coherent characters live.
Absent, though the sheets are full of them: swords, bows, staves, potions, food, gems, keys, boots. The original run of this same configuration produced a narrower slice still, mostly books and chests, which is what prompted the investigation.
This is not a class imbalance. The training set is roughly a third humanoid characters, with swords, potions, shields, armour, bows, coins and keys filling the rest, and the model emits characters in about 1.5% of draws. The dominant class in the data is nearly absent from the output.
It is also not "the model likes chests". It is that MSE at 32x32 is easiest to reduce on solid, high-contrast, rim-bounded shapes. A shield or a chest is a filled body with a strong outline, which is cheap to predict. A character is thin limbs, gaps between them and a head three pixels wide; a sword is a one-pixel diagonal. Those regions dominate the loss gradient least and get washed out, so the surviving family is the solid-object subset of the dataset, not the dataset.
Two things in the numbers follow from that and are worth reading together:
| dataset | generated | |
|---|---|---|
| fill extent median | 0.724 | 0.891 |
| characters, by eye | roughly 1 in 3 | about 3 in 256 |
The model did not lose the ability to draw a character. When one appears it is coherent, with a head, body, arms and whatever it is holding. It lost the prior, because the shapes that survive training are the ones where the pixels are easy to be right.
Those 15 are every non-boxy draw out of 256, isolated by the same shape statistics audit.py
uses. About three of them are readable characters. The original run of this configuration
produced none, and its own progression is in assets/original-epoch-020.png,
assets/original-epoch-100.png and assets/original-epoch-1000.png.
audit.py turns that from an impression into two distributions measured by the same code, from
the final checkpoint, 64 samples, one seed:
| measurement | dataset (2,655) | generated (64) |
|---|---|---|
| aspect median | 0.875 | 0.957 |
| aspect p10-p90 | 0.60 – 1.04 | 0.85 – 1.05 |
| fill extent median | 0.724 | 0.891 |
| ink mass median | 27.9% | 42.6% |
| boxy share | 14.9% | 89.1% |
| diversity (mean pairwise L1) | 0.114 | 0.100 |
Read the p10-p90 rather than the median. The generated aspect range is 0.85 to 1.05: the model has not shifted toward square objects, it can no longer produce anything else. Fill extent is 0.891 against 0.724, so each output also crams its ink into its box rather than floating inside it.
The samples are also not identical to each other. Diversity is 0.100 against the dataset's 0.114, so the palette, rim and interior texture variety survive almost intact. What collapsed is one dimension, the shape.
Sixty-four draws, and every one of them is the same kind of object: a square container with a rim and a centred motif. Shields, books with clasps, chests with lids and locks, the occasional plaque. There is real variation inside that family, in colour and ornament, and no swords, no potions, no bottles, nothing thin or diagonal. Calling this mode collapse would be wrong; calling it shape-space collapse is accurate, and the distinction matters because the fix is different.
The mean image makes the mechanism visible without any statistics at all: averaged over 2,655 centred sprites it is a brown rectangle filling the middle of the canvas, and the generated samples are that rectangle with sharper edges.
That image is not a coincidence of this dataset's art style. It is what centring 2,655 same-sized sprites produces, whatever the sprites look like, and it is the target the model can hit without using the noise at all.
pip install -r requirements.txt
python process_sheets.py # data/raw/*.jpg -> data/processed/ (2,655 sprites)
python train.py # 1,000 epochs, checkpoints/final.pt
python sample.py --ckpt checkpoints/final.pt --n 16 --out samples/grid.png
python audit.py --ckpt checkpoints/final.pt --n 64
# the experiment in "Does the augmentation help?"
python train.py --epochs 1000 --jitter 6 --scale 0.75 1.0 --out checkpoints_aug
python audit.py --ckpt checkpoints_aug/final.pt --n 64 --data-jitter 6 --data-scale 0.75 1.0CPU works for the tests and for a two-epoch smoke run; the 1,000-epoch training run wants a GPU.
On Windows with an RDNA3 or RDNA4 card, AMD publishes ROCm wheels that run this unchanged:
pip install --extra-index-url https://stable.repo.amd.com/rocm/whl-next/ \
"torch[device-gfx1201]==2.15.0+rocm10.2.0"pip install -r requirements-dev.txt
pytest -q # 14 tests, CPU only, ~2sThey cover the things that fail quietly: the parameter count the README quotes, that the
schedule is monotone and bounded, that add_noise matches its closed form at both ends, that the
posterior variance is always at or below beta_t (the difference between the two sampling
choices), that shape statistics return the right numbers on a block, a bar and a diagonal blade,
that jitter moves a sprite without losing it, and that sampling is deterministic for a seed.
gsde/
config.py model and diffusion settings, serialised into checkpoints
unet.py residual blocks and the two-stage U-Net
diffusion.py schedule, forward noising, reverse sampling
data.py sprite loader with optional jitter and scale augmentation
metrics.py aspect / extent / ink / boxy / diversity — the numbers in this README
checkpoint.py weights plus configs, so a sampler cannot guess the schedule wrong
render.py nearest-neighbour PNG grids
process_sheets.py texture sheets -> 2,655 centred 32x32 sprites
train.py training loop
sample.py generate a grid from a checkpoint
audit.py dataset vs generated shape distributions
audit_coherence.py palette, edge energy and connected components, so variety is
not mistaken for coherence
The model is small enough to ship somewhere. Measured here, one model, 300-step DDPM:
| parameters | 3,435,395 |
| weights on disk | 13.7 MB fp32, 6.9 MB fp16, 3.4 MB int8 |
| sampling, batch 1 | 712 ms per sprite |
| sampling, batch 64 | 55 ms per sprite, 253 MB peak VRAM |
The batch-1 figure is the interesting one. 300 forwards of a 32x32 image costs about 2.4 ms per step, which is mostly kernel launch overhead rather than arithmetic, so a far weaker device is not much slower: it is already overhead-bound on a desktop GPU. A current phone GPU sits in the same order of magnitude, which is roughly a second per sprite at 300 steps, and well under that with a 50-step deterministic sampler.
That last number is arithmetic from published mobile peak FLOPS, not a measurement on a phone. It is also the argument for keeping the model this small instead of widening it.
No. It made the output worse, and it also degraded the training data, so this run is not a clean test of the hypothesis either.
Same 1,000 epochs, same sampling seed, --jitter 6 --scale 0.75 1.0, compared against a reference
drawn from the same augmented loader (256 draws) rather than against the centred dataset:
| measurement | dataset (centred) | baseline model | augmented reference | augmented model |
|---|---|---|---|---|
| aspect median | 0.875 | 0.957 | 0.947 | 0.952 |
| aspect p10-p90 | 0.60 – 1.04 | 0.85 – 1.05 | 0.75 – 1.13 | 0.85 – 1.10 |
| fill extent median | 0.724 | 0.891 | 0.719 | 0.857 |
| ink mass median | 27.9% | 42.6% | 22.9% | 42.5% |
| boxy share | 14.9% | 89.1% | 33.6% | 78.1% |
| diversity (mean pairwise L1) | 0.114 | 0.100 | 0.118 | 0.133 |
| median palette (distinct colours) | 349 | 498 | 280 | 536 |
| edge energy | 0.0492 | 0.0528 | 0.0427 | 0.0651 |
Read the palette and edge rows. The sprites the model was fed lost a fifth of their distinct colours (349 to 280) and 13% of their edge energy before training even started, because scaling a 20-pixel sprite to 75% with nearest-neighbour resampling throws away the one- and two-pixel features that make a sword a sword. The model then learned from sprites that were already mush.
Diversity rose from 0.100 to 0.133, which is why the first reading of this run called it an improvement. That was the wrong instrument: diversity measures difference, not coherence, so a model that produces more distinct blobs scores higher. Sampled side by side, the baseline is the one where you can still tell a shield from a sword, and it is the one that occasionally draws a character with legible arms. The augmented run has more silhouettes and fewer recognisable objects.
Verdict. This is not a fair test of the centring hypothesis, and it should not be counted as evidence either way. The honest version of the experiment is jitter alone: pasting the sprite at a different offset moves it without touching a single pixel, so the training data stays intact. That run has not been done.
None of the following has been run. It is a plan, not a result.
Conditioning. The model has no way to know that a sword was requested, so on any given draw its best strategy is the marginal. Rough family labels (sword, potion, book, shield, chest, helm, food, coin, and so on) would turn an unconditional mixture into a conditional one, which is the only fix that addresses "it produces the average object" directly. Label effort, no extra compute.
x0-prediction with L1. Instead of predicting the noise and squaring the error, predict the image and take the absolute error. One run, no new parameters, and thin structures are punished less gently than squared error punishes them.
More sheets. The cheapest information you can buy for the same 45 minutes. More modes mean less pressure on any single one, and the extraction pipeline is already scripted.
Not more parameters. At 3.4M parameters over 2,655 images the model can already overfit the whole set, and diversity measures at 0.100 against the dataset's 0.114, so it is not failing to represent the palette or the texture. Widening the network buys memorisation first. If capacity were the limit, the diversity figure would not look like the data's.
Repeat the run on other seeds. One training run is one draw, and in an unconditional model at this scale the set of surviving modes is sensitive to initialisation. A single grid, mine or the original's, is not enough to attribute the difference between them.
Built in June 2026 and moved to this account in November 2026, so the commit history here starts at the move.
MIT licensed.