提交 c4d000a9 编写于 作者: M minqiyang

Make code more efficient

上级 9812bb8b
...@@ -57,14 +57,12 @@ static DDim GetDims(const Scope& scope, const std::string& name, ...@@ -57,14 +57,12 @@ static DDim GetDims(const Scope& scope, const std::string& name,
return DDim({-1}); return DDim({-1});
} }
if (var->IsInitialized()) {
if (var->IsType<LoDTensor>()) { if (var->IsType<LoDTensor>()) {
const LoDTensor& tensor = var->Get<LoDTensor>(); const LoDTensor& tensor = var->Get<LoDTensor>();
if (tensor.IsInitialized()) { if (UNLIKELY(tensor.IsInitialized())) {
return tensor.dims();
} else {
return DDim({-1}); return DDim({-1});
} }
return tensor.dims();
} else if (var->IsType<SelectedRows>()) { } else if (var->IsType<SelectedRows>()) {
if (get_actual_dim) { if (get_actual_dim) {
return var->Get<SelectedRows>().value().dims(); return var->Get<SelectedRows>().value().dims();
...@@ -74,9 +72,6 @@ static DDim GetDims(const Scope& scope, const std::string& name, ...@@ -74,9 +72,6 @@ static DDim GetDims(const Scope& scope, const std::string& name,
} else { } else {
return DDim({-1}); return DDim({-1});
} }
} else {
return DDim({-1});
}
} }
static std::string GetDtype(const Scope& scope, const std::string& name) { static std::string GetDtype(const Scope& scope, const std::string& name) {
...@@ -85,23 +80,18 @@ static std::string GetDtype(const Scope& scope, const std::string& name) { ...@@ -85,23 +80,18 @@ static std::string GetDtype(const Scope& scope, const std::string& name) {
return ""; return "";
} }
if (var->IsInitialized()) {
if (var->IsType<LoDTensor>()) { if (var->IsType<LoDTensor>()) {
const LoDTensor& tensor = var->Get<LoDTensor>(); const LoDTensor& tensor = var->Get<LoDTensor>();
if (tensor.IsInitialized()) { if (UNLIKELY(!tensor.IsInitialized())) {
return DataTypeToString(ToDataType(tensor.type()));
} else {
return ""; return "";
} }
return DataTypeToString(ToDataType(tensor.type()));
} else if (var->IsType<SelectedRows>()) { } else if (var->IsType<SelectedRows>()) {
return DataTypeToString( return DataTypeToString(
ToDataType(var->Get<SelectedRows>().value().type())); ToDataType(var->Get<SelectedRows>().value().type()));
} else { } else {
return ""; return "";
} }
} else {
return "";
}
} }
static int GetRowSize(const Scope& scope, const std::string& name) { static int GetRowSize(const Scope& scope, const std::string& name) {
...@@ -110,11 +100,9 @@ static int GetRowSize(const Scope& scope, const std::string& name) { ...@@ -110,11 +100,9 @@ static int GetRowSize(const Scope& scope, const std::string& name) {
return -1; return -1;
} }
if (var->IsInitialized()) {
if (var->IsType<SelectedRows>()) { if (var->IsType<SelectedRows>()) {
return var->Get<SelectedRows>().rows().size(); return var->Get<SelectedRows>().rows().size();
} }
}
return -1; return -1;
} }
...@@ -127,17 +115,12 @@ static LoD GetLoD(const Scope& scope, const std::string& name) { ...@@ -127,17 +115,12 @@ static LoD GetLoD(const Scope& scope, const std::string& name) {
return default_lod; return default_lod;
} }
if (var->IsInitialized()) {
if (var->IsType<LoDTensor>()) { if (var->IsType<LoDTensor>()) {
const LoDTensor& tensor = var->Get<LoDTensor>(); const LoDTensor& tensor = var->Get<LoDTensor>();
if (tensor.IsInitialized()) { if (UNLIKELY(!tensor.IsInitialized())) {
return tensor.lod();
} else {
return default_lod;
}
} else {
return default_lod; return default_lod;
} }
return tensor.lod();
} else { } else {
return default_lod; return default_lod;
} }
......
...@@ -82,7 +82,7 @@ class Tensor { ...@@ -82,7 +82,7 @@ class Tensor {
template <typename T> template <typename T>
const T* data() const; const T* data() const;
bool IsInitialized() const; inline bool IsInitialized() const;
/** /**
* @brief Return a pointer to mutable memory block. * @brief Return a pointer to mutable memory block.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册