BingBertSquad_test_common.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # coding=utf-8
  2. # Copyright (c) 2019, The Microsoft DeepSpeed Team. All rights reserved.
  3. #
  4. import unittest
  5. import subprocess
  6. import os
  7. import time
  8. import re
  9. class BaseTestCase(unittest.TestCase):
  10. def __init__(self, methodName="DeepSpeed performance test"):
  11. super(BaseTestCase, self).__init__(methodName)
  12. self.test_dir = "./test"
  13. self.baseline_dir = "./baseline"
  14. self.timestr = time.strftime("%Y%m%d-%H%M%S")
  15. def gen_output_name(self, test_config, prefix):
  16. other_args = test_config["other_args"] if "other_args" in test_config else ""
  17. zero_args = "_zero" if "zero" in test_config and test_config["zero"] else ""
  18. other_args = other_args.strip(' -\\').replace(" ", "").replace("\"", "")
  19. if other_args:
  20. other_args = "_" + other_args
  21. if test_config["deepspeed"]:
  22. file_name = "_gpu{0}_{1}_ds{2}-{3}.log".format(test_config["gpus"],
  23. other_args,
  24. zero_args,
  25. self.timestr)
  26. save_dir = self.test_dir
  27. else:
  28. file_name = "_gpu{0}_{1}.log".format(test_config["gpus"], other_args)
  29. save_dir = self.baseline_dir
  30. return os.path.join(save_dir, prefix + file_name)
  31. def ensure_directory_exists(self, filename):
  32. dirname = os.path.dirname(filename)
  33. if not os.path.exists(dirname):
  34. os.makedirs(dirname)
  35. def clean_test_env(self):
  36. cmd = "dlts_ssh pkill -9 -f /usr/bin/python"
  37. print(cmd)
  38. subprocess.run(cmd, shell=True, check=False, executable='/bin/bash')
  39. time.sleep(20)
  40. def run_BingBertSquad_test(self, test_config, output):
  41. ds_flag = " -d --deepspeed_config " + test_config["json"] if test_config[
  42. "deepspeed"] else " "
  43. other_args = " " + test_config[
  44. "other_args"] if "other_args" in test_config else " "
  45. cmd = "./run_BingBertSquad_sanity.sh -e 1 -g {0} {1} {2}".format(
  46. test_config["gpus"],
  47. other_args,
  48. ds_flag)
  49. self.ensure_directory_exists(output)
  50. with open(output, "w") as f:
  51. print(cmd)
  52. subprocess.run(cmd,
  53. shell=True,
  54. check=False,
  55. executable='/bin/bash',
  56. stdout=f,
  57. stderr=f)