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_lin_space.py 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. import numpy as np
  16. import pytest
  17. import mindspore.common.dtype as mstype
  18. import mindspore.context as context
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.nn import Cell
  21. from mindspore.ops import operations as P
  22. class LinSpaceNet(Cell):
  23. def __init__(self, num):
  24. super(LinSpaceNet, self).__init__()
  25. self.ls_op = P.LinSpace()
  26. self.num = num
  27. def construct(self, start, stop):
  28. output = self.ls_op(start, stop, self.num)
  29. return output
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_gpu_training
  32. @pytest.mark.env_onecard
  33. def test_lin_space_1():
  34. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  35. start_np = 5
  36. stop_np = 150
  37. num_np = 12
  38. start = Tensor(start_np, dtype=mstype.float32)
  39. stop = Tensor(stop_np, dtype=mstype.float32)
  40. num = num_np
  41. ls_op = P.LinSpace()
  42. result_ms = ls_op(start, stop, num).asnumpy()
  43. result_np = np.linspace(start_np, stop_np, num_np)
  44. assert np.allclose(result_ms, result_np)
  45. @pytest.mark.level0
  46. @pytest.mark.platform_x86_gpu_training
  47. @pytest.mark.env_onecard
  48. def test_lin_shape_2():
  49. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  50. start_np = -25
  51. stop_np = 147
  52. num_np = 10
  53. start = Tensor(start_np, dtype=mstype.float32)
  54. stop = Tensor(stop_np, dtype=mstype.float32)
  55. num = num_np
  56. ls_op = P.LinSpace()
  57. result_ms = ls_op(start, stop, num).asnumpy()
  58. result_np = np.linspace(start_np, stop_np, num_np)
  59. assert np.allclose(result_ms, result_np)
  60. @pytest.mark.level0
  61. @pytest.mark.platform_x86_gpu_training
  62. @pytest.mark.env_onecard
  63. def test_lin_shape_3():
  64. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  65. start_np = 25
  66. stop_np = -147
  67. num_np = 20
  68. start = Tensor(start_np, dtype=mstype.float32)
  69. stop = Tensor(stop_np, dtype=mstype.float32)
  70. net = LinSpaceNet(num_np)
  71. result_ms = net(start, stop).asnumpy()
  72. result_np = np.linspace(start_np, stop_np, num_np)
  73. assert np.allclose(result_ms, result_np)
  74. @pytest.mark.level0
  75. @pytest.mark.platform_x86_gpu_training
  76. @pytest.mark.env_onecard
  77. def test_lin_shape_4():
  78. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  79. start_np = -25.3
  80. stop_np = -147
  81. num_np = 36
  82. start = Tensor(start_np, dtype=mstype.float32)
  83. stop = Tensor(stop_np, dtype=mstype.float32)
  84. net = LinSpaceNet(num_np)
  85. result_ms = net(start, stop).asnumpy()
  86. result_np = np.linspace(start_np, stop_np, num_np)
  87. assert np.allclose(result_ms, result_np)