From ce49821249bb3d9cb7843ecd4d805cb0914a3eeb Mon Sep 17 00:00:00 2001
From: Hashim Sharif <hsharif3@tyler.cs.illinois.edu>
Date: Sat, 18 Jul 2020 00:18:24 -0500
Subject: [PATCH] Fixing Batchnorm unit tests

---
 .../dnn_sources/src/unit_tests.cc             | 93 ++++++++++++++-----
 1 file changed, 71 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 dd0abdb9c6..ae9fdc1731 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
@@ -326,28 +326,46 @@ void testTensorHalfConv(){
 
 
 
-void testTensorGroupedConv(){
+void testTensorGroupConv(){
 
   // NOTE: The input channel count value (param2 to Tensor and Filter) must be the same
-  void* x3 = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 2, 4, 4);
-  // NOTE: Filter descriptors do NOT have batch size
-  // NOTE: First two dims are output channels (configurable), input channels (MUST match input channels)
+  void* input = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 2, 4, 4);
+  void* filter = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 2, 1, 3, 3);
+
+  // FIXIT: fillTensor* calls should be replaced with initTensorValue(tenosor, val)
+  fillTensorWithOnes(input);
+  fillTensorWithOnes(filter);
+
+  int conv_mode = 1; // NOTE: uses CROSS_CORRELATION
+  int conv_groups = 2;
+  
+  void* conv_out = tensorConvolution(input, filter,
+	                             0, 0,
+				     1, 1,
+				     conv_mode, conv_groups);
+  printTensorValues(conv_out);
+  
+}
+
+
+void testTensorHalfGroupConv(){
+
+  // NOTE: The input channel count value (param2 to Tensor and Filter) must be the same
+  void* input = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 2, 4, 4);
   void* filter = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 2, 1, 3, 3);
-  fillTensorWithOnes(x3);
+
+  fillTensorWithOnes(input);
   fillTensorWithOnes(filter);
 
   int conv_mode = 1; // NOTE: uses CROSS_CORRELATION
   int conv_groups = 2;
   
-  void* conv1 = tensorConvolution(x3, filter, 2, 2,
-				  2, 2, conv_mode, conv_groups);
-  printTensorValues(conv1);
+  void* conv_out = tensorConvolution(input, filter,
+	                             0, 0,
+				     1, 1,
+				     conv_mode, conv_groups);
 
-  // NOTE: For cudnnTensorAdd, the only dimension that MUST match is channels  
-  //void* bias3 = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 1, 1);
-  // fillTensorWithOnes(bias3);
-  //tensorAdd(conv1, bias3);
-  //printTensorValues(conv1);
+  printTensorValues(conv_out);
 }
 
 
@@ -376,8 +394,35 @@ void testTensorBatchNorm(){
   void* variance = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 1, 1);
   fillTensorWithVal(variance, 1);
 
-  
-  void* output = tensorBatchNorm(x, gamma, beta, mean, variance, 0.01);  
+  double epsilon = 1;
+  // NOTE: result = X - mean / sqrt(epsilon + variance)
+  void* output = tensorBatchNorm(x, gamma, beta, mean, variance, 1);  
+  printTensorValues(output);  
+}
+
+
+void testTensorHalfBatchNorm(){
+
+  void* x = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 2, 2);
+  fillTensorWithVal(x, 3);
+
+  void* gamma = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 1, 1);
+  fillTensorWithVal(gamma, 1);
+
+  void* beta = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 1, 1);
+  fillTensorWithVal(beta, 0);
+
+  void* mean = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 1, 1);
+  fillTensorWithVal(mean, 1);
+
+  void* variance = create4DTensor(CUDNN_DATA_FLOAT, CUDNN_TENSOR_NCHW, 1, 3, 1, 1);
+  fillTensorWithVal(variance, 1);
+
+
+  double epsilon = 1;
+  // NOTE: result = X - mean / sqrt(epsilon + variance)
+  void* output = tensorBatchNorm(x, gamma, beta, mean, variance, 1);  
+
   printTensorValues(output);  
 }
 
@@ -1200,16 +1245,21 @@ int main(){
   
   testTensorConv();
   testTensorHalfConv();
-  
-  //testTensorError();
 
+  testTensorGroupConv();
+  testTensorHalfGroupConv();
+
+  testTensorBatchNorm();
+  testTensorHalfBatchNorm();
+
+  ///testTensorPooling();
+  ///testTensorHalfPooling();
+
+
+  //testTensorError();
   //testQuantization();
 
-  //testTensorConv();
-  //testTensorGroupedConv();
 
-  //testTensorBatchNorm();
-  
   //testTensorGemm();
   //testTensorGemmGPU();
   //testTensorGemmBias();  
@@ -1228,7 +1278,6 @@ int main(){
 
   
 
-
   /********* SAMPLING TESTS ****
 
   testSampling_3_3();
-- 
GitLab