You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gen_random.py 4.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """generate gaussian random array"""
  15. import numpy as np
  16. import os
  17. import random
  18. import logging
  19. import sys
  20. import time
  21. import functools
  22. from multiprocessing import Pool
  23. from itertools import repeat
  24. from akg.utils.kernel_exec import func_time_required
  25. from akg.utils.kernel_exec import get_profiling_mode
  26. RANDOM_SEED_NUM = 20
  27. PROF_ERROR_CODE = 9999999999
  28. def func(size_, miu_=0, sigma_=8, seed_=None):
  29. """
  30. Select random func according to RANDOM_FUNC_MODE and randint, calculated by the length of the random_func_list.
  31. Args:
  32. size_ (int): Size of data.
  33. miu_ (int): Mean value. Default: 0.
  34. sigma_ (int): Standard deviation. Default: 8.
  35. seed_ (int): seed for random.
  36. Returns:
  37. Random func, from random_func_list.
  38. """
  39. size_ = (size_ + RANDOM_SEED_NUM - 1) // RANDOM_SEED_NUM
  40. random_func_list = [
  41. np.random.RandomState(seed_).normal(miu_, sigma_, size_),
  42. np.random.RandomState(seed_).logistic(miu_, sigma_, size_),
  43. np.random.RandomState(seed_).laplace(miu_, sigma_, size_),
  44. np.random.RandomState(seed_).uniform(miu_, sigma_, size_),
  45. np.random.RandomState(seed_).tomaxint(size_),
  46. ]
  47. env_dic = os.environ
  48. if not env_dic.get('RANDOM_FUNC_MODE'):
  49. func_idx = 0
  50. else:
  51. func_idx = np.random.RandomState(None).randint(len(random_func_list))
  52. res = random_func_list[func_idx]
  53. return res
  54. @func_time_required
  55. def random_gaussian(size, miu=0, sigma=8, epsilon=0, seed=None):
  56. """Generate random array with absolution value obeys gaussian distribution."""
  57. random_data_disk_path = None
  58. if os.environ.get("RANDOM_DATA_DISK_PATH") is not None:
  59. random_data_disk_path = os.environ.get("RANDOM_DATA_DISK_PATH") + "/random_data_%s_%s.bin" % (str(miu), str(sigma))
  60. if random_data_disk_path is None or (not os.path.exists(random_data_disk_path)):
  61. if sigma <= 0:
  62. sys.stderr.write("Error: Expect positive sigmal for gaussian distribution. but get %f\n" % sigma)
  63. sys.exit(1)
  64. size_c = 1
  65. for x in size:
  66. size_c = size_c * x
  67. if seed is None:
  68. seed_ = []
  69. for i in range(RANDOM_SEED_NUM):
  70. now = int(time.time() % 10000 * 10000) + random.randint(i, 100)
  71. seed_.append(now)
  72. else:
  73. seed_ = [seed] * RANDOM_SEED_NUM
  74. logging.debug("random_gaussian seeds: {}".format(seed_))
  75. # In the profiling scenario, when a new process is used to run test cases, data generated by multiple processes
  76. # stops responding. To locate the fault, please set this parameter gen_data_multi_process to False.
  77. gen_data_multi_process = not bool(get_profiling_mode())
  78. if gen_data_multi_process:
  79. with Pool(processes=8) as pool:
  80. ret = np.array(pool.starmap(func, zip(repeat(size_c), repeat(miu), repeat(sigma), seed_)))
  81. else:
  82. numbers = list()
  83. for s in seed_:
  84. numbers.extend(func(size_c, miu, sigma, s))
  85. ret = np.array(numbers)
  86. ret = ret.flatten()
  87. return ret[:size_c].reshape(size) + epsilon
  88. data_len = functools.reduce(lambda x, y: x * y, size)
  89. data_pool = np.fromfile(random_data_disk_path)
  90. if data_len % len(data_pool) != 0:
  91. copy_num = (data_len // len(data_pool)) + 1
  92. else:
  93. copy_num = data_len // len(data_pool)
  94. data_copy = np.copy(data_pool)
  95. data_copy_list = []
  96. for _ in range(copy_num):
  97. np.random.shuffle(data_copy)
  98. data_copy_list.append(data_copy)
  99. data_pool = np.concatenate(tuple(data_copy_list), axis=0)
  100. return data_pool[0:data_len].reshape(size) + epsilon
  101. def gen_epsilon(dtype):
  102. """Generate suggested epsilon according to data type."""
  103. return 1e-7 if dtype == np.float32 else 1e-3

AKG(Auto Kernel Generator)对深度神经网络中的算子进行优化,并提供特定模式下的算子自动融合功能。AKG与MindSpore的图算融合功能协同工作,可提升在不同硬件后端上运行网络的性能。