JAX Best Practices: High-Performance Python Coding for…
    Neura Market
    Neura Market
    /Claude
    Marketplace
    Directories
    Resources
    Claude
    ChatGPTChatGPTClaudeClaudeGeminiGeminiCursorCursorGrokGrokPerplexityPerplexityDeepSeekDeepSeekCoPilotCoPilotStable DiffusionStable DiffusionMidjourneyMidjourney
    OverviewRulesPromptsMCPsAgentsGamesBlogVideosGuidesCoursesCommunityTrendingMCP TutorialMCP Resources
    ClaudeRulesJAX Best Practices: High-Performance Python Coding for Machine Learning with JIT and Vectorization
    Back to Rules
    Machine Learning

    JAX Best Practices: High-Performance Python Coding for Machine Learning with JIT and Vectorization

    Claude Directory November 29, 2025
    2 copies 1 downloads

    Unlock JAX best practices to supercharge your Python code for ML workloads. Learn functional programming, JIT compilation, vmap vectorization, and pure functions for optimal speed and compatibility.

    Rule Content
    ### Context
    JAX is a powerful Python library for high-performance numerical computing and machine learning, built on NumPy-like APIs with automatic differentiation and JIT compilation. These best practices ensure your code is efficient, pure, and optimized for accelerators like GPUs/TPUs. Focus on functional style, immutability, and JAX transformations to avoid pitfalls and maximize performance.
    
    ### Rules
    - **Adopt Functional Programming**: Stick to pure functions without side effects or mutable state; pass all dependencies explicitly to enable transformations like `jax.jit`, `jax.grad`, and `jax.vmap`.
    - **Use JAX NumPy**: Replace `numpy` with `jax.numpy` (import as `jnp`) for full JAX compatibility in array operations.
    - **Enable JIT Compilation**: Wrap performance-critical functions with `jax.jit` to compile to XLA for speedups, ensuring no Python control flow or side effects inside.
    - **Leverage Automatic Differentiation**: Use `jax.grad`, `jax.value_and_grad` for gradients; design functions to take array inputs and output scalars for grad computation.
    - **Vectorize with vmap**: Apply `jax.vmap` to batch over axes instead of loops; prefer `jax.lax.scan` for sequential data.
    - **Handle Control Flow**: Inside JIT, use `jax.lax.cond`, `jax.lax.fori_loop`, or `jax.lax.while_loop` instead of Python `if`/`for`/`while`.
    - **Manage Randomness**: Use explicit PRNG keys with `jax.random.PRNGKey` for reproducible randomness; split keys for multiple uses.
    - **Optimize Data Types and Memory**: Default to `jnp.float32`; avoid unnecessary copies and use static shapes where possible.
    - **Immutability First**: Never mutate arrays in-place; JAX arrays are immutable—create new ones for updates.
    - **Profile and Test**: Validate inputs with shape/type checks; use `jax.debug.print` for JIT debugging; write pytest unit tests; benchmark with `jax.profiler`.
    - **Style and Docs**: Follow PEP 8, snake_case for vars/functions, UPPER_CASE constants; add NumPy-style docstrings; keep functions small and modular.
    - **Parallelism**: Use `jax.pmap` for multi-device execution when scaling across GPUs/TPUs.
    
    ### Examples
    **JIT-Compiled Loss Function**:
    ```python
    import jax
    import jax.numpy as jnp
    from jax import jit, grad
    
    def loss_fn(params, x, y):
        predictions = jnp.dot(x, params)  # Pure, vectorized
        return jnp.mean((predictions - y) ** 2)
    
    jitted_loss = jit(loss_fn)
    loss_grad = grad(jitted_loss, argnums=0)  # Gradient w.r.t. params
    ```
    
    **Vectorized Over Batches with vmap**:
    ```python
    def single_predict(params, x):
        return jnp.dot(x, params)
    
    batched_predict = jax.vmap(single_predict, in_axes=(None, 0))  # Vectorize over x batch
    
    # Usage: predictions = batched_predict(weights, batch_x)
    ```
    
    **Random Key Splitting for Reproducibility**:
    ```python
    key = jax.random.PRNGKey(0)
    key1, key2 = jax.random.split(key, 2)
    weights = jax.random.normal(key1, (100, 10))
    bias = jax.random.normal(key2, (10,))
    ```
    
    **Scan for RNN-Like Loop**:
    ```python
    def step_fn(carry, x):
        hidden, _ = carry
        hidden = jnp.tanh(jnp.dot(x, hidden))
        return (hidden, None), hidden
    
    final_hidden, outputs = jax.lax.scan(step_fn, (init_hidden, None), inputs)

    Tags

    jaxbest-practicespythonmachine-learningnumpy

    Comments

    More Rules

    View all
    AI/ML

    GLM-4.7 Optimized Config & System Prompt Designer

    Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

    C
    Community
    1
    AI/ML

    GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

    Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

    C
    Community
    1
    AI/ML

    GLM-4.7 Optimized Coding Agent

    This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

    C
    Community
    1
    DevOps

    Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

    Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

    C
    Claude Directory
    AI/ML

    Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

    Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

    C
    Community
    Database

    PostgreSQL Best Practices: Expert Subagent Guide

    Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

    C
    Claude Directory

    Stay up to date

    Get the latest Claude prompts, rules, and resources delivered to your inbox weekly.

    Neura Market LogoNeura Market

    Discover the best AI prompts, plugins, and resources for Claude and more.

    Content Types

    • Rules
    • Prompts
    • MCPs
    • Agents
    • Games
    • Blog
    • Videos
    • Guides
    • Courses
    • Community

    Platforms

    • ChatGPT Directory
    • Claude Directory
    • Gemini Directory
    • Cursor Directory
    • Grok Directory
    • Perplexity Directory
    • DeepSeek Directory
    • CoPilot Directory
    • Stable Diffusion Directory
    • Midjourney Directory
    • All Directories

    Resources

    • Blog
    • Documentation
    • Help Center
    • Marketplace

    Legal

    • Privacy Policy
    • Terms of Service

    © 2026 Neura Market. All rights reserved.

    |

    Not affiliated with any AI platform vendors.

    Neura Market

    Custom AI Systems & Services

    Our team of experienced AI builders will help build custom AI systems, workflows, and solutions.

    Request custom work

    Ready-made automations for this

    Workflows from the Neura Market marketplace related to this Claude resource

    • Automate Your Ad Campaign Performance Alerts with N8Nn8n · $18.07 · Related topic
    • Automate Restaurant Daily Performance Reports with AI Insightsn8n · $24.99 · Related topic
    • Automate Sales Performance Tracking with AI and Google Sheetsn8n · $14.99 · Related topic
    • Automate Multi-LLM Testing and Performance Tracking with LM Studion8n · $14.99 · Related topic
    Browse all workflows