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_hsigmoid_op.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.composite import GradOperation
  22. from mindspore.ops.operations import _inner_ops as inner
  23. class Grad(nn.Cell):
  24. def __init__(self, network):
  25. super(Grad, self).__init__()
  26. self.grad = GradOperation(get_all=True, sens_param=True)
  27. self.network = network
  28. def construct(self, input_x, dout):
  29. return self.grad(self.network)(input_x, dout)
  30. class Net(nn.Cell):
  31. def __init__(self):
  32. super(Net, self).__init__()
  33. self.HSigmoid = P.HSigmoid()
  34. def construct(self, x):
  35. return self.HSigmoid(x)
  36. class DynamicNet(nn.Cell):
  37. def __init__(self):
  38. super(DynamicNet, self).__init__()
  39. self.HSigmoid = P.HSigmoid()
  40. self.d = inner.GpuConvertToDynamicShape()
  41. def construct(self, x):
  42. x = self.d(x)
  43. return self.HSigmoid(x)
  44. def generate_testcases(nptype):
  45. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  46. x = np.array([-1, -2, 0, 2, 1]).astype(nptype)
  47. net = Net()
  48. output = net(Tensor(x))
  49. expect = np.array([0.33333334, 0.16666667, 0.5, 0.8333333, 0.6666667]).astype(nptype)
  50. np.testing.assert_almost_equal(output.asnumpy(), expect)
  51. sens = np.array([-1.45, -2.63, 0.34, 6.43, 34.6]).astype(nptype)
  52. backward_net = Grad(Net())
  53. output = backward_net(Tensor(x), Tensor(sens))
  54. expect = np.array([0, 0, 5.66666685e-02, 0, 0]).astype(nptype)
  55. np.testing.assert_almost_equal(output[0].asnumpy(), expect)
  56. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  57. x = np.array([-1, -2, 0, 2, 1]).astype(nptype)
  58. net = Net()
  59. output = net(Tensor(x))
  60. expect = np.array([0.33333334, 0.16666667, 0.5, 0.8333333, 0.6666667]).astype(nptype)
  61. np.testing.assert_almost_equal(output.asnumpy(), expect)
  62. sens = np.array([-1.45, -2.63, 0.34, 6.43, 34.6]).astype(nptype)
  63. backward_net = Grad(Net())
  64. output = backward_net(Tensor(x), Tensor(sens))
  65. expect = np.array([0, 0, 5.66666685e-02, 0, 0]).astype(nptype)
  66. np.testing.assert_almost_equal(output[0].asnumpy(), expect)
  67. def generate_dynamic_testcase(nptype):
  68. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  69. x = np.array([-1, -2, 0, 2, 1]).astype(nptype)
  70. net = DynamicNet()
  71. output = net(Tensor(x))
  72. expect = np.array([0.33333334, 0.16666667, 0.5, 0.8333333, 0.6666667]).astype(nptype)
  73. np.testing.assert_almost_equal(output.asnumpy(), expect)
  74. @pytest.mark.level0
  75. @pytest.mark.platform_x86_gpu_training
  76. @pytest.mark.env_onecard
  77. def test_hsigmoid_dynamic_float32():
  78. generate_dynamic_testcase(np.float32)
  79. @pytest.mark.level0
  80. @pytest.mark.platform_x86_gpu_training
  81. @pytest.mark.env_onecard
  82. def test_hsigmoid_float32():
  83. generate_testcases(np.float32)
  84. @pytest.mark.level0
  85. @pytest.mark.platform_x86_gpu_training
  86. @pytest.mark.env_onecard
  87. def test_hsigmoid_float16():
  88. generate_testcases(np.float16)