Skip to content
Snippets Groups Projects
Commit 8c39d024 authored by shingjan's avatar shingjan
Browse files

refactoring

parent f28c30e3
No related branches found
No related tags found
No related merge requests found
......@@ -8,17 +8,29 @@ class Tensor(object):
if not proto.name.strip():
raise ValueError("Tensor's name is required.")
self.name = proto.name
self.mapped_name = None
def set_mapped_name(self, mapped_name):
self.mapped_name = mapped_name
def get_mapped_name(self):
if self.mapped_name != None:
return self.mapped_name
else:
raise ValueError("Var name not mapped before use!")
def __str__(self):
return "Tensor: {}\n".format(self.name)
__repr__ = __str__
class InputTensor(Tensor):
def __init__(self, input_name):
self.name = input_name
# Can be either input or weight tensor
class WeightTensor(Tensor):
def __init__(self, input_proto):
Tensor.__init__(self, input_proto)
self.input_data = numpy_helper.to_array(input_proto)#.reshape(tuple(input_proto.dims))
def __init__(self, weight_proto):
Tensor.__init__(self, weight_proto)
self.input_data = numpy_helper.to_array(weight_proto)#.reshape(tuple(input_proto.dims))
class InputTensor(Tensor):
def __init__(self, inter_name):
self.name = inter_name
......@@ -13,7 +13,10 @@ support_onnx_ops = {"DepthwiseConv2D" : None,
"Add" : None,
"Relu" : None,
"Softmax" : None,
"Identity": None}
"Identity": None,
"Pad": None,
"AveragePool": None,
"Tanh": None}
class GraphBuilder(object):
def __init__(self, model, shape, dtype, opset, weight_dir):
......@@ -96,7 +99,7 @@ class GraphBuilder(object):
return False
def _dump_weight(self, weight_tensor):
print("weights dump here")
print("Dump weight: {0}".format(weight_tensor.name))
################################################
......@@ -105,11 +108,14 @@ class GraphBuilder(object):
################################################
def build_graph(self):
# parse weight
for weight_tensor in self.graph.initializer:
self.tensors[weight_tensor.name] = WeightTensor(weight_tensor)
# parse input
for i in self.graph.input:
if i.name not in self.tensors:
self.tensors[i.name] = InputTensor(i.name)
# parse intermediate tensor
for node in self.graph.node:
op_name = node.op_type
#print("###############################")
......@@ -127,9 +133,10 @@ class GraphBuilder(object):
for i in node.output:
if i not in self.tensors:
self.tensors[i] = InputTensor(i)
# Dump weights
for tensor in self.tensors.values():
if isinstance(tensor, WeightTensor):
print(tensor.name)
self._dump_weight(tensor)
return DFG(self.graph, self.tensors)
class DFG(object):
......
......@@ -8,16 +8,31 @@ from common import *
class GraphCodeGen(object):
def __init__(self, DFG):
def __init__(self, DFG, weights_dir, test_data, test_labels):
self.program_str = ""
self.graph = DFG.graph
self.tensors = DFG.tensors
self.var_cnt = 0
self.weights_dir = weights_dir
self.test_data = test_data
self.test_labels =test_labels
################################################
# Aux functions
################################################
def get_var_cnt(self):
cnt = self.var_cnt
self.var_cnt = self.var_cnt + 1
return cnt
################################################
# Emit functions for code generation
################################################
def emit_weights(self):
weight_str += "std::string dir_prefix = std::string(\"" + str(self.weights_dir) + "\");"
weight_str += "std::string input_path = dir_prefix + std::string(\"input.bin\");"
weight_str += "std::string labels_path = dir_prefix + std::string(\"labels.bin\");"
for tensor in self.tensors.values():
if isinstance(tensor, WeightTensor):
print(tensor.name)
......@@ -115,15 +130,15 @@ class GraphCodeGen(object):
# program with HPVM intrinsics
################################################
def codegen(self, weights_dir, test_data, test_labels):
if os.path.exists(weights_dir):
def compile(self, src_dir):
if os.path.exists(self.weights_dir):
raise ValueError("Weight dir existed. Compilation interrupted!")
os.mkdir(weights_dir)
self.emit_header()
self.emit_weights(weights_dir)
self.emit_batch_loop(test_data)
self.emit_weights()
self.emit_batch_loop()
self.emit_graph()
self.emit_batch_loop_end()
self.emit_footer(test_data)
self.emit_footer()
# Write the program to source/disk
self.emit_source(weights_dir)
self.emit_source(src_dir)
import numpy as np
import struct
import random
def dumpLabels(file_name, Y_test):
f = open(file_name, "wb")
labels_map = {}
for label in Y_test:
label_val = 0
if len(Y_test.shape) > 1:
#label_val = np.int8(label[0])
label_val = np.int32(label[0])
else:
#label_val = np.int8(label)
label_val = np.int32(label)
if label_val not in labels_map:
labels_map[label_val] = 0
labels_map[label_val] += 1
f.write(label_val)
f.close()
"""def dumpData(file_name, X_test):
N = X_test.shape[0]
C = X_test.shape[1]
H = X_test.shape[2]
W = X_test.shape[3]
print ("*DumpData")
print("-min_val = ", np.amin(X_test))
print("-max_val = ", np.amax(X_test))
f = open(file_name, "wb")
for i in range(N):
for j in range(C):
for k in range(H):
for l in range(W):
val = struct.unpack("f", struct.pack("f", X_test[i][j][k][l]))
f.write(np.float32(val[0]))
f.close()
"""
def dumpData(file_name, X_test):
N = X_test.shape[0]
C = X_test.shape[1]
H = X_test.shape[2]
W = X_test.shape[3]
print ("*DumpData")
print("-min_val = ", np.amin(X_test))
print("-max_val = ", np.amax(X_test))
f = open(file_name, "wb")
X_test.tofile(f)
f.close()
def dumpConvWeights(file_name, weights, N, C, H, W):
print (weights.shape)
print ("*DumpConvWeights")
print("-min_val = ", np.amin(weights))
print("-max_val = ", np.amax(weights))
f = open(file_name, "wb")
for i in range(N):
for j in range(C):
for k in range(H):
for l in range(W):
f.write(weights[k][l][j][i])
f.close()
def dumpFcWeights(file_name, weights, H, W):
print (weights.shape)
print ("*DumpFcWeights")
print("-min_val = ", np.amin(weights))
print("-max_val = ", np.amax(weights))
f = open(file_name, "wb")
for i in range(H):
for j in range(W):
f.write(weights[i][j])
f.close()
def dumpFcBias(file_name, bias, W):
print (bias.shape)
print ("*DumpFcBias")
print("-min_val = ", np.amin(bias))
print("-max_val = ", np.amax(bias))
f = open(file_name, "wb")
for i in range(W):
f.write(bias[i])
f.close()
def dumpCalibrationData(file_name, X_train, labels_fname, train_labels):
combined_list = []
for i in range(len(X_train)):
tup = (X_train[i], train_labels[i])
combined_list.append(tup)
np.random.shuffle(combined_list)
#X_calibration = X_train[0:5000]
data_list = []
labels_list = []
for i in range(5000):
tup = combined_list[i]
data_list.append(tup[0])
labels_list.append(tup[1])
data_list = np.array(data_list)
labels_list = np.array(labels_list)
dumpData(file_name, data_list)
dumpLabels(labels_fname, labels_list)
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