Enhance the op benchmark: (#16066)

- Support setting attr in config
- Support setting dtype and initializer for input in config
test=develop
align_pyramid
Yiqun Liu 6 years ago committed by GitHub
parent 9be825a982
commit 36e2d3241e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because it is too large Load Diff

@ -14,7 +14,9 @@ limitations under the License. */
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
#include "paddle/fluid/framework/ddim.h" #include "paddle/fluid/framework/ddim.h"
#include "paddle/fluid/framework/op_desc.h" #include "paddle/fluid/framework/op_desc.h"
@ -39,16 +41,21 @@ class OpTester {
private: private:
std::vector<std::string> GetOpProtoInputNames(); std::vector<std::string> GetOpProtoInputNames();
std::vector<std::string> GetOpProtoOutputNames(); std::vector<std::string> GetOpProtoOutputNames();
std::unordered_map<std::string, framework::proto::AttrType>
GetOpProtoAttrNames();
framework::proto::VarType::Type TransToVarType(std::string str);
void CreateInputVarDesc(); void CreateInputVarDesc();
void CreateOutputVarDesc(); void CreateOutputVarDesc();
void CreateOpDesc();
framework::VarDesc *Var(const std::string &name); framework::VarDesc *Var(const std::string &name);
void CreateVariables(framework::Scope *scope); void CreateVariables(framework::Scope *scope);
template <typename T> template <typename T>
void SetupTensor(framework::LoDTensor *input, void SetupTensor(framework::LoDTensor *input,
const std::vector<int64_t> &shape, T lower, T upper); const std::vector<int64_t> &shape, T lower, T upper,
const std::string &initializer);
void RunImpl(); void RunImpl();
@ -57,7 +64,7 @@ class OpTester {
std::string type_; std::string type_;
framework::OpDesc op_desc_; framework::OpDesc op_desc_;
std::unordered_map<std::string, std::unique_ptr<framework::VarDesc>> vars_; std::unordered_map<std::string, std::unique_ptr<framework::VarDesc>> vars_;
std::unordered_map<std::string, std::vector<std::vector<size_t>>> input_lods_; std::unordered_map<std::string, OpInputConfig> inputs_;
std::unique_ptr<framework::OperatorBase> op_; std::unique_ptr<framework::OperatorBase> op_;
platform::Place place_; platform::Place place_;
std::unique_ptr<framework::Scope> scope_; std::unique_ptr<framework::Scope> scope_;

@ -14,7 +14,6 @@ limitations under the License. */
#include "paddle/fluid/operators/benchmark/op_tester_config.h" #include "paddle/fluid/operators/benchmark/op_tester_config.h"
#include <fstream> #include <fstream>
#include "glog/logging.h"
#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/enforce.h"
namespace paddle { namespace paddle {
@ -40,6 +39,62 @@ static void EraseEndSep(std::string* str,
} }
} }
OpInputConfig::OpInputConfig(std::istream& is) {
std::string sep;
is >> sep;
if (sep == kStartSeparator) {
while (sep != kEndSeparator) {
is >> sep;
if (sep == "name" || sep == "name:") {
is >> name;
EraseEndSep(&name);
} else if (sep == "dtype" || sep == "dtype:") {
ParseDType(is);
} else if (sep == "initializer" || sep == "initializer:") {
ParseInitializer(is);
} else if (sep == "dims" || sep == "dims:") {
ParseDims(is);
} else if (sep == "lod" || sep == "lod:") {
ParseLoD(is);
}
}
}
}
void OpInputConfig::ParseDType(std::istream& is) {
std::string dtype_str;
is >> dtype_str;
EraseEndSep(&dtype_str);
if (dtype_str == "int32" || dtype_str == "int") {
dtype = "int32";
} else if (dtype_str == "int64" || dtype_str == "long") {
dtype = "int64";
} else if (dtype_str == "fp32" || dtype_str == "float") {
dtype = "fp32";
} else if (dtype_str == "fp64" || dtype_str == "double") {
dtype = "fp64";
} else {
PADDLE_THROW("Unsupported dtype %s", dtype_str.c_str());
}
VLOG(4) << "dtype of input " << name << " is: " << dtype;
}
void OpInputConfig::ParseInitializer(std::istream& is) {
std::string initializer_str;
is >> initializer_str;
EraseEndSep(&initializer_str);
const std::vector<std::string> supported_initializers = {"random", "natural",
"zeros"};
if (!Has(supported_initializers, initializer_str)) {
PADDLE_THROW("Unsupported initializer %s", initializer_str.c_str());
}
initializer = initializer_str;
VLOG(4) << "initializer of input " << name << " is: " << initializer;
}
void OpInputConfig::ParseDims(std::istream& is) { void OpInputConfig::ParseDims(std::istream& is) {
std::string dims_str; std::string dims_str;
is >> dims_str; is >> dims_str;
@ -84,7 +139,7 @@ void OpInputConfig::ParseLoD(std::istream& is) {
number += lod_str[i]; number += lod_str[i];
++i; ++i;
} }
level.push_back(atoi(number.c_str())); level.push_back(StringTo<size_t>(number));
} }
lod.push_back(level); lod.push_back(level);
} else if (lod_str[i] == '}') { } else if (lod_str[i] == '}') {
@ -93,24 +148,6 @@ void OpInputConfig::ParseLoD(std::istream& is) {
} }
} }
OpInputConfig::OpInputConfig(std::istream& is) {
std::string sep;
is >> sep;
if (sep == kStartSeparator) {
while (sep != kEndSeparator) {
is >> sep;
if (sep == "name" || sep == "name:") {
is >> name;
EraseEndSep(&name);
} else if (sep == "dims" || sep == "dims:") {
ParseDims(is);
} else if (sep == "lod" || sep == "lod:") {
ParseLoD(is);
}
}
}
}
OpTesterConfig::OpTesterConfig(const std::string& filename) { OpTesterConfig::OpTesterConfig(const std::string& filename) {
std::ifstream fin(filename, std::ios::in | std::ios::binary); std::ifstream fin(filename, std::ios::in | std::ios::binary);
PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot open file %s", PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot open file %s",
@ -167,6 +204,7 @@ bool OpTesterConfig::ParseAttrs(std::istream& is) {
is >> value; is >> value;
EraseEndSep(&key, ":"); EraseEndSep(&key, ":");
EraseEndSep(&value); EraseEndSep(&value);
VLOG(4) << "attrs: " << key << ", " << value;
attrs[key] = value; attrs[key] = value;
} }

@ -15,6 +15,7 @@ limitations under the License. */
#pragma once #pragma once
#include <istream> #include <istream>
#include <sstream>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -27,10 +28,14 @@ struct OpInputConfig {
OpInputConfig() {} OpInputConfig() {}
explicit OpInputConfig(std::istream& is); explicit OpInputConfig(std::istream& is);
void ParseDType(std::istream& is);
void ParseInitializer(std::istream& is);
void ParseDims(std::istream& is); void ParseDims(std::istream& is);
void ParseLoD(std::istream& is); void ParseLoD(std::istream& is);
std::string name; std::string name;
std::string dtype{"fp32"}; // int32/int, int64/long, fp32/float, fp64/double
std::string initializer{"random"}; // random, natural
std::vector<int64_t> dims; std::vector<int64_t> dims;
std::vector<std::vector<size_t>> lod; std::vector<std::vector<size_t>> lod;
}; };
@ -55,6 +60,23 @@ struct OpTesterConfig {
double runtime{0.0}; double runtime{0.0};
}; };
static bool Has(const std::vector<std::string>& vec, const std::string& item) {
for (size_t i = 0; i < vec.size(); ++i) {
if (vec[i] == item) {
return true;
}
}
return false;
}
template <typename T>
T StringTo(const std::string& str) {
std::istringstream is(str);
T value;
is >> value;
return value;
}
} // namespace benchmark } // namespace benchmark
} // namespace operators } // namespace operators
} // namespace paddle } // namespace paddle

Loading…
Cancel
Save