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.

tensorio.py 3.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. """
  15. Data dump load operation
  16. """
  17. import os
  18. import struct
  19. import numpy as np
  20. from tests.common.log import Log
  21. from tests.common.gen_random import random_gaussian
  22. def dump_tensor(tensor, file_path):
  23. rank = len(tensor.shape)
  24. t = (rank,) + tensor.shape
  25. desc = struct.pack('I%dI' % rank, *t)
  26. with open(file_path, 'wb') as f:
  27. f.write(desc)
  28. f.write(tensor.tobytes())
  29. def load_tensor(file_path, dtype=None):
  30. with open(file_path, 'rb') as f:
  31. content = f.read()
  32. rank = struct.unpack_from('I', content, 0)[0]
  33. shape = struct.unpack_from('%dI' % rank, content, 4)
  34. data = np.frombuffer(
  35. content[4 + rank * 4:], dtype=dtype).reshape(shape)
  36. return rank, shape, data
  37. def compare_tensor(acu_output, exp_output, rtol=1.e-5, atol=1.e-8, equal_nan=False):
  38. """
  39. Output and expected result comparison method
  40. :param acu_output: array_like Input arrays to compare.
  41. :param exp_output: array_like Input arrays to compare.
  42. :param rtol: float The relative tolerance parameter (see Notes).
  43. :param atol: float The absolute tolerance parameter (see Notes).
  44. :param equal_nan: bool
  45. Whether to compare NaN's as equal. If True, NaN's in `a` will be
  46. considered equal to NaN's in `b` in the output array.
  47. :return: True / False
  48. """
  49. res = np.allclose(acu_output, exp_output, rtol, atol, equal_nan)
  50. if not res:
  51. pandora_logger_ = Log(case_name=os.path.dirname(__file__), case_path=os.getcwd())
  52. pandora_logger_.log.error("This shape precision is not up to standard, compare failed.")
  53. return res
  54. def random_data_to_disk(size, miu=None, sigma=None, seed=None, random_data_disk_path=None):
  55. """
  56. Generate local disk data
  57. :param size: Generate disk data size
  58. :param miu: Average value
  59. :param sigma: Standard deviation
  60. :param seed: Seed of random number
  61. :param random_data_disk_path: Specify the disk data save path
  62. :return:
  63. """
  64. if miu is None or sigma is None:
  65. miu_sigma_list = [[1, 0.1]]
  66. else:
  67. miu_sigma_list = []
  68. for i in miu:
  69. for j in sigma:
  70. miu_sigma_list.append([i, j])
  71. for miu_sigma in miu_sigma_list:
  72. random_data = size // 8
  73. random_data = random_gaussian(tuple([random_data]), miu=miu_sigma[0], sigma=miu_sigma[1], seed=seed)
  74. if random_data_disk_path is None:
  75. random_data_disk_path = os.environ.get("RANDOM_DATA_DISK_PATH")
  76. if random_data_disk_path is None:
  77. raise ValueError("Environment variable is missing from the current environment RANDOM_DATA_DISK_PATH "
  78. ": {0}".format(random_data_disk_path))
  79. data_path = random_data_disk_path + "/random_data_%s_%s.bin" % (str(miu_sigma[0]), str(miu_sigma[1]))
  80. with open(data_path, "w+") as file:
  81. random_data.tofile(file)
  82. file.close()

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