test_1.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import tempfile
  2. import time
  3. import pandas as pd
  4. import pickle
  5. import subprocess
  6. from imc_utils.pps_e36311a import PPS_E36311A
  7. from imc_utils.smu_b2902a import SMU_B2902A
  8. from imc_utils.build_config.cortex_m33 import BuildConfigM33
  9. from imc_utils.build_config.test_env import TestEnv
  10. from imc_utils.serial_watch import SerialWatcher
  11. WORKSPACE_ROOT = "/home/ybkim/workspace/imc/imc_freertos_app_m33"
  12. NVM_RESET_BIN = f"{WORKSPACE_ROOT}/imc/utils/nvm_reset.elf"
  13. OPENOCD_SCRIPT = f"{WORKSPACE_ROOT}/imc_freertos_app_m33.cfg"
  14. def measure_original(benchmark, pps, smu):
  15. config = get_default_build_config(benchmark)
  16. print("\n===== A. measure original program =====\n")
  17. target_current = 0.1
  18. currs = measure(config, pps, smu, target_current, repeat=2)
  19. with open(f"output/test_1_{benchmark}_original.pickle", "wb") as f:
  20. pickle.dump(currs, f)
  21. def measure_ckpt(benchmark, pps, smu: SMU_B2902A):
  22. config = get_default_build_config(benchmark)
  23. config.insert_compiler_checkpoints = False
  24. config.use_checkpoint_pass_counter = False
  25. config.split_loop = False
  26. config.enable_adaptive_loop_pass_count = False
  27. config.use_checkpoint_voltage_check = False
  28. config.max_loop_ids = 15
  29. config.enable_dynamic_checkpoint = True
  30. print("\n===== B. measure checkpoint-instrumented program =====\n")
  31. target_current = 0.014
  32. # target_current = 0.1
  33. wait_s = 1
  34. currs = measure(config, pps, smu, target_current, repeat=5, wait_before_start_s=wait_s)
  35. with open(f"output/test_1_{benchmark}_ckpt.pickle", "wb") as f:
  36. pickle.dump(currs, f)
  37. def measure(config, pps: PPS_E36311A, smu: SMU_B2902A, target_current, repeat=1, wait_before_start_s=0):
  38. env = program_with_config(config, pps, smu)
  39. benchmark = config.bench_name
  40. pps.set_voltage(3.3, 1)
  41. pps.set_current(target_current, 1)
  42. watcher = SerialWatcher(benchmark, repeat)
  43. if wait_before_start_s > 0:
  44. print(f" > wait for {wait_before_start_s} secs")
  45. time.sleep(wait_before_start_s)
  46. env.resume_board(terminate=True, verbose=False)
  47. print(" > start measuring\n")
  48. smu.start_measure()
  49. if wait_before_start_s == 0:
  50. env.resume_board(terminate=True, verbose=False)
  51. records = watcher.run()
  52. smu.stop_measure_only()
  53. result = smu.fetch_curr()
  54. print("\n > measure finished")
  55. currs = pd.DataFrame(result)[0]
  56. return currs
  57. def main():
  58. pps = PPS_E36311A()
  59. smu = SMU_B2902A(voltage=0, use_external_power=True)
  60. smu.setup_sense()
  61. smu.set_current_limit(0.1)
  62. # benchmarks = ["vFFT", "vConv2d", "vCrc", "vBasicMath", "vSha", "vStringSearch", "vAes", "vMatMul"]
  63. # benchmarks = ["vCrc"]
  64. benchmarks = ["vFFT", "vCrc", "vBasicMath", "vSha"]
  65. total = len(benchmarks)
  66. for i, benchmark in enumerate(benchmarks):
  67. print(f"\n+++ ({i+1}/{total}) {benchmark} +++")
  68. measure_original(benchmark, pps, smu)
  69. measure_ckpt(benchmark, pps, smu)
  70. def program_with_config(config, pps, smu):
  71. env = TestEnv(WORKSPACE_ROOT, NVM_RESET_BIN, OPENOCD_SCRIPT)
  72. smu.power_on()
  73. pps.set_voltage(3.3, 1)
  74. pps.set_current(0.1, 1)
  75. pps.output_on(1)
  76. time.sleep(1)
  77. print(" > build and program binary")
  78. with tempfile.TemporaryDirectory() as build_dir:
  79. binary = env.build_binary(config, build_dir, redirect_file=subprocess.DEVNULL)
  80. env.clear_nvm_and_load_binary(binary, resume=False, verbose=False)
  81. print(" > program finished")
  82. return env
  83. def get_bench_repeat_count(benchmark):
  84. d = {
  85. "vFFT": 50,
  86. "vConv2d": 15000,
  87. "vCrc": 5000,
  88. "vBasicMath": 2,
  89. "vSha": 200,
  90. "vStringSearch": 3000,
  91. "vAes": 6000,
  92. "vMatMul": 20000,
  93. }
  94. return d[benchmark]
  95. def get_default_build_config(benchmark):
  96. config = BuildConfigM33()
  97. config.bench_name = benchmark
  98. config.insert_compiler_checkpoints = False
  99. config.enable_extension = True
  100. config.use_checkpoint_pass_counter = False
  101. config.use_checkpoint_voltage_check = False
  102. config.bench_infinite_loop = True
  103. config.print_recovery_message = True
  104. config.split_loop = False
  105. config.enable_static_loop_pass_count = False
  106. config.enable_adaptive_loop_pass_count = False
  107. config.print_stats = False
  108. config.custom_unroll = False
  109. config.enable_dynamic_checkpoint = False
  110. config.bench_repeat_count = get_bench_repeat_count(benchmark)
  111. return config
  112. if __name__ == "__main__":
  113. main()