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_sparse_tensor.py 4.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. """
  21. import numpy as np
  22. import pytest
  23. import mindspore as ms
  24. import mindspore.nn as nn
  25. from mindspore.ops import composite as C
  26. from mindspore import Tensor, COOTensor, context
  27. @pytest.fixture(scope="module", autouse=True)
  28. def setup_teardown():
  29. context.set_context(mode=context.GRAPH_MODE, enable_sparse=True)
  30. yield
  31. context.set_context(enable_sparse=False)
  32. grad_op = C.GradOperation(get_all=True)
  33. class MakeSparseTensor(nn.Cell):
  34. def __init__(self, dense_shape):
  35. super(MakeSparseTensor, self).__init__()
  36. self.dense_shape = dense_shape
  37. def construct(self, indices, values):
  38. ret = (COOTensor(indices, values, self.dense_shape),)
  39. return ret[0]
  40. def test_sparse_tensor_make_coo_tensor():
  41. """
  42. Feature: Test MakeCOOTensor.
  43. Description: Test MakeCOOTensor.
  44. Expectation: Success.
  45. """
  46. indices = Tensor([[0, 1], [1, 2]])
  47. values = Tensor([1, 2], dtype=ms.float32)
  48. MakeSparseTensor((3, 4))(indices, values)
  49. def test_sparse_tensor_attr():
  50. """
  51. Feature: Test GetAttr.
  52. Description: Test GetAttr in COOTensor (values, indices, dense_shape).
  53. Expectation: Success.
  54. """
  55. class SparseTensorGetAttr(nn.Cell):
  56. def __init__(self):
  57. super(SparseTensorGetAttr, self).__init__()
  58. self.dense_shape = (3, 4)
  59. def construct(self, indices, values):
  60. x = COOTensor(indices, values, self.dense_shape)
  61. return x.values, x.indices, x.shape
  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. def test_sparse_tensor_indices_dim_greater_than_dense_shape_dim():
  67. """
  68. Feature: Test MakeSparseTensor.
  69. Description: Test sparse tensor indices dim greater than dense shape dim.
  70. Expectation: Success.
  71. """
  72. indices = Tensor(np.array([[0, 0, 0], [0, 0, 1]], dtype=np.int32))
  73. values = Tensor(np.array([100, 200], dtype=np.float32))
  74. dense_shape = (2, 2)
  75. with pytest.raises(ValueError):
  76. MakeSparseTensor(dense_shape)(indices, values)
  77. def test_sparse_tensor_indices_dim_less_than_dense_shape_dim():
  78. """
  79. Feature: Test MakeSparseTensor.
  80. Description: Test sparse tensor indices dim less than dense shape dim.
  81. Expectation: Success.
  82. """
  83. indices = Tensor(np.array([[0, 0], [0, 1]], dtype=np.int32))
  84. values = Tensor(np.array([100, 200], dtype=np.float32))
  85. dense_shape = (2, 2, 2)
  86. with pytest.raises(TypeError):
  87. MakeSparseTensor(dense_shape)(indices, values)
  88. def test_sparse_tensor_to_tensor():
  89. """
  90. Feature: Test nn.SparseToDense.
  91. Description: Test COOTensor to dense tensor.
  92. Expectation: Success.
  93. """
  94. class SparseToDenseCell(nn.Cell):
  95. def __init__(self, dense_shape):
  96. super(SparseToDenseCell, self).__init__()
  97. self.dense_shape = dense_shape
  98. self.sparse_to_dense = nn.SparseToDense()
  99. def construct(self, indices, values):
  100. sparse = COOTensor(indices, values, self.dense_shape)
  101. return self.sparse_to_dense(sparse)
  102. indices = Tensor([[0, 1], [1, 2]])
  103. values = Tensor([1, 2], dtype=ms.float32)
  104. dense_shape = (3, 4)
  105. SparseToDenseCell(dense_shape)(indices, values)
  106. grad_op(SparseToDenseCell(dense_shape))(indices, values)