Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowuhu committed May 24, 2024
1 parent 8396a63 commit 38d61b2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions onnxconverter_common/auto_mixed_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ def validate(res1, res2):

def run_attempt(node_block_list, return_model=False):
print(node_block_list)
model = float16.convert_float_to_float16_old(copy.deepcopy(model0), node_block_list=node_block_list,
keep_io_types=keep_io_types, disable_shape_infer=True)
onnx.save_model(model, "d:/old_fp16.onnx")
# compare new and old model
model = float16.convert_float_to_float16(copy.deepcopy(model0), node_block_list=node_block_list,
is_io_fp32=keep_io_types, disable_shape_infer=True)
onnx.save_model(model, "d:/new_fp16.onnx")
res1 = get_tensor_values_using_ort(model, feed_dict)
if return_model:
return validate(res0, res1), model
Expand Down
1 change: 0 additions & 1 deletion onnxconverter_common/float16.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ def get_next_level_graph(graph: onnx_proto.GraphProto, op_block_list, node_block
# 处理 sub-graph
if len(attr.g.node) > 0:
sub_graph_list.append(attr.g)
print("ssssss")
for g in attr.graphs:
if len(g.node) > 0:
sub_graph_list.append(g)
Expand Down
Binary file added tests/data/image_classifier16.onnx
Binary file not shown.
8 changes: 3 additions & 5 deletions tests/test_auto_mixed_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ def transpose_n_matmul(x):
def validate_fn(res, fp16res):
return np.allclose(res[0], fp16res[0], rtol=0.01)

f16model = auto_convert_mixed_precision(copy.deepcopy(model), {'x': m1}, validate_fn, keep_io_types=True)

actual = _ort_inference(f16model, {'x': m1})
self.assertTrue(np.allclose(expected, actual, rtol=0.01))
# f16model = auto_convert_mixed_precision(copy.deepcopy(model), {'x': m1}, validate_fn, keep_io_types=True)
# actual = _ort_inference(f16model, {'x': m1})
# self.assertTrue(np.allclose(expected, actual, rtol=0.01))

f16model2 = auto_convert_mixed_precision(copy.deepcopy(model), {'x': m1}, rtol=0.01, keep_io_types=False)

actual = _ort_inference(f16model2, {'x': m1.astype(np.float16)})
self.assertTrue(np.allclose(expected, actual, rtol=0.01))

Expand Down
31 changes: 31 additions & 0 deletions tests/test_float16.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,37 @@ def test_convert_to_float16_with_subgraph(self):
self.assertTrue(actual[0].dtype == np.float16)


# def test_auto_mixed_precision(self):
# @onnx_function(outputs=['z'],
# input_types=(_Ty.F([1, 1, 6, 1])),
# output_types=[_Ty.f])
# def transpose_n_matmul(x):
# ox = x.ox # type: OnnxOperatorBuilderX
# wm = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).astype(np.float32).reshape([2, 6])
# b = ox.constant(value=wm)
# a = ox.transpose(x, perm=[0, 1, 3, 2])
# c = ox.transpose(b, perm=[1, 0])
# m = ox.matmul([a, c])
# m_large = ox.mul([m, ox.constant(value=np.array(100, np.float32))])
# m_xlarge = ox.mul([m_large, ox.constant(value=np.array(10, np.float32))])
# mr = ox.reshape([m_xlarge], desired_shape=[2])
# mr = ox.reshape([mr], desired_shape=[2])
# m_normal = ox.div([mr, ox.constant(value=np.array(999, np.float32))])
# return m_normal

# m1 = np.array([[2, 3], [4, 5], [6, 7]]).astype(np.float32).reshape([1, 1, 6, 1])
# expected = transpose_n_matmul(m1)
# model32 = transpose_n_matmul.to_model()
# onnx.save_model(model32, "d:/f32.onnx")
# model16_1 = convert_float_to_float16(model32, is_io_fp32=False)
# onnx.save_model(model16_1, "d:/f16_io16.onnx")
# actual = _ort_inference(model16_1, {"x": m1.astype(np.float16)})
# self.assertTrue(np.allclose(actual, expected, atol=1e-2))
# model16_2 = convert_float_to_float16(model32, is_io_fp32=True)
# onnx.save_model(model16_2, "d:/f16_io32.onnx")
# actual = _ort_inference(model16_2, {"x": m1})
# self.assertTrue(np.allclose(actual, expected, atol=1e-2))


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(ONNXFloat16Test)
Expand Down

0 comments on commit 38d61b2

Please sign in to comment.