Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a python operator for DepthwiseConvolution #99

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mrbeann
Copy link
Contributor

@mrbeann mrbeann commented Jul 17, 2021

Since the smaug does not provide the python API for depthwise conv, I add it.

Example Python code.

# consider apply same padding and stride [1, 1] with filter `filter_tensor`
out = nn_ops.depthwise_convolution(
          out, filter_tensor, stride=[1, 1], padding="same", name="depthwise_conv0")

def depthwise_convolution(
input_tensor, filter_tensor, stride, padding, activation=None,
activation_params=None, name="depthwise_conv"):
"""Compute a 3D depthwise Convolution given 4D `input_tensor` and `filter_tensor`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the "3D" here. A 4D tensor + 4D filter produces a 4D output tensor.

stride: A list of two integers: [row_stride, col_stride].
padding: A string from: `same`, `valid`. The zero padding options.
activation: A string representing the activation function (optional).
activation_params: kwargs for the activation function (optional).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kwargs should be the last argument in the signature

name=name, op=types_pb2.ConvolutionDepthwise,
input_tensors=[input_tensor, filter_tensor])

row_idx = 2 if input_tensor.shape.layout == types_pb2.NCHW else 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check that the input tensor shape is either NCHW or NHWC (and same for all the other tensors). Prefer raising a ValueError over assert, which should only happen if an invariant is violated and not because the user specified something incorrectly.

row_idx = 2 if input_tensor.shape.layout == types_pb2.NCHW else 1
col_idx = 3 if input_tensor.shape.layout == types_pb2.NCHW else 2
chan_idx = 1 if input_tensor.shape.layout == types_pb2.NCHW else 3
assert input_tensor.dims(chan_idx) == filter_tensor.dims(chan_idx), (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise a ValueError instead of assert.

filter_tensor.shape.dims[col_idx], stride[1],
padding)
output_layout = input_tensor.shape.layout
if output_layout == types_pb2.NCHW:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should happen before any work is done.

@@ -74,6 +74,66 @@ def compute_output_dim(input_dim, weight_dim, stride, padding):
output_tensors_dims=[output_tensor_dims],
output_tensor_layout=output_layout, params=params)[0]

def depthwise_convolution(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that this is only enabled for the Reference backend, not for SMV (unless we properly implement an accelerated kernel for it). Can you first do a check for the backend? You can get this with get_graph().backend() (don't forget to check for graph == None too).

@xyzsam xyzsam changed the title Add a python warpper for Depthwiseconv Add a python operator for DepthwiseConvolution Jul 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants