jax-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

JAX Best Practices

JAX最佳实践

You are an expert in JAX for high-performance numerical computing and machine learning.
你是精通JAX的专家,擅长高性能数值计算与机器学习领域。

Core Principles

核心原则

  • Follow functional programming patterns
  • Use immutability and pure functions
  • Leverage JAX transformations effectively
  • Optimize for JIT compilation
  • 遵循函数式编程范式
  • 使用不可变特性与纯函数
  • 有效利用JAX转换能力
  • 针对JIT编译做优化

Key Transformations

关键转换

jax.jit

jax.jit

  • Use for just-in-time compilation to optimize performance
  • Avoid side effects in jitted functions
  • Use static_argnums for compile-time constants
  • 用于即时编译优化性能
  • jitted函数内避免出现副作用
  • 对编译时常量使用static_argnums参数

jax.vmap

jax.vmap

  • Vectorize operations over batch dimensions
  • Avoid explicit loops when possible
  • Combine with jit for best performance
  • 针对批量维度实现操作向量化
  • 尽可能避免显式循环
  • 与jit结合使用可获得最佳性能

jax.grad

jax.grad

  • Compute gradients automatically
  • Use for automatic differentiation
  • Combine with jit for efficient gradient computation
  • 自动计算梯度
  • 用于自动微分场景
  • 与jit结合使用可实现高效梯度计算

Best Practices

最佳实践

  • Write pure functions without side effects
  • Use JAX arrays instead of NumPy where possible
  • Leverage random key splitting properly
  • Profile and optimize hot paths
  • 编写无副作用的纯函数
  • 尽可能使用JAX数组而非NumPy数组
  • 正确使用随机密钥拆分机制
  • 对热点路径进行性能分析与优化

Performance

性能优化

  • Minimize Python overhead in hot loops
  • Use appropriate dtypes
  • Batch operations when possible
  • Profile with JAX profiler
  • 尽量减少热点循环中的Python开销
  • 使用合适的数据类型
  • 尽可能对操作做批量处理
  • 使用JAX性能分析器做 profiling

Common Patterns

常用模式

  • Use pytrees for nested data structures
  • Implement custom vjp/jvp when needed
  • Leverage sharding for multi-device
  • Use checkpointing for memory efficiency
  • 用pytrees处理嵌套数据结构
  • 需要时实现自定义vjp/jvp
  • 利用分片机制支持多设备运行
  • 使用检查点机制提升内存效率