mindformers.tools.register.MindFormerRegister
- class mindformers.tools.register.MindFormerRegister[source]
The registration interface for MindFormers, provides methods for registering and obtaining the interface.
Examples
>>> from mindformers.tools import MindFormerModuleType, MindFormerRegister >>> >>> >>> # Using decorator to register the class >>> @MindFormerRegister.register(MindFormerModuleType.CONFIG) >>> class MyConfig: ... def __init__(self, param): ... self.param = param >>> >>> >>> # Using method to register the class >>> MindFormerRegister.register_cls(cls=MyConfig, module_type=MindFormerRegister) >>> >>> print(MindFormerRegister.is_exist(module_type=MindFormerModuleType.CONFIG, class_name="MyConfig")) True >>> cls = MindFormerRegister.get_cls(module_type=MindFormerModuleType.CONFIG, class_name="MyConfig") >>> print(cls.__name__) MyConfig >>> instance = MindFormerRegister.get_instance_from_cfg(cfg={'type': 'MyConfig', 'param': 0}, ... module_type=MindFormerModuleType.CONFIG) >>> print(instance.__class__.__name__) MyConfig >>> print(instance.param) 0 >>> instance = MindFormerRegister.get_instance(module_type=MindFormerModuleType.CONFIG, ... class_name="MyConfig", ... param=0) >>> print(instance.__class__.__name__) MyConfig >>> print(instance.param) 0
- classmethod get_cls(module_type, class_name=None)[source]
Get the class from the registry.
- Parameters
module_type (MindFormerModuleType) – Module type name of MindFormers.
class_name (str, optional) – Class name. Default:
None
.
- Returns
A registered class.
- Raises
ValueError – Can't find class class_name of type module_type in the registry.
ValueError – Can't find type module_type in the registry.
- classmethod get_instance(module_type=MindFormerModuleType.TOOLS, class_name=None, **kwargs)[source]
Gets an instance of the class in the registry.
- Parameters
module_type (MindFormerModuleType, optional) – Module type name of MindFormers. Default:
MindFormerModuleType.TOOLS
.class_name (str, optional) – Class name. Default:
None
.kwargs (Any) – Additional keyword arguments for constructing instances of the class.
- Returns
An instance of the class.
- Raises
ValueError – class_name cannot be
None
.ValueError – Can't find class class_name of type module_type in the registry.
- classmethod get_instance_from_cfg(cfg, module_type=MindFormerModuleType.TOOLS, default_args=None)[source]
Get instances of the class in the registry via configuration.
- Parameters
cfg (dict) – Configuration dictionary. It should contain at least the key "type".
module_type (MindFormerModuleType, optional) – Module type name of MindFormers. Default:
MindFormerModuleType.TOOLS
.default_args (dict, optional) – Default initialization arguments. Default:
None
.
- Returns
An instance of the class.
- Raises
TypeError – cfg must be a configuration.
KeyError – cfg or default_args must contain the key "type".
TypeError – default_args must be a dictionary or
None
.ValueError – Can't find class class_name of type module_type in the registry.
- classmethod is_exist(module_type, class_name=None)[source]
Determines whether the given class name is in the current type group. If class_name is not given, determines if the given class name is in the current registered dictionary.
- Parameters
module_type (MindFormerModuleType) – Module type name of MindFormers.
class_name (str, optional) – Class name. Default:
None
.
- Returns
A boolean value, indicating whether it exists or not.
- classmethod register(module_type=MindFormerModuleType.TOOLS, alias=None)[source]
A decorator that registers the class in the registry.
- Parameters
module_type (MindFormerModuleType, optional) – Module type name of MindFormers. Default:
MindFormerModuleType.TOOLS
.alias (str, optional) – Alias for the class. Default:
None
.
- Returns
Wrapper, decorates the registered class.
- classmethod register_cls(register_class, module_type=MindFormerModuleType.TOOLS, alias=None)[source]
A method that registers a class into the registry.
- Parameters
register_class (type) – The class that need to be registered.
module_type (MindFormerModuleType, optional) – Module type name of MindFormers. Default:
MindFormerModuleType.TOOLS
.alias (str, optional) – Alias for the class. Default:
None
.
- Returns
Class, the registered class itself.