check.py 3.4 KB

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