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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, SparseTensor, 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 = (SparseTensor(indices, values, self.dense_shape),)
  39. return ret[0]
  40. def test_sparse_tensor_make_sparse_tensor():
  41. indices = Tensor([[0, 1], [1, 2]])
  42. values = Tensor([1, 2], dtype=ms.float32)
  43. MakeSparseTensor((3, 4))(indices, values)
  44. def test_sparse_tensor_attr():
  45. class SparseTensorGetAttr(nn.Cell):
  46. def __init__(self):
  47. super(SparseTensorGetAttr, self).__init__()
  48. self.dense_shape = (3, 4)
  49. def construct(self, indices, values):
  50. x = SparseTensor(indices, values, self.dense_shape)
  51. return x.values, x.indices, x.dense_shape
  52. indices = Tensor([[0, 1], [1, 2]])
  53. values = Tensor([1, 2], dtype=ms.float32)
  54. SparseTensorGetAttr()(indices, values)
  55. grad_op(SparseTensorGetAttr())(indices, values)
  56. def test_sparse_tensor_indices_dim_greater_than_dense_shape_dim():
  57. indices = Tensor(np.array([[0, 0, 0], [0, 0, 1]], dtype=np.int32))
  58. values = Tensor(np.array([100, 200], dtype=np.float32))
  59. dense_shape = (2, 2)
  60. with pytest.raises(TypeError):
  61. MakeSparseTensor(dense_shape)(indices, values)
  62. def test_sparse_tensor_indices_dim_less_than_dense_shape_dim():
  63. indices = Tensor(np.array([[0, 0], [0, 1]], dtype=np.int32))
  64. values = Tensor(np.array([100, 200], dtype=np.float32))
  65. dense_shape = (2, 2, 2)
  66. with pytest.raises(TypeError):
  67. MakeSparseTensor(dense_shape)(indices, values)
  68. def test_sparse_tensor_to_tensor():
  69. class SparseToDenseCell(nn.Cell):
  70. def __init__(self, dense_shape):
  71. super(SparseToDenseCell, self).__init__()
  72. self.dense_shape = dense_shape
  73. self.sparse_to_dense = nn.SparseToDense()
  74. def construct(self, indices, values):
  75. sparse = SparseTensor(indices, values, self.dense_shape)
  76. return self.sparse_to_dense(sparse)
  77. indices = Tensor([[0, 1], [1, 2]])
  78. values = Tensor([1, 2], dtype=ms.float32)
  79. dense_shape = (3, 4)
  80. SparseToDenseCell(dense_shape)(indices, values)
  81. grad_op(SparseToDenseCell(dense_shape))(indices, values)