lstmp_op.h 21.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Y
Yibing Liu 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8 9 10 11 12 13 14 15

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
16
#include <string>
17
#include <vector>
Y
Yu Yang 已提交
18 19
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
Y
Yi Wang 已提交
20
#include "paddle/fluid/operators/activation_op.h"
Y
Yu Yang 已提交
21
#include "paddle/fluid/operators/math/blas.h"
Y
Yi Wang 已提交
22 23 24
#include "paddle/fluid/operators/math/detail/activation_functions.h"
#include "paddle/fluid/operators/math/lstm_compute.h"
#include "paddle/fluid/operators/math/sequence2batch.h"
25
#include "paddle/fluid/platform/transform.h"
26 27 28 29 30 31

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;
using Tensor = framework::Tensor;
32
using platform::Transform;
33

34 35 36 37
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
template <typename T>
class _ClipFunctor {
 public:
  explicit _ClipFunctor(const T min, const T max) : min_(min), max_(max) {}
  HOSTDEVICE T operator()(const T& x) const {
    if (x < min_)
      return min_;
    else if (x > max_)
      return max_;
    else
      return x;
  }

 private:
  T min_;
  T max_;
};

template <typename T>
class _ClipGradFunctor {
 public:
  explicit _ClipGradFunctor(const T min, const T max) : min_(min), max_(max) {}
  HOSTDEVICE T operator()(const T& x, const T& y) const {
    return (y > min_ && y < max_) ? x : 0;
  }

 private:
  T min_;
  T max_;
};

69 70
template <typename DeviceContext, typename T>
inline void ReorderInitState(const DeviceContext& ctx,
D
dzhwinter 已提交
71 72
                             const framework::Tensor& src,
                             framework::Vector<size_t> index,
73 74 75
                             framework::Tensor* dst, bool indexed_src) {
  math::CopyMatrixRowsFunctor<DeviceContext, T> row_shuffle;
  dst->mutable_data<T>(src.dims(), ctx.GetPlace());
76
  row_shuffle(ctx, src, index, dst, indexed_src);
77 78 79 80 81
}

template <typename DeviceContext, typename T>
class LSTMPKernel : public framework::OpKernel<T> {
 public:
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  template <typename Device, typename X, typename Y>
  void ActCompute(const math::detail::ActivationType act_type, const Device& d,
                  X x, Y y) const {
    if (act_type == math::detail::ActivationType::kIdentity)
      y.device(d) = x;
    else if (act_type == math::detail::ActivationType::kSigmoid)
      SigmoidFunctor<T>()(d, x, y);
    else if (act_type == math::detail::ActivationType::kTanh)
      TanhFunctor<T>()(d, x, y);
    else if (act_type == math::detail::ActivationType::kReLU)
      ReluFunctor<T>()(d, x, y);
    else
      PADDLE_THROW("unsupported activation type");
  }

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  void Print(const Tensor& t, std::string name) const {
    VLOG(1) << name << "size = " << t.numel();
    size_t size = t.numel();
    T* d = t.data<T>();
#ifdef PADDLE_WITH_CUDA
    std::vector<T> vec;
    platform::DeviceContextPool::Instance().Get(t.place())->Wait();
    if (platform::is_gpu_place(t.place())) {
      vec.resize(size);
      cudaMemcpy(vec.data(), d, sizeof(T) * size, cudaMemcpyDeviceToHost);
      d = vec.data();
    }
#endif
    VLOG(1) << name << " data_ptr = " << static_cast<void*>(d);
    for (size_t i = 0; i < size; i++) {
      VLOG(1) << d[i] << ",";
    }
  }

116 117 118 119 120 121 122 123 124
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<LoDTensor>("Input");
    auto* weight = ctx.Input<Tensor>("Weight");
    auto* proj_weight = ctx.Input<Tensor>("ProjWeight");
    auto* bias = ctx.Input<Tensor>("Bias");

    auto* hidden_t0 = ctx.Input<Tensor>("H0");
    auto* cell_t0 = ctx.Input<Tensor>("C0");

125 126 127
    auto proj_clip = static_cast<T>(ctx.Attr<float>("proj_clip"));
    auto cell_clip = static_cast<T>(ctx.Attr<float>("cell_clip"));

128 129 130 131 132 133 134 135 136 137
    auto* batch_gate = ctx.Output<LoDTensor>("BatchGate");
    batch_gate->mutable_data<T>(ctx.GetPlace());
    auto* proj_out = ctx.Output<LoDTensor>("Projection");
    proj_out->mutable_data<T>(ctx.GetPlace());
    auto* cell_out = ctx.Output<LoDTensor>("Cell");
    cell_out->mutable_data<T>(ctx.GetPlace());

    bool is_reverse = ctx.Attr<bool>("is_reverse");
    math::LoDTensor2BatchFunctor<DeviceContext, T> to_batch;
    auto& device_ctx = ctx.template device_context<DeviceContext>();
138
    to_batch(device_ctx, *input, batch_gate, true, is_reverse);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    auto in_dims = input->dims();
    int frame_size = static_cast<int>(in_dims[1] / 4);
    framework::DDim dims({in_dims[0], frame_size});
    framework::DDim proj_dims({in_dims[0], proj_weight->dims()[1]});

    if (bias) {
      Tensor b = *bias;
      b.Resize({bias->numel(), 1});
      Tensor gate_bias = b.Slice(0, 4 * frame_size);
      math::RowwiseAdd<DeviceContext, T> add_bias;
      add_bias(device_ctx, *batch_gate, gate_bias, batch_gate);
    }

    math::LstmMetaValue<T> lstmp_value;
    if (bias && ctx.Attr<bool>("use_peepholes")) {
      T* bias_data = const_cast<T*>(bias->data<T>());
      // the code style in LstmpMetaValue will be updated later.

      lstmp_value.check_ig = bias_data + 4 * frame_size;
      lstmp_value.check_fg = lstmp_value.check_ig + frame_size;
      lstmp_value.check_og = lstmp_value.check_fg + frame_size;
    } else {
      lstmp_value.check_ig = nullptr;
      lstmp_value.check_fg = nullptr;
      lstmp_value.check_og = nullptr;
    }
    lstmp_value.prev_state_value = nullptr;
    Tensor ordered_c0;
168
    Tensor ordered_h0;
D
dzhwinter 已提交
169 170 171

    framework::Vector<size_t> order(batch_gate->lod()[2]);

172 173 174 175 176 177 178 179 180 181
    if (cell_t0) {
      // Since the batch computing for LSTMP reorders the input sequence
      // according to their length. The initialized cell state also needs
      // to reorder.
      ReorderInitState<DeviceContext, T>(device_ctx, *cell_t0, order,
                                         &ordered_c0, true);
      lstmp_value.prev_state_value = ordered_c0.data<T>();
    }

    // Use the local variable as here.
182
    LoDTensor batch_proj, batch_cell;
183
    auto* batch_cell_pre_act = ctx.Output<LoDTensor>("BatchCellPreAct");
184 185 186
    batch_cell_pre_act->mutable_data<T>(dims, ctx.GetPlace());
    auto* batch_hidden = ctx.Output<LoDTensor>("BatchHidden");
    batch_hidden->mutable_data<T>(dims, ctx.GetPlace());    // T x D
187 188 189 190 191 192 193 194 195 196 197
    batch_proj.mutable_data<T>(proj_dims, ctx.GetPlace());  // T x P
    batch_cell.mutable_data<T>(dims, ctx.GetPlace());       // T x D

    auto batch_starts = batch_gate->lod()[0];
    size_t num_batch = batch_starts.size() - 1;
    auto gate_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("gate_activation"));
    auto cell_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("cell_activation"));
    auto cand_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("candidate_activation"));
198 199
    auto proj_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("proj_activation"));
200
    auto& place = *ctx.template device_context<DeviceContext>().eigen_device();
Y
Yu Yang 已提交
201
    auto blas = math::GetBlas<DeviceContext, T>(device_ctx);
202 203 204 205 206
    for (size_t n = 0; n < num_batch; n++) {
      int bstart = static_cast<int>(batch_starts[n]);
      int bend = static_cast<int>(batch_starts[n + 1]);

      Tensor gate_t = batch_gate->Slice(bstart, bend);
207
      Tensor hidden_t = batch_hidden->Slice(bstart, bend);
208 209 210 211 212 213 214 215 216 217
      Tensor proj_t = batch_proj.Slice(bstart, bend);
      Tensor cell_t = batch_cell.Slice(bstart, bend);
      Tensor cell_pre_act_t = batch_cell_pre_act->Slice(bstart, bend);

      int cur_batch_size = bend - bstart;

      if (n > 0) {
        int pre_h_start = static_cast<int>(batch_starts[n - 1]);
        int pre_h_end = pre_h_start + cur_batch_size;
        auto pre_proj_t = batch_proj.Slice(pre_h_start, pre_h_end);
Y
Yu Yang 已提交
218 219
        blas.MatMul(pre_proj_t, false, *weight, false, static_cast<T>(1.0),
                    &gate_t, static_cast<T>(1.0));
220 221 222 223 224 225 226 227
      } else if (hidden_t0) {
        // If n == 0 and there is no initialized hidden state, that is to say
        // the H0 is zeros, the calculation W_h * H0 will be skiped.
        // If n == 0 and there is initialized hidden state, calculate W_h * H0.

        // Since the batch computing for LSTMP reorders the input sequence
        // according to their length. The initialized hidden state also needs
        // to reorder.
228
        VLOG(1) << "qxz h0 used";
229 230
        ReorderInitState<DeviceContext, T>(device_ctx, *hidden_t0, order,
                                           &ordered_h0, true);
231
        blas.MatMul(ordered_h0, false, *weight, false, static_cast<T>(1.0),
Y
Yu Yang 已提交
232
                    &gate_t, static_cast<T>(1.0));
233 234 235 236 237 238 239
      }

      lstmp_value.gate_value = gate_t.data<T>();
      lstmp_value.output_value = hidden_t.data<T>();
      lstmp_value.state_value = cell_t.data<T>();
      lstmp_value.state_active_value = cell_pre_act_t.data<T>();
      math::LstmUnitFunctor<DeviceContext, T>::compute(
240 241
          device_ctx, lstmp_value, frame_size, cur_batch_size, cell_clip,
          gate_act, cell_act, cand_act);
242
      lstmp_value.prev_state_value = lstmp_value.state_value;
Y
Yu Yang 已提交
243 244
      blas.MatMul(hidden_t, false, *proj_weight, false, static_cast<T>(1.0),
                  &proj_t, static_cast<T>(0.0));
245
      if (proj_act != math::detail::ActivationType::kIdentity) {
246 247 248
        auto proj_t_dev = EigenMatrix<T>::From(proj_t);
        ActCompute(cell_act, place, proj_t_dev, proj_t_dev);
      }
249 250 251 252 253 254 255 256
      if (proj_clip && proj_clip > 0.0) {
        T* x_data = proj_t.data<T>();
        int64_t numel = proj_t.numel();
        Transform<DeviceContext> trans;
        trans(ctx.template device_context<DeviceContext>(), x_data,
              x_data + numel, x_data,
              _ClipFunctor<T>(-1.0 * proj_clip, proj_clip));
      }
257 258 259 260 261
    }

    math::Batch2LoDTensorFunctor<DeviceContext, T> to_seq;
    batch_proj.set_lod(batch_gate->lod());
    // restore the output hidden in LoDTensor from the batch hidden
262
    to_seq(device_ctx, batch_proj, proj_out);
263 264 265

    batch_cell.set_lod(batch_gate->lod());
    // restore the output cell state in LoDTensor from the batch cell
266
    to_seq(device_ctx, batch_cell, cell_out);
267 268 269 270 271 272
  }
};

template <typename DeviceContext, typename T>
class LSTMPGradKernel : public framework::OpKernel<T> {
 public:
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  template <typename Device, typename X, typename Y, typename DX, typename DY>
  void ActGradCompute(const math::detail::ActivationType act_type,
                      const Device& d, X x, Y y, DX dx, DY dy) const {
    // x is dummy and won't be used even in Relu(use y instead)
    if (act_type == math::detail::ActivationType::kIdentity)
      dx.device(d) = dy;
    else if (act_type == math::detail::ActivationType::kSigmoid)
      SigmoidGradFunctor<T>()(d, x, y, dy, dx);
    else if (act_type == math::detail::ActivationType::kTanh)
      TanhGradFunctor<T>()(d, x, y, dy, dx);
    else if (act_type == math::detail::ActivationType::kReLU)
      ReluGradFunctor<T>()(d, x, y, dy, dx);
    else
      PADDLE_THROW("unsupported activation type");
  }

289 290 291
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* input = ctx.Input<LoDTensor>("Input");
    auto* weight = ctx.Input<Tensor>("Weight");
292
    auto* proj_weight = ctx.Input<Tensor>("ProjWeight");
293 294 295 296 297
    auto* bias = ctx.Input<Tensor>("Bias");

    auto* proj_out = ctx.Input<LoDTensor>("Projection");
    auto* cell_out = ctx.Input<LoDTensor>("Cell");

298 299 300
    auto proj_clip = static_cast<T>(ctx.Attr<float>("proj_clip"));
    auto cell_clip = static_cast<T>(ctx.Attr<float>("cell_clip"));

301 302
    auto* batch_gate = ctx.Input<LoDTensor>("BatchGate");
    auto* batch_cell_pre_act = ctx.Input<LoDTensor>("BatchCellPreAct");
303
    auto* batch_hidden = ctx.Input<LoDTensor>("BatchHidden");
304

305 306
    auto* projection_g =
        ctx.Input<LoDTensor>(framework::GradVarName("Projection"));
307 308 309

    auto* in_g = ctx.Output<LoDTensor>(framework::GradVarName("Input"));
    auto* weight_g = ctx.Output<Tensor>(framework::GradVarName("Weight"));
310 311
    auto* proj_weight_g =
        ctx.Output<Tensor>(framework::GradVarName("ProjWeight"));
312 313 314 315 316 317 318 319 320 321 322 323 324 325
    auto* bias_g = ctx.Output<Tensor>(framework::GradVarName("Bias"));

    auto* h0 = ctx.Input<Tensor>("H0");
    auto* c0 = ctx.Input<Tensor>("C0");

    auto* h0_g = ctx.Output<Tensor>(framework::GradVarName("H0"));
    auto* c0_g = ctx.Output<Tensor>(framework::GradVarName("C0"));

    auto& device_ctx = ctx.template device_context<DeviceContext>();
    math::SetConstant<DeviceContext, T> zero;
    if (weight_g) {
      weight_g->mutable_data<T>(ctx.GetPlace());
      zero(device_ctx, weight_g, static_cast<T>(0.0));
    }
326 327 328 329
    if (proj_weight_g) {
      proj_weight_g->mutable_data<T>(ctx.GetPlace());
      zero(device_ctx, proj_weight_g, static_cast<T>(0.0));
    }
330 331 332 333 334

    // ordered_h0/c0 is the reordered hidden/cell initialization.
    // ordered_h0_g/c0_g is the reordered gradient of hidden/cell
    // initialization.
    Tensor ordered_h0, ordered_c0, ordered_h0_g, ordered_c0_g;
D
dzhwinter 已提交
335 336 337

    framework::Vector<size_t> order(batch_gate->lod()[2]);

338 339 340 341 342 343 344 345 346
    if (c0) {
      ReorderInitState<DeviceContext, T>(device_ctx, *c0, order, &ordered_c0,
                                         true);
    }
    if (c0 && c0_g) {
      ordered_c0_g.mutable_data<T>(c0_g->dims(), ctx.GetPlace());
    }

    auto in_dims = input->dims();
347 348
    auto out_dims = cell_out->dims();
    framework::DDim proj_dims({in_dims[0], proj_weight->dims()[1]});
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    int frame_size = static_cast<int>(in_dims[1] / 4);
    PADDLE_ENFORCE_EQ(frame_size, out_dims[1]);

    math::LstmMetaValue<T> lstmp_value;
    if (bias && ctx.Attr<bool>("use_peepholes")) {
      T* bias_data = const_cast<T*>(bias->data<T>());
      lstmp_value.check_ig = bias_data + 4 * frame_size;
      lstmp_value.check_fg = lstmp_value.check_ig + frame_size;
      lstmp_value.check_og = lstmp_value.check_fg + frame_size;
    } else {
      lstmp_value.check_ig = nullptr;
      lstmp_value.check_fg = nullptr;
      lstmp_value.check_og = nullptr;
    }

    math::LstmMetaGrad<T> lstmp_grad;

    if (bias && bias_g) {
      bias_g->mutable_data<T>(ctx.GetPlace());
      zero(device_ctx, bias_g, static_cast<T>(0.0));
    }
    if (bias && bias_g && ctx.Attr<bool>("use_peepholes")) {
      T* bias_g_data = bias_g->data<T>();
      lstmp_grad.check_ig_grad = bias_g_data + 4 * frame_size;
      lstmp_grad.check_fg_grad = lstmp_grad.check_ig_grad + frame_size;
      lstmp_grad.check_og_grad = lstmp_grad.check_fg_grad + frame_size;
    } else {
      lstmp_grad.check_ig_grad = nullptr;
      lstmp_grad.check_fg_grad = nullptr;
      lstmp_grad.check_og_grad = nullptr;
    }

    math::LoDTensor2BatchFunctor<DeviceContext, T> to_batch;

    auto ToBatch = [&batch_gate, &to_batch](
        const DeviceContext& ctx, const framework::LoDTensor& src,
        const framework::DDim& dims, framework::LoDTensor& dst) {
      dst.mutable_data<T>(dims, ctx.GetPlace());
      dst.set_lod(batch_gate->lod());
388
      to_batch(ctx, src, &dst, false);
389 390
    };

391 392 393 394 395
    LoDTensor batch_hidden_g, batch_proj, batch_proj_g, batch_cell;
    batch_hidden_g.mutable_data<T>(out_dims, ctx.GetPlace());
    ToBatch(device_ctx, *proj_out, proj_dims, batch_proj);        // T x P
    ToBatch(device_ctx, *projection_g, proj_dims, batch_proj_g);  // T x P
    ToBatch(device_ctx, *cell_out, out_dims, batch_cell);         // T x D
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

    LoDTensor batch_cell_g, batch_gate_g;
    batch_cell_g.mutable_data<T>(out_dims, ctx.GetPlace());
    // TODO(qingqing) support the case output cell has gradient.
    // to_batch(device_ctx, *cell_g, batch_cell_g, false);
    zero(device_ctx, &batch_cell_g, static_cast<T>(0.0));
    batch_gate_g.mutable_data<T>(batch_gate->dims(), ctx.GetPlace());
    batch_gate_g.set_lod(batch_gate->lod());

    auto gate_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("gate_activation"));
    auto cell_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("cell_activation"));
    auto cand_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("candidate_activation"));
411 412
    auto proj_act = math::detail::GetActivationType(
        ctx.Attr<std::string>("proj_activation"));
413
    auto& place = *ctx.template device_context<DeviceContext>().eigen_device();
414 415 416

    auto batch_starts = batch_gate->lod()[0];
    size_t num_batch = batch_starts.size() - 1;
Y
Yu Yang 已提交
417
    auto blas = math::GetBlas<DeviceContext, T>(device_ctx);
418 419 420 421
    for (int n = static_cast<int>(num_batch) - 1; n >= 0; n--) {
      int bstart = static_cast<int>(batch_starts[n]);
      int bend = static_cast<int>(batch_starts[n + 1]);

422 423
      Tensor cur_proj = batch_proj.Slice(bstart, bend);
      Tensor proj_g = batch_proj_g.Slice(bstart, bend);
424 425 426 427 428 429 430 431 432 433 434

      if (proj_clip && proj_clip > 0.0) {
        T* dx_data = proj_g.data<T>();
        T* x_data = cur_proj.data<T>();
        int64_t numel = proj_g.numel();
        Transform<DeviceContext> trans;
        trans(ctx.template device_context<DeviceContext>(), dx_data,
              dx_data + numel, x_data, dx_data,
              _ClipGradFunctor<T>(-1.0 * proj_clip, proj_clip));
      }

435
      if (proj_act != math::detail::ActivationType::kIdentity) {
436 437 438 439 440
        auto cur_proj_dev = EigenMatrix<T>::From(cur_proj);
        auto proj_g_dev = EigenMatrix<T>::From(proj_g);
        ActGradCompute(cell_act, place, cur_proj_dev, cur_proj_dev, proj_g_dev,
                       proj_g_dev);
      }
441
      /* hidden state backwarad */
442
      Tensor out_g = batch_hidden_g.Slice(bstart, bend);
Y
Yu Yang 已提交
443 444
      blas.MatMul(proj_g, false, *proj_weight, true, static_cast<T>(1.0),
                  &out_g, static_cast<T>(0.0));
445 446 447
      /* projection weight backward*/
      if (proj_weight_g) {
        Tensor hidden_t = batch_hidden->Slice(bstart, bend);
Y
Yu Yang 已提交
448 449
        blas.MatMul(hidden_t, true, proj_g, false, static_cast<T>(1.0),
                    proj_weight_g, static_cast<T>(1.0));
450
      }
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
      Tensor gate = batch_gate->Slice(bstart, bend);
      Tensor cell = batch_cell.Slice(bstart, bend);
      Tensor cell_pre_act = batch_cell_pre_act->Slice(bstart, bend);
      lstmp_value.gate_value = gate.data<T>();
      lstmp_value.state_value = cell.data<T>();
      lstmp_value.state_active_value = cell_pre_act.data<T>();

      Tensor gate_g = batch_gate_g.Slice(bstart, bend);
      Tensor cell_g = batch_cell_g.Slice(bstart, bend);
      lstmp_grad.state_grad = cell_g.data<T>();
      lstmp_grad.gate_grad = gate_g.data<T>();
      lstmp_grad.output_grad = out_g.data<T>();

      if (n > 0) {
        int bstart_pre = static_cast<int>(batch_starts[n - 1]);
        Tensor cell_pre = batch_cell.Slice(bstart_pre, bstart);
        Tensor cell_pre_g = batch_cell_g.Slice(bstart_pre, bstart);
        lstmp_value.prev_state_value = cell_pre.data<T>();
        lstmp_grad.prev_state_grad = cell_pre_g.data<T>();
      } else {
        lstmp_value.prev_state_value = c0 ? ordered_c0.data<T>() : nullptr;
        lstmp_grad.prev_state_grad = c0_g ? ordered_c0_g.data<T>() : nullptr;
      }

      int cur_batch_size = bend - bstart;
      math::LstmUnitGradFunctor<DeviceContext, T>::compute(
          device_ctx, lstmp_value, lstmp_grad, frame_size, cur_batch_size,
479
          cell_clip, gate_act, cell_act, cand_act);
480 481 482 483 484

      if (n > 0) {
        int pre_h_start = static_cast<int>(batch_starts[n - 1]);
        int pre_h_end = pre_h_start + cur_batch_size;
        auto pre_proj_g = batch_proj_g.Slice(pre_h_start, pre_h_end);
Y
Yu Yang 已提交
485 486
        blas.MatMul(gate_g, false, *weight, true, static_cast<T>(1.0),
                    &pre_proj_g, static_cast<T>(1.0));
487
        if (weight_g) {
488
          /* weight backward*/
489
          auto pre_proj = batch_proj.Slice(pre_h_start, pre_h_end);
Y
Yu Yang 已提交
490 491
          blas.MatMul(pre_proj, true, gate_g, false, static_cast<T>(1.0),
                      weight_g, static_cast<T>(1.0));
492 493 494 495 496
        }
      } else {
        if (h0 && weight_g) {
          ReorderInitState<DeviceContext, T>(device_ctx, *h0, order,
                                             &ordered_h0, true);
497
          if (weight_g) {
498 499
            blas.MatMul(ordered_h0, true, gate_g, false, static_cast<T>(1.0),
                        weight_g, static_cast<T>(1.0));
500
          }
501
        }
502
        if (h0 && (h0_g || proj_weight_g)) {
503
          ordered_h0_g.mutable_data<T>(h0_g->dims(), ctx.GetPlace());
Y
Yu Yang 已提交
504
          blas.MatMul(gate_g, false, *weight, true, static_cast<T>(1.0),
505
                      &ordered_h0_g, static_cast<T>(0.0));
506 507 508 509 510 511 512 513
        }
      }
    }

    math::Batch2LoDTensorFunctor<DeviceContext, T> to_seq;
    if (in_g) {
      /* backward data */
      in_g->mutable_data<T>(ctx.GetPlace());
514
      to_seq(device_ctx, batch_gate_g, in_g);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    }
    if (bias && bias_g) {
      /* backward bias */
      Tensor b_g = *bias_g;
      b_g.Resize({bias_g->numel(), 1});
      Tensor gate_bias_g = b_g.Slice(0, 4 * frame_size);
      math::ColwiseSum<DeviceContext, T> col_sum;
      col_sum(device_ctx, batch_gate_g, &gate_bias_g);
    }

    if (h0 && h0_g) {
      ReorderInitState<DeviceContext, T>(device_ctx, ordered_h0_g, order, h0_g,
                                         false);
    }
    if (c0 && c0_g) {
      ReorderInitState<DeviceContext, T>(device_ctx, ordered_c0_g, order, c0_g,
                                         false);
    }
  }
};

}  // namespace operators
}  // namespace paddle