Skip to content

📋 Allow calling trl cli in sft mode with config file #3380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2025
Merged
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
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
# limitations under the License.


import os
import sys
import tempfile
import unittest
from io import StringIO
from unittest.mock import patch

import yaml


@unittest.skipIf(
sys.version_info < (3, 10),
Expand Down Expand Up @@ -67,6 +70,33 @@ def test_sft(self):
with patch("sys.argv", command.split(" ")):
main()

def test_sft_config_file(self):
from trl.cli import main

with tempfile.TemporaryDirectory() as tmp_dir: # Create a temporary directory
output_dir = os.path.join(tmp_dir, "output")

# Create a temporary config file
config_path = os.path.join(tmp_dir, "config.yaml")
config_content = {
"model_name_or_path": "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5",
"dataset_name": "trl-internal-testing/zen",
"dataset_config": "standard_language_modeling",
"report_to": "none",
"output_dir": output_dir,
"lr_scheduler_type": "cosine_with_restarts",
}
with open(config_path, "w") as config_file:
yaml.dump(config_content, config_file)

# Test the CLI with config file
command = f"trl sft --config {config_path}"
with patch("sys.argv", command.split(" ")):
main()

# Verify that output directory was created
self.assertTrue(os.path.exists(output_dir))


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion trl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def main():
make_vllm_serve_parser(subparsers)

# Parse the arguments
args = parser.parse_args()
args = parser.parse_args_and_config()[0]

if args.command == "chat":
(chat_args,) = parser.parse_args_and_config()
Expand Down
2 changes: 1 addition & 1 deletion trl/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ScriptArguments:
type, inplace operation. See https://github.com/huggingface/transformers/issues/22482#issuecomment-1595790992.
"""

dataset_name: str = field(metadata={"help": "Dataset name."})
dataset_name: Optional[str] = field(default=None, metadata={"help": "Dataset name."})
dataset_config: Optional[str] = field(
default=None,
metadata={
Expand Down
Loading