Tunix (Tune-in-JAX)

repository·main·Indexed 25 days ago

https://github.com/google/tunix

A lightweight, JAX-native LLM post-training framework designed for high performance on TPUs. Tunix supports Supervised Fine-Tuning (SFT) including LoRA, DPO, and ORPO; Reinforcement Learning (RL) with PPO, GRPO, GSPO-Token, DAPO, and Dr.GRPO; and Agentic RL for multi-turn tool use. It features native integration with MaxText, vLLM, and SGLang-JAX, and supports multi-host distributed training via Pathways.

Tokens
43.1K
Snippets
85
Records
196
Agent score
81%

What's inside google-tunix

  1. What is Tunix?

    main

    Tunix (Tune-in-JAX) is a JAX-based library designed for the efficient and scalable post-training of Large Language Models (LLMs). It is optimized for TPU performance and integrates with the JAX ecosystem (Flax, Optax, Orbax) and high-performance inference engines (vLLM, SGLang-JAX) for rollouts.

    Core Capabilities:

    • Supervised Fine-Tuning (SFT): Supports Full Weights Fine-Tuning, PEFT (Parameter-Efficient Fine-Tuning), DPO (Direct Preference Optimization), and ORPO (Odds Ratio Preference Optimization).
    • Reinforcement Learning (RL): Supports PPO (Proximal Policy Optimization), GRPO (Group Relative Policy Optimization), GSPO-Token, DAPO, and Dr.GRPO.
    • Agentic RL: Supports multi-turn tool use, asynchronous rollout for high-throughput trajectory collection, and trajectory batching/grouping.
  2. Overview of Tunix post-training capabilities

    main

    Tunix (Tune-in-JAX) is a JAX-based library for efficient and scalable LLM post-training. It is designed to work within the JAX ecosystem (leveraging Flax, Optax, Orbax) and sits between core utilities and optimized models like MaxText.

    Key capabilities include:

    Supervised Fine-Tuning (SFT)

    • Full Weights Fine-Tuning
    • PEFT (Parameter-Efficient Fine-Tuning) such as LoRA
    • Preference Optimization: DPO (Direct Preference Optimization) and ORPO (Odds Ratio Preference Optimization)

    Reinforcement Learning (RL)

    • PPO (Proximal Policy Optimization)
    • GRPO (Group Relative Policy Optimization)
    • GSPO-Token (Token-level Group Sequence Policy Optimization)
    • DAPO (Direct Alignment via Preference Optimization)
    • Dr.GRPO (Distributionally Robust GRPO)

    Agentic RL

    • Multi-turn tool use
    • Asynchronous rollout for high-throughput trajectory collection
    • Trajectory batching and grouping
  3. Ways to launch Tunix training jobs

    main

    Tunix provides two primary methods for launching training jobs:

    1. Tunix CLI: The recommended default method. It is a simple launching tool that provides comprehensive configuration options through a command-line interface.
    2. Interactive and Custom Launch: A method for users who require full control. This is best suited for:
      • Early experimentation: Learning the framework's capabilities.
      • Advanced customization: Implementing complex workflows that require more flexibility than the CLI offers.
  4. Available fine-tuning notebooks and scripts in Tunix examples

    main

    The examples/ directory contains various notebooks and scripts for different fine-tuning paradigms:

    Notebooks

    • qlora_gemma.ipynb: LoRA and QLoRA fine-tuning with Gemma models.
    • grpo_gemma.ipynb: Group Relative Policy Optimization (GRPO) reinforcement learning.
    • dpo_gemma.ipynb: Direct Preference Optimization (DPO) for alignment.
    • logit_distillation.ipynb: Knowledge distillation from teacher to student models.

    Subdirectories

    • deepscaler/: Training and evaluating DeepScaler models (train_deepscaler_nb.py, math_eval_nb.py).
    • model_load/: Loading models from different formats (e.g., from_safetensor_load/ for Gemma2/Gemma3).
    • rl/: Reinforcement learning implementations (e.g., grpo/gsm8k/ for mathematical reasoning).
    • sft/: Supervised fine-tuning examples (e.g., mtnt/ for translation tasks).
  5. Supported Algorithms in Tunix

    main

    Tunix provides built-in support for several state-of-the-art algorithms across Supervised Fine-Tuning (SFT), Preference Optimization, and Reinforcement Learning (RL).

    Supervised Fine-Tuning (SFT) & Preference

    • PEFT (Parameter-Efficient Fine-Tuning)
    • DPO (Direct Preference Optimization)
    • ORPO (Odds ratio Preference Optimization)

    Reinforcement Learning (RL)

    • PPO (Proximal Policy Optimization)
    • GRPO (Group Relative Policy Optimization)
    • GSPO-Token (Token-level Group Sequence Policy Optimization)
    • DAPO (Direct Alignment via Preference Optimization)
    • Dr.GRPO (Distributionally Robust GRPO)
  6. Core components of Agentic RL in Tunix

    main

    The Agentic RL framework in Tunix is composed of several interacting components designed for trajectory collection:

    • Agent: Generates actions based on observations and conversation history. Inherits from ConversationAgentBase.
      • ModelAgent: For single-turn tasks.
      • ToolAgent: For multi-turn tasks; uses a ToolManager and ToolParser to handle tool calls.
    • Environment: Represents the task. Inherits from BaseTaskEnv.
      • TaskEnvironment: For single-turn tasks; terminates after one action.
      • ToolEnvironment: For multi-turn tasks; executes tool calls via ToolManager and returns results in a {"tool_outputs": ...} format.
    • Tool: Reusable components inheriting from BaseTool. They must implement get_json_schema() and either apply() (sync) or apply_async() (async).
    • Parser: Translates between natural language model responses and structured data (like tool calls).
    • TrajectoryCollectEngine: Manages the interaction loop for a single agent-environment pair to produce a trajectory.
    • RolloutOrchestrator: Manages multiple TrajectoryCollectEngine instances for parallel collection.
  7. Understand the Tunix layered architecture

    main

    Tunix (Tune-in-JAX) is organized into five distinct layers that abstract the complexity of LLM post-training:

    1. User Interface & Application Layer: The entry point for users via CLI tools, interactive notebooks, and configuration files.
    2. Algorithm & Workflow Layer: Contains core training paradigms like Supervised Fine-Tuning (SFT), Reinforcement Learning (RL), and Agentic workflows.
    3. Core Components Layer: Handles the heavy lifting of training loop orchestration, metrics tracking, and distributed state management.
    4. Foundation Frameworks & Integration Layer: Leverages JAX, Flax, and Optax for high-performance computation and optimization.
    5. Hardware Layer: Manages compute resources, specifically optimized for Google Cloud TPUs, multi-host GPU clusters, and CPU hosts.
  8. Peer Discovery and Inter-Process Communication (IPC)

    main

    Tunix allows processes to discover each other dynamically via a gRPC discovery service.

    • Server Side: A process acts as a discovery server by specifying --discovery_port. It can register callbacks using context.ipc.discovery.on_register(callback) to react when new peers join.
    • Client Side: A process connects to an existing discovery server using --discovery_addrs=<hostname>:<port>. It can transmit metadata to the server using context.ipc.discovery.register(metadata=...).
    # Server: Register a callback for new peers
    context.ipc.discovery.on_register(
        lambda hostname, _, metadata: (
            logging.info(f"{hostname} knocked and said: {pickle.loads(metadata)}")
        )
    )
    
    # Client: Register with the discovery server
    context.ipc.discovery.register(metadata=pickle.dumps(args.say))
  9. Understand the Tunix CLI configuration hierarchy

    main

    Tunix uses a layered configuration system to launch SFT (Supervised Fine-Tuning) and RL (Reinforcement Learning) jobs. Configurations are merged in the following order of precedence (later sources override earlier ones):

    1. Base Config File (Lowest Priority): The default settings defined in base_config.yaml.
    2. Config File Override: An optional secondary YAML file specified via the override_config_file argument. This is useful for project-specific settings.
    3. CLI Arguments (Highest Priority): Individual key=value pairs passed directly in the command line. These override all previous settings.
  10. Batch and group trajectories for algorithms like GRPO

    main

    Tunix uses a GroupQueueManager within the RolloutOrchestrator to collect TrajectoryItem instances into groups. This is essential for algorithms like GRPO that require multiple samples per prompt.

    • Grouping: Items are bucketed based on a group_key (e.g., a prompt ID from env.task["group_id"]) and episode_id.
    • Ready Groups: Once a bucket reaches a specified group_size (e.g., num_generations), it is marked as a "ready group" and yielded via yield_batches.
    • Memory Management: Use the max_open_buckets parameter to limit the number of groups being populated simultaneously to control memory usage.
  11. How Agentic RL works in Tunix

    main

    Agentic RL enables training agents capable of multi-turn reasoning and external tool interaction.

    Key Concepts:

    • Multi-turn Conversations: Agents break complex problems into sequential steps of reasoning, tool invocation, and response generation.
    • Tool Integration: Agents can use external tools (e.g., search, code execution, APIs). The environment manages the execution and feeds results back to the agent.
    • Asynchronous Architecture: To maximize hardware utilization (like TPUs), Tunix uses a highly asynchronous architecture for both rollout and training. This overlaps model inference latency, I/O-bound tool execution, and reward computations.
    • Trajectory Batching: The framework supports grouping trajectories, which is essential for algorithms like GRPO that require multiple samples per prompt.
  12. Configure hierarchical batch sizes for RL training

    main

    Tunix uses a three-level hierarchy for batching to balance throughput and memory pressure:

    1. Global Batch Size: The total data processed in one full RL iteration. It is derived from the dataloader batch size and num_generation. For algorithms like GRPO, it is dataloader_batch_size * num_generation.
    2. Mini Batch Size: Determines how frequently the model is updated within a global step. The model is updated global_batch_size / mini_batch_size times. The global batch size must be divisible by the mini batch size. If not specified, it defaults to the global batch size.
    3. Micro Batch Size: Controls the input size for specific components to manage HBM pressure:
      • train_micro_batch_size: Controls the trainer's input. Setting this enables gradient accumulation (steps = mini_batch_size / train_micro_batch_size).
      • compute_logps_micro_batch_size: Controls the inference worker during log-probability calculation. Typically 2–4x higher than train_micro_batch_size.
      • rollout_micro_batch_size: Controls the input to the rollout engine. (Note: This is deprecated for agentic rollouts as the system uses continuous batching).