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

env_helpers.py 2.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. import argparse
  2. import os
  3. import sys
  4. from functools import wraps
  5. from super_gradients.common.environment import environment_config
  6. class TerminalColours:
  7. """
  8. Usage: https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python?page=1&tab=votes#tab-top
  9. """
  10. HEADER = '\033[95m'
  11. OKBLUE = '\033[94m'
  12. OKCYAN = '\033[96m'
  13. OKGREEN = '\033[92m'
  14. WARNING = '\033[93m'
  15. FAIL = '\033[91m'
  16. ENDC = '\033[0m'
  17. BOLD = '\033[1m'
  18. UNDERLINE = '\033[4m'
  19. class ColouredTextFormatter:
  20. @staticmethod
  21. def print_coloured_text(text: str, colour: str):
  22. """
  23. Prints a text with colour ascii characters.
  24. """
  25. return print(''.join([colour, text, TerminalColours.ENDC]))
  26. def get_environ_as_type(environment_variable_name: str, default=None, cast_to_type: type = str) -> object:
  27. """
  28. Tries to get an environment variable and cast it into a requested type.
  29. :return: cast_to_type object, or None if failed.
  30. :raises ValueError: If the value could not be casted into type 'cast_to_type'
  31. """
  32. value = os.environ.get(environment_variable_name, default)
  33. if value is not None:
  34. try:
  35. return cast_to_type(value)
  36. except Exception as e:
  37. print(e)
  38. raise ValueError(
  39. f'Failed to cast environment variable {environment_variable_name} to type {cast_to_type}: the value {value} is not a valid {cast_to_type}')
  40. return
  41. def init_trainer():
  42. """
  43. a function to initialize the super_gradients environment. This function should be the first thing to be called
  44. by any code running super_gradients. It resolves conflicts between the different tools, packages and environments used
  45. and prepares the super_gradients environment.
  46. """
  47. parser = argparse.ArgumentParser()
  48. parser.add_argument("--local_rank", type=int, default=-1) # used by DDP
  49. args, _ = parser.parse_known_args()
  50. # remove any flags starting with --local_rank from the argv list
  51. to_remove = list(filter(lambda x: x.startswith('--local_rank'), sys.argv))
  52. if len(to_remove) > 0:
  53. for val in to_remove:
  54. sys.argv.remove(val)
  55. environment_config.DDP_LOCAL_RANK = args.local_rank
  56. def is_distributed() -> bool:
  57. return environment_config.DDP_LOCAL_RANK >= 0
  58. def multi_process_safe(func):
  59. """
  60. A decorator for making sure a function runs only in main process.
  61. If not in DDP mode (local_rank = -1), the function will run.
  62. If in DDP mode, the function will run only in the main process (local_rank = 0)
  63. This works only for functions with no return value
  64. """
  65. def do_nothing(*args, **kwargs):
  66. pass
  67. @wraps(func)
  68. def wrapper(*args, **kwargs):
  69. if environment_config.DDP_LOCAL_RANK <= 0:
  70. return func(*args, **kwargs)
  71. else:
  72. return do_nothing(*args, **kwargs)
  73. return wrapper
Tip!

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

Comments

Loading...