Skip to content

Commit

Permalink
modify test units for the influence of __del__ of runner 2
Browse files Browse the repository at this point in the history
  • Loading branch information
shufanwu committed Apr 13, 2023
1 parent 0cd19e9 commit 7966b63
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
28 changes: 20 additions & 8 deletions tests/test_hooks/test_ema_hook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import os.path as osp
import shutil
import tempfile

import torch
import torch.nn as nn
Expand Down Expand Up @@ -219,57 +221,66 @@ def test_after_load_checkpoint(self):
def test_with_runner(self):
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.custom_hooks = [ConfigDict(type='EMAHook')]
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
ema_hook = self._get_ema_hook(runner)
runner.train()
epoch_pre_work_dir = cfg.work_dir + 'tmp'
shutil.copytree(cfg.work_dir, epoch_pre_work_dir)
self.assertTrue(
isinstance(ema_hook.ema_model, ExponentialMovingAverage))

checkpoint = torch.load(osp.join(self.temp_dir.name, 'epoch_2.pth'))
checkpoint = torch.load(osp.join(epoch_pre_work_dir, 'epoch_2.pth'))
self.assertTrue('ema_state_dict' in checkpoint)
self.assertTrue(checkpoint['ema_state_dict']['steps'] == 8)

# load and testing
cfg.load_from = osp.join(self.temp_dir.name, 'epoch_2.pth')
cfg.load_from = osp.join(epoch_pre_work_dir, 'epoch_2.pth')
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()

# with model wrapper
cfg.model = ConfigDict(type='DummyWrapper', model=cfg.model)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()

# Test load checkpoint without ema_state_dict
checkpoint = torch.load(osp.join(self.temp_dir.name, 'epoch_2.pth'))
checkpoint = torch.load(osp.join(epoch_pre_work_dir, 'epoch_2.pth'))
checkpoint.pop('ema_state_dict')
torch.save(checkpoint,
osp.join(self.temp_dir.name, 'without_ema_state_dict.pth'))
osp.join(epoch_pre_work_dir, 'without_ema_state_dict.pth'))

cfg.load_from = osp.join(self.temp_dir.name,
cfg.load_from = osp.join(epoch_pre_work_dir,
'without_ema_state_dict.pth')
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()

# Test does not load checkpoint strictly (different name).
# Test load checkpoint without ema_state_dict
cfg.model = ConfigDict(type='ToyModel2')
cfg.custom_hooks = [ConfigDict(type='EMAHook', strict_load=False)]
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()

# Test does not load ckpt strictly (different weight size).
# Test load checkpoint without ema_state_dict
cfg.model = ConfigDict(type='ToyModel3')
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()

# Test enable ema at 5 epochs.
cfg.train_cfg.max_epochs = 10
cfg.custom_hooks = [ConfigDict(type='EMAHook', begin_epoch=5)]
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()
state_dict = torch.load(
osp.join(self.temp_dir.name, 'epoch_4.pth'), map_location='cpu')
osp.join(cfg.work_dir, 'epoch_4.pth'), map_location='cpu')
self.assertIn('ema_state_dict', state_dict)
for k, v in state_dict['state_dict'].items():
assert_allclose(v, state_dict['ema_state_dict']['module.' + k])
Expand All @@ -279,15 +290,16 @@ def test_with_runner(self):
cfg.train_cfg.val_interval = 1
cfg.custom_hooks = [ConfigDict(type='EMAHook', begin_iter=5)]
cfg.default_hooks.checkpoint.interval = 1
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()
state_dict = torch.load(
osp.join(self.temp_dir.name, 'iter_4.pth'), map_location='cpu')
osp.join(cfg.work_dir, 'iter_4.pth'), map_location='cpu')
self.assertIn('ema_state_dict', state_dict)
for k, v in state_dict['state_dict'].items():
assert_allclose(v, state_dict['ema_state_dict']['module.' + k])
state_dict = torch.load(
osp.join(self.temp_dir.name, 'iter_5.pth'), map_location='cpu')
osp.join(cfg.work_dir, 'iter_5.pth'), map_location='cpu')
self.assertIn('ema_state_dict', state_dict)

def _test_swap_parameters(self, func_name, *args, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_hooks/test_empty_cache_hook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
import tempfile
from unittest.mock import patch

from mmengine.testing import RunnerTestCase
Expand All @@ -11,6 +12,7 @@ def test_with_runner(self):
cfg = self.epoch_based_cfg
cfg.custom_hooks = [dict(type='EmptyCacheHook')]
cfg.train_cfg.val_interval = 1e6 # disable validation during training # noqa: E501
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)

runner.train()
Expand All @@ -26,6 +28,7 @@ def test_with_runner(self):

with patch('torch.cuda.empty_cache') as mock_empty_cache:
cfg.custom_hooks = [dict(type='EmptyCacheHook', before_epoch=True)]
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)

runner.train()
Expand All @@ -45,6 +48,7 @@ def test_with_runner(self):
dict(
type='EmptyCacheHook', after_iter=True, before_epoch=True)
]
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)

runner.train()
Expand Down
3 changes: 3 additions & 0 deletions tests/test_hooks/test_param_scheduler_hook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import tempfile
from unittest.mock import Mock

from mmengine.hooks import ParamSchedulerHook
Expand Down Expand Up @@ -144,6 +145,7 @@ def test_with_runner(self):
)
]
init_lr = cfg.optim_wrapper.optimizer.lr
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()

Expand Down Expand Up @@ -174,6 +176,7 @@ def test_with_runner(self):
]

init_lr = cfg.optim_wrapper.optimizer.lr
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()

Expand Down
21 changes: 15 additions & 6 deletions tests/test_hooks/test_profiler_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,18 @@ def test_with_runner(self):
activity_with_cpu=False,
activity_with_cuda=False)
]
runner = self.build_runner(self.epoch_based_cfg)
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()

json_path = ops.join(self.temp_dir.name, 'demo.json')
self.epoch_based_cfg['custom_hooks'] = [
dict(type='ProfilerHook', json_trace_path=json_path)
]
runner = self.build_runner(self.epoch_based_cfg)
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()
self.assertTrue(
ops.exists(json_path), 'ERROR::json file is not generated!')
Expand All @@ -189,21 +193,27 @@ def test_with_runner(self):
sort_by='self_cpu_time_total',
row_limit=10))
]
runner = self.build_runner(self.epoch_based_cfg)
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()

with self.assertRaises(ValueError):
self.epoch_based_cfg['custom_hooks'] = [
dict(type='ProfilerHook', on_trace_ready=0)
]
runner = self.build_runner(self.epoch_based_cfg)
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()

if torch.cuda.is_available():
self.epoch_based_cfg['custom_hooks'] = [
dict(type='ProfilerHook', activity_with_cuda=True)
]
runner = self.build_runner(self.epoch_based_cfg)
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.train()


Expand Down Expand Up @@ -271,7 +281,6 @@ def test_with_runner(self):
cfg = copy.deepcopy(self.epoch_based_cfg)
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner = self.build_runner(self.epoch_based_cfg)
runner.train()

self.assertTrue(
Expand Down
3 changes: 3 additions & 0 deletions tests/test_model/test_test_aug_time.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
import copy
import tempfile

import torch
from torch.utils.data import DataLoader, Dataset
Expand Down Expand Up @@ -107,11 +108,13 @@ def test_with_runner(self):
type='ToyTestTimeAugModel', module=dict(type='ToyModel'))
cfg.test_dataloader.dataset = dict(type='ToyDatasetTTA')
cfg.test_dataloader.dataset['pipeline'] = dict(type='ToyTTAPipeline')
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()

if torch.cuda.is_available() and torch.distributed.is_nccl_available():
cfg.launcher = 'pytorch'
self.setup_dist_env()
cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir.name)
runner = self.build_runner(cfg)
runner.test()
6 changes: 3 additions & 3 deletions tests/test_runner/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2505,9 +2505,9 @@ def test_checkpoint(self):

resumed_cfg.experiment_name = 'test_checkpoint17'
resumed_cfg.work_dir = tempfile.mkdtemp(dir=self.temp_dir)
shutil.rmtree(cfg.work_dir)
shutil.copytree(gan_epoch_pre_work_dir, cfg.work_dir)
path = osp.join(cfg.work_dir, 'epoch_3.pth')
shutil.rmtree(resumed_cfg.work_dir)
shutil.copytree(gan_epoch_pre_work_dir, resumed_cfg.work_dir)
path = osp.join(resumed_cfg.work_dir, 'epoch_3.pth')
runner = Runner.from_cfg(resumed_cfg)
runner.resume(path)
self.assertEqual(len(runner.param_schedulers['linear1']), 1)
Expand Down

0 comments on commit 7966b63

Please sign in to comment.