Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
MMCV
Commits
54c527ac
Unverified
Commit
54c527ac
authored
Oct 13, 2020
by
Ryan Li
Committed by
GitHub
Oct 13, 2020
Browse files
fix wrappers when using parrots (#613)
* fix wrappers when using parrots * linting * refactor according to review
parent
993da2bb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
7 deletions
+14
-7
mmcv/cnn/bricks/wrappers.py
mmcv/cnn/bricks/wrappers.py
+14
-7
No files found.
mmcv/cnn/bricks/wrappers.py
View file @
54c527ac
...
@@ -12,9 +12,16 @@ from torch.nn.modules.utils import _pair
...
@@ -12,9 +12,16 @@ from torch.nn.modules.utils import _pair
from
.registry
import
CONV_LAYERS
,
UPSAMPLE_LAYERS
from
.registry
import
CONV_LAYERS
,
UPSAMPLE_LAYERS
# torch.__version__ could be 1.3.1+cu92, we only need the first two
if
torch
.
__version__
==
'parrots'
:
# for comparison
TORCH_VERSION
=
torch
.
__version__
TORCH_VERSION
=
tuple
(
int
(
x
)
for
x
in
torch
.
__version__
.
split
(
'.'
)[:
2
])
else
:
# 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
])
def
obsolete_torch_version
(
torch_version
,
version_threshold
):
return
torch_version
==
'parrots'
or
torch_version
<=
version_threshold
class
NewEmptyTensorOp
(
torch
.
autograd
.
Function
):
class
NewEmptyTensorOp
(
torch
.
autograd
.
Function
):
...
@@ -34,7 +41,7 @@ class NewEmptyTensorOp(torch.autograd.Function):
...
@@ -34,7 +41,7 @@ class NewEmptyTensorOp(torch.autograd.Function):
class
Conv2d
(
nn
.
Conv2d
):
class
Conv2d
(
nn
.
Conv2d
):
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
if
x
.
numel
()
==
0
and
TORCH_VERSION
<=
(
1
,
4
):
if
x
.
numel
()
==
0
and
obsolete_torch_version
(
TORCH_VERSION
,
(
1
,
4
)
)
:
out_shape
=
[
x
.
shape
[
0
],
self
.
out_channels
]
out_shape
=
[
x
.
shape
[
0
],
self
.
out_channels
]
for
i
,
k
,
p
,
s
,
d
in
zip
(
x
.
shape
[
-
2
:],
self
.
kernel_size
,
for
i
,
k
,
p
,
s
,
d
in
zip
(
x
.
shape
[
-
2
:],
self
.
kernel_size
,
self
.
padding
,
self
.
stride
,
self
.
dilation
):
self
.
padding
,
self
.
stride
,
self
.
dilation
):
...
@@ -57,7 +64,7 @@ class Conv2d(nn.Conv2d):
...
@@ -57,7 +64,7 @@ class Conv2d(nn.Conv2d):
class
ConvTranspose2d
(
nn
.
ConvTranspose2d
):
class
ConvTranspose2d
(
nn
.
ConvTranspose2d
):
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
if
x
.
numel
()
==
0
and
TORCH_VERSION
<=
(
1
,
4
):
if
x
.
numel
()
==
0
and
obsolete_torch_version
(
TORCH_VERSION
,
(
1
,
4
)
)
:
out_shape
=
[
x
.
shape
[
0
],
self
.
out_channels
]
out_shape
=
[
x
.
shape
[
0
],
self
.
out_channels
]
for
i
,
k
,
p
,
s
,
d
,
op
in
zip
(
x
.
shape
[
-
2
:],
self
.
kernel_size
,
for
i
,
k
,
p
,
s
,
d
,
op
in
zip
(
x
.
shape
[
-
2
:],
self
.
kernel_size
,
self
.
padding
,
self
.
stride
,
self
.
padding
,
self
.
stride
,
...
@@ -78,7 +85,7 @@ class MaxPool2d(nn.MaxPool2d):
...
@@ -78,7 +85,7 @@ class MaxPool2d(nn.MaxPool2d):
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
# PyTorch 1.6 does not support empty tensor inference yet
# PyTorch 1.6 does not support empty tensor inference yet
if
x
.
numel
()
==
0
and
TORCH_VERSION
<=
(
1
,
6
):
if
x
.
numel
()
==
0
and
obsolete_torch_version
(
TORCH_VERSION
,
(
1
,
6
)
)
:
out_shape
=
list
(
x
.
shape
[:
2
])
out_shape
=
list
(
x
.
shape
[:
2
])
for
i
,
k
,
p
,
s
,
d
in
zip
(
x
.
shape
[
-
2
:],
_pair
(
self
.
kernel_size
),
for
i
,
k
,
p
,
s
,
d
in
zip
(
x
.
shape
[
-
2
:],
_pair
(
self
.
kernel_size
),
_pair
(
self
.
padding
),
_pair
(
self
.
stride
),
_pair
(
self
.
padding
),
_pair
(
self
.
stride
),
...
@@ -96,7 +103,7 @@ class Linear(torch.nn.Linear):
...
@@ -96,7 +103,7 @@ class Linear(torch.nn.Linear):
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
# empty tensor forward of Linear layer is supported in Pytorch 1.6
# empty tensor forward of Linear layer is supported in Pytorch 1.6
if
x
.
numel
()
==
0
and
TORCH_VERSION
<=
(
1
,
5
):
if
x
.
numel
()
==
0
and
obsolete_torch_version
(
TORCH_VERSION
,
(
1
,
5
)
)
:
out_shape
=
[
x
.
shape
[
0
],
self
.
out_features
]
out_shape
=
[
x
.
shape
[
0
],
self
.
out_features
]
empty
=
NewEmptyTensorOp
.
apply
(
x
,
out_shape
)
empty
=
NewEmptyTensorOp
.
apply
(
x
,
out_shape
)
if
self
.
training
:
if
self
.
training
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment