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

#549 Feature/infra 1481 call integration tests

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/infra-1481_call_integration_tests
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
  1. import logging
  2. import os
  3. import sys
  4. import time
  5. from typing import Union
  6. class AutoLoggerConfig:
  7. """
  8. A Class for the Automated Logging Config
  9. """
  10. FILE_LOGGING_LEVEL = os.environ.get("LOG_LEVEL", "DEBUG").upper()
  11. CONSOLE_LOGGING_LEVEL = os.environ.get("CONSOLE_LOG_LEVEL", "INFO").upper()
  12. filename: Union[str, None]
  13. def __init__(self):
  14. self.filename = None
  15. def _setup_default_logging(self, log_level: str = None) -> None:
  16. """
  17. Setup default logging configuration. Usually happens when app starts, and we don't have
  18. experiment dir yet.
  19. The default log directory will be `~/sg_logs`
  20. :param log_level: The default log level to use. If None, uses LOG_LEVEL and CONSOLE_LOG_LEVEL environment vars.
  21. :return: None
  22. """
  23. # There is no _easy_ way to log all events to a single file, when using DDP or DataLoader with num_workers > 1
  24. # on Windows platform. In both these cases a multiple processes will be spawned and multiple logs may be created.
  25. # Therefore the log file will have the parent PID to being able to discriminate the logs corresponding to a single run.
  26. timestamp = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
  27. self._setup_logging(
  28. filename=os.path.expanduser(f"~/sg_logs/sg_logs_{os.getppid()}_{timestamp}.log"),
  29. copy_already_logged_messages=False,
  30. filemode="w",
  31. log_level=log_level,
  32. )
  33. def _setup_logging(self, filename: str, copy_already_logged_messages: bool, filemode: str = "a", log_level: str = None) -> None:
  34. """
  35. Sets the logging configuration to store messages to specific file
  36. :param filename: Output log file
  37. :param filemode: Open mode for file
  38. :param copy_already_logged_messages: Controls whether messages from previous log configuration should be copied
  39. to new place. This is helpful to transfer diagnostic messages (from the app start) to experiment dir.
  40. :param log_level: The default log level to use. If None, uses LOG_LEVEL and CONSOLE_LOG_LEVEL environment vars.
  41. :return:
  42. """
  43. os.makedirs(os.path.dirname(filename), exist_ok=True)
  44. if copy_already_logged_messages and self.filename is not None and os.path.exists(self.filename):
  45. with open(self.filename, "r", encoding="utf-8") as src:
  46. with open(filename, "w") as dst:
  47. dst.write(src.read())
  48. file_logging_level = log_level or self.FILE_LOGGING_LEVEL
  49. console_logging_level = log_level or self.CONSOLE_LOGGING_LEVEL
  50. cur_version = sys.version_info
  51. python_38 = (3, 8)
  52. python_39 = (3, 9)
  53. manager = logging.getLogger("").manager
  54. extra_kwargs = {}
  55. if cur_version >= python_38:
  56. extra_kwargs = dict(
  57. force=True,
  58. )
  59. else:
  60. # If the logging does not support force=True, we should manually delete handlers
  61. del manager.root.handlers[:]
  62. if cur_version >= python_39:
  63. extra_kwargs["encoding"] = "utf-8"
  64. logging.basicConfig(
  65. filename=filename,
  66. filemode=filemode,
  67. format="%(asctime)s %(levelname)s - %(name)s - %(message)s",
  68. datefmt="[%Y-%m-%d %H:%M:%S]",
  69. level=file_logging_level,
  70. **extra_kwargs,
  71. )
  72. # Add console handler
  73. console_handler = logging.StreamHandler()
  74. console_handler.setLevel(console_logging_level)
  75. console_handler.setFormatter(
  76. logging.Formatter(
  77. "%(asctime)s %(levelname)s - %(filename)s - %(message)s",
  78. datefmt="[%Y-%m-%d %H:%M:%S]",
  79. )
  80. )
  81. manager.root.handlers.append(console_handler)
  82. self.filename = filename
  83. @classmethod
  84. def get_instance(cls):
  85. global _super_gradients_logger_config
  86. if _super_gradients_logger_config is None:
  87. _super_gradients_logger_config = cls()
  88. _super_gradients_logger_config._setup_default_logging()
  89. return _super_gradients_logger_config
  90. @classmethod
  91. def get_log_file_path(cls) -> str:
  92. """
  93. Return the current log file used to store log messages
  94. :return: Full path to log file
  95. """
  96. self = cls.get_instance()
  97. return self.filename
  98. @classmethod
  99. def setup_logging(cls, filename: str, copy_already_logged_messages: bool, filemode: str = "a", log_level: str = None) -> None:
  100. self = cls.get_instance()
  101. self._setup_logging(filename, copy_already_logged_messages, filemode, log_level)
  102. _super_gradients_logger_config = None
Discard
Tip!

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