diff --git a/tests/common/gen_random.py b/tests/common/gen_random.py index 260fbc6f21b727f6b91d8df8f1b5e7a7563f8e9b..978ce0c1d9c6320d13c71a461d297c6dc4f9f20c 100644 --- a/tests/common/gen_random.py +++ b/tests/common/gen_random.py @@ -28,6 +28,8 @@ from akg.utils.kernel_exec import get_profiling_mode RANDOM_SEED_NUM = 20 PROF_ERROR_CODE = 9999999999 + + def func(size_, miu_=0, sigma_=8, seed_=None): """ Select random func according to RANDOM_FUNC_MODE and randint, calculated by the length of the random_func_list. @@ -59,7 +61,7 @@ def func(size_, miu_=0, sigma_=8, seed_=None): @func_time_required -def random_gaussian(size, miu=0, sigma=8, seed=None): +def random_gaussian(size, miu=0, sigma=8, epsilon=0, seed=None): """Generate random array with absolution value obeys gaussian distribution.""" random_data_disk_path = None if os.environ.get("RANDOM_DATA_DISK_PATH") is not None: @@ -93,7 +95,7 @@ def random_gaussian(size, miu=0, sigma=8, seed=None): numbers.extend(func(size_c, miu, sigma, s)) ret = np.array(numbers) ret = ret.flatten() - return ret[:size_c].reshape(size) + return ret[:size_c].reshape(size) + epsilon data_len = functools.reduce(lambda x, y: x * y, size) data_pool = np.fromfile(random_data_disk_path) @@ -107,4 +109,8 @@ def random_gaussian(size, miu=0, sigma=8, seed=None): np.random.shuffle(data_copy) data_copy_list.append(data_copy) data_pool = np.concatenate(tuple(data_copy_list), axis=0) - return data_pool[0:data_len].reshape(size) \ No newline at end of file + return data_pool[0:data_len].reshape(size) + epsilon + +def gen_epsilon(dtype): + """Generate suggested epsilon according to data type.""" + return 1e-7 if dtype == np.float32 else 1e-3 diff --git a/tests/common/test_run/distr_normal_diag_KLdiv_ad_run.py b/tests/common/test_run/distr_normal_diag_KLdiv_ad_run.py index ff97c30c5c6d34df9c0f59496b5bbef86b94ed06..a20d40970caee42543e5791201fb1964857e625d 100644 --- a/tests/common/test_run/distr_normal_diag_KLdiv_ad_run.py +++ b/tests/common/test_run/distr_normal_diag_KLdiv_ad_run.py @@ -16,7 +16,7 @@ import numpy as np from tensorio import compare_tensor from akg.utils import kernel_exec as utils from test_op.prob_program import distr_normal_diag_KLdiv_ad -from gen_random import random_gaussian +from gen_random import random_gaussian, gen_epsilon from base import get_rtol_atol @@ -40,9 +40,8 @@ def gen_data(dtype, shape): support_list = {"float16": np.float16, "float32": np.float32} m, k = shape - mean = random_gaussian((m, k), miu=1, sigma=0.1).astype(support_list[dtype]) - scale = random_gaussian((m, k), miu=1, sigma=0.1).astype(support_list[dtype]) + scale = random_gaussian((m, k), miu=1, sigma=0.1, epsilon=gen_epsilon(dtype)).astype(support_list[dtype]) head = random_gaussian((m, ), miu=1, sigma=0.1).astype(support_list[dtype]) output1 = np.full((m, k), 0.0, dtype)