From e3d685d3e618e70763e612d7d8328968ea9e2b96 Mon Sep 17 00:00:00 2001
From: Yifan Zhao <yifanz16@illinois.edu>
Date: Sat, 13 Mar 2021 22:27:43 -0600
Subject: [PATCH] Some little dance with hpvm-rt-controller

---
 .../tensor_runtime/src/hpvm-rt-controller.cpp       | 11 ++++++-----
 .../torch2hpvm/template_hpvm_inspect.cpp.in         | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp b/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
index 66e8e3d1ba..5e1fbc9919 100644
--- a/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
+++ b/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
@@ -268,10 +268,12 @@ double ProfileInfo::getCurrentIterationComputeEnergy() {
 void ProfileInfo::set_out_file_name(std::string &str) { out_file_name = str; }
 
 void ProfileInfo::printToFile() {
-
   INFO("Writing Runtime Profile Info File...\n");
-  std::ofstream s_out(out_file_name.c_str());
 
+  if (control_time_info.size() == 0)
+    return;
+
+  std::ofstream s_out(out_file_name.c_str());
   if (!s_out) {
     ERROR("Failed to open output file.");
     abort();
@@ -1390,14 +1392,14 @@ uint32_t *hpvm_rt_readLabelsBatch_cached(const char *labels_file, int start,
       ERROR("Data file %s is not found. Aborting...\n", labels_file);
       abort();
     }
-    
+
     // Get number of labels
     fseek(file, 0, SEEK_END);
     long size = ftell(file);
     fseek(file, 0, SEEK_SET); // return file pointer to beginning
 
     // Allocate memory for labels
-    labels_from_file = (uint32_t *) malloc(size);
+    labels_from_file = (uint32_t *)malloc(size);
     if (labels_from_file == NULL) {
       ERROR("Memory allocation for labels unsucessfull. Aborting...\n");
       abort();
@@ -1474,7 +1476,6 @@ float hpvm_rt_computeAccuracy3(uint32_t *labels, void *result_ptr) {
   return accuracy;
 }
 
-
 #define llvm_hpvm_invokeRtControl_BASE llvm_hpvm_invokeRtControl
 //#define llvm_hpvm_invokeRtControl_ADJUST_PR llvm_hpvm_invokeRtControl
 //#define llvm_hpvm_invokeRtControl_ITERATE llvm_hpvm_invokeRtControl
diff --git a/hpvm/projects/torch2hpvm/torch2hpvm/template_hpvm_inspect.cpp.in b/hpvm/projects/torch2hpvm/torch2hpvm/template_hpvm_inspect.cpp.in
index 4fb25a5574..94a8e0a534 100644
--- a/hpvm/projects/torch2hpvm/torch2hpvm/template_hpvm_inspect.cpp.in
+++ b/hpvm/projects/torch2hpvm/torch2hpvm/template_hpvm_inspect.cpp.in
@@ -1,3 +1,4 @@
+#include <fstream>
 #include <string>
 #include <array>
 #include <hpvm.h>
@@ -49,6 +50,11 @@ void fifo_write_batch(FILE *fp, void *output_ptr) {
   fwrite(output->host_data, 1, output->size_in_bytes, fp);
 }
 
+void write_accuracy(float accuracy) {
+  std::ofstream fout("final_accuracy");
+  fout << std::fixed << accuracy;
+}
+
 {% for node in nodes %}
 void var_{{node.idx}}_node(
 {%- for n in range(node.input_size) -%}
@@ -134,6 +140,7 @@ int main(){
 
     // Keep this open so the other side knows we have more batches to write
     auto* fp = open_fifo("{{fifo_path_w}}", "wb");
+    float total_accuracy = 0;
     for (int i = 0; i < batch_count; i++){
       int start = i * batch_size, end = start + batch_size;
       void *{{input_name}} = readInputBatch(input_pth, 0, start, end, {{input_shape|join(', ')}});
@@ -145,11 +152,15 @@ int main(){
       void *result = static_cast<RootIn*>(args)->r.tensor;
       hpvm_request_tensor(result, 0);
 
-      llvm_hpvm_invokeRtControl(result, labels_pth, start, end);
+      uint32_t* labels = readLabelsBatch3(labels_pth, start, end);
+      float accuracy = computeAccuracy3(labels, result);
+      total_accuracy += accuracy * batch_size;
+
       fifo_write_batch(fp, result);
       freeBatchMemory();
     }
     fclose(fp);
+    write_accuracy(total_accuracy / input_size);
     __hpvm__cleanup();
   }
 
-- 
GitLab