Skip to content

Commit

Permalink
[Fix] Adapt latest mmcv (#253)
Browse files Browse the repository at this point in the history
* Adapt to the latest mmcv and mmengine

* fixed ut_subnet_sampler_loop

* fix get_model

* fix lints

Co-authored-by: humu789 <humu@pjlab.org.cn>
  • Loading branch information
pppppM and humu789 authored Aug 29, 2022
1 parent b2272eb commit 179bd52
Show file tree
Hide file tree
Showing 45 changed files with 151 additions and 121 deletions.
13 changes: 13 additions & 0 deletions configs/nas/mmcls/spos/spos_shufflenet_search_8xb128_in1k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
_base_ = ['./spos_shufflenet_supernet_8xb128_in1k.py']

train_cfg = dict(
_delete_=True,
type='mmrazor.EvolutionSearchLoop',
dataloader=_base_.val_dataloader,
evaluator=_base_.val_evaluator,
max_epochs=2,
num_candidates=4,
top_k=2,
num_mutation=2,
num_crossover=2,
mutate_prob=0.1)
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/algorithm.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Algorithm
# Algorithm
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/customize_architectures.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Customize Architectures
# Customize Architectures
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/customize_kd_algorithms.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Customize KD algorithms
# Customize KD algorithms
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/customize_mixed_algorithms.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Customize mixed algorithms
# Customize mixed algorithms
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/customize_nas_algorithms.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Customize NAS algorithms
# Customize NAS algorithms
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/customize_pruning_algorithms.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Customize pruning algorithms
# Customize pruning algorithms
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/delivery.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Delivery
# Delivery
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Development tutorials
customize_pruning_algorithms.md
customize_kd_algorithms.md
customize_mixed_algorithms.md
apply_existing_algorithms_to_new_tasks.md
apply_existing_algorithms_to_new_tasks.md
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/mutable.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Mutable
# Mutable
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/mutator.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Mutator
# Mutator
2 changes: 1 addition & 1 deletion docs/en/advanced_guides/recorder.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Recorder
# Recorder
5 changes: 3 additions & 2 deletions docs/en/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@

sys.path.insert(0, os.path.abspath('../..'))


# -- Project information -----------------------------------------------------

project = 'mmrazor'
copyright = '2018-2022, OpenMMLab'
author = 'MMRazor Author'
version_file = '../../mmflow/version.py'


def get_version():
with open(version_file, 'r') as f:
exec(compile(f.read(), version_file, 'exec'))
return locals()['__version__']


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
Expand Down Expand Up @@ -96,4 +97,4 @@ def get_version():
# -- Extension configuration -------------------------------------------------
# Ignore >>> when copying code
copybutton_prompt_text = r'>>> |\.\.\. '
copybutton_prompt_is_regexp = True
copybutton_prompt_is_regexp = True
2 changes: 1 addition & 1 deletion docs/en/get_started/installation.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Installation
# Installation
2 changes: 1 addition & 1 deletion docs/en/get_started/model_zoo.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Model Zoo
# Model Zoo
70 changes: 35 additions & 35 deletions docs/en/make.bat
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
2 changes: 1 addition & 1 deletion docs/en/notes/changelog.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Changelog
# Changelog
2 changes: 1 addition & 1 deletion docs/en/notes/faq.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Frequently Asked Questions
# Frequently Asked Questions
2 changes: 1 addition & 1 deletion docs/en/user_guides/1_learn_about_config.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Learn about Configs
# Learn about Configs
2 changes: 1 addition & 1 deletion docs/en/user_guides/2_train_different_types_algorithms.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Train different types algorithms
# Train different types algorithms
2 changes: 1 addition & 1 deletion docs/en/user_guides/3_train_with_different_devices.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Train with different devices
# Train with different devices
2 changes: 1 addition & 1 deletion docs/en/user_guides/4_test_a_model.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Test a model
# Test a model
2 changes: 1 addition & 1 deletion docs/en/user_guides/analysis_tools.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Analysis tools
# Analysis tools
1 change: 0 additions & 1 deletion docs/en/user_guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ Introductions
nas.md
pruning.md
kd.md

2 changes: 1 addition & 1 deletion docs/en/user_guides/kd.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Knowledge Distillation
# Knowledge Distillation
2 changes: 1 addition & 1 deletion docs/en/user_guides/nas.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Neural Architecture Search
# Neural Architecture Search
2 changes: 1 addition & 1 deletion docs/en/user_guides/pruning.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Pruning
# Pruning
2 changes: 1 addition & 1 deletion docs/en/user_guides/visualization.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Visualization
# Visualization
2 changes: 1 addition & 1 deletion mmrazor/engine/hooks/estimate_resources_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from typing import Any, Dict, Optional, Sequence

import torch
from mmengine.data import BaseDataElement
from mmengine.hooks import Hook
from mmengine.registry import HOOKS
from mmengine.structures import BaseDataElement

from mmrazor.models.task_modules import ResourceEstimator

Expand Down
22 changes: 11 additions & 11 deletions mmrazor/models/algorithms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import torch
import torch.nn as nn
from mmengine import BaseDataElement
from mmengine.model import BaseModel
from mmengine.structures import BaseDataElement

from mmrazor.registry import MODELS

Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(self,
self.architecture = architecture

def forward(self,
batch_inputs: torch.Tensor,
inputs: torch.Tensor,
data_samples: Optional[List[BaseDataElement]] = None,
mode: str = 'tensor') -> ForwardResults:
"""Returns losses or predictions of training, validation, testing, and
Expand Down Expand Up @@ -129,36 +129,36 @@ def forward(self,
or ``dict of tensor for custom use.
"""
if mode == 'loss':
return self.loss(batch_inputs, data_samples)
return self.loss(inputs, data_samples)
elif mode == 'tensor':
return self._forward(batch_inputs, data_samples)
return self._forward(inputs, data_samples)
elif mode == 'predict':
return self._predict(batch_inputs, data_samples)
return self._predict(inputs, data_samples)
else:
raise RuntimeError(f'Invalid mode "{mode}". '
'Only supports loss, predict and tensor mode')

def loss(
self,
batch_inputs: torch.Tensor,
inputs: torch.Tensor,
data_samples: Optional[List[BaseDataElement]] = None,
) -> LossResults:
"""Calculate losses from a batch of inputs and data samples."""
return self.architecture(batch_inputs, data_samples, mode='loss')
return self.architecture(inputs, data_samples, mode='loss')

def _forward(
self,
batch_inputs: torch.Tensor,
inputs: torch.Tensor,
data_samples: Optional[List[BaseDataElement]] = None,
) -> TensorResults:
"""Network forward process."""
return self.architecture(batch_inputs, data_samples, mode='tensor')
return self.architecture(inputs, data_samples, mode='tensor')

def _predict(
self,
batch_inputs: torch.Tensor,
inputs: torch.Tensor,
data_samples: Optional[List[BaseDataElement]] = None,
) -> PredictResults:
"""Predict results from a batch of inputs and data samples with post-
processing."""
return self.architecture(batch_inputs, data_samples, mode='predict')
return self.architecture(inputs, data_samples, mode='predict')
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ def train_generator(
fakeimg = self.generator(fakeimg_init, batch_size)

with optimizer.optim_context(self.generator):
_, data_samples = self.data_preprocessor(data, True)
pseudo_data = self.data_preprocessor(data, True)
pseudo_data_samples = pseudo_data['data_samples']
# recorde the needed information
with self.generator_distiller.student_recorders:
_ = self.student(fakeimg, data_samples, mode='loss')
_ = self.student(fakeimg, pseudo_data_samples, mode='loss')
with self.generator_distiller.teacher_recorders:
for _, teacher in self.teachers.items():
_ = teacher(fakeimg, data_samples, mode='loss')
_ = teacher(fakeimg, pseudo_data_samples, mode='loss')
loss_generator = self.generator_distiller.compute_distill_losses()

generator_loss, generator_loss_vars = self.parse_losses(loss_generator)
Expand Down Expand Up @@ -192,15 +193,16 @@ def train_step(self, data: List[dict],
fakeimg_init = torch.randn(
(batch_size, self.generator.module.latent_dim))
fakeimg = self.generator(fakeimg_init, batch_size)
_, data_samples = self.data_preprocessor(data, True)
pseudo_data = self.data_preprocessor(data, True)
pseudo_data_samples = pseudo_data['data_samples']

with optim_wrapper['generator'].optim_context(self.generator):
# recorde the needed information
with self.generator_distiller.student_recorders:
_ = self.student(fakeimg, data_samples, mode='loss')
_ = self.student(fakeimg, pseudo_data_samples, mode='loss')
with self.generator_distiller.teacher_recorders:
for _, teacher in self.teachers.items():
_ = teacher(fakeimg, data_samples, mode='loss')
_ = teacher(fakeimg, pseudo_data_samples, mode='loss')
loss_generator = self.generator_distiller.compute_distill_losses()

generator_loss, generator_loss_vars = self.parse_losses(loss_generator)
Expand All @@ -209,10 +211,12 @@ def train_step(self, data: List[dict],
with optim_wrapper['architecture'].optim_context(self.student):
# recorde the needed information
with self.distiller.student_recorders:
_ = self.student(fakeimg.detach(), data_samples, mode='loss')
_ = self.student(
fakeimg.detach(), pseudo_data_samples, mode='loss')
with self.distiller.teacher_recorders, torch.no_grad():
for _, teacher in self.teachers.items():
_ = teacher(fakeimg.detach(), data_samples, mode='loss')
_ = teacher(
fakeimg.detach(), pseudo_data_samples, mode='loss')
loss_distill = self.distiller.compute_distill_losses()

distill_loss, distill_log_vars = self.parse_losses(loss_distill)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Optional

import torch
from mmengine import BaseDataElement
from mmengine.structures import BaseDataElement

from mmrazor.models.utils import add_prefix
from mmrazor.registry import MODELS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Optional

import torch
from mmengine import BaseDataElement
from mmengine.structures import BaseDataElement
from torch import nn

from mmrazor.models.utils import add_prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import Dict, List, Optional, Union

import torch
from mmengine import BaseDataElement
from mmengine.model import BaseModel
from mmengine.runner import load_checkpoint
from mmengine.structures import BaseDataElement
from torch import nn
from torch.nn.modules.batchnorm import _BatchNorm

Expand Down
2 changes: 1 addition & 1 deletion mmrazor/models/algorithms/nas/autoslim.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Dict, List, Optional, Union

import torch
from mmengine import BaseDataElement
from mmengine.model import BaseModel, MMDistributedDataParallel
from mmengine.optim import OptimWrapper
from mmengine.structures import BaseDataElement
from torch import nn

from mmrazor.models.distillers import ConfigurableDistiller
Expand Down
2 changes: 1 addition & 1 deletion mmrazor/models/algorithms/nas/spos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing import Dict, List, Optional, Union

import torch
from mmengine import BaseDataElement
from mmengine.model import BaseModel
from mmengine.structures import BaseDataElement
from torch import nn
from torch.nn.modules.batchnorm import _BatchNorm

Expand Down
Loading

0 comments on commit 179bd52

Please sign in to comment.