test_2_new.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.015
  32. wait_s = 3
  33. repeat = 200
  34. records = measure(config, pps, smu, target_current, repeat=repeat, wait_before_start_s=wait_s)
  35. records = pd.DataFrame(records)
  36. columns = ["bench_name", "time_taken", "outputs", "is_correct"]
  37. print(records[columns])
  38. with open(f"output/test_2_{benchmark}_ckpt.pickle", "wb") as f:
  39. pickle.dump(records, f)
  40. def measure(config, pps: PPS_E36311A, smu: SMU_B2902A, target_current, repeat=1, wait_before_start_s=0):
  41. env = program_with_config(config, pps, smu)
  42. benchmark = config.bench_name
  43. pps.set_voltage(3.3, 1)
  44. pps.set_current(target_current, 1)
  45. watcher = SerialWatcher(benchmark, repeat)
  46. if wait_before_start_s > 0:
  47. print(f" > wait for {wait_before_start_s} secs")
  48. time.sleep(wait_before_start_s)
  49. env.resume_board(terminate=True, verbose=False)
  50. print(" > start measuring\n")
  51. if wait_before_start_s == 0:
  52. env.resume_board(terminate=True, verbose=False)
  53. records = watcher.run()
  54. print("\n > measure finished")
  55. return records
  56. def main():
  57. pps = PPS_E36311A()
  58. smu = SMU_B2902A(voltage=0, use_external_power=True)
  59. smu.setup_sense()
  60. smu.set_current_limit(0.1)
  61. benchmarks = ["vFFT", "vCrc", "vAes", "vSha"]
  62. benchmarks = ["vSha"]
  63. total = len(benchmarks)
  64. for i, benchmark in enumerate(benchmarks):
  65. print(f"\n+++ ({i+1}/{total}) {benchmark} +++")
  66. # measure_original(benchmark, pps, smu)
  67. measure_ckpt(benchmark, pps, smu)
  68. def program_with_config(config, pps, smu):
  69. env = TestEnv(WORKSPACE_ROOT, NVM_RESET_BIN, OPENOCD_SCRIPT)
  70. smu.power_on()
  71. pps.set_voltage(3.3, 1)
  72. pps.set_current(0.1, 1)
  73. pps.output_on(1)
  74. time.sleep(1)
  75. print(" > build and program binary")
  76. with tempfile.TemporaryDirectory() as build_dir:
  77. binary = env.build_binary(config, build_dir, redirect_file=subprocess.DEVNULL)
  78. env.clear_nvm_and_load_binary(binary, resume=False, verbose=False)
  79. print(" > program finished")
  80. return env
  81. def get_bench_repeat_count(benchmark):
  82. d = {
  83. "vFFT": 10,
  84. "vConv2d": 15000,
  85. "vCrc": 1000,
  86. "vBasicMath": 1,
  87. "vSha": 40,
  88. "vStringSearch": 3000,
  89. "vAes": 1300,
  90. "vMatMul": 20000,
  91. }
  92. return d[benchmark]
  93. def get_default_build_config(benchmark):
  94. config = BuildConfigM33()
  95. config.bench_name = benchmark
  96. config.insert_compiler_checkpoints = False
  97. config.enable_extension = True
  98. config.use_checkpoint_pass_counter = False
  99. config.use_checkpoint_voltage_check = False
  100. config.bench_infinite_loop = True
  101. config.print_recovery_message = True
  102. config.split_loop = False
  103. config.enable_static_loop_pass_count = False
  104. config.enable_adaptive_loop_pass_count = False
  105. config.print_stats = False
  106. config.custom_unroll = False
  107. config.enable_dynamic_checkpoint = False
  108. config.bench_repeat_count = get_bench_repeat_count(benchmark)
  109. return config
  110. if __name__ == "__main__":
  111. main()