fused_seqpool_cvm_op.cu 24.3 KB
Newer Older
D
danleifeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

#include <string>
16

D
danleifeng 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "paddle/fluid/framework/mixed_vector.h"
#include "paddle/fluid/operators/fused/fused_seqpool_cvm_op.h"
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
#include "paddle/fluid/platform/device/gpu/gpu_launch_config.h"

namespace paddle {
namespace operators {

template <typename T>
using Vector = framework::Vector<T>;

#define CUDA_KERNEL_LOOP(i, n)                                  \
  for (auto i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \
       i += blockDim.x * gridDim.x)

// normal
template <typename T>
34 35
__global__ void FusedSeqpoolKernelNormal(const size_t N,
                                         T **input_values,
D
danleifeng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
                                         T **seqpool_output_values,
                                         size_t **lods_values,
                                         const int batch_size,
                                         const int embedding_size,
                                         const float pad_value) {
  CUDA_KERNEL_LOOP(i, N) {
    int key = i / embedding_size;
    int offset = i % embedding_size;
    int x = key / batch_size;  // slot id
    int y = key % batch_size;  // ins id
    auto &start = *(lods_values[x] + y);
    auto &end = *(lods_values[x] + y + 1);

    T val = static_cast<T>(pad_value);
    for (auto k = start; k < end; ++k) {
      val += *(input_values[x] + k * embedding_size + offset);
    }
    *(seqpool_output_values[x] + y * embedding_size + offset) = val;
  }
}

// join need show click input
template <typename T>
59 60
__global__ void FusedCVMKernelWithCVM(const size_t N,
                                      T **output_values,
D
danleifeng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                                      T **seqpool_output_values,
                                      const int batch_size,
                                      const int embedding_size,
                                      const int cvm_offset) {
  CUDA_KERNEL_LOOP(i, N) {
    int key = i / embedding_size;
    int offset = i % embedding_size;
    int x = key / batch_size;  // slot id
    int y = key % batch_size;  // ins id
    if (offset == 0) {         // show
      *(output_values[x] + y * embedding_size) =
          log(*(seqpool_output_values[x] + y * embedding_size) + 1);
    } else if (offset == 1) {  // click
      *(output_values[x] + y * embedding_size + offset) =
          log(*(seqpool_output_values[x] + y * embedding_size + 1) + 1) -
          log(*(seqpool_output_values[x] + y * embedding_size) + 1);
    } else {
      *(output_values[x] + y * embedding_size + offset) =
          *(seqpool_output_values[x] + y * embedding_size + offset);
    }
  }
}

// update not need show click input
template <typename T>
86 87
__global__ void FusedCVMKernelNoCVM(const size_t N,
                                    T **output_values,
D
danleifeng 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
                                    T **seqpool_output_values,
                                    const int batch_size,
                                    const int no_cvm_embedding_size,
                                    const int cvm_offset) {
  CUDA_KERNEL_LOOP(i, N) {
    int key = i / no_cvm_embedding_size;
    int offset = i % no_cvm_embedding_size;
    int x = key / batch_size;  // slot id
    int y = key % batch_size;  // ins id
    // no cvm
    *(output_values[x] + y * no_cvm_embedding_size + offset) =
        *(seqpool_output_values[x] + y * (no_cvm_embedding_size + cvm_offset) +
          offset + cvm_offset);
  }
}

template <typename T>
void FusedSeqpoolCVM(const framework::ExecutionContext
                         &ctx,  // const paddle::platform::Place &place,
                     const std::vector<const T *> &input_data,
                     const std::vector<T *> &output_data,
                     const std::vector<T *> &seqpool_output_data,
110 111 112 113 114 115
                     std::vector<const size_t *> lods,
                     const int batch_size,
                     const int slot_num,
                     const int embedding_size,
                     const float padding_value,
                     const bool use_cvm,
D
danleifeng 已提交
116
                     const int cvm_offset) {
L
Leo Chen 已提交
117 118
  auto stream = ctx.template device_context<phi::GPUContext>().stream();
  auto &dev_ctx = ctx.template device_context<phi::GPUContext>();
D
danleifeng 已提交
119 120 121 122 123 124 125 126
  size_t total_ptr_len = input_data.size() + output_data.size() +
                         seqpool_output_data.size() + lods.size();
  auto temp_ptr =
      memory::AllocShared(ctx.GetPlace(), total_ptr_len * sizeof(void *));
  void *ptr = temp_ptr->ptr();

#ifdef PADDLE_WITH_HIP
  T **gpu_input_values = reinterpret_cast<T **>(temp_ptr->ptr());
127 128
  platform::GpuMemcpyAsync(gpu_input_values,
                           input_data.data(),
D
danleifeng 已提交
129
                           input_data.size() * sizeof(T *),
130 131
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
132 133
  T **gpu_output_values =
      reinterpret_cast<T **>(&gpu_input_values[input_data.size()]);
134 135
  platform::GpuMemcpyAsync(gpu_output_values,
                           output_data.data(),
D
danleifeng 已提交
136
                           output_data.size() * sizeof(T *),
137 138
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
139 140
  T **gpu_seqpool_output_values =
      reinterpret_cast<T **>(&gpu_output_values[output_data.size()]);
141 142 143 144 145
  platform::GpuMemcpyAsync(gpu_seqpool_output_values,
                           seqpool_output_data.data(),
                           seqpool_output_data.size() * sizeof(T *),
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
146 147
  size_t **lods_values = reinterpret_cast<size_t **>(
      &gpu_seqpool_output_values[seqpool_output_data.size()]);
148 149
  platform::GpuMemcpyAsync(lods_values,
                           lods.data(),
D
danleifeng 已提交
150
                           lods.size() * sizeof(size_t *),
151 152
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
153 154
#else
  T **gpu_input_values = reinterpret_cast<T **>(temp_ptr->ptr());
155 156
  platform::GpuMemcpyAsync(gpu_input_values,
                           input_data.data(),
D
danleifeng 已提交
157
                           input_data.size() * sizeof(T *),
158 159
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
160 161
  T **gpu_output_values =
      reinterpret_cast<T **>(&gpu_input_values[input_data.size()]);
162 163
  platform::GpuMemcpyAsync(gpu_output_values,
                           output_data.data(),
D
danleifeng 已提交
164
                           output_data.size() * sizeof(T *),
165 166
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
167 168
  T **gpu_seqpool_output_values =
      reinterpret_cast<T **>(&gpu_output_values[output_data.size()]);
169 170 171 172 173
  platform::GpuMemcpyAsync(gpu_seqpool_output_values,
                           seqpool_output_data.data(),
                           seqpool_output_data.size() * sizeof(T *),
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
174 175
  size_t **lods_values = reinterpret_cast<size_t **>(
      &gpu_seqpool_output_values[seqpool_output_data.size()]);
176 177
  platform::GpuMemcpyAsync(lods_values,
                           lods.data(),
D
danleifeng 已提交
178
                           lods.size() * sizeof(size_t *),
179 180
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
181 182 183
#endif

  size_t N = static_cast<size_t>(batch_size * slot_num * embedding_size);
184
  platform::GpuLaunchConfig config = platform::GetGpuLaunchConfig1D(dev_ctx, N);
D
danleifeng 已提交
185
  // first sum pool
186 187 188 189 190 191 192 193 194 195
  FusedSeqpoolKernelNormal<<<config.block_per_grid.x,
                             config.thread_per_block.x,
                             0,
                             stream>>>(N,
                                       gpu_input_values,
                                       gpu_seqpool_output_values,
                                       lods_values,
                                       batch_size,
                                       embedding_size,
                                       padding_value);
D
danleifeng 已提交
196 197
  // second log
  if (use_cvm) {
198 199 200 201 202 203 204 205 206
    FusedCVMKernelWithCVM<<<config.block_per_grid.x,
                            config.thread_per_block.x,
                            0,
                            stream>>>(N,
                                      gpu_output_values,
                                      gpu_seqpool_output_values,
                                      batch_size,
                                      embedding_size,
                                      cvm_offset);
D
danleifeng 已提交
207 208 209 210
  } else {
    // not need show click input
    N = static_cast<size_t>(batch_size * slot_num *
                            (embedding_size - cvm_offset));
211 212
    platform::GpuLaunchConfig config =
        platform::GetGpuLaunchConfig1D(dev_ctx, N);
213 214 215 216 217 218 219 220 221
    FusedCVMKernelNoCVM<<<config.block_per_grid.x,
                          config.thread_per_block.x,
                          0,
                          stream>>>(N,
                                    gpu_output_values,
                                    gpu_seqpool_output_values,
                                    batch_size,
                                    (embedding_size - cvm_offset),
                                    cvm_offset);
D
danleifeng 已提交
222 223 224 225 226
  }
}

// join grad
template <typename T>
227 228 229 230 231 232 233 234
__global__ void FusedSeqpoolCVMGradKernelWithCVM(const size_t N,
                                                 T **out_grads_values,
                                                 T **in_grads_values,
                                                 T **cvm_values,
                                                 size_t **lods_values,
                                                 const int batch_size,
                                                 const int embedding_size,
                                                 const int cvm_offset) {
D
danleifeng 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  CUDA_KERNEL_LOOP(i, N) {
    int key = i / embedding_size;
    int offset = i % embedding_size;  // embedx offset
    int x = key / batch_size;         // slot id
    int y = key % batch_size;         // ins id

    T &val = (offset < cvm_offset)
                 ? *(cvm_values[x] + y * cvm_offset + offset)
                 : *(out_grads_values[x] + y * embedding_size + offset);

    auto &start = *(lods_values[x] + y);
    auto &end = *(lods_values[x] + y + 1);
    for (auto k = start; k < end; ++k) {
      *(in_grads_values[x] + k * embedding_size + offset) = val;
    }
  }
}

// join only show not has click
template <typename T>
255 256 257 258 259 260 261 262
__global__ void FusedSeqpoolCVMGradKernelWithShow(const size_t N,
                                                  T **out_grads_values,
                                                  T **in_grads_values,
                                                  T **cvm_values,
                                                  size_t **lods_values,
                                                  const int batch_size,
                                                  const int embedding_size,
                                                  const int cvm_offset) {
D
danleifeng 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  CUDA_KERNEL_LOOP(i, N) {
    int key = i / embedding_size;
    int offset = i % embedding_size;  // embedx offset
    int x = key / batch_size;         // slot id
    int y = key % batch_size;         // ins id

    T &val =
        (offset < cvm_offset)
            ? *(cvm_values[x] + y * cvm_offset + offset)
            : *(out_grads_values[x] + y * (embedding_size - 1) + offset - 1);

    auto &start = *(lods_values[x] + y);
    auto &end = *(lods_values[x] + y + 1);
    for (auto k = start; k < end; ++k) {
      *(in_grads_values[x] + k * embedding_size + offset) = val;
    }
  }
}

// update grad
template <typename T>
284 285 286 287 288 289 290 291
__global__ void FusedSeqpoolCVMGradKernelNoCVM(const size_t N,
                                               T **out_grads_values,
                                               T **in_grads_values,
                                               T **cvm_values,
                                               size_t **lods_values,
                                               const int batch_size,
                                               const int embedding_size,
                                               const int cvm_offset) {
D
danleifeng 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  CUDA_KERNEL_LOOP(i, N) {
    int key = i / embedding_size;
    int offset = i % embedding_size;  // embedx offset
    int x = key / batch_size;         // slot id
    int y = key % batch_size;         // ins id

    T &val = (offset < cvm_offset)
                 ? *(cvm_values[x] + y * cvm_offset + offset)
                 : *(out_grads_values[x] + y * (embedding_size - cvm_offset) +
                     offset - cvm_offset);

    auto &start = *(lods_values[x] + y);
    auto &end = *(lods_values[x] + y + 1);
    for (auto k = start; k < end; ++k) {
      *(in_grads_values[x] + k * embedding_size + offset) = val;
    }
  }
}

template <typename T>
void FusedSeqpoolCVMGrad(const framework::ExecutionContext &ctx,
                         const std::vector<const T *> &out_grads_data,
                         const std::vector<T *> &in_grads_data,
                         const std::vector<const T *> &cvm_data,
                         const std::vector<const size_t *> &lods,
317 318 319 320
                         const int batch_size,
                         const int slot_num,
                         const int embedding_size,
                         const bool use_cvm,
D
danleifeng 已提交
321
                         const int cvm_offset) {
L
Leo Chen 已提交
322 323
  auto stream = ctx.template device_context<phi::GPUContext>().stream();
  auto &dev_ctx = ctx.template device_context<phi::GPUContext>();
D
danleifeng 已提交
324 325 326 327 328 329
  size_t total_ptr_len = out_grads_data.size() + in_grads_data.size() +
                         cvm_data.size() + lods.size();
  auto temp_ptr =
      memory::AllocShared(ctx.GetPlace(), total_ptr_len * sizeof(void *));
#ifdef PADDLE_WITH_HIP
  T **gpu_out_grads_values = reinterpret_cast<T **>(temp_ptr->ptr());
330 331
  platform::GpuMemcpyAsync(gpu_out_grads_values,
                           out_grads_data.data(),
D
danleifeng 已提交
332
                           out_grads_data.size() * sizeof(T *),
333 334
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
335 336 337

  T **gpu_in_grads_values =
      reinterpret_cast<T **>(&gpu_out_grads_values[out_grads_data.size()]);
338 339
  platform::GpuMemcpyAsync(gpu_in_grads_values,
                           in_grads_data.data(),
D
danleifeng 已提交
340
                           in_grads_data.size() * sizeof(T *),
341 342
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
343 344 345

  T **gpu_cvm_values =
      reinterpret_cast<T **>(&gpu_in_grads_values[in_grads_data.size()]);
346 347 348 349
  platform::GpuMemcpyAsync(gpu_cvm_values,
                           cvm_data.data(),
                           cvm_data.size() * sizeof(T *),
                           hipMemcpyHostToDevice,
D
danleifeng 已提交
350 351 352 353
                           stream);

  size_t **lods_values =
      reinterpret_cast<size_t **>(&gpu_cvm_values[cvm_data.size()]);
354 355
  platform::GpuMemcpyAsync(lods_values,
                           lods.data(),
D
danleifeng 已提交
356
                           lods.size() * sizeof(size_t *),
357 358
                           hipMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
359 360
#else
  T **gpu_out_grads_values = reinterpret_cast<T **>(temp_ptr->ptr());
361 362
  platform::GpuMemcpyAsync(gpu_out_grads_values,
                           out_grads_data.data(),
D
danleifeng 已提交
363
                           out_grads_data.size() * sizeof(T *),
364 365
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
366 367 368

  T **gpu_in_grads_values =
      reinterpret_cast<T **>(&gpu_out_grads_values[out_grads_data.size()]);
369 370
  platform::GpuMemcpyAsync(gpu_in_grads_values,
                           in_grads_data.data(),
D
danleifeng 已提交
371
                           in_grads_data.size() * sizeof(T *),
372 373
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
374 375 376

  T **gpu_cvm_values =
      reinterpret_cast<T **>(&gpu_in_grads_values[in_grads_data.size()]);
377 378
  platform::GpuMemcpyAsync(gpu_cvm_values,
                           cvm_data.data(),
D
danleifeng 已提交
379
                           cvm_data.size() * sizeof(T *),
380 381
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
382 383 384

  size_t **lods_values =
      reinterpret_cast<size_t **>(&gpu_cvm_values[cvm_data.size()]);
385 386
  platform::GpuMemcpyAsync(lods_values,
                           lods.data(),
D
danleifeng 已提交
387
                           lods.size() * sizeof(size_t *),
388 389
                           cudaMemcpyHostToDevice,
                           stream);
D
danleifeng 已提交
390 391 392
#endif

  size_t N = static_cast<size_t>(batch_size * slot_num * embedding_size);
393
  auto config = platform::GetGpuLaunchConfig1D(dev_ctx, N);
D
danleifeng 已提交
394 395 396
  if (use_cvm) {
    // join grad
    FusedSeqpoolCVMGradKernelWithCVM<<<config.block_per_grid.x,
397 398 399 400 401 402 403 404 405 406
                                       config.thread_per_block.x,
                                       0,
                                       stream>>>(N,
                                                 gpu_out_grads_values,
                                                 gpu_in_grads_values,
                                                 gpu_cvm_values,
                                                 lods_values,
                                                 batch_size,
                                                 embedding_size,
                                                 cvm_offset);
D
danleifeng 已提交
407 408 409
  } else {
    // update grad
    FusedSeqpoolCVMGradKernelNoCVM<<<config.block_per_grid.x,
410 411 412 413 414 415 416 417 418 419
                                     config.thread_per_block.x,
                                     0,
                                     stream>>>(N,
                                               gpu_out_grads_values,
                                               gpu_in_grads_values,
                                               gpu_cvm_values,
                                               lods_values,
                                               batch_size,
                                               embedding_size,
                                               cvm_offset);
D
danleifeng 已提交
420 421 422 423 424 425 426 427 428
  }
}

template <typename T>
class FusedSeqpoolCVMCUDAKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    auto inputs = ctx.MultiInput<LoDTensor>("X");
    auto outputs = ctx.MultiOutput<framework::Tensor>("Out");
429
    auto &dev_ctx = ctx.template device_context<phi::GPUContext>();
D
danleifeng 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    const auto slot_size = inputs.size();
    std::vector<const float *> input_data(slot_size);
    std::vector<const size_t *> lods_data(slot_size);
    std::vector<T *> output_data(slot_size);

    std::vector<LoDTensor> seqpool_outputs(slot_size);
    std::vector<T *> seqpool_output_data(slot_size);

    auto padding_value = ctx.Attr<float>("pad_value");
    auto use_cvm = ctx.Attr<bool>("use_cvm");
    const int cvm_offset = ctx.Attr<int>("cvm_offset");

    int embedding_size = inputs[0]->numel() / inputs[0]->dims()[0];
    int batch_size = -1;
    std::vector<paddle::framework::MixVector<size_t> *> mix_lods_v(slot_size);

    for (size_t i = 0; i < slot_size; ++i) {
      const auto *input = inputs[i];

      Vector<size_t> lods;
      if (input->lod().size() != 0) {
        auto lod = input->lod();
        lods = lod[0];
      } else {
        lods.push_back(0);
        for (int i = 0; i < input->dims()[0]; i++) {
          lods.push_back(i + 1);
        }
      }
      int cur_batch_size =
          input->lod().size() ? input->lod()[0].size() - 1 : input->dims()[0];
      if (batch_size == -1) {
        batch_size = cur_batch_size;
      } else {
464 465
        PADDLE_ENFORCE_EQ(batch_size,
                          cur_batch_size,
D
danleifeng 已提交
466 467 468 469
                          platform::errors::PreconditionNotMet(
                              "The batch size of all input should be same, "
                              "please cheack, last batchsize is %d, current "
                              "batchsize is %d",
470 471
                              batch_size,
                              cur_batch_size));
D
danleifeng 已提交
472 473 474 475 476 477 478 479 480
      }
      input_data[i] = reinterpret_cast<const T *>(input->data<T>());

      auto *output = outputs[i];
      if (use_cvm) {
        output->Resize({batch_size, embedding_size});
      } else {
        output->Resize({batch_size, embedding_size - cvm_offset});
      }
481 482
      output_data[i] = reinterpret_cast<T *>(
          dev_ctx.Alloc<T>(output, output->numel() * sizeof(T)));
D
danleifeng 已提交
483 484
      mix_lods_v[i] = new paddle::framework::MixVector<size_t>(&lods);
      lods_data[i] = mix_lods_v[i]->CUDAData(ctx.GetPlace());
485 486 487
      seqpool_outputs[i].Resize({batch_size, embedding_size});
      seqpool_output_data[i] = reinterpret_cast<T *>(dev_ctx.Alloc<T>(
          &seqpool_outputs[i], seqpool_outputs[i].numel() * sizeof(T)));
D
danleifeng 已提交
488 489
    }

490 491 492 493 494 495 496 497 498 499 500
    FusedSeqpoolCVM(ctx,
                    input_data,
                    output_data,
                    seqpool_output_data,
                    lods_data,
                    batch_size,
                    slot_size,
                    embedding_size,
                    padding_value,
                    use_cvm,
                    cvm_offset);
D
danleifeng 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514

    for (int i = 0; i < slot_size; i++) {
      delete mix_lods_v[i];
    }
  }
};

template <typename T>
class FusedSeqpoolCVMGradCUDAKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    auto out_grads = ctx.MultiInput<LoDTensor>(framework::GradVarName("Out"));
    auto in_grads = ctx.MultiOutput<LoDTensor>(framework::GradVarName("X"));
    auto *cvm = ctx.Input<LoDTensor>("CVM");
515
    auto &dev_ctx = ctx.template device_context<phi::GPUContext>();
D
danleifeng 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
    std::string pooltype = ctx.Attr<std::string>("pooltype");
    auto use_cvm = ctx.Attr<bool>("use_cvm");
    const int cvm_offset = ctx.Attr<int>("cvm_offset");

    const auto slot_size = in_grads.size();
    std::vector<const T *> out_grads_data(slot_size);
    std::vector<T *> in_grads_data(slot_size);
    std::vector<const T *> cvm_data(slot_size);
    std::vector<const size_t *> lods_data(slot_size);

    int embedding_size = in_grads[0]->numel() / in_grads[0]->dims()[0];
    int batch_size = -1;
    std::vector<paddle::framework::MixVector<size_t> *> mix_lods_v(slot_size);

    for (size_t i = 0; i < slot_size; ++i) {
      auto *in_grad = in_grads[i];

      Vector<size_t> lods;
      if (in_grad->lod().size() != 0) {
        auto lod = in_grad->lod();
        lods = lod[0];
      } else {
        lods.push_back(0);
        for (int i = 0; i < in_grad->dims()[0]; i++) {
          lods.push_back(i + 1);
        }
      }

      int cur_batch_size = in_grad->lod().size() ? in_grad->lod()[0].size() - 1
                                                 : in_grad->dims()[0];
      if (batch_size == -1) {
        batch_size = cur_batch_size;
      } else {
549 550
        PADDLE_ENFORCE_EQ(batch_size,
                          cur_batch_size,
D
danleifeng 已提交
551 552 553 554
                          platform::errors::PreconditionNotMet(
                              "The batch size of all input should be same, "
                              "please cheack, last batchsize is %d, current "
                              "batchsize is %d",
555 556
                              batch_size,
                              cur_batch_size));
D
danleifeng 已提交
557 558 559 560 561
      }

      auto *out_grad = out_grads[i];
      out_grads_data[i] = reinterpret_cast<const T *>(out_grad->data<T>());

562 563
      in_grads_data[i] = reinterpret_cast<T *>(
          dev_ctx.Alloc<T>(in_grad, in_grad->numel() * sizeof(T)));
D
danleifeng 已提交
564 565 566 567
      mix_lods_v[i] = new paddle::framework::MixVector<size_t>(&lods);
      lods_data[i] = mix_lods_v[i]->CUDAData(ctx.GetPlace());
      cvm_data[i] = reinterpret_cast<const T *>(cvm->data<T>());
    }
568 569 570 571 572 573 574 575 576
    FusedSeqpoolCVMGrad(ctx,
                        out_grads_data,
                        in_grads_data,
                        cvm_data,
                        lods_data,
                        batch_size,
                        slot_size,
                        embedding_size,
                        use_cvm,
D
danleifeng 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
                        cvm_offset);

    for (int i = 0; i < slot_size; i++) {
      delete mix_lods_v[i];
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_CUDA_KERNEL(fused_seqpool_cvm,
                        ops::FusedSeqpoolCVMCUDAKernel<float>);

REGISTER_OP_CUDA_KERNEL(fused_seqpool_cvm_grad,
                        ops::FusedSeqpoolCVMGradCUDAKernel<float>);