-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathprune_onnx_model.py
More file actions
140 lines (127 loc) · 4.4 KB
/
prune_onnx_model.py
File metadata and controls
140 lines (127 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import math
import os
import onnx
from onnx import TensorProto, helper
logger = logging.getLogger("p2o-logger")
def prune_onnx_model(
onnx_model_file,
target_node_name="p2o.print",
target_dims=None,
target_dtype="float32",
):
if target_dims is None:
target_dims = [1]
dtype_map = {
"bool": (TensorProto.BOOL, bool),
"float32": (TensorProto.FLOAT, float),
"float64": (TensorProto.DOUBLE, float),
"int32": (TensorProto.INT32, int),
"int64": (TensorProto.INT64, int),
}
model = onnx.load(onnx_model_file)
onnx.checker.check_model(model)
loop_body = None
loop_node = None
target_node_output = None
for n in model.graph.node:
if n.op_type == "Loop":
loop_node = n
loop_body = n.attribute[0].g
for sub_n in loop_body.node:
if sub_n.name.startswith(target_node_name):
target_node_output = sub_n.output[0]
break
if target_node_output is None:
raise ValueError(f"Cannot find target node '{target_node_name}' in Loop.")
first_iter_initial_name = "first_iter_initial"
first_iter_initial = helper.make_tensor(
name=first_iter_initial_name,
data_type=dtype_map[target_dtype][0],
dims=target_dims if len(target_dims) > 0 else (),
vals=(
[dtype_map[target_dtype][1](0) for _ in range(math.prod(target_dims))]
if len(target_dims) > 0
else [dtype_map[target_dtype][1](0)]
),
)
model.graph.initializer.append(first_iter_initial)
loop_node.input.append(first_iter_initial_name)
first_iter_output = helper.make_tensor_value_info(
"first_iter_output", dtype_map[target_dtype][0], target_dims
)
loop_body.input.append(first_iter_output)
zero_const_tensor = helper.make_tensor(
name="zero_const",
data_type=TensorProto.INT64,
dims=(),
vals=[0],
)
model.graph.initializer.append(zero_const_tensor)
first_iter_cond = helper.make_node(
"Equal",
inputs=[loop_body.input[0].name, zero_const_tensor.name],
outputs=["first_iter_cond"],
)
if_node = helper.make_node(
"If",
inputs=["first_iter_cond"],
outputs=["first_iter_output_next"],
then_branch=helper.make_graph(
[
helper.make_node(
"Identity", [target_node_output], ["first_iter_output_next"]
)
],
"then_branch",
[],
[
helper.make_tensor_value_info(
"first_iter_output_next", dtype_map[target_dtype][0], target_dims
)
],
),
else_branch=helper.make_graph(
[
helper.make_node(
"Identity", [first_iter_output.name], ["first_iter_output_next"]
)
],
"else_branch",
[],
[
helper.make_tensor_value_info(
"first_iter_output_next", dtype_map[target_dtype][0], target_dims
)
],
),
)
loop_body.node.extend([first_iter_cond, if_node])
loop_body.output.append(
helper.make_tensor_value_info(
"first_iter_output_next", dtype_map[target_dtype][0], target_dims
)
)
loop_node.output.append("first_iter_output_next")
model.graph.output.append(
helper.make_tensor_value_info(
"first_iter_output_next", dtype_map[target_dtype][0], target_dims
)
)
output_model_file = os.path.basename(onnx_model_file) + "_modified.onnx"
onnx.save(model, output_model_file)
logger.info("Modified onnx model saved to %s", output_model_file)
return output_model_file