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.

utils.py 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright 2021 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. """utility functions for mindspore.numpy st tests"""
  16. import functools
  17. import numpy as onp
  18. import mindspore.numpy as mnp
  19. def match_array(actual, expected, error=0):
  20. if isinstance(actual, int):
  21. actual = onp.asarray(actual)
  22. if isinstance(expected, int):
  23. expected = onp.asarray(expected)
  24. if error > 0:
  25. onp.testing.assert_almost_equal(actual.tolist(), expected.tolist(),
  26. decimal=error)
  27. else:
  28. onp.testing.assert_equal(actual.tolist(), expected.tolist())
  29. def check_all_results(onp_results, mnp_results, error=0):
  30. """Check all results from numpy and mindspore.numpy"""
  31. for i, _ in enumerate(onp_results):
  32. match_array(onp_results[i], mnp_results[i].asnumpy())
  33. def check_all_unique_results(onp_results, mnp_results):
  34. """
  35. Check all results from numpy and mindspore.numpy.
  36. Args:
  37. onp_results (Union[tuple of numpy.arrays, numpy.array])
  38. mnp_results (Union[tuple of Tensors, Tensor])
  39. """
  40. for i, _ in enumerate(onp_results):
  41. if isinstance(onp_results[i], tuple):
  42. for j in range(len(onp_results[i])):
  43. match_array(onp_results[i][j],
  44. mnp_results[i][j].asnumpy(), error=7)
  45. else:
  46. match_array(onp_results[i], mnp_results[i].asnumpy(), error=7)
  47. def run_non_kw_test(mnp_fn, onp_fn, test_case):
  48. """Run tests on functions with non keyword arguments"""
  49. for i in range(len(test_case.arrs)):
  50. arrs = test_case.arrs[:i]
  51. match_res(mnp_fn, onp_fn, *arrs)
  52. for i in range(len(test_case.scalars)):
  53. arrs = test_case.scalars[:i]
  54. match_res(mnp_fn, onp_fn, *arrs)
  55. for i in range(len(test_case.expanded_arrs)):
  56. arrs = test_case.expanded_arrs[:i]
  57. match_res(mnp_fn, onp_fn, *arrs)
  58. for i in range(len(test_case.nested_arrs)):
  59. arrs = test_case.nested_arrs[:i]
  60. match_res(mnp_fn, onp_fn, *arrs)
  61. def rand_int(*shape):
  62. """return an random integer array with parameter shape"""
  63. res = onp.random.randint(low=1, high=5, size=shape)
  64. if isinstance(res, onp.ndarray):
  65. return res.astype(onp.float32)
  66. return float(res)
  67. # return an random boolean array
  68. def rand_bool(*shape):
  69. return onp.random.rand(*shape) > 0.5
  70. def match_res(mnp_fn, onp_fn, *arrs, **kwargs):
  71. """Checks results from applying mnp_fn and onp_fn on arrs respectively"""
  72. mnp_arrs = map(functools.partial(mnp.asarray, dtype='float32'), arrs)
  73. error = kwargs.get('error', 0)
  74. kwargs.pop('error', None)
  75. mnp_res = mnp_fn(*mnp_arrs, **kwargs)
  76. onp_res = onp_fn(*arrs, **kwargs)
  77. match_all_arrays(mnp_res, onp_res, error=error)
  78. def match_all_arrays(mnp_res, onp_res, error=0):
  79. if isinstance(mnp_res, (tuple, list)):
  80. assert len(mnp_res) == len(onp_res)
  81. for actual, expected in zip(mnp_res, onp_res):
  82. match_array(actual.asnumpy(), expected, error)
  83. else:
  84. match_array(mnp_res.asnumpy(), onp_res, error)
  85. def match_meta(actual, expected):
  86. # float64 and int64 are not supported, and the default type for
  87. # float and int are float32 and int32, respectively
  88. if expected.dtype == onp.float64:
  89. expected = expected.astype(onp.float32)
  90. elif expected.dtype == onp.int64:
  91. expected = expected.astype(onp.int32)
  92. assert actual.shape == expected.shape
  93. assert actual.dtype == expected.dtype
  94. def run_binop_test(mnp_fn, onp_fn, test_case, error=0):
  95. for arr in test_case.arrs:
  96. match_res(mnp_fn, onp_fn, arr, arr, error=error)
  97. for scalar in test_case.scalars:
  98. match_res(mnp_fn, onp_fn, arr, scalar, error=error)
  99. match_res(mnp_fn, onp_fn, scalar, arr, error=error)
  100. for scalar1 in test_case.scalars:
  101. for scalar2 in test_case.scalars:
  102. match_res(mnp_fn, onp_fn, scalar1, scalar2, error=error)
  103. for expanded_arr1 in test_case.expanded_arrs:
  104. for expanded_arr2 in test_case.expanded_arrs:
  105. match_res(mnp_fn, onp_fn, expanded_arr1, expanded_arr2, error=error)
  106. for broadcastable1 in test_case.broadcastables:
  107. for broadcastable2 in test_case.broadcastables:
  108. match_res(mnp_fn, onp_fn, broadcastable1, broadcastable2, error=error)
  109. def run_unary_test(mnp_fn, onp_fn, test_case, error=0):
  110. for arr in test_case.arrs:
  111. match_res(mnp_fn, onp_fn, arr, error=error)
  112. for arr in test_case.scalars:
  113. match_res(mnp_fn, onp_fn, arr, error=error)
  114. for arr in test_case.expanded_arrs:
  115. match_res(mnp_fn, onp_fn, arr, error=error)
  116. def run_multi_test(mnp_fn, onp_fn, arrs, error=0):
  117. mnp_arrs = map(mnp.asarray, arrs)
  118. for actual, expected in zip(mnp_fn(*mnp_arrs), onp_fn(*arrs)):
  119. match_array(actual.asnumpy(), expected, error)
  120. def run_single_test(mnp_fn, onp_fn, arr, error=0):
  121. mnp_arr = mnp.asarray(arr)
  122. for actual, expected in zip(mnp_fn(mnp_arr), onp_fn(arr)):
  123. if isinstance(expected, tuple):
  124. for actual_arr, expected_arr in zip(actual, expected):
  125. match_array(actual_arr.asnumpy(), expected_arr, error)
  126. match_array(actual.asnumpy(), expected, error)