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_pynative.py 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_pynative.py
  17. @Author:
  18. @Date : 2020-08-04
  19. @Desc : test mindspore sparse pynative
  20. """
  21. import pytest
  22. import mindspore as ms
  23. import mindspore.nn as nn
  24. from mindspore import context, Tensor, RowTensor, COOTensor
  25. from mindspore.ops import composite as C
  26. @pytest.fixture(scope="module", autouse=True)
  27. def setup_teardown():
  28. context.set_context(mode=context.PYNATIVE_MODE, enable_sparse=True)
  29. yield
  30. context.set_context(mode=context.GRAPH_MODE, enable_sparse=False)
  31. grad_all = C.GradOperation(get_all=True)
  32. class GradWrap(nn.Cell):
  33. def __init__(self, network):
  34. super(GradWrap, self).__init__()
  35. self.network = network
  36. def construct(self, *args):
  37. grad = grad_all(self.network)(*args)
  38. return grad
  39. def test_row_tensor_attr():
  40. """
  41. Feature: Test RowTensor.
  42. Description: Test RowTensor GetAttr.
  43. Expectation: Success.
  44. """
  45. class RowTensorGetAttr(nn.Cell):
  46. def __init__(self, dense_shape):
  47. super(RowTensorGetAttr, self).__init__()
  48. self.dense_shape = dense_shape
  49. def construct(self, indices, values):
  50. x = RowTensor(indices, values, self.dense_shape)
  51. return x.values, x.indices, x.dense_shape
  52. indices = Tensor([0])
  53. values = Tensor([[1, 2]], dtype=ms.float32)
  54. RowTensorGetAttr((3, 2))(indices, values)
  55. GradWrap(RowTensorGetAttr((3, 2)))(indices, values)
  56. def test_sparse_tensor_attr():
  57. """
  58. Feature: Test COOTensor.
  59. Description: Test COOTensor GetAttr.
  60. Expectation: Success.
  61. """
  62. class SparseTensorGetAttr(nn.Cell):
  63. def __init__(self):
  64. super(SparseTensorGetAttr, self).__init__()
  65. self.dense_shape = (3, 4)
  66. def construct(self, indices, values):
  67. x = COOTensor(indices, values, self.dense_shape)
  68. return x.values, x.indices, x.shape
  69. indices = Tensor([[0, 1], [1, 2]])
  70. values = Tensor([1, 2], dtype=ms.float32)
  71. SparseTensorGetAttr()(indices, values)
  72. GradWrap(SparseTensorGetAttr())(indices, values)