From 01287a08fcd56070af595127ea22b8cb2360c15f Mon Sep 17 00:00:00 2001 From: Liangliang He Date: Tue, 27 Feb 2018 20:53:04 +0800 Subject: [PATCH] Sync mace changes and add latency logging --- python/tools/model.template | 80 ++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/python/tools/model.template b/python/tools/model.template index a11ccf85..6bcb3e76 100644 --- a/python/tools/model.template +++ b/python/tools/model.template @@ -3,16 +3,23 @@ // Generated by the mace converter. DO NOT EDIT! // -{% if mode == 0 %} #include -#include "mace/core/public/mace.h" +#include + +#include "mace/public/mace.h" +#include "mace/utils/env_time.h" +#include "mace/utils/logging.h" + +{% if mode == 0 %} namespace mace { namespace {{tag}} { -void Create{{tensor.name}}(std::vector &tensors, const unsigned char *model_data) { +void Create{{tensor.name}}(std::vector &tensors, + const unsigned char *model_data) { + MACE_LATENCY_LOGGER(2, "Create tensor {{ tensor.name }}"); tensors.emplace_back(mace::ConstTensor( - {{ tensor.name|tojson }}, const_cast(model_data) + {{ offset }}, + {{ tensor.name|tojson }}, model_data + {{ offset }}, { {{ tensor.dims|join(', ') }} }, {{ tensor_info.data_type }}, {{ tensor.node_id }})); } @@ -21,34 +28,51 @@ void Create{{tensor.name}}(std::vector &tensors, const unsign {% elif mode == 1 %} {% if not embed_model_data %} -#include + +#include #include +#include +#include #include + {% endif %} namespace mace { namespace {{tag}} { {% if embed_model_data %} -alignas(4) unsigned char model_data[{{ model_data_size }}] = { +alignas(4) const unsigned char model_data[{{ model_data_size }}] = { {% for d in model_data %}{{"0x%02X, " % d }}{%endfor%} }; {% endif %} -unsigned char *LoadModelData(const char *model_data_file) { +const unsigned char *LoadModelData(const char *model_data_file) { {% if embed_model_data %} return model_data; {% else %} - int fd=open(model_data_file, O_RDONLY); - unsigned char *model_data = (unsigned char *)mmap(nullptr, {{ model_data_size }}, PROT_READ, MAP_PRIVATE, fd, 0); - close(fd); + int fd = open(model_data_file, O_RDONLY); + MACE_CHECK(fd >= 0, "Failed to open model data file ", + model_data_file, ", error code: ", errno); + + const unsigned char *model_data = + static_cast(mmap(nullptr, {{ model_data_size }}, + PROT_READ, MAP_PRIVATE, fd, 0)); + MACE_CHECK(model_data != MAP_FAILED, "Failed to map model data file ", + model_data_file, ", error code: ", errno); + + int ret = close(fd); + MACE_CHECK(ret == 0, "Failed to close model data file ", + model_data_file, ", error code: ", errno); + return model_data; {% endif %} } -void UnloadModelData(unsigned char *model_data) { +void UnloadModelData(const unsigned char *model_data) { {% if not embed_model_data %} - munmap(model_data, {{ model_data_size }}); + int ret = munmap(const_cast(model_data), + {{ model_data_size }}); + MACE_CHECK(ret == 0, "Failed to unmap model data file, error code: ", errno); {% endif %} } @@ -56,11 +80,10 @@ void UnloadModelData(unsigned char *model_data) { } // namespace mace {% elif mode == 2 %} -#include -#include -#include "mace/core/public/mace.h" +namespace mace { namespace { + void UpdateOp(mace::OperatorDef &op, const std::string &name, const std::string &type, @@ -75,7 +98,9 @@ void UpdateOp(mace::OperatorDef &op, op.set_output_type(output_types); op.set_node_id(node_id); } -} + +} // namespace +} // namespace mace namespace mace { namespace {{tag}} { @@ -83,6 +108,8 @@ namespace {{tag}} { {% for i in range(start, end) %} void CreateOperator{{i}}(mace::OperatorDef &op) { + MACE_LATENCY_LOGGER(2, "Create operator ", op.name()); + mace::Argument *arg = nullptr; {% for arg in net.op[i].arg %} @@ -149,7 +176,6 @@ void CreateOperator{{i}}(mace::OperatorDef &op) { } {% endif %} {% endif %} - } {% endfor %} @@ -158,15 +184,13 @@ void CreateOperator{{i}}(mace::OperatorDef &op) { } // namespace mace {% else %} -#include -#include -#include "mace/core/public/mace.h" namespace mace { namespace {{tag}} { {% for tensor in tensors %} -extern void Create{{ tensor.name }}(std::vector &tensors, const unsigned char *model_data); +extern void Create{{ tensor.name }}(std::vector &tensors, + const unsigned char *model_data); {% endfor %} @@ -175,8 +199,6 @@ extern void CreateOperator{{i}}(mace::OperatorDef &op); {% endfor %} } // namespace {{ tag }} -} // namespace mace - namespace { @@ -233,22 +255,24 @@ void CreateOutputInfo(mace::NetDef &net_def) { {% endif %} void CreateOperators(std::vector &ops) { + MACE_LATENCY_LOGGER(1, "Create operators"); + ops.resize({{ net.op|length }}); {% for i in range(net.op|length) %} mace::{{tag}}::CreateOperator{{i}}(ops[{{i}}]); {% endfor %} - } -void CreateTensors(std::vector &tensors, const unsigned char *model_data) { +void CreateTensors(std::vector &tensors, + const unsigned char *model_data) { + MACE_LATENCY_LOGGER(1, "Create tensors"); tensors.reserve({{ net.tensors|length }}); {% for tensor in net.tensors %} mace::{{tag}}::Create{{tensor.name}}(tensors, model_data); {% endfor %} - } @@ -266,12 +290,12 @@ void CreateMemoryArena(mace::MemoryArena &mem_arena) { } {% endif %} -} +} // namespace -namespace mace { namespace {{tag}} { NetDef CreateNet(const unsigned char *model_data) { + MACE_LATENCY_LOGGER(1, "Create net {{ net.name}}"); NetDef net_def; net_def.set_name("{{ net.name}}"); net_def.set_version("{{ net.version }}"); -- GitLab