RecursiveMAS Multi-Agent Framework

repository·main·Indexed 21 days ago

https://github.com/recursivemas/recursivemas

A multi-agent framework that scales agent collaboration using latent-space recursion. It treats multi-agent systems as unified recursive computations using RecursiveLink modules to exchange latent states. The framework supports several collaboration patterns, including Sequential, Mixture, Distillation, and Deliberation, and utilizes a progressive co-optimization paradigm consisting of inner-loop training for individual agents and outer-loop training for the system-level RecursiveLink.

Tokens
7.5K
Snippets
22
Records
27
Agent score
75%

What's inside RecursiveMAS

  1. Explore the RecursiveMAS training code structure

    main

    The ./train directory contains the complete training pipeline. The following files and directories define the training logic:

    • train_inner.py: Entry point for inner-loop training.
    • train_outer.py: Entry point for outer-loop training.
    • data.py: Handles dataset loading and tokenization.
    • model.py: Defines RecursiveLink and model adapter modules.
    • mas_prompt.py: Manages training prompts based on different collaboration styles.
    • outer/: Contains style-specific outer-training implementations:
      • common.py: Shared utilities for outer training.
      • sequential.py: Sequential-style training.
      • mixture.py: Mixture-style training.
      • distillation.py: Distillation-style training.
      • deliberation.py: Deliberation-style training.
    • data/: Contains documentation regarding Hugging Face dataset mapping and local backup formats.
    train/
    ├── train_inner.py              # inner-loop training
    ├── train_outer.py              # outer-loop training
    ├── data.py                     # dataset loading and tokenization
    ├── model.py                    # RecursiveLink and model adapter modules
    ├── mas_prompt.py               # training prompts by collaboration style
    ├── outer/
    │   ├── common.py               # shared outer-training utilities
    │   ├── sequential.py
    │   ├── mixture.py
    │   ├── distillation.py
    │   └── deliberation.py
    └── data/
        └── README.md               # Hugging Face dataset mapping and local backup format
  2. What is RecursiveMAS and how does it work?

    main

    RecursiveMAS is a multi-agent framework that scales agent collaboration through latent-space recursion. Instead of treating LLM agents as isolated modules, it treats the entire multi-agent system (MAS) as a unified recursive computation.

    Key components include:

    • Heterogeneous Agents: Different LLM agents connected via RecursiveLink modules.
    • RecursiveLink: Lightweight modules that allow agents to exchange, refine, and evolve latent states across recursion rounds.
    • Inner-Outer Loop Training: A progressive co-optimization paradigm where the inner loop provides a model-level warm start for individual agents, and the outer loop trains the RecursiveLink across agents at the system level.

    It supports several collaboration patterns: Sequential, Mixture, Distillation, and Deliberation.

  3. Understand the Inner-Outer Loop training architecture

    main

    RecursiveMAS training follows a two-phase approach to optimize agent collaboration through recursion:

    1. Inner-Loop Training: Performed using train/train_inner.py. This phase trains each agent's role-specific inner RecursiveLink. This involves a frozen base model combined with a small ln_res_adapter. This step is run once per agent role.

    2. Outer-Loop Training: Performed using train/train_outer.py. This phase connects all agents and trains the outer RecursiveLink between them. It loads the inner adapters from the first stage and keeps them frozen. Only the outer RecursiveLink modules are optimized.

    Note on Gradients: During outer-loop training, gradients are allowed to pass through the frozen inner adapters, enabling the outer links to be trained across multiple recursion rounds.

  4. Inference Styles and Checkpoint Mapping

    main

    RecursiveMAS implements different collaboration styles, each requiring specific model roles via --ckpt_override:

    Sequential-Style

    Roles: planner, critic, solver, outer.

    Distillation-Style

    Roles: expert, learner, outer.

    Mixture-Style

    Roles: math, code, science, summarizer, outer.

    Deliberation-Style

    Roles: reflector, toolcaller, outer. Requires --tavily_keys_file for search integration.

    # Example: Mixture-Style
    python inference/run.py \
      --style mixture \
      --dataset math500 \
      --device cuda \
      --ckpt_override math=train/ckpts/mixture/math_expert \
      --ckpt_override code=train/ckpts/mixture/code_expert \
      --ckpt_override science=train/ckpts/mixture/science_expert \
      --ckpt_override summarizer=train/ckpts/mixture/summarizer \
      --ckpt_override outer=train/ckpts/mixture/outer
  5. Perform Outer-Loop Training with `train_outer.py`

    main

    Outer-Loop Training is the second phase of the training pipeline. It connects all agents together and trains the outer RecursiveLink between agents through recursion.

    Use train/train_outer.py to perform this step. This requires specifying the collaboration style (e.g., sequential_light), the model paths for all participating agents, and the paths to the inner aligners (checkpoints) trained during the Inner-Loop phase.

    python train/train_outer.py \
      --style sequential_light \
      --agent1_model_name_or_path Qwen/Qwen3-1.7B \
      --agent2_model_name_or_path meta-llama/Llama-3.2-1B-Instruct \
      --agent3_model_name_or_path Qwen/Qwen2.5-Math-1.5B-Instruct \
      --agent1_inner_aligner_path train/ckpts/seq_light/planner_math \
      --agent2_inner_aligner_path train/ckpts/seq_light/refiner_math \
      --agent3_inner_aligner_path train/ckpts/seq_light/solver_math \
      --mas_task math \
      --dataset_name RecursiveMAS/Sequential-Math \
      --save_dir train/ckpts/seq_light/outer_math
  6. Evaluate models using `inference/run.py`

    main

    Use inference/run.py to evaluate either a released reference system or a locally trained, task-specific configuration.

    When evaluating, you can use the --ckpt_override flag multiple times to map specific agent roles (e.g., planner, critic, solver, outer) to their respective checkpoint directories.

    # Evaluate Sequential Light Style RecursiveMAS on Math500
    python inference/run.py \
      --style sequential_light \
      --dataset math500 \
      --device cuda \
      --ckpt_override planner=train/ckpts/seq_light/planner_math \
      --ckpt_override critic=train/ckpts/seq_light/refiner_math \
      --ckpt_override solver=train/ckpts/seq_light/solver_math \
      --ckpt_override outer=train/ckpts/seq_light/outer_math
  7. Train Deliberation-Style models

    main

    Deliberation-Style uses a reflector agent and a tool-caller agent.

    Backbone Models

    RoleBackbone Model
    reflectorQwen/Qwen3.5-4B
    tool-callerQwen/Qwen3.5-4B

    Use --mas_design deliberation for inner-loop and --style deliberation for outer-loop.

    # Inner-Loop training (Reflector)
    python train/train_inner.py \
      --model_name_or_path Qwen/Qwen3.5-4B \
      --mas_design deliberation \
      --mas_role deliberation_reflector \
      --mas_task math \
      --dataset_name RecursiveMAS/Deliberation \
      --save_dir train/ckpts/deliberation/reflector
    
    # Outer-Loop training
    python train/train_outer.py \
      --style deliberation \
      --reflector_model_name_or_path Qwen/Qwen3.5-4B \
      --toolcaller_model_name_or_path Qwen/Qwen3.5-4B \
      --reflector_inner_aligner_path train/ckpts/deliberation/reflector \
      --toolcaller_inner_aligner_path train/ckpts/deliberation/toolcaller \
      --mas_task math \
      --dataset_name RecursiveMAS/Deliberation \
      --save_dir train/ckpts/deliberation/outer
  8. Use local JSON files for training data

    main

    You can use custom offline training data by placing JSON files in the ./train/data directory.

    Each JSON file must follow this structure:

    {
      "data": [
        {
          "question": "...",
          "answer": "..."
        }
      ]
    }

    To load local data, use the following flags in your training command:

    • --dataset_name: The path to your local JSON file.
    • --dataset_json_field: The name of the JSON key containing the data array (e.g., data).

    Note: The --dataset_json_field flag should only be used for offline local data loading; it can be ignored when using Hugging Face datasets.

    --dataset_name train/data/Sequential-Math.json \
    --dataset_json_field data
  9. Train Mixture-Style models

    main

    Mixture-Style uses three domain-expert agents and one summarizer agent.

    Backbone Models

    RoleBackbone Model
    math expertdeepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
    code expertQwen/Qwen2.5-Coder-3B-Instruct
    science expertBioMistral/BioMistral-7B
    summarizerQwen/Qwen3.5-2B

    Use --mas_design hie for inner-loop and --style mixture for outer-loop.

    # Outer-Loop training
    python train/train_outer.py \
      --style mixture \
      --agent1_model_name_or_path deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B \
      --agent2_model_name_or_path Qwen/Qwen2.5-Coder-3B-Instruct \
      --agent3_model_name_or_path BioMistral/BioMistral-7B \
      --agent4_model_name_or_path Qwen/Qwen3.5-2B \
      --agent1_inner_aligner_path train/ckpts/mixture/math_expert \
      --agent2_inner_aligner_path train/ckpts/mixture/code_expert \
      --agent3_inner_aligner_path train/ckpts/mixture/science_expert \
      --agent4_inner_aligner_path train/ckpts/mixture/summarizer \
      --mas_task math \
      --dataset_name RecursiveMAS/Mixture-Outer \
      --save_dir train/ckpts/mixture/outer
  10. Perform Inner-Loop Training with `train_inner.py`

    main

    Inner-Loop Training is the first phase of the RecursiveMAS training pipeline. It involves training each agent's role-specific inner RecursiveLink, which consists of a frozen base model and a small ln_res_adapter.

    Use train/train_inner.py to perform this step. You must specify the model path, the MAS design (e.g., sequential), the specific agent role (e.g., planner), the task (e.g., math), and the corresponding dataset.

    python train/train_inner.py \
      --model_name_or_path Qwen/Qwen3-1.7B \
      --mas_design sequential \
      --mas_role planner \
      --mas_task math \
      --dataset_name RecursiveMAS/Sequential-Math \
      --save_dir train/ckpts/seq_light/planner_math
  11. Use Hugging Face datasets for training

    main

    RecursiveMAS training data is hosted on Hugging Face as row-format datasets with a train split. It is recommended to use these datasets directly whenever possible. To use a Hugging Face dataset, pass its name to the --dataset_name flag in the training command.

    python train/train_inner.py \
      --dataset_name RecursiveMAS/Sequential-Math \
      ...
  12. Install RecursiveMAS and set up the environment

    main

    To set up RecursiveMAS, create a Python 3.10 environment using Conda and install the required dependencies from the repository root.

    1. Create and activate environment

    conda create -n recursivemas python=3.10 -y
    conda activate recursivemas

    2. Install dependencies

    pip install -r requirements.txt
    conda create -n recursivemas python=3.10 -y
    conda activate recursivemas
    pip install -r requirements.txt