Here we show how to develop new Pruning algorithms with an example of AutoSlim.
1. Register a new algorithm
Create a new file `mmrazor/models/algorithms/prunning/autoslim.py`, class `AutoSlim` inherits from class `BaseAlgorithm`.
```Python
from mmrazor.registry import MODELS
from .base import BaseAlgorithm
@MODELS.register_module()
class AutoSlim(BaseAlgorithm):
def __init__(self,
mutator,
distiller,
architecture,
data_preprocessor,
init_cfg = None,
num_samples = 2) -> None:
super().__init__(**kwargs)
pass
def train_step(self, data, optimizer):
pass
```
2. Develop new algorithm components (optional)
AutoSlim can directly use class `OneShotChannelMutator` as core functions provider. If it can not meet your needs, you can develop new algorithm components for your algorithm like `OneShotChannalMutator`. We will take `OneShotChannelMutator` as an example to introduce how to develop a new algorithm component:
a. Create a new file `mmrazor/models/mutators/channel_mutator/one_shot_channel_mutator.py`, class `OneShotChannelMutator` can inherits from `ChannelMutator`.
b. Finish the functions you need, eg: `build_search_groups`, `set_choices` , `sample_choices` and so on
```Python
from mmrazor.registry import MODELS
from .channel_mutator import ChannelMutator
@MODELS.register_module()
class OneShotChannelMutator(ChannelMutator):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def sample_choices(self):
pass
def set_choices(self, choice_dict):
pass
# supernet is a kind of architecture in `mmrazor/models/architectures/`
def build_search_groups(self, supernet):
pass
```
c. Import the module in `mmrazor/models/mutators/channel_mutator/__init__.py`
```Python
from .one_shot_channel_mutator import OneShotChannelMutator
__all__ = [..., 'OneShotChannelMutator']
```
3. Rewrite its train_step
Develop key logic of your algorithm in function`train_step`