diff --git a/lite/include/lite/pack_model.h b/lite/include/lite/pack_model.h index 14e7227bc6e88ed378a62620bb2bd645b7a29c5c..fb2ab1814168896b4d32bcfd614994f9fc2833dc 100644 --- a/lite/include/lite/pack_model.h +++ b/lite/include/lite/pack_model.h @@ -11,7 +11,7 @@ #pragma once #include - +#include namespace lite { struct FeatureBits32 { @@ -45,6 +45,11 @@ public: std::string model_path, std::string packed_model_path, std::string info_data_path = "", std::string info_algo_policy_path = "", std::string info_binary_cache_path = ""); + ModelPacker( + std::vector model_data, std::string packed_model_path, + std::vector info_data = {}, + std::vector info_algo_policy_data = {}, + std::vector info_binary_cache_data = {}); void set_header( std::string model_decryption_method = "NONE", @@ -53,13 +58,12 @@ public: void pack_model(); private: - std::string m_packed_model_path; - std::string m_info_data_path; + std::vector m_info_data; //! fastrun cache / algo policy - std::string m_info_algo_policy_path; + std::vector m_algo_policy_data; //! binary cache - std::string m_info_binary_cache_path; - + std::vector m_binary_cache_data; + std::string m_packed_model_path; Header m_header; friend class FbsHelper; diff --git a/lite/src/pack_model/pack_model.cpp b/lite/src/pack_model/pack_model.cpp index d4594ea1f4f830022b0955d7f78475a7c6af3806..a305bd4fb997bd4597fb61efb83023b77dfffa90 100644 --- a/lite/src/pack_model/pack_model.cpp +++ b/lite/src/pack_model/pack_model.cpp @@ -25,6 +25,7 @@ class FbsHelper { public: FbsHelper() = default; FbsHelper(ModelPacker* packer, std::string model_path); + FbsHelper(ModelPacker* packer, std::vector& model_data); flatbuffers::Offset build_header(); flatbuffers::Offset build_info(); flatbuffers::Offset build_data(); @@ -58,6 +59,19 @@ std::vector read_file(std::string path) { fclose(fin); return buf; } +FbsHelper::FbsHelper(ModelPacker* packer, std::vector& model_data) + : m_packer(packer), m_model_buffer(model_data) { + const char* model_ptr = + static_cast(static_cast(m_model_buffer.data())); + std::string tag(model_ptr, 12); + if (tag == "packed_model") { + uint8_t* buffer = m_model_buffer.data() + 12; + auto model = GetPackModel(buffer)->models()->Get(0); + m_model_header = model->header(); + m_model_info = model->info(); + m_model_data = model->data(); + } +} FbsHelper::FbsHelper(ModelPacker* packer, std::string model_path) : m_packer(packer) { m_model_buffer = read_file(model_path); @@ -118,21 +132,20 @@ flatbuffers::Offset FbsHelper::build_data() { flatbuffers::Offset FbsHelper::build_info() { flatbuffers::Offset> fb_data; - if (m_model_info && m_model_info->data() && m_packer->m_info_data_path.empty()) { + if (m_model_info && m_model_info->data() && m_packer->m_info_data.empty()) { auto data = m_model_info->data()->Data(); auto size = m_model_info->data()->size(); fb_data = m_builder.CreateVector(data, size); - } else if (!m_packer->m_info_data_path.empty()) { - auto info_data = read_file(m_packer->m_info_data_path); - fb_data = m_builder.CreateVector(info_data); + } else if (!m_packer->m_info_data.empty()) { + fb_data = m_builder.CreateVector(m_packer->m_info_data); } flatbuffers::Offset> fb_algo_policy; flatbuffers::Offset> fb_binary_cache; if (m_packer->m_header.fb32.is_fast_run_cache) { std::vector info_algo_policy; - if (!m_packer->m_info_algo_policy_path.empty()) { - info_algo_policy = read_file(m_packer->m_info_algo_policy_path); + if (!m_packer->m_algo_policy_data.empty()) { + info_algo_policy = m_packer->m_algo_policy_data; if (m_model_info && m_model_info->algo_policy()) { auto cache = m_model_info->algo_policy()->Data(); auto size = m_model_info->algo_policy()->size(); @@ -178,11 +191,27 @@ ModelPacker::ModelPacker( std::string model_path, std::string packed_model_path, std::string info_data_path, std::string info_algo_policy_path, std::string info_binary_cache_path) - : m_packed_model_path(packed_model_path), - m_info_data_path(info_data_path), - m_info_algo_policy_path(info_algo_policy_path), - m_info_binary_cache_path(info_binary_cache_path) { + : m_packed_model_path(packed_model_path) { m_fbs_helper = new FbsHelper(this, model_path); + std::vector empty_vec; + m_info_data = info_data_path.empty() ? empty_vec : read_file(info_data_path); + m_algo_policy_data = info_algo_policy_path.empty() + ? empty_vec + : read_file(info_algo_policy_path); + m_binary_cache_data = info_binary_cache_path.empty() + ? empty_vec + : read_file(info_binary_cache_path); +} + +ModelPacker::ModelPacker( + std::vector model_data, std::string packed_model_path, + std::vector info_data, std::vector info_algo_policy_data, + std::vector info_binary_cache_data) { + m_fbs_helper = new FbsHelper(this, model_data); + m_packed_model_path = packed_model_path; + m_info_data = info_data; + m_algo_policy_data = info_algo_policy_data; + m_binary_cache_data = info_binary_cache_data; } void ModelPacker::set_header( @@ -192,10 +221,10 @@ void ModelPacker::set_header( m_header.info_decryption_method = info_decryption_method; memset(&m_header.fb32, 0, sizeof(m_header.fb32)); m_header.fb32.is_fast_run_cache = is_fast_run_cache; - if (!m_info_data_path.empty()) { - auto buf = read_file(m_info_data_path); + if (!m_info_data.empty()) { std::string json_string( - static_cast(static_cast(buf.data())), buf.size()); + static_cast(static_cast(m_info_data.data())), + m_info_data.size()); auto info = nlohmann::json::parse(json_string); m_header.name = info["name"]; } @@ -221,6 +250,9 @@ void ModelPacker::pack_model() { m_fbs_helper->builder().Finish(pack_model_builder.Finish()); FILE* fptr = fopen(m_packed_model_path.c_str(), "wb"); + LITE_ASSERT( + fptr, "failed to open %s: %s", m_packed_model_path.c_str(), + strerror(errno)); std::string packed_model_tag = "packed_model"; auto nr_tag = fwrite(packed_model_tag.c_str(), 1, packed_model_tag.size(), fptr); LITE_ASSERT(nr_tag == packed_model_tag.size()); diff --git a/src/core/include/megbrain/utils/json.h b/src/core/include/megbrain/utils/json.h index a33db07b8730cb02b66d8d34b38141a85867554b..2b9e0e62aba23d2006c04523084c88f54055d34d 100644 --- a/src/core/include/megbrain/utils/json.h +++ b/src/core/include/megbrain/utils/json.h @@ -15,7 +15,8 @@ namespace json { class Value : public std::enable_shared_from_this, public DynTypeObj { public: - virtual void writeto(std::string& fout, int indent = 0) const = 0; + MGE_WIN_DECLSPEC_FUC virtual void writeto( + std::string& fout, int indent = 0) const = 0; MGE_WIN_DECLSPEC_FUC void writeto_fpath( const std::string& fout_path, int indent = 0) const { @@ -38,11 +39,11 @@ class Number final : public Value { public: Number(double v) : m_val(v) {} - static std::shared_ptr make(double v) { + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make(double v) { return std::make_shared(v); } - void writeto(std::string& fout, int indent = 0) const override; + MGE_WIN_DECLSPEC_FUC void writeto(std::string& fout, int indent = 0) const override; auto&& get_impl() { return m_val; } @@ -57,7 +58,7 @@ class NumberInt final : public Value { public: NumberInt(int64_t v) : m_val(v) {} - static std::shared_ptr make(int64_t v) { + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make(int64_t v) { return std::make_shared(v); } @@ -76,7 +77,7 @@ class Bool final : public Value { public: Bool(bool v) : m_val(v) {} - static std::shared_ptr make(bool v); + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make(bool v); MGE_WIN_DECLSPEC_FUC void writeto(std::string& fout, int indent = 0) const override; @@ -95,11 +96,13 @@ public: String(char const* v) : m_val(v) {} - static std::shared_ptr make(const std::string& v) { + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make(const std::string& v) { return std::make_shared(v); } - bool operator==(const String& rhs) const { return m_val == rhs.m_val; } + MGE_WIN_DECLSPEC_FUC bool operator==(const String& rhs) const { + return m_val == rhs.m_val; + } MGE_WIN_DECLSPEC_FUC void writeto(std::string& fout, int indent = 0) const override; @@ -114,9 +117,11 @@ class Object final : public Value { std::unordered_map, StdHashAdaptor> m_val; public: - static std::shared_ptr make() { return std::make_shared(); } + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make() { + return std::make_shared(); + } - static std::shared_ptr make( + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make( const std::vector>>& val) { for (auto&& i : val) mgb_assert(i.second); @@ -125,11 +130,17 @@ public: return rst; } - std::shared_ptr& operator[](const String& s) { return m_val[s]; } + MGE_WIN_DECLSPEC_FUC std::shared_ptr& operator[](const String& s) { + return m_val[s]; + } - std::shared_ptr& operator[](const std::string& s) { return m_val[s]; } + MGE_WIN_DECLSPEC_FUC std::shared_ptr& operator[](const std::string& s) { + return m_val[s]; + } - std::shared_ptr& operator[](const char* s) { return m_val[std::string(s)]; } + MGE_WIN_DECLSPEC_FUC std::shared_ptr& operator[](const char* s) { + return m_val[std::string(s)]; + } MGE_WIN_DECLSPEC_FUC void writeto(std::string& fout, int indent = 0) const override; @@ -144,14 +155,18 @@ class Array final : public Value { std::vector> m_val; public: - static std::shared_ptr make() { return std::make_shared(); } + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make() { + return std::make_shared(); + } - void add(std::shared_ptr val) { + MGE_WIN_DECLSPEC_FUC void add(std::shared_ptr val) { mgb_assert(val); m_val.emplace_back(std::move(val)); } - std::shared_ptr& operator[](size_t idx) { return m_val.at(idx); } + MGE_WIN_DECLSPEC_FUC std::shared_ptr& operator[](size_t idx) { + return m_val.at(idx); + } MGE_WIN_DECLSPEC_FUC void writeto(std::string& fout, int indent = 0) const override; @@ -164,7 +179,7 @@ class Null final : public Value { MGB_DYN_TYPE_OBJ_FINAL_DECL_WITH_EXPORT; public: - static std::shared_ptr make() { + MGE_WIN_DECLSPEC_FUC static std::shared_ptr make() { static std::shared_ptr v(new Null); return v; } diff --git a/src/gopt/impl/inference.cpp b/src/gopt/impl/inference.cpp index 8d7b74ee60c74f3b7fd8c14ecad8997ef7e1ffe2..43293311aa4e31cf49e847f9cd6ca0e10d745041 100644 --- a/src/gopt/impl/inference.cpp +++ b/src/gopt/impl/inference.cpp @@ -524,9 +524,14 @@ void ParamFusePass::apply(OptState& state) const { { auto orig_level = cg->options().log_level; + auto orig_record_level = cg->options().comp_node_seq_record_level; + cg->options().comp_node_seq_record_level = 0; cg->options().log_level = 0; MGB_TRY { cg->compile({{var, cb}})->execute(); } - MGB_FINALLY(cg->options().log_level = orig_level); + MGB_FINALLY({ + cg->options().comp_node_seq_record_level = orig_record_level; + cg->options().log_level = orig_level; + }); } SymbolVar new_var; diff --git a/src/opr/impl/io.cpp b/src/opr/impl/io.cpp index 253033955dcf523d46c907ff962ada5c6b59cf68..da0f0414ae1d6efdc91df4d36328ef120f50fa2c 100644 --- a/src/opr/impl/io.cpp +++ b/src/opr/impl/io.cpp @@ -398,7 +398,7 @@ void ImmutableTensor::Value::setup(CompNode cn, const HostTensorND& val) { return true; }; - if (one_elem(val.shape())) { + if (!val.empty() && one_elem(val.shape())) { float v; static_cast_dtype(&v, val.dtype(), val.raw_ptr()); m_summary = ssprintf("const<%.3g>", v);