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_switch_layer.py 3.4 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. from mindspore import Tensor, nn
  19. from mindspore.common import dtype as mstype
  20. from mindspore.ops.composite import GradOperation
  21. class Grad(nn.Cell):
  22. def __init__(self, net):
  23. super().__init__()
  24. self.grad = GradOperation(get_all=False)
  25. self.net = net
  26. def construct(self, x, y):
  27. grad_net = self.grad(self.net)
  28. grad = grad_net(x, y)
  29. return grad
  30. class CaseNet(nn.Cell):
  31. def __init__(self):
  32. super(CaseNet, self).__init__()
  33. self.conv = nn.Conv2d(1, 1, 3)
  34. self.relu = nn.ReLU()
  35. self.relu1 = nn.ReLU()
  36. self.softmax = nn.Softmax()
  37. self.layers1 = (self.relu, self.softmax)
  38. self.layers2 = (self.conv, self.relu1)
  39. def construct(self, x, index1, index2):
  40. x = self.layers1[index1](x)
  41. x = self.layers2[index2](x)
  42. return x
  43. @pytest.mark.level1
  44. @pytest.mark.platform_arm_ascend_training
  45. @pytest.mark.platform_x86_ascend_training
  46. @pytest.mark.platform_x86_gpu_training
  47. @pytest.mark.env_onecard
  48. def test_switch_layer():
  49. context.set_context(mode=context.GRAPH_MODE)
  50. net = CaseNet()
  51. data = Tensor(np.ones((1, 1, 224, 224)), mstype.float32)
  52. idx = Tensor(0, mstype.int32)
  53. idx2 = Tensor(-1, mstype.int32)
  54. value = net(data, idx, idx2)
  55. relu = nn.ReLU()
  56. true_value = relu(data)
  57. ret = np.allclose(value.asnumpy(), true_value.asnumpy())
  58. assert ret
  59. @pytest.mark.level0
  60. @pytest.mark.platform_arm_ascend_training
  61. @pytest.mark.platform_x86_ascend_training
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_cell_in_list():
  65. """
  66. Feature: Switch layer in while.
  67. Description: test recursive switch layer.
  68. Expectation: success if grad and output are correct.
  69. """
  70. class TestCell(nn.Cell):
  71. def __init__(self, i):
  72. super().__init__()
  73. self.i = i
  74. def construct(self, x):
  75. return self.i * x
  76. class CellInList(nn.Cell):
  77. def __init__(self):
  78. super().__init__()
  79. self.cell_list = nn.CellList()
  80. self.cell_list.append(TestCell(4))
  81. self.cell_list.append(TestCell(5))
  82. self.cell_list.append(TestCell(6))
  83. def construct(self, t, x):
  84. out = t
  85. while x < 3:
  86. add = self.cell_list[x](t)
  87. out = out + add
  88. x += 1
  89. return out
  90. net = CellInList()
  91. t = Tensor(10, mstype.int32)
  92. x = Tensor(0, mstype.int32)
  93. out = net(t, x)
  94. grad_net = Grad(net)
  95. grad_out = grad_net(t, x)
  96. assert out == Tensor(160, mstype.int32)
  97. assert grad_out == Tensor(16, mstype.int32)