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

Feature/try to remove parallel nn #1198

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion paddle/gserver/gradientmachines/GradientMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ GradientMachine* GradientMachine::create(
if (config.type() == "multi_nn") {
/* multi submodel calculate, thread(s) will be initialized inside */
nn = new MultiNetwork("root");
} else if (FLAGS_parallel_nn) {
} else if (config.cmd_args().parallel_nn()) {
/* multi threads calculate */
nn = new ParallelNeuralNetwork();
} else {
Expand Down
6 changes: 3 additions & 3 deletions paddle/gserver/gradientmachines/MultiGradientMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ TrainerThread::TrainerThread(const ModelConfig& config,
SetDevice gpuDevice(deviceId_);

NeuralNetwork* nn = nullptr;
if (!multiMachine->useGpu() || !FLAGS_parallel_nn) {
if (!multiMachine->useGpu() || !config.cmd_args().parallel_nn()) {
nn = NeuralNetwork::create(config);
} else {
nn = new ParallelNeuralNetwork();
Expand All @@ -422,7 +422,7 @@ TrainerThread::TrainerThread(const ModelConfig& config,
nn->init(config_, slaveParamInitCb);
gradientMachine_.reset(nn);
parameters_ = gradientMachine_->getParameters();
if (!FLAGS_parallel_nn) {
if (!config.cmd_args().parallel_nn()) {
for (auto& para : parameters_) {
para->setDevice(deviceId_);
}
Expand Down Expand Up @@ -744,7 +744,7 @@ void TrainerThread::copyInArgs() {
fullInArgs[i],
startSeq,
copySize,
FLAGS_parallel_nn ? false : multiMachine_->useGpu());
config_.cmd_args().parallel_nn() ? false : multiMachine_->useGpu());
}
}

Expand Down
2 changes: 1 addition & 1 deletion paddle/gserver/gradientmachines/MultiNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void MultiNetwork::init(const ModelConfig& config,
// sub networks
for (int i = 1; i < config.sub_models_size(); ++i) {
std::string subModelName = config.sub_models(i).name();
if (FLAGS_parallel_nn) {
if (config.cmd_args().parallel_nn()) {
subNetworks_[i - 1] = std::unique_ptr<ParallelNeuralNetwork>(
new ParallelNeuralNetwork(subModelName, this));
} else {
Expand Down
2 changes: 1 addition & 1 deletion paddle/gserver/gradientmachines/NeuralNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void NeuralNetwork::prefetch(const std::vector<Argument>& inArgs) {
}

for (size_t i = 0; i != dataLayers_.size(); ++i) {
if (FLAGS_parallel_nn) {
if (this->config_.cmd_args().parallel_nn()) {
const_cast<Argument&>(inArgs[i]).deviceId = -1;
}
dataLayers_[i]->setData(inArgs[i]);
Expand Down
2 changes: 1 addition & 1 deletion paddle/gserver/layers/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Layer::Layer(const LayerConfig& config, bool useGpu)
needSequenceInfo_(true) {}

bool Layer::init(const LayerMap& layerMap, const ParameterMap& parameterMap) {
if (useGpu_ && FLAGS_parallel_nn) {
if (useGpu_ && config_.cmd_args().parallel_nn()) {
/* gpu environment is specified by device property */
deviceId_ = config_.device();
if (deviceId_ < 0) {
Expand Down
2 changes: 1 addition & 1 deletion paddle/parameter/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Parameter::Parameter(const ParameterConfig& config, bool useGpu, bool doInit)
updateCounter_(0),
updated_(false) {
setID(-1); /* capture uninitialized id */
if (useGpu_ && FLAGS_parallel_nn) {
if (useGpu_ && config_.cmd_args().parallel_nn()) {
/* gpu environment is specified by device property */
deviceId_ = config_.device();
if (deviceId_ < 0) {
Expand Down
13 changes: 13 additions & 0 deletions paddle/trainer/TrainerConfigHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ bool TrainerConfigHelper::hasTestDataConfig() const {
return m->conf.has_test_data_config();
}

template <typename T>
static void updateCMDArgs(T *mutableConf) {
if (mutableConf->has_cmd_args()) {
mutableConf->set_allocated_cmd_args(new paddle::CMDArguments());
}
paddle::CMDArguments &args = *mutableConf->mutable_cmd_args();
args.set_parallel_nn(FLAGS_parallel_nn);
}

void TrainerConfigHelper::updateConfigFromFlags() {
if (!FLAGS_save_dir.empty()) {
m->conf.set_save_dir(FLAGS_save_dir);
Expand All @@ -111,6 +120,10 @@ void TrainerConfigHelper::updateConfigFromFlags() {
if (FLAGS_start_pass != 0) {
m->conf.set_start_pass(FLAGS_start_pass);
}
updateCMDArgs(m->conf.mutable_model_config());
for (auto paramConf : *m->conf.mutable_model_config()->mutable_parameters()) {
updateCMDArgs(&paramConf);
}
}

void TrainerConfigHelper::disableRemoteSparseUpdater() {
Expand Down
19 changes: 19 additions & 0 deletions proto/CMDArguments.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
syntax = "proto2";
Copy link
Contributor

@gangliao gangliao Jan 19, 2017

Choose a reason for hiding this comment

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

所以还是用proto2的语法?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这里主要是还要被其他的protobuf引用,所以还得用proto2的吧。

如果把Paddle内部的proto2语法都升级到3,是一个特别大的工程。(主要是proto3没有default value)了。

package paddle;

message CMDArguments {
Copy link
Collaborator

Choose a reason for hiding this comment

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

我们清理全局变量,本来是想把代码变得更简单。这个办法竟然需要增加一个protobuf定义,还要在各个activation function classes里增加一个class member。反而把代码变复杂了。

optional bool parallel_nn = 1 [default = false];
}
3 changes: 2 additions & 1 deletion proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ set(proto_filenames
ParameterConfig.proto
ParameterService.proto
TrainerConfig.proto
ParameterServerConfig.proto)
ParameterServerConfig.proto
CMDArguments.proto)

set(PROTO_GEN)
set(PROTO_GEN_PY)
Expand Down
7 changes: 7 additions & 0 deletions proto/ModelConfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License. */
syntax = "proto2";

import "ParameterConfig.proto";
import "CMDArguments.proto";

package paddle;

Expand Down Expand Up @@ -433,6 +434,9 @@ message LayerConfig {

// blank label used in ctc loss
optional uint32 blank = 52 [default = 0];

// To pass command line through Paddle codes.
optional CMDArguments cmd_args = 53;
}

message EvaluatorConfig {
Expand Down Expand Up @@ -554,4 +558,7 @@ message ModelConfig {
// For External Machine, defining how to split a neural network
// into multiple parts.
optional ExternalConfig external_config = 9;

// To pass command line through Paddle codes.
optional CMDArguments cmd_args = 10;
};
6 changes: 6 additions & 0 deletions proto/ParameterConfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ syntax = "proto2";

package paddle;


import "CMDArguments.proto";

/**
* Configuration structure for parameter
*/
Expand Down Expand Up @@ -77,4 +80,7 @@ message ParameterConfig {
optional bool is_shared = 23 [default = false];
// parameter block size
optional uint64 parameter_block_size = 24 [default = 0];

// To pass command line through Paddle codes.
optional CMDArguments cmd_args = 25;
}