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

Aperta
#16 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
45/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
python

Direzione di ricerca

Inizia con v0.1 TrainState e train_step, poi leggi TrainTask.loss_fn e _SynthesisedTask per comprendere il percorso stateless esistente. Controlla l'Orbax bridge e l'integrazione di TrainingLoop prima di progettare la persistenza dello stato. Il lavoro è completato quando un piccolo modello eqx.nn.BatchNorm viene addestrato tramite TrainingLoop, le statistiche correnti sopravvivono a save_state/restore_state, il comportamento stateless rimane invariato e vengono aggiornati il docstring e la nota ADR.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Python
Stelle
0
Fork
0
Merge medio
3h 50m
PR unite (30g)
143

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di jejjohnson/pipekit

Tutte le issue di jejjohnson/pipekit

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.