nnx_module

A Python class used to define a neural-network component with its parameters and other state stored on the object. It is part of Flax NNX, a library for building neural networks with JAX.

In plain words
What is it for?
Use it to define layers and complete neural networks with initialisation, stored parameters, and forward-computation methods.
Why use it?
It makes stateful neural-network code look like familiar Python classes while still working with JAX's numerical transformations.

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/nnx_module
Clone the repo
git clone --depth 1 https://github.com/altaidevorg/rules-for-ai
Per session 3,606 This file is loaded in full into every session.
When invoked 3,606 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.03606 $0.03606
Opus 5 $0.01803 $0.01803
Sonnet 5 $0.00721 $0.00721
Haiku 4.5 $0.00361 $0.00361

Measured 2d ago against content hash 45bc74268992, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

nnx_module 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 2d ago.

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/nnx_module.mdc · 280 lines

How it starts

The opening of the file, as written. The whole thing — 280 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Chapter 1: nnx.Module

Welcome to the Flax NNX tutorial! This first chapter introduces the cornerstone of building neural networks with NNX: nnx.Module.

Motivation: Pythonic Neural Networks in JAX

JAX provides powerful tools for accelerated numerical computing, particularly automatic differentiation and compilation (XLA). However, its functional nature can sometimes make defining and managing the state (parameters, batch statistics, etc.) of complex neural networks cumbersome.

Traditional Flax (Linen) addressed this with a functional API built around nn.Module, which required special methods (setup, compact) and external state management.

Flax NNX introduces nnx.Module to offer a more intuitive, object-oriented approach. The core idea is simple: define your network layers and models as standard Python classes. State is held directly as instance attributes, initialization happens in __init__, and forward computation is defined in regular methods (like __call__). This leverages familiar Python principles, simplifying debugging, inspection, and overall development workflow, while still integrating seamlessly with JAX's functional transformations through helper APIs.

Central Use Case: Defining a Simple Linear Layer

Let's see how nnx.Module works by defining a basic linear transformation layer.

import jax
import jax.numpy as jnp
from flax import nnx
from flax.nnx.nn import initializers # For parameter initialization

# Define needed RNGs for initialization
rngs = nnx.Rngs(0)

class SimpleLinear(nnx.Module):
  def __init__(self, in_features: int, out_features: int, *, rngs: nnx.Rngs):
    # Define state (parameters) directly as attributes
    # We use nnx.Param, a type of nnx.Variable, to mark learnable parameters
    self.kernel = nnx.Param(
        initializers.lecun_normal()(rngs.params(), (in_features, out_features))
    )
    self.bias = nnx.Param(initializers.zeros_init()(rngs.params(), (out_features,)))

  def __call__(self, x: jax.Array) -> jax.Array:
    # Access state attributes directly in forward computation
    y = jnp.dot(x, self.kernel.value) + self.bias.value
    return y

# Instantiate the layer like a regular Python class
layer = SimpleLinear(in_features=3, out_features=4, rngs=rngs)

# Create dummy input data
x = jnp.ones((1, 3))

# Call the layer instance directly
y = layer(x)

print(f"Output shape: {y.shape}")
# Output: Output shape: (1, 4)

Read the full file on GitHub · 280 lines

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. 2d ago First seen · 280 lines · 3,606 tokens per session scan A 45bc74268992

Subscribe to this mod's changes

nnx_module is a cursor rule published in the GitHub repository altaidevorg/rules-for-ai (2 stars, last pushed 1y ago), licensed MIT. It adds 3,606 tokens to every session, about $0.0180 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.