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.

test_cache_ops.py 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # Copyright 2020 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. import math
  16. import numpy as np
  17. import pytest
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. from mindspore import Parameter
  22. from mindspore.ops import operations as P
  23. context.set_context(mode=context.GRAPH_MODE,
  24. device_target='CPU', save_graphs=True)
  25. def hash_func(key, length):
  26. return (int)(((0.6180339 * key) - math.floor(0.6180339 * key)) * length)
  27. def init_hashmap(hash_map_length):
  28. key_np = np.array([2, 3, 10, 15, 21], np.int32)
  29. value_np = np.array([1, 3, 5, 7, 9], np.int32)
  30. NULLTAG = 0
  31. INIT_STEP = -5
  32. hashmap_np = np.zeros((hash_map_length, 4), np.int32)
  33. for i, key in enumerate(key_np):
  34. entry = hash_func(key, hash_map_length)
  35. count = 1
  36. while (hashmap_np[entry, 3] != NULLTAG and hashmap_np[entry, 0] != key):
  37. count += 1
  38. entry = (entry + 1) % hash_map_length
  39. if (hashmap_np[entry, 3] == NULLTAG):
  40. hashmap_np[entry] = [key, value_np[i], INIT_STEP, count]
  41. return hashmap_np
  42. class SearchCacheIdxNet(nn.Cell):
  43. def __init__(self, hashmap_np):
  44. super().__init__()
  45. self.ops = P.SearchCacheIdx()
  46. self.hashmap = Parameter(Tensor(hashmap_np), name="hashmap")
  47. self.emb_max = 25
  48. self.cache_max = 10
  49. self.step = 0
  50. def construct(self, indices):
  51. return self.ops(self.hashmap, indices, self.step, self.emb_max, self.cache_max)
  52. class CacheSwapHashmapNet(nn.Cell):
  53. def __init__(self, hashmap_np):
  54. super().__init__()
  55. self.net = SearchCacheIdxNet(hashmap_np)
  56. self.ops = P.CacheSwapHashmap()
  57. self.step = 0
  58. self.emb_max = 25
  59. self.cache_max = 10
  60. def construct(self, indices):
  61. _, _, miss_emb_idx = self.net(indices)
  62. return self.ops(self.net.hashmap, miss_emb_idx, self.step)
  63. class MapCacheIdxNet(nn.Cell):
  64. def __init__(self, hashmap_np):
  65. super().__init__()
  66. self.ops = P.MapCacheIdx()
  67. self.hashmap = Parameter(Tensor(hashmap_np), name="hashmap")
  68. self.emb_max = 25
  69. self.cache_max = 10
  70. self.step = 0
  71. def construct(self, indices):
  72. return self.ops(self.hashmap, indices, self.step, self.emb_max, self.cache_max)
  73. class UpdateCacheNet(nn.Cell):
  74. def __init__(self, x):
  75. super().__init__()
  76. self.ops = P.UpdateCache()
  77. self.max_num = 9999
  78. self.x = Parameter(Tensor(x), name='x')
  79. def construct(self, indices, update):
  80. return self.ops(self.x, indices, update, self.max_num)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_cpu
  83. @pytest.mark.env_onecard
  84. def test_search_cache_idx():
  85. hashmap_np = init_hashmap(10)
  86. indices_np = np.array([10, 2, 20, 5, 3], np.int32)
  87. search_cache_idx = SearchCacheIdxNet(hashmap_np)
  88. indices = Tensor(indices_np)
  89. cache_idx, miss_idx, miss_emb_idx = search_cache_idx(indices)
  90. expect_cache_idx = [5, 1, -1, -1, 3]
  91. expect_miss_idx = [-1, -1, 2, 3, -1]
  92. expect_miss_emb_idx = [-1, -1, 20, 5, -1]
  93. hashmap_np_after_ops = [[0, 0, 0, 0],
  94. [10, 5, 0, 1],
  95. [2, 1, 0, 1],
  96. [15, 7, -5, 2],
  97. [0, 0, 0, 0],
  98. [0, 0, 0, 0],
  99. [0, 0, 0, 0],
  100. [0, 0, 0, 0],
  101. [3, 3, 0, 1],
  102. [21, 9, -5, 1]]
  103. assert np.allclose(cache_idx.asnumpy(),
  104. np.array(expect_cache_idx, np.int32))
  105. assert np.allclose(miss_idx.asnumpy(), np.array(expect_miss_idx, np.int32))
  106. assert np.allclose(miss_emb_idx.asnumpy(),
  107. np.array(expect_miss_emb_idx, np.int32))
  108. assert np.allclose(search_cache_idx.hashmap.data.asnumpy(),
  109. np.array(hashmap_np_after_ops, np.int32))
  110. @pytest.mark.level0
  111. @pytest.mark.platform_x86_cpu
  112. @pytest.mark.env_onecard
  113. def test_cache_swap_hashmap():
  114. hashmap_np = init_hashmap(10)
  115. indices_np = np.array([10, 2, 20, 5, 3], np.int32)
  116. net = CacheSwapHashmapNet(hashmap_np)
  117. indices = Tensor(indices_np)
  118. swap_cache_idx, old_emb_idx = net(indices)
  119. expect_swap_cache_idx = [-1, -1, 9, 7, -1]
  120. expect_old_emb_idx = [-1, -1, 21, 15, -1]
  121. hashmap_np_after_ops = [[5, 7, 0, 1],
  122. [10, 5, 0, 1],
  123. [2, 1, 0, 1],
  124. [20, 9, 0, 1],
  125. [20, 9, 0, 0],
  126. [0, 0, 0, 0],
  127. [0, 0, 0, 0],
  128. [0, 0, 0, 0],
  129. [3, 3, 0, 1],
  130. [21, 9, -5, 0]]
  131. assert np.allclose(swap_cache_idx.asnumpy(),
  132. np.array(expect_swap_cache_idx, np.int32))
  133. assert np.allclose(old_emb_idx.asnumpy(),
  134. np.array(expect_old_emb_idx, np.int32))
  135. assert np.allclose(net.net.hashmap.data.asnumpy(),
  136. np.array(hashmap_np_after_ops, np.int32))
  137. @pytest.mark.level0
  138. @pytest.mark.platform_x86_cpu
  139. @pytest.mark.env_onecard
  140. def test_map_cache_idx():
  141. hashmap_np = init_hashmap(10)
  142. indices_np = np.array([10, 2, 20, 5, 3], np.int32)
  143. map_cache_idx = MapCacheIdxNet(hashmap_np)
  144. indices = Tensor(indices_np)
  145. cache_idx, old_emb_idx, miss_emb_idx, swap_cache_idx = map_cache_idx(
  146. indices)
  147. expect_cache_idx = [5, 1, 9, 7, 3]
  148. expect_old_emb_idx = [-1, -1, 21, 15, -1]
  149. expect_miss_emb_idx = [-1, -1, 20, 5, -1]
  150. expect_swap_cache_idx = [-1, -1, 9, 7, -1]
  151. hashmap_np_after_ops = [[5, 7, 0, 1],
  152. [10, 5, 0, 1],
  153. [2, 1, 0, 1],
  154. [20, 9, 0, 1],
  155. [20, 9, 0, 0],
  156. [0, 0, 0, 0],
  157. [0, 0, 0, 0],
  158. [0, 0, 0, 0],
  159. [3, 3, 0, 1],
  160. [21, 9, -5, 0]]
  161. assert np.allclose(cache_idx.asnumpy(),
  162. np.array(expect_cache_idx, np.int32))
  163. assert np.allclose(old_emb_idx.asnumpy(),
  164. np.array(expect_old_emb_idx, np.int32))
  165. assert np.allclose(miss_emb_idx.asnumpy(),
  166. np.array(expect_miss_emb_idx, np.int32))
  167. assert np.allclose(swap_cache_idx.asnumpy(),
  168. np.array(expect_swap_cache_idx, np.int32))
  169. assert np.allclose(map_cache_idx.hashmap.data.asnumpy(),
  170. np.array(hashmap_np_after_ops, np.int32))
  171. @pytest.mark.level0
  172. @pytest.mark.platform_x86_cpu
  173. @pytest.mark.env_onecard
  174. def test_update_cache():
  175. x_np = np.array([[2, 3, 4, 5],
  176. [6, 7, 8, 9],
  177. [11, 12, 13, 14],
  178. [1, 2, 3, 4],
  179. [5, 6, 7, 8]], np.int32)
  180. indices_np = np.array([[-1, 3, 4]], np.int32)
  181. update_np = np.array([[0, 0, 0, 0],
  182. [23, 34, 56, 78],
  183. [44, 55, 66, 77]], np.int32)
  184. indices = Tensor(indices_np)
  185. update = Tensor(update_np)
  186. expect = np.array([[2, 3, 4, 5],
  187. [6, 7, 8, 9],
  188. [11, 12, 13, 14],
  189. [23, 34, 56, 78],
  190. [44, 55, 66, 77]], np.int32)
  191. net = UpdateCacheNet(x_np)
  192. out = net(indices, update)
  193. assert np.allclose(net.x.data.asnumpy(), expect)
  194. assert np.allclose(out.asnumpy(), np.array([0], np.int32))