Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#609 Ci fix

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/infra-000_ci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  1. import os
  2. from typing import Union, Any
  3. import numpy as np
  4. from PIL import Image
  5. import matplotlib.pyplot as plt
  6. import torch
  7. from super_gradients.common.abstractions.abstract_logger import get_logger
  8. from super_gradients.common.sg_loggers.base_sg_logger import BaseSGLogger
  9. from super_gradients.common.environment.ddp_utils import multi_process_safe
  10. logger = get_logger(__name__)
  11. try:
  12. from clearml import Task
  13. _imported_clear_ml_failure = None
  14. except (ImportError, NameError, ModuleNotFoundError) as import_err:
  15. logger.debug("Failed to import clearml")
  16. _imported_clear_ml_failure = import_err
  17. class ClearMLSGLogger(BaseSGLogger):
  18. def __init__(
  19. self,
  20. project_name: str,
  21. experiment_name: str,
  22. storage_location: str,
  23. resumed: bool,
  24. training_params: dict,
  25. checkpoints_dir_path: str,
  26. tb_files_user_prompt: bool = False,
  27. launch_tensorboard: bool = False,
  28. tensorboard_port: int = None,
  29. save_checkpoints_remote: bool = True,
  30. save_tensorboard_remote: bool = True,
  31. save_logs_remote: bool = True,
  32. monitor_system: bool = None,
  33. ):
  34. """
  35. :param project_name: ClearML project name that can include many experiments
  36. :param experiment_name: Used for logging and loading purposes
  37. :param storage_location: If set to 's3' (i.e. s3://my-bucket) saves the Checkpoints in AWS S3 otherwise saves the Checkpoints Locally
  38. :param resumed: if true, then old tensorboard files will *not* be deleted when tb_files_user_prompt=True
  39. :param training_params: training_params for the experiment.
  40. :param checkpoints_dir_path: Local root directory path where all experiment logging directories will
  41. reside.
  42. :param tb_files_user_prompt: Asks user for Tensorboard deletion prompt.
  43. :param launch_tensorboard: Whether to launch a TensorBoard process.
  44. :param tensorboard_port: Specific port number for the tensorboard to use when launched (when set to None, some free port
  45. number will be used
  46. :param save_checkpoints_remote: Saves checkpoints in ClearML server.
  47. :param save_tensorboard_remote: Saves tensorboard in ClearML server.
  48. :param save_logs_remote: Saves log files in ClearML server.
  49. """
  50. if monitor_system is not None:
  51. logger.warning("monitor_system not available on ClearMLSGLogger. To remove this warning, please don't set monitor_system in your logger parameters")
  52. self.s3_location_available = storage_location.startswith("s3")
  53. super().__init__(
  54. project_name=project_name,
  55. experiment_name=experiment_name,
  56. storage_location=storage_location,
  57. resumed=resumed,
  58. training_params=training_params,
  59. checkpoints_dir_path=checkpoints_dir_path,
  60. tb_files_user_prompt=tb_files_user_prompt,
  61. launch_tensorboard=launch_tensorboard,
  62. tensorboard_port=tensorboard_port,
  63. save_checkpoints_remote=self.s3_location_available,
  64. save_tensorboard_remote=self.s3_location_available,
  65. save_logs_remote=self.s3_location_available,
  66. monitor_system=False,
  67. )
  68. if _imported_clear_ml_failure is not None:
  69. raise _imported_clear_ml_failure
  70. self.setup(project_name, experiment_name)
  71. self.save_checkpoints = save_checkpoints_remote
  72. self.save_tensorboard = save_tensorboard_remote
  73. self.save_logs = save_logs_remote
  74. @multi_process_safe
  75. def setup(self, project_name, experiment_name):
  76. from multiprocessing.process import BaseProcess
  77. # Prevent clearml modifying os.fork and BaseProcess.run, which can cause a DataLoader to crash (if num_worker > 0)
  78. # Issue opened here: https://github.com/allegroai/clearml/issues/790
  79. default_fork, default_run = os.fork, BaseProcess.run
  80. self.task = Task.init(
  81. project_name=project_name, # project name of at least 3 characters
  82. task_name=experiment_name, # task name of at least 3 characters
  83. continue_last_task=0, # This prevents clear_ml to add an offset to the epoch
  84. auto_connect_arg_parser=False,
  85. auto_connect_frameworks=False,
  86. auto_resource_monitoring=False,
  87. auto_connect_streams=True,
  88. )
  89. os.fork, BaseProcess.run = default_fork, default_run
  90. self.clearml_logger = self.task.get_logger()
  91. @multi_process_safe
  92. def add_config(self, tag: str, config: dict):
  93. super(ClearMLSGLogger, self).add_config(tag=tag, config=config)
  94. self.task.connect(config)
  95. def __add_scalar(self, tag: str, scalar_value: float, global_step: int):
  96. self.clearml_logger.report_scalar(title=tag, series=tag, value=scalar_value, iteration=global_step)
  97. @multi_process_safe
  98. def add_scalar(self, tag: str, scalar_value: float, global_step: int = 0):
  99. super(ClearMLSGLogger, self).add_scalar(tag=tag, scalar_value=scalar_value, global_step=global_step)
  100. self.__add_scalar(tag=tag, scalar_value=scalar_value, global_step=global_step)
  101. @multi_process_safe
  102. def add_scalars(self, tag_scalar_dict: dict, global_step: int = 0):
  103. super(ClearMLSGLogger, self).add_scalars(tag_scalar_dict=tag_scalar_dict, global_step=global_step)
  104. for tag, scalar_value in tag_scalar_dict.items():
  105. self.__add_scalar(tag=tag, scalar_value=scalar_value, global_step=global_step)
  106. def __add_image(
  107. self,
  108. tag: str,
  109. image: Union[torch.Tensor, np.array, Image.Image],
  110. global_step: int,
  111. ):
  112. if isinstance(image, torch.Tensor):
  113. image = image.cpu().detach().numpy()
  114. if image.shape[0] < 5:
  115. image = image.transpose([1, 2, 0])
  116. self.clearml_logger.report_image(
  117. title=tag,
  118. series=tag,
  119. image=image,
  120. iteration=global_step,
  121. max_image_history=-1,
  122. )
  123. @multi_process_safe
  124. def add_image(
  125. self,
  126. tag: str,
  127. image: Union[torch.Tensor, np.array, Image.Image],
  128. data_format="CHW",
  129. global_step: int = 0,
  130. ):
  131. super(ClearMLSGLogger, self).add_image(tag=tag, image=image, data_format=data_format, global_step=global_step)
  132. self.__add_image(tag, image, global_step)
  133. @multi_process_safe
  134. def add_images(
  135. self,
  136. tag: str,
  137. images: Union[torch.Tensor, np.array],
  138. data_format="NCHW",
  139. global_step: int = 0,
  140. ):
  141. super(ClearMLSGLogger, self).add_images(tag=tag, images=images, data_format=data_format, global_step=global_step)
  142. for image in images:
  143. self.__add_image(tag, image, global_step)
  144. @multi_process_safe
  145. def add_video(self, tag: str, video: Union[torch.Tensor, np.array], global_step: int = 0):
  146. super().add_video(tag, video, global_step)
  147. logger.warning("ClearMLSGLogger does not support uploading video to clearML from a tensor/array.")
  148. @multi_process_safe
  149. def add_histogram(
  150. self,
  151. tag: str,
  152. values: Union[torch.Tensor, np.array],
  153. bins: str,
  154. global_step: int = 0,
  155. ):
  156. super().add_histogram(tag, values, bins, global_step)
  157. self.clearml_logger.report_histogram(title=tag, series=tag, iteration=global_step, values=values)
  158. @multi_process_safe
  159. def add_text(self, tag: str, text_string: str, global_step: int = 0):
  160. super().add_text(tag, text_string, global_step)
  161. self.clearml_logger.report_text(text_string)
  162. @multi_process_safe
  163. def add_figure(self, tag: str, figure: plt.figure, global_step: int = 0):
  164. super().add_figure(tag, figure, global_step)
  165. name = f"tmp_{tag}.png"
  166. path = os.path.join(self._local_dir, name)
  167. figure.savefig(path)
  168. self.task.upload_artifact(name=name, artifact_object=path)
  169. os.remove(path)
  170. @multi_process_safe
  171. def close(self):
  172. super().close()
  173. self.task.close()
  174. @multi_process_safe
  175. def add_file(self, file_name: str = None):
  176. super().add_file(file_name)
  177. self.task.upload_artifact(name=file_name, artifact_object=os.path.join(self._local_dir, file_name))
  178. @multi_process_safe
  179. def upload(self):
  180. super().upload()
  181. if self.save_tensorboard:
  182. name = self._get_tensorboard_file_name().split("/")[-1]
  183. self.task.upload_artifact(name=name, artifact_object=self._get_tensorboard_file_name())
  184. if self.save_logs:
  185. name = self.log_file_path.split("/")[-1]
  186. self.task.upload_artifact(name=name, artifact_object=self.log_file_path)
  187. @multi_process_safe
  188. def add_checkpoint(self, tag: str, state_dict: dict, global_step: int = 0):
  189. name = f"ckpt_{global_step}.pth" if tag is None else tag
  190. if not name.endswith(".pth"):
  191. name += ".pth"
  192. path = os.path.join(self._local_dir, name)
  193. torch.save(state_dict, path)
  194. if self.save_checkpoints:
  195. if self.s3_location_available:
  196. self.model_checkpoints_data_interface.save_remote_checkpoints_file(self.experiment_name, self._local_dir, name)
  197. self.task.upload_artifact(name=name, artifact_object=path)
  198. def _get_tensorboard_file_name(self):
  199. try:
  200. tb_file_path = self.tensorboard_writer.file_writer.event_writer._file_name
  201. except RuntimeError:
  202. logger.warning("tensorboard file could not be located for ")
  203. return None
  204. return tb_file_path
  205. def add(self, tag: str, obj: Any, global_step: int = None):
  206. pass
Discard
Tip!

Press p or to see the previous file or, n or to see the next file