From 0381a4d62866de9e40a9d2cd9f37157dbc8eccf7 Mon Sep 17 00:00:00 2001 From: liuqi Date: Mon, 25 Mar 2019 11:21:25 +0800 Subject: [PATCH] BUG: fix memory leak in SerialNet(96 bytes) --- .gitlab-ci.yml | 2 +- mace/core/device.cc | 9 +++++---- mace/core/net.cc | 16 +++++++++------- mace/core/net.h | 2 +- tools/device.py | 1 + tools/sh_commands.py | 4 ++-- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 11bc1c2e..a574449d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -144,7 +144,7 @@ model_tests: - CONF_FILE=mace-models/mobilenet-v2/mobilenet-v2-host.yml - > python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=file --model_data_format=file || exit 1; - python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1; + python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=1 --validate --model_graph_format=file --model_data_format=file --address_sanitizer || exit 1; python tools/converter.py run --config=${CONF_FILE} --target_socs=$TARGET_SOCS --example --round=1 --validate --model_graph_format=file --model_data_format=file || exit 1; python tools/converter.py benchmark --config=${CONF_FILE} --target_socs=$TARGET_SOCS --round=5 --model_graph_format=file --model_data_format=file || exit 1; python tools/converter.py convert --config=${CONF_FILE} --target_socs=$TARGET_SOCS --model_graph_format=code --model_data_format=file || exit 1; diff --git a/mace/core/device.cc b/mace/core/device.cc index 177b443b..535b7193 100644 --- a/mace/core/device.cc +++ b/mace/core/device.cc @@ -15,16 +15,17 @@ #include "mace/core/device.h" #include "mace/core/buffer.h" +#include "mace/utils/memory.h" namespace mace { CPUDevice::CPUDevice(const int num_threads, const CPUAffinityPolicy policy, const bool use_gemmlowp) - : cpu_runtime_(new CPURuntime(num_threads, - policy, - use_gemmlowp)), - scratch_buffer_(new ScratchBuffer(GetCPUAllocator())) {} + : cpu_runtime_(make_unique(num_threads, + policy, + use_gemmlowp)), + scratch_buffer_(make_unique(GetCPUAllocator())) {} CPUDevice::~CPUDevice() = default; diff --git a/mace/core/net.cc b/mace/core/net.cc index 365bb353..cf994078 100644 --- a/mace/core/net.cc +++ b/mace/core/net.cc @@ -28,6 +28,7 @@ #include "mace/utils/logging.h" #include "mace/utils/macros.h" #include "mace/utils/math.h" +#include "mace/utils/memory.h" #include "mace/utils/timer.h" #ifdef MACE_ENABLE_OPENCL @@ -77,7 +78,7 @@ std::unique_ptr SerialNet::CreateOperation( // Create the Operation DeviceType target_device_type = target_device_->device_type(); DeviceType device_type = DeviceType::CPU; - construct_context->set_device(cpu_device_); + construct_context->set_device(cpu_device_.get()); construct_context->set_operator_def(op_def); construct_context->set_output_mem_type(MemoryType::CPU_BUFFER); // Get available devices @@ -129,9 +130,10 @@ SerialNet::SerialNet(const OpRegistryBase *op_registry, ws_(ws), target_device_(target_device), cpu_device_( - new CPUDevice(target_device->cpu_runtime()->num_threads(), - target_device->cpu_runtime()->policy(), - target_device->cpu_runtime()->use_gemmlowp())) { + make_unique( + target_device->cpu_runtime()->num_threads(), + target_device->cpu_runtime()->policy(), + target_device->cpu_runtime()->use_gemmlowp())) { MACE_LATENCY_LOGGER(1, "Constructing SerialNet"); // output tensor : related information std::unordered_map output_map; @@ -380,7 +382,7 @@ MaceStatus SerialNet::Init() { if (device_type == target_device_->device_type()) { init_context.set_device(target_device_); } else { - init_context.set_device(cpu_device_); + init_context.set_device(cpu_device_.get()); } // Initialize the operation MACE_RETURN_IF_ERROR(op->Init(&init_context)); @@ -391,7 +393,7 @@ MaceStatus SerialNet::Init() { MaceStatus SerialNet::Run(RunMetadata *run_metadata) { MACE_MEMORY_LOGGING_GUARD(); MACE_LATENCY_LOGGER(1, "Running net"); - OpContext context(ws_, cpu_device_); + OpContext context(ws_, cpu_device_.get()); for (auto iter = operators_.begin(); iter != operators_.end(); ++iter) { auto &op = *iter; DeviceType device_type = op->device_type(); @@ -404,7 +406,7 @@ MaceStatus SerialNet::Run(RunMetadata *run_metadata) { if (device_type == target_device_->device_type()) { context.set_device(target_device_); } else { - context.set_device(cpu_device_); + context.set_device(cpu_device_.get()); } CallStats call_stats; diff --git a/mace/core/net.h b/mace/core/net.h index cafd1a7f..788eb611 100644 --- a/mace/core/net.h +++ b/mace/core/net.h @@ -66,7 +66,7 @@ class SerialNet : public NetBase { Workspace *ws_; Device *target_device_; // CPU is base device. - Device *cpu_device_; + std::unique_ptr cpu_device_; std::vector > operators_; MACE_DISABLE_COPY_AND_ASSIGN(SerialNet); diff --git a/tools/device.py b/tools/device.py index 42936a9b..0ff868f4 100644 --- a/tools/device.py +++ b/tools/device.py @@ -208,6 +208,7 @@ class DeviceWrapper: p = subprocess.Popen( [ "env", + "ASAN_OPTIONS=detect_leaks=1", "LD_LIBRARY_PATH=%s" % libmace_dynamic_lib_path, "MACE_CPP_MIN_VLOG_LEVEL=%s" % vlog_level, "MACE_RUNTIME_FAILURE_RATIO=%f" % runtime_failure_ratio, diff --git a/tools/sh_commands.py b/tools/sh_commands.py index ac00dc66..aa46a8ac 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -304,8 +304,8 @@ def bazel_build(target, bazel_args += ("--config", "asan") else: bazel_args += ("--config", "optimization") - if symbol_hidden: - bazel_args += ("--config", "symbol_hidden") + if symbol_hidden: + bazel_args += ("--config", "symbol_hidden") if extra_args: bazel_args += (extra_args,) six.print_(bazel_args) -- GitLab