Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bilinear Tensor Product operator. #5014

Merged
merged 11 commits into from
Nov 14, 2017
52 changes: 27 additions & 25 deletions paddle/operators/bilinear_tensor_product_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,26 @@ class BilinearTensorProductKernel : public framework::OpKernel<T> {

auto batch_size = x->dims()[0];
auto weight_dims = weight->dims();
int Out_dim = weight_dims[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out_dim --> out_dim
X_dim --> x_dim
Y_dim -->y_dim

第一个字母不要大写。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

int X_dim = weight_dims[1];
int Y_dim = weight_dims[2];
auto place = ctx.GetEigenDevice<Place>();

// Create the intermediate variable to caculate the result of
// Input(X) multiplied by Input(Weight_i), the formula is:
// left_mul = X Weight_i.
Tensor left_mul;
left_mul.mutable_data<T>(framework::make_ddim({batch_size, weight_dims[2]}),
left_mul.mutable_data<T>(framework::make_ddim({batch_size, Y_dim}),
ctx.GetPlace());
auto left_mul_mat = EigenMatrix<T>::From(left_mul);

for (size_t i = 0; i < weight_dims[0]; ++i) {
for (int i = 0; i < Out_dim; ++i) {
auto output_col_vec = output_mat.chip(i, 1);
Tensor weight_mat = weight->Slice(i, i + 1).Resize(
framework::make_ddim({weight_dims[1], weight_dims[2]}));
Tensor weight_mat =
weight->Slice(i, i + 1).Resize(framework::make_ddim({X_dim, Y_dim}));
math::gemm<Place, T>(ctx.device_context(), CblasNoTrans, CblasNoTrans,
batch_size, weight_dims[2], weight_dims[1], 1,
x->data<T>(), weight_mat.data<T>(), 0,
left_mul.data<T>());
batch_size, Y_dim, X_dim, 1, x->data<T>(),
weight_mat.data<T>(), 0, left_mul.data<T>());
output_col_vec.device(place) =
(left_mul_mat * y_mat).sum(Eigen::DSizes<int, 1>(1));
}
Expand All @@ -87,6 +89,9 @@ class BilinearTensorProductGradKernel : public framework::OpKernel<T> {

auto batch_size = x->dims()[0];
auto weight_dims = weight->dims();
int Out_dim = weight_dims[0];
int X_dim = weight_dims[1];
int Y_dim = weight_dims[2];

auto x_mat = EigenMatrix<T>::From(*x);
auto y_mat = EigenMatrix<T>::From(*y);
Expand All @@ -95,13 +100,13 @@ class BilinearTensorProductGradKernel : public framework::OpKernel<T> {

// Create the intermediate variable to caculate the Output(Y@Grad).
Tensor x_scale;
x_scale.mutable_data<T>(framework::make_ddim({batch_size, weight_dims[1]}),
x_scale.mutable_data<T>(framework::make_ddim({batch_size, X_dim}),
ctx.GetPlace());
auto x_scale_mat = EigenMatrix<T>::From(x_scale);

// Create the intermediate variable to caculate the Output(X@Grad).
Tensor y_scale;
y_scale.mutable_data<T>(framework::make_ddim({batch_size, weight_dims[2]}),
y_scale.mutable_data<T>(framework::make_ddim({batch_size, Y_dim}),
ctx.GetPlace());
auto y_scale_mat = EigenMatrix<T>::From(y_scale);

Expand All @@ -121,51 +126,48 @@ class BilinearTensorProductGradKernel : public framework::OpKernel<T> {

// Caculate the Output(X@Grad) and Output(Y@Grad).
if (d_x || d_y) {
Eigen::DSizes<int, 2> bcast_for_x(1, weight_dims[2]);
Eigen::DSizes<int, 2> bcast_for_y(1, weight_dims[1]);
for (int i = 0; i < weight_dims[0]; ++i) {
Eigen::DSizes<int, 2> bcast_for_x(1, Y_dim);
Eigen::DSizes<int, 2> bcast_for_y(1, X_dim);
for (int i = 0; i < Out_dim; ++i) {
Tensor weight_i = weight->Slice(i, i + 1).Resize(
framework::make_ddim({weight_dims[1], weight_dims[2]}));
framework::make_ddim({X_dim, Y_dim}));
auto output_vec = d_out_mat.chip(i, 1);
if (d_x) {
Copy link
Contributor

@lcy-seso lcy-seso Nov 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以 dx 为例 ,dy相同,
$dx = \frac{\partial{\mathcal{L}}}{\partial{Z}}WY^T$ 其中乘以 \partial{\mathcal{L}}}{\partial{Z} 是一个broadcast 的 “scaling” 运算。

为什么不可以在 135 ~ 138 之后再进行这个 “scaling” 运算呢?这样是不是就可以直接去掉 x_scalex_scale 这样两个中间变量(也避免分配内存的问题)。

不知是否可行。因为这个 "scaling" 操作从计算的逻辑上是可以 “原地” 运算。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里由于broadcast是在batch的方向展开,且TMP = scaled(X) W,scaled(X)中每一行元素所乘的放缩系数不同,所以无法在矩阵乘法之后做scaling计算。即scaled(X) W != scaled(X W).

y_scale_mat.device(place) =
output_vec.reshape(Eigen::DSizes<int, 2>(batch_size, 1))
.broadcast(bcast_for_x) *
y_mat;
math::gemm<Place, T>(ctx.device_context(), CblasNoTrans, CblasTrans,
batch_size, weight_dims[1], weight_dims[2], 1,
y_scale.data<T>(), weight_i.data<T>(), 1,
d_x->data<T>());
batch_size, X_dim, Y_dim, 1, y_scale.data<T>(),
weight_i.data<T>(), 1, d_x->data<T>());
}
if (d_y) {
x_scale_mat.device(place) =
output_vec.reshape(Eigen::DSizes<int, 2>(batch_size, 1))
.broadcast(bcast_for_y) *
x_mat;
math::gemm<Place, T>(ctx.device_context(), CblasNoTrans, CblasNoTrans,
batch_size, weight_dims[2], weight_dims[1], 1,
x_scale.data<T>(), weight_i.data<T>(), 1,
d_y->data<T>());
batch_size, Y_dim, X_dim, 1, x_scale.data<T>(),
weight_i.data<T>(), 1, d_y->data<T>());
}
}
}

// Caculate the gradient of Input(Weight).
if (d_weight) {
d_weight->mutable_data<T>(ctx.GetPlace());
Eigen::DSizes<int, 2> bcast_for_weight(1, weight_dims[1]);
for (int i = 0; i < weight_dims[0]; ++i) {
Eigen::DSizes<int, 2> bcast_for_weight(1, X_dim);
for (int i = 0; i < Out_dim; ++i) {
Tensor d_weight_i = d_weight->Slice(i, i + 1).Resize(
framework::make_ddim({weight_dims[1], weight_dims[2]}));
framework::make_ddim({X_dim, Y_dim}));
auto output_vec = d_out_mat.chip(i, 1);
x_scale_mat.device(place) =
output_vec.reshape(Eigen::DSizes<int, 2>(batch_size, 1))
.broadcast(bcast_for_weight) *
x_mat;
math::gemm<Place, T>(ctx.device_context(), CblasTrans, CblasNoTrans,
weight_dims[1], weight_dims[2], batch_size, 1,
x_scale.data<T>(), y->data<T>(), 0,
d_weight_i.data<T>());
X_dim, Y_dim, batch_size, 1, x_scale.data<T>(),
y->data<T>(), 0, d_weight_i.data<T>());
}
}

Expand Down