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_initializer_fuzz.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_initializer_fuzz """
  16. import pytest
  17. import mindspore.nn as nn
  18. from mindspore import Model
  19. class Net(nn.Cell):
  20. """ Net definition """
  21. def __init__(self, in_str):
  22. a, b, c, d, e, f, g, h = in_str.strip().split()
  23. a = int(a)
  24. b = int(b)
  25. c = int(b)
  26. d = int(b)
  27. e = int(b)
  28. f = int(b)
  29. g = int(b)
  30. h = int(b)
  31. super(Net, self).__init__()
  32. self.conv = nn.Conv2d(a, b, c, pad_mode="valid")
  33. self.bn = nn.BatchNorm2d(d)
  34. self.relu = nn.ReLU()
  35. self.flatten = nn.Flatten()
  36. self.fc = nn.Dense(e * f * g, h)
  37. def construct(self, x):
  38. x = self.conv(x)
  39. x = self.bn(x)
  40. x = self.relu(x)
  41. x = self.flatten(x)
  42. out = self.fc(x)
  43. return out
  44. def test_shape_error():
  45. """ for fuzz test"""
  46. in_str = "3 22222222222222222222222222264 3 64 64 222 222 3"
  47. with pytest.raises(ValueError):
  48. Net(in_str)
  49. class LeNet5(nn.Cell):
  50. """ LeNet5 definition """
  51. def __init__(self, in_str):
  52. super(LeNet5, self).__init__()
  53. a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 = in_str.strip().split()
  54. a1 = int(a1)
  55. a2 = int(a2)
  56. a3 = int(a3)
  57. a4 = int(a4)
  58. a5 = int(a5)
  59. a6 = int(a6)
  60. a7 = int(a7)
  61. a8 = int(a8)
  62. a9 = int(a9)
  63. a10 = int(a10)
  64. a11 = int(a11)
  65. a12 = int(a12)
  66. a13 = int(a13)
  67. a14 = int(a14)
  68. a15 = int(a15)
  69. self.conv1 = nn.Conv2d(a1, a2, a3, pad_mode="valid")
  70. self.conv2 = nn.Conv2d(a4, a5, a6, pad_mode="valid")
  71. self.fc1 = nn.Dense(a7 * a8 * a9, a10)
  72. self.fc2 = nn.Dense(a11, a12)
  73. self.fc3 = nn.Dense(a13, a14)
  74. self.relu = nn.ReLU()
  75. self.max_pool2d = nn.MaxPool2d(kernel_size=a15)
  76. self.flatten = nn.Flatten()
  77. def construct(self, x):
  78. x = self.max_pool2d(self.relu(self.conv1(x)))
  79. x = self.max_pool2d(self.relu(self.conv2(x)))
  80. x = self.flatten(x)
  81. x = self.relu(self.fc1(x))
  82. x = self.relu(self.fc2(x))
  83. x = self.fc3(x)
  84. return x
  85. def test_shape_error_2():
  86. """ for fuzz test"""
  87. in_str = "3 6 5 6 -6 5 16 5 5 120 120 84 84 3 2"
  88. with pytest.raises(ValueError):
  89. net = LeNet5(in_str) # neural network
  90. Model(net)