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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2019 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. """
  16. Function:
  17. Test mindconverter to convert user's PyTorch network script.
  18. Usage:
  19. pytest tests/st/func/mindconverter
  20. """
  21. import difflib
  22. import os
  23. import sys
  24. import pytest
  25. from mindinsight.mindconverter.converter import main
  26. @pytest.mark.usefixtures('create_output_dir')
  27. class TestConverter:
  28. """Test Converter module."""
  29. @classmethod
  30. def setup_class(cls):
  31. """Setup method."""
  32. cls.script_dir = os.path.join(os.path.dirname(__file__), 'data')
  33. pytorch_base_dir = os.path.dirname(__file__).split('/')[:3]
  34. cls.pytorch_dir = \
  35. '/'.join(pytorch_base_dir + ['share-data', 'dataset', 'mindinsight_dataset', 'resnet50'])
  36. sys.path.insert(0, cls.script_dir)
  37. @classmethod
  38. def teardown_class(cls):
  39. """Teardown method."""
  40. sys.path.remove(cls.script_dir)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_arm_ascend_training
  43. @pytest.mark.platform_x86_gpu_training
  44. @pytest.mark.platform_x86_ascend_training
  45. @pytest.mark.platform_x86_cpu
  46. @pytest.mark.env_single
  47. def test_convert_lenet(self, output):
  48. """Test LeNet script of the PyTorch convert to MindSpore script"""
  49. script_filename = "lenet_script.py"
  50. expect_filename = "lenet_converted.py"
  51. files_config = {
  52. 'root_path': self.script_dir,
  53. 'in_files': [os.path.join(self.script_dir, script_filename)],
  54. 'outfile_dir': output,
  55. 'report_dir': output
  56. }
  57. main(files_config)
  58. assert os.path.isfile(os.path.join(output, script_filename))
  59. with open(os.path.join(output, script_filename)) as converted_f:
  60. converted_source = converted_f.readlines()
  61. with open(os.path.join(self.script_dir, expect_filename)) as expect_f:
  62. expect_source = expect_f.readlines()
  63. diff = difflib.ndiff(converted_source, expect_source)
  64. diff_lines = 0
  65. for line in diff:
  66. if line.startswith('+'):
  67. diff_lines += 1
  68. converted_ratio = 100 - (diff_lines * 100) / (len(expect_source))
  69. assert converted_ratio >= 80