ekalda commented on a change in pull request #9508:
URL: https://github.com/apache/tvm/pull/9508#discussion_r757048242
##########
File path: tests/python/contrib/test_ethosu/test_codegen.py
##########
@@ -48,123 +47,184 @@ def get_shape_expr(in_expr, out_expr):
return shape
[email protected](
- "accel_type",
- ACCEL_TYPES,
-)
-def test_ethosu_conv2d(accel_type):
- def create_graph_single(input_tensor_name, input_tensor_shape,
input_tensor_dtype):
- c1_params = relay_ir_builder.QnnConv2DParams(input_tensor_dtype)
- c1_params.ifm.shape = input_tensor_shape
- c1_params.kernel.shape = (3, 3, c1_params.ifm.shape[3], 32)
- c1_params.kernel.sc = relay.const(np.random.rand(32) * 2, "float32")
- c1_params.strides = (1, 1)
- c1_params.pad = "VALID"
- c1_params.update_output_qnn_params(
- input_tensor_dtype, input_tensor_dtype, input_tensor_dtype
- )
- input0 = relay.var(input_tensor_name, shape=c1_params.ifm.shape,
dtype=c1_params.ifm.dtype)
- c1, new_params = relay_ir_builder.create_qnn_conv2d(c1_params, input0)
- c1_params.ofm.shape = get_shape_expr(input0, c1)
[email protected]("ifm_shape", [(1, 299, 299, 3), (1, 55, 55, 3)])
[email protected]("kernel_shape", [(3, 2), (1, 3)])
[email protected]("strides, dilation", [((1, 1), (2, 1)), ((3, 2), (1,
1))])
[email protected]("padding", ["SAME", "VALID"])
[email protected]("accel_type", ACCEL_TYPES)
[email protected]("activation", None, "relu")
+def test_ethosu_conv2d_single(
+ ifm_shape,
+ kernel_shape,
+ strides,
+ dilation,
+ padding,
+ accel_type,
+ activation,
+):
+ dtype = "int8"
- f = relay.Function([input0], c1)
- mod = tvm.IRModule()
- mod["main"] = f
- return mod, [c1_params]
-
- def create_graph_double(input_tensor_name, input_tensor_shape,
input_tensor_dtype):
- c1_params = relay_ir_builder.QnnConv2DParams(input_tensor_dtype)
- c1_params.ifm.shape = input_tensor_shape
- c1_params.kernel.shape = (7, 7, c1_params.ifm.shape[3], 8)
- c1_params.strides = (2, 2)
- c1_params.pad = "VALID"
- c1_params.update_output_qnn_params(
- input_tensor_dtype, input_tensor_dtype, input_tensor_dtype
- )
- input0 = relay.var(input_tensor_name, shape=c1_params.ifm.shape,
dtype=c1_params.ifm.dtype)
- c1, new_params = relay_ir_builder.create_qnn_conv2d(c1_params, input0)
- c1_params.ofm.shape = get_shape_expr(input0, c1)
-
- c2_params = relay_ir_builder.QnnConv2DParams(input_tensor_dtype)
- c2_params.ifm.shape = c1_params.ofm.shape
- c2_params.kernel.shape = (5, 5, c2_params.ifm.shape[3], 16)
- c2_params.strides = (1, 1)
- c2_params.pad = "SAME"
- c2_params.update_output_qnn_params()
- c2, new_params = relay_ir_builder.create_qnn_conv2d(c2_params, c1)
- c2_params.ofm.shape = get_shape_expr(input0, c2)
-
- f = relay.Function([input0], c2)
- mod = tvm.IRModule()
- mod["main"] = f
- return mod, [c2_params, c1_params]
-
- def create_graph_activation(input_tensor_name, input_tensor_shape,
input_tensor_dtype):
- c1_params = relay_ir_builder.QnnConv2DParams(input_tensor_dtype)
- c1_params.ifm.shape = input_tensor_shape
- c1_params.kernel.shape = (7, 7, c1_params.ifm.shape[3], 8)
- c1_params.strides = (2, 2)
- c1_params.pad = "VALID"
- c1_params.activation = "CLIP"
- c1_params.clip_min = 90
- c1_params.clip_max = 110
- c1_params.update_output_qnn_params(
- input_tensor_dtype, input_tensor_dtype, input_tensor_dtype
- )
- input0 = relay.var(input_tensor_name, shape=c1_params.ifm.shape,
dtype=c1_params.ifm.dtype)
- c1, new_params = relay_ir_builder.create_qnn_conv2d(c1_params, input0)
- c1_params.ofm.shape = get_shape_expr(input0, c1)
-
- c2_params = relay_ir_builder.QnnConv2DParams(input_tensor_dtype)
- c2_params.ifm.shape = c1_params.ofm.shape
- c2_params.kernel.shape = (5, 5, c2_params.ifm.shape[3], 16)
- c2_params.strides = (1, 1)
- c2_params.pad = "SAME"
- c2_params.update_output_qnn_params()
- c2, new_params = relay_ir_builder.create_qnn_conv2d(c2_params, c1)
- c2_params.ofm.shape = get_shape_expr(input0, c2)
-
- f = relay.Function([input0], c2)
- mod = tvm.IRModule()
- mod["main"] = f
- return mod, [c2_params, c1_params]
-
- test_cases = [
- (create_graph_single, ["input", (1, 300, 300, 3), "int8"]),
- (create_graph_double, ["input", (1, 128, 256, 4), "int8"]),
- (create_graph_activation, ["input", (1, 64, 100, 4), "int8"]),
- ]
- np.random.seed(42)
- for test_case in test_cases:
- relay_module, conv_params = test_case[0](*test_case[1])
- input_tensor, input_shape, input_dtype = test_case[1]
- mod = partition_for_ethosu(relay_module)
-
- # Generate reference data
- in_min, in_max = util.get_range_for_dtype_str(input_dtype)
- input_data = {
- input_tensor: np.random.randint(
- in_min, high=in_max, size=input_shape, dtype=input_dtype
- )
- }
- output_data = generate_ref_data(relay_module, input_data)
-
- compiled_models = infra.build_source(
- mod, input_data, output_data, accel_type, output_tolerance=1
+ def create_tflite_graph_single():
+ class Model(tf.Module):
+ @tf.function
+ def tf_function(self, x):
+ # Use tf.nn API to create the model
+ op = tf.nn.conv2d(
+ x,
+ filters=tf.constant(np.random.uniform(
+ size=(kernel_shape[0], kernel_shape[1], 3, 3)),
dtype=tf.float32),
+ strides=strides
+ padding=padding,
+ data_format="NHWC",
+ dilations=dilation,
+ )
Review comment:
This currently tests a special case of input channels and output
channels being equal, I think it would be better to test a more general case
for the conv2d (e.g. if the legalization pass got ifm and ofm channels mixed
up, we would never find out), but I wonder what @manupa-arm thinks about
whether it's worth changing?
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]