def topk_run(shape, k, dtype, attrs): if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(topk.topk, [shape], [dtype], [k], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect_index, expect_value, input, output_value = gen_data( dtype, k, shape) return mod, (expect_index, expect_value), (input, output_value) else: return mod else: mod = topk.topk(shape, k, dtype, "topk", attrs) expect_index, expect_value, input, output_value = gen_data( dtype, k, shape) output_index = np.full(expect_index.shape, np.nan, dtype) #output_value, output_index = utils.mod_launch(mod, (input, output_value, output_index)) output_value = utils.mod_launch(mod, (input, output_value), expect=(expect_value, expect_index)) result = compare_tensor(output_value, expect_value, rtol=5e-03, equal_nan=True) and \ compare_tensor(output_index, expect_index, rtol=5e-03, equal_nan=True) return input, (output_value, output_index), (expect_value, expect_index), result
def prelu_grad_run(shape, w_shape, dtype, rtol, attrs): if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(prelu_grad.prelu_grad, [shape, shape, w_shape], [dtype, dtype, dtype], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: dy, expect_dA, expect_dw, input_data, output_dA, output_dw, w_data = gen_data( dtype, shape, w_shape) return mod, (expect_dA, expect_dw), { "args": (dy, input_data, w_data, output_dA, output_dw), 'outputs': (-2, -1), 'tuning': False } else: return mod else: mod = utils.op_build_test(prelu_grad.prelu_grad, [shape, shape, w_shape], [dtype, dtype, dtype], kernel_name='prelu_grad', attrs=attrs) dy, expect_dA, expect_dw, input_data, output_dA, output_dw, w_data = gen_data( dtype, shape, w_shape) output_dA, output_dw = utils.mod_launch( mod, (dy, input_data, w_data, output_dA, output_dw), outputs=(-2, -1), expect=(expect_dw, expect_dA)) return (input_data, dy, w_data), (output_dw, output_dA), (expect_dw, expect_dA), \ compare_tensor(output_dw, expect_dw, rtol=rtol) and compare_tensor(output_dA, expect_dA, rtol=rtol)
def fused_mul_div_rsqrt_mul_isfinite_red_run(shape, dtype='float32', poly_sch=True, attrs=None): if not attrs: attrs = {"target": "cuda"} attrs.update({"enable_akg_reduce_lib": True, "enable_atomic_add": True}) inputs = gen_data(shape, dtype) expect = compute_expect(inputs) input_shape = [shape, shape] input_dtype = [dtype, dtype] mod = utils.op_build_test(fused_mul_div_rsqrt_mul_isfinite_red, input_shape, input_dtype, kernel_name="fused_mul_div_rsqrt_mul_isfinite_red", polyhedral=poly_sch, attrs=attrs) outputs = [np.full((1,), False, 'bool')] + [np.full(shape, np.nan, dtype)] * 3 output = utils.mod_launch(mod, [*inputs, *outputs], outputs=list(range(-len(outputs), 0)), expect=expect) ret = compare_tensor(output[0], expect[0], rtol=5e-03, atol=1.e-08) ret &= compare_tensor(output[1], expect[1], rtol=5e-03, atol=1.e-08) ret &= compare_tensor(output[2], expect[2], rtol=5e-03, atol=1.e-08) ret &= compare_tensor(output[3], expect[3], rtol=5e-03, atol=1.e-08) print("Test {}".format("Pass" if ret else "Failed")) target_name = attrs["target"].split()[0] if not ret: mod_source = mod if target_name != "llvm": mod_source = mod.imported_modules[0] print("Error {}:========================".format(target_name)) print(mod_source.get_source()) raise AssertionError("Test fail") if attrs["profiling"]: data = to_tvm_nd_array([*inputs, *outputs], akg.tvm.context(target_name, 0)) target_profiling(mod, *data, target=target_name, repeat_time=attrs["repeat_times"]) return inputs, outputs, expect, ret
def fused_gather_gather_add_mul_max_exp_scatter_add_run( input1_shape, input2_shape, input3_shape, input4_shape, data_dtype, indices_type, axis, poly_sch=True, attrs=None): op_attrs = [axis] default_attrs = {"target": "cuda"} if attrs: default_attrs.update(attrs) mod = utils.op_build_test( fused_gather_gather_add_mul_max_exp_scatter_add, [input1_shape, input2_shape, input3_shape, input4_shape], [data_dtype, indices_type, data_dtype, indices_type], op_attrs=op_attrs, attrs=default_attrs, polyhedral=poly_sch, kernel_name="fused_gather_gather_add_mul_max_exp_scatter_add", ) # gen data input1, input2, input3, input4, expect1, expect2 = gen_data( input1_shape, input2_shape, input3_shape, input4_shape, data_dtype, indices_type, axis) output1 = np.zeros(expect1.shape, expect1.dtype) output2 = deepcopy(input1) output1, output2 = utils.mod_launch( mod, (input1, input2, input3, input4, output1, output2), outputs=(-2, -1)) atol, rtol = get_rtol_atol( "fused_gather_gather_add_mul_max_exp_scatter_add", data_dtype) res = compare_tensor(output1, expect1, rtol=rtol, atol=atol) res &= compare_tensor(output2, expect2, rtol=rtol, atol=atol) print("Test {}".format("Pass" if res else "Failed")) target_name = attrs["target"].split()[0] if not res: mod_source = mod if target_name != "llvm": mod_source = mod.imported_modules[0] print("Error {}:========================".format(target_name)) print(mod_source.get_source()) raise AssertionError("Test fail") if attrs["profiling"]: inputs = to_tvm_nd_array( [input1, input2, input3, input4, output1, output2], akg.tvm.context(target_name, 0)) target_profiling(mod, *inputs, target=target_name, repeat_time=attrs["repeat_times"]) return (input1, input2, input3, input4), (output1, output2), (expect1, expect2), res
def winograd_ad_run(filter_shape, tile, dtype, attrs): def RANGEFILL(shape): size = np.prod([d for d in shape]) offset = size // 2 return np.arange(-offset, size - offset, dtype=dtype).reshape(shape) A = akg.tvm.placeholder(filter_shape, dtype=dtype, name='image') B = akg.topi.nn.conv2d_winograd_weight_transform(A, 2) head_np = RANGEFILL([d.value for d in B.shape]).astype(dtype) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(winograd_ad, [head_np.shape, filter_shape], [dtype, dtype], kernel_name=kernel_name, attrs=attrs, log_code=True, dump_code=True, tuning=t) if t: expect, input_np, output = gen_data(filter_shape, RANGEFILL, dtype) return mod, expect, (head_np, input_np, output) else: return mod else: # scenario 1: expect, input_np, output = gen_data(filter_shape, RANGEFILL, dtype) mod = utils.op_build_test(winograd_ad, [head_np.shape, filter_shape], [dtype, dtype], kernel_name="winograd_ad", attrs=attrs, log_code=True, dump_code=True) output = utils.mod_launch(mod, [head_np, input_np, output], expect=expect) if not compare_tensor(output, expect, atol=0.1): return [head_np, input_np], output, expect, compare_tensor(output, expect, rtol=5e-03, atol=5e-03, equal_nan=True) # scenario 2: head_np = np.ones(np.prod([d for d in B.shape ])).reshape(B.shape).astype(dtype) expect = np.array([[[4., 0., 4.], [0., 0., 0.], [4., 0., 4.]], [[4., 0., 4.], [0., 0., 0.], [4., 0., 4.]]]).astype(dtype) return [head_np, input_np], output, expect, compare_tensor(output, expect, rtol=5e-03, atol=5e-03, equal_nan=True)
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
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, 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, 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)
def mean_square_run(shape, reduce_axis, keepdims, dtype, attrs): op_attrs = [reduce_axis, keepdims] if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(mean_square.mean_square, [shape], [dtype], op_attrs, kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect, input1, output = gen_data(dtype, keepdims, reduce_axis, shape) return mod, expect, (input1, output) else: return mod else: mod = utils.op_build_test(mean_square.mean_square, [shape], [dtype], op_attrs, attrs=attrs) expect, input1, output = gen_data(dtype, keepdims, reduce_axis, shape) output = utils.mod_launch(mod, (input1, output), expect=expect) return input1, output, expect, compare_tensor(output, expect, rtol=5e-3, atol=5e-3, equal_nan=True)
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)
def hpl_cholesky_run(shape, dtype, poly_sch=True, attrs=None): attrs = { "enable_double_buffer": False, "enable_pre_poly_loop_partition": False, "enable_post_poly_loop_partition": False, "enable_to_three_address": False, "pragma_checkcoincident": False, "pragma_enable_reschedule": False, "dim": "0 0 65536 65536 0 1 65536 65536 0 2 65536 65536" } mod = utils.op_build_test(hpl_cholesky, [ shape, ], [ dtype, ], kernel_name="hpl_cholesky", polyhedral=poly_sch, attrs=attrs) input1, output, expect = gen_data(shape, dtype) output = utils.mod_launch(mod, (input1, output), expect=expect, outputs=(0, )) rtol = atol = 1e-04 res = compare_tensor(output, expect, rtol=rtol, atol=atol) print("Test {}".format("Pass" if res else "Failed")) return (input1, ), output, expect, res
def proposal_sort_run(shape, topk: int, dtype, kernel_name, attrs): op_attrs = [topk] if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(proposal_sort.proposal_sort, [shape], [dtype], op_attrs=op_attrs, kernel_name=kernel_name, attrs=attrs, log_code=False, tuning=t) if t: data, expect, output = np_proposal_sort(shape, topk, dtype) return mod, expect, (data, output) else: return mod else: mod = utils.op_build_test(proposal_sort.proposal_sort, [shape], [dtype], op_attrs=op_attrs, kernel_name=kernel_name, attrs=attrs, log_code=False) data, expect, output = np_proposal_sort(shape, topk, dtype) output = utils.mod_launch(mod, (data, output), expect=expect) test_case_result = compare_tensor(output, expect, rtol=5e-03, equal_nan=True) print(" ========== PASS ============") return data, output, expect, test_case_result
def quantized_avg_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_avg_pool, [shape], [dtype1], op_attrs=[None] + op_attrs, kernel_name='quantized_avgpool', attrs=attrs) else: mod = utils.op_build_test(quantized_avg_pool, [shape, shape_list], [dtype1, dtype2], op_attrs=op_attrs, kernel_name='quantized_avgpool', 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_avgpool", 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
def tanh_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(TanhAd, [shape, shape], [dtype, dtype], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect, head_np, input_np, output = gen_data(dtype, shape) return mod, expect, (head_np, input_np, output) return mod else: mod = utils.op_build_test(TanhAd, [shape, shape], [dtype, dtype], kernel_name='tanh_ad', attrs=attrs) expect, head_np, input_np, output = gen_data(dtype, shape) output = utils.mod_launch(mod, (head_np, input_np, output), expect=expect) rtol, atol = get_rtol_atol("tanh_ad", dtype) return (head_np, input_np), output, expect, compare_tensor(output, expect, rtol=rtol, atol=atol)
def discontinous_mov_run(shapes, dtype, attrs): # Result_Numpy shape1 = shapes[0] shape2 = shapes[1] op_attrs = [shape2] if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(discontinous_mov.discontinous_mov, [shape1], [dtype], op_attrs, kernel_name=kernel_name, attrs=attrs, tuning=t) if t: args, exp_output, input = gen_data(dtype, shape1, shape2) return mod, exp_output, args else: return mod else: mod = utils.op_build_test(discontinous_mov.discontinous_mov, [shape1], [dtype], op_attrs, kernel_name='discontinous_mov', attrs=attrs) args, exp_output, input = gen_data(dtype, shape1, shape2) acu_output = utils.mod_launch(mod, args, expect=exp_output) # compare result TestCase_Result = compare_tensor(acu_output, exp_output, rtol=5e-03, equal_nan=True) return input, acu_output, exp_output, TestCase_Result
def sqrt_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(sqrt, [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: expect, input, output = gen_data(dtype, shape) mod = utils.op_build_test(sqrt, [shape], [dtype], kernel_name='sqrt', attrs=attrs) output = utils.mod_launch(mod, (input, output), expect=expect) if attrs.get("profiling", False): target_name = attrs["target"].split()[0] args_list = to_tvm_nd_array([input, output], akg.tvm.context(target_name, 0)) target_profiling(mod, *args_list, target=target_name, repeat_time=attrs["repeat_times"]) return input, output, expect, compare_tensor(output, expect, rtol=5e-03, equal_nan=True)
def space_to_batch_nd_run(input_shape, input_dtype, block, pad, kernel_name, attrs=None): op_attrs = [block, pad] attrs["pragma_disable_whole_component"] = False if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(space_to_batch_nd.space_to_batch_nd, [input_shape], [input_dtype], op_attrs, kernel_name=kernel_name, attrs=attrs, tuning=t) if t: data, expect, output = gen_data(block, input_dtype, input_shape, pad) return mod, expect, (data, output) else: return mod else: data, expect, output = gen_data(block, input_dtype, input_shape, pad) mod = utils.op_build_test(space_to_batch_nd.space_to_batch_nd, [input_shape], [input_dtype], op_attrs, kernel_name=kernel_name, attrs=attrs) output = utils.mod_launch(mod, (data, output), expect=expect) return data, output, expect, compare_tensor(output, expect, atol=0.01)
def matmul_ad_run(data_shape, weight_shape, dtype, attrs_op={}, cce_path="./", attrs={}): attrs.update(attrs_op) check_list = ["float16"] if not (dtype.lower() in check_list): raise RuntimeError("matmul test only support %s while dtype is %s" % (",".join(check_list), dtype)) mod = matmul_ad.matmul_ad(data_shape, weight_shape, dtype, attrs=attrs) input_data = random_gaussian(data_shape, miu=0.1, sigma=0.1) input_data = input_data.astype(np.float16) input_weight = random_gaussian(weight_shape, miu=0.1, sigma=0.1) input_weight = input_weight.astype(np.float16) expect = np.matmul(np.matmul(input_data, input_weight), np.transpose(input_weight)) output = np.full(data_shape, 1.0, dtype) output = utils.mod_launch( mod, (np.matmul(input_data, input_weight), input_weight, output), expect=expect) return (np.matmul(input_data, input_weight), input_weight), output, expect, compare_tensor(output, expect, atol=5e-01, rtol=5e-03, equal_nan=True)
def squeeze_ad_run(shape, axis, dtype, attrs): if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) input_np = np.random.uniform(low=-1.0, high=1.0, size=shape).astype(dtype) forward_output = np.squeeze(input_np, axis=axis) mod = utils.op_build_test(squeeze_ad, [forward_output.shape, shape], [dtype, dtype], kernel_name=kernel_name, op_attrs=[axis], attrs=attrs, tuning=t) if t: expect, head_np, output = gen_data(dtype, forward_output, shape) return mod, expect, (head_np, input_np, output) else: return mod else: input_np = np.random.uniform(low=-1.0, high=1.0, size=shape).astype(dtype) forward_output = np.squeeze(input_np, axis=axis) mod = utils.op_build_test(squeeze_ad, [forward_output.shape, shape], [dtype, dtype], kernel_name='squeeze_ad', op_attrs=[axis], attrs=attrs) expect, head_np, output = gen_data(dtype, forward_output, shape) output = utils.mod_launch(mod, (head_np, input_np, output), expect=expect) return (head_np, input_np, axis), output, expect, compare_tensor(output, expect, atol=0.1)
def add_ad_run(ashape, bshape, dtype, kernel_name="add", scale=1.0, attrs_op={}, polyhedral=True, attrs={}): if type(scale) is not float or not int: if type(attrs_op) is not bool: scale, attrs_op = 1.0, scale else: scale, attrs_op, polyhedral = 1.0, scale, attrs_op attrs.update(attrs_op) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) a = random_gaussian(ashape, miu=1, sigma=0.1).astype(dtype) b = random_gaussian(bshape, miu=1, sigma=0.1).astype(dtype) out = np.add(a, b * scale) mod = utils.op_build_test(add_ad, [out.shape, ashape, bshape], [dtype, dtype, dtype], kernel_name=kernel_name, op_attrs=[scale], attrs=attrs, polyhedral=polyhedral, tuning=t) if t: expect, head_np, output = gen_data(dtype, out) return mod, expect, (head_np, a, b, output) else: return mod else: a = random_gaussian(ashape, miu=1, sigma=0.1).astype(dtype) b = random_gaussian(bshape, miu=1, sigma=0.1).astype(dtype) out = np.add(a, b * scale) mod = utils.op_build_test(add_ad, [out.shape, ashape, bshape], [dtype, dtype, dtype], kernel_name='add_ad', op_attrs=[scale], attrs=attrs, polyhedral=polyhedral) expect, head_np, output = gen_data(dtype, out) output = utils.mod_launch(mod, (head_np, a, b, output), expect=expect) return (head_np, a, b), output, expect, compare_tensor(output, expect, atol=0.1)
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
def gather_run(params_shape, indices_shape, params_dtype, indices_dtype, axis, attrs): if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(Gather, [params_shape, indices_shape], [params_dtype, indices_dtype], [axis], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: params, indices, bench_mark, output_shape = gather_data( params_shape, indices_shape, params_dtype, indices_dtype) output = np.full(output_shape, np.nan, params_dtype) return mod, bench_mark, (params, indices, output) else: return mod else: mod = Gather(params_shape, indices_shape, params_dtype, indices_dtype, axis, "gather_cce", "./") params, indices, bench_mark, output_shape = gather_data( params_shape, indices_shape, params_dtype, indices_dtype) # mod launch output = np.full(output_shape, np.nan, params_dtype) output = utils.mod_launch(mod, (params, indices, output), expect=bench_mark) compare_res = compare_tensor(output, bench_mark, rtol=5e-03, equal_nan=True) print(" ========== PASS ============") return (params, indices), output, bench_mark, compare_res
def strided_slice_grad_execute(input_shape, begin, end, strides, begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, grad_shape, dtype, attrs=None): check_grad_shape(input_shape, begin, end, strides, begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, grad_shape, dtype) attrs["pragma_disable_whole_component"] = False if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = strided_slice_grad_compile(input_shape, begin, end, strides, begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, grad_shape, dtype, attrs=None, kernel_name=kernel_name, tuning=t) if t: expect, grad, output = gen_data(begin, begin_mask, dtype, ellipsis_mask, end, end_mask, grad_shape, input_shape, new_axis_mask, shrink_axis_mask, strides) return mod, expect, (grad, output) else: return mod else: mod = strided_slice_grad_compile(input_shape, begin, end, strides, begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, grad_shape, dtype, attrs) expect, grad, output = gen_data(begin, begin_mask, dtype, ellipsis_mask, end, end_mask, grad_shape, input_shape, new_axis_mask, shrink_axis_mask, strides) output = utils.mod_launch(mod, (grad, output), expect=expect) rtol, atol = get_rtol_atol("strided_slice_grad", dtype) return grad, output, expect, compare_tensor(output, expect, rtol=rtol, atol=atol, equal_nan=True)
def acos_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(acos_grad, [shape, shape], [dtype, dtype], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect, grad, inputs, output = gen_data(dtype, shape) return mod, expect, (inputs, grad, output) else: return mod else: mod = utils.op_build_test(acos_grad, [shape, shape], [dtype, dtype], kernel_name='acos_grad', attrs=attrs) expect, grad, inputs, output = gen_data(dtype, shape) output = utils.mod_launch(mod, (inputs, grad, output), expect=expect) # compare result TestCase_Result = compare_tensor(output, expect, rtol=5e-03, atol=1e-04, equal_nan=False) return (inputs, grad), output, expect, TestCase_Result
def upsampling_run(in_shape, out_shape, dtype, kernel_name, attrs): kernel_name = utils.gen_name_kernel(kernel_name, dtype, in_shape) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(upsampling.upsampling, input_shapes=[in_shape], input_types=[dtype], op_attrs=[out_shape], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect, input, output = gen_data(dtype, in_shape, out_shape) return mod, expect, (input, output) else: return mod else: # Create op mod = utils.op_build_test(upsampling.upsampling, input_shapes=[in_shape], input_types=[dtype], op_attrs=[out_shape], kernel_name=kernel_name, attrs=attrs) expect, input, output = gen_data(dtype, in_shape, out_shape) output = utils.mod_launch(mod, (input, output), expect=expect) return input, output, expect, compare_tensor(output, expect, atol=5e-01, rtol=5e-03, equal_nan=True)
def clip_run(shape, min_val, max_val, dtype, attrs): """clip_run""" if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(clip.clip, [shape], [dtype], kernel_name=kernel_name, op_attrs=[min_val, max_val], attrs=attrs, tuning=t) if t: exp_output, inputs, output = gen_data(dtype, max_val, min_val, shape) return mod, exp_output, (inputs, output) return mod else: # op_attrs=[shape, dtype] mod = utils.op_build_test(clip.clip, [shape], [dtype], kernel_name='clip', op_attrs=[min_val, max_val], attrs=attrs) exp_output, inputs, output = gen_data(dtype, max_val, min_val, shape) # result_tvm acu_output = utils.mod_launch(mod, (inputs, output), expect=exp_output) # compare result compare_result = compare_tensor(acu_output, exp_output, rtol=5e-03, equal_nan=True) return inputs, acu_output, exp_output, compare_result
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
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
def sgd_run(shape, dtype, nesterov=False, dampening=0.0, weight_decay=0.0, lr_mat=0.1, momt_mat=0.9, attrs=None): """run function for dsl function sgd.""" lr = np.full((1, ), lr_mat).astype(dtype) momt = np.full((1, ), momt_mat).astype(dtype) mod = utils.op_build_test( sgd.sgd, [shape, shape, shape, shape, lr.shape, momt.shape], [dtype, dtype, dtype, dtype, dtype, dtype], [dampening, weight_decay, nesterov], kernel_name='sgd', attrs=attrs) parameters, gradient, accum, stat, parameters_t, accum_t, stat_t, output_para, output_accum, output_stat \ = gen_data(dtype, shape, lr, momt, dampening, weight_decay, nesterov) output_para, output_accum, output_stat = utils.mod_launch( mod, (parameters, gradient, accum, stat, lr, momt), outputs=(0, 2, 3), expect=(parameters_t, accum_t, stat_t)) expects = (parameters_t, accum_t, stat_t) outputs = (output_para, output_accum, output_stat) rtol, atol = get_rtol_atol("sgd", dtype) testcase_result = compare_tensor(outputs, expects, rtol=rtol, atol=atol, equal_nan=True) return (parameters, gradient, accum, stat), (output_para, output_accum, output_stat), \ (parameters_t, accum_t, stat_t), testcase_result
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
def focal_loss_run(shape, p_dtype, t_dtype, gamma, kernel_name, attrs): attrs["pragma_disable_whole_component"] = False if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(focal_loss.focal_loss, [shape, shape], [p_dtype, t_dtype], op_attrs=[gamma], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect, pred, targ = gen_data(attrs, gamma, p_dtype, shape, t_dtype) output = np.full(expect.shape, 0.0, p_dtype) return mod, expect, (pred, targ, output) else: return mod else: mod = utils.op_build_test(focal_loss.focal_loss, [shape, shape], [p_dtype, t_dtype], op_attrs=[gamma], kernel_name=kernel_name, attrs=attrs) expect, pred, targ = gen_data(attrs, gamma, p_dtype, shape, t_dtype) output = np.full(expect.shape, 0.0, p_dtype) output = utils.mod_launch(mod, (pred, targ, output), expect=expect) return (pred, targ), output, expect, compare_tensor(output, expect, rtol=5e-2, atol=1e-4)