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_fix_bug.py 3.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. """ test_fix_bug """
  16. import numpy as np
  17. import pytest
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import composite as C
  21. from mindspore.common.api import _executor
  22. class assignment1_Net(nn.Cell):
  23. """ assignment1_Net definition """
  24. def __init__(self, number):
  25. super().__init__()
  26. self.number = number
  27. self.relu = nn.ReLU()
  28. def construct(self, x):
  29. y = self.number
  30. for _ in [1, y]:
  31. x = self.relu(x)
  32. return x
  33. class assignment2_Net(nn.Cell):
  34. """ assignment2_Net definition """
  35. def __init__(self, number):
  36. super().__init__()
  37. self.number = number
  38. self.relu = nn.ReLU()
  39. def construct(self, x):
  40. a, b = self.number
  41. for _ in [a, b]:
  42. x = self.relu(x)
  43. return x
  44. def assignment_operator_base(number):
  45. """ assignment_operator_base """
  46. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  47. input_me = Tensor(input_np)
  48. x = number
  49. if isinstance(x, int):
  50. net = assignment1_Net(x)
  51. else:
  52. net = assignment2_Net(x)
  53. _executor.compile(net, input_me)
  54. def test_ME_assignment_operator_0010():
  55. """ test_ME_assignment_operator_0010 """
  56. assignment_operator_base(3)
  57. def test_ME_assignment_operator_0020():
  58. """ test_ME_assignment_operator_0020 """
  59. assignment_operator_base((1, 3))
  60. class unsupported_method_net(nn.Cell):
  61. """ unsupported_method_net definition """
  62. def __init__(self):
  63. super().__init__()
  64. self.relu = nn.ReLU()
  65. def construct(self, x):
  66. with open("a.txt") as f:
  67. f.read()
  68. return x
  69. def test_compile_unspported():
  70. """ test_compile_unspported """
  71. input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
  72. input_me = Tensor(input_np)
  73. net = unsupported_method_net()
  74. with pytest.raises(RuntimeError):
  75. _executor.compile(net, input_me)
  76. def test_parser_map_0002():
  77. class NetMap0002(nn.Cell):
  78. def __init__(self):
  79. super().__init__()
  80. self.relu = nn.ReLU()
  81. self.hypermap = C.Map()
  82. def mul(self, x=2, y=4):
  83. return x * y
  84. def construct(self, x):
  85. if map(self.mul) == 8:
  86. x = self.relu(x)
  87. return x
  88. input_np_x = np.random.randn(2, 3, 4, 5).astype(np.float32)
  89. input_me_x = Tensor(input_np_x)
  90. net = NetMap0002()
  91. with pytest.raises(TypeError):
  92. net(input_me_x)