From c4d000a990e12681b78f30b1c5a617e8d64c0c68 Mon Sep 17 00:00:00 2001 From: minqiyang Date: Thu, 2 Aug 2018 11:36:07 +0800 Subject: [PATCH] Make code more efficient --- paddle/fluid/framework/operator.cc | 63 +++++++++++------------------- paddle/fluid/framework/tensor.h | 2 +- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 922b1a548be..266b5749c5d 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -57,23 +57,18 @@ static DDim GetDims(const Scope& scope, const std::string& name, return DDim({-1}); } - if (var->IsInitialized()) { - if (var->IsType()) { - const LoDTensor& tensor = var->Get(); - if (tensor.IsInitialized()) { - return tensor.dims(); - } else { - return DDim({-1}); - } - } else if (var->IsType()) { - if (get_actual_dim) { - return var->Get().value().dims(); - } else { - return var->Get().GetCompleteDims(); - } - } else { + if (var->IsType()) { + const LoDTensor& tensor = var->Get(); + if (UNLIKELY(tensor.IsInitialized())) { return DDim({-1}); } + return tensor.dims(); + } else if (var->IsType()) { + if (get_actual_dim) { + return var->Get().value().dims(); + } else { + return var->Get().GetCompleteDims(); + } } else { return DDim({-1}); } @@ -85,20 +80,15 @@ static std::string GetDtype(const Scope& scope, const std::string& name) { return ""; } - if (var->IsInitialized()) { - if (var->IsType()) { - const LoDTensor& tensor = var->Get(); - if (tensor.IsInitialized()) { - return DataTypeToString(ToDataType(tensor.type())); - } else { - return ""; - } - } else if (var->IsType()) { - return DataTypeToString( - ToDataType(var->Get().value().type())); - } else { + if (var->IsType()) { + const LoDTensor& tensor = var->Get(); + if (UNLIKELY(!tensor.IsInitialized())) { return ""; } + return DataTypeToString(ToDataType(tensor.type())); + } else if (var->IsType()) { + return DataTypeToString( + ToDataType(var->Get().value().type())); } else { return ""; } @@ -110,10 +100,8 @@ static int GetRowSize(const Scope& scope, const std::string& name) { return -1; } - if (var->IsInitialized()) { - if (var->IsType()) { - return var->Get().rows().size(); - } + if (var->IsType()) { + return var->Get().rows().size(); } return -1; @@ -127,17 +115,12 @@ static LoD GetLoD(const Scope& scope, const std::string& name) { return default_lod; } - if (var->IsInitialized()) { - if (var->IsType()) { - const LoDTensor& tensor = var->Get(); - if (tensor.IsInitialized()) { - return tensor.lod(); - } else { - return default_lod; - } - } else { + if (var->IsType()) { + const LoDTensor& tensor = var->Get(); + if (UNLIKELY(!tensor.IsInitialized())) { return default_lod; } + return tensor.lod(); } else { return default_lod; } diff --git a/paddle/fluid/framework/tensor.h b/paddle/fluid/framework/tensor.h index ef224d68f1f..0bbfd66148e 100644 --- a/paddle/fluid/framework/tensor.h +++ b/paddle/fluid/framework/tensor.h @@ -82,7 +82,7 @@ class Tensor { template const T* data() const; - bool IsInitialized() const; + inline bool IsInitialized() const; /** * @brief Return a pointer to mutable memory block. -- GitLab