diff --git a/paddle/framework/ddim.cc b/paddle/framework/ddim.cc index fc3d508553c0e966978b28d58127bdbff10d45f1..a3357867530c110df16a5f3ec8c799735206cc71 100644 --- a/paddle/framework/ddim.cc +++ b/paddle/framework/ddim.cc @@ -292,5 +292,13 @@ DDim flatten_to_2d(const DDim& src, int num_col_dims) { DDim flatten_to_1d(const DDim& src) { return make_ddim({product(src)}); } +DDim stride(const DDim& ddim) { + std::vector strides(ddim.size()); + strides[ddim.size() - 1] = 1; + for (int i = ddim.size() - 2; i >= 0; --i) { + strides[i] = strides[i + 1] * ddim[i + 1]; + } + return framework::make_ddim(strides); +} } // namespace framework } // namespace paddle diff --git a/paddle/framework/ddim.h b/paddle/framework/ddim.h index ca29e7e8c7776de6adf3e3b0e8f11f0d4d8487c3..4a871bb0a91ed4050847509cc3f24218bcd57142 100644 --- a/paddle/framework/ddim.h +++ b/paddle/framework/ddim.h @@ -121,6 +121,7 @@ DDim flatten_to_2d(const DDim& src, int num_col_dims); DDim flatten_to_1d(const DDim& src); +DDim stride(const DDim& ddim); } // namespace framework } // namespace paddle diff --git a/paddle/operators/crop_op.cc b/paddle/operators/crop_op.cc index d38c7ba35891ae73830726899468eaac8f7719dc..7ed21f336f69e494f3c4039c609c83407a80cd8c 100644 --- a/paddle/operators/crop_op.cc +++ b/paddle/operators/crop_op.cc @@ -32,8 +32,9 @@ class CropOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Out"), "Output(Out) of CropOp should not be null."); auto x_dim = ctx.Input("X")->dims(); - auto Y = ctx.Input("Y"); - if (Y == nullptr) { + auto *y = ctx.Input("Y"); + auto *out = ctx.Output("Out"); + if (y == nullptr) { auto shape = Attr>("shape"); PADDLE_ENFORCE_EQ( int64_t(shape.size()), x_dim.size(), @@ -42,12 +43,12 @@ class CropOp : public framework::OperatorWithKernel { for (size_t i = 0; i < shape.size(); ++i) { tensor_shape[i] = static_cast(shape[i]); } - ctx.Output("Out")->Resize(framework::make_ddim(tensor_shape)); + out->Resize(framework::make_ddim(tensor_shape)); } else { - PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(Y->dims()), + PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(y->dims()), "Tensor rank of both CropOp's " "inputs must be same."); - ctx.Output("Out")->Resize(Y->dims()); + out->Resize(y->dims()); } } }; diff --git a/paddle/operators/crop_op.h b/paddle/operators/crop_op.h index d4c523cf30e594575bfe5599597960829054f03f..2f40c059033ec649b29f6ecdee4fcedd128a63a6 100644 --- a/paddle/operators/crop_op.h +++ b/paddle/operators/crop_op.h @@ -24,19 +24,7 @@ namespace operators { // Internal template using EigenTensor = framework::EigenTensor; - using framework::Tensor; -using framework::DDim; - -// TODO(wanghaoshuang): move this function to other place -DDim stride(const DDim& ddim) { - std::vector strides(ddim.size()); - strides[ddim.size() - 1] = 1; - for (int i = ddim.size() - 2; i >= 0; --i) { - strides[i] = strides[i + 1] * ddim[i + 1]; - } - return make_ddim(strides); -} template class CropKernel : public framework::OpKernel { @@ -44,13 +32,13 @@ class CropKernel : public framework::OpKernel { void Compute(const framework::ExecutionContext& context) const override { auto* x = context.Input("X"); auto* out = context.Output("Out"); - T* x_data = x->data(); + const T* x_data = x->data(); T* out_data = out->mutable_data(context.GetPlace()); - auto x_stride = stride(x->dims()); - auto out_stride = stride(out->dims()); + auto x_stride = framework::stride(x->dims()); + auto out_stride = framework::stride(out->dims()); auto offsets = context.Attr>("offsets"); PADDLE_ENFORCE_EQ( - x_dims.size(), offsets.size(), + x->dims().size(), offsets.size(), "Offsets size should be equal to dimension size of input tensor."); int64_t offset = 0; for (int i = 0; i < offsets.size(); ++i) { @@ -71,7 +59,7 @@ void CropGradFunction(const framework::ExecutionContext& context) { Eigen::array, D> paddings; for (int i = 0; i < D; ++i) { paddings[i].first = offsets[i]; - paddings[i].second = d_x_dims[i] - d_out_dims[i] - offsets[i]; + paddings[i].second = d_x->dims()[i] - d_out->dims()[i] - offsets[i]; } auto d_x_tensor = EigenTensor::From(*d_x); auto d_out_tensor = EigenTensor::From(*d_out);