training_args_seq2seq.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2020 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from dataclasses import dataclass, field
  16. from pathlib import Path
  17. from .generation.configuration_utils import GenerationConfig
  18. from .training_args import TrainingArguments
  19. from .utils import add_start_docstrings
  20. logger = logging.getLogger(__name__)
  21. @dataclass
  22. @add_start_docstrings(TrainingArguments.__doc__)
  23. class Seq2SeqTrainingArguments(TrainingArguments):
  24. """
  25. sortish_sampler (`bool`, *optional*, defaults to `False`):
  26. Whether to use a *sortish sampler* or not. Only possible if the underlying datasets are *Seq2SeqDataset*
  27. for now but will become generally available in the near future.
  28. It sorts the inputs according to lengths in order to minimize the padding size, with a bit of randomness
  29. for the training set.
  30. predict_with_generate (`bool`, *optional*, defaults to `False`):
  31. Whether to use generate to calculate generative metrics (ROUGE, BLEU).
  32. generation_max_length (`int`, *optional*):
  33. The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default to the
  34. `max_length` value of the model configuration.
  35. generation_num_beams (`int`, *optional*):
  36. The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default to the
  37. `num_beams` value of the model configuration.
  38. generation_config (`str` or `Path` or [`~generation.GenerationConfig`], *optional*):
  39. Allows to load a [`~generation.GenerationConfig`] from the `from_pretrained` method. This can be either:
  40. - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
  41. huggingface.co.
  42. - a path to a *directory* containing a configuration file saved using the
  43. [`~GenerationConfig.save_pretrained`] method, e.g., `./my_model_directory/`.
  44. - a [`~generation.GenerationConfig`] object.
  45. """ # fmt: skip # Prevent Ruff from altering the indentation
  46. sortish_sampler: bool = field(default=False, metadata={"help": "Whether to use SortishSampler or not."})
  47. predict_with_generate: bool = field(
  48. default=False, metadata={"help": "Whether to use generate to calculate generative metrics (ROUGE, BLEU)."}
  49. )
  50. generation_max_length: int | None = field(
  51. default=None,
  52. metadata={
  53. "help": (
  54. "The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default "
  55. "to the `max_length` value of the model configuration."
  56. )
  57. },
  58. )
  59. generation_num_beams: int | None = field(
  60. default=None,
  61. metadata={
  62. "help": (
  63. "The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default "
  64. "to the `num_beams` value of the model configuration."
  65. )
  66. },
  67. )
  68. generation_config: str | Path | GenerationConfig | None = field(
  69. default=None,
  70. metadata={
  71. "help": "Model id, file path or url pointing to a GenerationConfig json file, to use during prediction."
  72. },
  73. )
  74. def to_dict(self):
  75. """
  76. Serializes this instance while replace `Enum` by their values and `GenerationConfig` by dictionaries (for JSON
  77. serialization support). It obfuscates the token values by removing their value.
  78. """
  79. # filter out fields that are defined as field(init=False)
  80. d = super().to_dict()
  81. for k, v in d.items():
  82. if isinstance(v, GenerationConfig):
  83. d[k] = v.to_dict()
  84. return d