-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f71981f
commit e39f910
Showing
13 changed files
with
1,377 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import copy | ||
|
||
|
||
def singleton(class_): | ||
instances = {} | ||
|
||
def getinstance(*args, **kwargs): | ||
if class_ not in instances: | ||
instances[class_] = class_(*args, **kwargs) | ||
return instances[class_] | ||
return getinstance | ||
|
||
############## | ||
# cfg_holder # | ||
############## | ||
|
||
|
||
@singleton | ||
class cfg_unique_holder(object): | ||
def __init__(self): | ||
self.cfg = None | ||
# this is use to track the main codes. | ||
self.code = set() | ||
|
||
def save_cfg(self, cfg): | ||
self.cfg = copy.deepcopy(cfg) | ||
|
||
def add_code(self, code): | ||
""" | ||
A new main code is reached and | ||
its name is added. | ||
""" | ||
self.code.add(code) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from argparse import Namespace | ||
from typing import Union | ||
|
||
from hydra.core.config_store import ConfigStore | ||
from omegaconf import DictConfig | ||
|
||
REGISTRIES = {} | ||
|
||
|
||
def setup_registry(registry_name: str, | ||
base_class=None, | ||
default=None, | ||
required=False): | ||
assert registry_name.startswith('--') | ||
registry_name = registry_name[2:].replace('-', '_') | ||
|
||
REGISTRY = {} | ||
REGISTRY_CLASS_NAMES = set() | ||
DATACLASS_REGISTRY = {} | ||
|
||
# maintain a registry of all registries | ||
if registry_name in REGISTRIES: | ||
return # registry already exists | ||
REGISTRIES[registry_name] = { | ||
'registry': REGISTRY, | ||
'default': default, | ||
'dataclass_registry': DATACLASS_REGISTRY, | ||
} | ||
|
||
def build_x(cfg: Union[DictConfig, str, Namespace], *extra_args, | ||
**extra_kwargs): | ||
|
||
assert isinstance(cfg, str) | ||
choice = cfg | ||
if choice in DATACLASS_REGISTRY: | ||
cfg = DATACLASS_REGISTRY[choice]() | ||
|
||
if choice is None: | ||
if required: | ||
raise ValueError('{} is required!'.format(registry_name)) | ||
return None | ||
|
||
cls = REGISTRY[choice] | ||
if hasattr(cls, 'build_' + registry_name): | ||
builder = getattr(cls, 'build_' + registry_name) | ||
else: | ||
builder = cls | ||
return builder(cfg, *extra_args, **extra_kwargs) | ||
|
||
def register_x(name, dataclass=None): | ||
def register_x_cls(cls): | ||
if name in REGISTRY: | ||
raise ValueError('Cannot register duplicate {} ({})'.format( | ||
registry_name, name)) | ||
if cls.__name__ in REGISTRY_CLASS_NAMES: | ||
raise ValueError( | ||
'Cannot register {} with duplicate class name ({})'.format( | ||
registry_name, cls.__name__)) | ||
if base_class is not None and not issubclass(cls, base_class): | ||
raise ValueError('{} must extend {}'.format( | ||
cls.__name__, base_class.__name__)) | ||
|
||
cls.__dataclass = dataclass | ||
if cls.__dataclass is not None: | ||
DATACLASS_REGISTRY[name] = cls.__dataclass | ||
|
||
cs = ConfigStore.instance() | ||
node = dataclass() | ||
node._name = name | ||
cs.store(name=name, | ||
group=registry_name, | ||
node=node, | ||
provider='layoutlmft') | ||
|
||
REGISTRY[name] = cls | ||
|
||
return cls | ||
|
||
return register_x_cls | ||
|
||
return build_x, register_x, REGISTRY, DATACLASS_REGISTRY |
Oops, something went wrong.