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_float_status_op.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 pytest
  16. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. import numpy as np
  20. import mindspore.context as context
  21. class Net(nn.Cell):
  22. def __init__(self):
  23. super(Net, self).__init__()
  24. self.status = P.FloatStatus()
  25. def construct(self, x):
  26. return self.status(x)
  27. class Netnan(nn.Cell):
  28. def __init__(self):
  29. super(Netnan, self).__init__()
  30. self.isnan = P.IsNan()
  31. def construct(self, x):
  32. return self.isnan(x)
  33. class Netinf(nn.Cell):
  34. def __init__(self):
  35. super(Netinf, self).__init__()
  36. self.isinf = P.IsInf()
  37. def construct(self, x):
  38. return self.isinf(x)
  39. class Netfinite(nn.Cell):
  40. def __init__(self):
  41. super(Netfinite, self).__init__()
  42. self.isfinite = P.IsFinite()
  43. def construct(self, x):
  44. return self.isfinite(x)
  45. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  46. x1 = np.array([[1.2, 2, np.nan, 88]]).astype(np.float32)
  47. x2 = np.array([[np.inf, 1, 88.0, 0]]).astype(np.float32)
  48. x3 = np.array([[1, 2], [3, 4], [5.0, 88.0]]).astype(np.float32)
  49. @pytest.mark.level0
  50. @pytest.mark.platform_x86_gpu_training
  51. @pytest.mark.env_onecard
  52. def test_status():
  53. ms_status = Net();
  54. output1 = ms_status(Tensor(x1))
  55. output2 = ms_status(Tensor(x2))
  56. output3 = ms_status(Tensor(x3))
  57. expect1 = 1
  58. expect2 = 1
  59. expect3 = 0
  60. assert output1.asnumpy()[0] == expect1
  61. assert output2.asnumpy()[0] == expect2
  62. assert output3.asnumpy()[0] == expect3
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_training
  65. @pytest.mark.env_onecard
  66. def test_nan():
  67. ms_isnan = Netnan();
  68. output1 = ms_isnan(Tensor(x1))
  69. output2 = ms_isnan(Tensor(x2))
  70. output3 = ms_isnan(Tensor(x3))
  71. expect1 = [[False, False, True, False]]
  72. expect2 = [[False, False, False, False]]
  73. expect3 = [[False, False], [False, False], [False, False]]
  74. assert (output1.asnumpy() == expect1).all()
  75. assert (output2.asnumpy() == expect2).all()
  76. assert (output3.asnumpy() == expect3).all()
  77. @pytest.mark.level0
  78. @pytest.mark.platform_x86_gpu_training
  79. @pytest.mark.env_onecard
  80. def test_inf():
  81. ms_isinf = Netinf();
  82. output1 = ms_isinf(Tensor(x1))
  83. output2 = ms_isinf(Tensor(x2))
  84. output3 = ms_isinf(Tensor(x3))
  85. expect1 = [[False, False, False, False]]
  86. expect2 = [[True, False, False, False]]
  87. expect3 = [[False, False], [False, False], [False, False]]
  88. assert (output1.asnumpy() == expect1).all()
  89. assert (output2.asnumpy() == expect2).all()
  90. assert (output3.asnumpy() == expect3).all()
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_finite():
  95. ms_isfinite = Netfinite();
  96. output1 = ms_isfinite(Tensor(x1))
  97. output2 = ms_isfinite(Tensor(x2))
  98. output3 = ms_isfinite(Tensor(x3))
  99. expect1 = [[True, True, False, True]]
  100. expect2 = [[False, True, True, True]]
  101. expect3 = [[True, True], [True, True], [True, True]]
  102. assert (output1.asnumpy() == expect1).all()
  103. assert (output2.asnumpy() == expect2).all()
  104. assert (output3.asnumpy() == expect3).all()