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_forward_call.py 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 forward_call module."""
  16. import ast
  17. import textwrap
  18. from mindinsight.mindconverter.forward_call import ForwardCall
  19. class TestForwardCall:
  20. """Test the class of ForwardCall."""
  21. source = textwrap.dedent("""\
  22. import a
  23. import a.nn as nn
  24. import a.nn.functional as F
  25. class TestNet:
  26. def __init__(self):
  27. self.conv1 = nn.Conv2d(3, 6, 5)
  28. self.conv2 = nn.Conv2d(6, 16, 5)
  29. self.fc1 = nn.Linear(16 * 5 * 5, 120)
  30. self.fc2 = nn.Linear(120, 84)
  31. self.fc3 = nn.Linear(84, 10)
  32. def forward(self, x):
  33. out = self.forward1(x)
  34. return out
  35. def forward1(self, x):
  36. out = F.relu(self.conv1(x))
  37. out = F.max_pool2d(out, 2)
  38. out = F.relu(self.conv2(out))
  39. out = F.max_pool2d(out, 2)
  40. out = out.view(out.size(0), -1)
  41. out = F.relu(self.fc1(out))
  42. out = F.relu(self.fc2(out))
  43. out = self.fc3(out)
  44. return out
  45. """)
  46. def test_process(self):
  47. """Test the function of visit ast tree to find out forward functions."""
  48. ast_tree = ast.parse(self.source)
  49. forward_call = ForwardCall(ast_tree)
  50. expect_calls = ['TestNet.forward',
  51. 'TestNet.forward1',
  52. 'F.relu',
  53. 'TestNet.conv1',
  54. 'F.max_pool2d',
  55. 'TestNet.conv2',
  56. 'out.view',
  57. 'out.size',
  58. 'TestNet.fc1',
  59. 'TestNet.fc2',
  60. 'TestNet.fc3',
  61. ]
  62. expect_calls.sort()
  63. real_calls = list(forward_call.calls.keys())
  64. real_calls.sort()
  65. assert real_calls == expect_calls