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.

cpu_evaluation_tools.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. """
  10. purpose: Used to simply measure CPU performance by running several basic models.
  11. how to use: python3 cpu_evaluation_tools.py --help for more details, now need to args:
  12. --load_and_run_file: path of load_and_run binary, please refs to ../scripts/cmake-build/BUILD_README.md to build it.
  13. --models_dir: path of model directory.
  14. how to config test device info: config device[name/login_name/ip/port/thread_number].
  15. """
  16. import argparse
  17. import logging
  18. import os
  19. import re
  20. import subprocess
  21. # test device
  22. device = {
  23. "name": "hwmt40p",
  24. "login_name": "hwmt40p-K9000-maliG78",
  25. "ip": "box86.br.megvii-inc.com",
  26. "port": 2200,
  27. "thread_number": 3,
  28. }
  29. # test models
  30. test_cpu_models = [
  31. "inceptionv2",
  32. "mobilenetv1",
  33. "mobilenetv2",
  34. "resnet18",
  35. "resnet50",
  36. "shufflenetv2",
  37. "vgg16",
  38. ]
  39. class SshConnector:
  40. """imp ssh control master connector"""
  41. ip = None
  42. port = None
  43. login_name = None
  44. def setup(self, login_name, ip, port):
  45. self.ip = ip
  46. self.login_name = login_name
  47. self.port = port
  48. def copy(self, src_list, dst_dir):
  49. assert isinstance(src_list, list), "code issue happened!!"
  50. assert isinstance(dst_dir, str), "code issue happened!!"
  51. for src in src_list:
  52. cmd = 'rsync --progress -a -e "ssh -p {}" {} {}@{}:{}'.format(
  53. self.port, src, self.login_name, self.ip, dst_dir
  54. )
  55. logging.debug("ssh run cmd: {}".format(cmd))
  56. subprocess.check_call(cmd, shell=True)
  57. def cmd(self, cmd):
  58. output = ""
  59. assert isinstance(cmd, list), "code issue happened!!"
  60. for sub_cmd in cmd:
  61. p_cmd = 'ssh -p {} {}@{} "{}" '.format(
  62. self.port, self.login_name, self.ip, sub_cmd
  63. )
  64. logging.debug("ssh run cmd: {}".format(p_cmd))
  65. output = output + subprocess.check_output(p_cmd, shell=True).decode("utf-8")
  66. return output
  67. def get_finally_bench_resulut_from_log(raw_log) -> float:
  68. # raw_log --> avg_time=23.331ms -->23.331ms
  69. h = re.findall(r"avg_time=.*ms ", raw_log)[-1][9:]
  70. # to 23.331
  71. h = h[: h.find("ms")]
  72. # to float
  73. h = float(h)
  74. return h
  75. def main():
  76. parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
  77. parser.add_argument("--models_dir", help="models dir", required=True)
  78. parser.add_argument(
  79. "--load_and_run_file", help="path for load_and_run", required=True
  80. )
  81. args = parser.parse_args()
  82. assert os.path.isdir(
  83. args.models_dir
  84. ), "invalid args for models_dir, need a dir for models"
  85. assert os.path.isfile(args.load_and_run_file), "invalid args for load_and_run_file"
  86. for m in test_cpu_models:
  87. assert os.path.isfile(
  88. os.path.join(args.models_dir, m)
  89. ), "invalid args for models_dir, need put model: {} to args.models_dir".format(
  90. test_cpu_models
  91. )
  92. # init device
  93. ssh = SshConnector()
  94. ssh.setup(device["login_name"], device["ip"], device["port"])
  95. # create test dir
  96. workspace = "cpu_evaluation_workspace"
  97. ssh.cmd(["mkdir -p {}".format(workspace)])
  98. # copy load_and_run_file
  99. ssh.copy([args.load_and_run_file], workspace)
  100. # call test
  101. result = []
  102. for m in test_cpu_models:
  103. m_path = os.path.join(args.models_dir, m)
  104. # copy model file
  105. ssh.copy([m_path], workspace)
  106. # run single thread
  107. sub_b = ["-cpu", "-multithread {}".format(device["thread_number"])]
  108. for b in sub_b:
  109. cmd = []
  110. cmd0 = "cd {} && rm -rf fastrun.cache".format(workspace)
  111. cmd1 = "cd {} && ./load_and_run {} --fast-run --fast_run_algo_policy fastrun.cache --iter 1 --warmup-iter 1 --no-sanity-check --weight-preprocess".format(
  112. workspace, m, b
  113. )
  114. cmd2 = "cd {} && ./load_and_run {} {} --fast_run_algo_policy fastrun.cache --iter 20 --warmup-iter 5 --no-sanity-check --weight-preprocess --record-comp-seq".format(
  115. workspace, m, b
  116. )
  117. cmd.append(cmd0)
  118. cmd.append(cmd1)
  119. cmd.append(cmd2)
  120. raw_log = ssh.cmd(cmd)
  121. # logging.debug(raw_log)
  122. ret = get_finally_bench_resulut_from_log(raw_log)
  123. logging.debug("model: {} with backend: {} result is: {}".format(m, b, ret))
  124. result.append(ret)
  125. total_time = 0.0
  126. for r in result:
  127. total_time += r
  128. logging.debug("total time is: {}".format(total_time))
  129. score = 100000.0 / total_time * 1000
  130. logging.debug("device: {} score is: {}".format(device["name"], score))
  131. if __name__ == "__main__":
  132. LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
  133. DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
  134. logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
  135. main()