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 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 UpdateCacheNet(nn.Cell):
  64. def __init__(self, x):
  65. super().__init__()
  66. self.ops = P.UpdateCache()
  67. self.max_num = 9999
  68. self.x = Parameter(Tensor(x), name='x')
  69. def construct(self, indices, update):
  70. return self.ops(self.x, indices, update, self.max_num)
  71. @pytest.mark.level0
  72. @pytest.mark.platform_x86_cpu
  73. @pytest.mark.env_onecard
  74. def test_search_cache_idx():
  75. hashmap_np = init_hashmap(10)
  76. indices_np = np.array([10, 2, 20, 5, 3], np.int32)
  77. search_cache_idx = SearchCacheIdxNet(hashmap_np)
  78. indices = Tensor(indices_np)
  79. cache_idx, miss_idx, miss_emb_idx = search_cache_idx(indices)
  80. expect_cache_idx = [5, 1, -1, -1, 3]
  81. expect_miss_idx = [-1, -1, 2, 3, -1]
  82. expect_miss_emb_idx = [-1, -1, 20, 5, -1]
  83. hashmap_np_after_ops = [[0, 0, 0, 0],
  84. [10, 5, 0, 1],
  85. [2, 1, 0, 1],
  86. [15, 7, -5, 2],
  87. [0, 0, 0, 0],
  88. [0, 0, 0, 0],
  89. [0, 0, 0, 0],
  90. [0, 0, 0, 0],
  91. [3, 3, 0, 1],
  92. [21, 9, -5, 1]]
  93. assert np.allclose(cache_idx.asnumpy(),
  94. np.array(expect_cache_idx, np.int32))
  95. assert np.allclose(miss_idx.asnumpy(), np.array(expect_miss_idx, np.int32))
  96. assert np.allclose(miss_emb_idx.asnumpy(),
  97. np.array(expect_miss_emb_idx, np.int32))
  98. assert np.allclose(search_cache_idx.hashmap.data.asnumpy(),
  99. np.array(hashmap_np_after_ops, np.int32))
  100. @pytest.mark.level0
  101. @pytest.mark.platform_x86_cpu
  102. @pytest.mark.env_onecard
  103. def test_cache_swap_hashmap():
  104. hashmap_np = init_hashmap(10)
  105. indices_np = np.array([10, 2, 20, 5, 3], np.int32)
  106. net = CacheSwapHashmapNet(hashmap_np)
  107. indices = Tensor(indices_np)
  108. swap_cache_idx, old_emb_idx = net(indices)
  109. expect_swap_cache_idx = [-1, -1, 9, 7, -1]
  110. expect_old_emb_idx = [-1, -1, 21, 15, -1]
  111. hashmap_np_after_ops = [[5, 7, 0, 1],
  112. [10, 5, 0, 1],
  113. [2, 1, 0, 1],
  114. [20, 9, 0, 1],
  115. [20, 9, 0, 0],
  116. [0, 0, 0, 0],
  117. [0, 0, 0, 0],
  118. [0, 0, 0, 0],
  119. [3, 3, 0, 1],
  120. [21, 9, -5, 0]]
  121. assert np.allclose(swap_cache_idx.asnumpy(),
  122. np.array(expect_swap_cache_idx, np.int32))
  123. assert np.allclose(old_emb_idx.asnumpy(),
  124. np.array(expect_old_emb_idx, np.int32))
  125. assert np.allclose(net.net.hashmap.data.asnumpy(),
  126. np.array(hashmap_np_after_ops, np.int32))
  127. @pytest.mark.level0
  128. @pytest.mark.platform_x86_cpu
  129. @pytest.mark.env_onecard
  130. def test_update_cache():
  131. x_np = np.array([[2, 3, 4, 5],
  132. [6, 7, 8, 9],
  133. [11, 12, 13, 14],
  134. [1, 2, 3, 4],
  135. [5, 6, 7, 8]], np.int32)
  136. indices_np = np.array([[-1, 3, 4]], np.int32)
  137. update_np = np.array([[0, 0, 0, 0],
  138. [23, 34, 56, 78],
  139. [44, 55, 66, 77]], np.int32)
  140. indices = Tensor(indices_np)
  141. update = Tensor(update_np)
  142. expect = np.array([[2, 3, 4, 5],
  143. [6, 7, 8, 9],
  144. [11, 12, 13, 14],
  145. [23, 34, 56, 78],
  146. [44, 55, 66, 77]], np.int32)
  147. net = UpdateCacheNet(x_np)
  148. out = net(indices, update)
  149. assert np.allclose(net.x.data.asnumpy(), expect)
  150. assert np.allclose(out.asnumpy(), np.array([0], np.int32))