def gen_data(dtype, reduction, shape): # support_list = {"float16": np.float16, "float32": np.float32} target = random_gaussian(shape, miu=0, sigma=1).astype(dtype) target = np.abs(target) prediction = random_gaussian(shape, miu=0, sigma=1).astype(dtype) prediction = np.abs(prediction) # off_set = np.full(prediction.shape, 0.05, dtype) off_set = np.full(prediction.shape, 2, dtype) prediction = np.add(prediction, off_set) target = np.add(target, off_set) pre_log = np.log(prediction).astype(dtype) tar_log = np.log(target).astype(dtype) sub_log = np.subtract(tar_log, pre_log) expect = np.multiply(target, sub_log).astype(dtype) if reduction == 'sum': expect = np.sum(expect) if reduction == 'mean': expect = np.mean(expect) if reduction == 'batchmean': reduce_axis = tuple(np.arange(1, len(shape))) expect = np.mean(expect, axis=reduce_axis, keepdims=False) if reduction == 'sum' or reduction == 'mean': out_shape = [] out_shape.append(1) output = np.full(out_shape, 0, dtype) if reduction == 'batchmean': reduce_axis = tuple(np.arange(1, len(shape))) out_shape = get_reduce_out_shape(shape, axis=reduce_axis, keepdims=False) output = np.full(expect.shape, np.nan, dtype) if reduction == 'none': output = np.full(expect.shape, np.nan, dtype) return expect, output, prediction, target
def gen_data(dtype, keepdims, reduce_axis, shape): input1 = random_gaussian(shape, miu=1, sigma=0.1) input1 = input1.astype(dtype) input1_square = np.square(input1) expect = np.mean(input1_square, axis=reduce_axis, keepdims=keepdims) out_shape = get_reduce_out_shape(shape, axis=reduce_axis, keepdims=keepdims) output = np.full(out_shape, np.nan, dtype) return expect, input1, output
def gen_data(dtype, shape, axis, keepdims): input_np = random_gaussian(shape, miu=1, sigma=0.5).astype(dtype) exp_input = np.exp(input_np) sumexp_input = np.sum(exp_input, axis=axis, keepdims=keepdims) logsumexp_input = np.log(sumexp_input) out_shape = get_reduce_out_shape(shape, axis=axis, keepdims=keepdims) output = np.full(out_shape, np.nan, dtype) return logsumexp_input, input_np, output
def gen_data(axis, dtype, keepdims, shape): """Generates input, output and expect data.""" inputs = random_gaussian( shape, miu=0, sigma=100.0).astype("float16").astype(dtype.lower()) expect = np.amin(inputs, axis=axis, keepdims=keepdims) out_shape = get_reduce_out_shape(shape, axis=axis, keepdims=keepdims) output = np.full(out_shape, np.nan, dtype) return expect, inputs, output
def gen_data(dtype, shape, axis, keepdims): # generate data for test x = np.abs(np.random.uniform(low=-127, high=128, size=tuple(shape)).astype(dtype)) exp_output = np.amax(x, axis=axis, keepdims=keepdims) out_shape = get_reduce_out_shape(shape, axis=axis, keepdims=keepdims) # inputs and output to hold the data output = np.full(out_shape, np.nan, dtype) args = [x, output] return args, exp_output, x
def gen_data(dtype, shape, axis, keepdims): input_np = random_gaussian(shape, miu=1, sigma=0.5).astype(dtype) out_shape = get_reduce_out_shape(shape, axis=axis, keepdims=keepdims) head_np = random_gaussian(out_shape, miu=1, sigma=0.5).astype(dtype) reduce_logsumexp_grad = reduce_logsumexp_ad_benchmark(input_np, axis, keepdims) expect = reduce_logsumexp_grad * head_np output = np.full(expect.shape, np.nan, dtype) return expect, head_np, input_np, output
def gen_data(axis, dtype, keepdims, shape): support_list = {"float16": np.float16, "float32": np.float32} input_ = random_gaussian(shape, miu=0.05, sigma=0.1).astype(support_list[dtype]) if dtype == "float16": expect = akg_fp16_mean(input_, axis=axis, keepdims=keepdims) else: expect = np.mean(input_, axis=axis, keepdims=keepdims) out_shape = get_reduce_out_shape(shape, axis=axis, keepdims=keepdims) output = np.full(out_shape, 0, dtype) return expect, input_, output
def gen_data(axis, dtype, keepdims, shape): """Generates input, output and expect data.""" inputs = random_gaussian( shape, miu=0, sigma=100.0).astype("float16").astype(dtype.lower()) expect = np.amin(inputs, axis=axis, keepdims=keepdims) if axis == None and keepdims == False: expect = np.broadcast_to(expect, (1, )) out_shape = get_reduce_out_shape(shape, axis=axis, keepdims=keepdims) output = np.full(out_shape, 3.402823e38, dtype) return expect, inputs, output
def gen_data(axis, dtype, method, shape): support_list = {"float16": np.float16, "float32": np.float32, "int32": np.int32, "int8": np.int8} input = random_gaussian(shape, miu=1, sigma=100).astype(support_list[dtype]) if dtype == "float32": input = np.around(input, 0) if method is "min": exp_output = np.argmin(input, axis=axis) elif method is "max": exp_output = np.argmax(input, axis=axis) else: raise RuntimeError("not support " + method) out_shape = get_reduce_out_shape(shape, axis=axis) output = np.full(out_shape, np.nan, np.int32) args = [input, output] return args, exp_output, input
def gen_data(dtype, keepdims, reduce_axis, shape): support_list = {"float16": np.float16, "float32": np.float32} input1 = random_gaussian(shape, miu=1, sigma=0.1) input1 = input1.astype(support_list[dtype]) if dtype == 'float16': expect = np_bisect_sum(input1, axis=reduce_axis, keepdims=keepdims) else: expect = np.sum(input1, axis=reduce_axis, keepdims=keepdims) out_shape = get_reduce_out_shape(shape, axis=reduce_axis, keepdims=keepdims) # if dump_data: # with open('input1.bin', 'wb') as fo: # fo.write(input1.astype(np.float16, copy=False)) # with open('output.bin', 'wb') as fo: # fo.write(benchMark.astype(np.float16, copy=False)) output = np.full(out_shape, np.nan, dtype) return expect, input1, output
def common(data, axis, method="min"): """ Returns the index with the max or min value across axes of a tensor. Note: method can be "max" or "min" to get argmax or argmin. Args: data (tvm.tensor.Tensor): Tensor of type float16, float32, int8, int32. axis (int): Describe the axis of input tensor. method (str): Can be "max" or "min". Returns: tvm.tensor.Tensor, has type of int32. """ shape = get_shape(data) dtype = data.dtype utils.ops_dtype_check( data.dtype, [utils.DtypeForDavinci.ALL_FLOAT, utils.DtypeForDavinci.ALL_INT]) utils.reduce_axis_check(shape, axis) real_axis = refine_reduce_axis(shape, axis)[0] out_shape = get_reduce_out_shape(shape, axis=axis) attr_map = {} if shape_is_dynamic(data): attr_map["dynamic_shape"] = set_dynamic_shape_limit_for_tensor( data, 4096, real_axis) if dtype != "float16": data = akg.topi.cast(data, "float16") k = akg.tvm.reduce_axis((0, data.shape[real_axis]), "k") if axis in (len(shape) - 1, -1): if method == "min": reducer = akg.tvm.comm_reducer(lambda x, y: dav.fargmin(x, y), lambda t: akg.tvm.max_value(t)) elif method == "max": reducer = akg.tvm.comm_reducer(lambda x, y: dav.fargmax(x, y), lambda t: akg.tvm.min_value(t)) else: raise ValueError("not support {}".format(method)) if len(data.shape) == 1: res = akg.tvm.compute((1, ), lambda i: reducer(data[k], axis=k)) else: res = akg.tvm.compute( out_shape, lambda *indice: reducer(data(*indice, k), axis=k)) res = akg.tvm.compute(out_shape, lambda *indice: res(*indice).astype("int32"), "argred_output") elif axis in (0, -len(shape)): tmp_idx = akg.tvm.compute( shape[1:], lambda *indice: akg.tvm.const(0.0, "float16"), name='tmp_index') local_data = akg.tvm.compute(shape[1:], lambda *indice: data(0, *indice), name="tmp_data") for idx in range(shape[axis] - 1): if method == 'min': tmp_idx = akg.tvm.compute( shape[1:], lambda *indice, ite_idx=idx: akg.tvm.expr.Select( local_data(*indice) > data(ite_idx + 1, *indice), akg.tvm.const(ite_idx + 1, "float16"), tmp_idx(*indice) )) local_data = akg.tvm.compute( shape[1:], lambda *indice, ite_idx=idx: akg.tvm.expr.Select( local_data(*indice) > data(ite_idx + 1, *indice), data(ite_idx + 1, *indice), local_data(*indice))) elif method == "max": tmp_idx = akg.tvm.compute( shape[1:], lambda *indice, ite_idx=idx: akg.tvm.expr.Select( local_data(*indice) < data(ite_idx + 1, *indice), akg.tvm.const(ite_idx + 1, "float16"), tmp_idx(*indice) )) local_data = akg.tvm.compute( shape[1:], lambda *indice, ite_idx=idx: akg.tvm.expr.Select( local_data(*indice) < data(ite_idx + 1, *indice), data(ite_idx + 1, *indice), local_data(*indice))) else: raise ValueError("not support " + method) res = akg.tvm.compute(out_shape, lambda *indice: tmp_idx(*indice).astype("int32"), "cast1") else: raise ValueError( "Argmax only support first axis and is last axis now!") lager = out_shape if len(out_shape) > len(shape) else shape strategy = argminmax_tiling_strategy(lager, real_axis) if strategy: attr_map["custom_tiling"] = strategy return res, attr_map