From: Sergei Barannikov/AI Tools Lab /SRR/Engineer/Samsung Electronics Date: Fri, 6 Dec 2019 10:29:59 +0000 (+0300) Subject: [min_onnx] Support models stored as text (#9426) X-Git-Tag: accepted/tizen/unified/20191209.144032~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=20ba332946b791b2bc62a77d940ab75077beda6e;p=platform%2Fcore%2Fml%2Fnnfw.git [min_onnx] Support models stored as text (#9426) Add support for models stored as text. Signed-off-by: Sergei Barannikov --- diff --git a/compiler/mir-onnx-importer/ONNXImporterImpl.cpp b/compiler/mir-onnx-importer/ONNXImporterImpl.cpp index cbe03ed..800e982 100644 --- a/compiler/mir-onnx-importer/ONNXImporterImpl.cpp +++ b/compiler/mir-onnx-importer/ONNXImporterImpl.cpp @@ -19,10 +19,8 @@ #include "ONNXOpRegistration.h" #include "onnx/onnx.pb.h" -#include "mir/Operation.h" #include "mir/Shape.h" #include "mir/TensorUtil.h" -#include "mir/TensorVariant.h" #include "mir/ops/ConstantOp.h" @@ -30,45 +28,41 @@ #include #include +#include #include #include #include #include -#include -namespace +namespace mir_onnx { -using namespace mir_onnx; +namespace +{ class ONNXImporterImpl final { public: - explicit ONNXImporterImpl(std::string filename); + ONNXImporterImpl(); ~ONNXImporterImpl(); /// @brief Load the model and convert it into a MIR Graph. - std::unique_ptr importModel(); + std::unique_ptr importModelFromBinaryFile(const std::string &filename); + std::unique_ptr importModelFromTextFile(const std::string &filename); private: - void import(); std::unique_ptr createIR(); void createGraphInputs(); void collectUnsupportedOps(); - // Maps ONNX tensor names to corresponding MIR operation outputs. - std::string _modelFilename; std::unique_ptr _model; std::unique_ptr _context; std::unique_ptr _graph; }; -ONNXImporterImpl::ONNXImporterImpl(std::string filename) : _modelFilename(std::move(filename)) -{ - registerSupportedOps(); -} +ONNXImporterImpl::ONNXImporterImpl() { registerSupportedOps(); } ONNXImporterImpl::~ONNXImporterImpl() = default; -static void loadModelFile(const std::string &filename, onnx::ModelProto *model) +void loadModelFromBinaryFile(const std::string &filename, onnx::ModelProto *model) { GOOGLE_PROTOBUF_VERIFY_VERSION; @@ -92,12 +86,39 @@ static void loadModelFile(const std::string &filename, onnx::ModelProto *model) throw std::runtime_error("File \"" + filename + "\" has not been consumed entirely."); } -void ONNXImporterImpl::import() +void loadModelFromTextFile(const std::string &filename, onnx::ModelProto *model) +{ + GOOGLE_PROTOBUF_VERIFY_VERSION; + + int file_handle = open(filename.c_str(), O_RDONLY); + + if (file_handle == -1) + throw std::runtime_error("Couldn't open file \"" + filename + "\": " + std::strerror(errno) + + "."); + + google::protobuf::io::FileInputStream file_stream(file_handle); + file_stream.SetCloseOnDelete(true); + + if (!google::protobuf::TextFormat::Parse(&file_stream, model)) + throw std::runtime_error("Couldn't parse file \"" + filename + "\"."); +} + +std::unique_ptr ONNXImporterImpl::importModelFromBinaryFile(const std::string &filename) { _model = stdex::make_unique(); - loadModelFile(_modelFilename, _model.get()); + loadModelFromBinaryFile(filename, _model.get()); collectUnsupportedOps(); + return createIR(); +} + +std::unique_ptr ONNXImporterImpl::importModelFromTextFile(const std::string &filename) +{ + _model = stdex::make_unique(); + loadModelFromTextFile(filename, _model.get()); + + collectUnsupportedOps(); + return createIR(); } void ONNXImporterImpl::collectUnsupportedOps() @@ -201,20 +222,23 @@ std::unique_ptr ONNXImporterImpl::createIR() return std::move(_graph); } -std::unique_ptr ONNXImporterImpl::importModel() +} // namespace + +std::unique_ptr importModelFromBinaryFile(const std::string &filename) { - import(); - return createIR(); -} + ONNXImporterImpl importer; + return importer.importModelFromBinaryFile(filename); } -namespace mir_onnx +std::unique_ptr importModelFromTextFile(const std::string &filename) { + ONNXImporterImpl importer; + return importer.importModelFromTextFile(filename); +} -std::unique_ptr loadModel(std::string filename) +std::unique_ptr loadModel(const std::string &filename) { - ONNXImporterImpl importer(std::move(filename)); - return importer.importModel(); + return importModelFromBinaryFile(filename); } } // namespace mir_onnx diff --git a/compiler/mir-onnx-importer/ONNXImporterImpl.h b/compiler/mir-onnx-importer/ONNXImporterImpl.h index 7b13370..02a49b3 100644 --- a/compiler/mir-onnx-importer/ONNXImporterImpl.h +++ b/compiler/mir-onnx-importer/ONNXImporterImpl.h @@ -25,7 +25,10 @@ namespace mir_onnx { -std::unique_ptr loadModel(std::string filename); +std::unique_ptr importModelFromBinaryFile(const std::string &filename); +std::unique_ptr importModelFromTextFile(const std::string &filename); +// TODO Remove after changing all uses. +std::unique_ptr loadModel(const std::string &filename); } // namespace mir_onnx