Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dynamic time shape #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion onnxsim/onnx_simplifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def get_np_type_from_elem_type(elem_type: int):


def get_inputs(model: onnx.ModelProto) -> List[onnx.ValueInfoProto]:
# weights
initializer_names = [x.name for x in model.graph.initializer]
# input not in weights
return [ipt for ipt in model.graph.input if ipt.name not in initializer_names]


Expand All @@ -127,9 +129,11 @@ def remove_unused_output(model: onnx.ModelProto, unused_output: Sequence[str]) -
if unused_output_name not in output_names:
raise RuntimeError(
f'The model doesn\'t have output named "{unused_output_name}"')

for graph_output in copy.deepcopy(model.graph.output):
if graph_output.name in unused_output_names:
model.graph.output.remove(graph_output)

model = onnxoptimizer.optimize(model, ['eliminate_deadend'],
fixed_point=True)
onnx.checker.check_model(model)
Expand All @@ -144,9 +148,20 @@ def generate_specific_rand_input(model, input_shapes: TensorShapes):
for key, shape in input_shapes.items():
shape_np = np.array(shape)
if not np.all(shape_np > 0):
# treat batch size as 1 automatically if dynamic_input_shape is True

if config.dynamic_input_shape and len(shape_np) >= 3 and np.all(shape_np[1:] > 0):
# treat batch size as 1 automatically if dynamic_input_shape is True
input_shapes[key] = [1] + shape[1:]
print(f"dynamic input shape: {key} {shape} -> {input_shapes[key]}")
continue

if config.dynamic_input_shape:
dyn_dim = np.where(shape_np==-1)[0]
shape_tmp = copy.deepcopy(shape)
for i, idx in enumerate(dyn_dim):
shape_tmp[idx.item()] = 10 + i
input_shapes[key] = shape_tmp
print(f"dynamic input shape: {key} {shape} -> {shape_tmp}")
continue

raise RuntimeError(
Expand Down Expand Up @@ -362,6 +377,7 @@ def optimize(model: onnx.ModelProto, skip_fuse_bn: bool, skipped_optimizers: Opt
onnx.checker.check_model(model)
onnx.helper.strip_doc_string(model)
optimizers_list = onnxoptimizer.get_fuse_and_elimination_passes()
print(f'opt passes: {optimizers_list}')
if skip_fuse_bn:
optimizers_list.remove('fuse_bn_into_conv')
if skipped_optimizers is not None:
Expand Down