Skip to content

Commit

Permalink
Merge pull request apache#17 from GrassSunFlower/master
Browse files Browse the repository at this point in the history
Fix layers object detection and fix profiler attributes
  • Loading branch information
GrassSunFlower committed Jun 13, 2017
2 parents bece19a + f0600fb commit 45801ef
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 361 deletions.
14 changes: 10 additions & 4 deletions include/mxnet/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ class MXNET_API Engine {
* \param mutable_vars The variables that current operation will mutate.
* \param prop Property of the function.
* \param opr_name The operator name.
* \param attr_name The attribute name.
* \return The new operator allocated.
*/
virtual OprHandle NewOperator(AsyncFn fn,
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
const char* opr_name = nullptr) = 0;
const char* opr_name = nullptr,
const char* attr_name = nullptr) = 0;
/*!
* \brief Delete the given operator.
* \param op The operator to delete.
Expand Down Expand Up @@ -143,13 +145,15 @@ class MXNET_API Engine {
* \param prop Property of the function.
* \param priority Priority of the action, as hint to the engine.
* \param opr_name The operator name.
* \param attr_name The attribute name.
*/
virtual void PushAsync(AsyncFn exec_fun, Context exec_ctx,
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
int priority = 0,
const char* opr_name = nullptr) = 0;
const char* opr_name = nullptr,
const char* attr_name = nullptr) = 0;
/*!
* \brief Schedule the deletion of a variable.
*
Expand Down Expand Up @@ -199,18 +203,20 @@ class MXNET_API Engine {
* \param prop Property of the function.
* \param priority Priority of the action, as hint to the engine.
* \param opr_name The operator name.
* \param attr_name The attribute name.
* \tparam SyncFn the synchronous function to be pushed.
*/
inline void PushSync(SyncFn exec_fn, Context exec_ctx,
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
int priority = 0,
const char* opr_name = nullptr) {
const char* opr_name = nullptr,
const char* attr_name = nullptr) {
this->PushAsync([exec_fn](RunContext ctx, CallbackOnComplete on_complete) {
exec_fn(ctx);
on_complete();
}, exec_ctx, const_vars, mutable_vars, prop, priority, opr_name);
}, exec_ctx, const_vars, mutable_vars, prop, priority, opr_name, attr_name);
}

/*!
Expand Down
21 changes: 16 additions & 5 deletions src/engine/naive_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NaiveEngine final : public Engine {
std::vector<VarHandle> mutable_vars;
FnProperty prop;
const char* opr_name;
const char* attr_name;
/*! \brief indicate whether to profile this operator */
bool profiling{false};
/*! \brief operator execution statistics */
Expand Down Expand Up @@ -53,13 +54,15 @@ class NaiveEngine final : public Engine {
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
const char* opr_name = nullptr) override {
const char* opr_name = nullptr,
const char* attr_name = nullptr) override {
NaiveOpr *opr = new NaiveOpr();
opr->fn = fn;
opr->const_vars = const_vars;
opr->mutable_vars = mutable_vars;
opr->prop = prop;
opr->opr_name = opr_name;
opr->attr_name = attr_name;
return opr;
}

Expand All @@ -81,6 +84,9 @@ class NaiveEngine final : public Engine {
strncpy(opr->opr_stat->opr_name,
opr->opr_name,
sizeof(opr->opr_stat->opr_name) - 1);
strncpy(opr->opr_stat->attr_name,
opr->attr_name,
sizeof(opr->opr_stat->attr_name) - 1);
SetOprStart(opr->opr_stat);
}
opr->fn(ctx, on_complete);
Expand All @@ -96,7 +102,8 @@ class NaiveEngine final : public Engine {
opr->mutable_vars,
opr->prop,
priority,
PROFILER_MESSAGE(opr->opr_name));
PROFILER_MESSAGE(opr->opr_name),
PROFILER_MESSAGE(opr->attr_name));
}

void PushAsync(AsyncFn exec_fun,
Expand All @@ -105,7 +112,8 @@ class NaiveEngine final : public Engine {
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
int priority = 0,
const char* opr_name = nullptr) override {
const char* opr_name = nullptr,
const char* attr_name = nullptr) override {
CallbackOnComplete callback = CreateCallback(
NaiveEngine::OnComplete, nullptr);
this->req_completed_ = false;
Expand All @@ -114,17 +122,20 @@ class NaiveEngine final : public Engine {
NaiveOpr *opr = nullptr;
bool profiling = (profiler->GetState() == Profiler::kRunning) &&
(profiler->GetMode() == Profiler::kAllOperator) &&
opr_name;
opr_name && attr_name;
if (profiling) {
opr = NewOperator(exec_fun, const_vars, mutable_vars,
prop, opr_name)->Cast<NaiveOpr>();
prop, opr_name, attr_name)->Cast<NaiveOpr>();
opr->profiling = profiling;
opr->opr_stat = Profiler::Get()->AddOprStat(exec_ctx.dev_type, exec_ctx.dev_id);
uint64_t id = std::hash<std::thread::id>()(std::this_thread::get_id());
opr->opr_stat->thread_id = id;
strncpy(opr->opr_stat->opr_name,
opr->opr_name,
sizeof(opr->opr_stat->opr_name) - 1);
strncpy(opr->opr_stat->attr_name,
opr->attr_name,
sizeof(opr->opr_stat->attr_name) - 1);
SetOprStart(opr->opr_stat);
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions src/engine/profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ OprExecStat *Profiler::AddOprStat(int dev_type, uint32_t dev_id) {
opr_stat->dev_type = dev_type;
opr_stat->dev_id = dev_id;
opr_stat->opr_name[sizeof(opr_stat->opr_name)-1] = '\0';
opr_stat->attr_name[sizeof(opr_stat->attr_name)-1] = '\0';

int idx;
switch (dev_type) {
Expand Down Expand Up @@ -167,10 +168,10 @@ void Profiler::DumpProfile() {
file << ",";
}
file << std::endl;
this->EmitEvent(&file, opr_stat->opr_name, "category", "B",
this->EmitEvent(&file, opr_stat->attr_name, opr_stat->opr_name, "B",
opr_stat->opr_start_rel_micros, pid, tid);
file << ",\n";
this->EmitEvent(&file, opr_stat->opr_name, "category", "E",
this->EmitEvent(&file, opr_stat->attr_name, opr_stat->opr_name, "E",
opr_stat->opr_end_rel_micros, pid, tid);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/engine/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace engine {
struct OprExecStat {
/*! \brief operation name */
char opr_name[32];
/*! \brief layer name */
char attr_name[32];
/*!
* \brief operation execution start relative timestamp
* time unit is microsecond (10^-6 s)
Expand Down
11 changes: 7 additions & 4 deletions src/engine/threaded_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ ThreadedOpr* ThreadedEngine::NewOperator(
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop,
const char* opr_name) {
const char* opr_name,
const char* attr_name) {
auto ret = ThreadedOpr::New();
ret->opr_name = opr_name;
ret->attr_name = attr_name;
ret->fn = std::move(fn);
ret->prop = prop;
ret->const_vars.resize(const_vars.size());
Expand Down Expand Up @@ -285,8 +287,9 @@ void ThreadedEngine::PushAsync(AsyncFn fn, Context exec_ctx,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop,
int priority,
const char* opr_name) {
ThreadedOpr *opr = NewOperator(std::move(fn), const_vars, mutable_vars, prop, opr_name);
const char* opr_name,
const char* attr_name) {
ThreadedOpr *opr = NewOperator(std::move(fn), const_vars, mutable_vars, prop, opr_name, attr_name);
opr->temporary = true;
#if MXNET_USE_PROFILER
Profiler *profiler = Profiler::Get();
Expand Down Expand Up @@ -403,7 +406,7 @@ void ThreadedEngine::OnCompleteStatic(
OprBlock *opr_block = static_cast<OprBlock*>(opr_block_);
ThreadedOpr *threaded_opr = opr_block->opr;
#if MXNET_USE_PROFILER
if (opr_block->profiling && threaded_opr->opr_name) {
if (opr_block->profiling && threaded_opr->opr_name && threaded_opr->attr_name) {
// record operator end timestamp
SetOprEnd(opr_block->opr_stat);
}
Expand Down
13 changes: 10 additions & 3 deletions src/engine/threaded_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ struct ThreadedOpr final : public Opr,
FnProperty prop;
/*! \brief The name of the operator */
const char* opr_name{nullptr};
/*! \brief The name of the attribute */
const char* attr_name{nullptr};
/*!
* \brief Whether this is an temporary operator
* that can be deleted right after the operation completed.
Expand Down Expand Up @@ -243,15 +245,17 @@ class ThreadedEngine : public Engine {
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
const char* opr_name = nullptr) override;
const char* opr_name = nullptr,
const char* attr_name = nullptr) override;
void DeleteOperator(OprHandle op) override;
void Push(OprHandle op, Context exec_ctx, int priority = 0, bool profiling = false) override;
void PushAsync(AsyncFn exec_fun, Context exec_ctx,
std::vector<VarHandle> const& const_vars,
std::vector<VarHandle> const& mutable_vars,
FnProperty prop = FnProperty::kNormal,
int priority = 0,
const char* opr_name = nullptr) override;
const char* opr_name = nullptr,
const char* attr_name = nullptr) override;
void DeleteVariable(SyncFn delete_fn, Context exec_ctx, VarHandle var) override;
void WaitForVar(VarHandle var) override;
void WaitForAll() override;
Expand Down Expand Up @@ -294,14 +298,17 @@ class ThreadedEngine : public Engine {
void ExecuteOprBlock(RunContext run_ctx, OprBlock *opr_block) {
ThreadedOpr* threaded_opr = opr_block->opr;
#if MXNET_USE_PROFILER
if (opr_block->profiling && threaded_opr->opr_name) {
if (opr_block->profiling && threaded_opr->opr_name && threaded_opr->attr_name) {
const Context& ctx = opr_block->ctx;
opr_block->opr_stat = Profiler::Get()->AddOprStat(ctx.dev_type, ctx.dev_id);
uint64_t id = std::hash<std::thread::id>()(std::this_thread::get_id());
opr_block->opr_stat->thread_id = id;
strncpy(opr_block->opr_stat->opr_name,
threaded_opr->opr_name,
sizeof(opr_block->opr_stat->opr_name) - 1);
strncpy(opr_block->opr_stat->attr_name,
threaded_opr->attr_name,
sizeof(opr_block->opr_stat->attr_name) - 1);
// record operator start timestamp
SetOprStart(opr_block->opr_stat);
}
Expand Down
15 changes: 13 additions & 2 deletions src/executor/graph_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,10 @@ void GraphExecutor::InitCachedOps() {
if (inode.source->is_variable()) continue;
#if MXNET_USE_PROFILER
op_nodes_[nid].opr_name = inode.source->op()->name.c_str();
op_nodes_[nid].attr_name = inode.source->attrs.name.c_str();
#else
op_nodes_[nid].opr_name = nullptr;
op_nodes_[nid].attr_name = nullptr;
#endif
if (skip_plus_node.at(nid)) {
op_nodes_[nid].skip_exec_node = true; continue;
Expand Down Expand Up @@ -674,7 +676,8 @@ void GraphExecutor::InitCachedOps() {
// setup the vars
op_nodes_[nid].cached_opr = Engine::Get()->NewOperator(
exec_fun, use_vars, mutate_vars, FnProperty::kNormal,
PROFILER_MESSAGE(op_nodes_[nid].opr_name));
PROFILER_MESSAGE(op_nodes_[nid].opr_name),
PROFILER_MESSAGE(op_nodes_[nid].attr_name));
op_nodes_[nid].mutate_vars = mutate_vars;
op_nodes_[nid].use_vars = use_vars;
}
Expand Down Expand Up @@ -849,8 +852,10 @@ GraphExecutor::CachedSegOpr GraphExecutor::CreateCachedSegOpr(size_t topo_start,
}
#if MXNET_USE_PROFILER
std::string opr_names = "[";
std::string attr_names = "[";
#else
std::string opr_names = "Bulk Execution";
std::string attr_names = "Bulk Execution";
#endif

const auto& idx = graph_.indexed_graph();
Expand All @@ -875,6 +880,7 @@ GraphExecutor::CachedSegOpr GraphExecutor::CreateCachedSegOpr(size_t topo_start,
ret.exec_list.push_back(exec.get());
#if MXNET_USE_PROFILER
opr_names += inode.source->op()->name + ",";
attr_names += inode.source->attrs.name + ",";
#endif
}

Expand Down Expand Up @@ -902,14 +908,19 @@ GraphExecutor::CachedSegOpr GraphExecutor::CreateCachedSegOpr(size_t topo_start,
#if MXNET_USE_PROFILER
opr_names.pop_back();
opr_names += "]";
attr_names.pop_back();
attr_names += "]";
// the lifetime of `opr_names.c_str()` is same with opr_names
// you need to copy it out. (potential memory leak risk)
char *p_opr_name = new char[opr_names.size() + 1];
memcpy(p_opr_name, opr_names.c_str(), opr_names.size() + 1);
char *p_attr_name = new char[attr_names.size() + 1];
memcpy(p_attr_name, attr_names.c_str(), attr_names.size() + 1);
#endif
ret.opr = Engine::Get()->NewOperator(
exec_fun, use_vars, mutate_vars, FnProperty::kNormal,
PROFILER_MESSAGE(p_opr_name));
PROFILER_MESSAGE(p_opr_name),
PROFILER_MESSAGE(p_attr_name));
return ret;
}
} // namespace exec
Expand Down
2 changes: 2 additions & 0 deletions src/executor/graph_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class GraphExecutor : public Executor {
// The name of the operator
const char* opr_name;
// the context of the node
const char* attr_name;
// the context of the node
Context ctx;
// The executor
std::shared_ptr<OpExecutor> exec;
Expand Down
6 changes: 3 additions & 3 deletions src/operator/contrib/proposal-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2015 by Contributors
* \file proposal-inl.h
* \brief Proposal Operator
* \author Piotr Teterwak, Bing Xu, Jian Guo
* \author Piotr Teterwak, Bing Xu, Jian Guo, Pengfei Chen
*/
#ifndef MXNET_OPERATOR_CONTRIB_PROPOSAL_INL_H_
#define MXNET_OPERATOR_CONTRIB_PROPOSAL_INL_H_
Expand Down Expand Up @@ -186,9 +186,9 @@ class ProposalProp : public OperatorProperty {
SHAPE_ASSIGN_CHECK(*in_shape, proposal::kImInfo, im_info_shape);
out_shape->clear();
// output
out_shape->push_back(Shape2(param_.rpn_post_nms_top_n, 5));
out_shape->push_back(Shape3(dshape[0], param_.rpn_post_nms_top_n, 5));
// score
out_shape->push_back(Shape2(param_.rpn_post_nms_top_n, 1));
out_shape->push_back(Shape3(dshape[0], param_.rpn_post_nms_top_n, 1));
return true;
}

Expand Down
Loading

0 comments on commit 45801ef

Please sign in to comment.