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# ============================================================================ 15import numpy as np 16import pytest 17 18import mindspore.context as context 19import mindspore.nn as nn 20from mindspore import Tensor 21from mindspore import Parameter 22from mindspore.ops import operations as P 23 24context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 25 26 27class UpdateCacheNet(nn.Cell): 28 def __init__(self, x): 29 super().__init__() 30 self.ops = P.UpdateCache() 31 self.max_num = 9999 32 self.x = Parameter(Tensor(x), name='x') 33 34 def construct(self, indices, update): 35 return self.ops(self.x, indices, update, self.max_num) 36 37 38@pytest.mark.level0 39@pytest.mark.platform_x86_cpu 40@pytest.mark.env_onecard 41def test_update_cache(): 42 x_np = np.array([[2, 3, 4, 5], 43 [6, 7, 8, 9], 44 [11, 12, 13, 14], 45 [1, 2, 3, 4], 46 [5, 6, 7, 8]], np.int32) 47 48 indices_np = np.array([[-1, 3, 4]], np.int32) 49 update_np = np.array([[0, 0, 0, 0], 50 [23, 34, 56, 78], 51 [44, 55, 66, 77]], np.int32) 52 53 indices = Tensor(indices_np) 54 update = Tensor(update_np) 55 56 expect = np.array([[2, 3, 4, 5], 57 [6, 7, 8, 9], 58 [11, 12, 13, 14], 59 [23, 34, 56, 78], 60 [44, 55, 66, 77]], np.int32) 61 net = UpdateCacheNet(x_np) 62 out = net(indices, update) 63 assert np.allclose(net.x.data.asnumpy(), expect) 64 assert np.allclose(out.asnumpy(), np.array([0], np.int32)) 65