nnx_rngs

nnx_rngs is a cursor rule for Cursor from altaidevorg/rules-for-ai. It costs 28 tokens per session (4,573 once invoked), scanned A, original, MIT.

A Flax NNX container for managing JAX’s random-number keys. It keeps separate named streams, such as for initialization or dropout, and produces new keys as modules request them.

In plain words
What is it for?
Use it when initializing neural networks or adding operations that need randomness, such as dropout, while keeping random streams separate.
Why use it?
JAX requires random-number keys to be passed explicitly, which becomes tedious and error-prone in nested models. This keeps key handling organized while preserving repeatable results.

Cursor rule for Cursor

Written for Cursor: a Cursor rule (.mdc).

Good fit Use it when initializing neural networks or adding operations that need randomness, such as dropout, while keeping random streams separate.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/altaidevorg/rules-for-ai/nnx_rngs
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.

Clone the repo
git clone --depth 1 https://github.com/altaidevorg/rules-for-ai

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for nnx_rngs

README.md
[![agentmods](https://agentmods.dev/badge/rules/altaidevorg/rules-for-ai/nnx_rngs.svg)](https://agentmods.dev/rules/altaidevorg/rules-for-ai/nnx_rngs)
Your own site
<a href="https://agentmods.dev/rules/altaidevorg/rules-for-ai/nnx_rngs"><img src="https://agentmods.dev/badge/rules/altaidevorg/rules-for-ai/nnx_rngs.svg" alt="Measured on agentmods" height="20"></a>
Per session 28 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 4,573 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
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.1 $0.00028 $0.04573
Opus 5 $0.00014 $0.02286
Sonnet 5 $0.00006 $0.00915
Haiku 4.5 $0.00003 $0.00457

Measured 8d ago against content hash 785565082d08, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-08, from the pricing page.

Security

Grade A, and why

nnx_rngs 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 8d 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_rngs.mdc · 347 lines

How it starts

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

Chapter 3: nnx.Rngs

In the previous chapter, we explored how nnx.Variable and nnx.VariableState manage state within nnx.Module instances. We saw specific variable types like nnx.Param and nnx.BatchStat. This chapter focuses on another crucial aspect of neural network initialization and certain layer types: managing randomness using nnx.Rngs.

Motivation: Explicit Randomness in JAX

JAX's functional programming paradigm requires explicit handling of Pseudo-Random Number Generator (PRNG) keys. Unlike stateful libraries where randomness might be implicit via a global seed, JAX functions need PRNG keys passed as arguments. Manually managing and splitting these keys across complex nested modules can become tedious and error-prone.

Flax NNX introduces nnx.Rngs to streamline PRNG key management. It acts as a central container for different "streams" of randomness (e.g., one for parameter initialization, another for dropout). Modules request keys from specific streams as needed, and nnx.Rngs ensures unique keys are generated sequentially, maintaining reproducibility and fitting within JAX's functional requirements.

Central Use Case: Initializing Modules with Randomness

Most neural networks require random initialization for parameters, and some layers (like dropout) require randomness during the forward pass. nnx.Rngs is typically instantiated once at the top level and passed down the module hierarchy during construction.

import jax
import jax.numpy as jnp
from flax import nnx
from flax.nnx.nn import Linear, Dropout # Using pre-built NNX layers

class SimpleMLP(nnx.Module):
  def __init__(self, din: int, dhidden: int, dout: int, *, rngs: nnx.Rngs):
    # Pass the Rngs instance down to submodules
    self.linear1 = Linear(din, dhidden, rngs=rngs)
    # Dropout requires an RNG key for its mask generation setup
    self.dropout = Dropout(rate=0.5, rngs=rngs) 
    self.linear2 = Linear(dhidden, dout, rngs=rngs)

  def __call__(self, x: jax.Array, *, train: bool) -> jax.Array:
    x = self.linear1(x)
    x = nnx.relu(x)
    # Pass deterministic flag to control dropout behavior
    x = self.dropout(x, deterministic=not train) 
    x = self.linear2(x)
    return x

# 1. Instantiate Rngs at the top level with a base seed (0)
#    Specify different seeds for specific streams if needed.
top_level_rngs = nnx.Rngs(default=0, params=1, dropout=2)

# 2. Pass the Rngs instance during MLP initialization
mlp = SimpleMLP(din=10, dhidden=20, dout=5, rngs=top_level_rngs)

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

# Run in training mode (dropout active)
# Dropout internally uses its 'dropout' stream key, implicitly managed by nnx.Dropout
y_train = mlp(x, train=True)

# Run in evaluation mode (dropout inactive)
y_eval = mlp(x, train=False)

print(f"MLP Output shape (train): {y_train.shape}")
print(f"MLP Output shape (eval): {y_eval.shape}")

# Inspect the state to see the RngState variables managed internally
mlp_state = nnx.state(mlp)
print("\nMLP State showing RngState for Dropout:")
# Note: Linear layers use 'params' stream *during init*, but don't store RngState after.
# Dropout stores RngState to generate masks *during calls*.
print(mlp_state['dropout']) 

Read the full file on GitHub · 347 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. 8d ago First seen · 347 lines · 28 tokens per session scan A 785565082d08

Subscribe to this mod's changes

nnx_rngs is a cursor rule published in the GitHub repository altaidevorg/rules-for-ai (2 stars, last pushed 1y ago), licensed MIT. It adds 28 tokens to every session and 4,573 once invoked, about $0.0001 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.