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_tuple.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 mindspore.context as context
  16. import functools
  17. import numpy as np
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore import dtype as mstype
  21. from mindspore.ops import operations as P
  22. from mindspore import context
  23. from ..ut_filter import non_graph_engine
  24. from ....mindspore_test_framework.mindspore_test import mindspore_test
  25. from ....mindspore_test_framework.pipeline.forward.compile_forward \
  26. import pipeline_for_compile_forward_ge_graph_for_case_by_case_config
  27. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  28. class TupleGraphNet(nn.Cell):
  29. def __init__(self):
  30. super(TupleGraphNet, self).__init__()
  31. self.conv1 = nn.Conv2d(3, 1, 3, pad_mode='same')
  32. self.conv2 = nn.Conv2d(3, 1, 7, pad_mode='same')
  33. self.conv3 = nn.Conv2d(3, 3, 3, pad_mode='same')
  34. self.layers = (self.conv1, self.conv2, self.conv3)
  35. def construct(self, x):
  36. return self.layers[0](x)
  37. class NestTupleGraphNet(nn.Cell):
  38. def __init__(self):
  39. super(NestTupleGraphNet, self).__init__()
  40. self.conv1 = nn.Conv2d(3, 1, 3, pad_mode='same')
  41. self.conv2 = nn.Conv2d(3, 1, 7, pad_mode='same')
  42. self.conv3 = nn.Conv2d(3, 3, 3, pad_mode='same')
  43. self.layers = ((self.conv1, self.conv2),
  44. (self.conv2, self.conv1, self.conv3))
  45. def construct(self, x):
  46. return self.layers[0][1](x)
  47. test_case_ops = [
  48. ('TupleGraph', {
  49. 'block': TupleGraphNet(),
  50. 'desc_inputs': [Tensor(np.ones((3, 3, 24, 24)), mstype.float32)]}),
  51. ('NestTupleGraph', {
  52. 'block': NestTupleGraphNet(),
  53. 'desc_inputs': [Tensor(np.ones((3, 3, 24, 24)), mstype.float32)]}),
  54. ]
  55. test_case_lists = [test_case_ops]
  56. test_exec_case = functools.reduce(lambda x, y: x + y, test_case_lists)
  57. # use -k to select certain testcast
  58. # pytest tests/python/ops/test_ops.py::test_backward -k LayerNorm
  59. @non_graph_engine
  60. @mindspore_test(pipeline_for_compile_forward_ge_graph_for_case_by_case_config)
  61. def test_exec():
  62. context.set_context(mode=context.GRAPH_MODE)
  63. return test_exec_case