Porting Backbones
This page is a contributor guide for adding a new timm backbone to Luximm.jl. The bar is numeric parity with timm on the released weights; the rest is mechanics.
If you are using Claude Code to drive the port, the same workflow is encoded as an agent-facing skill at skills/timm-to-lux/SKILL.md, installed with the luximm Claude Code plugin (see the README). This page covers the same ground for human contributors without those tools.
Acceptance criteria
A new backbone is mergeable when all four hold:
- Pretrained parity. Forward output of the Lux model with weights loaded via the closure returned by
create_pretrainedmatchestimm's forward output on the same input: features (num_classes = 0, and thein_chans = 1companion), logits and everyfeatures_onlytap are each within a relative L2 error‖y - ref‖ / ‖ref‖ofPARITY_RTOL = 1f-3. Existing models land far inside this, at1e-6to1e-5; ConvNeXt V2 Tiny, for example, sits near2e-6on features and logits. - Random-init recipe.
Lux.setupreproduces the distributions of the_init_weightsrecipetimmuses for the family, so a model trained from scratch starts where timm's would.test/test_init.jlchecks this statistically (per-layer standard deviations). See_CN_INITinsrc/Models/ConvNeXtCommon/Common.jl(truncated_normal(mean = 0f0, std = 0.02f0), mirroring timm'strunc_normal_(std = 0.02)) for a worked example. - State-dict round-trip. The mapping function consumes every PyTorch
state_dictkey. Mappings raise on a missing key so silent random-init leaks cannot pass parity by coincidence; see the assertion block at the end ofconvnextv2_mappinginsrc/Models/ConvNeXtV2/Model.jl. - Variant table entry and bookkeeping. New variants are registered in
<FAMILY>_VARIANTS(src/Models/<Family>/Config.jl) with their HuggingFace repo id and default class count, and the Phase 7 checklist below is complete.
Why the bar is 1e-3 relative L2
The suite runs on CPU in FP32, and its question is whether a model is close enough to timm that its pretrained weights transfer, not whether it reproduces timm to the last bit. Float32 round-off accumulates through the depth of a network, and summation order, BLAS thread counts and CPU kernels all move it, so a correct port lands between 1e-7 and 1e-5 relative L2 and varies a little from run to run. Real porting bugs (cross-correlation vs convolution, a wrong weight mapping, an axis permutation, a missing layer, the wrong activation) land at 1e-2 and above. The bar sits between the two, with room for the spread between machines.
Relative L2 is used everywhere, not max-abs-diff: pre-norm features on deep or wide backbones reach magnitudes in the thousands, and transformers carry a few outlier activations, so an absolute bar or one normalized by the largest element misjudges them.
The bar is loose enough to pass a few subtle mismatches, such as a norm epsilon of 1e-5 against 1e-6 or a population against a sample variance on large maps. Those do not change what a downstream head learns, but they are still wrong, so read the logged number, not just the pass: if a port that ought to be exact reports 1e-4 or more, find out why before calling it done. Bisect with the per-stage debugging fixtures described in Phase 1.
Reference example
src/Models/ResNetV2/ is the canonical, fully-worked port. Read it first. It exercises most of the shared utilities in one place:
- Pre-activation residual blocks.
- Weight-standardized convolutions (
std_convfromLuximm.Layers). Layers.LuximmGroupNormwith explicit epsilon (gn_act).adapt_input_convstem adaptation for non-RGB inputs.- The full mapping/loader pattern that every other family follows.
If your port can be expressed as parameter-table changes on top of an existing family, do that. Adding a new variant to BIT_VARIANTS or CONVNEXTV2_VARIANTS is a one-row PR. Adding a new family is the multi-file workflow below.
Workflow
Phase 1: Capture a parity fixture
Each port starts with an HDF5 fixture produced by a small Python sidecar in test/parity/. The sidecar uses the shared _dump_common.py helpers and must be named dump_<family>_io.py (scripts/parity_sweep.sh finds it by that name). Look at test/parity/dump_resnet_io.py for the template.
The fixture stores /input (deterministic random input, PyTorch NCHW layout, a batch of two samples) and /output/features plus optional /output/logits. Its /state_dict group is an empty placeholder: the parity tests load the released weights from HuggingFace through create_pretrained, so they cover the production loading path. The Julia side reads the fixture via Luximm.Interop.read_parity, which reverses the axes so tensors arrive in Lux's WHCN layout.
Run the dump once per variant:
uv run python test/parity/dump_<family>_io.py --variant <timm_name> --out data/parity/<key>_io.h5Optionally dump a single-channel companion fixture (--in-chans 1, output data/parity/<key>_in1c_io.h5) so the in_chans = 1 parity test can run as well.
For a brand-new family, also dump local debugging fixtures while you port: the end-to-end check gives a pass/fail signal with no localization power. A debugging fixture can carry the full model.state_dict() (so you can test the forward without the HuggingFace download), a few random seeds, and per-stage intermediates captured with forward hooks on model.stages[i] and stored as /output/stage0, /output/stage1, ... These are something you add for the port, not something the committed sidecars write; they are the bisection tool when the end-to-end test fails.
Phase 2: Reuse the shared utilities
Do not re-implement anything that already lives under src/Layers/ or src/Interop/. The load-bearing helpers and what they do:
Luximm.Interop.read_parityreturns(input, state_dict, output)asFloat32arrays in WHCN layout (the reverse of PyTorch's logical NCHW). Conv weight(out, in, kH, kW)becomes(kW, kH, in, out), which is exactly Lux's Conv layout. For most parameters the layout is what you want and the per-key transform isidentity.apply_state_dictrebuilds the parameterNamedTupleby setting leaves from the dict. Mapping entries are triples(pytorch_key, lux_path_tuple, transform). Non-mutating; bind the result.load_safetensors_state_dictreads HuggingFace weights into the same reversed layout (reverse_axes = trueis the default), so one mapping function serves both fixturestate_dicts and production loading.Luximm.Interop.axis_reverseandLuximm.Interop.pypermare ready-made transforms for the cases where the HDF5-natural layout is not what you want, typically Dense weights.as_channel4dandas_token_normreshape a(C,)norm parameter forlayernorm2dandvit_layernorm;adapt_input_convadapts the stem weight toin_chans.std_conv,layernorm2d,vit_layernorm,grn_layerare the building blocks that matchtimm'sStdConv2dSame, channel-axisLayerNorm2d, ViT LayerNorm and Global Response Norm. The normalization primitives (Layers.LuximmLayerNorm,Layers.LuximmGroupNorm,Layers.layernorm,Layers.groupnorm,Layers.mean_var) are covered in the Layers reference.
Keep the per-family weight mapping in src/Models/<Family>/Model.jl as <family>_mapping(state_dict, variant; load_classifier, in_chans, prefix), returning a vector of (pytorch_key, lux_path, transform) triples. The prefix argument lets a backbone be nested under a wrapper model. BatchNorm families also need a <family>_state_mapping for the running statistics.
Phase 3: Implement the model with @compact
Lux's @compact is the right primitive for composing layers. The pattern is fixed:
@compact( conv1 = Conv((3, 3), in_ch => out_ch; pad = 1, cross_correlation = true), norm1 = Layers.LuximmGroupNorm(out_ch, 32; epsilon = 1f-5),) do x @return NNlib.relu.(norm1(conv1(x)))endNumeric conventions that bite if you forget them, in rough order of how often they cost real debugging time:
- Cross-correlation, always. PyTorch's
Conv2dis cross-correlation; Lux'sConvdefaults to true convolution (kernel-flipped). Passcross_correlation = trueto everyConv. Without it, weights load with the right shape but produce mirrored outputs. When you must drop intoNNlib.convdirectly (weight standardization is the canonical case), passflipkernel = trueonNNlib.DenseConvDims. Same semantic, two flags. - Explicit padding when zero-padding matters.
Conv((k, k), ...; pad = p)works for symmetric same-value padding. For pooling that must pad with zeros instead of-Inf(timm's BiT stem is the canonical case), callNNlib.pad_zeros(x, (l, r, t, b, 0, 0, 0, 0))first and usepad = 0on the op. - Luximm's norms, not Lux's. Build LayerNorm and GroupNorm with
layernorm2d,vit_layernorm,Layers.LuximmLayerNorm,Layers.LuximmGroupNorm(or theLayers.layernorm/Layers.groupnorm/Layers.mean_varfunctions), neverLux.LayerNorm,Lux.GroupNormorStatistics.vardirectly. They have Lux's parameter trees, and they are what the Reactant extension accelerates. BatchNorm staysLux.BatchNorm. - Pass
epsilonexplicitly on every norm. Lux and PyTorch both default to1e-5, but timm overrides it in places:LayerNorm2d(ConvNeXt, CoAtNet) and ViT use1e-6. A mismatched epsilon silently shifts activations by an amount small enough to pass the parity bar. - Variance corrections. Sample variance (Bessel-corrected, the Julia default) and population variance (
corrected = false, what PyTorch uses for BN-style stats and for weight standardization) differ by a factor ofN / (N - 1). Passcorrected = falsetovarwhenever you are matching a BN- or WS-style operation. Seestd_convinsrc/Layers/StdConv.jl. Lux.testmode(st)for parity tests. Otherwise BatchNorm running stats update and any dropout activates, neither of which is whatmodel.eval()does on the PyTorch side.
The forward should look like math: a sequence of broadcasts and tensor ops with no scalar control flow. No x[i] = ..., no Array(x) inside the forward, no if/else on tensor values, no scalar indexing.
Phase 4: Wire the variant table and mapping
src/Models/<Family>/Config.jl holds the variant catalog:
- The
<Family>Variantstruct captures the architectural knobs (depths, dims, stem channels) plushf_repo,default_num_classes, anddefault_input_size. <FAMILY>_VARIANTS :: Dict{Symbol, <Family>Variant}lists every registered variant.
Variant keys are the timm model name with the dot rewritten as an underscore (so the key is a single Julia identifier). The full HuggingFace repo id (timm/<timm name>) lives at <FAMILY>_VARIANTS[key].hf_repo.
Pick one variant and finish it before adding the second. When a model family has many variants, port the smallest first: it surfaces every shared numeric trap with the fastest test loop, and the mapping function written for one variant typically generalizes by changing only depths or widths. Adding a second variant before the first passes parity creates code paths nothing has exercised and that the first failed test cannot localize.
Phase 5: Add the loader
Each family has a private _load_<family>(ps, st, variant; in_chans, num_classes, revision, cache_dir, prefix) -> (ps, st), reached through the create_pretrained closure. The flow is identical across families:
- Look up the variant in
<FAMILY>_VARIANTS. - Load the classifier head only when
num_classesmatchesdefault_num_classes; otherwise warn and leave the head at its random init (or omit it fornum_classes = 0). - Call
hf_hub_downloadto resolve the snapshot path under the HuggingFace Hub cache layout. - Load via
load_safetensors_state_dict, which returns the same WHCN layout the HDF5 fixtures produce. - Apply via
apply_state_dict, after checking that every checkpoint key was consumed. - For families with BatchNorm running stats (ResNet, SE-ResNet, CoAtNet,
vgg*_bn), additionally load the statistics into the model state via<family>_state_mapping. LayerNorm and GroupNorm families returnstunchanged.
A new variant of an existing family needs only its table row. A new family also needs a branch in each of the four dispatch chains in src/Models/Models.jl: create_model, default_num_classes, feature_info and _load_pretrained (each is a haskey(<FAMILY>_VARIANTS, variant) chain).
Keep the constructor and the weight loading separate. The constructor returns a @compact block; the loader takes the result of Lux.setup and returns a new (ps, st). Mixing the two inside @compact makes the model unusable in tests that want a random init.
Phase 6: Verify parity end-to-end
The verification loop is layered. Run the cheap gates between meaningful edits.
End to end first, through the production loading path (what run_variant_parity in test/_parity_helpers.jl does):
data = Luximm.Interop.read_parity(fixture_path)model, load = create_pretrained(variant; num_classes = 0)ps, st = Lux.setup(Xoshiro(0), model)st = Lux.testmode(st)ps, st = load(ps, st)y, _ = model(data.input, ps, st)check_parity(variant, "features", y, data.output["features"])check_parity (in test/_parity_tol.jl) tests the relative L2 error against PARITY_RTOL and logs it. To isolate the forward from the weight download, run the same comparison against a debugging fixture's state_dict with ps = apply_state_dict(ps, data.state_dict, <family>_mapping(data.state_dict, variant)).
The convenience wrapper scripts/test_variant.sh runs these checks (features, logits, in_chans = 1 and the pyramid taps) for one variant, dumping the fixtures first if absent. See Testing for the variant-test workflow.
When parity fails, the per-stage and per-block fixtures pay off. Walk the forward by hand: run the partial forward up to each stage and compare against data.output["stage0"], data.output["stage1"], ... The first stage where parity breaks localizes the bug. Inside the failing stage, splice the matching state-dict entries into a single block in isolation and compare against the per-block fixture.
Phase 7: Update the bookkeeping files
After parity passes, the rest of the repo has to learn about the family. Each item missed is a silent gap: code that is never tested, or a table that is no longer true.
- Dispatch and exports. A branch in
create_model,default_num_classes,feature_infoand_load_pretrainedinsrc/Models/Models.jl, and the family's constructor, mapping and variant table in the export lists ofsrc/Models/Models.jlandsrc/Luximm.jl. test/_filter.jl. The family's row inFAMILY_FILES, its name inPARITY_FAMILIES, and its variant prefix in_jimm_variant_family. A test file that is not listed inFAMILY_FILESnever runs.test/test_interface.jl. The variant table inVARIANT_TABLESand one small variant inREPRESENTATIVES(plusNO_PYRAMIDif it has no feature pyramid).scripts/test_variant.sh. The family's prefix and sidecar in thecase "$variant"block.- Feature pyramid. For a family with
features_onlysupport, its name inPYRAMID_FAMILIESand its variants inFEATURES_ONLY_VARIANTSintest/parity/dump_features_only_io.py. - Tables and docs. The backbone tables in
README.mdanddocs/src/index.md, the family list indocs/src/getting_started.md("Switching families"), and the API reference page (docs/src/api/models.md).
Pitfalls reference
A running checklist. Every item here has cost real debugging time on a previous port.
- Cross-correlation. Every
Convneedscross_correlation = true. EveryNNlib.convneedsflipkernel = trueon itsDenseConvDims. - Zero-padded pooling.
NNlib.maxpool(pad = 1)pads with-Inf.nn.MaxPool2d(padding = 1)pads with zero. When the input has negative values (post-norm activations, leaky-relu outputs), this diverges. Usepad_zerosfirst, then pool withpad = 0. - Norm epsilons. Always pass
epsilonexplicitly; timm'sLayerNorm2dand ViT use1e-6, not the1e-5default. - Luximm norms, not Lux norms.
Lux.LayerNormandLux.GroupNormwork, but miss the Reactant extension. - Variance correction. Pass
corrected = falsewhen matching anything BN-style or WS-style. - Axis order on
apply_state_dict. HDF5 fixtures andload_safetensors_state_dict(with its defaultreverse_axes = true) both arrive reversed (Lux-natural), so both sources share one mapping function. forward_featuresvsforward. The fixture and the Julia forward must agree on which one was dumped. Mismatch shows up as a shape error if you are lucky, a silent wrong number if you are not.- Pre-activation block order. norm then activation then conv vs conv then norm then activation. Both compile. Only one matches the upstream architecture.
timm'sadapt_input_convforin_chans != 3. The stem weight in the safetensors file is the 3-channel version. The loader appliesadapt_input_convat load time to collapse it. Do not re-collapse on the Julia side.Lux.testmode(st). Required for parity. Forgetting it makes the test occasionally pass and occasionally fail depending on RNG state inside dropout.- Batch of one. A single-sample input cannot catch a reshape that mixes the batch axis into tokens or channels; keep the fixtures at
PARITY_BATCH = 2. - Generalize only after one variant passes. Adding a second variant before the first lands creates code paths that the first failed test cannot localize.