File size: 11,614 Bytes
1fa3c6c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | # Copyright 2020-2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import subprocess
from pathlib import Path
import pytest
import torch
import transformers
from packaging.version import Version
from ..testing_utils import TrlTestCase, require_torch_multi_accelerator
ROOT = Path(__file__).resolve().parents[2]
def run_command(command: list[str], env: dict[str, str]) -> None:
result = subprocess.run(command, env=env, cwd=ROOT)
assert result.returncode == 0
@pytest.fixture
def get_config_path(lazy_shared_datadir):
def _get_config_path(config_name):
return lazy_shared_datadir / "accelerate_configs" / f"{config_name}.yaml"
return _get_config_path
@require_torch_multi_accelerator
class TestDistributed(TrlTestCase):
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
"fsdp2",
],
)
def test_sft(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/sft.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "standard_language_modeling",
],
os.environ.copy(),
)
# fmt: on
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
"fsdp2",
],
)
def test_dpo(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/dpo.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "standard_preference",
],
os.environ.copy(),
)
# fmt: on
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
"fsdp2",
],
)
def test_sft_dataset_streaming(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/sft.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "standard_language_modeling",
"--dataset_streaming",
"--max_steps", "3",
],
os.environ.copy(),
)
# fmt: on
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
condition=Version("2.10") <= Version(torch.__version__)
and Version(transformers.__version__) < Version("5.1.0"),
reason="ZeRO 2 + PEFT was failing before transformers 5.1.0 on torch 2.10; see #4884",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
condition=Version("2.10") <= Version(torch.__version__)
and Version(transformers.__version__) < Version("5.1.0"),
reason="ZeRO 3 + PEFT was failing before transformers 5.1.0 on torch 2.10; see #4884",
),
),
"fsdp2",
],
)
def test_sft_peft(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/sft.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "standard_language_modeling",
"--use_peft",
],
os.environ.copy(),
)
# fmt: on
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
"fsdp2",
],
)
def test_reward(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/reward.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "conversational_implicit_prompt_preference",
],
os.environ.copy(),
)
# fmt: on
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
Version("5.0.0") <= Version(transformers.__version__) < Version("5.5.4"),
reason="ZeRO-3 fails with transformers >= 5.0.0 and < 5.5.4 (fixed in transformers#45414), see #4899",
strict=True,
),
),
pytest.param(
"fsdp2",
marks=pytest.mark.skipif(
Version("5.4.0") <= Version(transformers.__version__) < Version("5.6.0"),
reason="Upstream issue: NaN weights on non-rank-0 FSDP processes (see #5386 and transformers#45050)",
),
),
],
)
def test_rloo(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/rloo.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "conversational_prompt_only",
"--reward_model_name_or_path", "trl-internal-testing/tiny-Qwen2ForSequenceClassification-2.5",
],
os.environ.copy(),
)
# fmt: on
@pytest.mark.parametrize(
"config",
[
"ddp",
pytest.param(
"zero2",
marks=pytest.mark.xfail(
Version(transformers.__version__) == Version("5.1.0"),
reason="Upstream incompatibility: deepspeed and transformers==5.1.0 (see transformers#43780)",
),
),
pytest.param(
"zero3",
marks=pytest.mark.xfail(
Version("5.0.0") <= Version(transformers.__version__) < Version("5.5.4"),
reason="ZeRO-3 fails with transformers >= 5.0.0 and < 5.5.4 (fixed in transformers#45414), see #4899",
strict=True,
),
),
"fsdp2",
],
)
def test_grpo(self, config, get_config_path):
# fmt: off
run_command(
[
"accelerate", "launch", "--config_file", get_config_path(config), "trl/scripts/grpo.py",
"--output_dir", self.tmp_dir,
"--model_name_or_path", "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"--dataset_name", "trl-internal-testing/zen",
"--dataset_config", "conversational_prompt_only",
"--reward_model_name_or_path", "trl-internal-testing/tiny-Qwen2ForSequenceClassification-2.5",
],
os.environ.copy(),
)
# fmt: on
|