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""" 16@File : test_sparse_tensor.py 17@Author: 18@Date : 2020-07-16 19@Desc : test mindspore sparse_tensor's operation 20""" 21import numpy as np 22import pytest 23 24import mindspore as ms 25import mindspore.nn as nn 26from mindspore.ops import composite as C 27from mindspore import Tensor, SparseTensor, context 28 29@pytest.fixture(scope="module", autouse=True) 30def setup_teardown(): 31 context.set_context(mode=context.GRAPH_MODE, enable_sparse=True) 32 yield 33 context.set_context(enable_sparse=False) 34 35 36grad_op = C.GradOperation(get_all=True) 37 38class MakeSparseTensor(nn.Cell): 39 def __init__(self, dense_shape): 40 super(MakeSparseTensor, self).__init__() 41 self.dense_shape = dense_shape 42 def construct(self, indices, values): 43 ret = (SparseTensor(indices, values, self.dense_shape),) 44 return ret[0] 45 46 47def test_sparse_tensor_make_sparse_tensor(): 48 indices = Tensor([[0, 1], [1, 2]]) 49 values = Tensor([1, 2], dtype=ms.float32) 50 MakeSparseTensor((3, 4))(indices, values) 51 52 53def test_sparse_tensor_attr(): 54 class SparseTensorGetAttr(nn.Cell): 55 def __init__(self): 56 super(SparseTensorGetAttr, self).__init__() 57 self.dense_shape = (3, 4) 58 def construct(self, indices, values): 59 x = SparseTensor(indices, values, self.dense_shape) 60 return x.values, x.indices, x.dense_shape 61 62 indices = Tensor([[0, 1], [1, 2]]) 63 values = Tensor([1, 2], dtype=ms.float32) 64 SparseTensorGetAttr()(indices, values) 65 grad_op(SparseTensorGetAttr())(indices, values) 66 67 68def test_sparse_tensor_indices_dim_greater_than_dense_shape_dim(): 69 indices = Tensor(np.array([[0, 0, 0], [0, 0, 1]], dtype=np.int32)) 70 values = Tensor(np.array([100, 200], dtype=np.float32)) 71 dense_shape = (2, 2) 72 with pytest.raises(TypeError): 73 MakeSparseTensor(dense_shape)(indices, values) 74 75 76def test_sparse_tensor_indices_dim_less_than_dense_shape_dim(): 77 indices = Tensor(np.array([[0, 0], [0, 1]], dtype=np.int32)) 78 values = Tensor(np.array([100, 200], dtype=np.float32)) 79 dense_shape = (2, 2, 2) 80 with pytest.raises(TypeError): 81 MakeSparseTensor(dense_shape)(indices, values) 82 83 84def test_sparse_tensor_to_tensor(): 85 class SparseToDenseCell(nn.Cell): 86 def __init__(self, dense_shape): 87 super(SparseToDenseCell, self).__init__() 88 self.dense_shape = dense_shape 89 self.sparse_to_dense = nn.SparseToDense() 90 def construct(self, indices, values): 91 sparse = SparseTensor(indices, values, self.dense_shape) 92 return self.sparse_to_dense(sparse) 93 94 indices = Tensor([[0, 1], [1, 2]]) 95 values = Tensor([1, 2], dtype=ms.float32) 96 dense_shape = (3, 4) 97 SparseToDenseCell(dense_shape)(indices, values) 98 grad_op(SparseToDenseCell(dense_shape))(indices, values) 99