Absolute Zero Reasoner (AZR)

repository·master·Indexed 23 days ago

https://github.com/leaplabthu/absolute-zero-reasoner

A framework for reinforced self-play reasoning that enables models to improve their reasoning capabilities in code and math without using external curated training data. The repository includes evaluation tools for code correctness and efficiency via EvalPlus (supporting HumanEval+, MBPP+, and EvalPerf) and LiveCodeBench for code generation, self-repair, test output prediction, and code execution scenarios.

Tokens
10.3K
Snippets
32
Records
53
Agent score
83%

What's inside Absolute Zero Reasoner

  1. EvalPlus CLI Commands Overview

    master

    EvalPlus provides three primary CLI entrypoints for the code generation and evaluation lifecycle:

    • evalplus.codegen: Performs code generation followed by automatic code post-processing.
    • evalplus.evaluate: Performs code generation, post-processing, and full evaluation.
    • evalplus.sanitize: Performs only code post-processing (cleaning up LLM-generated text to ensure compilability).
  2. Create customized code generation samples

    master

    You can programmatically generate your own samples using evalplus.data.

    Expected Schema for samples.jsonl

    When providing a JSONL file, use one of the following schemas:

    1. task_id: The identifier string for the task.
    2. solution (optional): A self-contained solution (usually including the prompt).
    3. completion (optional): The function body without the prompt.

    Note: If both solution and completion are provided, solution takes precedence.

    Directory-based samples

    Alternatively, you can provide a directory via --samples ${SAMPLE_DIR}. The directory must be organized as: ${SAMPLE_DIR}/${TASK_ID}/{SAMPLE_ID}.py where ${TASK_ID} is task_id.replace("/", "_").

    from evalplus.data import get_[human_eval|mbpp]_plus, write_jsonl
    
    samples = [
        dict(task_id=task_id, solution=GEN_SOLUTION(problem["prompt"]))
        for task_id, problem in get_[human_eval|mbpp]_plus().items()
    ]
    write_jsonl("samples.jsonl", samples)
  3. How EvalPerf evaluation works

    master

    The evalplus.evalperf process follows four distinct steps:

    1. Sampling: Samples 100 solutions (n_samples) from the target LLM for each task.
    2. Efficiency Evaluation: For tasks where at least 10 samples pass (min_correct), the script performs efficiency profiling.
    3. Data Generation: Produces a .jsonl output file containing detailed results for each task, including solutions, pass status, and profiling metrics (like _num_cpu_instructions and dps).
    4. Score Computation: Computes the final Differential Performance Score (DPS).
  4. AZR Prompt Template (Deepseek R1 style)

    master

    AZR uses the Deepseek R1 <think> and <answer> tags as the prompt template. The assistant is expected to reason within <think> tags and provide the final answer within <answer> tags.

    Template Structure:

    A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively, i.e., <think> reasoning process here </think> <answer> answer here </answer>. User: {question}
    Assistant: <think>
  5. How Absolute Zero Reasoner (AZR) works

    master

    The Absolute Zero Reasoner (AZR) uses a reinforced self-play reasoning approach that requires zero external training data. The algorithm follows an iterative two-step loop:

    1. PROPOSE: The model generates reasoning tasks using abduction, deduction, and induction. These tasks are validated via Python execution and assigned a learnability reward.
    2. SOLVE: The model attempts to solve the self-generated tasks. Solutions are verified through Python execution and assigned an accuracy reward.

    This cycle uses TRR++ to create a self-evolving loop, strengthening reasoning capabilities through self-play.

  6. Download AZR and Qwen models for evaluation

    master

    Before running evaluations, download the required models from Hugging Face using the hf download command. Ensure you are in the evaluation/math_eval directory and your virtual environment is activated.

    Important: Note the difference in Hugging Face usernames: Base models use andrewzh2, while Coder models use andrewzh.

    cd evaluation/math_eval
    source .venv/bin/activate
    
    # Download 3B Coder model
    hf download andrewzh/Absolute_Zero_Reasoner-Coder-3b --local-dir-use-symlinks False --local-dir ./models/Absolute_Zero_Reasoner-Coder-3b
    
    # Download 7B Coder model  
    hf download andrewzh/Absolute_Zero_Reasoner-Coder-7b --local-dir-use-symlinks False --local-dir ./models/Absolute_Zero_Reasoner-Coder-7b
    
    # Download 7B Base model (Note: andrewzh2)
    hf download andrewzh2/Absolute_Zero_Reasoner-Base-7b --local-dir-use-symlinks False --local-dir ./models/Absolute_Zero_Reasoner-Base-7b
    
    # Download 14B Coder model
    hf download andrewzh/Absolute_Zero_Reasoner-Coder-14b --local-dir-use-symlinks False --local-dir ./models/Absolute_Zero_Reasoner-Coder-14b
    
    # Download 14B Base model (Note: andrewzh2)
    hf download andrewzh2/Absolute_Zero_Reasoner-Base-14b --local-dir-use-symlinks False --local-dir ./models/Absolute_Zero_Reasoner-Base-14b
    
    # Download Qwen baseline
    hf download Qwen/Qwen2.5-7B --local-dir-use-symlinks False --local-dir ./models/Qwen2.5-7B
  7. Evaluate code correctness with EvalPlus via CLI

    master

    Use the evalplus.evaluate command to evaluate LLM code generation performance on datasets like humaneval or mbpp. You must specify a --model, a --dataset, and a --backend. For most evaluations, adding --greedy is recommended.

    Results and generated code are saved to the evalplus_results/[humaneval|mbpp]/ directory.

    evalplus.evaluate --model "ise-uiuc/Magicoder-S-DS-6.7B" \
                      --dataset humaneval \
                      --backend hf \
                      --greedy
  8. Configure vLLM backends for EvalPlus

    master

    You can use the vllm backend directly for local models, or use the openai backend to connect to a running vLLM OpenAI-compatible server.

    Direct vLLM usage: Use the --tp flag to specify the Tensor Parallel size.

    vLLM OpenAI-compatible server usage: Set the --base-url to point to your server (e.g., http://localhost:8000/v1).

    evalplus.evaluate --model "ise-uiuc/Magicoder-S-DS-6.7B" \
                      --dataset [humaneval|mbpp]             \
                      --backend vllm                         \
                      --tp [TENSOR_PARALLEL_SIZE]            \
                      --greedy
  9. Run AZR Self-play training

    master

    Execute the self-play training scripts based on your model size.

    Hardware Requirements:

    • 3b models: 2 x 80GB GPUs
    • 7/8b models: 4 x 80GB GPUs
    • 14b models: 8 x 80GB GPUs

    Standard usage:

    bash scripts/selfplay/<7b|14b|coder3b|coder7b|coder14b|llama>.sh

    Using custom seed datasets: If you have custom datasets, export their paths first:

    export OUTPUT_SEED_PATH=data/<your_ded_abd_seed_data_name>.jsonl
    export OUTPUT_CODE_F_SEED_PATH=data/<your_ind_seed_data_name>.jsonl
    bash scripts/selfplay/<7b|14b|coder3b|coder7b|coder14b|llama>.sh

    Using Sandbox-Fusion Executor: To use the sandbox-fusion executor, run via Docker and set the configuration flag azr.executor=sandboxfusion.

    ⚠️WARNING⚠️: The Python executor in this repository is very raw and intended for research purposes only. It is not secure for production environments.

  10. Generate seed datasets for AZR

    master

    If you want to create your own seed data instead of using the provided datasets in data/, use the seeding scripts. You must export the output paths for the ded/abd seed data and the individual seed data before running the script.

    export OUTPUT_SEED_PATH=data/<new_ded_abd_seed_data_name>.jsonl
    export OUTPUT_CODE_F_SEED_PATH=data/<new_ind_seed_data_name>.jsonl
    bash scripts/seeding/<7b|14b|coder3b|coder7b|coder14b|llama>.sh
  11. Run Test-suite Reduction

    master

    The Test-suite Reduction tool requires that you have already run evaluation scripts and generated an eval_results.json file for each model being tested.

    Run the tool using run.py with the following parameters:

    • --dataset: The dataset to use. Currently supports humaneval and mbpp.
    • --sample_eval_dir: The directory containing LLM evaluation results. The directory must follow this structure:
      SAMPLE_EVAL_DIR
      ├── LLM_1
      │   ├── ...
      │   └── eval_results.json
      ├── LLM_2
      │   ├── ...
      │   └── eval_results.json
      └── ...
    • --model: The specific LLM name or ALL.
      • If a specific LLM name is provided, cross-validation results are generated in the report directory.
      • If ALL is provided, a reduced dataset is generated in the report directory.
    • --report_dir: (Optional) The directory to store intermediate files, pass@k results, and the reduced dataset. Defaults to ./tsr_info if not specified.
    python3 run.py \
      --dataset DATASET \
      --sample_eval_dir SAMPLE_DIR \
      --model MODEL \
      [--report_dir REPORT_DIR]