提交 c4d000a9 编写于 作者: M minqiyang

Make code more efficient

上级 9812bb8b
...@@ -57,23 +57,18 @@ static DDim GetDims(const Scope& scope, const std::string& name, ...@@ -57,23 +57,18 @@ 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 (UNLIKELY(tensor.IsInitialized())) {
if (tensor.IsInitialized()) {
return tensor.dims();
} else {
return DDim({-1});
}
} else if (var->IsType<SelectedRows>()) {
if (get_actual_dim) {
return var->Get<SelectedRows>().value().dims();
} else {
return var->Get<SelectedRows>().GetCompleteDims();
}
} else {
return DDim({-1}); return DDim({-1});
} }
return tensor.dims();
} else if (var->IsType<SelectedRows>()) {
if (get_actual_dim) {
return var->Get<SelectedRows>().value().dims();
} else {
return var->Get<SelectedRows>().GetCompleteDims();
}
} else { } else {
return DDim({-1}); return DDim({-1});
} }
...@@ -85,20 +80,15 @@ static std::string GetDtype(const Scope& scope, const std::string& name) { ...@@ -85,20 +80,15 @@ 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 (UNLIKELY(!tensor.IsInitialized())) {
if (tensor.IsInitialized()) {
return DataTypeToString(ToDataType(tensor.type()));
} else {
return "";
}
} else if (var->IsType<SelectedRows>()) {
return DataTypeToString(
ToDataType(var->Get<SelectedRows>().value().type()));
} else {
return ""; return "";
} }
return DataTypeToString(ToDataType(tensor.type()));
} else if (var->IsType<SelectedRows>()) {
return DataTypeToString(
ToDataType(var->Get<SelectedRows>().value().type()));
} else { } else {
return ""; return "";
} }
...@@ -110,10 +100,8 @@ static int GetRowSize(const Scope& scope, const std::string& name) { ...@@ -110,10 +100,8 @@ 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 (UNLIKELY(!tensor.IsInitialized())) {
if (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.
先完成此消息的编辑!
想要评论请 注册