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 4.9 kB

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