SWE-smith Documentation

repository·main·Indexed 20 days ago

https://github.com/swe-bench/swe-smith

A toolkit for generating software engineering training data at scale. SWE-smith enables the transformation of GitHub repositories into training environments (SWE-gyms), the synthesis of diverse tasks such as program repair and file localization, and the training of high-performance language models for code repair. It includes tools for LLM-based bug generation, custom criteria filtering, and integration with Modal for fine-tuning and hosting models like SWE-agent-LM-32B.

Tokens
13K
Snippets
49
Records
64
Agent score
71%

What's inside SWE-smith

  1. Overview of SWE-smith

    main

    SWE-smith is a toolkit designed for training SWE-agents. It enables developers to:

    • Convert GitHub repositories into SWE-gyms: Transform any repository into a training environment.
    • Synthesize unlimited tasks: Generate various task types such as file localization, program repair, and SWE-bench style tasks for a given repository.
    • Train Language Models: Use the generated data to train models to become better software engineering agents (e.g., the SWE-agent-LM-32B model).

    Requirements & Compatibility

    • Python: Requires Python 3.10+.
    • Docker: Required to create and manage execution environments.
    • OS Support: Developed and tested on Ubuntu 22.04.4 LTS. Note that Windows and MacOS are not currently supported.
  2. Overview of SWE-smith capabilities

    main

    SWE-smith provides a pipeline for creating high-quality training data for software engineering agents. The core capabilities include:

    • Build Environments: Create reproducible Docker images for any repository, capturing dependencies and validating with automated testing.
    • Create Instances: Generate task instances using LM prompts, procedural modifications, PR mirroring, or combined techniques.
    • Validate & Evaluate: Use built-in harnesses to filter candidates that break tests and verify proposed solutions.
    • Generate Issue Text: Use LM generation or other methods to add natural language problem statements to task instances.
    • Rate Difficulty: Classify tasks as easy, medium, or hard using a fine-tuned Qwen 2.5 Coder model.
    • Train SWE-agents: Execute a complete RSFT (Rejection Sampling Fine-Tuning) pipeline: generate trajectories, filter successful solutions, fine-tune models, and evaluate on SWE-bench.
  3. The SWE-smith recommended workflow

    main

    The complete workflow for building training data and training SWE-agents follows these steps in order:

    1. Build Environments: Set up Docker images for repositories.
    2. Create Instances: Generate synthetic bugs/task instances.
    3. Validate & Evaluate: Filter valid task instances using built-in harnesses.
    4. Generate Issue Text: Add natural language problem descriptions to instances.
    5. Rate Difficulty (Optional): Classify task complexity (easy/medium/hard).
    6. Train SWE-agents: Fine-tune models using the RSFT pipeline.
  4. Define custom criteria for bug generation

    main

    Criteria are used to filter functions so that the LLM only attempts to generate bugs in code where that specific bug is actually possible (e.g., only attempting 'off-by-one' bugs on functions containing loops or list indexing).

    To create a new criterion, add a function to swesmith/bug_gen/llm/criteria.py following this pattern:

    1. The function name should follow the pattern filter_<criteria_name>.
    2. It must accept a CodeEntity object.
    3. The CodeEntity object provides:
      • src_code: The raw string representation of the function.
      • src_node: An AST node representation of the function.
    4. The function must return True if the function satisfies the criteria, or False otherwise.
    def filter_<criteria>(code_entity: CodeEntity) -> bool:
        """
        `code_entity` is an object representing a function. It includes several
        pieces of information, most notably:
            * `src_code`: The raw string repr. of a function
            * `src_node`: An AST node representation of a function.
        """
        node = code_entity.src_node
        # Logic for checking whether a function has a property
        if satisfies_criteria:
            return True
        return False
  5. Generate task instances using LM Modify or LM Rewrite

    main

    You can use Language Models (LMs) to introduce bugs by either modifying an existing programmatic entity (class or function) or rewriting it from scratch.

    Artifacts Produced: Under logs/bug_gen/<repo>, artifacts are organized by file path and then by individual entity.

    • For LM Modify: bug__lm_modify__<hash>.diff and metadata__lm_modify__<hash>.json.
    • For LM Rewrite: bug__lm_rewrite__<hash>.diff and metadata__lm_rewrite__<hash>.json.

    The <hash> is computed over the contents of the .diff file.

    # To prompt an LM to modify a function to introduce a bug:
    python -m swesmith.bug_gen.llm.modify $repo \
      --n_bugs 1 \
      --model openai/gpt-4o \
      --config_file configs/bug_gen/lm_modify.yml
    
    # To prompt an LM to rewrite a function from scratch:
    python -m swesmith.bug_gen.llm.rewrite $repo \
      --model anthropic/claude-3-7-sonnet-20250219 \
      --config_file configs/bug_gen/lm_rewrite.yml \
      --n_workers 1
  6. Fine-tune a model with SWE-smith dataset

    main

    Run the fine-tuning process using torchtune via Modal. You must provide a configuration file (e.g., train/config/torchtune.yml) and specify the number of GPUs using the NGPUS environment variable.

    NGPUS=8 modal run train/run_ft_torchtune.py --config train/config/torchtune.yml
  7. Evaluate the SFT-ed model on SWE-bench

    main

    To evaluate your newly trained model on SWE-bench (Lite/Verified/Multimodal):

    1. Serve the model: Update scripts/train.serve_sglang.sh with the path to your SFT-ed model and run it.
    2. Run inference: Use the SWE-agent inference script ./agent/_infer_model.sh. Ensure the Modal URL is correct and the evaluation dataset is specified.
    3. Submit results: Use sb-cli to submit the predictions for evaluation on the target SWE-bench dataset.
    # 1. Serve (Update script first)
    ./scripts/train.serve_sglang.sh
    
    # 2. Inference
    ./agent/_infer_model.sh
    
    # 3. Submit
    sb-cli submit swe-bench_verified test \
        --predictions_path trajectories/<username>/<run ID>/preds.json \
        --run_id <run ID>
  8. Run inference with SWE-agent using a local Modal model

    main

    Once the model is hosted on Modal, you can run inference using the sweagent run-batch command from your local SWE-agent repository.

    Key configuration requirements:

    • --agent.model.api_base: Set this to the Modal endpoint URL (e.g., <MODAL_LINK>/v1).
    • --agent.model.api_key: Set this to swesmith (as configured in serve_sglang.py).
    • --agent.model.name: The name of the served model.
    • --instances.type: Set to swe_bench.
    • --instances.dataset_name: The name of the dataset (e.g., jyang20/swebv-mini).
    • --instances.split: The dataset split (e.g., test).
    • --config: Path to the SWE-agent configuration file.
    sweagent run-batch \
        --agent.model.api_base <REPLACE WITH MODAL LINK>/v1 \
        --agent.model.api_key swesmith \
        --agent.model.name gpt-4o \
        --instances.type swe_bench \
        --instances.dataset_name jyang20/swebv-mini \
        --instances.split test \
        --config config/anthropic_no_fcalls.yaml
  9. Access SWE-smith datasets and models

    main

    The following assets are available via HuggingFace:

  10. Train a model using Rejection Sampling Fine Tuning

    main

    Once you have the ft_xml_*.jsonl file, follow these steps to train your model:

    1. Upload the SFT dataset to Modal: modal volume put <volume> trajectories_sft/ft_xml_*.jsonl
    2. Update your training configuration (e.g., config/train/full_ft_qwen_7b.yml) to point to the file location on Modal.
    3. Execute the training script: ./scripts/train.run_ft_torchtune.py.
    modal volume put <volume> trajectories_sft/ft_xml_*.jsonl
    ./scripts/train.run_ft_torchtune.py