From 6c952c26d31bdb37189459ec17f886e361cbebf6 Mon Sep 17 00:00:00 2001
From: Prakalp Srivastava <prakalps@google.com>
Date: Wed, 27 Mar 2019 13:46:03 -0700
Subject: [PATCH] pipeline promise version added

---
 .../src/promise/pipeline_promise.cc           | 145 ++++++++++++++++++
 1 file changed, 145 insertions(+)
 create mode 100644 llvm/projects/hpvm-tensor-rt/dnn_sources/src/promise/pipeline_promise.cc

diff --git a/llvm/projects/hpvm-tensor-rt/dnn_sources/src/promise/pipeline_promise.cc b/llvm/projects/hpvm-tensor-rt/dnn_sources/src/promise/pipeline_promise.cc
new file mode 100644
index 0000000000..c271879b1e
--- /dev/null
+++ b/llvm/projects/hpvm-tensor-rt/dnn_sources/src/promise/pipeline_promise.cc
@@ -0,0 +1,145 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+
+
+#include "../../tensor_runtime/include/tensor_runtime.h"
+#include "../include/utils.h"
+
+
+bool Opentuner_run = false;
+
+
+/* NOTE: Reference Architecture to use for profiling */
+void testPipeline(){
+
+  int total_runs = 1;
+  if(Opentuner_run){
+    total_runs = 1000000;
+  }
+
+  printf("********* Pipeline: Gaussian - Outline - Motion Blur - Emboss ********** \n");
+
+  int test_batch_size = 9145;
+  int H = 240;
+  int W = 300;
+
+  void* golden_output = readTrainedWeights("../pipeline/golden_output/caltech-gaussian-outline-motionblur-emboss.bin",
+                                        float_type,
+                                        test_batch_size, 1, H, W);
+
+  clearTensorMap();
+  for(int i = 0; i < total_runs; i++){
+    void* input = readTrainedWeights("../pipeline/datasets/caltech101_255_float32.bin",
+                                          float_type,
+                                          test_batch_size, 1, H, W);
+
+    // NOTE: Filter descriptors do NOT have batch size
+    // NOTE: First two dims are output channels (configurable), input channels (MUST match input channels)
+    // IMP: The output channels matches the trained model - not the Lenet arch proposed in Andrew Ng's class
+    void* gauss_filter = readTrainedWeights("../pipeline/filters/GaussianFilter.bin",
+                                            float_type, 1, 1, 9, 9);
+    void* outline_filter = readTrainedWeights("../pipeline/filters/OutlineFilter.bin",
+                                            float_type, 1, 1, 3, 3);
+    void* sharpen_filter = readTrainedWeights("../pipeline/filters/SharpenFilter.bin",
+                                            float_type, 1, 1, 3, 3);
+    void* motionblur_filter = readTrainedWeights("../pipeline/filters/MotionblurFilter.bin",
+                                            float_type, 1, 1, 9, 9);
+    void* emboss_filter = readTrainedWeights("../pipeline/filters/EmbossFilter.bin",
+                                            float_type, 1, 1, 5, 5);
+    void* emboss_bias = readTrainedWeights("../pipeline/filters/EmbossBias.bin",
+                                            float_type, 1, 1, 1, 1);
+
+    if(Opentuner_run){
+
+      char* myfifo = "/tmp/myfifo";
+      int fd = open(myfifo, O_RDONLY);
+
+      int ret_val = fcntl(fd, F_GETFD);
+      if(ret_val == -1){
+        printf("Invalid descriptor \n");
+        abort();
+      }
+
+      char str[100];
+      read(fd, str, 80);
+      if(strcmp(str, "stop_run") == 0){
+        abort();
+      }
+
+      close(fd);
+    }
+
+    readOpenTunerFlags("opentuner_flags"); // Resets the OpenTuner counters
+
+
+    void* gaussian_out = ConvLayer_PROMISE(input, 0, 255, gaussian_filter, 0, 1, NULL, 0, 0,
+                                           4, 4, 1, 1,
+                                           0, 0, // pool? no pooling needed
+                                           0,
+                                           0, 255, // out min max? should we assume 0 - 255 for all filters.
+                                                   // Will have to rerun to generate golden output
+                                           9);
+    void* outline_out = ConvLayer_PROMISE(gaussian_out, 0, 255, outline_filter, -1, 8, NULL, 0, 0,
+                                           1, 1, 1, 1,
+                                           0, 0, // pool? no pooling needed
+                                           0,
+                                           0, 255, // out min max? should we assume 0 - 255 for all filters.
+                                                   // Will have to rerun to generate golden output
+                                           9);
+
+    void* motionblur_out = ConvLayer_PROMISE(outline_out, 0, 255, motionblur_filter, 0, 1, NULL, 0, 0,
+                                           4, 4, 1, 1,
+                                           0, 0, // pool? no pooling needed
+                                           0,
+                                           0, 255, // out min max? should we assume 0 - 255 for all filters.
+                                                   // Will have to rerun to generate golden output
+                                           9);
+
+    void* result = ConvLayer_PROMISE(motionblur_out, 0, 255, emboss_filter, -1, 1, emboss_bias, 128, 128,
+                                           2, 2, 1, 1,
+                                           0, 0, // pool? no pooling needed
+                                           0,
+                                           0, 255, // out min max? should we assume 0 - 255 for all filters.
+                                                   // Will have to rerun to generate golden output
+                                           9);
+
+    computeAccuracy2(golden_output, test_batch_size, result);
+
+    freeOutputTensors();
+
+    if(Opentuner_run){
+
+      char* myfifo = "/tmp/myfifo";
+      int fd_out = open(myfifo, O_WRONLY);
+      int ret_val = fcntl(fd_out, F_GETFD);
+      if(ret_val == -1){
+        printf("Invalid descriptor \n");
+        abort();
+      }
+      const char* str = "completed***!\n\0";
+      write(fd_out, str, 80);
+      close(fd_out);
+    }
+  }
+}
+
+
+int main(int argc, char* argv[]){
+
+  if(argc > 1)
+    Opentuner_run = true;
+
+  llvm_hpvm_initTensorRt(0);
+
+  testPipeline();
+
+  llvm_hpvm_cleanupTensorRt();
+
+  return 0;
+}
+
-- 
GitLab