Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion tools/onnx_subgraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ 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" )

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)
target_link_libraries(onnx-subgraph ${Python3_LIBRARIES})

set(ONNX_SUBGRAPH_FILES
test_model_download.sh
Expand Down
90 changes: 90 additions & 0 deletions tools/onnx_subgraph/include/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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>
#include <filesystem>

// 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 GraphAdjacencyNode
{
std::vector<int> outputNodeIndex;
int rank;
std::string name;
int index;
};

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>
{
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

/**
* @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);

#endif // GRAPH_H
Loading