[Docs] Add short explanation about registry scope (#1114)

This commit is contained in:
Miguel Méndez 2023-07-17 04:21:44 +02:00 committed by GitHub
parent 66d828d8d3
commit 9f21d38907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -272,6 +272,38 @@ output = model(input)
print(output) print(output)
``` ```
### How does the parent node know about child registry?
When working in our `MMAlpha` it might be necessary to use the `Runner` class defined in MMENGINE. This class is in charge of building most of the objects. If these objects are added to the child registry (`MMAlpha`), how is `MMEngine` able to find them? It cannot, `MMEngine` needs to switch to the Registry from `MMEngine` to `MMAlpha` according to the scope which is defined in default_runtime.py for searching the target class.
We can also init the scope accordingly, see example below:
```python
from mmalpha.registry import MODELS
from mmengine.registry import MODELS as MMENGINE_MODELS
from mmengine.registry import init_default_scope
import torch.nn as nn
@MODELS.register_module()
class LogSoftmax(nn.Module):
def __init__(self, dim=None):
super().__init__()
def forward(self, x):
print('call LogSoftmax.forward')
return x
# Works because we are using mmalpha registry
MODELS.build(dict(type="LogSoftmax"))
# Fails because mmengine registry does not know about stuff registered in mmalpha
MMENGINE_MODELS.build(dict(type="LogSoftmax"))
# Works because we are using mmalpha registry
init_default_scope('mmalpha')
MMENGINE_MODELS.build(dict(type="LogSoftmax"))
```
### Use the module of a sibling node ### Use the module of a sibling node
In addition to using the module of the parent nodes, users can also call the module of a sibling node. In addition to using the module of the parent nodes, users can also call the module of a sibling node.