pipekit-train: support `eqx.nn.State` for stateful layers (BatchNorm, etc.) (v0.2, Q4)

Open
#16 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python

Research direction

Start with the v0.1 TrainState and train_step, then read TrainTask.loss_fn and _SynthesisedTask to understand the existing stateless path. Check the Orbax bridge and TrainingLoop integration before designing state persistence. Done means a tiny eqx.nn.BatchNorm model trains through TrainingLoop, running statistics survive save_state/restore_state, stateless behavior remains unchanged, and the docstring and ADR note are updated.

Written by the indexing model from the issue text.

Description

type:feature

Problem / Request

Add support for eqx.nn.State so BatchNorm-like stateful layers can be trained. The v0.1 TrainState doesn't carry per-layer running statistics.

boundaries.md Q4: "BatchNorm and similar layers that carry running statistics need eqx.nn.State. The Equinox adapter's TrainState should probably carry an optional eqx.nn.State alongside the model."

Motivation

  • Many real models use BatchNorm (CNN classifiers, U-Nets). Without eqx.nn.State they can't be trained via this adapter.
  • Straightforward extension; just adds a code path. The shape of the fix is documented.

Proposed API

class TrainState(eqx.Module):
    model: eqx.Module
    state: eqx.nn.State | None  # NEW — optional stateful-layer state
    opt_state: optax.OptState
    step: jax.Array

train_step threads state through:

def train_step(train_state, batch, key, task, optimizer):
    (loss, (aux, new_state)) = grad_fn(train_state.model, batch, key, train_state.state)
    ...
    return TrainState(model=new_model, state=new_state, ...), aux

TrainTask.loss_fn becomes optionally state-aware:

  • If task has stateful = True: signature is loss_fn(model, batch, key, state) -> (loss, (aux, new_state)).
  • Otherwise: existing v0.1 signature, ignored state.

_SynthesisedTask detects stateful layers via eqx.is_inexact_array partition and wires accordingly.

Implementation Steps

  • TrainState.state: eqx.nn.State | None field.
  • TrainState.create builds initial state via eqx.nn.State(model) when the model contains stateful layers.
  • train_step threads state; default loss_fn ignores it for backwards compat.
  • _SynthesisedTask detects stateful models and adjusts the call.
  • Save/restore preserves the state PyTree (extend Orbax bridge).
  • Test: train a tiny CNN with eqx.nn.BatchNorm; assert running stats update across steps and are preserved through save_state / restore_state.

Definition of Done

  • BatchNorm-using model trains end-to-end via TrainingLoop.
  • Existing stateless model paths unchanged.
  • Docstring + ADR note.

Relationships

  • Parent: pipekit-train v0.2
Dominant language
Python
Stars
0
Forks
0
Avg merge
3h 50m
Merged PRs (30d)
143

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jejjohnson/pipekit

All issues in jejjohnson/pipekit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.