def scatter_nd_ad_run(indices_shape, data_shape, output_shape, indices_dtype, dtype, attrs): kernel_name = utils.gen_name_kernel("scatter_nd_ad", dtype, data_shape) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(scatter_nd_ad, [output_shape, indices_shape, data_shape], [dtype, indices_dtype, dtype], op_attrs=[output_shape], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: data_input, expect, head_input, indicies_input, output = gen_data(data_shape, dtype, indices_dtype, indices_shape, output_shape) return mod, expect, (head_input, indicies_input, data_input, output) else: return mod else: data_input, expect, head_input, indicies_input, output = gen_data(data_shape, dtype, indices_dtype, indices_shape, output_shape) mod = utils.op_build_test(scatter_nd_ad, [output_shape, indices_shape, data_shape], [dtype, indices_dtype, dtype], op_attrs=[output_shape], kernel_name=kernel_name, attrs=attrs) output = utils.mod_launch(mod, (head_input, indicies_input, data_input, output), expect=expect) return (head_input, indicies_input, data_input), output, expect, compare_tensor(output, expect, rtol=5e-03, equal_nan=True)
def focalloss_ad_run2(shape, dtype, attrs): logits_pld = akg.tvm.placeholder(shape, dtype=dtype, name='logits') labels_pld = akg.tvm.placeholder(shape, dtype='int32', name='labels') d_labels, d_logits, head = focalloss_ad.focalloss_ad( labels_pld, logits_pld) print("autodiff d_logits:\n", akg.tvm.PrintTensorRecursively(d_logits)) print("autodiff d_labels:\n", akg.tvm.PrintTensorRecursively(d_labels)) # build autodiff kernels io = [labels_pld, logits_pld, head, d_labels, d_logits] s = akg.tvm.create_schedule([e.op for e in io]) kernel_name = utils.gen_name_kernel("focalloss_ad", dtype, ( shape[0], shape[1], )) with akg.build_config(add_lower_pass=cce.debug_mode(0), dump_pass_ir=True): mod = akg.build(s, io, "cce", name=kernel_name, attrs=attrs, polyhedral=True) labels_np = RANGEFILL((batchsize, )) logits_np = RANGEFILL((batchsize, ), 2) head_np = RANGEFILL((batchsize, ), 2) output = np.full(expect.shape, np.nan, dtype) output = utils.mod_launch(mod, (labels_np, logits_np, head_np, output), expect=output) expect = output # hack return (input_np, head_np), output, expect, compare_tensor(output, expect, atol=0.1)
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 invert_permutation_run(shape, dtype, attrs): # check shapes vc_util.check_shape(shape) if not (dtype.lower() in "int32"): raise RuntimeError( "indices_dtype only support int32 while dtype is %s" % dtype) A = akg.tvm.placeholder(shape, dtype, name="A") op = invert_permutation.invert_permutation(A) s = akg.tvm.create_schedule(op.op) kernel_name = utils.gen_name_kernel("invert_permutation", dtype, shape) with akg.build_config(add_lower_pass=cce.debug_mode(0), dump_pass_ir=True): mod = akg.build(s, [A, op], "cce", name=kernel_name, attrs=attrs, polyhedral=True) input_data = np.random.permutation(np.arange(shape[0])).astype(np.int32) expect = np.full([shape[0]], 0, np.int32) for i, e in enumerate(input_data): expect[e] = i output = np.full([shape[0]], 0, np.int32) output = utils.mod_launch(mod, (input_data, output), expect=expect) return (input_data, ), output, expect, compare_tensor(output, expect, rtol=5e-03, equal_nan=True)
def case_1(data_shape, dtype, kernel_name, attrs): """elemwise chain case 1""" vc_util.ops_dtype_check(dtype, vc_util.DtypeForDavinci.FLOAT16) vc_util.check_shape_length_equal("data", data_shape, 2) m, k = data_shape A = akg.tvm.placeholder((m, k), name='A', dtype=dtype) B = akg.tvm.placeholder((k, ), name='B', dtype=dtype) C = akg.tvm.placeholder((m, k), name='C', dtype=dtype) E = akg.tvm.compute((m, k), lambda i, j: A[i, j] * (B[j] + C[i, j]), name="E") forward_s = akg.tvm.create_schedule(E.op) op_vars = [A, B, C, E] forward_low = akg.lower(forward_s, op_vars, simple_mode=True, polyhedral=True) kernel_name = utils.gen_name_kernel(kernel_name, dtype, data_shape) with akg.build_config(add_lower_pass=cce.debug_mode(0), dump_pass_ir=True): mod = akg.build(forward_s, op_vars, "cce", name="test", attrs=attrs, polyhedral=True) source_code = mod.imported_modules[0].get_source() return mod
def focalloss_grad_run(shape, dtype, label_dtype, gamma, attrs): kernel_name = utils.gen_name_kernel("focalloss_grad", dtype, shape) 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(focalloss_grad.focal_loss_bwd, input_shapes=[shape, shape, shape[:2]], input_types=[dtype, label_dtype, dtype], op_attrs=[gamma], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: d_logits, expect_logits, grad_np, labels_np, logits_np = gen_grad_data(attrs, dtype, gamma, label_dtype, shape) return mod, expect_logits, (logits_np, labels_np, grad_np, d_logits) else: return mod else: mod = utils.op_build_test(focalloss_grad.focal_loss_bwd, input_shapes=[shape, shape, shape[:2]], input_types=[dtype, label_dtype, dtype], op_attrs=[gamma], kernel_name=kernel_name, attrs=attrs) d_logits, expect_logits, grad_np, labels_np, logits_np = gen_grad_data(attrs, dtype, gamma, label_dtype, shape) d_logits = utils.mod_launch(mod, (logits_np, labels_np, grad_np, d_logits), expect=expect_logits) result_logits = compare_tensor(d_logits, expect_logits, rtol=2e-2, atol=1e-4) return (labels_np, logits_np, grad_np), d_logits, expect_logits, result_logits
def roipool(shape, roibox, pooled_shape, dtype, kernel_name="roipool_forward_output", attrs=None): check_list = ["float16"] if not (dtype.lower() in check_list): raise RuntimeError("tile_cce only support %s while dtype is %s" % (",".join(check_list), dtype)) vc_util.check_shape(shape) assert (len(shape) == 4) assert (len(roibox) == 4) assert (len(pooled_shape) == 2) a_n, a_c, a_h, a_w = shape roi_t, roi_b, roi_l, roi_r = roibox assert (roi_t >= 0 and roi_t < roi_b and roi_b < a_h) assert (roi_l >= 0 and roi_l < roi_r and roi_r < a_w) a = akg.tvm.placeholder(shape, name="a", dtype=dtype) Crop = akg.tvm.compute([a_n, a_c, roi_b - roi_t, roi_r - roi_l], lambda n, c, h, w: a[n, c, roi_t + h, roi_l + w]) p_h, p_w = pooled_shape win_h = (roi_b - roi_t) // p_h + (1 if (roi_b - roi_t) % p_h > 0 else 0) win_w = (roi_r - roi_l) // p_w + (1 if (roi_r - roi_l) % p_w > 0 else 0) assert p_h <= (roi_b - roi_t) and p_w <= (roi_r - roi_l) Unpooled = akg.tvm.compute( [a_n, a_c, p_h, p_w, win_h, win_w], lambda n, c, h, w, wh, ww: akg.tvm.expr.Select( akg.tvm.all(h * win_h + wh < roi_b - roi_t, w * win_w + ww < roi_r - roi_l), Crop[n, c, h * win_h + wh, w * win_w + ww], akg.tvm.const(0, a.dtype))) rh = akg.tvm.reduce_axis((0, win_h)) rw = akg.tvm.reduce_axis((0, win_w)) output_shape = [a_n, a_c, p_h, p_w] res = akg.tvm.compute( output_shape, lambda n, c, h, w: akg.tvm.max(Unpooled[n, c, h, w, rh, rw], axis=[rh, rw])) s = akg.tvm.create_schedule(res.op) s[Crop].compute_inline() s[Unpooled].compute_inline() kernel_name = utils.gen_name_kernel(kernel_name, dtype, shape) with akg.build_config(add_lower_pass=cce.debug_mode(0), dump_pass_ir=True): mod = akg.build(s, [a, res], "cce", name=kernel_name, attrs=attrs, polyhedral=True) return mod, output_shape
def encode_onehot_classes_run(gt_shape, anchor_shape, class_num, dtype, kernel_name, attrs): kernel_name = utils.gen_name_kernel(kernel_name, dtype, gt_shape) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(encode_onehot_classes.encode_onehot_classes, input_shapes=[gt_shape, anchor_shape], input_types=[dtype, dtype], op_attrs=[class_num], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: anchor_sample, expect, groundtruth_class, output = gen_data( anchor_shape, class_num, dtype, gt_shape) return mod, expect, (groundtruth_class, anchor_sample, output) else: return mod else: # Create op mod = utils.op_build_test(encode_onehot_classes.encode_onehot_classes, input_shapes=[gt_shape, anchor_shape], input_types=[dtype, dtype], op_attrs=[class_num], kernel_name=kernel_name, attrs=attrs) anchor_sample, expect, groundtruth_class, output = gen_data( anchor_shape, class_num, dtype, gt_shape) output = utils.mod_launch(mod, (groundtruth_class, anchor_sample, output), expect=expect) return (groundtruth_class, anchor_sample), output, expect, compare_tensor(output, expect, atol=5e-01, rtol=5e-03, equal_nan=True)
def resize_bilinear_grad_run(resized_shape, original_shape, dtype, kernel_name, attrs): kernel_name = utils.gen_name_kernel(kernel_name, dtype, resized_shape) original_data = akg.tvm.placeholder(original_shape, dtype) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(resize_bilinear_grad.resize_bilinear_grad, [resized_shape], [dtype], op_attrs=[original_data], kernel_name=kernel_name, attrs=attrs, tuning=t) if t: expect, input, output = gen_data(dtype, original_shape, resized_shape) return mod, expect, (input, output) else: return mod else: mod = utils.op_build_test(resize_bilinear_grad.resize_bilinear_grad, [resized_shape], [dtype], op_attrs=[original_data], kernel_name=kernel_name, attrs=attrs) expect, input, output = gen_data(dtype, original_shape, resized_shape) # auto-tensor output 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 sliceeven_run(shape, dtype, kernel_name="sliceeven_backward", attrs=None): kernel_name = utils.gen_name_kernel(kernel_name, dtype, shape) if 'tuning' in attrs.keys(): t = attrs.get("tuning", False) kernel_name = attrs.get("kernel_name", False) mod = utils.op_build_test(sliceeven.sliceeven, [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(sliceeven.sliceeven, [shape], [dtype], kernel_name=kernel_name, attrs=attrs) expect, input_, output = gen_data(dtype, shape) output = utils.mod_launch(mod, (input_, output), expect=expect) return (input_, ), output, expect, compare_tensor(output, expect, rtol=5e-03, equal_nan=True)