1# Copyright 2022 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"""smoke tests for RowTensor operations""" 16 17import pytest 18import numpy as np 19 20from mindspore import Tensor, nn, context 21from mindspore.common.sparse_tensor import RowTensorInner 22from mindspore.common import dtype as mstype 23 24 25def compare_row(row1, row2): 26 assert isinstance(row1, RowTensorInner) 27 assert isinstance(row2, RowTensorInner) 28 assert (row1.indices.asnumpy() == row1.indices.asnumpy()).all() 29 assert (row2.values.asnumpy() == row2.values.asnumpy()).all() 30 assert row1.dense_shape == row2.dense_shape 31 32 33@pytest.mark.level2 34@pytest.mark.platform_arm_ascend_training 35@pytest.mark.platform_x86_ascend_training 36@pytest.mark.platform_x86_gpu_training 37@pytest.mark.platform_x86_cpu 38@pytest.mark.env_onecard 39def test_make_row(): 40 """ 41 Feature: Test RowTensor Constructor in Graph and PyNative. 42 Description: Test RowTensorInner(indices, values, shape) and RowTensorInner(RowTensor) 43 Expectation: Success. 44 """ 45 indices = Tensor([0, 1], dtype=mstype.int32) 46 values = Tensor([[1, 2], [3, 4]], dtype=mstype.float32) 47 dense_shape = (3, 2) 48 49 def test_pynative(): 50 return RowTensorInner(indices, values, dense_shape) 51 52 row1 = test_pynative() 53 compare_row(row1, row1) 54 row2 = RowTensorInner(row_tensor=row1) 55 compare_row(row1, row2) 56 57 58@pytest.mark.level2 59@pytest.mark.platform_arm_ascend_training 60@pytest.mark.platform_x86_ascend_training 61@pytest.mark.platform_x86_gpu_training 62@pytest.mark.env_onecard 63def test_row_tensor_with_control_if(): 64 """ 65 Feature: Test RowTensor in if. 66 Description: Test RowTensor computation in while loop. 67 Expectation: Success. 68 """ 69 class RowTensorValuesDouble(nn.Cell): 70 71 def construct(self, x): 72 indices = x.indices 73 values = x.values * 2 74 shape = x.dense_shape 75 return RowTensorInner(indices, values, shape) 76 77 class RowTensorValuesAdd2(nn.Cell): 78 79 def construct(self, x): 80 indices = x.indices 81 values = x.values + 2 82 shape = x.dense_shape 83 return RowTensorInner(indices, values, shape) 84 85 class RowTensorWithControlIf(nn.Cell): 86 def __init__(self, shape): 87 super(RowTensorWithControlIf, self).__init__() 88 self.op1 = RowTensorValuesDouble() 89 self.op2 = RowTensorValuesAdd2() 90 self.shape = shape 91 92 def construct(self, a, b, indices, values): 93 x = RowTensorInner(indices, values, self.shape) 94 if a > b: 95 x = self.op1(x) 96 else: 97 x = self.op2(x) 98 return x.indices, x.values, x.dense_shape 99 context.set_context(mode=context.PYNATIVE_MODE) 100 a = Tensor(0, mstype.int32) 101 b = Tensor(2, mstype.int32) 102 indices = Tensor([0, 1], dtype=mstype.int32) 103 values = Tensor([[1, 2], [3, 4]], dtype=mstype.float32) 104 shape = (3, 2) 105 net = RowTensorWithControlIf(shape) 106 out = net(a, b, indices, values) 107 assert np.allclose(out[0].asnumpy(), indices.asnumpy(), .0, .0) 108 assert np.allclose(out[1].asnumpy(), values.asnumpy() + 2, .0, .0) 109 assert out[2] == shape 110