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

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

Comments

Loading...