Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Trainer, Reporter system #42

Merged
merged 25 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update README.md
  • Loading branch information
moskomule committed Aug 6, 2020
commit 0f035a709c3dc614975b89706afa0468efb48950
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ trainer = CustomTrainer({"generator": generator, "discriminator": discriminator}

## Distributed training

Easy distributed initializer `homura.init_distributed()` is available. See [imagenet.py](example/imagenet.py) as an example.
Distributed training is complicated at glance. `homura` has simple APIs, to hide the messy codes for DDP, such as `homura.init_distributed` for the initialization and `homura.is_master` for checking if the process is master or not.

For details, see `examples/imagenet.py`.

## Reproducibility

Expand All @@ -148,6 +149,30 @@ with set_seed(seed):
other_thing()
```

## Registry System

Following major libraries, `homura` also has a simple register system.

```python
from homura import Registry
MODEL_REGISTRY = Registry("language_models")

@MODEL_REGISTRY.register
class Transformer(nn.Module):
...

# or

MODEL_REGISTRY.register(bert_model, 'bert')

# magic
MODEL_REGISTRY.import_modules(".")

transformer = MODEL_REGISTRY('Transformer')(...)
# or
bert = MODEL_REGISTRY('bert', ...)
```

# Examples

See [examples](examples).
Expand All @@ -168,7 +193,7 @@ If you want

* single node multi threads multi gpus

run `python -m torch.distributed.launch --nproc_per_node=$NUM_GPUS imagenet.py distributed.on=true`.
run `python -m torch.distributed.launch --nproc_per_node=$NUM_GPUS imagenet.py [...]`.

If you want

Expand Down
18 changes: 11 additions & 7 deletions homura/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,38 @@ def register_from_dict(self,
def register(self,
func: Callable = None,
*,
name: Optional[str] = None):
name: Optional[str] = None
) -> Callable:
if func is None:
return functools.partial(self.register, name=name)

_type = self.type
if _type is not None and not isinstance(_type, types.FunctionType):
if _type is not None and not isinstance(func, types.FunctionType):
if not (isinstance(func, _type) or issubclass(func, _type)):
raise TypeError(
f'`func` is expected to be subclass of {_type}.')
raise TypeError(f'`func` is expected to be subclass of {_type}.')

if name is None:
name = func.__name__

if self._registry.get(name) is not None:
raise KeyError(
f'Name {name} is already used, try another name!')
raise KeyError(f'Name {name} is already used, try another name!')

self._registry[name] = func
return func

def __call__(self,
name: str):
name: str,
*args,
**kwargs):
ret = self._registry.get(name)
if ret is None:
_registry = {k.lower(): v for k, v in self._registry.items()}
ret = _registry.get(name.lower())
if ret is None:
raise KeyError(f'Unknown {name} is called!')
if len(args) > 0 or len(kwargs) > 0:
# if args and kwargs is specified, instantiate
ret = ret(*args, **kwargs)
return ret

@classmethod
Expand Down