|
| 1 | +.. _finetune_agent: |
| 2 | + |
| 3 | +============================= |
| 4 | +Fine-tuning an Existing Model |
| 5 | +============================= |
| 6 | + |
| 7 | +## **🎯 Scenario: Continue Training on a Pre-trained Model** |
| 8 | + |
| 9 | +In this workflow the **Data Science Agent** starts from a *previously trained* model (and its training script), performs additional fine-tuning on new data, and then re-uses the updated weights for subsequent inference runs. |
| 10 | + |
| 11 | +🚧 Directory Structure |
| 12 | + |
| 13 | +Your competition folder (here called ``custom_data``) must contain **one extra sub-directory** named ``prev_model`` where you keep the old weights and the code that produced them: |
| 14 | + |
| 15 | +.. code-block:: text |
| 16 | +
|
| 17 | + ds_data |
| 18 | + └── custom_data |
| 19 | + ├── train.csv |
| 20 | + ├── test.csv |
| 21 | + ├── sample_submission.csv # optional |
| 22 | + ├── description.md # optional |
| 23 | + ├── sample.py # optional |
| 24 | + └── prev_model # ← NEW |
| 25 | + ├── models/ # previous checkpoints (e.g. *.bin, *.pt, *.ckpt) |
| 26 | + └── main.py # training/inference scripts you used before |
| 27 | +
|
| 28 | +If your competition provides custom grading/validation scripts, keep them under ``ds_data/eval/custom_data`` exactly as before. |
| 29 | + |
| 30 | +🔧 Environment Setup |
| 31 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +Add or update the following variables in **.env** (examples shown): |
| 34 | + |
| 35 | +.. code-block:: sh |
| 36 | +
|
| 37 | + # required for all Data-Science runs |
| 38 | + dotenv set DS_LOCAL_DATA_PATH <your local path>/ds_data |
| 39 | +
|
| 40 | + # optional: choose docker / conda, etc. |
| 41 | + dotenv set DS_CODER_COSTEER_ENV_TYPE docker |
| 42 | +
|
| 43 | +🚀 How It Works at Runtime |
| 44 | + |
| 45 | +1. **First run** |
| 46 | + |
| 47 | + * `rdagent` detects `prev_model/models`. |
| 48 | + * It loads the latest checkpoint and prepare the fine-tuning based on code found under `prev_model/*.py` (or your own pipeline if you override it). |
| 49 | + * Fine-tuned weights are written to `./workspace_input/models`. |
| 50 | + |
| 51 | +2. **Subsequent runs** |
| 52 | + |
| 53 | + * When you execute `python ./workspace_input/main.py`, the script first looks for a checkpoint in `./workspace_input/models`. |
| 54 | + * If found, it **skips fine-tuning** and goes straight to prediction / submission generation. |
| 55 | + |
| 56 | +⏰ Managing Timeouts |
| 57 | + |
| 58 | + |
| 59 | +By default: |
| 60 | + |
| 61 | +* **Debug loop**: 1 hour (``DS_DEBUG_TIMEOUT=3600`` seconds) |
| 62 | +* **Full run** : 3 hours (``DS_FULL_TIMEOUT=10800`` seconds) |
| 63 | + |
| 64 | +Override either value in **.env**: |
| 65 | + |
| 66 | +.. code-block:: sh |
| 67 | +
|
| 68 | + # give the debug loop 45 min and the full loop 6 h |
| 69 | + dotenv set DS_DEBUG_TIMEOUT 2700 |
| 70 | + dotenv set DS_FULL_TIMEOUT 21600 |
| 71 | +
|
| 72 | +- 🚀 **Run the Application** |
| 73 | + |
| 74 | + - You can directly run the application by using the following command: |
| 75 | + |
| 76 | + .. code-block:: sh |
| 77 | +
|
| 78 | + dotenv run -- python rdagent/app/finetune/data_science/loop.py --competition <Competition ID> |
| 79 | +
|
| 80 | + - Then, you can run the test set score corresponding to each round of the loop. |
| 81 | + |
| 82 | + .. code-block:: sh |
| 83 | +
|
| 84 | + dotenv run -- python rdagent/log/mle_summary.py grade <url_to_log> |
| 85 | +
|
| 86 | + Here, <url_to_log> refers to the parent directory of the log folder generated during the run. |
| 87 | + |
| 88 | +- 📥 **Visualize the R&D Process** |
| 89 | + |
| 90 | + - We provide a web UI to visualize the log. You just need to run: |
| 91 | + |
| 92 | + .. code-block:: sh |
| 93 | +
|
| 94 | + streamlit run rdagent/log/ui/dsapp.py |
| 95 | +
|
| 96 | + - Then you can input the log path and visualize the R&D process. |
| 97 | + |
| 98 | +🔍 MLE-bench Guide: Running ML Engineering via MLE-bench |
| 99 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +- 📝 **MLE-bench Overview** |
| 102 | + |
| 103 | + - MLE-bench is a comprehensive benchmark designed to evaluate the ML engineering capabilities of AI systems using real-world scenarios. The dataset comprises 75 Kaggle competitions. Since Kaggle does not provide held-out test sets for these competitions, the benchmark includes preparation scripts that split the publicly available training data into new training and test sets, and grading scripts are provided for each competition to accurately evaluate submission scores. |
| 104 | + |
| 105 | +- 🔧 **Set up Environment for MLE-bench** |
| 106 | + |
| 107 | + - Running R&D-Agent on MLE-bench is designed for full automation. There is no need for manual downloads and data preparation. Simply set the environment variable ``DS_IF_USING_MLE_DATA`` to True. |
| 108 | + |
| 109 | + - At runtime, R&D-Agent will automatically build the Docker image specified at ``rdagent/scenarios/kaggle/docker/mle_bench_docker/Dockerfile``. This image is responsible for downloading the required datasets and grading files for MLE-bench. |
| 110 | + |
| 111 | + - Note: The first run may take longer than subsequent runs as the Docker image and data are being downloaded and set up for the first time. |
| 112 | + |
| 113 | + .. code-block:: sh |
| 114 | +
|
| 115 | + dotenv set DS_LOCAL_DATA_PATH <your local directory>/ds_data |
| 116 | + dotenv set DS_IF_USING_MLE_DATA True |
| 117 | +
|
| 118 | +- 🔨 **Configuring the Kaggle API** |
| 119 | + |
| 120 | + - Downloading Kaggle competition data requires the Kaggle API. You can set up the Kaggle API by following these steps: |
| 121 | + |
| 122 | + - Register and login on the `Kaggle <https://www.kaggle.com/>`_ website. |
| 123 | + |
| 124 | + - Click on the avatar (usually in the top right corner of the page) -> ``Settings`` -> ``Create New Token``, A file called ``kaggle.json`` will be downloaded. |
| 125 | + |
| 126 | + - Move ``kaggle.json`` to ``~/.config/kaggle/`` |
| 127 | + |
| 128 | + - Modify the permissions of the ``kaggle.json`` file. |
| 129 | + |
| 130 | + .. code-block:: sh |
| 131 | +
|
| 132 | + chmod 600 ~/.config/kaggle/kaggle.json |
| 133 | +
|
| 134 | + - For more information about Kaggle API Settings, refer to the `Kaggle API <https://github.com/Kaggle/kaggle-api>`_. |
| 135 | + |
| 136 | + |
| 137 | +- 🔩 **Setting the Environment Variables for MLE-bench** |
| 138 | + |
| 139 | + - In addition to auto-downloading the benchmark data, you must also configure the runtime environment for executing the competition code. |
| 140 | + - Use the environment variable ``DS_CODER_COSTEER_ENV_TYPE`` to select the execution mode: |
| 141 | + |
| 142 | + • When set to docker (the default), RD-Agent utilizes the official Kaggle Docker image (``gcr.io/kaggle-gpu-images/python:latest``) to ensure that all required packages are available. |
| 143 | + • If you prefer to use a custom Docker setup, you can modify the configuration using ``DS_DOCKER_IMAGE`` or ``DS_DOCKERFILE_FOLDER_PATH``. |
| 144 | + • Alternatively, if your competition work only demands basic libraries, you may set ``DS_CODER_COSTEER_ENV_TYPE`` to conda. In this mode, you must create a local conda environment named “kaggle” and pre-install the necessary packages. RD-Agent will execute the competition code within this “kaggle” conda environment. |
| 145 | + |
| 146 | + .. code-block:: sh |
| 147 | +
|
| 148 | + # Configure the runtime environment: choice between 'docker' (default) or 'conda' |
| 149 | + dotenv set DS_CODER_COSTEER_ENV_TYPE docker |
| 150 | +
|
| 151 | +- **Additional Guidance** |
| 152 | + |
| 153 | + - **Combine different LLM Models at R&D Stage** |
| 154 | + |
| 155 | + - You can combine different LLM models at the R&D stage. |
| 156 | + |
| 157 | + - By default, when you set environment variable ``CHAT_MODEL``, it covers both R&D stages. When customizing the model for the development stage, you can set: |
| 158 | + |
| 159 | + .. code-block:: sh |
| 160 | +
|
| 161 | + # This example sets the model to "o3-mini". For some models, the reasoning effort shoule be set to "None". |
| 162 | + dotenv set LITELLM_CHAT_MODEL_MAP '{"coding":{"model":"o3-mini","reasoning_effort":"high"},"running":{"model":"o3-mini","reasoning_effort":"high"}}' |
| 163 | +
|
0 commit comments