Scripts Utilities
ScriptArguments
class trl.ScriptArguments
< source >( dataset_name: str dataset_config: typing.Optional[str] = None dataset_train_split: str = 'train' dataset_test_split: str = 'test' gradient_checkpointing_use_reentrant: bool = False ignore_bias_buffers: bool = False )
Parameters
- dataset_name (
str
) — Dataset name. - dataset_config (
str
orNone
, optional, defaults toNone
) — Dataset configuration name. Corresponds to thename
argument of the load_dataset function. - dataset_train_split (
str
, optional, defaults to"train"
) — Dataset split to use for training. - dataset_test_split (
str
, optional, defaults to"test"
) — Dataset split to use for evaluation. - gradient_checkpointing_use_reentrant (
bool
, optional, defaults toFalse
) — Whether to applyuse_reentrant
for gradient_checkpointing. - ignore_bias_buffers (
bool
, optional, defaults toFalse
) — Debug argument for distributed training. Fix for DDP issues with LM bias/mask buffers - invalid scalar type, inplace operation. See https://github.com/huggingface/transformers/issues/22482#issuecomment-1595790992.
Arguments common to all scripts.
TrlParser
class trl.TrlParser
< source >( dataclass_types: typing.Union[transformers.hf_argparser.DataClassType, typing.Iterable[transformers.hf_argparser.DataClassType]] ignore_extra_args: typing.Optional[bool] = None **kwargs )
Parameters
- dataclass_types (
Union[DataClassType, Iterable[DataClassType]]
) — Dataclass types to use for argument parsing. - **kwargs — Additional keyword arguments passed to the transformers.HfArgumentParser constructor.
A subclass of transformers.HfArgumentParser designed for parsing command-line arguments with dataclass-backed configurations, while also supporting configuration file loading and environment variable management.
# main.py
import os
from dataclasses import dataclass
from trl import TrlParser
@dataclass
class MyArguments:
arg1: int
arg2: str = "alpha"
parser = TrlParser(dataclass_types=[MyArguments])
training_args = parser.parse_args_and_config()
print(training_args, os.environ.get("VAR1"))
$ python main.py --config config.yaml
(MyArguments(arg1=23, arg2='alpha'),) value1
$ python main.py --arg1 5 --arg2 beta
(MyArguments(arg1=5, arg2='beta'),) None
parse_args_and_config
< source >( args: typing.Optional[typing.Iterable[str]] = None return_remaining_strings: bool = False )
Parse command-line args and config file into instances of the specified dataclass types.
This method wraps transformers.HfArgumentParser.parse_args_into_dataclasses and also parses the config file
specified with the --config
flag. The config file (in YAML format) provides argument values that replace the
default values in the dataclasses. Command line arguments can override values set by the config file. The
method also sets any environment variables specified in the env
field of the config file.
parse_args_into_dataclasses
< source >( args = None return_remaining_strings = False look_for_args_file = True args_filename = None args_file_flag = None ) → Tuple consisting of
Parameters
- args — List of strings to parse. The default is taken from sys.argv. (same as argparse.ArgumentParser)
- return_remaining_strings — If true, also return a list of remaining argument strings.
- look_for_args_file — If true, will look for a “.args” file with the same base name as the entry point script for this process, and will append its potential content to the command line args.
- args_filename — If not None, will uses this file instead of the “.args” file specified in the previous argument.
- args_file_flag — If not None, will look for a file in the command-line args specified with this flag. The flag can be specified multiple times and precedence is determined by the order (last one wins).
Returns
Tuple consisting of
- the dataclass instances in the same order as they were passed to the initializer.abspath
- if applicable, an additional namespace for more (non-dataclass backed) arguments added to the parser after initialization.
- The potential list of remaining argument strings. (same as argparse.ArgumentParser.parse_known_args)
Parse command-line args into instances of the specified dataclass types.
This relies on argparse’s ArgumentParser.parse_known_args
. See the doc at:
docs.python.org/3.7/library/argparse.html#argparse.ArgumentParser.parse_args
Overrides the parser’s default values with those provided via keyword arguments.
Any argument with an updated default will also be marked as not required if it was previously required.
Returns a list of strings that were not consumed by the parser.