Skip to content

Commit 1b4f614

Browse files
committed
- a more comprehensive test graph
- add op the first tensor still fails
1 parent 37d15f9 commit 1b4f614

File tree

3 files changed

+86
-28
lines changed

3 files changed

+86
-28
lines changed

tests/tflm/tflite_export/conftest.py

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,91 @@
99
def simple_tflm_graph():
1010
ugraph = uTensorGraph()
1111

12-
weight_op = OperationInfo(
13-
name = "weight_const",
12+
mock_input_op0 = OperationInfo(
13+
name = "mock_input_const0",
1414
op_type = "Const",
1515
backend = "tensorflow",
1616
ugraph = ugraph,
1717
op_attr = dict(),
1818
input_tensors = [],
1919
output_tensors = []
2020
)
21-
#weight_op.op_attr["value"] = np.array([1,2,3,4], dtype=np.int8)
22-
weight_op.op_attr["value"] = np.array([10,20,30,40], dtype=np.float32)
23-
weight_op.op_attr["shape"] = [1,4]
24-
25-
weight = TensorInfo(
26-
name = "weight",
27-
op_name = "weight_const",
28-
dtype = np.dtype("float32"),
29-
shape = weight_op.op_attr["shape"],
21+
mock_input_op0.op_attr["value"] = np.array([[2],[4],[6],[8]], dtype=np.float32)
22+
mock_input_op0.op_attr["shape"] = [4,1]
23+
24+
input0 = TensorInfo(
25+
name = "input0",
26+
op_name = "mock_input_const0",
27+
dtype = mock_input_op0.op_attr["value"].dtype,
28+
shape = mock_input_op0.op_attr["shape"],
3029
ugraph = ugraph
3130
)
32-
weight_op.output_tensors = [weight]
3331

34-
mock_input_op = OperationInfo(
35-
name = "mock_input_const",
32+
mock_input_op0.output_tensors = [input0]
33+
34+
mock_input1_op = OperationInfo(
35+
name = "mock_input_const1",
3636
op_type = "Const",
3737
backend = "tensorflow",
3838
ugraph = ugraph,
3939
op_attr = dict(),
4040
input_tensors = [],
4141
output_tensors = []
4242
)
43-
mock_input_op.op_attr["value"] = np.array([[2],[4],[6],[8]], dtype=np.float32)
44-
mock_input_op.op_attr["shape"] = [4,1]
43+
mock_input1_op.op_attr["value"] = np.array([[2],[4],[6],[8]], dtype=np.float32)
44+
mock_input1_op.op_attr["shape"] = [4,1]
4545

4646
input1 = TensorInfo(
4747
name = "input1",
48-
op_name = "mock_input_const",
49-
dtype = mock_input_op.op_attr["value"].dtype,
50-
shape = mock_input_op.op_attr["shape"],
48+
op_name = "mock_input_const1",
49+
dtype = mock_input1_op.op_attr["value"].dtype,
50+
shape = mock_input1_op.op_attr["shape"],
51+
ugraph = ugraph
52+
)
53+
54+
mock_input1_op.output_tensors = [input1]
55+
56+
add_output = TensorInfo(
57+
name = "add_out",
58+
op_name = "add0",
59+
dtype = mock_input_op0.op_attr["value"].dtype,
60+
shape = mock_input_op0.op_attr["shape"],
5161
ugraph = ugraph
5262
)
5363

54-
mock_input_op.output_tensors = [input1]
64+
add_op = OperationInfo(
65+
name = "add0",
66+
op_type = "ADD",
67+
backend = "tensorflow",
68+
ugraph = ugraph,
69+
op_attr = dict(),
70+
input_tensors = [input0, input1],
71+
output_tensors = [add_output]
72+
)
73+
74+
ugraph.ops_info["ADD0"] = add_op
75+
76+
weight_op = OperationInfo(
77+
name = "weight_const",
78+
op_type = "Const",
79+
backend = "tensorflow",
80+
ugraph = ugraph,
81+
op_attr = dict(),
82+
input_tensors = [],
83+
output_tensors = []
84+
)
85+
#weight_op.op_attr["value"] = np.array([1,2,3,4], dtype=np.int8)
86+
weight_op.op_attr["value"] = np.array([10,20,30,40], dtype=np.float32)
87+
weight_op.op_attr["shape"] = [1,4]
88+
89+
weight = TensorInfo(
90+
name = "weight",
91+
op_name = "weight_const",
92+
dtype = np.dtype("float32"),
93+
shape = weight_op.op_attr["shape"],
94+
ugraph = ugraph
95+
)
96+
weight_op.output_tensors = [weight]
5597

5698
bias_op = OperationInfo(
5799
name = "bias_const",
@@ -94,7 +136,7 @@ def simple_tflm_graph():
94136
ugraph = ugraph
95137
)
96138

97-
fc1_op.input_tensors = [input1, weight, bias]
139+
fc1_op.input_tensors = [add_output, weight, bias]
98140
fc1_op.output_tensors = [output]
99141

100142
ugraph.ops_info["FC1"] = fc1_op
@@ -105,4 +147,4 @@ def simple_tflm_graph():
105147
#ugraph = prune_graph(ugraph)
106148

107149
#return: ugraph, input tensors, output tensors
108-
return [ugraph, [], ["input1", "weight", "bias", "output"]]
150+
return [ugraph, ["input0", "input1"], ["weight", "bias", "output"]]

tests/tflm/tflite_export/test_write.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_tflite_fb_write(hybrid_quant_output):
8686
ugraph = exporter.transform(sample_ugraph)
8787
model_content = exporter.output()
8888

89-
#print_tflite_graph(model_content)
89+
print_tflite_graph(model_content)
9090

9191
# referece_model_content = open('/Users/neitan01/Documents/tflm/sinExample/sine_model.tflite', "rb").read()
9292
# print_tflite_graph(referece_model_content)
@@ -96,11 +96,17 @@ def test_tflite_fb_write(hybrid_quant_output):
9696
test_model.allocate_tensors()
9797
test_model.invoke()
9898

99+
print("0 :", test_model.get_tensor(0))
100+
print("1 :", test_model.get_tensor(1))
101+
print("2 :", test_model.get_tensor(2))
102+
print("3 :", test_model.get_tensor(3))
103+
104+
99105
print(test_model.get_tensor_details())
100-
print("1 :", test_model.get_tensor(test_model.get_output_details()[0]["index"]))
101-
print("2 :", test_model.get_tensor(test_model.get_output_details()[1]["index"]))
102-
print("3 :", test_model.get_tensor(test_model.get_output_details()[2]["index"]))
103-
print("out :", test_model.get_tensor(test_model.get_output_details()[3]["index"]))
106+
print("out0 :", test_model.get_tensor(test_model.get_output_details()[0]["index"]))
107+
print("out1 :", test_model.get_tensor(test_model.get_output_details()[1]["index"]))
108+
print("out2 :", test_model.get_tensor(test_model.get_output_details()[2]["index"]))
109+
104110

105111
test_pass = True
106112
assert test_pass, 'error message here'

utensor_cgen/transformer/tflite_exporter.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def get_fullyconnected_builtin_option(fbuilder, op_info):
3838

3939
return obj, BuiltinOptions.FullyConnectedOptions
4040

41+
def get_add_builtin_option(fbuilder, op_info):
42+
43+
tflite.AddOptions.AddOptionsStart(fbuilder)
44+
#FIXME: node fusion and select an activation function here
45+
tflite.AddOptions.AddOptionsAddFusedActivationFunction(fbuilder, ActivationFunctionType.NONE)
46+
obj = tflite.AddOptions.AddOptionsEnd(fbuilder)
47+
48+
return obj, BuiltinOptions.AddOptions
49+
4150
def tensor_type_lookup(numpy_dtype):
4251
TensorType = tflite.TensorType.TensorType
4352
lookup_map = dict()
@@ -249,6 +258,7 @@ def add_Op(self, op_info):
249258
##TODO: add op factory to deal with op options
250259
op_option_factory = dict()
251260
op_option_factory["FULLY_CONNECTED"] = get_fullyconnected_builtin_option
261+
op_option_factory["ADD"] = get_add_builtin_option
252262

253263
builtin_opt_func = op_option_factory[op_info.op_type]
254264

@@ -314,11 +324,11 @@ def __create_variable_tensors(self, ugraph):
314324

315325
tflite.Tensor.TensorStart(self.fbuilder)
316326
tflite.Tensor.TensorAddShape(self.fbuilder, shape_vec)
317-
#tflite.Tensor.TensorAddType(self.fbuilder, TensorType.INT8) #TODO: tensor type conversion here
318327
tflite.Tensor.TensorAddType(self.fbuilder, tensor_type_lookup(tensor_info.dtype))
319328
tflite.Tensor.TensorAddName(self.fbuilder, tensor_name)
320329
#tflite.Tensor.TensorAddQuantization(self.fbuilder, q_param)
321330
tflite.Tensor.TensorAddIsVariable(self.fbuilder, True)
331+
#tflite.Tensor.TensorAddIsVariable(self.fbuilder, False) #FIXME: TOCO outputs False here, no idea why
322332

323333
self.tensor_index[tensor_info.name] = tflite.Tensor.TensorEnd(self.fbuilder)
324334

0 commit comments

Comments
 (0)