Esempio n. 1
0
def fused_bn_grad_5D_run_3(shape, dtype, kernel_name, attrs):
    """ test bnGrad_3 """
    def get_expect(dy, rs, dgamma_dx, dbeta_dx, data_minus_mean):
        dx = rs * dy + dbeta_dx + data_minus_mean * dgamma_dx
        return [dx]
    shape_c1c0 = (1, shape[1], 1, 1, shape[4])
    bng3_shapes = [shape, shape_c1c0, shape_c1c0, shape_c1c0, shape]
    bng3_dtypes = [dtype, "float32", "float32", "float32", "float32"]
    
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(fused_batch_norm_grad_split.fused_bn_grad_3,
                        bng3_shapes, bng3_dtypes,
                        kernel_name=kernel_name + "_step3", attrs=attrs, tuning=t)  
        if t:
            inputs = [np.random.rand(*s).astype(t) for (s, t) in zip(bng3_shapes, bng3_dtypes)]
            outputs = np.full(shape, np.nan, dtype)
            expects = get_expect(*inputs)
            return mod, expects, (*inputs, *outputs)
        else:
            return mod   
    # np.random.seed(0)
    inputs = [np.random.rand(*s).astype(t) for (s, t) in zip(bng3_shapes, bng3_dtypes)]
    outputs = np.full(shape, np.nan, dtype)
    mod = utils.op_build_test(fused_batch_norm_grad_split.fused_bn_grad_3,
                    bng3_shapes, bng3_dtypes,
                    kernel_name=kernel_name + "_step3", attrs=attrs)
    outputs = [ utils.mod_launch(mod, (*inputs, outputs), expect=get_expect(*inputs)) ]
    expects = get_expect(*inputs)
    rtol, atol = get_rtol_atol("fused_batch_norm_grad", dtype)
    results = list(map(lambda x, y: np.allclose(x, y, rtol=rtol, atol=atol), outputs, expects))
    print("results", results)
    return inputs, outputs, expects, all(results)
Esempio n. 2
0
def unsortedsegmentsum_execute(shape, ids_shape, num_segments, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = unsortedsegmentsum_compile(shape,
                                         ids_shape,
                                         num_segments,
                                         dtype,
                                         attrs,
                                         kernel_name=kernel_name,
                                         tuning=t)
        if t:
            expect, input, output, segment_ids = gen_data(
                dtype, ids_shape, num_segments, shape)
            return mod, expect, (input, segment_ids, output)
        else:
            return mod
    else:
        mod = unsortedsegmentsum_compile(shape, ids_shape, num_segments, dtype,
                                         attrs)
        expect, input, output, segment_ids = gen_data(dtype, ids_shape,
                                                      num_segments, shape)
        output = utils.mod_launch(mod, (input, segment_ids, output),
                                  expect=expect)

        rtol, atol = get_rtol_atol("unsortedsegmentsum", dtype)
        return (input, segment_ids,
                num_segments), output, expect, compare_tensor(output,
                                                              expect,
                                                              rtol=rtol,
                                                              atol=atol,
                                                              equal_nan=True)
Esempio n. 3
0
def fake_quant_with_min_max_vars_per_channel_run(shape_input,
                                                 shape_min,
                                                 shape_max,
                                                 dtype,
                                                 num_bits=8,
                                                 narror_range=False,
                                                 attrs=None):
    """fake_quant_with_min_max_vars_per_channel_run"""
    mod = utils.op_build_test(
        fake_quant_with_min_max_vars_per_channel.
        fake_quant_with_min_max_vars_per_channel,
        [shape_input, shape_min, shape_max], [dtype, dtype, dtype],
        [num_bits, narror_range],
        kernel_name='fake_quant_with_min_max_vars_per_channel',
        attrs=attrs)
    args, exp_output, input_data, input_min, input_max = gen_data(
        shape_input, shape_min, shape_max, dtype, num_bits, narror_range)
    acu_output = utils.mod_launch(mod, args, expect=exp_output)
    # compare result
    rtol, atol = get_rtol_atol("fake_quant_with_min_max_vars_per_channel",
                               dtype)
    testcase_result = compare_tensor(acu_output,
                                     exp_output,
                                     rtol=rtol,
                                     atol=atol,
                                     equal_nan=True)
    return [input_data, input_min,
            input_max], acu_output, exp_output, testcase_result
Esempio n. 4
0
def dropout_execute(shape_tensor, keep_prob, dtype, kernel_name, attrs=None):
    # Create op
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = dropout_compile(shape_tensor,
                              keep_prob,
                              dtype,
                              kernel_name,
                              attrs,
                              tuning=t)
        if t:
            expect, input, output, mask = gen_data(dtype, shape_tensor,
                                                   keep_prob)
            return mod, expect, (input, mask, output)
        else:
            return mod
    else:
        mod = dropout_compile(shape_tensor, keep_prob, dtype, kernel_name,
                              attrs)
        expect, input, output, mask = gen_data(dtype, shape_tensor, keep_prob)
        output = utils.mod_launch(mod, (input, mask, output), expect=expect)

        source_code = mod.imported_modules[0].get_source()
        utils.create_code(kernel_name, "./", source_code)

        rtol, atol = get_rtol_atol("dropout", dtype)
        return (input, mask), output, expect, compare_tensor(output,
                                                             expect,
                                                             rtol=rtol,
                                                             atol=atol,
                                                             equal_nan=True)
Esempio n. 5
0
def logsoftmax_execute(shape, dtype, axis, kernel_name, attrs=None):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = logsoftmax_compile(shape,
                                 dtype,
                                 axis,
                                 kernel_name,
                                 attrs=None,
                                 tuning=t)
        if t:
            expect, input, output = method_name(axis, dtype, shape)
            return mod, expect, (input, output)
        else:
            return mod
    else:
        mod = logsoftmax_compile(shape, dtype, axis, kernel_name, attrs)
        expect, input, output = method_name(axis, dtype, shape)
        output = utils.mod_launch(mod, (input, output), expect=expect)
        rtol, atol = get_rtol_atol("logsoftmax", dtype)
        return input, output, expect, compare_tensor(output,
                                                     expect,
                                                     rtol=rtol,
                                                     atol=atol,
                                                     equal_nan=True)
Esempio n. 6
0
def matmul_execute(shape_x, shape_y, bias, left_format, right_format, out_format, adj_x, adj_y, dtype, out_dtype, kernel_name, attrs):
    '''
    There are four types of fractal format in Davinci core: zZ, zN, nZ, nN
    general matmul format
    left_trans: False right_trans False: zZ * nZ = zN
    left_trans: True  right_trans False: nN * nZ = zN
    left_trans: False right_trans True : zZ * zN = zN
    left_trans: True  right_trans True : nN * zN = zN

    Now we need to support: zN * nZ = zN
    use left_format to specify, left matrix data format
    use right_format to specify, right matrix data format
    '''
    batch_tuple, m, k, n = extract_dim(shape_x, shape_y, adj_x, adj_y)
    m = (m + 15) // 16 * 16
    n = (n + 15) // 16 * 16
    k = (k + 15) // 16 * 16
    shape_xx, shape_yy, bias_shape, out_shape, k = get_converted_shapes(m, n, k, batch_tuple, adj_x, adj_y, bias, left_format, right_format, out_format)
    mod = dynamic_matmul_compile(shape_x, shape_y, bias, left_format, right_format, out_format, adj_x, adj_y, dtype, out_dtype, kernel_name, attrs)
    # Generate data
    m_x, m_y, bench_mark, bias_data = matmul_data(batch_tuple, m, k, n, dtype, out_dtype, bias, adj_x, adj_y, left_format, right_format, out_format)

    # mod launch
    output = np.full(out_shape, np.nan, out_dtype)
    if bias == 0:
        output = utils.mod_launch(mod, (m_x, m_y, output, 1, 1, 1, 1, 1, 1, 1, 1, 1), outputs=(2,), expect=bench_mark)
    elif bias == 1:
        output = utils.mod_launch(mod, (m_x, m_y, bias_data, output), expect=bench_mark)

    # compare result
    rtol, atol = get_rtol_atol("matmul", dtype)
    compare_result = compare_tensor(output, bench_mark, rtol=rtol, atol=atol, equal_nan=True)
    # compare_result = utils.result_compare(output, bench_mark, r_tol=5e-3)
    return (m_x, m_y), output, bench_mark, compare_result
Esempio n. 7
0
def log_ad_run(shape, dtype, kernel_name, attrs):
    input_shape = [shape, shape]
    input_dtype = [dtype, dtype]

    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(log_ad.log_ad,
                                  input_shape,
                                  input_dtype,
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        if t:
            expect, head_np, input, output = gen_data(dtype, shape)
            return mod, expect, (head_np, input, output)
        else:
            return mod
    else:
        mod = utils.op_build_test(log_ad.log_ad,
                                  input_shape,
                                  input_dtype,
                                  kernel_name=kernel_name,
                                  attrs=attrs)
        expect, head_np, input, output = gen_data(dtype, shape)
        output = utils.mod_launch(mod, (head_np, input, output), expect=expect)
        rtol, atol = get_rtol_atol("log_ad", dtype)
        return input, output, expect, compare_tensor(output,
                                                     expect,
                                                     rtol=rtol,
                                                     atol=atol,
                                                     equal_nan=True)
Esempio n. 8
0
def cos_run(shape, dtype, attrs):
    # Generate data for testing the op
    inputs = random_gaussian(shape, miu=0, sigma=0.1).astype(dtype)
    expect = np.cos(inputs)
    # inputs and output to hold the data
    output = np.full(shape, np.nan, dtype)
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(cos.cos, [shape], [dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        if t:
            return mod, expect, (inputs, output)
        else:
            return mod

    else:
        mod = utils.op_build_test(cos.cos, [shape], [dtype],
                                  kernel_name='cos',
                                  attrs=attrs)

    # result_tvm
    output = utils.mod_launch(mod, (inputs, output))

    # compare result
    rtol, atol = get_rtol_atol("cos", dtype)
    TestCase_Result = compare_tensor(output,
                                     expect,
                                     rtol=rtol,
                                     atol=atol,
                                     equal_nan=False)

    return inputs, output, expect, TestCase_Result
Esempio n. 9
0
def cos_ad_run(shape, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        expect, head_np, inputs = get_input_data(dtype, shape)
        mod = utils.op_build_test(cos_ad.cos_ad, [expect.shape, shape],
                                  [dtype, dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        if t:
            # inputs and output to hold the data
            output = np.full(expect.shape, np.nan, dtype)
            return mod, expect, (head_np, inputs, output)
        else:
            return mod
    else:
        expect, head_np, inputs = get_input_data(dtype, shape)
        mod = utils.op_build_test(cos_ad.cos_ad, [expect.shape, shape],
                                  [dtype, dtype],
                                  kernel_name='cos_ad',
                                  attrs=attrs)
        # inputs and output to hold the data
        output = np.full(expect.shape, np.nan, dtype)
        output = utils.mod_launch(mod, (head_np, inputs, output),
                                  expect=expect)
        # compare result
        rtol, atol = get_rtol_atol("cos_ad", dtype)
        TestCase_Result = compare_tensor(output,
                                         expect,
                                         rtol=rtol,
                                         atol=atol,
                                         equal_nan=False)
        return (head_np, inputs), output, expect, TestCase_Result
Esempio n. 10
0
def fused_layernorm_execute(shape_x, begin_norm_axis, begin_params_axis, dtype,
                            attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = fused_layernorm_compile(shape_x,
                                      begin_norm_axis,
                                      begin_params_axis,
                                      dtype,
                                      attrs,
                                      kernel_name=kernel_name,
                                      tuning=t)
        if t:
            beta, expect, gamma, input, out_mean, out_variance, output = gen_data(
                begin_norm_axis, begin_params_axis, dtype, shape_x)
            return mod, expect, {
                "args": (input, gamma, beta, output, out_mean, out_variance),
                'outputs': (-3, -2, -1),
                'tuning': False
            }
        else:
            return mod
    else:
        mod = fused_layernorm_compile(shape_x, begin_norm_axis,
                                      begin_params_axis, dtype, attrs)
        beta, expect, gamma, input, out_mean, out_variance, output = gen_data(
            begin_norm_axis, begin_params_axis, dtype, shape_x)
        res = utils.mod_launch(
            mod, (input, gamma, beta, output, out_mean, out_variance),
            outputs=(-3, -2, -1),
            expect=expect)
        rtol, atol = get_rtol_atol("layernorm", dtype)
        return (input, gamma, beta, begin_norm_axis, begin_params_axis), res, expect, \
            all(map(lambda x, y: compare_tensor(x, y, rtol=rtol, atol=atol), res, expect))
Esempio n. 11
0
def exp_run(shape, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(exp.exp, [shape], [dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        if t:
            expect, input, output = gen_data(dtype, shape)
            return mod, expect, (input, output)
        else:
            return mod
    else:
        mod = utils.op_build_test(exp.exp, [shape], [dtype],
                                  kernel_name='exp',
                                  attrs=attrs)
        expect, input, output = gen_data(dtype, shape)
        output = utils.mod_launch(mod, (input, output), expect=expect)
        rtol, atol = get_rtol_atol("exp", dtype)
        return input, output, expect, compare_tensor(output,
                                                     expect,
                                                     rtol=rtol,
                                                     atol=atol,
                                                     equal_nan=True)
def KLdiv_ad_run(shape, dtype, kernel_name="", attrs=None):
    expects, head, mean, scale, outputs = gen_data(dtype, shape)

    mod = utils.op_build_test(distr_normal_diag_KLdiv_ad.normal_diag_KLdiv_ad,
                              [head.shape, mean.shape, scale.shape],
                              [dtype, dtype, dtype],
                              kernel_name=kernel_name,
                              op_attrs=None,
                              attrs=None,
                              log_cce=True,
                              dump_cce=True,
                              polyhedral=True)
    outputs = utils.mod_launch(mod, [head, mean, scale, *outputs],
                               outputs=tuple(range(-len(outputs), 0)),
                               expect=expects)
    outputs = list(outputs)

    result = True
    rtol, atol = get_rtol_atol("KL_div_ad", dtype)
    for i in range(len(outputs)):
        result &= compare_tensor(outputs[i],
                                 expects[i],
                                 rtol=rtol,
                                 atol=atol,
                                 equal_nan=True)

    return (head, mean, scale), outputs, expects, result
Esempio n. 13
0
def apply_ftrl_run(shape, dtype, attrs=None):
    """run function for dsl function apply_ftrl."""
    scalar_shape = (1, )
    var_shape, accum_shape, linear_shape, grad_shape = [shape] * 4
    lr_shape, l1_shape, l2_shape, lr_power_shape = [scalar_shape] * 4
    shapes = [
        var_shape, accum_shape, linear_shape, grad_shape, lr_shape, l1_shape,
        l2_shape, lr_power_shape
    ]
    dtypes = [dtype] * 9
    mod = utils.op_build_test(apply_ftrl,
                              shapes,
                              dtypes,
                              kernel_name='apply_ftrl',
                              attrs=attrs)
    expects, (var, accum, linear, grad), (lr, l1, l2,
                                          lr_power) = gen_data(dtype, shape)
    outputs = utils.mod_launch(
        mod, (var, accum, linear, grad, lr, l1, l2, lr_power),
        outputs=(0, 1, 2))
    rtol, atol = get_rtol_atol("apply_ftrl", dtype)
    compare_result = list(
        map(lambda x, y: compare_tensor(x, y, rtol=rtol, atol=atol), outputs,
            expects))
    inputs = (var, accum, linear, grad, lr, l1, l2, lr_power)
    return inputs, outputs, expects, all(compare_result)
def fused_minimum_or_maximum_grad_execute(shape_dz, shape_x, shape_y, grad_x, grad_y, op_type, dtype, kernel_name, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = fused_minimum_or_maximum_grad_compile(shape_dz, shape_x, shape_y, grad_x, grad_y, op_type, dtype,
                                                    kernel_name, attrs, t)
        if t:
            input, expect = genData(shape_dz, shape_x, shape_y, grad_x, grad_y, op_type, dtype)
            output_dx = np.full(expect[0].shape, 0, dtype)
            output_dy = np.full(expect[1].shape, 0, dtype)
            return mod, expect, {"args": (input[2], input[0], input[1], output_dx, output_dy), 'outputs': (-2, -1),
                                 'tuning': False}
        else:
            return mod
    input, expect = genData(shape_dz, shape_x, shape_y, grad_x, grad_y, op_type, dtype)
    mod = fused_minimum_or_maximum_grad_compile(shape_dz, shape_x, shape_y, grad_x, grad_y, op_type, dtype,
                                                kernel_name, attrs)

    # get the result from mod
    rtol, atol = get_rtol_atol("fused_minimum_or_maximum_grad", dtype)
    if grad_x and grad_y:
        output_dx = np.full(expect[0].shape, 0, dtype)
        output_dy = np.full(expect[1].shape, 0, dtype)
        res = utils.mod_launch(mod, (input[2], input[0], input[1], output_dx, output_dy), (-2, -1), expect=expect)
        return (input[2], input[0], input[1]), res, expect, all(
            map(lambda x, y: compare_tensor(x, y, rtol=rtol, atol=atol, equal_nan=True), res, expect))

    else:
        output = np.full(expect.shape, 0, dtype)
        output = utils.mod_launch(mod, (input[2], input[0], input[1], output), expect=expect)
        test_case_result = compare_tensor(output, expect, rtol=rtol, atol=atol, equal_nan=True)
        return input, output, expect, test_case_result
Esempio n. 15
0
def maxpool_run(shape, kernel, stride, pad, hybrid, dtype, attrs=None, polyhedral=True):
    if attrs.get("dynamic"):
        var_shape = []
        for i in range(len(shape)):
            if i == len(shape) - 1:
                var_shape.append(shape[i])
            else:
                var_shape.append(tvm.var("I" + str(i)))
        build_shape = var_shape
    else:
        build_shape = shape
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(maxpool.maxpool, [build_shape], [dtype], op_attrs=[kernel, stride, pad],
                                  kernel_name=kernel_name, attrs=attrs, tuning=t)
        if t:
            expect, input, out_shape, res = gen_data(dtype, kernel, pad, shape, stride)
            return mod, expect,  {"args": (input, res), 'outputs': (-1, ), 'tuning': False}
        else:
            return mod
    else:
        if polyhedral:
            if hybrid:
                mod = utils.op_build_test(maxpool.maxpool, [build_shape], [dtype], op_attrs=[kernel, stride, pad],
                                          kernel_name='maxpool', attrs=attrs)
            else:
                mod = utils.op_build_test(maxpool.old_maxpool, [build_shape], [dtype], op_attrs=[kernel, stride, pad],
                                          kernel_name='maxpool_old', attrs=attrs)
        else:
            mod = maxpool.maxpool_manual_schedule(build_shape, kernel, stride, pad, dtype,attrs=attrs, polyhedral=polyhedral)
        expect, input, out_shape, res = gen_data(dtype, kernel, pad, shape, stride)
        output = utils.mod_launch(mod, [input, res], expect=expect)
        rtol, atol = get_rtol_atol("maxpool", dtype)
        return input, output, expect, compare_tensor(output, expect, rtol=rtol, atol=atol, equal_nan=True)
Esempio n. 16
0
def mul_run(shapes, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(mul.mul,
                                  shapes, [dtype, dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        if t:
            expect, lhd, output, rhd = gen_data(dtype, shapes)
            return mod, expect, (lhd, rhd, output)
        else:
            return mod
    else:
        mod = utils.op_build_test(mul.mul,
                                  shapes, [dtype, dtype],
                                  kernel_name='mul',
                                  attrs=attrs)
        expect, lhd, output, rhd = gen_data(dtype, shapes)
        output = utils.mod_launch(mod, (lhd, rhd, output), expect=expect)
        rtol, atol = get_rtol_atol("mul", dtype)
        return (lhd, rhd), output, expect, compare_tensor(output,
                                                          expect,
                                                          rtol=rtol,
                                                          atol=atol,
                                                          equal_nan=True)
Esempio n. 17
0
def reshape_execute(in_shape, out_shape, dtype, attrs):
    if attrs is None:
        attrs = {}
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = reshape_compile(in_shape,
                              out_shape,
                              dtype,
                              attrs,
                              kernel_name=kernel_name,
                              tuning=t)
        if t:
            expect, input, output = gen_data(dtype, in_shape, out_shape)
            return mod, expect, (input, output)
        else:
            return mod
    else:
        mod = reshape_compile(in_shape, out_shape, dtype, attrs)
        expect, input, output = gen_data(dtype, in_shape, out_shape)
        args = [input, output]
        if attrs.get("dynamic"):
            for index in range(len(out_shape) - 1):
                args.append(out_shape[index])
            for i in in_shape:
                args.append(i)
            block_dim = compute_blockdim(in_shape)
            args.append(block_dim)
        output = utils.mod_launch(mod, args, outputs=(1, ), expect=expect)
        rtol, atol = get_rtol_atol("reshape", dtype)
        return input, output, expect, compare_tensor(output,
                                                     expect,
                                                     rtol=rtol,
                                                     atol=atol,
                                                     equal_nan=True)
Esempio n. 18
0
def exp_ad_run(shape, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(exp_ad, [shape, shape], [dtype, dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        expect, head_np, input_np = gen_data(dtype, shape)
        if t:
            output = np.full(expect.shape, np.nan, dtype)
            return mod, expect, (head_np, input_np, output)
        else:
            return mod
    else:
        mod = utils.op_build_test(exp_ad, [shape, shape], [dtype, dtype],
                                  kernel_name='exp_ad',
                                  attrs=attrs)
        expect, head_np, input_np = gen_data(dtype, shape)
        output = np.full(expect.shape, np.nan, dtype)
        output = utils.mod_launch(mod, (head_np, input_np, output),
                                  expect=expect)
        rtol, atol = get_rtol_atol("exp", dtype)
        return (head_np, input_np), output, expect, compare_tensor(output,
                                                                   expect,
                                                                   rtol=rtol,
                                                                   atol=atol)
Esempio n. 19
0
def relu_grad_run(shape, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(relu_grad.relu_grad, [shape, shape],
                                  [dtype, dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  tuning=t)
        if t:
            dy, expect, input_np, output = gen_data(dtype, shape)
            return mod, expect, (input_np, dy, output)
        else:
            return mod
    else:
        mod = utils.op_build_test(relu_grad.relu_grad, [shape, shape],
                                  [dtype, dtype],
                                  kernel_name='relu_grad',
                                  attrs=attrs)
        dy, expect, input_np, output = gen_data(dtype, shape)
        output = utils.mod_launch(mod, (input_np, dy, output), expect=expect)
        rtol, atol = get_rtol_atol("relu_grad", dtype)
        return (input_np, dy), output, expect, compare_tensor(output,
                                                              expect,
                                                              rtol=rtol,
                                                              atol=atol,
                                                              equal_nan=True)
Esempio n. 20
0
def minimum_ad_run(shape, dtype, grad_x=True, grad_y=True, attrs=None):
    """minimum_ad_run implementation"""
    mod = utils.op_build_test(minimum_ad.minimum_ad, [shape, shape, shape],
                              [dtype, dtype, dtype],
                              kernel_name='minimum_ad',
                              op_attrs=[grad_x, grad_y],
                              attrs=attrs)
    rtol, atol = get_rtol_atol("minimum_ad", dtype)
    grads, data_x, data_y, output_dx, output_dy, exp_dx, exp_dy = gen_data(
        shape, dtype)
    inputs = (grads, data_x, data_y)
    if grad_x and grad_y:
        args = (*inputs, output_dx, output_dy)
        expects = (exp_dx, exp_dy)
        outputs = utils.mod_launch(mod, args, outputs=(-2, -1), expect=expects)

    elif grad_x:
        args = (*inputs, output_dx)
        expects = exp_dx
        outputs = utils.mod_launch(mod, args, outputs=(-1, ), expect=expects)

    else:
        args = (*inputs, output_dy)
        expects = exp_dy
        outputs = utils.mod_launch(mod, args, outputs=(-1, ), expect=expects)

    testcase_result = compare_tensor(outputs,
                                     expects,
                                     rtol=rtol,
                                     atol=atol,
                                     equal_nan=True)
    return inputs, outputs, expects, testcase_result
Esempio n. 21
0
def quantized_max_pool_run(shape, dtype1, shape_list, dtype2, ksize, strides,
                           padding, data_format, quant_algo,
                           scale_mode, scale_sqrt, attrs):
    """run function"""
    if not isinstance(shape_list, (list, tuple, type(None))):
        raise RuntimeError("shape_list should be a list, tuple or None!")
    op_attrs = [ksize, strides, padding, data_format,
                quant_algo, scale_mode, scale_sqrt]
    if shape_list is None:
        mod = utils.op_build_test(quantized_max_pool, [shape], [dtype1],
                                  op_attrs=[None] + op_attrs,
                                  kernel_name='quantized_maxpool', attrs=attrs)
    else:
        mod = utils.op_build_test(quantized_max_pool,
                                  [shape, shape_list], [dtype1, dtype2],
                                  op_attrs=op_attrs,
                                  kernel_name='quantized_maxpool', attrs=attrs)
    expect, inputs, out_buf = gen_data(shape, dtype1, shape_list, dtype2, ksize,
                                       strides, padding, data_format, quant_algo,
                                       scale_mode, scale_sqrt)
    output = utils.mod_launch(mod, (*inputs, *out_buf), expect=expect)
    rtol, atol = get_rtol_atol("quantized_maxpool", dtype1)
    if expect.dtype in ("int8", "uint8"):
        cmp_res = compare_int(output, expect)
    else:
        cmp_res = compare_tensor(output, expect, rtol=rtol, atol=atol)
    return inputs, output, expect, cmp_res
Esempio n. 22
0
def scatter_add_run(shape_ref,
                    shape_indices,
                    dtype_ref,
                    dtype_indices,
                    attrs=None):
    """scatter_add_run implementation"""
    args, exp_output, ref, indices, updates = gen_data(shape_ref,
                                                       shape_indices,
                                                       dtype_ref,
                                                       dtype_indices)
    shape_updates = updates.shape
    mod = utils.op_build_test(scatter_add.scatter_add,
                              [shape_ref, shape_indices, shape_updates],
                              [dtype_ref, dtype_indices, dtype_ref],
                              kernel_name='scatter_add',
                              op_attrs=[],
                              attrs=attrs)
    acu_output = utils.mod_launch(mod, args, outputs=(0, ), expect=exp_output)
    # compare result
    rtol, atol = get_rtol_atol("scatter_add", dtype_ref)
    testcase_result = compare_tensor(acu_output,
                                     exp_output,
                                     rtol=rtol,
                                     atol=atol,
                                     equal_nan=True)

    return [ref, indices, updates], acu_output, exp_output, testcase_result
Esempio n. 23
0
def blas_axby_ad_run(shape, dtype, kernel_name, attrs):
    alpha = 2.0
    beta = 3.0

    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(blas_axby_ad.blas_axby_ad, [shape], [dtype], [alpha, beta], kernel_name=kernel_name,
                                  attrs=attrs, tuning=t)
        if t:
            args, expect_dx, expect_dy, head = gen_data(alpha, beta, dtype, shape)
            return mod, (expect_dx, expect_dy), {"args": args, 'outputs': (1, 2), 'tuning': False}
        else:
            return mod
    else:
        mod = utils.op_build_test(blas_axby_ad.blas_axby_ad, [shape], [dtype], [alpha, beta], kernel_name=kernel_name,
                                  attrs=attrs)
        args, expect_dx, expect_dy, head = gen_data(alpha, beta, dtype, shape)
        output_dx, output_dy = utils.mod_launch(mod, args, outputs=(1, 2), expect=(expect_dx, expect_dy))

        rtol, atol = get_rtol_atol("blas_axby_ad", dtype)
        result = compare_tensor(expect_dx, output_dx, rtol=rtol, atol=atol, equal_nan=True) and \
            compare_tensor(expect_dy, output_dy, rtol=rtol, atol=atol, equal_nan=True)

        return (head), (expect_dx, expect_dy), (output_dx, output_dy), result
Esempio n. 24
0
def div_execute(shape1, shape2, dtype, attrs=None):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = div_compile(shape1,
                          shape2,
                          dtype,
                          attrs,
                          kernel_name=kernel_name,
                          tuning=t)
        if t:
            expect, input1, input2, output = gen_data(dtype, shape1, shape2)
            return mod, expect, (input1, input2, output)
        else:
            return mod
    else:
        mod = div_compile(shape1, shape2, dtype, attrs)
        expect, input1, input2, output = gen_data(dtype, shape1, shape2)
        output = utils.mod_launch(mod, (input1, input2, output), expect=expect)
        rtol, atol = get_rtol_atol("div", dtype)
        return (input1, input2), output, expect, compare_tensor(output,
                                                                expect,
                                                                rtol=rtol,
                                                                atol=atol,
                                                                equal_nan=True)
def apply_rms_prop_mixed_precision_run(shape,
                                       dtype,
                                       lr,
                                       momentum,
                                       rho,
                                       epsilon,
                                       attrs=None):
    """run function for dsl function apply_rms_prop_mixed_precision."""
    if attrs is None:
        attrs = {}

    dtype = dtype.lower()
    shapes = [shape, shape, shape, shape, (1, ), (1, ), (1, )]
    types = [dtype, dtype, dtype, dtype, dtype, dtype, dtype]
    op_attrs = [epsilon]

    mod = utils.op_build_test(apply_rms_prop.apply_rms_prop_mixed_precision,
                              shapes,
                              types,
                              op_attrs=op_attrs,
                              kernel_name="apply_rms_prop_mixed_precision",
                              attrs=attrs)
    inputs, expects, args = gen_data(shape, dtype, lr, momentum, rho, epsilon)
    outputs = utils.mod_launch(mod,
                               args,
                               outputs=(0, -1, 1, 2),
                               expect=expects)

    # output type: fp32, fp16, fp32, fp32
    precision = [get_rtol_atol("apply_rms_prop", e.dtype) for e in expects]
    results = list(
        map(lambda x, y, p: compare_tensor(x, y, rtol=p[0], atol=p[1]),
            outputs, expects, precision))

    return inputs, outputs, expects, all(results)
Esempio n. 26
0
def gelu_grad_execute(shape, dtype, attrs):
    np.random.seed(0)

    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = gelu_grad_compile(shape,
                                dtype,
                                attrs,
                                kernel_name=kernel_name,
                                tuning=t)
        if t:
            x, dy, bench_mark, output = gelu_grad_data(shape, dtype)
            return mod, bench_mark, (x, dy, output)
        else:
            return mod
    else:
        mod = gelu_grad_compile(shape, dtype, attrs)
        x, dy, bench_mark, output = gelu_grad_data(shape, dtype)
        output = utils.mod_launch(mod, (x, dy, output), expect=bench_mark)

        rtol, atol = get_rtol_atol("gelu_grad", dtype)
        compare_res = compare_tensor(output,
                                     bench_mark,
                                     rtol=rtol,
                                     atol=atol,
                                     equal_nan=False)

        return (x, dy), output, bench_mark, compare_res
Esempio n. 27
0
def get_result(desc, poly, attrs=None):
    if poly:
        reduce_lib_key = "enable_akg_reduce_lib"
        if reduce_lib_key not in attrs.keys():
            attrs[reduce_lib_key] = poly
    if attrs == {}:
        mod = composite.build(desc, {'dim':"0 0 9728 9728"}, poly=poly)
    else:
        mod = composite.build(desc, attrs, poly=poly)
    input_for_mod, expect, output_indexes = gen_json_data(desc)
    output = utils.mod_launch(mod, input_for_mod, output_indexes)

    rtol, atol = get_rtol_atol("FUSED", "float32")
    flag = True
    if len(output_indexes) > 1:
        if not all(map(lambda x, y: compare_tensor(x, y, rtol=rtol, atol=atol), output, expect)):
            logging.info(mod.imported_modules[0].get_source())
            flag = False
    else:
        if not compare_tensor(output, expect, rtol=rtol, atol=atol):
            logging.info(mod.imported_modules[0].get_source())
            flag = False
    desc_d = json.loads(desc)
    if desc_d["process"] == "cuda":
        inputs = to_tvm_nd_array(input_for_mod)
        expect = to_tvm_nd_array(expect)
        gpu_profiling(mod, *inputs, *expect, repeat_time=400)
    return flag
Esempio n. 28
0
def two2fractal_execute(dim_size, format, dtype, attrs):
    # Generate data
    shape = dim_size
    support_formats = ['zN', 'zZ', 'nZ']
    assert format in support_formats
    assert len(shape) >= 2 and len(shape) <= 4

    # mod launch
    op_attrs = [format]

    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = two2fractal_compile(shape, dtype, op_attrs, attrs, t)
        if t:
            bench_mark, input, output = gen_data(dtype, format, shape)
            return mod, bench_mark, (input, output)
        else:
            return mod
    else:
        mod = two2fractal_compile(shape, dtype, op_attrs, attrs)
        source_code = mod.imported_modules[0].get_source()
        bench_mark, input, output = gen_data(dtype, format, shape)
        output = utils.mod_launch(mod, (input, output), expect=bench_mark)

        # compare result
        rtol, atol = get_rtol_atol("tile", dtype)
        compare_result = compare_tensor(output,
                                        bench_mark,
                                        rtol=rtol,
                                        atol=atol,
                                        equal_nan=True)
        return input, output, bench_mark, compare_result
Esempio n. 29
0
def truncatemod_run(shape1, shape2, dtype, attrs):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod = utils.op_build_test(truncatemod.truncatemod, [shape1, shape2],
                                  [dtype, dtype],
                                  kernel_name=kernel_name,
                                  attrs=attrs,
                                  dump_code=True,
                                  tuning=t)
        if t:
            expect, input1, input2, output = gen_data(dtype, shape1, shape2)
            return mod, expect, (input1, input2, output)
        else:
            return mod
    else:
        expect, input1, input2, output = gen_data(dtype, shape1, shape2)
        mod = utils.op_build_test(truncatemod.truncatemod, [shape1, shape2],
                                  [dtype, dtype],
                                  kernel_name="truncatemod",
                                  attrs=attrs,
                                  dump_code=True)
        output = utils.mod_launch(mod, (input1, input2, output), expect=expect)
        rtol, atol = get_rtol_atol("truncatemod", dtype)
        res = compare_tensor(output,
                             expect,
                             rtol=rtol,
                             atol=atol,
                             equal_nan=True)
        return (input1, input2), output, expect, res
Esempio n. 30
0
def addn_execute(shape, dtype, n, attrs={}):
    # for i in range(len(shapes)):
    if 'tuning' in attrs.keys():
        t = attrs.get("tuning", False)
        kernel_name = attrs.get("kernel_name", False)
        mod, shapes = addn_compile(shape,
                                   dtype,
                                   n,
                                   attrs,
                                   kernel_name=kernel_name,
                                   tuning=t)
        if t:
            expect, inputs, args = gen_data(dtype, n, shape, shapes)
            return mod, expect, args
        else:
            return mod
    else:
        # Result_addn
        mod, shapes = addn_compile(shape, dtype, n, attrs)
        exp_output, inputs, args = gen_data(dtype, n, shape, shapes)
        acu_output = utils.mod_launch(mod, args, expect=exp_output)
        # compare result
        rtol, atol = get_rtol_atol("addn", dtype)
        TestCase_Result = compare_tensor(acu_output,
                                         exp_output,
                                         rtol=rtol,
                                         atol=atol,
                                         equal_nan=True)
        return inputs, acu_output, exp_output, TestCase_Result