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_tensor_array.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2021 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. import numpy as np
  16. import pytest
  17. import mindspore
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. class TensorArrayNet(nn.Cell):
  22. def __init__(self, dtype, element_shape):
  23. super(TensorArrayNet, self).__init__()
  24. self.ta = nn.TensorArray(dtype, element_shape)
  25. def construct(self, index, value):
  26. self.ta.write(index, value)
  27. v = self.ta.read(index)
  28. s = self.ta.stack()
  29. self.ta.close()
  30. return v, s
  31. @pytest.mark.level0
  32. @pytest.mark.platform_x86_cpu
  33. @pytest.mark.env_onecard
  34. def test_tensorarray():
  35. """
  36. Feature: TensorArray gpu TEST.
  37. Description: Test the function write, read, stack, clear, close in both graph and pynative mode.
  38. Expectation: success.
  39. """
  40. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  41. index = Tensor(0, mindspore.int64)
  42. value = Tensor(5, mindspore.int64)
  43. ta = TensorArrayNet(dtype=mindspore.int64, element_shape=())
  44. v, s = ta(index, value)
  45. expect_v = 5
  46. expect_s = [5]
  47. assert np.allclose(s.asnumpy(), expect_s)
  48. assert np.allclose(v.asnumpy(), expect_v)
  49. context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
  50. ta = nn.TensorArray(mindspore.int64, ())
  51. for i in range(5):
  52. ta.write(i, 99)
  53. v = ta.read(0)
  54. s = ta.stack()
  55. expect_v = 99
  56. expect_s = [99, 99, 99, 99, 99]
  57. assert np.allclose(s.asnumpy(), expect_s)
  58. assert np.allclose(v.asnumpy(), expect_v)
  59. ta_size = ta.size()
  60. assert np.allclose(ta_size.asnumpy(), 5)
  61. ta.clear()
  62. ta_size = ta.size()
  63. assert np.allclose(ta_size.asnumpy(), 0)
  64. ta.write(0, 88)
  65. v = ta.read(0)
  66. s = ta.stack()
  67. ta.close()
  68. expect_v = 88
  69. expect_s = [88]
  70. assert np.allclose(s.asnumpy(), expect_s)
  71. assert np.allclose(v.asnumpy(), expect_v)
  72. ta = nn.TensorArray(mindspore.float32, ())
  73. ta.write(5, 1.)
  74. s = ta.stack()
  75. expect_s = [0., 0., 0., 0., 0., 1.]
  76. assert np.allclose(s.asnumpy(), expect_s)
  77. ta.write(2, 1.)
  78. s = ta.stack()
  79. expect_s = [0., 0., 1., 0., 0., 1.]
  80. assert np.allclose(s.asnumpy(), expect_s)
  81. ta.close()