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

[QNN] Conv2D type checking for kernel per-channel scales. #4732

Merged
merged 5 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/relay/qnn/op/convolution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ bool QnnConv2DRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
CHECK(IsScalarType(types[2], DataType::Int(32))); // input_zero_point
CHECK(IsScalarType(types[3], DataType::Int(32))); // kernel_zero_point
CHECK(IsScalarType(types[4], DataType::Float(32))); // input_scale
CHECK(IsScalarType(types[5], DataType::Float(32))); // kernel_scale
// Kernel scale can be a vector of length output_channels or a scalar.
size_t axis = param->kernel_layout.find("O");
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it is a single char, better use 'O' instead of "O", from C++ perspective.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also we should check what happens if there is no "O", just to be safe.

AssignType(types[4], DataType::Float(32), weight->shape[axis], reporter); // kernel scale
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be types[5]? there is a types[4] on line 59.


// Collect the input tensor and output tensor devoid of scale and zero points to reuse Relay
// Conv2D infer type function.
Expand Down
38 changes: 34 additions & 4 deletions tests/python/relay/test_op_qnn_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,8 @@ def test_depthwise_depth_multiplier():
channels=4)
verify(ref_func, qnn_func, data_shape, data_dtype,
kernel_shape, kernel_dtype)


# Depthwise multiplier = 2
data_shape = (10, 4, 16, 16)
data_dtype = 'uint8'
Expand All @@ -794,7 +794,7 @@ def test_depthwise_depth_multiplier():
channels=8)
verify(ref_func, qnn_func, data_shape, data_dtype,
kernel_shape, kernel_dtype)

# uint8 input, NHWC and HWOI
# Depthwise multiplier = 1
data_shape = (2, 16, 16, 4)
Expand All @@ -820,7 +820,7 @@ def test_depthwise_depth_multiplier():
channels=4)
verify(ref_func, qnn_func, data_shape, data_dtype,
kernel_shape, kernel_dtype)

# Depthwise multiplier = 2
data_shape = (2, 16, 16, 4)
data_dtype = 'uint8'
Expand All @@ -846,6 +846,35 @@ def test_depthwise_depth_multiplier():
verify(ref_func, qnn_func, data_shape, data_dtype,
kernel_shape, kernel_dtype)

def test_per_channel_kernel_scale():
with TempOpAttr("qnn.conv2d", "FTVMQnnLegalize", legalize_qnn_conv2d):
data_shape = (2, 1, 2, 4)
data_dtype = 'uint8'
kernel_shape = (3, 1, 2, 2)
kernel_dtype = 'uint8'
data = relay.var("data", shape=data_shape,
dtype=data_dtype)
kernel = relay.var("kernel", shape=kernel_shape,
dtype=kernel_dtype)
kernel_scales = [2, 2]
kernel_scales = relay.const(np.array(kernel_scales).astype('float32'))
func = relay.qnn.op.conv2d(
data, kernel,
input_zero_point=relay.const(0, 'int32'),
kernel_zero_point=relay.const(0, 'int32'),
input_scale=relay.const(2.0, 'float32'),
kernel_scale=kernel_scales,
kernel_size=(2, 2),
padding=(0, 0),
strides=(1, 1),
dilation=(1, 1),
data_layout="NCHW",
kernel_layout="OIHW",
out_dtype="int32")

mod = relay.Function(relay.analysis.free_vars(func), func)
mod = relay.Module.from_expr(mod)

if __name__ == "__main__":
test_no_zero_point()
test_input_zero_point()
Expand All @@ -861,3 +890,4 @@ def test_depthwise_depth_multiplier():
test_tflite_output_multiplier_greater_than_one()
test_tflite_anistropic_strides()
test_depthwise_depth_multiplier()
test_per_channel_kernel_scale()