Proposal: Add a StochasticDepth (DropPath) module to flax.nnx

Aperta
#5,666 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Start with flax/nnx/nn/stochastic.py and the existing Dropout implementation, then check its tests, exports, and API documentation to follow NNX conventions. The proposal lists the required behavior and tests, including eval and zero-rate identity, rate-one behavior, per-sample broadcasting, rescaling, JIT, and RNG handling. Done means the module is exported as nnx.StochasticDepth, documented with an example, and covered by those tests.

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

Descrizione

Motivation

Stochastic depth (Huang et al., "Deep Networks with Stochastic Depth", ECCV 2016) trains residual networks by randomly dropping entire residual branches per sample during training and using the full network at test time. It is a standard regularizer for modern residual architectures (ViT, ConvNeXt, Swin-style models): torchvision ships it as a first-class module and timm exposes it as DropPath, and training recipes for these architectures assume it is available.

Flax NNX currently has nnx.Dropout but no stochastic depth. The two are not interchangeable: dropout masks individual activations, while stochastic depth drops the whole residual branch for a sample (and rescales the surviving branches), which is what lets very deep residual networks train as an ensemble of shallower ones. When I went to port a ViT-style training recipe to NNX I had to hand-roll this, and the per-sample broadcasting and train-time rescaling are easy to get subtly wrong.

I checked before filing: there is no StochasticDepth / DropPath implementation in Flax (code search returns zero), no open issue requesting it, and no open or closed PR implementing it.

Proposed design

Add StochasticDepth to flax/nnx/nn/stochastic.py, next to Dropout, mirroring its API and conventions so it feels native to NNX:

class StochasticDepth(nnx.Module):
  def __init__(
    self,
    rate: float,                      # drop probability (not keep rate), like Dropout
    *,
    deterministic: bool | None = False,
    rng_collection: str = "dropout",
    rngs: nnx.Rngs | None = None,
  ): ...

  def __call__(self, inputs, *, deterministic=None, rngs=None):
    # training: draw one Bernoulli(1 - rate) per sample (leading axis),
    # broadcast it over all remaining axes, zero dropped samples and
    # scale survivors by 1 / (1 - rate)
    # eval / rate == 0: return inputs unchanged

Usage inside a residual block:

class Block(nnx.Module):
  def __init__(self, dim, drop_path_rate, rngs):
    self.mlp = ...
    self.drop_path = nnx.StochasticDepth(drop_path_rate, rngs=rngs)
  def __call__(self, x):
    return x + self.drop_path(self.mlp(x))

It would be exported as nnx.StochasticDepth, registered in the NNX API docs next to Dropout, and support model.train() / model.eval() and nnx.view(..., deterministic=...) the same way Dropout does (via set_view).

Scope

In scope: the NNX module, unit tests (identity at eval and at rate 0, all-dropped/zero behaviour at rate 1, per-sample mask broadcasting, train-time rescaling, jit compatibility, rng handling), docs/API registration, and a short usage example in the module docstring.

Out of scope for a first PR: a linen version (happy to do it as a follow-up if maintainers want both), and per-layer rate scheduling helpers (e.g. linearly increasing rates across blocks) — users can construct per-block rates themselves.

Offer

I am willing to implement this and maintain it through review. I would welcome a maintainer or mentor to guide it — in particular on whether the NNX-only scope is right and on the rng_collection naming — before I open the PR, so the design lands in the shape you want.

Lingua principale
Jupyter Notebook
Stelle
7.4k
Fork
855
Merge medio
3g 3h
PR unite (30g)
5

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 google/flax

Tutte le issue di google/flax

Issue simili

Altre issue su Machine Learning

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.