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:
[email protected]


With regards,
Apache Git Services

Reply via email to