diff --git a/paddle/fluid/inference/api/helper.h b/paddle/fluid/inference/api/helper.h index ab7f55337488f9e4c953210124e47c12e26ed6b1..38f754b92d4ddb15848060c548073eee1ba8ab32 100644 --- a/paddle/fluid/inference/api/helper.h +++ b/paddle/fluid/inference/api/helper.h @@ -21,6 +21,7 @@ #endif #include #include // NOLINT +#include #include #include #include @@ -79,26 +80,63 @@ static void split(const std::string &str, char sep, pieces->push_back(str.substr(pos)); } } + +template +static T convert(const std::string &item, + std::function func) { + T res; + try { + res = func(item); + } catch (std::invalid_argument &e) { + std::string message = + "invalid_argument exception when try to convert : " + item; + LOG(ERROR) << message; + PADDLE_THROW(message); + } catch (std::out_of_range &e) { + std::string message = + "out_of_range exception when try to convert : " + item; + LOG(ERROR) << message; + PADDLE_THROW(message); + } catch (...) { + std::string message = "unexpected exception when try to convert " + item; + LOG(ERROR) << message; + PADDLE_THROW(message); + } + return res; +} + static void split_to_float(const std::string &str, char sep, std::vector *fs) { std::vector pieces; split(str, sep, &pieces); std::transform(pieces.begin(), pieces.end(), std::back_inserter(*fs), - [](const std::string &v) { return std::stof(v); }); + [](const std::string &v) { + return convert(v, [](const std::string &item) { + return std::stof(item); + }); + }); } static void split_to_int64(const std::string &str, char sep, std::vector *is) { std::vector pieces; split(str, sep, &pieces); std::transform(pieces.begin(), pieces.end(), std::back_inserter(*is), - [](const std::string &v) { return std::stoi(v); }); + [](const std::string &v) { + return convert(v, [](const std::string &item) { + return std::stoll(item); + }); + }); } static void split_to_int(const std::string &str, char sep, std::vector *is) { std::vector pieces; split(str, sep, &pieces); std::transform(pieces.begin(), pieces.end(), std::back_inserter(*is), - [](const std::string &v) { return std::stoi(v); }); + [](const std::string &v) { + return convert(v, [](const std::string &item) { + return std::stoi(item); + }); + }); } template std::string to_string(const std::vector &vec) { diff --git a/paddle/fluid/operators/im2sequence_op.cc b/paddle/fluid/operators/im2sequence_op.cc index 44fd95edef253b814a166f724ca67fcafe979b99..0fa7322fbd65c85f574c18f822e2c189c8f87646 100644 --- a/paddle/fluid/operators/im2sequence_op.cc +++ b/paddle/fluid/operators/im2sequence_op.cc @@ -34,7 +34,7 @@ class Im2SequenceOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_EQ(in_dim.size(), 4, "Input(X) format must be 4D tensor, eg., NCHW."); - int img_channels = in_dim[1]; + auto img_channels = in_dim[1]; auto kernels = ctx->Attrs().Get>("kernels"); auto strides = ctx->Attrs().Get>("strides"); diff --git a/paddle/fluid/operators/im2sequence_op.h b/paddle/fluid/operators/im2sequence_op.h index 4a9942819414d552eb69bd0b30b66aab76a2dbf4..9c9069b722763d0ec0d39d2f6fb35477c7578f30 100644 --- a/paddle/fluid/operators/im2sequence_op.h +++ b/paddle/fluid/operators/im2sequence_op.h @@ -113,9 +113,10 @@ class Im2SequenceKernel : public framework::OpKernel { paddings[2], strides[0]); int output_width = Im2SeqOutputSize(img_width, kernels[1], paddings[1], paddings[3], strides[1]); - out->mutable_data({batch_size * output_height * output_width, - img_channels * kernels[0] * kernels[1]}, - ctx.GetPlace()); + out->mutable_data( + {static_cast(batch_size) * output_height * output_width, + static_cast(img_channels) * kernels[0] * kernels[1]}, + ctx.GetPlace()); const std::vector dilations({1, 1}); auto out_dims = out->dims(); out->Resize({batch_size, out->numel() / batch_size}); diff --git a/paddle/fluid/operators/math/context_project.h b/paddle/fluid/operators/math/context_project.h index f6094369567cd0481b68ebbad46d4a3717eb6ead..e9019c6d2fe6890ee92cb5a3b047666e3c2a7e04 100644 --- a/paddle/fluid/operators/math/context_project.h +++ b/paddle/fluid/operators/math/context_project.h @@ -144,7 +144,8 @@ class ContextProjectFunctor { sequence_height = static_cast(out_t.dims()[0]); // add up trainable data - out_t.Resize({sequence_height * context_length, sequence_width}); + out_t.Resize({static_cast(sequence_height) * context_length, + sequence_width}); if (up_pad > 0) { // add up pad int padding_rows = std::min( @@ -191,7 +192,8 @@ class ContextProjectFunctor { &out_t_sub); } } - out_t.Resize({sequence_height, context_length * sequence_width}); + out_t.Resize({sequence_height, + static_cast(context_length) * sequence_width}); } } } @@ -260,7 +262,8 @@ class ContextProjectGradFunctor { static_cast(lod_level_0[i + 1])); sequence_height = static_cast(out_t.dims()[0]); - out_t.Resize({sequence_height * context_length, sequence_width}); + out_t.Resize({static_cast(sequence_height) * context_length, + sequence_width}); if (up_pad > 0) { int padding_rows = std::min( @@ -308,7 +311,8 @@ class ContextProjectGradFunctor { w_sub.data()); } } - out_t.Resize({sequence_height, context_length * sequence_width}); + out_t.Resize({sequence_height, + static_cast(context_length) * sequence_width}); } } } diff --git a/paddle/fluid/operators/reader/ctr_reader.cc b/paddle/fluid/operators/reader/ctr_reader.cc index 43a49de52242b96aade91013e89228fcb3247302..10ad67ac8774c7f00c227bc7f2a15fcf55d22b5e 100644 --- a/paddle/fluid/operators/reader/ctr_reader.cc +++ b/paddle/fluid/operators/reader/ctr_reader.cc @@ -32,17 +32,17 @@ namespace reader { static inline void string_split(const std::string& s, const char delimiter, std::vector* output) { - size_t start = 0; - size_t end = s.find_first_of(delimiter); + if (s.empty()) return; - while (end <= std::string::npos) { - output->emplace_back(s.substr(start, end - start)); - if (end == std::string::npos) { - break; - } + size_t start = 0; + size_t end = s.find(delimiter); + while (end != std::string::npos) { + if (end > start) output->emplace_back(s.substr(start, end - start)); start = end + 1; - end = s.find_first_of(delimiter, start); + end = s.find(delimiter, start); } + auto term = s.substr(start); + if (!term.empty()) output->emplace_back(term); } static inline void parse_line( diff --git a/paddle/fluid/operators/unpool_op.h b/paddle/fluid/operators/unpool_op.h index 96abad3de9b959ee611355c67f1fa9e56c430b1b..e388ec5ae3937aadebdcd8ecce8d82dae05be7cd 100644 --- a/paddle/fluid/operators/unpool_op.h +++ b/paddle/fluid/operators/unpool_op.h @@ -61,10 +61,10 @@ class UnpoolGradKernel : public framework::OpKernel { auto& device_ctx = context.template device_context(); math::SetConstant zero; - if (in_x_grad) { - in_x_grad->mutable_data(context.GetPlace()); - zero(device_ctx, in_x_grad, static_cast(0)); - } + + in_x_grad->mutable_data(context.GetPlace()); + zero(device_ctx, in_x_grad, static_cast(0)); + math::Unpool2dMaxGradFunctor unpool2d_max_backward; unpool2d_max_backward(device_ctx, *in_x, *in_y, *out, *out_grad, in_x_grad); }