3
0

check.py 4.0 KB

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