Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_accelerate_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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.

from pathlib import Path

import pytest

from trl.cli.accelerate_config import resolve_accelerate_config_argument


@pytest.mark.parametrize(
"config_argument", [["--accelerate_config", "single_gpu"], ["--accelerate_config=single_gpu"]]
)
def test_resolve_packaged_accelerate_config(config_argument):
launch_args = resolve_accelerate_config_argument([*config_argument, "--num_processes", "2"])

assert launch_args[0] == "--config_file"
assert Path(launch_args[1]).name == "single_gpu.yaml"
assert launch_args[2:] == ["--num_processes", "2"]


@pytest.mark.parametrize("config_argument", ["--accelerate_config", "--accelerate_config="])
def test_resolve_accelerate_config_requires_value(config_argument):
with pytest.raises(ValueError, match="Expected a value"):
resolve_accelerate_config_argument([config_argument])
24 changes: 16 additions & 8 deletions trl/cli/accelerate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ def resolve_accelerate_config_argument(launch_args: list[str]) -> list[str]:
The function supports either a filesystem path or a predefined config name shipped in `trl/accelerate_configs`
(without the `.yaml` suffix).
"""
if "--accelerate_config" not in launch_args:
for config_index, argument in enumerate(launch_args):
if argument == "--accelerate_config":
if config_index + 1 >= len(launch_args):
raise ValueError("Expected a value after `--accelerate_config`.")
config_name = launch_args[config_index + 1]
argument_count = 2
break
if argument.startswith("--accelerate_config="):
config_name = argument.split("=", 1)[1]
if not config_name:
raise ValueError("Expected a value after `--accelerate_config`.")
argument_count = 1
break
else:
return launch_args

config_index = launch_args.index("--accelerate_config")
if config_index + 1 >= len(launch_args):
raise ValueError("Expected a value after `--accelerate_config`.")

config_name = launch_args[config_index + 1]
if Path(config_name).is_file():
accelerate_config_path = config_name
else:
Expand All @@ -42,6 +50,6 @@ def resolve_accelerate_config_argument(launch_args: list[str]) -> list[str]:
)
accelerate_config_path = candidate

# Remove '--accelerate_config <value>'.
launch_args = launch_args[:config_index] + launch_args[config_index + 2 :]
# Remove '--accelerate_config <value>' or '--accelerate_config=<value>'.
launch_args = launch_args[:config_index] + launch_args[config_index + argument_count :]
return ["--config_file", str(accelerate_config_path)] + launch_args