Skip to content
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

pnnx importer #474

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions include/nncase/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class NNCASE_API compiler
virtual void import_tflite(std::span<const uint8_t> model, const import_options &options) = 0;
virtual void import_onnx(std::span<const uint8_t> model, const import_options &options) = 0;
virtual void import_caffe(std::span<const uint8_t> model, std::span<const uint8_t> prototxt) = 0;
virtual void import_pnnx(std::string parampath, std::string binpath, const import_options &options) = 0;
virtual void use_ptq(ptq_dataset_options options) = 0;
virtual void use_ptq(ptq_tensor_options options) = 0;
virtual void dump_range_options(dump_range_dataset_options options) = 0;
Expand Down
1 change: 1 addition & 0 deletions include/nncase/importer/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ struct import_options
void import_tflite(ir::graph &graph, std::span<const uint8_t> model, const import_options &options, std::string &real_inlayout, std::string &real_outlayout);
void import_onnx(ir::graph &graph, std::span<const uint8_t> model, const import_options &options, std::string &real_inlayout, std::string &real_outlayout);
void import_caffe(ir::graph &graph, std::span<const uint8_t> model, std::span<const uint8_t> prototxt, std::string &real_inlayout, std::string &real_outlayout);
void import_pnnx(ir::graph &graph, std::string parampath, std::string binpath, const import_options &options, std::string &real_inlayout, std::string &real_outlayout);
}
1 change: 1 addition & 0 deletions python/nncase/native/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ PYBIND11_MODULE(_nncase, m)
.def("import_tflite", &compiler::import_tflite)
.def("import_onnx", &compiler::import_onnx)
.def("import_caffe", &compiler::import_caffe)
.def("import_pnnx", &compiler::import_pnnx)
.def("compile", &compiler::compile)
.def("use_ptq", py::overload_cast<ptq_tensor_options>(&compiler::use_ptq))
.def("dump_range_options", py::overload_cast<dump_range_tensor_options>(&compiler::dump_range_options))
Expand Down
8 changes: 7 additions & 1 deletion src/cli/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace nncase::cli;
compile_command::compile_command(lyra::cli &cli)
{
cli.add_argument(lyra::command("compile", [this](const lyra::group &) { this->run(); })
.add_argument(lyra::opt(input_format_, "input format").name("-i").name("--input-format").required().help("input format, e.g. tflite|onnx|caffe"))
.add_argument(lyra::opt(input_format_, "input format").name("-i").name("--input-format").required().help("input format, e.g. tflite|onnx|caffe|pnnx"))
.add_argument(lyra::opt(target_name_, "target").name("-t").name("--target").required().help("target architecture, e.g. cpu|k210|k510"))
.add_argument(lyra::arg(input_filename_, "input file").required().help("input file"))
.add_argument(lyra::opt(input_prototxt_, "input prototxt").name("--input-prototxt").optional().help("input prototxt"))
Expand Down Expand Up @@ -153,6 +153,12 @@ void compile_command::run()
auto input_prototxt = read_file(input_prototxt_);
compiler->import_caffe(file_data, input_prototxt);
}
else if (input_format_ == "pnnx")
{
std::filesystem::path input_bin_filename_ = input_filename_;
input_bin_filename_.replace_extension("bin");
compiler->import_pnnx(input_filename_, input_bin_filename_.string(), i_options);
}
else
{
throw std::invalid_argument("Invalid input format: " + input_format_);
Expand Down
5 changes: 3 additions & 2 deletions src/importer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
add_subdirectory(tflite)
add_subdirectory(onnx)
add_subdirectory(caffe)
add_subdirectory(pnnx)

add_library(importer OBJECT importer.cpp)
target_include_directories(importer PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries(importer PUBLIC tflite_importer onnx_proto onnx_importer caffe_importer)
set_target_properties(importer PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(importer PUBLIC tflite_importer onnx_proto onnx_importer caffe_importer pnnx_importer)
set_target_properties(importer PROPERTIES POSITION_INDEPENDENT_CODE ON)
8 changes: 7 additions & 1 deletion src/importer/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
#include "caffe/caffe_importer.h"
#include "onnx/onnx_importer.h"
#include "pnnx/pnnx_importer.h"
#include "tflite/tflite_importer.h"
#include <nncase/importer/importer.h>

Expand All @@ -34,4 +35,9 @@ void nncase::importer::import_onnx(ir::graph &graph, std::span<const uint8_t> mo
void nncase::importer::import_caffe(ir::graph &graph, std::span<const uint8_t> model, std::span<const uint8_t> prototxt, std::string &real_inlayout, std::string &real_outlayout)
{
caffe_importer(model, prototxt, graph).import(real_inlayout, real_outlayout);
}
}

void nncase::importer::import_pnnx(ir::graph &graph, std::string parampath, std::string binpath, const import_options &options, std::string &real_inlayout, std::string &real_outlayout)
{
pnnx_importer(parampath, binpath, graph).import(options, real_inlayout, real_outlayout);
}
26 changes: 26 additions & 0 deletions src/importer/pnnx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
include(TestBigEndian)
test_big_endian(BIG_ENDIAN)

set(PNNX_IMPORTER_SOURCES
pnnx_importer.cpp
ir.cpp
storezip.cpp
)

set(PNNX_IMPORTER_OPS_SOURCES
ops/input.cpp
ops/output.cpp
ops/relu.cpp
ops/relu6.cpp
ops/conv2d.cpp
)

add_library(pnnx_importer ${PNNX_IMPORTER_SOURCES} ${PNNX_IMPORTER_OPS_SOURCES})

target_compile_definitions(pnnx_importer PRIVATE NATIVE_IS_BIG_ENDIAN=${BIG_ENDIAN})
target_include_directories(pnnx_importer PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
get_filename_component(PARENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
target_include_directories(pnnx_importer PUBLIC ${PARENT_SOURCE_DIR}/include)

target_link_libraries(pnnx_importer PUBLIC ir)
set_target_properties(pnnx_importer PROPERTIES POSITION_INDEPENDENT_CODE ON)
Loading