Skip to content

Commit 5cf8204

Browse files
refine docString
1 parent 44e1ac3 commit 5cf8204

File tree

3 files changed

+40
-94
lines changed

3 files changed

+40
-94
lines changed

paddle/operators/bilinear_tensor_product_op.cc

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,28 @@ class BilinearTensorProductOp : public framework::OperatorWithKernel {
3434
auto y_dims = ctx->GetInputDim("Y");
3535
auto weight_dims = ctx->GetInputDim("Weight");
3636

37-
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The input X must be a 2D Tensor.");
38-
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The input Y must be a 2D Tensor.");
37+
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The input(X) must be a 2D Tensor.");
38+
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The input(Y) must be a 2D Tensor.");
3939
PADDLE_ENFORCE_EQ(weight_dims.size(), 3UL,
40-
"The input Weight must be a 3D tensor.");
41-
PADDLE_ENFORCE(weight_dims[0],
42-
"The first dimension of Weight must be larger than 0.");
43-
PADDLE_ENFORCE(weight_dims[1],
44-
"The second dimension of Weight must be larger than 0.");
45-
PADDLE_ENFORCE(weight_dims[2],
46-
"The third dimension of Weight must be larger than 0.");
40+
"The input(Weight) must be a 3D tensor.");
4741
PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0],
48-
"The first dimension(batch_size) of X must be "
49-
"equal to the first dimension of the Y.");
42+
"The first dimension(batch_size) of input(X) must be "
43+
"equal to the first dimension of the input(Y).");
5044
PADDLE_ENFORCE_EQ(x_dims[1], weight_dims[1],
51-
"The second dimension of X must be equal to the second "
52-
"dimension of the Weight.");
45+
"The second dimension of input(X) must be equal to "
46+
"the second dimension of the input(Weight).");
5347
PADDLE_ENFORCE_EQ(y_dims[1], weight_dims[2],
54-
"The second dimension of Y must be equal to the third "
55-
"dimension of the Weight.");
48+
"The second dimension of input(Y) must be equal to "
49+
"the third dimension of the input(Weight).");
5650

5751
if (ctx->HasInput("Bias")) {
5852
auto bias_dims = ctx->GetInputDim("Bias");
59-
PADDLE_ENFORCE_EQ(bias_dims.size(), 2UL,
60-
"The input Bias must have 2 dimensions.");
61-
PADDLE_ENFORCE_EQ(bias_dims[0], 1UL,
62-
"The first dimention of input Bias must be 1.");
53+
PADDLE_ENFORCE(bias_dims.size() == 2UL && bias_dims[0] == 1UL,
54+
"The Input(Bias) must be a 2-D tensor with "
55+
"the 2nd dimension fixed to 1 (a row vector).");
6356
PADDLE_ENFORCE_EQ(bias_dims[1], weight_dims[0],
64-
"The second dimension of Bias must be equal to the "
65-
"first dimension of the Weight.");
57+
"The second dimension of input(Bias) must be equal "
58+
"to the first dimension of the input(Weight).");
6659
}
6760

6861
ctx->SetOutputDim("Out", {x_dims[0], weight_dims[0]});
@@ -75,12 +68,13 @@ class BilinearTensorProductOpMaker : public framework::OpProtoAndCheckerMaker {
7568
BilinearTensorProductOpMaker(framework::OpProto* proto,
7669
framework::OpAttrChecker* op_checker)
7770
: OpProtoAndCheckerMaker(proto, op_checker) {
78-
AddInput("X", "The first input of BilinearTensorProduct op.");
79-
AddInput("Y", "The second input of BilinearTensorProduct op.");
80-
AddInput("Weight", "The input weight of BilinearTensorProduct op.");
81-
AddInput("Bias", "The input bias of BilinearTensorProduct op.")
71+
AddInput("X", "The first input of bilinear_tensor_product operator.");
72+
AddInput("Y", "The second input of bilinear_tensor_product operator.");
73+
AddInput("Weight",
74+
"The learnable parameters of bilinear_tensor_product operator.");
75+
AddInput("Bias", "The learnable bias of bilinear_tensor_product operator.")
8276
.AsDispensable();
83-
AddOutput("Out", "The output of BilinearTensorProduct op.");
77+
AddOutput("Out", "The output of bilinear_tensor_product operator.");
8478
AddComment(R"DOC(
8579
Bilinear Tensor Product operator.
8680
Given input X and Y, a 3D tensor weight, and bias. Each column of the
@@ -104,27 +98,29 @@ class BilinearTensorProductOpGrad : public framework::OperatorWithKernel {
10498
PADDLE_ENFORCE(ctx->HasInput("Weight"),
10599
"Input(Weight) should not be null.");
106100
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
107-
"Input (Out@GRAD) should not be null.");
101+
"Input(Out@GRAD) should not be null.");
108102
auto x_dims = ctx->GetInputDim("X");
109103
auto y_dims = ctx->GetInputDim("Y");
110104
auto weight_dims = ctx->GetInputDim("Weight");
111105
auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
112106

113107
PADDLE_ENFORCE_EQ(out_dims.size(), 2UL,
114-
"The Out@GRAD must be a 2D Tensor.");
108+
"The input(Out@GRAD) must be a 2D Tensor.");
115109
PADDLE_ENFORCE_EQ(
116110
x_dims[0], out_dims[0],
117-
"The first dimension(batch_size) of Out@GRAD must be equal to "
118-
"the first dimension of the Input(X).");
119-
PADDLE_ENFORCE_EQ(weight_dims[0], out_dims[1],
120-
"The second dimension of Out@GRAD must be equal to "
121-
"the third dimension of the Input(Weight).");
111+
"The first dimension(batch_size) of input(Out@GRAD) must be "
112+
"equal to the first dimension of the Input(X).");
113+
PADDLE_ENFORCE_EQ(
114+
weight_dims[0], out_dims[1],
115+
"The second dimension of input(Out@GRAD) must be equal to "
116+
"the third dimension of the Input(Weight).");
122117

123118
if (ctx->HasInput("Bias")) {
124119
auto bias_dims = ctx->GetInputDim("Bias");
125-
PADDLE_ENFORCE_EQ(bias_dims[1], out_dims[1],
126-
"The second dimension of Out@GRAD must be equal to "
127-
"the second dimension of the Input(Bias).");
120+
PADDLE_ENFORCE_EQ(
121+
bias_dims[1], out_dims[1],
122+
"The second dimension of input(Out@GRAD) must be equal to "
123+
"the second dimension of the Input(Bias).");
128124
auto bias_grad_name = framework::GradVarName("Bias");
129125
if (ctx->HasOutput(bias_grad_name))
130126
ctx->SetOutputDim(bias_grad_name, bias_dims);
@@ -155,7 +151,9 @@ REGISTER_OP(bilinear_tensor_product, ops::BilinearTensorProductOp,
155151
ops::BilinearTensorProductOpGrad);
156152
REGISTER_OP_CPU_KERNEL(
157153
bilinear_tensor_product,
158-
ops::BilinearTensorProductKernel<paddle::platform::CPUPlace, float>);
154+
ops::BilinearTensorProductKernel<paddle::platform::CPUPlace, float>,
155+
ops::BilinearTensorProductKernel<paddle::platform::CPUPlace, double>);
159156
REGISTER_OP_CPU_KERNEL(
160157
bilinear_tensor_product_grad,
161-
ops::BilinearTensorProductGradKernel<paddle::platform::CPUPlace, float>);
158+
ops::BilinearTensorProductGradKernel<paddle::platform::CPUPlace, float>,
159+
ops::BilinearTensorProductGradKernel<paddle::platform::CPUPlace, double>);

paddle/operators/bilinear_tensor_product_op.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ limitations under the License. */
1818
namespace ops = paddle::operators;
1919
REGISTER_OP_GPU_KERNEL(
2020
bilinear_tensor_product,
21-
ops::BilinearTensorProductKernel<paddle::platform::GPUPlace, float>);
21+
ops::BilinearTensorProductKernel<paddle::platform::GPUPlace, float>,
22+
ops::BilinearTensorProductKernel<paddle::platform::GPUPlace, double>);
2223
REGISTER_OP_GPU_KERNEL(
2324
bilinear_tensor_product_grad,
24-
ops::BilinearTensorProductGradKernel<paddle::platform::GPUPlace, float>);
25+
ops::BilinearTensorProductGradKernel<paddle::platform::GPUPlace, float>,
26+
ops::BilinearTensorProductGradKernel<paddle::platform::GPUPlace, double>);

python/paddle/v2/framework/tests/test_bilinear_tensor_product_op.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,59 +33,5 @@ def test_check_grad_normal(self):
3333
self.check_grad(['X', 'Y', 'Weight', 'Bias'], 'Out')
3434

3535

36-
class TestBilinearTensorProductOp2(TestBilinearTensorProductOp):
37-
def setUp(self):
38-
self.op_type = "bilinear_tensor_product"
39-
batch_size = 1
40-
size0 = 1
41-
size1 = 1
42-
size2 = 1
43-
a = np.random.random((batch_size, size0)).astype("float32")
44-
b = np.random.random((batch_size, size1)).astype("float32")
45-
w = np.random.random((size2, size0, size1)).astype("float32")
46-
bias = np.random.random((1, size2)).astype("float32")
47-
output = np.zeros((batch_size, size2)).astype("float32")
48-
for i in range(size2):
49-
w_i = w[i, :, :]
50-
output[:, i] = np.sum(np.matmul(a, w_i) * b, axis=1)
51-
self.inputs = {
52-
'X': a,
53-
'Y': b,
54-
'Weight': w,
55-
'Bias': bias,
56-
}
57-
self.outputs = {'Out': output + bias}
58-
59-
def test_check_output(self):
60-
self.check_output()
61-
62-
def test_check_grad_normal(self):
63-
self.check_grad(['X', 'Y', 'Weight', 'Bias'], 'Out')
64-
65-
66-
class TestBilinearTensorProductOp3(TestBilinearTensorProductOp):
67-
def setUp(self):
68-
self.op_type = "bilinear_tensor_product"
69-
batch_size = 7
70-
size0 = 4
71-
size1 = 5
72-
size2 = 6
73-
a = np.random.random((batch_size, size0)).astype("float32")
74-
b = np.random.random((batch_size, size1)).astype("float32")
75-
w = np.random.random((size2, size0, size1)).astype("float32")
76-
output = np.zeros((batch_size, size2)).astype("float32")
77-
for i in range(size2):
78-
w_i = w[i, :, :]
79-
output[:, i] = np.sum(np.matmul(a, w_i) * b, axis=1)
80-
self.inputs = {'X': a, 'Y': b, 'Weight': w}
81-
self.outputs = {'Out': output}
82-
83-
def test_check_output(self):
84-
self.check_output()
85-
86-
def test_check_grad_normal(self):
87-
self.check_grad(['X', 'Y', 'Weight'], 'Out')
88-
89-
9036
if __name__ == "__main__":
9137
unittest.main()

0 commit comments

Comments
 (0)