From 294790a9abd1d1a39469d159aa2826c273eb906a Mon Sep 17 00:00:00 2001 From: eric-haibin-lin Date: Fri, 14 Apr 2017 23:20:53 +0000 Subject: [PATCH 1/2] add cpp_package build option in CI --- Jenkinsfile | 3 ++ cpp-package/example/charRNN.cpp | 7 ++-- cpp-package/include/mxnet-cpp/op_suppl.h | 43 +----------------------- src/operator/tensor/matrix_op-inl.h | 8 ++--- src/operator/tensor/multisample_op.h | 10 +++--- 5 files changed, 17 insertions(+), 54 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 3c40229c9a20..2d9a14d32316 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -81,6 +81,7 @@ stage('Build') { init_git() def flag = """ \ USE_PROFILER=1 \ +USE_CPP_PACKAGE=1 \ USE_BLAS=openblas \ -j\$(nproc) """ @@ -99,6 +100,7 @@ USE_BLAS=openblas \ USE_CUDA=1 \ USE_CUDA_PATH=/usr/local/cuda \ USE_CUDNN=1 \ +USE_CPP_PACKAGE=1 \ -j\$(nproc) """ make('gpu', flag) @@ -126,6 +128,7 @@ USE_MKL2017_EXPERIMENTAL=1 \ USE_CUDA=1 \ USE_CUDA_PATH=/usr/local/cuda \ USE_CUDNN=1 \ +USE_CPP_PACKAGE=1 \ -j\$(nproc) """ make('mklml_gpu', flag) diff --git a/cpp-package/example/charRNN.cpp b/cpp-package/example/charRNN.cpp index daf31a4fe69c..d64f226d00b9 100644 --- a/cpp-package/example/charRNN.cpp +++ b/cpp-package/example/charRNN.cpp @@ -194,7 +194,8 @@ class Shuffler { class BucketSentenceIter : public DataIter { Shuffler* random; - int batch, current, end, sequence_length; + int batch, current, end; + unsigned int sequence_length; Context device; vector> sequences; vector index2chars; @@ -582,7 +583,7 @@ void predict(wstring* ptext, int sequence_length, const string param_file, LoadCheckpoint(param_file, exe); mx_float index; - wchar_t next; + wchar_t next = 0; vector softmax; softmax.resize(input_dim); for (auto c : *ptext) { @@ -642,7 +643,7 @@ void predictWithBuiltInRNNOp(wstring* ptext, int sequence_length, const string p LoadCheckpoint(param_file, exe); mx_float index; - wchar_t next; + wchar_t next = 0; vector softmax; softmax.resize(input_dim); for (auto c : *ptext) { diff --git a/cpp-package/include/mxnet-cpp/op_suppl.h b/cpp-package/include/mxnet-cpp/op_suppl.h index 5eb86d8ef275..9381a1ecade9 100644 --- a/cpp-package/include/mxnet-cpp/op_suppl.h +++ b/cpp-package/include/mxnet-cpp/op_suppl.h @@ -119,48 +119,7 @@ inline Symbol Crop(const std::string& symbol_name, /*! - * \breif Slice input equally along specified axis. - * \param data input symbol. - * \param num_outputs Number of outputs to be sliced. - * \param axis Dimension along which to slice. - * \param squeeze_axis If true AND the sliced dimension becomes 1, squeeze that dimension. - * \return new symbol - */ -inline Symbol SliceChannel(Symbol data, - int num_outputs, - int axis = 1, - bool squeeze_axis = false) { - return Operator("SliceChannel") - .SetParam("num_outputs", num_outputs) - .SetParam("axis", axis) - .SetParam("squeeze_axis", squeeze_axis) (data) - .CreateSymbol(); -} - - -/*! - * \breif Slice input equally along specified axis. - * \param symbol_name name of the resulting symbol. - * \param data input symbol. - * \param num_outputs Number of outputs to be sliced. - * \param axis Dimension along which to slice. - * \param squeeze_axis If true AND the sliced dimension becomes 1, squeeze that dimension. - * \return new symbol - */ -inline Symbol SliceChannel(const std::string& symbol_name, - Symbol data, - int num_outputs, - int axis = 1, - bool squeeze_axis = false) { - return Operator("SliceChannel") - .SetParam("num_outputs", num_outputs) - .SetParam("axis", axis) - .SetParam("squeeze_axis", squeeze_axis) (data) - .CreateSymbol(symbol_name); -} - -/*! - * \breif Apply activation function to input. + * \brief Apply activation function to input. * Softmax Activation is only available with CUDNN on GPUand will be * computed at each location across channel if input is 4D. * \param symbol_name name of the resulting symbol. diff --git a/src/operator/tensor/matrix_op-inl.h b/src/operator/tensor/matrix_op-inl.h index 9862476ad0cf..f2cda70331cc 100644 --- a/src/operator/tensor/matrix_op-inl.h +++ b/src/operator/tensor/matrix_op-inl.h @@ -32,6 +32,10 @@ struct ReshapeParam : public dmlc::Parameter { DMLC_DECLARE_FIELD(shape) .set_default(nnvm::Tuple()) .describe("The target shape"); + DMLC_DECLARE_FIELD(keep_highest).set_default(false) + .describe("(Deprecated! Use ``shape`` instead.) Whether keep the highest dim unchanged." + "If set to true, then the first dim in target_shape is ignored," + "and always fixed as input"); DMLC_DECLARE_FIELD(reverse) .set_default(false) .describe("If true then the special values are inferred from right to left"); @@ -40,10 +44,6 @@ struct ReshapeParam : public dmlc::Parameter { .describe("(Deprecated! Use ``shape`` instead.) " "Target new shape. One and only one dim can be 0, " "in which case it will be inferred from the rest of dims"); - DMLC_DECLARE_FIELD(keep_highest).set_default(false) - .describe("(Deprecated! Use ``shape`` instead.) Whether keep the highest dim unchanged." - "If set to true, then the first dim in target_shape is ignored," - "and always fixed as input"); } }; diff --git a/src/operator/tensor/multisample_op.h b/src/operator/tensor/multisample_op.h index 30e9aedf61ea..5033ff326360 100644 --- a/src/operator/tensor/multisample_op.h +++ b/src/operator/tensor/multisample_op.h @@ -45,7 +45,7 @@ inline bool MultiSampleOpShape(const nnvm::NodeAttrs& attrs, // Get shape to be sampled for each parameter set. const MultiSampleParam& param = nnvm::get(attrs.parsed); TShape sshape = param.shape; - for ( int i = 0; i < sshape.ndim(); ++i ) { + for ( size_t i = 0; i < sshape.ndim(); ++i ) { CHECK_GT((int)sshape[i], 0) << "shape parameter must be non-zero within each dimension"; } // Examine output shape whether it is already defined. @@ -57,10 +57,10 @@ inline bool MultiSampleOpShape(const nnvm::NodeAttrs& attrs, tshape = TShape(tshape.begin(), tshape.begin()+(tshape.ndim()-sshape.ndim())); } // Shape assignemnt/checking for inputs. - for ( int i = 0; i < in_attrs->size(); ++i ) { + for ( size_t i = 0; i < in_attrs->size(); ++i ) { if ( !shape_assign(&tshape, (*in_attrs)[i])) return false; } - for ( int i = 0; i < in_attrs->size(); ++i ) { + for ( size_t i = 0; i < in_attrs->size(); ++i ) { SHAPE_ASSIGN_CHECK(*in_attrs, i, tshape); } if ( tshape.ndim() > 0 ) { @@ -84,10 +84,10 @@ inline bool MultiSampleOpType(const nnvm::NodeAttrs& attrs, // All inputs must have same type. int dtype = -1; - for ( int i = 0; i < in_attrs->size(); ++i ) { + for ( size_t i = 0; i < in_attrs->size(); ++i ) { if (!type_assign(&dtype, (*in_attrs)[i])) return false; } - for ( int i = 0; i < in_attrs->size(); ++i ) { + for ( size_t i = 0; i < in_attrs->size(); ++i ) { TYPE_ASSIGN_CHECK(*in_attrs, i, dtype); } if (-1 == dtype) return false; From 96eb4f5a1a96e586be3558ab094b7d0139478365 Mon Sep 17 00:00:00 2001 From: eric-haibin-lin Date: Sat, 15 Apr 2017 17:19:04 +0000 Subject: [PATCH 2/2] Change usage of Reshape in cpp-package --- cpp-package/example/charRNN.cpp | 6 +++--- src/operator/tensor/matrix_op-inl.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cpp-package/example/charRNN.cpp b/cpp-package/example/charRNN.cpp index d64f226d00b9..4dd1ce7a5a51 100644 --- a/cpp-package/example/charRNN.cpp +++ b/cpp-package/example/charRNN.cpp @@ -115,7 +115,7 @@ Symbol LSTMUnroll(int num_lstm_layer, int sequence_length, int input_dim, auto label = Symbol::Variable("softmax_label"); label = transpose(label); - label = Reshape(label, Shape(), false, false, Shape(-1)); // -1: infer from graph + label = Reshape(label, Shape(), false, Shape(-1), false); // -1: infer from graph auto sm = SoftmaxOutput("softmax", pred, label); if (isTrain) return sm; @@ -141,7 +141,7 @@ Symbol LSTMWithBuiltInRNNOp(int num_lstm_layer, int sequence_length, int input_d auto label = Symbol::Variable("softmax_label"); label = transpose(label); label = Reshape(label, Shape(), false, - false, Shape(-1)); // FullyConnected requires one dimension + Shape(-1), false); // FullyConnected requires one dimension if (!TIME_MAJOR && isTrain) embed = SwapAxis(embed, 0, 1); // Change to time-major as cuDNN requires @@ -151,7 +151,7 @@ Symbol LSTMWithBuiltInRNNOp(int num_lstm_layer, int sequence_length, int input_d auto rnn_params = Symbol::Variable("LSTM_parameters"); // See explanations near RNNXavier class auto rnn = RNN(embed, rnn_params, rnn_h_init, rnn_c_init, num_hidden, num_lstm_layer, RNNMode::lstm, false, dropout, !isTrain); - auto hidden = Reshape(rnn[0], Shape(), false, false, Shape(-1, num_hidden)); + auto hidden = Reshape(rnn[0], Shape(), false, Shape(-1, num_hidden), false); auto cls_weight = Symbol::Variable("cls_weight"); auto cls_bias = Symbol::Variable("cls_bias"); diff --git a/src/operator/tensor/matrix_op-inl.h b/src/operator/tensor/matrix_op-inl.h index f2cda70331cc..9862476ad0cf 100644 --- a/src/operator/tensor/matrix_op-inl.h +++ b/src/operator/tensor/matrix_op-inl.h @@ -32,10 +32,6 @@ struct ReshapeParam : public dmlc::Parameter { DMLC_DECLARE_FIELD(shape) .set_default(nnvm::Tuple()) .describe("The target shape"); - DMLC_DECLARE_FIELD(keep_highest).set_default(false) - .describe("(Deprecated! Use ``shape`` instead.) Whether keep the highest dim unchanged." - "If set to true, then the first dim in target_shape is ignored," - "and always fixed as input"); DMLC_DECLARE_FIELD(reverse) .set_default(false) .describe("If true then the special values are inferred from right to left"); @@ -44,6 +40,10 @@ struct ReshapeParam : public dmlc::Parameter { .describe("(Deprecated! Use ``shape`` instead.) " "Target new shape. One and only one dim can be 0, " "in which case it will be inferred from the rest of dims"); + DMLC_DECLARE_FIELD(keep_highest).set_default(false) + .describe("(Deprecated! Use ``shape`` instead.) Whether keep the highest dim unchanged." + "If set to true, then the first dim in target_shape is ignored," + "and always fixed as input"); } };