Skip to content

Factories

ActivationsTypeFactory

Bases: AbstractFactory

This is a special factory for getting a type of the activation function by name. This factory does not instantiate a module, but rather return the type to be instantiated via call method.

Additionally, activation type factory supports already resolved types as input and fall-back to nop if the input is already a type that is subclass of nn.Module. This is done to support the case when the type is already resolved, which is the case when we're using CustomizableDetector.

Source code in src/super_gradients/common/factories/activations_type_factory.py
 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
class ActivationsTypeFactory(AbstractFactory):
    """
    This is a special factory for getting a type of the activation function by name.
    This factory does not instantiate a module, but rather return the type to be instantiated via call method.

    Additionally, activation type factory supports already resolved types as input and fall-back to nop if the input is
    already a type that is subclass of nn.Module. This is done to support the case when the type is already resolved,
    which is the case when we're using CustomizableDetector.
    """

    def get(self, conf: Union[str, Mapping, Type[nn.Module]]) -> Type[nn.Module]:
        """
        Get a type.
           :param conf: a configuration or a subclass of nn.Module (Type, not instance)
           if string - assumed to be a type name (not the real name, but a name defined in the Factory)
           a dictionary is not supported, since the actual instantiation takes place elsewhere

           If provided value is not one of the three above, the value will be returned as is
        """
        if isinstance(conf, str):
            return get_builtin_activation_type(conf)

        if isinstance(conf, Mapping):
            (type_name,) = list(conf.keys())
            type_args = conf[type_name]
            return get_builtin_activation_type(type_name, **type_args)

        if issubclass(conf, nn.Module):
            return conf

        raise RuntimeError(f"Unsupported conf param type {type(conf)}")

get(conf)

Get a type. :param conf: a configuration or a subclass of nn.Module (Type, not instance) if string - assumed to be a type name (not the real name, but a name defined in the Factory) a dictionary is not supported, since the actual instantiation takes place elsewhere

If provided value is not one of the three above, the value will be returned as is

Source code in src/super_gradients/common/factories/activations_type_factory.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def get(self, conf: Union[str, Mapping, Type[nn.Module]]) -> Type[nn.Module]:
    """
    Get a type.
       :param conf: a configuration or a subclass of nn.Module (Type, not instance)
       if string - assumed to be a type name (not the real name, but a name defined in the Factory)
       a dictionary is not supported, since the actual instantiation takes place elsewhere

       If provided value is not one of the three above, the value will be returned as is
    """
    if isinstance(conf, str):
        return get_builtin_activation_type(conf)

    if isinstance(conf, Mapping):
        (type_name,) = list(conf.keys())
        type_args = conf[type_name]
        return get_builtin_activation_type(type_name, **type_args)

    if issubclass(conf, nn.Module):
        return conf

    raise RuntimeError(f"Unsupported conf param type {type(conf)}")

AbstractFactory

An abstract factory to generate an object from a string, a dictionary or a list

Source code in src/super_gradients/common/factories/base_factory.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class AbstractFactory:
    """
    An abstract factory to generate an object from a string, a dictionary or a list
    """

    def get(self, conf: Union[str, dict, list]):
        """
        Get an instantiated object.
            :param conf: a configuration
                if string - assumed to be a type name (not the real name, but a name defined in the Factory)
                if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)
                if list - assumed to be a list of the two options above

                If provided value is not one of the three above, the value will be returned as is
        """
        raise NotImplementedError

get(conf)

Get an instantiated object. :param conf: a configuration if string - assumed to be a type name (not the real name, but a name defined in the Factory) if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict) if list - assumed to be a list of the two options above

    If provided value is not one of the three above, the value will be returned as is
Source code in src/super_gradients/common/factories/base_factory.py
13
14
15
16
17
18
19
20
21
22
23
def get(self, conf: Union[str, dict, list]):
    """
    Get an instantiated object.
        :param conf: a configuration
            if string - assumed to be a type name (not the real name, but a name defined in the Factory)
            if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)
            if list - assumed to be a list of the two options above

            If provided value is not one of the three above, the value will be returned as is
    """
    raise NotImplementedError

BaseFactory

Bases: AbstractFactory

The basic factory fo a single object generation.

Source code in src/super_gradients/common/factories/base_factory.py
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
class BaseFactory(AbstractFactory):
    """
    The basic factory fo a *single* object generation.
    """

    def __init__(self, type_dict: Dict[str, type]):
        """
        :param type_dict: a dictionary mapping a name to a type
        """
        self.type_dict = type_dict

    def get(self, conf: Union[str, dict]):
        """
        Get an instantiated object.
           :param conf: a configuration
           if string - assumed to be a type name (not the real name, but a name defined in the Factory)
           if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)

           If provided value is not one of the three above, the value will be returned as is
        """
        if isinstance(conf, str):
            warn_if_deprecated(name=conf, registry=self.type_dict)
            if conf in self.type_dict:
                return self.type_dict[conf]()
            elif fuzzy_str(conf) in fuzzy_keys(self.type_dict):
                return get_fuzzy_mapping_param(conf, self.type_dict)()
            else:
                raise UnknownTypeException(conf, list(self.type_dict.keys()))
        elif isinstance(conf, Mapping):
            if len(conf.keys()) > 1:
                raise RuntimeError(
                    "Malformed object definition in configuration. Expecting either a string of object type or a single entry dictionary"
                    "{type_name(str): {parameters...}}."
                    f"received: {conf}"
                )

            _type = list(conf.keys())[0]  # THE TYPE NAME
            _params = list(conf.values())[0]  # A DICT CONTAINING THE PARAMETERS FOR INIT
            if _type in self.type_dict:
                warn_if_deprecated(name=_type, registry=self.type_dict)
                return self.type_dict[_type](**_params)
            elif fuzzy_str(_type) in fuzzy_keys(self.type_dict):
                return get_fuzzy_mapping_param(_type, self.type_dict)(**_params)
            else:
                raise UnknownTypeException(_type, list(self.type_dict.keys()))
        else:
            return conf

__init__(type_dict)

Parameters:

Name Type Description Default
type_dict Dict[str, type]

a dictionary mapping a name to a type

required
Source code in src/super_gradients/common/factories/base_factory.py
31
32
33
34
35
def __init__(self, type_dict: Dict[str, type]):
    """
    :param type_dict: a dictionary mapping a name to a type
    """
    self.type_dict = type_dict

get(conf)

Get an instantiated object. :param conf: a configuration if string - assumed to be a type name (not the real name, but a name defined in the Factory) if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)

If provided value is not one of the three above, the value will be returned as is

Source code in src/super_gradients/common/factories/base_factory.py
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
def get(self, conf: Union[str, dict]):
    """
    Get an instantiated object.
       :param conf: a configuration
       if string - assumed to be a type name (not the real name, but a name defined in the Factory)
       if dictionary - assumed to be {type_name(str): {parameters...}} (single item in dict)

       If provided value is not one of the three above, the value will be returned as is
    """
    if isinstance(conf, str):
        warn_if_deprecated(name=conf, registry=self.type_dict)
        if conf in self.type_dict:
            return self.type_dict[conf]()
        elif fuzzy_str(conf) in fuzzy_keys(self.type_dict):
            return get_fuzzy_mapping_param(conf, self.type_dict)()
        else:
            raise UnknownTypeException(conf, list(self.type_dict.keys()))
    elif isinstance(conf, Mapping):
        if len(conf.keys()) > 1:
            raise RuntimeError(
                "Malformed object definition in configuration. Expecting either a string of object type or a single entry dictionary"
                "{type_name(str): {parameters...}}."
                f"received: {conf}"
            )

        _type = list(conf.keys())[0]  # THE TYPE NAME
        _params = list(conf.values())[0]  # A DICT CONTAINING THE PARAMETERS FOR INIT
        if _type in self.type_dict:
            warn_if_deprecated(name=_type, registry=self.type_dict)
            return self.type_dict[_type](**_params)
        elif fuzzy_str(_type) in fuzzy_keys(self.type_dict):
            return get_fuzzy_mapping_param(_type, self.type_dict)(**_params)
        else:
            raise UnknownTypeException(_type, list(self.type_dict.keys()))
    else:
        return conf

DetectionModulesFactory

Bases: BaseFactory

Source code in src/super_gradients/common/factories/detection_modules_factory.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class DetectionModulesFactory(BaseFactory):
    def __init__(self):
        super().__init__(ALL_DETECTION_MODULES)

    @staticmethod
    def insert_module_param(conf: Union[str, dict, HpmStruct, DictConfig], name: str, value: Any):
        """
        Assign a new parameter for the module
        :param conf:    a module config, either {type_name(str): {parameters...}} or just type_name(str)
        :param name:    parameter name
        :param value:   parameter value
        :return:        an update config {type_name(str): {name: value, parameters...}}
        """
        if isinstance(conf, str):
            return {conf: {name: value}}

        cls_type = list(conf.keys())[0]
        conf[cls_type][name] = value
        return conf

insert_module_param(conf, name, value) staticmethod

Assign a new parameter for the module

Parameters:

Name Type Description Default
conf Union[str, dict, HpmStruct, DictConfig]

a module config, either {type_name(str): {parameters...}} or just type_name(str)

required
name str

parameter name

required
value Any

parameter value

required

Returns:

Type Description

an update config {type_name(str): {name: value, parameters...}}

Source code in src/super_gradients/common/factories/detection_modules_factory.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@staticmethod
def insert_module_param(conf: Union[str, dict, HpmStruct, DictConfig], name: str, value: Any):
    """
    Assign a new parameter for the module
    :param conf:    a module config, either {type_name(str): {parameters...}} or just type_name(str)
    :param name:    parameter name
    :param value:   parameter value
    :return:        an update config {type_name(str): {name: value, parameters...}}
    """
    if isinstance(conf, str):
        return {conf: {name: value}}

    cls_type = list(conf.keys())[0]
    conf[cls_type][name] = value
    return conf

OptimizersTypeFactory

Bases: TypeFactory

This is a special factory for torch.optim.Optimizer. This factory does not instantiate an object but rather return the type, since optimizer instantiation requires the model to be instantiated first

Source code in src/super_gradients/common/factories/optimizers_type_factory.py
 5
 6
 7
 8
 9
10
11
12
13
class OptimizersTypeFactory(TypeFactory):
    """
    This is a special factory for torch.optim.Optimizer.
    This factory does not instantiate an object but rather return the type, since optimizer instantiation
    requires the model to be instantiated first
    """

    def __init__(self):
        super().__init__(OPTIMIZERS)

TorchDtypeFactory

Bases: TypeFactory

Factory to return PyTorch dtype from configuration string.

Source code in src/super_gradients/common/factories/torch_dtype_factory.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class TorchDtypeFactory(TypeFactory):
    """
    Factory to return PyTorch dtype from configuration string.
    """

    def __init__(self):
        super().__init__(self._get_torch_dtypes())

    def _get_torch_dtypes(self):
        """
        Get a dictionary of PyTorch dtypes.
        """
        return {
            "float32": torch.float32,
            "float64": torch.float64,
            "float16": torch.float16,
            "int8": torch.int8,
            "int16": torch.int16,
            "int32": torch.int32,
            "int64": torch.int64,
            "uint8": torch.uint8,
            "long": torch.long,
        }

TypeFactory

Bases: AbstractFactory

Factory to return class type from configuration string.

Source code in src/super_gradients/common/factories/type_factory.py
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
class TypeFactory(AbstractFactory):
    """
    Factory to return class type from configuration string.
    """

    def __init__(self, type_dict: Dict[str, type]):
        """
        :param type_dict: a dictionary mapping a name to a type
        """
        self.type_dict = type_dict

    @classmethod
    def from_enum_cls(cls, enum_cls: Type[Enum]):
        return cls({entity.value: entity for entity in enum_cls})

    def get(self, conf: Union[str, type]):
        """
        Get a type.
           :param conf: a configuration
           if string - assumed to be a type name (not the real name, but a name defined in the Factory)
           a dictionary is not supported, since the actual instantiation takes place elsewhere

           If provided value is already a class type, the value will be returned as is.
        """
        if isinstance(conf, str) or isinstance(conf, bool):
            if isinstance(conf, str):
                warn_if_deprecated(name=conf, registry=self.type_dict)

            if conf in self.type_dict:
                return self.type_dict[conf]
            elif isinstance(conf, bool) and conf in self.type_dict.values():
                pass
            elif isinstance(conf, str) and get_param(self.type_dict, conf) is not None:
                return get_param(self.type_dict, conf)
            elif isinstance(conf, str) and "." in conf:
                *lib_path, module = conf.split(".")
                lib = ".".join(lib_path)
                try:
                    lib = importlib.import_module(lib)  # Import the required packages
                    class_type = lib.__dict__[module]
                    return class_type
                except Exception as e:
                    err = f"An error occurred while instantiating '{conf}' with exception: \n\t => {e}.\n"
                    raise ValueError(err)
            else:
                raise UnknownTypeException(
                    unknown_type=conf,
                    choices=list(self.type_dict.keys()),
                    message=f"Unknown object type: {conf} in configuration. valid types are: {self.type_dict.keys()} or a class "
                    "type available in the env or in the form 'package_name.sub_package.MyClass'\n",
                )
        else:
            return conf

__init__(type_dict)

Parameters:

Name Type Description Default
type_dict Dict[str, type]

a dictionary mapping a name to a type

required
Source code in src/super_gradients/common/factories/type_factory.py
15
16
17
18
19
def __init__(self, type_dict: Dict[str, type]):
    """
    :param type_dict: a dictionary mapping a name to a type
    """
    self.type_dict = type_dict

get(conf)

Get a type. :param conf: a configuration if string - assumed to be a type name (not the real name, but a name defined in the Factory) a dictionary is not supported, since the actual instantiation takes place elsewhere

If provided value is already a class type, the value will be returned as is.

Source code in src/super_gradients/common/factories/type_factory.py
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
def get(self, conf: Union[str, type]):
    """
    Get a type.
       :param conf: a configuration
       if string - assumed to be a type name (not the real name, but a name defined in the Factory)
       a dictionary is not supported, since the actual instantiation takes place elsewhere

       If provided value is already a class type, the value will be returned as is.
    """
    if isinstance(conf, str) or isinstance(conf, bool):
        if isinstance(conf, str):
            warn_if_deprecated(name=conf, registry=self.type_dict)

        if conf in self.type_dict:
            return self.type_dict[conf]
        elif isinstance(conf, bool) and conf in self.type_dict.values():
            pass
        elif isinstance(conf, str) and get_param(self.type_dict, conf) is not None:
            return get_param(self.type_dict, conf)
        elif isinstance(conf, str) and "." in conf:
            *lib_path, module = conf.split(".")
            lib = ".".join(lib_path)
            try:
                lib = importlib.import_module(lib)  # Import the required packages
                class_type = lib.__dict__[module]
                return class_type
            except Exception as e:
                err = f"An error occurred while instantiating '{conf}' with exception: \n\t => {e}.\n"
                raise ValueError(err)
        else:
            raise UnknownTypeException(
                unknown_type=conf,
                choices=list(self.type_dict.keys()),
                message=f"Unknown object type: {conf} in configuration. valid types are: {self.type_dict.keys()} or a class "
                "type available in the env or in the form 'package_name.sub_package.MyClass'\n",
            )
    else:
        return conf