Skip to content
Snippets Groups Projects
Commit c47b6408 authored by Hashim Sharif's avatar Hashim Sharif
Browse files

Adding working FP16 quantization and Tranpose Dense weights scripts (tested on simulator)

parent 48b62996
No related branches found
No related tags found
No related merge requests found
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)
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment