config.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 os
  15. from urllib.parse import urlparse
  16. import ruamel.yaml
  17. from ...base import BaseConfig
  18. from ....utils.misc import abspath, convert_and_remove_types
  19. class BaseTSConfig(BaseConfig):
  20. """Base TS Config"""
  21. def update(self, dict_like_obj: list):
  22. """update self
  23. Args:
  24. dict_like_obj (dict): dict of pairs(key0.key1.idx.key2=value).
  25. """
  26. from paddlets.utils.config import merge_config_dicts
  27. dict_ = merge_config_dicts(dict_like_obj, self.dict)
  28. self.reset_from_dict(dict_)
  29. def load(self, config_file_path: str):
  30. """load config from yaml file
  31. Args:
  32. config_file_path (str): the path of yaml file.
  33. Raises:
  34. TypeError: the content of yaml file `config_file_path` error.
  35. """
  36. from paddlets.utils.config import parse_from_yaml
  37. dict_ = parse_from_yaml(config_file_path)
  38. if not isinstance(dict_, dict):
  39. raise TypeError
  40. self.reset_from_dict(dict_)
  41. def dump(self, config_file_path: str):
  42. """dump self to yaml file
  43. Args:
  44. config_file_path (str): the path to save self as yaml file.
  45. """
  46. yaml = ruamel.yaml.YAML()
  47. with open(config_file_path, "w", encoding="utf-8") as f:
  48. dict_to_dump = self.dict
  49. dict_to_dump = convert_and_remove_types(dict_to_dump)
  50. yaml.dump(dict_to_dump, f)
  51. def update_epochs(self, epochs: int):
  52. """update epochs setting
  53. Args:
  54. epochs (int): the epochs number value to set
  55. """
  56. self.update({"epoch": epochs})
  57. def update_to_static(self, dy2st: bool):
  58. """update config to set dynamic to static mode
  59. Args:
  60. dy2st (bool): whether or not to use the dynamic to static mode.
  61. """
  62. self.update({"to_static_train": dy2st})
  63. def update_amp(self, amp: str = "O2"):
  64. """update AMP settings
  65. Args:
  66. amp (None | str): the AMP level if it is not None or `OFF`.
  67. """
  68. _cfg = {
  69. "use_amp": True if amp is not None else False,
  70. "amp_level": amp,
  71. }
  72. self.update(_cfg)
  73. def update_weights(self, weight_path: str):
  74. """update weight path
  75. Args:
  76. weight_path (str): the local path of weight file to set.
  77. """
  78. self["weights"] = abspath(weight_path)
  79. def update_learning_rate(self, learning_rate: float):
  80. """update learning rate
  81. Args:
  82. learning_rate (float): the learning rate value to set.
  83. Raises:
  84. RuntimeError: Not able to update learning rate, because no LR scheduler config was found.
  85. """
  86. if "learning_rate" not in self.model["model_cfg"]["optimizer_params"]:
  87. raise RuntimeError(
  88. "Not able to update learning rate, because no LR scheduler config was found."
  89. )
  90. self.model["model_cfg"]["optimizer_params"]["learning_rate"] = float(
  91. learning_rate
  92. )
  93. def update_batch_size(self, batch_size: int, mode: str = "train"):
  94. """update batch size setting
  95. Args:
  96. batch_size (int): the batch size number to set.
  97. mode (str, optional): the mode that to be set batch size, must be one of 'train', 'eval', 'test'.
  98. Defaults to 'train'.
  99. Raises:
  100. ValueError: `mode` error. `train` is supported only.
  101. """
  102. if mode == "train":
  103. self.set_val("batch_size", batch_size)
  104. else:
  105. raise ValueError(
  106. f"Setting `batch_size` in {repr(mode)} mode is not supported."
  107. )
  108. def update_pretrained_weights(self, weight_path: str):
  109. """update pretrained weight path
  110. Args:
  111. weight_path (str): the local path or url of pretrained weight file to set.
  112. Raises:
  113. RuntimeError: "Not able to update pretrained weight path, because no model config was found.
  114. TypeError: `weight_path` error. `str` and `None` are supported only.
  115. """
  116. if "model" not in self:
  117. raise RuntimeError(
  118. "Not able to update pretrained weight path, because no model config was found."
  119. )
  120. if isinstance(weight_path, str):
  121. if urlparse(weight_path).scheme == "":
  122. # If `weight_path` is a string but not URL (with scheme present),
  123. # it will be recognized as a local file path.
  124. weight_path = abspath(weight_path)
  125. else:
  126. if weight_path is not None:
  127. raise TypeError("`weight_path` must be string or None.")
  128. self.model["pretrain"] = weight_path
  129. def update_log_ranks(self, device):
  130. """update log ranks
  131. Args:
  132. device (str): the running device to set
  133. """
  134. # PaddleTS does not support multi-device training currently.
  135. pass
  136. def update_print_mem_info(self, print_mem_info: bool):
  137. """setting print memory info"""
  138. assert isinstance(print_mem_info, bool), "print_mem_info should be a bool"
  139. self.update({"print_mem_info": print_mem_info})
  140. def update_log_interval(self, log_interval: int):
  141. """update log interval(steps)
  142. Args:
  143. log_interval (int): the log interval value to set.
  144. """
  145. self.update({"log_interval": log_interval})
  146. def update_dataset(self, dataset_dir: str, dataset_type: str = None):
  147. """update dataset settings"""
  148. raise NotImplementedError
  149. def update_save_dir(self, save_dir: str):
  150. """update save directory
  151. Args:
  152. save_dir (str): the path to save outputs.
  153. """
  154. self["output_dir"] = abspath(save_dir)
  155. def get_epochs_iters(self) -> int:
  156. """get epochs
  157. Returns:
  158. int: the epochs value, i.e., `Global.epochs` in config.
  159. """
  160. if "epoch" in self:
  161. return self.epoch
  162. else:
  163. # Default iters
  164. return 1000
  165. def get_learning_rate(self) -> float:
  166. """get learning rate
  167. Returns:
  168. float: the learning rate value, i.e., `Optimizer.lr.learning_rate` in config.
  169. """
  170. if "learning_rate" not in self.model["model_cfg"]["optimizer_params"]:
  171. # Default lr
  172. return 0.0001
  173. else:
  174. return self.model["model_cfg"]["optimizer_params"]["learning_rate"]
  175. def get_batch_size(self, mode="train") -> int:
  176. """get batch size
  177. Args:
  178. mode (str, optional): the mode that to be get batch size value, must be one of 'train', 'eval', 'test'.
  179. Defaults to 'train'.
  180. Raises:
  181. ValueError: `mode` error. `train` is supported only.
  182. Returns:
  183. int: the batch size value of `mode`, i.e., `DataLoader.{mode}.sampler.batch_size` in config.
  184. """
  185. if mode == "train":
  186. if "batch_size" in self:
  187. return self.batch_size
  188. else:
  189. # Default batch size
  190. return 16
  191. else:
  192. raise ValueError(
  193. f"Getting `batch_size` in {repr(mode)} mode is not supported."
  194. )