Index of every page on this site: llms.txt
All skills
yonesuke avatar

/developing-flax-models

@1e23d34
Run it yourself

A comprehensive guide for developing, training, and managing neural networks using Flax NNX. Use when defining models, managing state, or writing training loops.

  • 7 files
  • 18.4 KB
  • Updated 10 months ago
  • GitHub
Use this Skill: https://skilld.dev/gh/yonesuke/skills/developing-flax-models

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

SKILL.md

≈46 tokens for metadata: the name and description. ≈693 when used: this file. ≈2.1k more on demand in 1 file.

Description uses 2.3% of example budget

Before choosing a Skill, your Agent reads its name and description. All available Skills share that space.

  • A shorter description leaves more room for other Skills. This entry exceeds our 1% size suggestion.

In our Claude Code example, all Skill names and descriptions share 8,000 characters. This Skill uses ≈186 characters, or 2.3%.

The 1% threshold is a size suggestion. Longer descriptions can still fit.

Your model, settings, and other Skills decide how much text your Agent can read.

Example settings and source

The example uses a 200k-token context and default Claude Code settings. The count includes the name, description, separators, and when_to_use when present. Codex also counts local file paths.

Skit's source and limits: Codex 0.160.1, Claude Code 2.1.292.

Developing Flax Models

This skill helps you develop neural networks using Flax NNX, the object-oriented module system for JAX. Use this skill when you need to define models, handle state/randomness, or implement training loops.

Workflows

1. Implement a New Model

  1. Define Module: Subclass nnx.Module. Define layers/parameters in __init__.
  2. Handle Randomness: Pass nnx.Rngs to __init__ for weight initialization. Pass rngs to __call__ for stochastic operations (e.g., Dropout).
  3. Sanity Check: Add a if __name__ == "__main__": block to instantiate the model and run a dummy forward pass.

2. Implement a Training Loop

  1. Choose Strategy:
    • Automatic (nnx.jit): Easiest. Use @nnx.jit on your update function. Handles mutable state management automatically.
    • Functional (nnx.split/nnx.merge): Use for advanced control or when interfacing with pure JAX transformations like scan or vmap (though nnx.vmap exists).
  2. Define Loss: Write a loss function loss_fn(model, batch).
  3. Optimizer: Wrap the model with nnx.Optimizer(model, tx, wrt=nnx.Param).

    [!WARNING] Crucial Change: As of Flax 0.11.0, the wrt argument (e.g., wrt=nnx.Param) is REQUIRED for nnx.Optimizer. Failure to include it will raise a TypeError.

Core Concepts (Reference)

Flax NNX (v2.0+) replaces the immutable, functional design of flax.linen with standard Python classes and mutable state, while maintaining JAX compatibility.

Key Differences

  • Object-Oriented: Models are standard Python classes. You assign to self.param.
  • Reference Semantics: Layers hold their parameters directly.
  • Not Pytrees: nnx.Module objects are not Pytrees. You cannot pass them directly to jax.jit. You must use nnx.jit or manually split/merge state.

Variable Types

NNX variables allow granular state management via "Collections".

  • nnx.Param: Trainable parameters (weights, biases).
  • nnx.BatchStat: Batch normalization statistics (running mean/var).
  • nnx.Rngs: Random Number Generator streams key management.
  • nnx.Variable: Base class for custom state.

State Management

You can filter and manipulate state sets:

# Get only Parameters
params = nnx.state(model, nnx.Param)

# Get everything EXCEPT BatchStats
state = nnx.state(model, filter=nnx.All - nnx.BatchStat)

Examples

See examples.md for detailed code patterns mirrored from the scripts/ directory.

  • Defining Modules: Basic layer structure.
  • Randomness: Handling Dropout and stochastic layers.
  • Training: Comparison of nnx.jit vs Pure JAX loops.
  • Functional API: Using vmap and split/merge.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No third-party reports yet.

Signed by skilld at 1e23d34. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 10 months ago

README badge

README badge for yonesuke/skills/developing-flax-models