You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/04/14 16:07:15 UTC
[GitHub] [incubator-tvm] notoraptor opened a new pull request #5331:
[relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
notoraptor opened a new pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331
Hi! I made this PR to add operation `dilate` into relay.
My main goal is to be able to implement a complete version of `conv2d_transpose` in relay which supports all values for groups, dilations, output padding and strides (code based on Theano mplementation: https://github.com/Theano/Theano/blob/master/theano/tensor/nnet/abstract_conv.py#L2927 ). I needed operation `dilation` to do that:
```python
from tvm import relay
def conv2d_transpose(
input_shape, kern_shape, strides, padding, dilation, output_padding, groups
):
h_out = (
(input_shape[2] - 1) * strides[0]
- 2 * padding[0]
+ dilation[0] * (kern_shape[2] - 1)
+ output_padding[0]
+ 1
)
w_out = (
(input_shape[3] - 1) * strides[1]
- 2 * padding[1]
+ dilation[1] * (kern_shape[3] - 1)
+ output_padding[1]
+ 1
)
data = relay.var("data", shape=input_shape)
weight = relay.var("weight", shape=kern_shape)
data_dilated = relay.nn.dilate(data, (1, 1) + strides)
data_padded = relay.nn.pad(
data_dilated,
((0, 0), (0, 0), (0, output_padding[0]), (0, output_padding[1]),),
)
# Pre-process kernel,
# from (m0, m1, m2, m3) to (m1 * g, m0 // g, m2, m3).
mshp0 = kern_shape[0] // groups
c_out = kern_shape[1] * groups
kern = relay.reshape(weight, (groups, mshp0) + kern_shape[1:])
# => (g, m0 // g, m1, m2, m3)
kern = relay.op.transpose(kern, axes=(1, 0, 2, 3, 4))
# => (m0 // g, g, m1, m2, m3)
kern = relay.reshape(kern, (mshp0, c_out, kern_shape[-2], kern_shape[-1]))
# => (m0 // g, m1 * g, m2, m3)
kern = relay.op.transpose(kern, (1, 0, 2, 3))
# => (m1 * g, m0 // g, m2, m3)
# Kernel 2 latest dimensions must be flipped
kern = relay.op.transform.reverse(kern, 2)
kern = relay.op.transform.reverse(kern, 3)
# End pre-processing kernel.
img = relay.nn.conv2d(
data_padded,
kern,
groups=groups,
channels=c_out,
padding=[(kern_shape[2 + i] - 1) * dilation[i] for i in range(2)],
dilation=dilation,
)
if any(p != 0 for p in padding):
img = relay.op.transform.strided_slice(
data=img,
begin=[0, 0, padding[0], padding[1]],
end=[None, None, h_out + padding[0], w_out + padding[1],],
)
f = relay.Function([data, weight], img)
return f
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] notoraptor commented on issue #5331: [relay][topi]
Add operation relay.nn.dilate() which calls topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
notoraptor commented on issue #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331#issuecomment-613617468
@siju-samuel fixed!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request
#5331: [relay][topi] Add operation relay.nn.dilate() which calls
topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331#discussion_r408310639
##########
File path: src/relay/op/nn/nn.cc
##########
@@ -961,6 +961,56 @@ Do log on the data - do not accept logits.
.add_type_rel("CrossEntropy", CrossEntropyRel);
+/////
Review comment:
remove ////, down also
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] tqchen commented on issue #5331: [relay][topi] Add
operation relay.nn.dilate() which calls topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331#issuecomment-613566635
cc @vinx13 @icemelon9 @kevinthesun
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request
#5331: [relay][topi] Add operation relay.nn.dilate() which calls
topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331#discussion_r408311430
##########
File path: include/tvm/relay/attrs/nn.h
##########
@@ -442,6 +442,18 @@ struct Conv2DTransposeAttrs : public tvm::AttrsNode<Conv2DTransposeAttrs> {
}
};
+////
Review comment:
remove ////, down also
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request
#5331: [relay][topi] Add operation relay.nn.dilate() which calls
topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331#discussion_r408312445
##########
File path: python/tvm/relay/op/nn/nn.py
##########
@@ -1347,6 +1347,25 @@ def pad(data,
return _make.pad(data, pad_width, pad_value, pad_mode)
+def dilate(data, strides):
+ """Dilate data with zeros.
+
+ Parameters
+ ----------
+ data : tvm.relay.Expr
+ n-D, can be any layout.
+
+ strides : <tuple of n <int>
Review comment:
`tuple of <int>`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] siju-samuel commented on a change in pull request
#5331: [relay][topi] Add operation relay.nn.dilate() which calls
topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
siju-samuel commented on a change in pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
URL: https://github.com/apache/incubator-tvm/pull/5331#discussion_r408310450
##########
File path: src/relay/op/nn/nn.cc
##########
@@ -961,6 +961,56 @@ Do log on the data - do not accept logits.
.add_type_rel("CrossEntropy", CrossEntropyRel);
+/////
+// relay.nn.dilate
+TVM_REGISTER_NODE_TYPE(DilateAttrs);
+
+bool DilateRel(const Array<Type>& types,
+ int num_inputs,
+ const Attrs& attrs,
+ const TypeReporter& reporter) {
Review comment:
Align the arguments
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
[GitHub] [incubator-tvm] tqchen commented on issue #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
tqchen commented on issue #5331:
URL: https://github.com/apache/incubator-tvm/pull/5331#issuecomment-618087353
cc @Huyuwei @kazum please help manage this PR
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] notoraptor commented on a change in pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
notoraptor commented on a change in pull request #5331:
URL: https://github.com/apache/incubator-tvm/pull/5331#discussion_r413952517
##########
File path: python/tvm/relay/op/nn/_nn.py
##########
@@ -458,6 +458,15 @@ def compute_cross_entropy(attrs, inputs, out_dtype):
reg.register_pattern("nn.cross_entropy", OpPattern.OPAQUE)
+# dilate
+@reg.register_compute("nn.dilate")
+def compute_dilate(attrs, inputs, out_dtype):
+ return [topi.nn.dilate(inputs[0], attrs.strides)]
+
+reg.register_broadcast_schedule("nn.dilate")
+reg.register_pattern("nn.dilate", OpPattern.OPAQUE)
Review comment:
@kazum Done!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] kazum commented on a change in pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
kazum commented on a change in pull request #5331:
URL: https://github.com/apache/incubator-tvm/pull/5331#discussion_r413546386
##########
File path: python/tvm/relay/op/nn/_nn.py
##########
@@ -458,6 +458,15 @@ def compute_cross_entropy(attrs, inputs, out_dtype):
reg.register_pattern("nn.cross_entropy", OpPattern.OPAQUE)
+# dilate
+@reg.register_compute("nn.dilate")
+def compute_dilate(attrs, inputs, out_dtype):
+ return [topi.nn.dilate(inputs[0], attrs.strides)]
+
+reg.register_broadcast_schedule("nn.dilate")
+reg.register_pattern("nn.dilate", OpPattern.OPAQUE)
Review comment:
I wonder if the op pattern is better to be injective.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-tvm] kazum commented on pull request #5331: [relay][topi] Add operation relay.nn.dilate() which calls topi.nn.dilate()
Posted by GitBox <gi...@apache.org>.
kazum commented on pull request #5331:
URL: https://github.com/apache/incubator-tvm/pull/5331#issuecomment-619792700
Thanks @notoraptor @siju-samuel !
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org