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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.ops.operations as P
  19. import mindspore.ops.functional as F
  20. from mindspore import context, Tensor
  21. from mindspore.common import dtype as mstype
  22. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  23. class NpuFloatNet(nn.Cell):
  24. """ NpuFloat definition, base on the related code in test_math_ops.py."""
  25. def __init__(self):
  26. super(NpuFloatNet, self).__init__()
  27. self.mul = P.Mul()
  28. self.alloc_status = P.NPUAllocFloatStatus()
  29. self.get_status = P.NPUGetFloatStatus()
  30. self.clear_status = P.NPUClearFloatStatus()
  31. self.fill = P.Fill()
  32. self.shape_op = P.Shape()
  33. self.select = P.Select()
  34. self.less = P.Less()
  35. self.cast = P.Cast()
  36. self.dtype = P.DType()
  37. self.reduce_sum = P.ReduceSum(keep_dims=True)
  38. self.sub = P.Sub()
  39. self.neg = P.Neg()
  40. def construct(self, x):
  41. init = self.alloc_status()
  42. clear_status = self.clear_status(init)
  43. x = F.depend(x, clear_status) # let x depend on clear_status
  44. res = self.sub(x, self.neg(x))
  45. init = F.depend(init, res) # let get_status depend on res
  46. get_status = self.get_status(init)
  47. # let reduce_sum depend on get_statusk
  48. init = F.depend(init, get_status)
  49. flag_sum = self.reduce_sum(init, (0,))
  50. base = self.cast(self.fill(self.dtype(
  51. res), self.shape_op(res), 0.0), self.dtype(flag_sum))
  52. cond = self.less(base, flag_sum)
  53. out = self.select(cond, self.cast(base, self.dtype(res)), res)
  54. return out
  55. @pytest.mark.level0
  56. @pytest.mark.platform_arm_ascend_training
  57. @pytest.mark.platform_x86_ascend_training
  58. @pytest.mark.env_onecard
  59. def test_float_not_overflow():
  60. input_data = Tensor(np.full((8, 5, 3, 1), 655, dtype=np.float16), dtype=mstype.float16)
  61. net = NpuFloatNet()
  62. out = net(input_data)
  63. # not overflow, we should got expected output.
  64. expect = Tensor(np.full((8, 5, 3, 1), 655 * 2,
  65. dtype=np.float16), dtype=mstype.float16)
  66. np.testing.assert_array_equal(out.asnumpy(), expect.asnumpy())
  67. @pytest.mark.level0
  68. @pytest.mark.platform_arm_ascend_training
  69. @pytest.mark.platform_x86_ascend_training
  70. @pytest.mark.env_onecard
  71. def test_float_overflow():
  72. input_data = Tensor(np.full((8, 5, 3, 1), 65504, dtype=np.float16), dtype=mstype.float16)
  73. net = NpuFloatNet()
  74. out = net(input_data)
  75. # all zero if overflowed.
  76. assert np.all(out.asnumpy() == 0)