From 1fe23dca884f5f09615d89394027236bd80e06b1 Mon Sep 17 00:00:00 2001
From: Hashim Sharif <hsharif3@tyler.cs.illinois.edu>
Date: Sat, 18 Jul 2020 22:54:50 -0500
Subject: [PATCH] Adding routine to compare output tensors with expected
 results

---
 .../dnn_sources/src/unit_tests.cc             | 106 ++++++++++++++----
 1 file changed, 84 insertions(+), 22 deletions(-)

diff --git a/llvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc b/llvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc
index 7bfec2945f..538cea6511 100644
--- a/llvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc
+++ b/llvm/projects/hpvm-tensor-rt/dnn_sources/src/unit_tests.cc
@@ -2,10 +2,78 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <vector>
+#include <string.h>
 #include "tensor_runtime.h"
 #include "utils.h"
 #include "tensor_custom_ops_cpu.h"
 
+using namespace std;
+
+
+
+
+class UnitTestResults{
+
+private:
+  unsigned int total_tests;
+  unsigned int failed_tests;
+  unsigned int passed_tests;
+  std::vector<string> failed_test_ids;
+
+public:
+
+  UnitTestResults(){
+    total_tests = 0;
+    failed_tests = 0;
+    passed_tests = 0;
+  }
+
+  void evalTestResult(Tensor* res, const float* expected_result, size_t num_elems,
+		      float epsilon, string test_name){
+
+    total_tests += 1;      
+    if(res->num_elems != num_elems){
+      failed_tests += 1;
+      failed_test_ids.push_back(test_name);
+      return;
+    }
+
+    float* data_ptr = (float*) res->host_data;
+    for (unsigned int i = 0; i < res->num_elems; i++){
+      if (std::abs(data_ptr[i] - expected_result[i]) > epsilon){
+	failed_tests += 1;
+	failed_test_ids.push_back(test_name);
+        return;
+      }
+    }
+    
+    passed_tests += 1;    
+  }
+
+  void compareTensors(Tensor* res, Tensor* gold_res,
+		      float epsilon, string test_name){
+
+    const float* expected_result = (float*) gold_res->host_data;
+    unsigned int num_elems = res->num_elems;
+
+    evalTestResult(res, expected_result, num_elems, epsilon, test_name);
+    
+  }
+
+
+  void printSummary(){
+
+    printf("\n\n\n ************* Printing Results Summary ********** \n\n");
+    printf("-- Total tests :=  %d \n", total_tests);
+    printf("-- Tests Passed := %d \n", passed_tests);
+    printf("-- Tests Failed := %d \n", failed_tests);
+   
+  }
+  
+};
+
+
 
 void testTensorGemm(){
 
@@ -39,7 +107,7 @@ void testTensorGemm(){
 }
 
 
-void testTensorHgemm(){
+void testTensorHgemm(UnitTestResults& unitTestResults){
 
   printf("***** TensorHgemm ***** \n\n");
   void* lhs_ptr = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 5, 4, 1, 1);
@@ -57,7 +125,9 @@ void testTensorHgemm(){
   void* output = tensorHalfGemm(lhs, rhs);   
   printTensorValues(output);
 
-  // TODO: Add result comparator - Make a generic result comparator
+  const float expected_result[15] = {4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20, 20};
+
+  unitTestResults.evalTestResult((Tensor*) output, expected_result, 15, 0.01, "Hgemm");
 }
 
 
@@ -1200,24 +1270,6 @@ void testNewTensorOps(){
 
 
 
-class UnitTestResults{
-
-private:
-  unsigned int total_tests;
-  unsigned int failed_tests;
-  unsigned int passed_tests;
-
-public:
-
-  UnitTestResults(){
-    total_tests = 0;
-    failed_tests = 0;
-    passed_tests = 0;
-  }
-
-};
-
-
 
 
 
@@ -1225,10 +1277,14 @@ int main(){
 
   llvm_hpvm_initTensorRt(0);
 
+
+  UnitTestResults unitTestResults;
+  
   // Function call per unit test
-  testTensorHgemm(); 
+  testTensorHgemm(unitTestResults);
   testTensorSgemm();
-  
+
+  /*
   testTensorConv();
   testTensorHalfConv();
 
@@ -1246,6 +1302,12 @@ int main(){
 
   testPerforation();
 
+  */
+
+
+  unitTestResults.printSummary();
+  
+
   // testTensorError();
   // testQuantization(); 
   // testTensorGemm();
-- 
GitLab