Skip to content

Commit

Permalink
Fix wrappers version comparison (open-mmlab#602)
Browse files Browse the repository at this point in the history
* add version check in wrappers

* fix assersion

* use digital version for version comparison

* fix unit tests

* reformat

* fall back to compare the first two version

* fix unittest

* fix unittest

* fix unit test

* clean unnecessary change
  • Loading branch information
ZwwWayne authored and wxzs5 committed Nov 20, 2020
1 parent 87338b1 commit 6244070
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions mmcv/cnn/bricks/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

from .registry import CONV_LAYERS, UPSAMPLE_LAYERS

# torch.__version__ could be 1.3.1+cu92, we only need the first two
# for comparison
TORCH_VERSION = tuple(int(x) for x in torch.__version__.split('.')[:2])


class NewEmptyTensorOp(torch.autograd.Function):

Expand All @@ -30,7 +34,7 @@ def backward(ctx, grad):
class Conv2d(nn.Conv2d):

def forward(self, x):
if x.numel() == 0 and torch.__version__ <= '1.4.0':
if x.numel() == 0 and TORCH_VERSION <= (1, 4):
out_shape = [x.shape[0], self.out_channels]
for i, k, p, s, d in zip(x.shape[-2:], self.kernel_size,
self.padding, self.stride, self.dilation):
Expand All @@ -53,7 +57,7 @@ def forward(self, x):
class ConvTranspose2d(nn.ConvTranspose2d):

def forward(self, x):
if x.numel() == 0 and torch.__version__ <= '1.4.0':
if x.numel() == 0 and TORCH_VERSION <= (1, 4):
out_shape = [x.shape[0], self.out_channels]
for i, k, p, s, d, op in zip(x.shape[-2:], self.kernel_size,
self.padding, self.stride,
Expand All @@ -74,7 +78,7 @@ class MaxPool2d(nn.MaxPool2d):

def forward(self, x):
# PyTorch 1.6 does not support empty tensor inference yet
if x.numel() == 0 and torch.__version__ <= '1.6.0':
if x.numel() == 0 and TORCH_VERSION <= (1, 6):
out_shape = list(x.shape[:2])
for i, k, p, s, d in zip(x.shape[-2:], _pair(self.kernel_size),
_pair(self.padding), _pair(self.stride),
Expand All @@ -91,7 +95,8 @@ def forward(self, x):
class Linear(torch.nn.Linear):

def forward(self, x):
if x.numel() == 0:
# empty tensor forward of Linear layer is supported in Pytorch 1.6
if x.numel() == 0 and TORCH_VERSION <= (1, 5):
out_shape = [x.shape[0], self.out_features]
empty = NewEmptyTensorOp.apply(x, out_shape)
if self.training:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cnn/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_linear():
wrapper(x_empty)


@patch('torch.__version__', '1.6.1')
@patch('mmcv.cnn.bricks.wrappers.TORCH_VERSION', (1, 7))
def test_nn_op_forward_called():

for m in ['Conv2d', 'ConvTranspose2d', 'MaxPool2d']:
Expand All @@ -191,7 +191,7 @@ def test_nn_op_forward_called():
x_empty = torch.randn(0, 3)
wrapper = Linear(3, 3)
wrapper(x_empty)
nn_module_forward.assert_not_called()
nn_module_forward.assert_called_with(x_empty)

# non-randn input
x_normal = torch.randn(1, 3)
Expand Down

0 comments on commit 6244070

Please sign in to comment.