From 72cbd74314604aedd3626b1faa90556280df798b Mon Sep 17 00:00:00 2001
From: Hashim Sharif <hsharif3@miranda.cs.illinois.edu>
Date: Sun, 21 Mar 2021 03:03:58 -0500
Subject: [PATCH] Adding CPU approximation test cases - paritally failing

---
 .../dnn_sources/src/unit_tests.cc             | 111 ++++++++++++++----
 1 file changed, 85 insertions(+), 26 deletions(-)

diff --git a/hpvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc b/hpvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc
index ea959342a4..2a2e1c291c 100644
--- a/hpvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc
+++ b/hpvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc
@@ -5,6 +5,7 @@
 #include <vector>
 #include <string.h>
 #include "tensor_runtime.h"
+#include "tensor_cpu_runtime.h"
 #include "utils.h"
 #include "tensor_custom_ops_cpu.h"
 
@@ -628,7 +629,8 @@ void testSampleFilter() {
 }
 
 void testPerforationCalls(void *input, void *filter, int pad_h, int pad_w,
-                          int stride_h, int stride_w, int row, int col) {
+                          int stride_h, int stride_w, int row, int col,
+			  UnitTestResults &unitTestResults) {
 
   float interpolation_rate = 1.0;
   for (int offset = 0; offset < 2; offset++) {
@@ -668,6 +670,19 @@ void testPerforationCalls(void *input, void *filter, int pad_h, int pad_w,
     printf("\nConvApprox Result :");
     printTensorValues(res);
 
+
+    hpvm_request_tensor(input, HOST);
+    hpvm_request_tensor(filter, HOST);
+    
+    void *res_cpu = tensorConvApproxCPU(input, filter,
+					pad_h, pad_w,
+					stride_h, stride_w,
+					1, 1, row, col, 1, offset);
+
+    printf("\nConvApproxCPU Result :");
+    printTensorValues(res_cpu);
+    
+    
     void *res_half =
         tensorConvApproxHalf2(input, filter, pad_h, pad_w, stride_h, stride_w,
                               1, 1, row, col, 1, offset);
@@ -676,8 +691,34 @@ void testPerforationCalls(void *input, void *filter, int pad_h, int pad_w,
 
     printf("\nConvApproxHalf2 Result :");
     printTensorValues(res_half);
+
+
+    std::string suffix =
+        std::string(" pad_h = ") + std::to_string(pad_h) +
+        std::string(" pad_w = ") + std::to_string(pad_w) +
+        std::string(" stride_h = ") + std::to_string(stride_h) +
+        std::string(" stride_w = ") + std::to_string(stride_w) +
+        std::string(" row = ") + std::to_string(row) +
+        std::string(" col = ") + std::to_string(col) +
+        std::string(" offset = ") + std::to_string(offset);
+
+    
+    std::string test_name = std::string("PERF_FP32 ") + suffix;
+
+    unitTestResults.compareTensors((Tensor *)res, (Tensor *)res_sim, 0.05,
+                                   test_name);
+
+    std::string fp16_test_name = std::string("PERF_FP16 ") + suffix;
+    unitTestResults.compareTensors((Tensor *)res_half, (Tensor *)res_sim, 0.1,
+                                   fp16_test_name);
+
+    std::string cpu_test_name = std::string("PERF_CPU ") + suffix;
+    unitTestResults.compareTensors((Tensor *) res_cpu, (Tensor *)res_sim, 0.05,
+                                   cpu_test_name);
+    
   }
 
+
   printf("\n\n\n--- End of Test \n\n\n");
 }
 
@@ -711,17 +752,18 @@ void testPerforation(UnitTestResults &unitTestResults) {
   host_ptr[26] = 2;
   */
 
-  testPerforationCalls(input, filter, 0, 0, 1, 1, 1, 2);
+  testPerforationCalls(input, filter, 0, 0, 1, 1, 1, 2, unitTestResults);
 
-  testPerforationCalls(input, filter, 0, 0, 1, 1, 2, 1);
+  testPerforationCalls(input, filter, 0, 0, 1, 1, 2, 1, unitTestResults);
 
-  testPerforationCalls(input, filter, 1, 1, 1, 1, 1, 3);
+  testPerforationCalls(input, filter, 1, 1, 1, 1, 1, 3, unitTestResults);
 
-  testPerforationCalls(input, filter, 1, 1, 1, 1, 3, 1);
+  testPerforationCalls(input, filter, 1, 1, 1, 1, 3, 1, unitTestResults);
 
-  testPerforationCalls(input, filter, 1, 1, 2, 2, 1, 4);
+  testPerforationCalls(input, filter, 1, 1, 2, 2, 1, 4, unitTestResults);
 
-  testPerforationCalls(input, filter, 1, 1, 2, 2, 4, 1);
+  testPerforationCalls(input, filter, 1, 1, 2, 2, 4, 1, unitTestResults);
+  
 }
 
 void testSampling() {
@@ -783,6 +825,7 @@ void testSampling() {
 
 void testSamplingCalls(void *input, void *filter, int pad_h, int pad_w,
                        int stride_h, int stride_w, int skip_every,
+		       std::string filter_string, 
                        UnitTestResults &unitTestResults) {
 
   float interpolation_rate = 1.0;
@@ -825,6 +868,17 @@ void testSamplingCalls(void *input, void *filter, int pad_h, int pad_w,
     printf("\nConvApprox Result :");
     printTensorValues(res);
 
+
+    hpvm_request_tensor(input, HOST);
+    hpvm_request_tensor(filter, HOST);
+
+    void *res_cpu = tensorConvApproxCPU(input, filter, pad_h, pad_w,
+					stride_h, stride_w, 1, 1, 1, 1, skip_every, offset); 
+    
+    printf("\nConvApproxCPU Result :");
+    printTensorValues(res_cpu);
+
+   
     void *res_half =
         tensorConvApproxHalf2(input, filter, pad_h, pad_w, stride_h, stride_w,
                               1, 1, 1, 1, skip_every, offset);
@@ -835,6 +889,7 @@ void testSamplingCalls(void *input, void *filter, int pad_h, int pad_w,
     printTensorValues(res_half);
 
     std::string suffix =
+      "filter = " + std::string(filter_string) +
         std::string(" pad_h = ") + std::to_string(pad_h) +
         std::string(" pad_w = ") + std::to_string(pad_w) +
         std::string(" stride_h = ") + std::to_string(stride_h) +
@@ -844,12 +899,16 @@ void testSamplingCalls(void *input, void *filter, int pad_h, int pad_w,
 
     std::string test_name = std::string("SAMP_FP32 ") + suffix;
 
-    unitTestResults.compareTensors((Tensor *)res, (Tensor *)res_sim, 0.01,
+    unitTestResults.compareTensors((Tensor *)res, (Tensor *)res_sim, 0.05,
                                    test_name);
 
     std::string fp16_test_name = std::string("SAMP_FP16 ") + suffix;
-    unitTestResults.compareTensors((Tensor *)res_half, (Tensor *)res_sim, 0.04,
+    unitTestResults.compareTensors((Tensor *)res_half, (Tensor *)res_sim, 0.1,
                                    fp16_test_name);
+
+    std::string cpu_test_name = std::string("SAMP_CPU ") + suffix;
+    unitTestResults.compareTensors((Tensor *) res_cpu, (Tensor *)res_sim, 0.05,
+                                   cpu_test_name);
   }
 
   printf("\n\n\n --- End of Test \n\n\n");
@@ -869,7 +928,7 @@ void testSampling_3_3(UnitTestResults &unitTestResults) {
   fillTensorWithVal(filter, 1);
 
   float *host_ptr = (float *)((struct Tensor *)filter)->host_data;
-  host_ptr[0] = 2;
+  host_ptr[0] = 10; 
   host_ptr[2] = 2;
   host_ptr[4] = 2;
   host_ptr[6] = 2;
@@ -882,28 +941,28 @@ void testSampling_3_3(UnitTestResults &unitTestResults) {
   host_ptr[20] = 2;
   host_ptr[22] = 2;
   host_ptr[24] = 2;
-  host_ptr[26] = 2;
+  host_ptr[26] = 10;
 
   // Tests with padding = 0 stride = 1
-  testSamplingCalls(input, filter, 0, 0, 1, 1, 2, unitTestResults);
+  testSamplingCalls(input, filter, 0, 0, 1, 1, 2, "3_3", unitTestResults);
 
-  testSamplingCalls(input, filter, 0, 0, 1, 1, 3, unitTestResults);
+  testSamplingCalls(input, filter, 0, 0, 1, 1, 3, "3_3", unitTestResults);
 
-  testSamplingCalls(input, filter, 0, 0, 1, 1, 4, unitTestResults);
+  testSamplingCalls(input, filter, 0, 0, 1, 1, 4, "3_3", unitTestResults);
 
   // Tests with padding = 1 stride = 1
-  testSamplingCalls(input, filter, 1, 1, 1, 1, 2, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 1, 1, 2, "3_3", unitTestResults);
 
-  testSamplingCalls(input, filter, 1, 1, 1, 1, 3, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 1, 1, 3, "3_3", unitTestResults);
 
-  testSamplingCalls(input, filter, 1, 1, 1, 1, 4, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 1, 1, 4, "3_3", unitTestResults);
 
   // Tests with padding = 1 stride = 2
-  testSamplingCalls(input, filter, 1, 1, 2, 2, 2, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 2, 2, 2, "3_3", unitTestResults);
 
-  testSamplingCalls(input, filter, 1, 1, 2, 2, 3, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 2, 2, 3, "3_3", unitTestResults);
 
-  testSamplingCalls(input, filter, 1, 1, 2, 2, 4, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 2, 2, 4, "3_3", unitTestResults);
 }
 
 /**** Tests Sample for a sample 1 * 1 Filter */
@@ -919,18 +978,18 @@ void testSampling_1_1(UnitTestResults &unitTestResults) {
   fillTensorWithVal(filter, 2);
 
   // Tests with padding = 0 stride = 1
-  testSamplingCalls(input, filter, 0, 0, 1, 1, 2, unitTestResults);
+  testSamplingCalls(input, filter, 0, 0, 1, 1, 2, "1_1", unitTestResults);
 
-  testSamplingCalls(input, filter, 0, 0, 1, 1, 3, unitTestResults);
+  testSamplingCalls(input, filter, 0, 0, 1, 1, 3, "1_1", unitTestResults);
 
-  testSamplingCalls(input, filter, 0, 0, 1, 1, 4, unitTestResults);
+  testSamplingCalls(input, filter, 0, 0, 1, 1, 4, "1_1", unitTestResults);
 
   // Tests with padding = 1 stride = 1
-  testSamplingCalls(input, filter, 1, 1, 1, 1, 2, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 1, 1, 2, "1_1", unitTestResults);
 
-  testSamplingCalls(input, filter, 1, 1, 1, 1, 3, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 1, 1, 3, "1_1", unitTestResults);
 
-  testSamplingCalls(input, filter, 1, 1, 1, 1, 4, unitTestResults);
+  testSamplingCalls(input, filter, 1, 1, 1, 1, 4, "1_1", unitTestResults);
 }
 
 void *testTensorArgMax() {
-- 
GitLab