Communitygithub.com

JAX 专家技能

提供专业的 JAX 帮助,涵盖 Autograd、XLA 编译(`jit`)、向量化(`vmap`)和并行化(`pmap`)。用于构建高性能数值计算和前沿深度学习研究。

JAX 专家技能 是什么?

此技能为 AI 代理提供 JAX 相关的专业指导,JAX 是一个结合了 Autograd(自动微分)和 XLA(加速线性代数编译器)的高性能数值计算库。它对于需要 GPU/TPU 加速的复杂科学计算和定制化深度学习研究至关重要。技能涵盖 JAX 的核心概念,如可组合的函数变换(`jit`、`grad`、`vmap`)、显式 PRNG 密钥管理以及如何使用 Equinox 等库构建有状态模型。同时为`jit`编译内的副作用、数组不可变性和 PRNG 密钥安全提供了最佳实践和故障排除方案,使用户能够编写高效、可扩展且无错误的 JAX 代码。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/G1Joshi/Agent-Skills/tree/HEAD/skills/ai-ml/jax

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

JAX

JAX 是“增强版 NumPy”。它将 Autograd(自动微分)与 XLA(编译)相结合。到 2025 年,Flax NNX(类似 PyTorch 的 OOP 风格)将成为标准。

使用场景

  • 高性能数值与科学计算:使用 Autograd 和 XLA 编译在 GPU/TPU 上加速线性代数运算。
  • 自定义深度学习研究:使用 Flax、Haiku 或 Equinox 构建神经网络,无需框架臃肿的代码。
  • 可组合的函数变换:无缝链接 jit(编译)、grad(导数)和 vmap(向量化)。
  • 多设备分布式训练:使用 pmap 和分片映射(jax.experimental.shard_map)跨集群并行化计算。

快速开始

import jax
import jax.numpy as jnp

# JIT 编译的矩阵乘法与自动微分
@jax.jit
def loss_fn(w, x, y):
    pred = jnp.dot(x, w)
    return jnp.mean((pred - y) ** 2)

# 同时计算值和梯度
grad_fn = jax.grad(loss_fn)

w = jnp.array([1.0, 2.0])
x = jnp.array([[1.0, 0.5], [2.0, 1.0]])
y = jnp.array([2.0, 4.0])

grads = grad_fn(w, x, y)
print("Gradients:", grads)

核心概念

#可组合的函数变换:jit、grad 与 vmap

结合即时编译、自动微分和自动批处理:

import jax
import jax.numpy as jnp

# 纯函数定义
def loss_fn(w, x, y):
    pred = jnp.dot(x, w)
    return jnp.mean((pred - y) ** 2)

# 变换 1:关于权重的自动梯度
grad_fn = jax.grad(loss_fn)

# 变换 2:通过 XLA 进行 JIT 编译以获得极致的 GPU 加速
fast_grad_fn = jax.jit(grad_fn)

# 变换 3:跨批次的自动向量化
# batch_loss_fn 无需循环即可自动处理二维矩阵
vmapped_loss = jax.vmap(loss_fn, in_axes=(None, 0, 0))

# 示例计算
key = jax.random.PRNGKey(42)
w = jax.random.normal(key, (3,))
x = jnp.array([[1.0, 2.0, 3.0]])
y = jnp.array([5.0])

grad_val = fast_grad_fn(w, x, y)
print("Computed Gradient:", grad_val)

#显式 PRNG 密钥管理

无状态的伪随机数生成:

import jax

# 初始化根 PRNG 密钥
key = jax.random.PRNGKey(2026)

# JAX 密钥必须显式拆分;切勿复用
key, subkey1, subkey2 = jax.random.split(key, 3)

data_normal = jax.random.normal(subkey1, shape=(4, 4))
data_uniform = jax.random.uniform(subkey2, shape=(4, 4))

print("Normal sample mean:", jnp.mean(data_normal))

#使用 Equinox 与纯函数构建有状态模型

在纯 JAX 函数之上构建表达力强、面向对象的神经网络:

import equinox as eqx
import jax
import jax.numpy as jnp

class SimpleMLP(eqx.Module):
    linear1: eqx.nn.Linear
    linear2: eqx.nn.Linear

    def __init__(self, in_features, out_features, key):
        k1, k2 = jax.random.split(key)
        self.linear1 = eqx.nn.Linear(in_features, 64, key=k1)
        self.linear2 = eqx.nn.Linear(64, out_features, key=k2)

    def __call__(self, x):
        x = jax.nn.relu(self.linear1(x))
        return self.linear2(x)

key = jax.random.PRNGKey(0)
model = SimpleMLP(in_features=10, out_features=2, key=key)
sample_input = jnp.ones((10,))
output = model(sample_input)
print("Model output:", output)

常见模式

使用 vmap 进行批量向量化

问题:编写手动 Python 循环来评估批次上的函数会减慢执行速度。

解决方案: 使用 jax.vmap 自动向量化单示例函数:

def predict_single(w, x):
    return jnp.dot(x, w)

# 跨批次维度(x 的轴 0)自动向量化
predict_batch = jax.vmap(predict_single, in_axes=(None, 0))

batch_x = jnp.ones((100, 10))
weights = jnp.ones(10)
predictions = predict_batch(weights, batch_x) # 输出形状:(100,)

最佳实践(2026)

  • 务必编写纯函数式代码:零副作用,没有原地数组修改(使用 x.at[idx].set(val) 而非 x[idx] = val)。
  • 务必用 @jax.jit 包装性能关键的函数以触发 XLA 编译。
  • 务必在每次生成随机数时显式拆分 PRNG 密钥(jax.random.split(key))。
  • 务必使用 Equinox 或 Flax Linen 等现代高级库,而非编写原始参数字典。
  • 切勿在 JIT 函数内部对动态追踪器(tracer)使用标准 Python 条件语句(如 if x > 0:);请使用 jax.lax.cond。
  • 切勿复用 PRNG 密钥;复用密钥会生成统计上相关的随机数。
  • 切勿在经 jit、grad 或 vmap 变换的函数内部修改全局变量。

故障排除

错误原因解决方案
ConcretizationTypeError: Abstract tracer value used in Python if/whilePython 条件分支依赖于 jit 内部的动态 JAX 数组值。使用 jax.lax.cond 或 jax.lax.while_loop 替代标准 Python if。
JAX arrays are immutable尝试进行原地赋值(arr[0] = 5)。使用函数式更新语法:arr = arr.at[0].set(5)。
PRNGKey reuse warning / identical randomness复用相同的 PRNG 密钥会产生相同的随机数。在每个随机操作前拆分密钥:key, subkey = jax.random.split(key)。

参考资料

Individual skills in this repo

This repo contains 11 individual skills — each has its own dedicated page.

G1Joshi/Agent-Skills

Expert Dask distributed computing assistance covering Dask DataFrames, Arrays, Futures, and cluster scaling. Use when analyzing datasets too large for pandas on a single machine or multi-node cluster.

G1Joshi/Agent-Skills

Expert dbt (data build tool) assistance covering SQL modeling, Jinja macros, tests, documentation, and semantic layer. Use when building analytics engineering pipelines on BigQuery, Snowflake, or PostgreSQL.

G1Joshi/Agent-Skills

Expert Ray distributed computing assistance covering Ray Core (actors, tasks), Ray Train, Ray Tune, and Ray Serve. Use when scaling Python compute and ML training across multi-node clusters.

G1Joshi/Agent-Skills

Expert Git version control assistance covering branching, interactive rebase, cherry-pick, submodules, worktrees, and conflict resolution. Use when managing source code history and collaboration workflows.

G1Joshi/Agent-Skills

Expert K9s CLI assistance covering terminal Kubernetes cluster navigation, real-time log streaming, port forwarding, and pod debugging. Use when managing and troubleshooting Kubernetes clusters with speed.

G1Joshi/Agent-Skills

Expert SWC assistance covering high-performance Rust-based JavaScript/TypeScript compilation, .swcrc configuration, Jest testing via @swc/jest, and minification. Use when replacing Babel with SWC for faster builds, accelerating test execution, or compiling modern ECMAScript features.

G1Joshi/Agent-Skills

Expert Tig assistance covering text-mode interface for Git, interactive staging, commit graph visualization, diff exploration, and blame navigation. Use when navigating Git commit history in the terminal, staging hunks interactively, browsing file changes, or reviewing revisions.

G1Joshi/Agent-Skills

Expert Vim assistance covering modal editing, .vimrc configuration, registers, macros, search/replace, and plugin management via vim-plug. Use when editing text efficiently in terminal environments, writing Vimscript, recording macros, or configuring core Vim settings.

G1Joshi/Agent-Skills

Expert Zed editor assistance covering high-performance Rust-based text editing, multi-buffer editing, language server protocols (LSP), and AI assistant integrations. Use when configuring Zed settings.json, setting up language extensions, collaborating in real-time channels, or optimizing editor startup speed.

G1Joshi/Agent-Skills

Expert Zsh assistance covering shell customization, Oh My Zsh plugins, Zinit plugin manager, prompt engineering (Starship/Powerlevel10k), and shell scripting. Use when configuring .zshrc, writing Zsh automation scripts, optimizing shell startup time, or configuring tab-completion.

G1Joshi/Agent-Skills

Expert [skill-name] assistance covering [feature 1], [feature 2], and [feature 3]. Use when [working with X], [debugging Y], or [implementing Z].

相关技能