guide

A guide to Flax NNX, a neural-network library for JAX. It explains how NNX combines ordinary Python objects with JAX’s function-based processing, including model state, filters, transformations, and state handling.

In plain words
What is it for?
Use it as an introduction or reference when building NNX modules, selecting their state, applying JAX operations, or using the NNX functional interface.
Why use it?
It provides the background needed to understand how NNX models work and how their changing values are made compatible with JAX.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/altaidevorg/rules-for-ai/guide
Clone the repo
git clone --depth 1 https://github.com/altaidevorg/rules-for-ai
Per session 984 This file is loaded in full into every session.
When invoked 984 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00984 $0.00984
Opus 5 $0.00492 $0.00492
Sonnet 5 $0.00197 $0.00197
Haiku 4.5 $0.00098 $0.00098

Measured yesterday against content hash 0d86edc9c792, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

guide scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

examples/flax/guide.mdc · 62 lines

What it actually says

Flax NNX is a neural network library for JAX designed to enhance the developer experience by integrating stateful object-oriented programming with JAX's functional paradigm. The core abstraction is nnx.Module (0), a standard Python class where state (parameters, batch statistics, RNG states) is held directly as attributes, typically wrapped in nnx.Variable (1) subtypes (e.g., nnx.Param, nnx.BatchStat). This allows for intuitive initialization and direct attribute access.

To bridge the gap with JAX's requirement for pure functions and immutable data structures (pytrees), NNX provides:

  1. A Functional API (2): Functions like nnx.split, nnx.merge, nnx.state, and nnx.update allow explicitly separating a Module's static structure (GraphDef) from its dynamic state (GraphState), which is a pytree of immutable nnx.VariableState (1) objects. GraphDef and GraphState (7) form the internal representation used by this API. Filters (5) are used extensively within this API to select specific parts of the state based on type, tags, or path.
  2. Lifted Transforms (3): Higher-level wrappers around JAX transformations (e.g., nnx.jit, nnx.grad, nnx.vmap) that implicitly handle the state splitting and merging via the Functional API (2), offering a more object-oriented interface to users.

Auxiliary components include:

  • nnx.Rngs (4): Manages PRNG keys for reproducibility and simplified handling within nnx.Module methods.
  • nnx.Optimizer (6): A helper class encapsulating a Module, its differentiable parameters (selected via Filters (5)), an Optax optimizer, and its state, simplifying the training loop update step.

Overall, NNX aims to provide the flexibility and ease-of-use of Python objects while retaining compatibility with the performance benefits of JAX's functional transformations.

Source Repository: https://github.com/google/flax.git

flowchart TD
    A0["nnx.Module"]
    A1["nnx.Variable / nnx.VariableState"]
    A2["NNX Functional API (split/merge/state/update/graphdef)"]
    A3["NNX Lifted Transforms (jit, grad, vmap, scan, etc.)"]
    A4["nnx.Rngs"]
    A5["Filters (`filterlib`)"]
    A6["nnx.Optimizer"]
    A7["Graph Representation (GraphDef / GraphState)"]
    A0 -- "Contains / Holds state in" --> A1
    A0 -- "Uses for randomness" --> A4
    A2 -- "Operates on / Deconstructs ..." --> A0
    A2 -- "Produces / Consumes" --> A7
    A2 -- "Uses for selection" --> A5
    A3 -- "Operates on" --> A0
    A3 -- "Uses implicitly" --> A2
    A5 -- "Selects / Filters" --> A1
    A6 -- "Wraps / Manages" --> A0
    A6 -- "Updates state of" --> A1
    A6 -- "Uses for parameter selection" --> A5
    A7 -- "Contains immutable state" --> A1
    A4 -- "Manages RNG state via" --> A1
    A3 -- "Uses for selection (e.g., S..." --> A5

Chapters

nnx.Module nnx.Variable / nnx.VariableState nnx.Rngs Filters (filterlib) NNX Lifted Transforms (jit, grad, vmap, scan, etc.) NNX Functional API (split/merge/state/update/graphdef) Graph Representation (GraphDef / GraphState) nnx.Optimizer


Generated by Rules for AI

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 62 lines · 984 tokens per session scan A 0d86edc9c792

Subscribe to this mod's changes

guide is a cursor rule published in the GitHub repository altaidevorg/rules-for-ai (2 stars, last pushed 1y ago), licensed MIT. It adds 984 tokens to every session, about $0.0049 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.