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 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. class CaseNet(nn.Cell):
  21. def __init__(self):
  22. super(CaseNet, self).__init__()
  23. self.conv = nn.Conv2d(1, 1, 3)
  24. self.relu = nn.ReLU()
  25. self.relu1 = nn.ReLU()
  26. self.softmax = nn.Softmax()
  27. self.layers1 = (self.relu, self.softmax)
  28. self.layers2 = (self.conv, self.relu1)
  29. def construct(self, x, index1, index2):
  30. x = self.layers1[index1](x)
  31. x = self.layers2[index2](x)
  32. return x
  33. @pytest.mark.level0
  34. @pytest.mark.platform_arm_ascend_training
  35. @pytest.mark.platform_x86_ascend_training
  36. @pytest.mark.platform_x86_gpu_training
  37. @pytest.mark.env_onecard
  38. def test_switch_layer():
  39. context.set_context(mode=context.GRAPH_MODE)
  40. net = CaseNet()
  41. data = Tensor(np.ones((1, 1, 224, 224)), mstype.float32)
  42. idx = Tensor(0, mstype.int32)
  43. idx2 = Tensor(-1, mstype.int32)
  44. value = net(data, idx, idx2)
  45. relu = nn.ReLU()
  46. true_value = relu(data)
  47. ret = np.allclose(value.asnumpy(), true_value.asnumpy())
  48. assert ret