@@ -24,6 +24,7 @@ def any_lightning_module_function_or_hook(...):
24
24
25
25
"""
26
26
from argparse import Namespace
27
+ from os import environ
27
28
from pathlib import Path
28
29
from typing import Any , Dict , Optional , Union
29
30
@@ -58,9 +59,9 @@ class TrainsLogger(LightningLoggerBase):
58
59
sent along side the task scalars. Defaults to True.
59
60
60
61
Examples:
61
- >>> logger = TrainsLogger("lightning_log", "my-test", output_uri=".") # doctest: +ELLIPSIS
62
+ >>> logger = TrainsLogger("lightning_log", "my-lightning- test", output_uri=".") # doctest: +ELLIPSIS
62
63
TRAINS Task: ...
63
- TRAINS results page: https://demoapp.trains.allegro.ai/.../log
64
+ TRAINS results page: ...
64
65
>>> logger.log_metrics({"val_loss": 1.23}, step=0)
65
66
>>> logger.log_text("sample test")
66
67
sample test
@@ -69,7 +70,7 @@ class TrainsLogger(LightningLoggerBase):
69
70
>>> logger.log_image("passed", "Image 1", np.random.randint(0, 255, (200, 150, 3), dtype=np.uint8))
70
71
"""
71
72
72
- _bypass = False
73
+ _bypass = None
73
74
74
75
def __init__ (
75
76
self ,
@@ -83,8 +84,24 @@ def __init__(
83
84
auto_resource_monitoring : bool = True
84
85
) -> None :
85
86
super ().__init__ ()
86
- if self ._bypass :
87
+ if self .bypass_mode () :
87
88
self ._trains = None
89
+ print ('TRAINS Task: running in bypass mode' )
90
+ print ('TRAINS results page: disabled' )
91
+
92
+ class _TaskStub (object ):
93
+ def __call__ (self , * args , ** kwargs ):
94
+ return self
95
+
96
+ def __getattr__ (self , attr ):
97
+ if attr in ('name' , 'id' ):
98
+ return ''
99
+ return self
100
+
101
+ def __setattr__ (self , attr , val ):
102
+ pass
103
+
104
+ self ._trains = _TaskStub ()
88
105
else :
89
106
self ._trains = Task .init (
90
107
project_name = project_name ,
@@ -114,8 +131,9 @@ def id(self) -> Union[str, None]:
114
131
"""
115
132
ID is a uuid (string) representing this specific experiment in the entire system.
116
133
"""
117
- if self . _bypass or not self ._trains :
134
+ if not self ._trains :
118
135
return None
136
+
119
137
return self ._trains .id
120
138
121
139
@rank_zero_only
@@ -126,8 +144,8 @@ def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
126
144
params:
127
145
The hyperparameters that passed through the model.
128
146
"""
129
- if self . _bypass or not self ._trains :
130
- return None
147
+ if not self ._trains :
148
+ return
131
149
if not params :
132
150
return
133
151
@@ -147,8 +165,8 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) ->
147
165
then the elements will be logged as "title" and "series" respectively.
148
166
step: Step number at which the metrics should be recorded. Defaults to None.
149
167
"""
150
- if self . _bypass or not self ._trains :
151
- return None
168
+ if not self ._trains :
169
+ return
152
170
153
171
if not step :
154
172
step = self ._trains .get_last_iteration ()
@@ -179,8 +197,8 @@ def log_metric(self, title: str, series: str, value: float, step: Optional[int]
179
197
value: The value to log.
180
198
step: Step number at which the metrics should be recorded. Defaults to None.
181
199
"""
182
- if self . _bypass or not self ._trains :
183
- return None
200
+ if not self ._trains :
201
+ return
184
202
185
203
if not step :
186
204
step = self ._trains .get_last_iteration ()
@@ -197,8 +215,12 @@ def log_text(self, text: str) -> None:
197
215
Args:
198
216
text: The value of the log (data-point).
199
217
"""
200
- if self ._bypass or not self ._trains :
201
- return None
218
+ if self .bypass_mode ():
219
+ print (text )
220
+ return
221
+
222
+ if not self ._trains :
223
+ return
202
224
203
225
self ._trains .get_logger ().report_text (text )
204
226
@@ -222,8 +244,8 @@ def log_image(
222
244
step:
223
245
Step number at which the metrics should be recorded. Defaults to None.
224
246
"""
225
- if self . _bypass or not self ._trains :
226
- return None
247
+ if not self ._trains :
248
+ return
227
249
228
250
if not step :
229
251
step = self ._trains .get_last_iteration ()
@@ -265,8 +287,8 @@ def log_artifact(
265
287
If True local artifact will be deleted (only applies if artifact_object is a
266
288
local file). Defaults to False.
267
289
"""
268
- if self . _bypass or not self ._trains :
269
- return None
290
+ if not self ._trains :
291
+ return
270
292
271
293
self ._trains .upload_artifact (
272
294
name = name , artifact_object = artifact , metadata = metadata ,
@@ -278,8 +300,9 @@ def save(self) -> None:
278
300
279
301
@rank_zero_only
280
302
def finalize (self , status : str = None ) -> None :
281
- if self ._bypass or not self ._trains :
282
- return None
303
+ if self .bypass_mode () or not self ._trains :
304
+ return
305
+
283
306
self ._trains .close ()
284
307
self ._trains = None
285
308
@@ -288,14 +311,16 @@ def name(self) -> Union[str, None]:
288
311
"""
289
312
Name is a human readable non-unique name (str) of the experiment.
290
313
"""
291
- if self . _bypass or not self ._trains :
314
+ if not self ._trains :
292
315
return ''
316
+
293
317
return self ._trains .name
294
318
295
319
@property
296
320
def version (self ) -> Union [str , None ]:
297
- if self . _bypass or not self ._trains :
321
+ if not self ._trains :
298
322
return None
323
+
299
324
return self ._trains .id
300
325
301
326
@classmethod
@@ -327,9 +352,21 @@ def set_bypass_mode(cls, bypass: bool) -> None:
327
352
"""
328
353
cls ._bypass = bypass
329
354
355
+ @classmethod
356
+ def bypass_mode (cls ) -> bool :
357
+ """
358
+ bypass_mode returns the bypass mode state.
359
+ Notice GITHUB_ACTIONS env will automatically set bypass_mode to True
360
+ unless overridden specifically with set_bypass_mode(False)
361
+
362
+ :return: If True, all outside communication is skipped
363
+ """
364
+ return cls ._bypass if cls ._bypass is not None else bool (environ .get ('CI' ))
365
+
330
366
def __getstate__ (self ) -> Union [str , None ]:
331
- if self ._bypass or not self ._trains :
367
+ if self .bypass_mode () or not self ._trains :
332
368
return ''
369
+
333
370
return self ._trains .id
334
371
335
372
def __setstate__ (self , state : str ) -> None :
0 commit comments