提交 01287a08 编写于 作者: L Liangliang He

Sync mace changes and add latency logging

上级 a5bdbc62
...@@ -3,16 +3,23 @@ ...@@ -3,16 +3,23 @@
// Generated by the mace converter. DO NOT EDIT! // Generated by the mace converter. DO NOT EDIT!
// //
{% if mode == 0 %}
#include <vector> #include <vector>
#include "mace/core/public/mace.h" #include <string>
#include "mace/public/mace.h"
#include "mace/utils/env_time.h"
#include "mace/utils/logging.h"
{% if mode == 0 %}
namespace mace { namespace mace {
namespace {{tag}} { namespace {{tag}} {
void Create{{tensor.name}}(std::vector<mace::ConstTensor> &tensors, const unsigned char *model_data) { void Create{{tensor.name}}(std::vector<mace::ConstTensor> &tensors,
const unsigned char *model_data) {
MACE_LATENCY_LOGGER(2, "Create tensor {{ tensor.name }}");
tensors.emplace_back(mace::ConstTensor( tensors.emplace_back(mace::ConstTensor(
{{ tensor.name|tojson }}, const_cast<unsigned char *>(model_data) + {{ offset }}, {{ tensor.name|tojson }}, model_data + {{ offset }},
{ {{ tensor.dims|join(', ') }} }, {{ tensor_info.data_type }}, {{ tensor.node_id }})); { {{ tensor.dims|join(', ') }} }, {{ tensor_info.data_type }}, {{ tensor.node_id }}));
} }
...@@ -21,34 +28,51 @@ void Create{{tensor.name}}(std::vector<mace::ConstTensor> &tensors, const unsign ...@@ -21,34 +28,51 @@ void Create{{tensor.name}}(std::vector<mace::ConstTensor> &tensors, const unsign
{% elif mode == 1 %} {% elif mode == 1 %}
{% if not embed_model_data %} {% if not embed_model_data %}
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
{% endif %} {% endif %}
namespace mace { namespace mace {
namespace {{tag}} { namespace {{tag}} {
{% if embed_model_data %} {% 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%} {% for d in model_data %}{{"0x%02X, " % d }}{%endfor%}
}; };
{% endif %} {% endif %}
unsigned char *LoadModelData(const char *model_data_file) { const unsigned char *LoadModelData(const char *model_data_file) {
{% if embed_model_data %} {% if embed_model_data %}
return model_data; return model_data;
{% else %} {% else %}
int fd=open(model_data_file, O_RDONLY); 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); MACE_CHECK(fd >= 0, "Failed to open model data file ",
close(fd); model_data_file, ", error code: ", errno);
const unsigned char *model_data =
static_cast<const unsigned char *>(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; return model_data;
{% endif %} {% endif %}
} }
void UnloadModelData(unsigned char *model_data) { void UnloadModelData(const unsigned char *model_data) {
{% if not embed_model_data %} {% if not embed_model_data %}
munmap(model_data, {{ model_data_size }}); int ret = munmap(const_cast<unsigned char *>(model_data),
{{ model_data_size }});
MACE_CHECK(ret == 0, "Failed to unmap model data file, error code: ", errno);
{% endif %} {% endif %}
} }
...@@ -56,11 +80,10 @@ void UnloadModelData(unsigned char *model_data) { ...@@ -56,11 +80,10 @@ void UnloadModelData(unsigned char *model_data) {
} // namespace mace } // namespace mace
{% elif mode == 2 %} {% elif mode == 2 %}
#include <vector>
#include <string>
#include "mace/core/public/mace.h"
namespace mace {
namespace { namespace {
void UpdateOp(mace::OperatorDef &op, void UpdateOp(mace::OperatorDef &op,
const std::string &name, const std::string &name,
const std::string &type, const std::string &type,
...@@ -75,7 +98,9 @@ void UpdateOp(mace::OperatorDef &op, ...@@ -75,7 +98,9 @@ void UpdateOp(mace::OperatorDef &op,
op.set_output_type(output_types); op.set_output_type(output_types);
op.set_node_id(node_id); op.set_node_id(node_id);
} }
}
} // namespace
} // namespace mace
namespace mace { namespace mace {
namespace {{tag}} { namespace {{tag}} {
...@@ -83,6 +108,8 @@ namespace {{tag}} { ...@@ -83,6 +108,8 @@ namespace {{tag}} {
{% for i in range(start, end) %} {% for i in range(start, end) %}
void CreateOperator{{i}}(mace::OperatorDef &op) { void CreateOperator{{i}}(mace::OperatorDef &op) {
MACE_LATENCY_LOGGER(2, "Create operator ", op.name());
mace::Argument *arg = nullptr; mace::Argument *arg = nullptr;
{% for arg in net.op[i].arg %} {% for arg in net.op[i].arg %}
...@@ -149,7 +176,6 @@ void CreateOperator{{i}}(mace::OperatorDef &op) { ...@@ -149,7 +176,6 @@ void CreateOperator{{i}}(mace::OperatorDef &op) {
} }
{% endif %} {% endif %}
{% endif %} {% endif %}
} }
{% endfor %} {% endfor %}
...@@ -158,15 +184,13 @@ void CreateOperator{{i}}(mace::OperatorDef &op) { ...@@ -158,15 +184,13 @@ void CreateOperator{{i}}(mace::OperatorDef &op) {
} // namespace mace } // namespace mace
{% else %} {% else %}
#include <vector>
#include <string>
#include "mace/core/public/mace.h"
namespace mace { namespace mace {
namespace {{tag}} { namespace {{tag}} {
{% for tensor in tensors %} {% for tensor in tensors %}
extern void Create{{ tensor.name }}(std::vector<mace::ConstTensor> &tensors, const unsigned char *model_data); extern void Create{{ tensor.name }}(std::vector<mace::ConstTensor> &tensors,
const unsigned char *model_data);
{% endfor %} {% endfor %}
...@@ -175,8 +199,6 @@ extern void CreateOperator{{i}}(mace::OperatorDef &op); ...@@ -175,8 +199,6 @@ extern void CreateOperator{{i}}(mace::OperatorDef &op);
{% endfor %} {% endfor %}
} // namespace {{ tag }} } // namespace {{ tag }}
} // namespace mace
namespace { namespace {
...@@ -233,22 +255,24 @@ void CreateOutputInfo(mace::NetDef &net_def) { ...@@ -233,22 +255,24 @@ void CreateOutputInfo(mace::NetDef &net_def) {
{% endif %} {% endif %}
void CreateOperators(std::vector<mace::OperatorDef> &ops) { void CreateOperators(std::vector<mace::OperatorDef> &ops) {
MACE_LATENCY_LOGGER(1, "Create operators");
ops.resize({{ net.op|length }}); ops.resize({{ net.op|length }});
{% for i in range(net.op|length) %} {% for i in range(net.op|length) %}
mace::{{tag}}::CreateOperator{{i}}(ops[{{i}}]); mace::{{tag}}::CreateOperator{{i}}(ops[{{i}}]);
{% endfor %} {% endfor %}
} }
void CreateTensors(std::vector<mace::ConstTensor> &tensors, const unsigned char *model_data) { void CreateTensors(std::vector<mace::ConstTensor> &tensors,
const unsigned char *model_data) {
MACE_LATENCY_LOGGER(1, "Create tensors");
tensors.reserve({{ net.tensors|length }}); tensors.reserve({{ net.tensors|length }});
{% for tensor in net.tensors %} {% for tensor in net.tensors %}
mace::{{tag}}::Create{{tensor.name}}(tensors, model_data); mace::{{tag}}::Create{{tensor.name}}(tensors, model_data);
{% endfor %} {% endfor %}
} }
...@@ -266,12 +290,12 @@ void CreateMemoryArena(mace::MemoryArena &mem_arena) { ...@@ -266,12 +290,12 @@ void CreateMemoryArena(mace::MemoryArena &mem_arena) {
} }
{% endif %} {% endif %}
} } // namespace
namespace mace {
namespace {{tag}} { namespace {{tag}} {
NetDef CreateNet(const unsigned char *model_data) { NetDef CreateNet(const unsigned char *model_data) {
MACE_LATENCY_LOGGER(1, "Create net {{ net.name}}");
NetDef net_def; NetDef net_def;
net_def.set_name("{{ net.name}}"); net_def.set_name("{{ net.name}}");
net_def.set_version("{{ net.version }}"); net_def.set_version("{{ net.version }}");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册