Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion tools/onnx_subgraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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" )
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/lib/*.cpp is added twice. is there particular reason?


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})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional)

Suggested change
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})


set(ONNX_SUBGRAPH_FILES
test_model_download.sh
Expand Down
92 changes: 92 additions & 0 deletions tools/onnx_subgraph/include/graph.h
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>
#include <fstream>
#include <unordered_map>
#include <functional>
// save the size of each node's inputs and outputs
struct NodeIOSize
{
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
template <> struct hash<NodeTensor>
{
{
template <> struct hash<NodeTensor>
{

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

class Graph
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q) is there particular reason to use class? only single member exist and it's public, so it can be used with struct. what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it indeed doesn't need a class, just an API is ok, I have changed, thank you

{
private:
/* data */
public:
Graph() {}
~Graph() {}
/**
* @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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other struct uses pascal naming convention. is there particular reason to use snake case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change naming convention for this struct, thank you

{
std::vector<int> output_node_index;
int rank;
std::string name;
int index;
};
#endif
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
};
#endif
};
#endif // GRAPH_H

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
#endif // GRAPH_H

Loading