Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove flag use_mkldnn_wgt #3548

Merged
merged 5 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions paddle/gserver/layers/MKLDNNFcLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ bool MKLDNNFcLayer::init(const LayerMap& layerMap,
}

void MKLDNNFcLayer::convertWeightsFromPaddle() {
if (FLAGS_use_mkldnn_wgt) {
if (hasInitedWgt_) {
return;
}

if (hasInitedWgt_) {
// TODO(TJ): dst format should get from wgtVal_
int dstFmt = PARAM_FORMAT_MKLDNN_OI;
int srcFmt = weight_->getParameterPtr()->getHeaderFormat();
if (srcFmt == dstFmt) {
return;
}

Expand All @@ -78,6 +81,7 @@ void MKLDNNFcLayer::convertWeightsFromPaddle() {
MatrixPtr paddleWgtT;
paddleWgt->transpose(paddleWgtT, true);
weight_->getW()->copyFrom(*paddleWgtT);
weight_->getParameterPtr()->setHeaderFormat(dstFmt);
hasInitedWgt_ = true;
}

Expand Down
27 changes: 20 additions & 7 deletions paddle/gserver/tests/MKLDNNTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ void MKLDNNTester::run(const TestConfig& dnn,
log_ = log;
lvl_ = level;

// Firstly test FLAGS_use_mkldnn_wgt = false
FLAGS_use_mkldnn_wgt = false;
// reset and run once
// Firstly test mkldnn init from PARAM_FORMAT_ORIGINAL weight
reset(dnn, ref, batchSize);
randomWgtDatas();
clearWgtDiffs();
Expand All @@ -342,17 +340,32 @@ void MKLDNNTester::run(const TestConfig& dnn,
runOnce();
}

// Then test FLAGS_use_mkldnn_wgt = true
FLAGS_use_mkldnn_wgt = true;
// after run once the mkldnn weight has been stored in dnnlayer
if (parameters_[DNN].empty()) {
// has no paramters
return;
}

// After run some iters, the mkldnn weight has been stored in dnnLayer
// and we can also get the mkldnn weight paramter header format
// Weight param should always be index 0 (and bias index 1).
// TODO(TJ): should also considerate mean and var format when batchnorm ready
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. iters-iterations
  2. 349行header format后面加标点,parameter拼错
  3. 350行:Note that weight parameter(打全) XXX
  4. considerate->consider ,前者是形容词

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done

int dnnWgtFmt = parameters_[DNN][0]->getHeaderFormat();
int refWgtFmt = parameters_[REF][0]->getHeaderFormat();
if (dnnWgtFmt == refWgtFmt) {
// weight format are equal, so no need check more
return;
}

// then save the weights and restart again
vector<VectorPtr> dnnWgts, refWgts;
CHECK_EQ(parameters_[DNN].size(), parameters_[REF].size());
saveWgt(parameters_[DNN], dnnWgts);
saveWgt(parameters_[REF], refWgts);

// restart again with flag true
// restart again with dnn weight format
reset(dnn, ref, batchSize);
// TODO(TJ): should also considerate mean and var format when batchnorm ready
parameters_[DNN][0]->setHeaderFormat(dnnWgtFmt);

// restore wgt
restoreWgt(dnnWgts, parameters_[DNN]);
Expand Down
2 changes: 1 addition & 1 deletion paddle/gserver/tests/MKLDNNTester.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MKLDNNTester {
* if many(>failRate) wrong(abs(dnn-ref)/abs(ref)>thres) points return the
* max(diff/ref)
* else return sum(abs(a-b)) / sum(abs(b))
* The return value should smaller than eps when passing.
* The return value should be smaller than eps when passing.
*/
double getDelta(const real* d1,
const real* d2,
Expand Down
10 changes: 6 additions & 4 deletions paddle/parameter/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ Parameter::Parameter(const ParameterConfig& config, bool useGpu, bool doInit)
deviceId_(-1),
sharedCount_(0),
updateCounter_(0),
updated_(false) {
updated_(false),
headerFormat_(PARAM_FORMAT_ORIGINAL) {
setID(-1); /* capture uninitialized id */
if (useGpu_ && FLAGS_parallel_nn) {
/* gpu environment is specified by device property */
Expand Down Expand Up @@ -285,7 +286,7 @@ bool Parameter::save(const std::string& filename) const {
bool Parameter::save(std::ostream& s) const {
CpuVector vec(*bufs_[PARAMETER_VALUE].get());
Header header;
header.version = kFormatVersion;
header.format = headerFormat_;
header.valueSize = sizeof(real);
header.size = getSize();

Expand Down Expand Up @@ -344,8 +345,9 @@ bool Parameter::load(std::istream& s) {
Header header;
CHECK(s.read(reinterpret_cast<char*>(&header), sizeof(header)))
<< "Fail to read parameter " << getName();
CHECK_EQ(header.version, kFormatVersion) << "Incorrect format version: "
<< header.version;
CHECK(isHeaderFormatSupported(header.format)) << "Incorrect format version: "
<< header.format;
headerFormat_ = header.format;
CHECK_EQ(header.size, getSize())
<< "The size (" << header.size << ") in the file does not match the size "
<< "(" << getSize() << ") of the parameter: " << getName();
Expand Down
29 changes: 27 additions & 2 deletions paddle/parameter/Parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ limitations under the License. */

namespace paddle {

typedef enum {
PARAM_FORMAT_ORIGINAL = 0, // the paddle original basic format
PARAM_FORMAT_MKLDNN_OI, // the mkldnn format oi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oi是其中一种mkldnn的格式吧。这里能给个链接么,或者给个详细点的说明。
不然大家会认为mkldnn就一种格式,可以说明下有很多种格式,oi只是其中一种。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,done

PARAM_FORMAT_ITEMS, // the total format items numbers
} PARAM_FORMAT;

class SparsePrefetchRowCpuMatrix;

class Parameter;
Expand Down Expand Up @@ -242,14 +248,30 @@ class Parameter {
/// Initialize the value to 0
void zeroMem();

static const int kFormatVersion = 0;
/// file header structure
struct Header {
int32_t version; // = 0, file format version
int32_t format; // = PARAM_FORMAT
uint32_t valueSize; // = sizeof(real)
uint64_t size; // = getSize()
};

/**
* @brief Is the header supported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

259行注释不全,文件头支持什么?
另外结束时请加标点,下面几处brief的注释一样。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done.

*/
static bool isHeaderFormatSupported(int32_t fmt) {
return fmt < PARAM_FORMAT_ITEMS;
}

/**
* @brief Get the format in header
*/
int getHeaderFormat() { return headerFormat_; }

/**
* @brief Set the format in header
*/
void setHeaderFormat(int32_t fmt) { headerFormat_ = fmt; }

/**
* @brief Parameter Update Hook.
*
Expand Down Expand Up @@ -321,6 +343,9 @@ class Parameter {
bool updated_;
SparseFormat format_;

// The header format for saving or loading param
int32_t headerFormat_;

std::vector<std::shared_ptr<IParameterUpdaterHook>> updaterHooks_;

public:
Expand Down
7 changes: 4 additions & 3 deletions paddle/pserver/ParameterServer2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,8 @@ void ParameterServer2::loadValueVector(const LoadValueRequest& request,
Parameter::Header header;
CHECK(fs.read(reinterpret_cast<char*>(&header), sizeof(header)))
<< "Fail to read parameters in pserver";
CHECK_EQ(header.version, Parameter::kFormatVersion)
<< "Incorrect format version: " << header.version;
CHECK(Parameter::isHeaderFormatSupported(header.format))
<< "Incorrect format version: " << header.format;
CHECK_EQ(header.size, (size_t)size_)
<< "The size (" << header.size << ") in the file does not match the size "
<< "(" << size_ << ") of the pserver: " << serverId_;
Expand Down Expand Up @@ -1063,7 +1063,8 @@ void ParameterServer2::saveValueVector(const SaveValueRequest& request,
CpuVector& vec = vectors_[PARAMETER_APPLY] ? *vectors_[PARAMETER_APPLY]
: *vectors_[PARAMETER_VALUE];
Parameter::Header header;
header.version = Parameter::kFormatVersion;
// TODO(TJ): save param headerFormat_
header.format = PARAM_FORMAT_ORIGINAL;
header.valueSize = sizeof(real);
header.size = size_;

Expand Down
2 changes: 0 additions & 2 deletions paddle/trainer/TrainerConfigHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ DECLARE_bool(with_gpu);
DECLARE_bool(parallel_nn);
DECLARE_string(config_args);
DECLARE_bool(use_mkldnn);
DECLARE_bool(use_mkldnn_wgt);

const char *kConfigParserModuleName = "paddle.trainer.config_parser";
const char *kConfigParserFuncName = "parse_config_and_serialize";
Expand All @@ -47,7 +46,6 @@ TrainerConfigHelper::TrainerConfigHelper(const std::string &configFilePath)
<< ",with_cost=" << FLAGS_with_cost << ",use_gpu=" << FLAGS_use_gpu
<< ",parallel_nn=" << FLAGS_parallel_nn
<< ",use_mkldnn=" << FLAGS_use_mkldnn
<< ",use_mkldnn_wgt=" << FLAGS_use_mkldnn_wgt
<< ",cudnn_version=" << hl_get_cudnn_lib_version();
if (!FLAGS_config_args.empty()) {
configArgs << "," << FLAGS_config_args;
Expand Down
1 change: 0 additions & 1 deletion paddle/utils/Flags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ DEFINE_bool(use_mkldnn, false, "Default still keep use CPU training");
DEFINE_bool(use_mkldnn, false, "Only support CPU training");
#endif

DEFINE_bool(use_mkldnn_wgt, false, "Init weight from CPU weight");
DEFINE_bool(parallel_nn,
false,
"Whether to use multi-threads to calculate one neural network."
Expand Down
1 change: 0 additions & 1 deletion paddle/utils/Flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ DECLARE_string(predict_file);
DECLARE_bool(prev_batch_state);
DECLARE_string(init_model_path);
DECLARE_bool(use_mkldnn);
DECLARE_bool(use_mkldnn_wgt);