Skip to content

Commit

Permalink
cherry-pick bugs fix in release/v2.7, test=develop (#4726)
Browse files Browse the repository at this point in the history
* fix the memory leak of scope, test=develop

* fix tensor persistable bugs, test=develop (#4656)

* check nullptr in TargetMalloc and flatbuffers, test=develop (#4671)
  • Loading branch information
Shixiaowei02 committed Nov 11, 2020
1 parent d13c94d commit 47982d8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
4 changes: 1 addition & 3 deletions lite/backends/host/target_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const int MALLOC_ALIGN = 64;
void* TargetWrapper<TARGET(kHost)>::Malloc(size_t size) {
size_t offset = sizeof(void*) + MALLOC_ALIGN - 1;
char* p = static_cast<char*>(malloc(offset + size));
if (!p) {
return nullptr;
}
CHECK(p);
void* r = reinterpret_cast<void*>(reinterpret_cast<size_t>(p + offset) &
(~(MALLOC_ALIGN - 1)));
static_cast<void**>(r)[-1] = p;
Expand Down
12 changes: 8 additions & 4 deletions lite/core/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
// limitations under the License.

#include "lite/core/scope.h"
#define SCOPE_KIDS_READER_LOCK lite::fluid::AutoRDLock auto_lock(kids_lock_);
#define SCOPE_KIDS_WRITER_LOCK lite::fluid::AutoWRLock auto_lock(kids_lock_);
#define SCOPE_VARS_READER_LOCK lite::fluid::AutoRDLock auto_lock(vars_lock_);
#define SCOPE_VARS_WRITER_LOCK lite::fluid::AutoWRLock auto_lock(vars_lock_);
#define SCOPE_KIDS_READER_LOCK \
lite::fluid::AutoRDLock auto_lock(kids_lock_.get());
#define SCOPE_KIDS_WRITER_LOCK \
lite::fluid::AutoWRLock auto_lock(kids_lock_.get());
#define SCOPE_VARS_READER_LOCK \
lite::fluid::AutoRDLock auto_lock(vars_lock_.get());
#define SCOPE_VARS_WRITER_LOCK \
lite::fluid::AutoWRLock auto_lock(vars_lock_.get());

namespace paddle {
namespace lite {
Expand Down
13 changes: 6 additions & 7 deletions lite/core/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ namespace lite {

class Scope final {
public:
Scope() {
kids_lock_ = new lite::fluid::RWLock;
vars_lock_ = new lite::fluid::RWLock;
rwlock_.reset(new lite::fluid::RWLock);
}
Scope()
: kids_lock_{new lite::fluid::RWLock},
vars_lock_{new lite::fluid::RWLock},
rwlock_{new lite::fluid::RWLock} {}
// delete below two functions to allow pybind to recognise it cannot make a
// copy
// link:
Expand Down Expand Up @@ -99,8 +98,8 @@ class Scope final {
mutable std::list<Scope*> kids_;
const Scope* parent_{nullptr};
std::map<std::string, std::unique_ptr<Variable>> vars_;
lite::fluid::RWLock* kids_lock_{nullptr};
lite::fluid::RWLock* vars_lock_{nullptr};
std::unique_ptr<lite::fluid::RWLock> kids_lock_{nullptr};
std::unique_ptr<lite::fluid::RWLock> vars_lock_{nullptr};
std::unique_ptr<lite::fluid::RWLock> rwlock_{nullptr};
};

Expand Down
2 changes: 2 additions & 0 deletions lite/core/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ TensorLite TensorLite::Slice(int64_t begin, int64_t end) const {
template <typename TensorT>
bool TensorCompareWith(const TensorT &a, const TensorT &b) {
if (a.dims() != b.dims()) return false;
if (a.precision() != b.precision()) return false;
if (a.persistable() != b.persistable()) return false;
if (memcmp(a.raw_data(), b.raw_data(), a.data_size()) != 0) return false;
return true;
}
Expand Down
17 changes: 11 additions & 6 deletions lite/model_parser/flatbuffers/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ void SetParamWithTensor(const std::string& name,
}

void SetTensorWithParam(lite::Tensor* tensor, const ParamDescReadAPI& param) {
CHECK(tensor);
tensor->Resize(param.Dim());
tensor->set_precision(lite::ConvertPrecisionType(param.GetDataType()));
std::memcpy(tensor->mutable_data(param.byte_size()),
param.GetData(),
param.byte_size());
auto* dst = tensor->mutable_data(param.byte_size());
CHECK(dst);
CHECK(param.GetData());
std::memcpy(dst, param.GetData(), param.byte_size());
}

void SetCombinedParamsWithScope(const lite::Scope& scope,
Expand All @@ -82,9 +84,12 @@ void SetScopeWithCombinedParams(lite::Scope* scope,
const CombinedParamsDescReadAPI& params) {
CHECK(scope);
for (size_t i = 0; i < params.GetParamsSize(); ++i) {
const auto& param = *params.GetParamDesc(i);
auto* tensor = scope->Var(param.Name())->GetMutable<lite::Tensor>();
SetTensorWithParam(tensor, param);
const auto* param = params.GetParamDesc(i);
CHECK(param);
auto* tensor = scope->Var(param->Name())->GetMutable<lite::Tensor>();
CHECK(tensor);
SetTensorWithParam(tensor, *param);
tensor->set_persistable(true);
}
}

Expand Down
1 change: 1 addition & 0 deletions lite/model_parser/flatbuffers/io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void set_tensor(paddle::lite::Tensor* tensor,
auto production =
std::accumulate(begin(dims), end(dims), 1, std::multiplies<int64_t>());
tensor->Resize(dims);
tensor->set_persistable(true);
std::vector<T> data;
data.resize(production);
for (int i = 0; i < production; ++i) {
Expand Down
12 changes: 10 additions & 2 deletions lite/model_parser/flatbuffers/param_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ class ParamDescView : public ParamDescReadAPI {
CHECK(desc_->variable_type() ==
proto::ParamDesc_::VariableDesc_LoDTensorDesc);
tensor_desc_ = desc_->variable_as<proto::ParamDesc_::LoDTensorDesc>();
CHECK(tensor_desc_);
CHECK(tensor_desc_->data());
}
std::string Name() const override {
CHECK(desc_->name());
return desc_->name()->c_str();
}
std::string Name() const override { return desc_->name()->c_str(); }

std::vector<int64_t> Dim() const override {
const auto& dims = tensor_desc_->dim();
const auto* dims = tensor_desc_->dim();
CHECK(dims);
std::vector<int64_t> dims_vec;
dims_vec.resize(dims->size());
for (size_t i = 0; i < dims->size(); ++i) {
Expand Down Expand Up @@ -86,6 +92,8 @@ class CombinedParamsDescView : public CombinedParamsDescReadAPI {

void InitParams() {
desc_ = proto::GetCombinedParamsDesc(buf_.data());
CHECK(desc_);
CHECK(desc_->params());
size_t params_size = desc_->params()->size();
params_.resize(params_size);
for (size_t idx = 0; idx < params_size; ++idx) {
Expand Down

0 comments on commit 47982d8

Please sign in to comment.