Add a python operator for DepthwiseConvolution#99
Add a python operator for DepthwiseConvolution#99mrbeann wants to merge 4 commits intoharvard-acc:masterfrom
Conversation
| 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`. |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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), ( |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
This check should happen before any work is done.
| output_tensors_dims=[output_tensor_dims], | ||
| output_tensor_layout=output_layout, params=params)[0] | ||
|
|
||
| def depthwise_convolution( |
There was a problem hiding this comment.
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).
Since the smaug does not provide the python API for depthwise conv, I add it.
Example Python code.