config.py 7.9 KB

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