Skip to content

Commit c095f1d

Browse files
committed
feat:add merge_with_model optimize_on_merged_model
1 parent 86cb9be commit c095f1d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Diff for: onnxoptimizer/optimize_c_api/optimize_c_api.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ void merge_double_models_with_predicate(std::string& onnx_model_path,std::string
3131
onnx::optimization::merge_double_models_with_predicate(onnx_model_path,predicate,prefix_l,prefix_r);
3232
}
3333

34+
void merge_with_model_path(std::string& mp_in_path1,std::string& mp_in_path2,std::string& mp_name1,std::string& mp_name2,
35+
std::string& mp_out_path){
36+
onnx::optimization::MergeWithModels(mp_in_path1,mp_in_path2,mp_name1,mp_name2,mp_out_path);
37+
}
38+
39+
void optimize_on_merged_model(std::string& mp_in_path,std::string& mp_out_path){
40+
onnx::optimization::OptimizeOnMergedModel(mp_in_path, mp_out_path);
41+
}
42+

Diff for: onnxoptimizer/optimize_c_api/optimize_c_api.h

+5
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ void merge_single_model_with_predicate(std::string& onnx_model_path, std::string
1616

1717
void merge_double_models_with_predicate(std::string& onnx_model_path,std::string& predicate,
1818
std::string prefix_l,std::string prefix_r);
19+
20+
void merge_with_model_path(std::string& mp_in_path1,std::string& mp_in_path2,std::string& mp_name1,std::string& mp_name2,
21+
std::string& mp_out_path);
22+
23+
void optimize_on_merged_model(std::string& mp_in_path,std::string& mp_out_path);
1924
#endif // ONNX_OPTIMIZER_OPTIMIZE_C_API_H
2025

Diff for: onnxoptimizer/query_c_api/model_merge.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ ModelProto model_merge(
322322
return model;
323323
}
324324

325+
ModelProto opt_model(
326+
ModelProto* m
327+
){
328+
auto new_model = OptimizeFixed(*m, GetFuseAndEliminationPass());
329+
return new_model;
330+
}
331+
325332
void OptimizeWithModels(
326333
std::string& mp_in_path1,
327334
std::string& mp_in_path2,
@@ -341,4 +348,28 @@ void OptimizeWithModels(
341348
// onnx::optimization::saveModel(&model,"../examples/onnx_output_model/model_merged.onnx");
342349
}
343350

351+
void MergeWithModels(
352+
std::string& mp_in_path1,
353+
std::string& mp_in_path2,
354+
std::string& mp_name1,
355+
std::string& mp_name2,
356+
std::string& mp_out_path) {
357+
ONNX_NAMESPACE::ModelProto model1,model2;
358+
onnx::optimization::loadModel(&model1, mp_in_path1, true);
359+
onnx::optimization::loadModel(&model2, mp_in_path2, true);
360+
ModelProto model=model_merge(&model1,&model2,mp_name1,mp_name2);
361+
onnx::checker::check_model(model);
362+
onnx::optimization::saveModel(&model,mp_out_path);
363+
// onnx::optimization::saveModel(&model,"../examples/onnx_output_model/model_merged.onnx");
364+
}
365+
366+
void OptimizeOnMergedModel(
367+
std::string& mp_in_path,
368+
std::string& mp_out_path) {
369+
ONNX_NAMESPACE::ModelProto model;
370+
onnx::optimization::loadModel(&model, mp_in_path, true);
371+
ModelProto model_opted=opt_model(&model);
372+
onnx::checker::check_model(model_opted);
373+
onnx::optimization::saveModel(&model_opted,mp_out_path);
374+
}
344375
}//end namespace

0 commit comments

Comments
 (0)