From c47b6408722050730f6d814db3346abfad3b690a Mon Sep 17 00:00:00 2001
From: Hashim Sharif <hsharif3@miranda.cs.illinois.edu>
Date: Mon, 5 Jul 2021 01:53:22 -0500
Subject: [PATCH] Adding working FP16 quantization and Tranpose Dense weights
 scripts (tested on simulator)

---
 .../lenet_mnist/scripts/fp16_quantize.py      | 30 ++++++++++++
 .../lenet_mnist/scripts/transpose_dense.py    | 47 +++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/fp16_quantize.py
 create mode 100644 hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/transpose_dense.py

diff --git a/hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/fp16_quantize.py b/hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/fp16_quantize.py
new file mode 100644
index 0000000000..ded4932c04
--- /dev/null
+++ b/hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/fp16_quantize.py
@@ -0,0 +1,30 @@
+
+import numpy as np
+import os
+
+
+if __name__ == "__main__":
+
+    list_dir = os.listdir("./")
+    print (list_dir)
+
+    for file_name in list_dir:
+        if "fp16" not in file_name and ".bin" in file_name:
+            print ("name = ", file_name)
+            weights_arr = np.fromfile(file_name, dtype ='float32')
+            print (weights_arr[:5], weights_arr.shape)
+            fp16_weights = weights_arr.astype(np.float16)
+            print (fp16_weights[:5], fp16_weights.shape)
+        
+            fp16_file = open(file_name.split(".")[0] + "_fp16.bin", "w+")
+            fp16_weights.tofile(fp16_file)
+            fp16_file.close()
+
+
+    for file_name in list_dir:
+        if "fp16" in file_name:
+            print ("fp16_name = ", file_name)
+            weights_arr = np.fromfile(file_name, dtype ='float16')
+            print (weights_arr[:5], weights_arr.shape)
+            
+
diff --git a/hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/transpose_dense.py b/hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/transpose_dense.py
new file mode 100644
index 0000000000..37e89152e9
--- /dev/null
+++ b/hpvm/test/dnn_benchmarks/hpvm-c/benchmarks/lenet_mnist/scripts/transpose_dense.py
@@ -0,0 +1,47 @@
+
+import numpy as np
+import os
+import sys
+
+
+if __name__ == "__main__":
+
+    f_path = sys.argv[1]
+    dim1 = int(sys.argv[2])
+    dim2 = int(sys.argv[3])
+
+    print (f_path, dim1, dim2)
+
+    x = np.fromfile(f_path, dtype='float16')
+
+    x_reshaped = np.reshape(x, (dim1, dim2))
+    x_transposed = np.transpose(x_reshaped, (1, 0))
+
+    print (x_reshaped, x_transposed)
+            
+    #print ("x_trans = ", x_transposed)
+            
+    x_flat = x_transposed.flatten()
+    x_fp16 = x_flat.astype(np.float16)
+
+    out_fpath = f_path.split(".bin")[0] + "_transpose.bin"
+    outfile = open(out_fpath, "w+")
+
+    x_fp16.tofile(outfile)
+
+    
+    outfile.close()
+
+
+
+    # Double transpose to check corectness
+    #x_double_transposed = np.transpose(x_transposed, (1, 0))
+    #print ("double_tranposed = ", x_double_transposed)
+    #x2_flat = x_double_transposed.flatten()
+    #x2_fp16 = x2_flat.astype(np.float16)
+    #outfile = open("double_transpose.bin", "w+")
+    #x2_fp16.tofile(outfile)
+
+    #outfile.close()
+
+            
-- 
GitLab