MCPcopy Create free account
hub / github.com/AMAP-ML/EMF / parse_args_and_config

Method parse_args_and_config

trl/trl/scripts/utils.py:183–232  ·  view source on GitHub ↗

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) prov

(
        self,
        args: Optional[Iterable[str]] = None,
        return_remaining_strings: bool = False,
        fail_with_unknown_args: bool = True,
    )

Source from the content-addressed store, hash-verified

181 super().__init__(dataclass_types=dataclass_types, **kwargs)
182
183 def parse_args_and_config(
184 self,
185 args: Optional[Iterable[str]] = None,
186 return_remaining_strings: bool = False,
187 fail_with_unknown_args: bool = True,
188 ) -> tuple[DataClass, ...]:
189 """
190 Parse command-line args and config file into instances of the specified dataclass types.
191
192 This method wraps [`transformers.HfArgumentParser.parse_args_into_dataclasses`] and also parses the config file
193 specified with the `--config` flag. The config file (in YAML format) provides argument values that replace the
194 default values in the dataclasses. Command line arguments can override values set by the config file. The
195 method also sets any environment variables specified in the `env` field of the config file.
196 """
197 args = list(args) if args is not None else sys.argv[1:]
198 if "--config" in args:
199 # Get the config file path from
200 config_index = args.index("--config")
201 args.pop(config_index) # remove the --config flag
202 config_path = args.pop(config_index) # get the path to the config file
203 with open(config_path) as yaml_file:
204 config = yaml.safe_load(yaml_file)
205
206 # Set the environment variables specified in the config file
207 if "env" in config:
208 env_vars = config.pop("env", {})
209 if not isinstance(env_vars, dict):
210 raise ValueError("`env` field should be a dict in the YAML file.")
211 for key, value in env_vars.items():
212 os.environ[key] = str(value)
213
214 # Set the defaults from the config values
215 config_remaining_strings = self.set_defaults_with_config(**config)
216 else:
217 config_remaining_strings = []
218
219 # Parse the arguments from the command line
220 output = self.parse_args_into_dataclasses(args=args, return_remaining_strings=return_remaining_strings)
221
222 # Merge remaining strings from the config file with the remaining strings from the command line
223 if return_remaining_strings:
224 args_remaining_strings = output[-1]
225 return output[:-1] + (config_remaining_strings + args_remaining_strings,)
226 elif fail_with_unknown_args and config_remaining_strings:
227 raise ValueError(
228 f"Unknown arguments from config file: {config_remaining_strings}. Please remove them, add them to the "
229 "dataclass, or set `fail_with_unknown_args=False`."
230 )
231 else:
232 return output
233
234 def set_defaults_with_config(self, **kwargs) -> list[str]:
235 """

Callers 15

mainFunction · 0.95
mainFunction · 0.95
grpo_vlm.pyFile · 0.80
dpo_vlm.pyFile · 0.80
sft_vlm.pyFile · 0.80
sft_video_llm.pyFile · 0.80
dpo_online.pyFile · 0.80
mpo_vlm.pyFile · 0.80
nash_md.pyFile · 0.80
gkd.pyFile · 0.80
xpo.pyFile · 0.80

Calls 1

Tested by

no test coverage detected