check.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import sys
  18. import paddle
  19. import six
  20. import paddle.version as paddle_version
  21. from .logger import setup_logger
  22. logger = setup_logger(__name__)
  23. __all__ = [
  24. 'check_gpu', 'check_npu', 'check_xpu', 'check_mlu', 'check_version',
  25. 'check_config'
  26. ]
  27. def check_mlu(use_mlu):
  28. """
  29. Log error and exit when set use_mlu=true in paddlepaddle
  30. cpu/gpu/xpu/npu version.
  31. """
  32. err = "Config use_mlu cannot be set as true while you are " \
  33. "using paddlepaddle cpu/gpu/xpu/npu version ! \nPlease try: \n" \
  34. "\t1. Install paddlepaddle-mlu to run model on MLU \n" \
  35. "\t2. Set use_mlu as false in config file to run " \
  36. "model on CPU/GPU/XPU/NPU"
  37. try:
  38. if use_mlu and not paddle.is_compiled_with_mlu():
  39. logger.error(err)
  40. sys.exit(1)
  41. except Exception as e:
  42. pass
  43. def check_npu(use_npu):
  44. """
  45. Log error and exit when set use_npu=true in paddlepaddle
  46. version without paddle-custom-npu installed.
  47. """
  48. err = "Config use_npu cannot be set as true while you are " \
  49. "using paddlepaddle version without paddle-custom-npu " \
  50. "installed! \nPlease try: \n" \
  51. "\t1. Install paddle-custom-npu to run model on NPU \n" \
  52. "\t2. Set use_npu as false in config file to run " \
  53. "model on other devices supported."
  54. try:
  55. if use_npu and not 'npu' in paddle.device.get_all_custom_device_type():
  56. logger.error(err)
  57. sys.exit(1)
  58. except Exception as e:
  59. pass
  60. def check_xpu(use_xpu):
  61. """
  62. Log error and exit when set use_xpu=true in paddlepaddle
  63. cpu/gpu/npu version.
  64. """
  65. err = "Config use_xpu cannot be set as true while you are " \
  66. "using paddlepaddle cpu/gpu/npu version ! \nPlease try: \n" \
  67. "\t1. Install paddlepaddle-xpu to run model on XPU \n" \
  68. "\t2. Set use_xpu as false in config file to run " \
  69. "model on CPU/GPU/NPU"
  70. try:
  71. if use_xpu and not paddle.is_compiled_with_xpu():
  72. logger.error(err)
  73. sys.exit(1)
  74. except Exception as e:
  75. pass
  76. def check_gpu(use_gpu):
  77. """
  78. Log error and exit when set use_gpu=true in paddlepaddle
  79. cpu version.
  80. """
  81. err = "Config use_gpu cannot be set as true while you are " \
  82. "using paddlepaddle cpu version ! \nPlease try: \n" \
  83. "\t1. Install paddlepaddle-gpu to run model on GPU \n" \
  84. "\t2. Set use_gpu as false in config file to run " \
  85. "model on CPU"
  86. try:
  87. if use_gpu and not paddle.is_compiled_with_cuda():
  88. logger.error(err)
  89. sys.exit(1)
  90. except Exception as e:
  91. pass
  92. def check_version(version='2.2'):
  93. """
  94. Log error and exit when the installed version of paddlepaddle is
  95. not satisfied.
  96. """
  97. err = "PaddlePaddle version {} or higher is required, " \
  98. "or a suitable develop version is satisfied as well. \n" \
  99. "Please make sure the version is good with your code.".format(version)
  100. version_installed = [
  101. paddle_version.major, paddle_version.minor, paddle_version.patch,
  102. paddle_version.rc
  103. ]
  104. if version_installed == ['0', '0', '0', '0']:
  105. return
  106. version_split = version.split('.')
  107. length = min(len(version_installed), len(version_split))
  108. for i in six.moves.range(length):
  109. if version_installed[i] > version_split[i]:
  110. return
  111. if version_installed[i] < version_split[i]:
  112. raise Exception(err)
  113. def check_config(cfg):
  114. """
  115. Check the correctness of the configuration file. Log error and exit
  116. when Config is not compliant.
  117. """
  118. err = "'{}' not specified in config file. Please set it in config file."
  119. check_list = ['architecture', 'num_classes']
  120. try:
  121. for var in check_list:
  122. if not var in cfg:
  123. logger.error(err.format(var))
  124. sys.exit(1)
  125. except Exception as e:
  126. pass
  127. if 'log_iter' not in cfg:
  128. cfg.log_iter = 20
  129. return cfg