training_args_seq2seq.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 typing import Optional, Union
  18. from .generation.configuration_utils import GenerationConfig
  19. from .training_args import TrainingArguments
  20. from .utils import add_start_docstrings
  21. logger = logging.getLogger(__name__)
  22. @dataclass
  23. @add_start_docstrings(TrainingArguments.__doc__)
  24. class Seq2SeqTrainingArguments(TrainingArguments):
  25. """
  26. Args:
  27. predict_with_generate (`bool`, *optional*, defaults to `False`):
  28. Whether to use generate to calculate generative metrics (ROUGE, BLEU).
  29. generation_max_length (`int`, *optional*):
  30. The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default to the
  31. `max_length` value of the model configuration.
  32. generation_num_beams (`int`, *optional*):
  33. The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default to the
  34. `num_beams` value of the model configuration.
  35. generation_config (`str` or `Path` or [`~generation.GenerationConfig`], *optional*):
  36. Allows to load a [`~generation.GenerationConfig`] from the `from_pretrained` method. This can be either:
  37. - a string, the *model id* of a pretrained model configuration hosted inside a model repo on
  38. huggingface.co.
  39. - a path to a *directory* containing a configuration file saved using the
  40. [`~GenerationConfig.save_pretrained`] method, e.g., `./my_model_directory/`.
  41. - a [`~generation.GenerationConfig`] object.
  42. """
  43. sortish_sampler: bool = field(default=False, metadata={"help": "Whether to use SortishSampler or not."})
  44. predict_with_generate: bool = field(
  45. default=False, metadata={"help": "Whether to use generate to calculate generative metrics (ROUGE, BLEU)."}
  46. )
  47. generation_max_length: Optional[int] = field(
  48. default=None,
  49. metadata={
  50. "help": (
  51. "The `max_length` to use on each evaluation loop when `predict_with_generate=True`. Will default "
  52. "to the `max_length` value of the model configuration."
  53. )
  54. },
  55. )
  56. generation_num_beams: Optional[int] = field(
  57. default=None,
  58. metadata={
  59. "help": (
  60. "The `num_beams` to use on each evaluation loop when `predict_with_generate=True`. Will default "
  61. "to the `num_beams` value of the model configuration."
  62. )
  63. },
  64. )
  65. generation_config: Optional[Union[str, Path, GenerationConfig]] = field(
  66. default=None,
  67. metadata={
  68. "help": "Model id, file path or url pointing to a GenerationConfig json file, to use during prediction."
  69. },
  70. )
  71. def to_dict(self):
  72. """
  73. Serializes this instance while replace `Enum` by their values and `GenerationConfig` by dictionaries (for JSON
  74. serialization support). It obfuscates the token values by removing their value.
  75. """
  76. # filter out fields that are defined as field(init=False)
  77. d = super().to_dict()
  78. for k, v in d.items():
  79. if isinstance(v, GenerationConfig):
  80. d[k] = v.to_dict()
  81. return d