2022-09-20 06:17:21 +00:00
|
|
|
class Registry:
|
|
|
|
# TODO: refactor the registry classes used in colossalai.registry, colossalai.fx and here
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.store = {}
|
|
|
|
|
|
|
|
def register(self, source):
|
|
|
|
|
|
|
|
def wrapper(func):
|
2022-10-25 06:32:01 +00:00
|
|
|
if isinstance(source, (list, tuple)):
|
|
|
|
# support register a list of items for this func
|
|
|
|
for element in source:
|
|
|
|
self.store[element] = func
|
|
|
|
else:
|
|
|
|
self.store[source] = func
|
2022-09-20 06:17:21 +00:00
|
|
|
return func
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def get(self, source):
|
2022-09-28 06:01:36 +00:00
|
|
|
assert source in self.store, f'{source} not found in the {self.name} registry'
|
2022-09-20 06:17:21 +00:00
|
|
|
target = self.store[source]
|
|
|
|
return target
|
|
|
|
|
|
|
|
def has(self, source):
|
|
|
|
return source in self.store
|
|
|
|
|
|
|
|
|
|
|
|
operator_registry = Registry('operator')
|