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
24 changes: 24 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build onnxinfo source code and run tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build_and_test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Build docker image and source code
run: |
docker build -t onnxinfo -f docker/Dockerfile .

- name: Run tests (pytest)
run: |
docker run onnxinfo
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(ABSL_PROPAGATE_CXX_STD ON)
set(protobuf_BUILD_TESTS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${onnxinfo_SOURCE_DIR}/tests)

# protobuf
include(FetchContent)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ A tool to show ONNX model summary like torchinfo
3. `cmake -S . -B build/`
4. `cmake --build build/ [--parallel <thread number>]` to build dependency and onnxinfo

<!-- ## Test
`python3 -m pytest -v` -->
## Test
`python3 -m pytest -v`

Use model from [ONNX Model Zoo](https://github.com/onnx/models/tree/main) to test.

## Docker
* Run `docker build -t onnxinfo -f docker/Dockerfile .` first.
Expand Down
37 changes: 9 additions & 28 deletions docker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@ set(protobuf_BUILD_TESTS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# protobuf
# include(FetchContent)
# FetchContent_Declare(
# protobuf
# GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
# GIT_TAG e6ab258b7ca407ed1bad8a2f04971e72b16f5409 # v28.2
# )
# FetchContent_MakeAvailable(protobuf)

# generate onnx.proto3.pb.h and onnx.proto3.pb.cc
if (NOT EXISTS ${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3)
file(MAKE_DIRECTORY ${onnxinfo_SOURCE_DIR}/third_party/onnx)
execute_process(
COMMAND wget "https://raw.githubusercontent.com/onnx/onnx/main/onnx/onnx.proto3" -O ${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3
)
endif()
add_custom_command(
OUTPUT ${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3.pb.h
${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3.pb.cc
COMMAND protobuf::protoc --cpp_out=${onnxinfo_SOURCE_DIR}/third_party/onnx -I${onnxinfo_SOURCE_DIR}/third_party/onnx onnx.proto3
include(FetchContent)
FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG e6ab258b7ca407ed1bad8a2f04971e72b16f5409 # v28.2
)
FetchContent_MakeAvailable(protobuf)

# pybind11
find_package(pybind11 CONFIG)
Expand Down Expand Up @@ -56,16 +43,10 @@ add_library(
${SOURCES}
${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3.pb.cc
)
add_dependencies(${PROJECT_NAME} protobuf::protoc)
target_link_libraries(${PROJECT_NAME} protobuf::libprotobuf-lite)
target_link_libraries(${PROJECT_NAME}
protobuf::libprotobuf
)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${onnxinfo_SOURCE_DIR}/tests)
pybind11_add_module(_onnxinfo src/main.cpp)
target_link_libraries(_onnxinfo PRIVATE ${PROJECT_NAME})

# custom cmake target for test
# add_custom_target(test ALL)
# add_custom_command(
# TARGET test
# COMMAND python3 -m pytest -v
# )
30 changes: 7 additions & 23 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
# FROM debian:12-slim

# RUN apt-get update \
# && DEBIAN_FRONTEND=noninteractive apt-get install -y \
# build-essential cmake pybind11-dev python3.11-dev \
# git python3.11 python3-pytest \
# && apt-get clean autoclean \
# && apt-get autoremove --yes \
# && rm -rf /var/lib/apt/lists/*

# RUN mkdir /onnxinfo

# COPY docker/CMakeLists.txt /onnxinfo

# WORKDIR /onnxinfo

# RUN cmake -S . -B build/
# && cmake --build build/ --parallel 6

# CMD ["bash"]
### original Dockerfile for explorerray/onnxinfo:latest

FROM explorerray/onnxinfo:latest

COPY . /onnxinfo

WORKDIR /onnxinfo

RUN cmake --build build/ --parallel 6
RUN wget "https://raw.githubusercontent.com/onnx/onnx/main/onnx/onnx.proto3" -O ./third_party/onnx/onnx.proto3

RUN mkdir models \
&& wget "https://github.com/onnx/models/raw/refs/heads/main/Computer_Vision/resnet18_Opset16_timm/resnet18_Opset16.onnx" -O ./models/resnet18_Opset16.onnx

RUN mv docker/CMakeLists.txt . \
&& cmake --build build/ --parallel 4

# CMD ["bash"]
CMD ["python3", "-m", "pytest", "tests/", "-v"]
4 changes: 4 additions & 0 deletions docker/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
docker/Dockerfile
docker/original/
build/
.git/
*.md
models/
third_party/
*.so
64 changes: 64 additions & 0 deletions docker/original/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
cmake_minimum_required(VERSION 3.25)
project(onnxinfo CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ABSL_PROPAGATE_CXX_STD ON)
set(protobuf_BUILD_TESTS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# protobuf
include(FetchContent)
FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
GIT_TAG e6ab258b7ca407ed1bad8a2f04971e72b16f5409 # v28.2
)
FetchContent_MakeAvailable(protobuf)

# generate onnx.proto3.pb.h and onnx.proto3.pb.cc
if (NOT EXISTS ${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3)
file(MAKE_DIRECTORY ${onnxinfo_SOURCE_DIR}/third_party/onnx)
execute_process(
COMMAND wget "https://raw.githubusercontent.com/onnx/onnx/main/onnx/onnx.proto3" -O ${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3
)
endif()
add_custom_command(
OUTPUT ${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3.pb.h
${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3.pb.cc
COMMAND protobuf::protoc --cpp_out=${onnxinfo_SOURCE_DIR}/third_party/onnx -I${onnxinfo_SOURCE_DIR}/third_party/onnx onnx.proto3
)

# pybind11
find_package(pybind11 CONFIG)

# start building onnxinfo library for pybind11
include_directories(
${protobuf_SOURCE_DIR}/third_party/abseil-cpp
${protobuf_SOURCE_DIR}
${pybind11_SOURCE_DIR}/include
${onnxinfo_SOURCE_DIR}/include
${onnxinfo_SOURCE_DIR}/third_party/onnx
)

link_directories(
${protobuf_BINARY_DIR}
${protobuf_BINARY_DIR}/third_party/abseil-cpp/absl
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall -fPIC -flto=auto")
file(GLOB_RECURSE SOURCES "${onnxinfo_SOURCE_DIR}/src/*.cpp")
list(REMOVE_ITEM SOURCES "${onnxinfo_SOURCE_DIR}/src/main.cpp")

add_library(
${PROJECT_NAME} STATIC

${SOURCES}
${onnxinfo_SOURCE_DIR}/third_party/onnx/onnx.proto3.pb.cc
)
add_dependencies(${PROJECT_NAME} protobuf::protoc)
target_link_libraries(${PROJECT_NAME} protobuf::libprotobuf)

# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${onnxinfo_SOURCE_DIR}/tests)
# pybind11_add_module(_onnxinfo src/main.cpp)
# target_link_libraries(_onnxinfo PRIVATE ${PROJECT_NAME})
22 changes: 22 additions & 0 deletions docker/original/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM debian:12-slim

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
build-essential cmake wget git \
pybind11-dev python3.11-dev python3.11 \
python3-onnx python3-pytest \
&& apt-get clean autoclean \
&& apt-get autoremove --yes \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir /onnxinfo

COPY docker/original/CMakeLists.txt /onnxinfo

WORKDIR /onnxinfo

RUN cmake -S . -B build/ \
&& cmake --build build/ --parallel 6

CMD ["bash"]
### original Dockerfile for explorerray/onnxinfo:latest
4 changes: 2 additions & 2 deletions tests/test_onnxinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ def test_read_onnx_nofile():
_onnxinfo.read_onnx('non-existing.onnx')

def test_read_onnx():
info = _onnxinfo.read_onnx('models/resnet50-new.onnx')
info = _onnxinfo.read_onnx('models/resnet18_Opset16.onnx')
assert info is not None

def test_iterate_onnx():
try:
graph = _onnxinfo.read_onnx('models/resnet50-new.onnx')
graph = _onnxinfo.read_onnx('models/resnet18_Opset16.onnx')
_onnxinfo.iterate_graph(graph)
except:
pytest.fail("iterate_graph failed")