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_nll_loss.py 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.operations import _grad_ops as G
  21. from mindspore.ops import operations as P
  22. class Net(nn.Cell):
  23. def __init__(self, reduction):
  24. super(Net, self).__init__()
  25. self.loss = P.NLLLoss(reduction=reduction)
  26. def construct(self, predict, target, weight):
  27. return self.loss(predict, target, weight)
  28. class NLLLossGradNet(nn.Cell):
  29. def __init__(self, reduction):
  30. super(NLLLossGradNet, self).__init__()
  31. self.grad = G.NLLLossGrad(reduction=reduction)
  32. def construct(self, x, dout_x, target, weight, total_weight):
  33. gout = self.grad(x, dout_x, target, weight, total_weight)
  34. return gout
  35. def nll_loss_template(nptype_input, nptype_weight, reduction):
  36. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  37. nll_loss_net = Net(reduction)
  38. predict = Tensor(
  39. np.array([[0.53, 0.74, -2.12], [1.29, -0.34, -1.13]]).astype(nptype_input))
  40. target = Tensor(np.array([0, 1]).astype(np.int32))
  41. weight = Tensor(np.array([0.45, -0.32, 1.21]).astype(nptype_weight))
  42. loss, total_weight = nll_loss_net(predict, target, weight)
  43. loss_np = loss.asnumpy()
  44. total_weight_np = total_weight.asnumpy()
  45. expected_tot_weight = np.array(0.129999995)
  46. if reduction == 'none':
  47. expected_loss = np.array([-0.238499984, -0.108800001])
  48. elif reduction == 'mean':
  49. expected_loss = np.array(-2.67153859)
  50. elif reduction == 'sum':
  51. expected_loss = np.array(-0.347299993)
  52. if nptype_input == np.float32 and nptype_weight == np.float32:
  53. ertol_loss = 1e-06
  54. elif nptype_input == np.float16 or nptype_weight == np.float16:
  55. ertol_loss = 1e-03
  56. if nptype_weight == np.float32:
  57. ertol_weight = 1e-06
  58. elif nptype_weight == np.float16:
  59. ertol_weight = 1e-03
  60. np.testing.assert_allclose(loss_np, expected_loss, ertol_loss)
  61. np.testing.assert_allclose(
  62. total_weight_np, expected_tot_weight, ertol_weight)
  63. def nll_loss_grad_template(nptype_input, nptype_weight, reduction):
  64. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  65. nll_loss_grad_net = NLLLossGradNet(reduction)
  66. x = Tensor(
  67. np.array([[0.53, 0.74, -2.12], [1.29, -0.34, -1.13]]).astype(nptype_input))
  68. if reduction == "none":
  69. dloss = Tensor(
  70. np.array([3.24, -2.13]).astype(nptype_input))
  71. else:
  72. dloss = Tensor(np.array(1.23).astype(nptype_input))
  73. target = Tensor(np.array([0, 1]).astype(np.int32))
  74. weight = Tensor(np.array([0.45, -0.32, 1.21]).astype(nptype_weight))
  75. total_weight = Tensor(np.array(0.13).astype(nptype_weight))
  76. dx = nll_loss_grad_net(x, dloss, target, weight, total_weight)
  77. dx_np = dx.asnumpy()
  78. print(dx)
  79. if reduction == "none":
  80. dx_expected = np.array([[-1.45799994, 0, 0], [0, -0.681600034, 0]])
  81. elif reduction == "mean":
  82. dx_expected = np.array([[-4.25769234, 0, 0], [0, 3.02769232, 0]])
  83. else:
  84. dx_expected = np.array([[-0.553499997, 0, 0], [0, 0.393599987, 0]])
  85. if nptype_input == np.float32 and nptype_weight == np.float32:
  86. ertol_loss = 1e-06
  87. else:
  88. ertol_loss = 1e-02
  89. np.testing.assert_allclose(dx_np, dx_expected, ertol_loss)
  90. @pytest.mark.level0
  91. @pytest.mark.platform_x86_gpu_training
  92. @pytest.mark.env_onecard
  93. def test_nll_loss_no_reduction():
  94. # Four combinations of fp32 and fp16 inputs and weights
  95. nll_loss_template(np.float32, np.float32, "none")
  96. nll_loss_template(np.float32, np.float16, "none")
  97. nll_loss_template(np.float16, np.float32, "none")
  98. nll_loss_template(np.float16, np.float16, "none")
  99. @pytest.mark.level0
  100. @pytest.mark.platform_x86_gpu_training
  101. @pytest.mark.env_onecard
  102. def test_nll_loss_mean_reduction():
  103. # Four combinations of fp32 and fp16 inputs and weights
  104. nll_loss_template(np.float32, np.float32, "mean")
  105. nll_loss_template(np.float32, np.float16, "mean")
  106. nll_loss_template(np.float16, np.float32, "mean")
  107. nll_loss_template(np.float16, np.float16, "mean")
  108. @pytest.mark.level0
  109. @pytest.mark.platform_x86_gpu_training
  110. @pytest.mark.env_onecard
  111. def test_nll_loss_sum_reduction():
  112. # Four combinations of fp32 and fp16 inputs and weights
  113. nll_loss_template(np.float32, np.float32, "sum")
  114. nll_loss_template(np.float32, np.float16, "sum")
  115. nll_loss_template(np.float16, np.float32, "sum")
  116. nll_loss_template(np.float16, np.float16, "sum")
  117. @pytest.mark.level0
  118. @pytest.mark.platform_x86_gpu_training
  119. @pytest.mark.env_onecard
  120. def test_nll_loss_grad_mean_reduction():
  121. # Four combinations of fp32 and fp16 inputs and weights
  122. nll_loss_grad_template(np.float32, np.float32, "mean")
  123. nll_loss_grad_template(np.float32, np.float16, "mean")
  124. nll_loss_grad_template(np.float16, np.float32, "mean")
  125. nll_loss_grad_template(np.float16, np.float16, "mean")
  126. @pytest.mark.level0
  127. @pytest.mark.platform_x86_gpu_training
  128. @pytest.mark.env_onecard
  129. def test_nll_loss_grad_sum_reduction():
  130. # Four combinations of fp32 and fp16 inputs and weights
  131. nll_loss_grad_template(np.float32, np.float32, "sum")
  132. nll_loss_grad_template(np.float32, np.float16, "sum")
  133. nll_loss_grad_template(np.float16, np.float32, "sum")
  134. nll_loss_grad_template(np.float16, np.float16, "sum")
  135. @pytest.mark.level0
  136. @pytest.mark.platform_x86_gpu_training
  137. @pytest.mark.env_onecard
  138. def test_nll_loss_grad_no_reduction():
  139. # Four combinations of fp32 and fp16 inputs and weights
  140. nll_loss_grad_template(np.float32, np.float32, "none")
  141. nll_loss_grad_template(np.float32, np.float16, "none")
  142. nll_loss_grad_template(np.float16, np.float32, "none")
  143. nll_loss_grad_template(np.float16, np.float16, "none")