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_l2loss_op.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.context as context
  18. import mindspore.nn as nn
  19. import mindspore as ms
  20. from mindspore import Tensor
  21. from mindspore.ops import operations as P
  22. class L2LossNet(nn.Cell):
  23. def __init__(self):
  24. super(L2LossNet, self).__init__()
  25. self.l2_loss = P.L2Loss()
  26. def construct(self, x):
  27. return self.l2_loss(x)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_gather_pynative_fp32_22():
  32. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  33. error = 1e-4
  34. x = Tensor(np.array([[1., 2.], [3., 4.]]), ms.float32)
  35. expect = np.array(15, np.float32)
  36. output = P.L2Loss()(x)
  37. diff = output.asnumpy() - expect
  38. assert np.all(diff < error)
  39. @pytest.mark.level0
  40. @pytest.mark.platform_x86_gpu_training
  41. @pytest.mark.env_onecard
  42. def test_gather_pynative_fp16_22():
  43. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  44. error = 1e-4
  45. x = Tensor(np.array([[1., 2.], [3., 4.]]), ms.float16)
  46. expect = np.array(15, np.float16)
  47. output = P.L2Loss()(x)
  48. diff = output.asnumpy() - expect
  49. assert np.all(diff < error)
  50. @pytest.mark.level0
  51. @pytest.mark.platform_x86_gpu_training
  52. @pytest.mark.env_onecard
  53. def test_gather_pynative_fp32_14():
  54. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  55. error = 1e-4
  56. x = Tensor(np.array([1., 2., 3., 4.]), ms.float32)
  57. expect = np.array(15, np.float32)
  58. output = P.L2Loss()(x)
  59. diff = output.asnumpy() - expect
  60. assert np.all(diff < error)
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_gather_pynative_fp16_14():
  65. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  66. error = 1e-4
  67. x = Tensor(np.array([1., 2., 3., 4.]), ms.float16)
  68. expect = np.array(15, np.float16)
  69. output = P.L2Loss()(x)
  70. diff = output.asnumpy() - expect
  71. assert np.all(diff < error)
  72. def test_gather_graph_fp32_14():
  73. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  74. error = 1e-4
  75. x = Tensor(np.array([1., 2., 3., 4.]), ms.float32)
  76. expect = np.array(15, np.float32)
  77. l2_loss = L2LossNet()
  78. output = l2_loss(x)
  79. diff = output.asnumpy() - expect
  80. assert np.all(diff < error)
  81. def test_gather_graph_fp16_14():
  82. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  83. error = 1e-4
  84. x = Tensor(np.array([1., 2., 3., 4.]), ms.float16)
  85. expect = np.array(15, np.float16)
  86. l2_loss = L2LossNet()
  87. output = l2_loss(x)
  88. diff = output.asnumpy() - expect
  89. assert np.all(diff < error)