Skip to content

Commit f8e8d1a

Browse files
authored
Merge pull request #846 from reyoung/feature/remove_m4
Remove m4 when generate protobuf
2 parents b993585 + f62f518 commit f8e8d1a

11 files changed

+89
-102
lines changed

paddle/gserver/layers/MultinomialSampler.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ limitations under the License. */
1414

1515
#pragma once
1616

17+
#include <memory>
1718
#include <random>
18-
1919
#include "paddle/utils/TypeDefs.h"
2020

2121
namespace paddle {
@@ -32,6 +32,17 @@ class MultinomialSampler {
3232
public:
3333
MultinomialSampler(const real* prob, int size);
3434

35+
//! protobuf always using double.
36+
static MultinomialSampler* create(const double* prob, int size) {
37+
#ifdef PADDLE_TYPE_DOUBLE
38+
return new MultinomialSampler(prob, size);
39+
#else
40+
std::unique_ptr<real[]> tmp(new real[size]);
41+
std::copy(prob, prob + size, tmp.get());
42+
return new MultinomialSampler(tmp.get(), size);
43+
#endif
44+
}
45+
3546
/**
3647
* @brief Generate a random sample.
3748
* @param g is a random number engine. See <random>.

paddle/gserver/layers/NCELayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ class NCELayer : public Layer {
9999

100100
if (config_.neg_sampling_dist_size()) {
101101
CHECK_EQ(numClasses_, config_.neg_sampling_dist_size());
102-
sampler_.reset(new MultinomialSampler(config_.neg_sampling_dist().data(),
103-
numClasses_));
102+
sampler_.reset(MultinomialSampler::create(
103+
config_.neg_sampling_dist().data(), numClasses_));
104104
}
105105

106106
return true;

paddle/gserver/layers/WarpCTCLayer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ bool WarpCTCLayer::init(const LayerMap& layerMap,
3131
CHECK_EQ(numClasses_, inputLayers_[0]->getSize());
3232

3333
blank_ = config_.blank();
34-
CHECK_GE(blank_, 0UL);
3534
CHECK_LT(blank_, numClasses_);
3635

3736
normByTimes_ = config_.norm_by_times();

paddle/pserver/ParameterClient2.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,17 @@ P_DEFINE_int32(parallel_thread_num, 1, "Thread number for parameter send");
2525

2626
namespace paddle {
2727

28-
template <class T>
29-
void copyToRepeatedField(google::protobuf::RepeatedField<T>* dest,
30-
const T* src,
28+
template <typename T1, typename T2>
29+
void copyToRepeatedField(google::protobuf::RepeatedField<T1>* dest,
30+
const T2* src,
3131
size_t size) {
3232
dest->Clear();
3333
dest->Reserve(size);
34-
3534
for (size_t i = 0; i < size; ++i) {
3635
dest->AddAlreadyReserved(src[i]);
3736
}
3837
}
3938

40-
template <class T>
41-
void copyToRepeatedField(const std::vector<T>& src,
42-
google::protobuf::RepeatedField<T>* dest) {
43-
copyToRepeatedField(dest, &src[0], src.size());
44-
}
45-
4639
ParameterClient2::ParameterClient2(bool separate, int port, int numPorts)
4740
: BaseClient(separate, numPorts), port_(port) {
4841
#ifndef PADDLE_DISABLE_TIMER
@@ -618,6 +611,8 @@ void PreparedOperations::addOperationHelper(Operation* op, CpuMatrixPtr mat) {
618611
pmat.mutable_values(), mat->getData(), pmat.num_cols() * pmat.num_rows());
619612
}
620613

614+
static inline real addTwo(real a, double b) { return a + b; }
615+
621616
void ParameterClient2::doOperation(PreparedOperations& ops,
622617
bool waitForGradient,
623618
bool sendBackGradient,
@@ -682,8 +677,11 @@ void ParameterClient2::doOperation(PreparedOperations& ops,
682677
CpuVectorPtr rvec = resultVectors[i];
683678
if (!rvec) continue;
684679
CHECK_EQ(rvec->getSize(), (size_t)vec.dim());
685-
CpuVector avec(rvec->getSize(), const_cast<real*>(vec.values().data()));
686-
rvec->add(avec);
680+
std::transform(rvec->getData(),
681+
rvec->getData() + rvec->getSize(),
682+
vec.values().data(),
683+
rvec->getData(),
684+
addTwo);
687685
}
688686

689687
CHECK_EQ(resultMatrices.size(), (size_t)result.matrices_size());
@@ -693,11 +691,12 @@ void ParameterClient2::doOperation(PreparedOperations& ops,
693691
if (!rmat) continue;
694692
CHECK_EQ(rmat->getHeight(), (size_t)mat.num_rows());
695693
CHECK_EQ(rmat->getWidth(), (size_t)mat.num_cols());
696-
CpuMatrixPtr amat =
697-
std::make_shared<CpuMatrix>(const_cast<real*>(mat.values().data()),
698-
rmat->getHeight(),
699-
rmat->getWidth());
700-
rmat->add(*amat);
694+
695+
std::transform(rmat->getData(),
696+
rmat->getData() + rmat->getElementCnt(),
697+
mat.values().data(),
698+
rmat->getData(),
699+
addTwo);
701700
}
702701
}
703702
}

proto/CMakeLists.txt

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@ set(proto_filenames
66
ParameterService.proto
77
TrainerConfig.proto)
88

9-
set(real_proto_files)
10-
11-
# TODO(yuyang18): Some internal proto will also be depended on.
12-
# Find a way to automatically calculate all depends.
13-
foreach(filename ${proto_filenames})
14-
set(PROTOBUF_3_FLAGS "")
15-
if (PROTOBUF_3)
16-
set(PROTOBUF_3_FLAGS "-Dproto3")
17-
endif()
18-
add_custom_command(OUTPUT ${filename}
19-
COMMAND ${M4_EXECUTABLE} -Dreal=${ACCURACY} ${PROTOBUF_3_FLAGS} -I '${INTERNAL_PROTO_PATH}'
20-
${PROJ_ROOT}/proto/${filename}.m4 > ${filename}
21-
DEPENDS ${PROJ_ROOT}/proto/${filename}.m4
22-
COMMENT "Generate ${filename}")
23-
endforeach()
24-
25-
add_custom_target(proto_accuracy ALL
26-
DEPENDS ${proto_filenames})
27-
289
set(PROTO_GEN)
2910
set(PROTO_GEN_PY)
3011

@@ -39,9 +20,8 @@ foreach(filename ${proto_filenames})
3920
add_custom_command(OUTPUT ${CUR_PROTO_GEN}
4021
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
4122
--cpp_out ${CMAKE_CURRENT_BINARY_DIR}
42-
--proto_path ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${filename}
43-
DEPENDS proto_accuracy
44-
${PROJ_ROOT}/proto/${filename}.m4)
23+
--proto_path ${PROJ_ROOT}/proto ${PROJ_ROOT}/proto/${filename}
24+
DEPENDS ${filename})
4525

4626
set(CUR_PROTO_GEN_PY
4727
${PROJ_ROOT}/paddle/python/paddle/proto/${base_filename}_pb2.py)
@@ -50,9 +30,8 @@ foreach(filename ${proto_filenames})
5030
${PROTO_GEN_PY})
5131
add_custom_command(OUTPUT ${CUR_PROTO_GEN_PY}
5232
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${PROJ_ROOT}/python/paddle/proto
53-
--proto_path ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/${filename}
54-
DEPENDS proto_accuracy
55-
${PROJ_ROOT}/proto/${filename}.m4)
33+
--proto_path ${PROJ_ROOT}/proto ${PROJ_ROOT}/proto/${filename}
34+
DEPENDS ${filename})
5635
endforeach()
5736

5837
include_directories(${CMAKE_CURRENT_BINARY_DIR}/proto)
@@ -61,5 +40,4 @@ add_custom_target(gen_proto_cpp ALL DEPENDS ${PROTO_GEN})
6140
add_custom_target(gen_proto_py ALL DEPENDS ${PROTO_GEN_PY})
6241
add_library(paddle_proto STATIC
6342
${PROTO_GEN})
64-
add_dependencies(paddle_proto proto_accuracy)
6543
target_include_directories(paddle_proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

proto/DataConfig.proto.m4 renamed to proto/DataConfig.proto

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ distributed under the License is distributed on an "AS IS" BASIS,
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
14-
ifdef(`proto3', `syntax = "proto2";')
14+
syntax = "proto2";
1515

1616
package paddle;
1717

18-
sinclude(`DataConfigExt.proto.m4')
18+
1919
message FileGroupConf {
2020
optional uint32 queue_capacity = 1 [default = 1];
2121
// how many files to load for a load file thread
@@ -26,7 +26,7 @@ message FileGroupConf {
2626
};
2727

2828
message DataConfig {
29-
sinclude(`DataConfigInter.proto.m4')
29+
3030
required string type = 1;
3131

3232
// name of a text file which contains a list of file names at each line
@@ -51,11 +51,11 @@ sinclude(`DataConfigInter.proto.m4')
5151

5252
/// Note the field number 17, 18 and 19 have been deprecated.
5353

54-
// a list of values which will be used to create additional one dimensional real
54+
// a list of values which will be used to create additional one dimensional float
5555
// values slots. These one dimensional slots can be used as the weight input
5656
// for cost layers.
5757
// Currently this is only supported by ProtoDataProvider.
58-
repeated real constant_slots = 20;
58+
repeated double constant_slots = 20;
5959

6060
// for PyDataProvider.
6161
// Specify the load data script module name, object name and user args
@@ -80,6 +80,6 @@ sinclude(`DataConfigInter.proto.m4')
8080
optional bool is_main_data = 26 [default = true];
8181

8282
// the usage ratio of instances. Setting to 1.0 means the use of all instances.
83-
optional real usage_ratio = 27 [default = 1.0];
83+
optional double usage_ratio = 27 [default = 1.0];
8484
};
8585

proto/DataFormat.proto.m4 renamed to proto/DataFormat.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
14-
ifdef(`proto3', `syntax = "proto2";')
14+
syntax = "proto2";
1515

1616
package paddle;
1717

proto/ModelConfig.proto.m4 renamed to proto/ModelConfig.proto

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
14-
ifdef(`proto3', `syntax = "proto2";')
14+
syntax = "proto2";
1515

1616
import "ParameterConfig.proto";
1717

@@ -20,7 +20,7 @@ package paddle;
2020
/**
2121
* Various structs for the configuration of a neural network
2222
*/
23-
sinclude(`ModelConfigExt.proto.m4')
23+
2424

2525
message ExternalConfig {
2626
repeated string layer_names = 1;
@@ -146,8 +146,8 @@ message NormConfig {
146146

147147
// the parameters for normalization
148148
// u = u / (1+scale*sum(u^2 in window))^pow
149-
required real scale = 4;
150-
required real pow = 5;
149+
required double scale = 4;
150+
required double pow = 5;
151151

152152
// The size of output feature map.
153153
required uint32 output_x = 6;
@@ -223,7 +223,7 @@ message OperatorConfig {
223223
required uint64 output_size = 4;
224224

225225
// For DotMulOperator
226-
optional real dotmul_scale = 5 [default = 1.0];
226+
optional double dotmul_scale = 5 [default = 1.0];
227227

228228
// For ConvOperator
229229
optional ConvConfig conv_conf = 6;
@@ -266,7 +266,7 @@ message LayerInputConfig {
266266
}
267267

268268
message LayerConfig {
269-
sinclude(`ModelConfigLayer.proto.m4')
269+
270270
required string name = 1;
271271
required string type = 2;
272272
optional uint64 size = 3;
@@ -293,7 +293,7 @@ sinclude(`ModelConfigLayer.proto.m4')
293293
optional uint32 partial_sum = 9;
294294

295295
// for dropout
296-
optional real drop_rate = 10;
296+
optional double drop_rate = 10;
297297

298298
// for HierarchicalSoftmaxLayer and NCELayer
299299
// the number of classes
@@ -317,17 +317,17 @@ sinclude(`ModelConfigLayer.proto.m4')
317317
// For NCELayer
318318
// The distribution for generating the random negative labels.
319319
// A uniform distribution will be used if not provided
320-
repeated real neg_sampling_dist = 17 [packed = true];
320+
repeated double neg_sampling_dist = 17 [packed = true];
321321

322322
// For MaxLayer
323323
// default: output VALUE of MaxLayer. set this flag to true for output INDEX
324-
// INDEX will be put in Argument::value as real values.
324+
// INDEX will be put in Argument::value as double values.
325325
optional bool output_max_index = 19 [default = false];
326326

327327
/// The filed number 20 have been deprecated.
328328

329329
// For self-normalized estimation
330-
optional real softmax_selfnorm_alpha = 21 [default = 0.1];
330+
optional double softmax_selfnorm_alpha = 21 [default = 0.1];
331331

332332
/// The filed numbers 22 and 23 have been deprecated.
333333

@@ -338,14 +338,14 @@ sinclude(`ModelConfigLayer.proto.m4')
338338
optional bool norm_by_times = 25;
339339

340340
// for CostLayers
341-
optional real coeff = 26 [default = 1.0];
341+
optional double coeff = 26 [default = 1.0];
342342

343343
// for AverageLayer
344344
// can be set to: 'average', 'sum' or 'squarerootn'
345345
optional string average_strategy = 27;
346346

347347
// for error clipping
348-
optional real error_clipping_threshold = 28 [default = 0.0];
348+
optional double error_clipping_threshold = 28 [default = 0.0];
349349

350350
// for operators used by mixed layer
351351
repeated OperatorConfig operator_confs = 29;
@@ -355,11 +355,11 @@ sinclude(`ModelConfigLayer.proto.m4')
355355
optional int32 max_sort_size = 31;
356356

357357
// for SlopeInterceptLayer
358-
optional real slope = 32;
359-
optional real intercept = 33;
358+
optional double slope = 32;
359+
optional double intercept = 33;
360360

361361
// for CosSimVecMatLayer and CosSimLayer
362-
optional real cos_scale = 34;
362+
optional double cos_scale = 34;
363363

364364
// for DataNormLayer
365365
// can be set to: 'z-score', 'min-max' or 'decimal-scaling'
@@ -394,7 +394,7 @@ sinclude(`ModelConfigLayer.proto.m4')
394394
// if number of the selected columns is less than
395395
// sample number * selective_fc output size * selective_fc_mull_mull_ratio
396396
// sparse multiplication is used, otherwise, using full multiplication.
397-
optional real selective_fc_full_mul_ratio = 44 [default = 0.02];
397+
optional double selective_fc_full_mul_ratio = 44 [default = 0.02];
398398

399399
// to indicate how many threads selective_fc use to to accelate
400400
// the plain_mul period
@@ -406,7 +406,7 @@ sinclude(`ModelConfigLayer.proto.m4')
406406
optional bool use_global_stats = 46;
407407

408408
// use to compute moving mean and variance.
409-
optional real moving_average_fraction = 47 [default = 0.9];
409+
optional double moving_average_fraction = 47 [default = 0.9];
410410

411411
// bias size
412412
optional uint32 bias_size = 48 [default = 0];
@@ -438,7 +438,7 @@ message EvaluatorConfig {
438438

439439
// Used by PrecisionRecallEvaluator and ClassificationErrorEvaluator
440440
// For multi binary labels: true if output > classification_threshold
441-
optional real classification_threshold = 6 [default = 0.5];
441+
optional double classification_threshold = 6 [default = 0.5];
442442
// The positive label. -1 means average precision and recall
443443
optional int32 positive_label = 7 [default = -1];
444444

0 commit comments

Comments
 (0)