ReST-MCTS*

repository·main·Indexed 20 days ago

https://github.com/thudm/rest-mcts

A reinforced self-training approach that integrates process reward guidance with MCTS* tree search to collect high-quality reasoning traces and per-step values. It supports policy and reward model training without manual per-step annotations, with implementations for Llama3-8B-Instruct, Mistral-7B, and SciGLM-6B. The framework includes tools for MCTS* search, benchmark evaluation, and self-training via CoT and DPO datasets.

Tokens
1.7K
Snippets
6
Records
12
Agent score
22%

What's inside ReST-MCTS*

  1. Install ReST-MCTS dependencies

    main

    ReST-MCTS requires different environments depending on whether you are running Mistral/Llama models or SciGLM models due to transformers dependency variations. Use Miniconda to manage these environments.

    • For Mistral or Llama models: Use Python 3.12.
    • For SciGLM models: Use Python 3.11.

    Install the required packages using the provided requirements files.

    # For running Mistral (or Llama)
    pip install -r requirements_mistral.txt
    
    # For running SciGLM
    pip install -r requirements_sciglm.txt
  2. Download Policy Data for Self-Training

    main

    Policy data is used for training and comparing policy models across different iterations and self-training methods.

    Data Characteristics:

    • CoT (Chain of Thought) and MCTS datasets include only positive samples.
    • DPO (Direct Preference Optimization) datasets include both positive and negative samples.

    Datasets are available on Hugging Face for the following backbones:

    • Llama3-8b-Instruct
    • Mistral: MetaMATH-7b
    • SciGLM-6B

    Each backbone has data for the 1st and 2nd iterations, categorized by self-training method: ReST-EM (CoT), Self-Rewarding (DPO), and ReST-MCTS.

  3. Configure models in models/model.py

    main

    To run MCTS* search, you must implement a policy model and a process reward model (value model). You can configure these by editing models/model.py with the following keys:

    • INFERENCE_MODEL_DIR: Local path to the policy model (e.g., Llama3-8B-Instruct, Mistral-7B: MetaMATH, or SciGLM-6B).
    • VALUE_BASE_MODEL_DIR: Local path to the value model backbone.
      • Use Mistral-7B if the policy is Llama3-8B-Instruct or Mistral-7B: MetaMATH.
      • Use ChatGLM3-6B if the policy is SciGLM-6B.
    • VALUE_MODEL_STATE_DICT: The state dict for the value model.
    • LOCAL_INFERENCE_IDX: The index corresponding to your implemented policy model.
    • LOCAL_VALUE_IDX: The index corresponding to your implemented value model.

    Supported implementations in models/model.py include llama, glm, and mistral for policies, and glm and mistral for value models.

  4. Run MCTS* search for a single question

    main

    You can use the MCTS_Task interface in MCTS/task.py to run a search for a specific question. The lang parameter specifies the language (e.g., 'en').

    from MCTS.task import *
    question = "Calculate the sum of the first 10 prime numbers."
    task = MCTS_Task(question, 'llama', 'local', lang='en')
    output = task.run()
    print(output['solution'])
  5. Prepare target question datasets

    main

    Before running MCTS* search for evaluation or generation, ensure your target question dataset is a JSON file where each item follows this format:

    • content (Required): The question text.
    • answer (Optional): The ground truth answer, used for evaluation.
    {
      "content": "Calculate the sum of the first 10 prime numbers.",
      "answer": "129"
    }
  6. Configure MCTS* algorithm parameters

    main

    The MCTS* algorithm is controlled via several key parameters that govern search behavior, exploration/exploitation balance, and simulation strategies. Use these arguments to tune the tree search process:

    Search & Generation Control

    • temperature: Search temperature; determines the degrees of freedom for generating responses.
    • time_limit: The upper limit of the search time in milliseconds (ms).
    • iteration_limit: The maximum number of search rounds allowed for exploration.
    • use_case_prompt: Boolean flag to enable sample output prompt assisted generation.
    • use_reflection: Boolean flag to enable the reflection mechanism.

    MCTS Logic & UCT Formula

    • exploration_constant: The constant used in the UCT (Upper Confidence Bound applied to Trees) formula to balance exploration and exploitation.
    • branch: The number of branches to create during node expansion.
    • inf: The base value assigned to an unvisited node.
    • alpha: The value update weight used for Monte Carlo simulation.
    • end_gate: The lowest value threshold used to determine when the search should end.

    Simulation (Rollout) Settings

    • roll_policy: The strategy for Monte Carlo simulation. Supported values are random or greedy.
    • roll_forward_steps: The number of forward steps to take during the simulation process.
    • roll_branch: The number of branches to sample during simulation.

    Value Bounds & Visualization

    • low: The lower bound of the node value.
    • high: The upper bound of the node value.
    • visualize: Boolean flag to determine whether search results are rendered in a tree diagram.
  7. Evaluate MCTS* on benchmarks

    main

    To evaluate MCTS* performance on benchmarks, use the evaluate.py script with --mode "mcts".

    Key arguments:

    • --task_name: The name of the benchmark task.
    • --file: The specific file or subset to evaluate.
    • --propose_method: The method used for proposing solutions (e.g., gpt).
    • --value_method: The method used for value estimation (e.g., local).
    • --mode: Set to mcts for MCTS* evaluation.
    • --evaluate: The benchmark name.
    • --iteration_limit: Maximum iterations.
    • --use_reflection: Reflection strategy (e.g., simple).
    • --branch: Branching factor for search.
    python evaluate.py \
      --task_name "scibench" \
      --file "thermo" \
      --propose_method "gpt" \
      --value_method "local" \
      --mode "mcts" \
      --evaluate "scibench" \
      --iteration_limit 50 \
      --use_reflection "simple" \
      --branch 3