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

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