index_impl.cu.h 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) 2022 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. */
#pragma once
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/random.h>
#include "paddle/fluid/framework/generator.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/operators/amp/fp16_type_traits.h"
#include "paddle/fluid/operators/fill_constant_op.h"
#include "paddle/phi/backends/gpu/gpu_launch_config.h"
#include "paddle/phi/core/hostdevice.h"
25 26
#include "paddle/phi/kernels/funcs/aligned_vector.h"
#include "paddle/phi/kernels/funcs/distribution_helper.h"
27 28 29 30 31 32 33
#include "paddle/phi/kernels/primitive/kernel_primitives.h"

namespace paddle {
namespace operators {

namespace kps = phi::kps;
template <typename T, typename Functor, int VecSize>
34
__global__ void VectorizedIndexKernel(T *out, size_t numel, size_t main_offset,
35
                                      Functor func) {
36 37 38
  size_t data_offset = BLOCK_ID_X * BLOCK_NUM_X * VecSize;
  size_t stride = BLOCK_NUM_X * GRID_NUM_X * VecSize;
  size_t args[VecSize];
39 40
  T result[VecSize];
  for (; data_offset < main_offset; data_offset += stride) {
41 42 43
    kps::InitWithDataIndex<size_t, VecSize, 1, 1>(&args[0], data_offset);
    kps::ElementwiseUnary<size_t, T, VecSize, 1, 1, Functor>(&result[0],
                                                             &args[0], func);
44 45 46
    kps::WriteData<T, VecSize, 1, 1, false>(out + data_offset, &result[0],
                                            BLOCK_NUM_X * VecSize);
  }
47
  size_t num = numel - data_offset;
48
  if (num > 0) {
49 50 51
    kps::InitWithDataIndex<size_t, VecSize, 1, 1>(&args[0], data_offset);
    kps::ElementwiseUnary<size_t, T, VecSize, 1, 1, Functor>(&result[0],
                                                             &args[0], func);
52 53 54 55 56 57 58 59 60
    kps::WriteData<T, VecSize, 1, 1, true>(out + data_offset, &result[0], num);
  }
}

template <typename T, typename Functor>
void IndexKernel(const KPDevice &dev_ctx, Tensor *out, Functor func) {
  int numel = out->numel();
  T *out_data = out->mutable_data<T>(dev_ctx.GetPlace());
  if (numel <= 0) return;
61
  int vec_size = phi::GetVectorizedSize(out_data);
62 63 64 65 66 67 68 69 70 71 72
#ifdef PADDLE_WITH_XPU_KP
  int block = 64;
  int grid = 8;
  auto stream = dev_ctx.x_context()->xpu_stream;
#else
  auto config =
      phi::backends::gpu::GetGpuLaunchConfig1D(dev_ctx, numel, vec_size);
  int grid = config.block_per_grid.x;
  int block = config.thread_per_block.x;
  auto stream = dev_ctx.stream();
#endif
73
  size_t main_offset = (numel / (vec_size * block)) * vec_size * block;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  switch (vec_size) {
    case 4:
      VectorizedIndexKernel<T, Functor, 4><<<grid, block, 0, stream>>>(
          out_data, numel, main_offset, func);
      break;
    case 2:
      VectorizedIndexKernel<T, Functor, 2><<<grid, block, 0, stream>>>(
          out_data, numel, main_offset, func);
      break;
    case 1:
      VectorizedIndexKernel<T, Functor, 1><<<grid, block, 0, stream>>>(
          out_data, numel, main_offset, func);
      break;
    default: {
      PADDLE_THROW(paddle::platform::errors::Unimplemented(
          "Unsupported vectorized size: %d !", vec_size));
      break;
    }
  }
}

}  // namespace operators
}  // namespace paddle