提交 2120f075 编写于 作者: S SunGaofeng

modify infer shape in pad_op.cc, pad_constant_like_op.cc. No need in psroi_pool_op.cc, crop_op.cc

上级 eb02eb94
...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and ...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/operators/pad_constant_like_op.h" #include "paddle/fluid/operators/pad_constant_like_op.h"
#include <memory>
namespace paddle { namespace paddle {
namespace operators { namespace operators {
...@@ -38,8 +39,13 @@ class PadConstantLikeOp : public framework::OperatorWithKernel { ...@@ -38,8 +39,13 @@ class PadConstantLikeOp : public framework::OperatorWithKernel {
"The dimention of X and Y should be the same."); "The dimention of X and Y should be the same.");
for (int i = 0; i < x_dim.size(); ++i) { for (int i = 0; i < x_dim.size(); ++i) {
if ((!ctx->IsRuntime()) && ((x_dim[i] == -1) || (y_dim[i] == -1))) {
continue;
} else {
PADDLE_ENFORCE_GE(x_dim[i], y_dim[i]); PADDLE_ENFORCE_GE(x_dim[i], y_dim[i]);
} }
}
ctx->SetOutputDim("Out", x_dim); ctx->SetOutputDim("Out", x_dim);
ctx->ShareLoD("X", /*->*/ "Out"); ctx->ShareLoD("X", /*->*/ "Out");
} }
...@@ -162,10 +168,14 @@ class PadConstantLikeOpGrad : public framework::OperatorWithKernel { ...@@ -162,10 +168,14 @@ class PadConstantLikeOpGrad : public framework::OperatorWithKernel {
ctx->ShareLoD("Y", /*->*/ y_grad_name); ctx->ShareLoD("Y", /*->*/ y_grad_name);
for (int i = 0; i < y_dim.size(); ++i) { for (int i = 0; i < y_dim.size(); ++i) {
if ((!ctx->IsRuntime()) && ((dout_dim[i] == -1) || (y_dim[i] == -1))) {
continue;
} else {
PADDLE_ENFORCE_GE(dout_dim[i], y_dim[i]); PADDLE_ENFORCE_GE(dout_dim[i], y_dim[i]);
} }
} }
} }
}
protected: protected:
framework::OpKernelType GetExpectedKernelType( framework::OpKernelType GetExpectedKernelType(
......
...@@ -36,8 +36,12 @@ class PadOp : public framework::OperatorWithKernel { ...@@ -36,8 +36,12 @@ class PadOp : public framework::OperatorWithKernel {
"of input tensor."); "of input tensor.");
std::vector<int64_t> out_dims(x_dim.size()); std::vector<int64_t> out_dims(x_dim.size());
for (int i = 0; i < x_dim.size(); ++i) { for (int i = 0; i < x_dim.size(); ++i) {
if ((!ctx->IsRuntime()) && (x_dim[i] == -1)) {
out_dims[i] = -1;
} else {
out_dims[i] = x_dim[i] + paddings[i * 2] + paddings[i * 2 + 1]; out_dims[i] = x_dim[i] + paddings[i * 2] + paddings[i * 2 + 1];
} }
}
ctx->SetOutputDim("Out", framework::make_ddim(out_dims)); ctx->SetOutputDim("Out", framework::make_ddim(out_dims));
if (out_dims[0] == x_dim[0]) { if (out_dims[0] == x_dim[0]) {
// Only pass LoD when the first dimension is equal between // Only pass LoD when the first dimension is equal between
...@@ -100,19 +104,15 @@ class PadOpGrad : public framework::OperatorWithKernel { ...@@ -100,19 +104,15 @@ class PadOpGrad : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override { void InferShape(framework::InferShapeContext* ctx) const override {
auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out"));
auto& paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
for (int i = 0; i < dout_dims.size(); ++i) {
dout_dims[i] -= (paddings[i * 2] + paddings[i * 2 + 1]);
}
auto x_grad_name = framework::GradVarName("X"); auto x_grad_name = framework::GradVarName("X");
if (ctx->HasOutput(x_grad_name)) { if (ctx->HasOutput(x_grad_name)) {
auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out")); auto dout_dims = ctx->GetInputDim(framework::GradVarName("Out"));
auto& paddings = ctx->Attrs().Get<std::vector<int>>("paddings"); auto& paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
for (int i = 0; i < dout_dims.size(); ++i) { for (int i = 0; i < dout_dims.size(); ++i) {
if (ctx->IsRuntime() || (dout_dims[i] != -1)) {
dout_dims[i] -= (paddings[i * 2] + paddings[i * 2 + 1]); dout_dims[i] -= (paddings[i * 2] + paddings[i * 2 + 1]);
} }
}
ctx->SetOutputDim(x_grad_name, dout_dims); ctx->SetOutputDim(x_grad_name, dout_dims);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册