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

#869 Add DagsHub Logger to Super Gradients

Merged
Ghost merged 1 commits into Deci-AI:master from timho102003:dagshub_logger
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
  1. from typing import Union, Mapping, Dict
  2. from super_gradients.common.exceptions.factory_exceptions import UnknownTypeException
  3. from super_gradients.training.utils.utils import fuzzy_str, fuzzy_keys, get_fuzzy_mapping_param
  4. class AbstractFactory:
  5. """
  6. An abstract factory to generate an object from a string, a dictionary or a list
  7. """
  8. def get(self, conf: Union[str, dict, list]):
  9. """
  10. Get an instantiated object.
  11. :param conf: a configuration
  12. if string - assumed to be a type name (not the real name, but a name defined in the Factory)
  13. if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)
  14. if list - assumed to be a list of the two options above
  15. If provided value is not one of the three above, the value will be returned as is
  16. """
  17. raise NotImplementedError
  18. class BaseFactory(AbstractFactory):
  19. """
  20. The basic factory fo a *single* object generation.
  21. """
  22. def __init__(self, type_dict: Dict[str, type]):
  23. """
  24. :param type_dict: a dictionary mapping a name to a type
  25. """
  26. self.type_dict = type_dict
  27. def get(self, conf: Union[str, dict]):
  28. """
  29. Get an instantiated object.
  30. :param conf: a configuration
  31. if string - assumed to be a type name (not the real name, but a name defined in the Factory)
  32. if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)
  33. If provided value is not one of the three above, the value will be returned as is
  34. """
  35. if isinstance(conf, str):
  36. if conf in self.type_dict:
  37. return self.type_dict[conf]()
  38. elif fuzzy_str(conf) in fuzzy_keys(self.type_dict):
  39. return get_fuzzy_mapping_param(conf, self.type_dict)()
  40. else:
  41. raise UnknownTypeException(conf, list(self.type_dict.keys()))
  42. elif isinstance(conf, Mapping):
  43. if len(conf.keys()) > 1:
  44. raise RuntimeError(
  45. "Malformed object definition in configuration. Expecting either a string of object type or a single entry dictionary"
  46. "{type_name(str): {parameters...}}."
  47. f"received: {conf}"
  48. )
  49. _type = list(conf.keys())[0] # THE TYPE NAME
  50. _params = list(conf.values())[0] # A DICT CONTAINING THE PARAMETERS FOR INIT
  51. if _type in self.type_dict:
  52. return self.type_dict[_type](**_params)
  53. elif fuzzy_str(_type) in fuzzy_keys(self.type_dict):
  54. return get_fuzzy_mapping_param(_type, self.type_dict)(**_params)
  55. else:
  56. raise UnknownTypeException(_type, list(self.type_dict.keys()))
  57. else:
  58. return conf
Discard
Tip!

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