提交 9f8e4981 编写于 作者: W wanghaoshuang

Fix some issues.

上级 6684b55b
......@@ -27,6 +27,9 @@ class PadOp : public framework::OperatorWithKernel {
void InferShape(const framework::InferShapeContext &ctx) const override {
auto dim0 = ctx.Input<Tensor>("X")->dims();
auto paddings = GetAttr<std::vector<std::pair<int, int>>>("paddings");
PADDLE_ENFORCE_EQ(
dim0.size(), paddings.size(),
"Paddings size should be equal to dimension size of input tensor.");
std::vector<int> dim1(dim0.size());
for (int i = 0; i < dim0.size(); ++i) {
dim1[i] = dim0[i] + paddings[i].first + paddings[i].second;
......
......@@ -14,8 +14,6 @@
#pragma once
#include "paddle/operators/math/math_function.h"
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
......@@ -30,12 +28,13 @@ using EigenTensor = framework::EigenTensor<T, D, MajorType, IndexType>;
template <typename Place, typename T, size_t D>
void PadFunction(const framework::ExecutionContext& context) {
auto pads = context.op_.GetAttr<std::vector<std::pair<int, int>>>("paddings");
auto pads =
context.op().GetAttr<std::vector<std::pair<int, int>>>("paddings");
Eigen::array<std::pair<int, int>, D> paddings;
for (int i = 0; i < pads.size(); ++i) {
paddings[i] = pads[i];
}
T pad_value = context.op_.GetAttr<T>("pad_value");
T pad_value = context.op().GetAttr<T>("pad_value");
auto* X = context.Input<Tensor>("X");
auto* Out = context.Output<Tensor>("Out");
......@@ -80,7 +79,8 @@ class PadKernel : public framework::OpKernel {
template <typename Place, typename T, size_t D>
void PadGradFunction(const framework::ExecutionContext& context) {
auto pads = context.op_.GetAttr<std::vector<std::pair<int, int>>>("paddings");
auto pads =
context.op().GetAttr<std::vector<std::pair<int, int>>>("paddings");
Eigen::array<std::pair<int, int>, D> paddings;
for (int i = 0; i < pads.size(); ++i) {
paddings[0].first = -paddings[0].first;
......
......@@ -96,7 +96,7 @@ class OpDescCreationMethod(object):
new_attr.strings.extend(user_defined_attr)
elif attr.type == framework_pb2.INT_PAIRS:
for p in user_defined_attr:
pair = new_attr.pairs.add()
pair = new_attr.int_pairs.add()
pair.first = p[0]
pair.second = p[1]
else:
......
......@@ -22,17 +22,22 @@ class TestPadOp(unittest.TestCase):
}
class PadGradOpTest(GradientChecker):
def test_pad(self):
op = Operator(
class TestPadGradOp(GradientChecker):
def setUp(self):
self.op = Operator(
type="pad",
X="X",
Out="Out",
paddings=[(0, 1), (2, 3)],
pad_value=0)
inputs = {'X': np.random.random((16, 16)).astype("float32"), }
self.inputs = {'X': np.random.random((16, 16)).astype("float32"), }
def test_normal(self):
self.check_grad(
self.op, self.inputs, set(["X"]), "Out", max_relative_error=0.5)
self.check_grad(op, inputs, set(["X"]), "Out", max_relative_error=0.5)
def test_cpu_gpu_compare(self):
self.compare_grad(self.op, self.inputs)
if __name__ == '__main__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册