def test_codegen_dense(): if skip_codegen_test(): return np.random.seed(0) dtype = "float32" trials = [ [(1, 128), (16, 128), 16, True], [(1, 128), (16, 128), 16, False], [(32, 32), (32, 32), 32, True], [(32, 32), (32, 32), 32, False], [(1, 64), (1, 64), 1, True], [(1, 64), (1, 64), 1, False], [(11, 2), (2, 2), 2, True], [(11, 2), (2, 2), 2, False], ] for shape, weight_shape, units, composite in trials: inputs = {"a"} args = (shape, weight_shape, units, dtype) func, params = _get_model(*args, var_names=iter(inputs), has_bias=composite) exp_codegen = _get_expected_codegen(*args, has_bias=composite) verify_codegen(func, exp_codegen)
def test_codegen_global_pooling(): if skip_codegen_test(): return fp32_dtype = ("float32", -127, 128) uint8_dtype = ("uint8", 0, 255) trials = [ ["nn.global_max_pool2d", fp32_dtype, (8, 8, 16)], ["nn.global_max_pool2d", fp32_dtype, (9, 9, 16)], ["nn.global_max_pool2d", fp32_dtype, (8, 8, 16)], ["nn.global_max_pool2d", uint8_dtype, (8, 8, 16)], ["nn.global_max_pool2d", uint8_dtype, (9, 9, 16)], ["nn.global_avg_pool2d", fp32_dtype, (8, 8, 16)], ["nn.global_avg_pool2d", fp32_dtype, (8, 8, 16)], ["nn.global_avg_pool2d", fp32_dtype, (9, 9, 16)], ["nn.global_avg_pool2d", uint8_dtype, (8, 8, 16)], ["nn.global_avg_pool2d", uint8_dtype, (8, 8, 16)], ] for typef, (dtype, low, high), input_shape in trials: shape = (1, *input_shape) inputs = {"a"} args = (shape, dtype, typef) func = _get_global_pooling_model(*args, iter(inputs)) exp_codegen = _get_expected_global_pooling_codegen(*args) verify_codegen(func, exp_codegen, 1)
def test_codegen_conv2d(): if skip_codegen_test(): return dtype = "float32" trials = [ # Normal convolution [2, 2, (1, 1), (1, 1), (1, 1), 4, (10, 10, 14), (False, False, False), False], [2, 1, (2, 2), (1, 1), (1, 1), 7, (12, 15, 16), (False, False, True), False], [3, 3, (2, 1), (1, 1), (1, 1), 4, (10, 10, 14), (False, True, False), False], [3, 3, (1, 1), (1, 1), (1, 1), 16, (12, 15, 16), (False, False, False), False], [5, 5, (1, 1), (2, 2), (1, 1), 4, (10, 10, 14), (True, False, False), False], [1, 3, (1, 1), (1, 1), (1, 1), 7, (20, 20, 20), (False, False, True), False], [2, 2, (2, 2), (1, 1), (1, 1), 4, (20, 20, 20), (False, True, False), False], [5, 5, (1, 1), (2, 2), (1, 1), 4, (10, 10, 14), (True, False, False), False], [3, 3, (2, 1), (1, 1), (1, 1), 7, (20, 20, 20), (False, False, False), False], [3, 3, (1, 1), (2, 2), (1, 1), 16, (10, 10, 14), (False, True, True), False], # Depth-wise convolution [3, 3, (1, 1), (1, 1), (1, 1), 20, (20, 20, 20), (False, False, True), True], [5, 5, (2, 2), (1, 1), (1, 1), 20, (20, 20, 20), (False, True, False), True], [3, 3, (2, 2), (2, 2), (1, 1), 14, (10, 10, 14), (True, False, False), True], [5, 5, (0, 0), (1, 1), (1, 1), 20, (20, 20, 20), (False, False, False), True], [3, 3, (1, 1), (2, 2), (1, 1), 14, (10, 10, 14), (False, True, True), True], ] for ( kernel_h, kernel_w, pad, stride, dilation, out_channels, shape, composite, is_depthwise, ) in trials: shape = (1, *shape) if is_depthwise: groups = shape[3] else: groups = 1 inputs = {"a"} args = (shape, kernel_h, kernel_w, pad, stride, dilation, groups, dtype, out_channels) func, params = _get_model( *args, var_names=iter(inputs), has_pad=composite[0], has_bias=composite[1], has_activation=composite[2], ) exp_codegen = _get_expected_codegen( *args, has_bias=composite[1], has_activation=composite[2] ) verify_codegen(func, exp_codegen, 1)
def test_codegen_add(): if skip_codegen_test(): return inputs = {"a", "b"} for dtype, op_name, op, qnn_params in [ ("float32", "add", relay.add, {}), ("uint8", "qnn.add", relay.qnn.op.add, _qnn_params), ]: for shape in [(1, 1), (2, 2, 2), (3, 3, 3, 3)]: func = _get_model(shape, dtype, iter(inputs), op, qnn_params) exp_codegen = _get_expected_codegen(shape, dtype, op_name, qnn_params) verify_codegen(func, exp_codegen, 1)
def test_codegen_concatenate(): if skip_codegen_test(): return shape_a = [1, 234, 234, 256] shape_b = [2, 234, 234, 256] shape_c = [3, 234, 234, 256] axis = 0 inputs = {"a", "b", "c"} for dtype in ["float32"]: args = (shape_a, shape_b, shape_c, axis, dtype) func = _get_model(*args, iter(inputs)) exp_codegen = _get_expected_codegen(*args) verify_codegen(func, exp_codegen, 1, disabled_ops=[])
def test_codegen_qnn_dense(): if skip_codegen_test(): return np.random.seed(0) dtype = "uint8" trials = [ [(1, 2), (2, 2), 2, True], [(1, 2), (2, 2), 2, False], [(4, 4), (4, 4), 4, True], [(4, 4), (4, 4), 4, False], [(16, 16), (4, 16), 4, True], [(16, 16), (4, 16), 4, False], [(1, 128), (16, 128), 16, True], [(1, 128), (16, 128), 16, False], [(32, 32), (32, 32), 32, True], [(32, 32), (32, 32), 32, False], [(1, 64), (1, 64), 1, True], [(1, 64), (1, 64), 1, False], ] for shape, weight_shape, units, composite in trials: inputs = {"a"} args = (shape, weight_shape, units, dtype) input_zp = 100 input_sc = 0.5 kernel_zp = 25 kernel_sc = 0.03 output_zp, output_sc = _get_qnn_params(input_zp, input_sc, kernel_zp, kernel_sc, weight_shape[0], weight_shape[1]) func, params = _get_qnn_model( *args, var_names=iter(inputs), input_zp=input_zp, input_sc=input_sc, kernel_zp=kernel_zp, kernel_sc=kernel_sc, output_zp=output_zp, output_sc=output_sc, has_bias=composite, ) exp_codegen = _get_expected_codegen(*args, has_bias=composite) verify_codegen(func, exp_codegen)
def test_codegen_qnn_dense(): if skip_codegen_test(): return np.random.seed(0) dtype = ["uint8"] shape = [(1, (1, 128), (16, 128), 16), (1, (32, 32), (32, 32), 32), (0, (1, 64), (1, 64), 1)] composite = [False, True] trials = generate_trials([dtype, shape, composite], 3) for dtype, (acl_partitions, shape, weight_shape, units), composite in trials: inputs = {"a"} args = (shape, weight_shape, units, dtype) input_zp = 100 input_sc = 0.5 kernel_zp = 25 kernel_sc = 0.03 output_zp, output_sc = _get_qnn_params(input_zp, input_sc, kernel_zp, kernel_sc, weight_shape[0], weight_shape[1]) func, params = _get_qnn_model( *args, var_names=iter(inputs), input_zp=input_zp, input_sc=input_sc, kernel_zp=kernel_zp, kernel_sc=kernel_sc, output_zp=output_zp, output_sc=output_sc, has_bias=composite, ) exp_codegen = _get_expected_codegen(*args, has_bias=composite) verify_codegen(func, exp_codegen, acl_partitions, 2 - 2 * acl_partitions)
def test_codegen_dense(): if skip_codegen_test(): return np.random.seed(0) dtype = ["float32"] shape = [(1, (1, 128), (16, 128), 16), (1, (32, 32), (32, 32), 32), (0, (1, 64), (1, 64), 1)] composite = [False, True] trials = generate_trials([dtype, shape, composite], 3) for dtype, (acl_partitions, shape, weight_shape, units), composite in trials: inputs = {"a"} args = (shape, weight_shape, units, dtype) func, params = _get_model(*args, var_names=iter(inputs), has_bias=composite) exp_codegen = _get_expected_codegen(*args, has_bias=composite) verify_codegen(func, exp_codegen, acl_partitions, 1 - acl_partitions)
def test_codegen_pooling(): if skip_codegen_test(): return fp32_dtype = ("float32", -127, 128) uint8_dtype = ("uint8", 0, 255) # fmt: off trials = [ [ "nn.max_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (0, 0), False, True, (16, 16, 16), (0, 1), ], [ "nn.max_pool2d", fp32_dtype, (3, 3), (2, 2), (1, 1), (1, 1), True, True, (15, 15, 16), (0, 1), ], [ "nn.max_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (0, 1), False, False, (16, 16, 16), (0, 1), ], [ "nn.max_pool2d", uint8_dtype, (3, 3), (2, 2), (1, 1), (0, 1), False, False, (16, 16, 16), (0, 1), ], [ "nn.max_pool2d", uint8_dtype, (2, 2), (2, 2), (1, 1), (1, 1), True, True, (15, 15, 16), (0, 1), ], [ "nn.max_pool2d", uint8_dtype, (2, 2), (2, 2), (3, 2), (1, 1), True, True, (15, 15, 16), (1, 0), ], [ "nn.avg_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (1, 1), False, False, (16, 16, 16), (0, 1), ], [ "nn.avg_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (1, 1), False, False, (16, 16, 16), (0, 1), ], [ "nn.avg_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (0, 0), False, True, (16, 16, 16), (0, 1), ], [ "nn.avg_pool2d", fp32_dtype, (3, 3), (2, 2), (3, 2), (0, 1), True, False, (15, 15, 16), (1, 0), ], [ "nn.avg_pool2d", uint8_dtype, (2, 2), (2, 2), (1, 1), (1, 1), False, True, (16, 16, 16), (0, 1), ], [ "nn.avg_pool2d", uint8_dtype, (3, 3), (2, 2), (1, 1), (0, 1), False, False, (16, 16, 16), (0, 1), ], [ "nn.l2_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (0, 1), True, False, (15, 15, 16), (0, 1), ], [ "nn.l2_pool2d", fp32_dtype, (3, 3), (2, 2), (1, 1), (0, 0), False, False, (16, 16, 16), (0, 1), ], [ "nn.l2_pool2d", fp32_dtype, (2, 2), (2, 2), (1, 1), (1, 1), False, True, (15, 15, 16), (0, 1), ], ] # fmt: on for ( typef, (dtype, low, high), size, stride, dilation, pad, ceil_mode, count_include_pad, input_shape, (tvm_ops, acl_partitions), ) in trials: shape = (1, *input_shape) inputs = {"a"} args = (shape, dtype, typef, size, stride, dilation, pad, False, False) func = _get_pooling_model(*args, iter(inputs)) exp_codegen = _get_expected_pooling_codegen(*args) verify_codegen(func, exp_codegen, acl_partitions, tvm_ops)