-
Notifications
You must be signed in to change notification settings - Fork 178
[tools/onnx-subgraph] add onnx proto wrapper for graph parsing #14992
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,11 +11,20 @@ find_package(Protobuf REQUIRED) | |||||||
| find_package(jsoncpp REQUIRED) | ||||||||
| find_package(Python3 COMPONENTS Interpreter Development REQUIRED) | ||||||||
|
|
||||||||
| set(PROTO_FILES onnx.proto) | ||||||||
| protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES}) | ||||||||
|
|
||||||||
| include_directories(${CMAKE_CURRENT_BINARY_DIR}) | ||||||||
| include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) | ||||||||
| include_directories(${Python3_INCLUDE_DIRS}) | ||||||||
|
|
||||||||
| file(GLOB SOURCES "src/lib/*.cpp" "src/lib/*.cpp" ) | ||||||||
|
|
||||||||
| add_library(onnx-subgraph-parser STATIC ${SOURCES} ${PROTO_SRCS} ${PROTO_FILES}) | ||||||||
| target_link_libraries(onnx-subgraph-parser protobuf jsoncpp) | ||||||||
|
|
||||||||
| add_executable(onnx-subgraph src/main.cpp) | ||||||||
| target_link_libraries(onnx-subgraph ${Python3_LIBRARIES}) | ||||||||
| target_link_libraries(onnx-subgraph onnx-subgraph-parser ${Python3_LIBRARIES}) | ||||||||
|
||||||||
| target_link_libraries(onnx-subgraph onnx-subgraph-parser ${Python3_LIBRARIES}) | |
| target_link_libraries(onnx-subgraph onnx-subgraph-parser) | |
| target_link_libraries(onnx-subgraph ${Python3_LIBRARIES}) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,92 @@ | ||||||||||||||||
| /* | ||||||||||||||||
| * Copyright (c) 2025 Samsung Electronics Co., Ltd. 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. | ||||||||||||||||
| */ | ||||||||||||||||
|
|
||||||||||||||||
| #ifndef GRAPH_H | ||||||||||||||||
| #define GRAPH_H | ||||||||||||||||
|
|
||||||||||||||||
| #include "onnx.pb.h" | ||||||||||||||||
| #include <iostream> | ||||||||||||||||
seanshpark marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| #include <fstream> | ||||||||||||||||
| #include <unordered_map> | ||||||||||||||||
| #include <functional> | ||||||||||||||||
| // save the size of each node's inputs and outputs | ||||||||||||||||
| struct NodeIOSize | ||||||||||||||||
seanshpark marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
| { | ||||||||||||||||
| std::vector<std::vector<int64_t>> inputSizes; | ||||||||||||||||
| std::vector<std::vector<int64_t>> outputSizes; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| struct NodeTensor | ||||||||||||||||
| { | ||||||||||||||||
| std::string name; | ||||||||||||||||
| std::vector<int64_t> shape; | ||||||||||||||||
|
|
||||||||||||||||
| // Default constructor | ||||||||||||||||
| NodeTensor() = default; | ||||||||||||||||
|
|
||||||||||||||||
| // Constructor with parameters | ||||||||||||||||
| NodeTensor(const std::string &n, const std::vector<int64_t> &s) : name(n), shape(s) {} | ||||||||||||||||
|
|
||||||||||||||||
| // Equality comparison operator | ||||||||||||||||
| bool operator==(const NodeTensor &other) const | ||||||||||||||||
| { | ||||||||||||||||
| return name == other.name && shape == other.shape; | ||||||||||||||||
| } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| namespace std | ||||||||||||||||
| { | ||||||||||||||||
| template <> struct hash<NodeTensor> | ||||||||||||||||
| { | ||||||||||||||||
|
Comment on lines
+51
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| size_t operator()(const NodeTensor &tensor) const | ||||||||||||||||
| { | ||||||||||||||||
| size_t hashValue = hash<string>()(tensor.name); | ||||||||||||||||
| for (auto &val : tensor.shape) | ||||||||||||||||
| { | ||||||||||||||||
| hashValue ^= hash<int64_t>()(val) + 0x9e3779b9 + (hashValue << 6) + (hashValue >> 2); | ||||||||||||||||
| } | ||||||||||||||||
| return hashValue; | ||||||||||||||||
| } | ||||||||||||||||
| }; | ||||||||||||||||
| } // namespace std | ||||||||||||||||
seanshpark marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
|
|
||||||||||||||||
| class Graph | ||||||||||||||||
|
||||||||||||||||
| { | ||||||||||||||||
| private: | ||||||||||||||||
| /* data */ | ||||||||||||||||
| public: | ||||||||||||||||
| Graph() {} | ||||||||||||||||
| ~Graph() {} | ||||||||||||||||
seanshpark marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
| /** | ||||||||||||||||
| * @brief Loads an ONNX model from a file and returns the graph contained within. | ||||||||||||||||
| * | ||||||||||||||||
| * @param [in] path The file path to the ONNX model. | ||||||||||||||||
| * @pre The file specified by path should exist and be a valid ONNX model. | ||||||||||||||||
| * @post The ONNX model is parsed and its graph is returned. | ||||||||||||||||
| * @exception Exits the program with an error message if the file cannot be opened. | ||||||||||||||||
| * @return The ONNX GraphProto object representing the graph from the model. | ||||||||||||||||
| */ | ||||||||||||||||
| onnx::GraphProto GetGraphFromOnnx(std::string &path); | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| struct graph_adjacency_node | ||||||||||||||||
|
||||||||||||||||
| { | ||||||||||||||||
| std::vector<int> output_node_index; | ||||||||||||||||
| int rank; | ||||||||||||||||
| std::string name; | ||||||||||||||||
| int index; | ||||||||||||||||
| }; | ||||||||||||||||
| #endif | ||||||||||||||||
|
||||||||||||||||
| }; | |
| #endif | |
| }; | |
| #endif // GRAPH_H |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| #endif | |
| #endif // GRAPH_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/lib/*.cppis added twice. is there particular reason?