target_assign_op.cu 2.5 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

B
baiyf 已提交
15
#include "paddle/fluid/operators/detection/target_assign_op.h"
16 17 18 19

namespace paddle {
namespace operators {

20
template <typename T, typename WT>
21 22 23 24 25 26 27
__global__ void NegTargetAssignKernel(const int* neg_indices,
                                      const size_t* lod,
                                      const int N,
                                      const int M,
                                      const int K,
                                      const int mismatch_value,
                                      T* out,
28
                                      WT* out_wt) {
29 30 31 32
  int bidx = blockIdx.x;
  int st = lod[bidx];
  int ed = lod[bidx + 1];

33
  int row_start = bidx * M;
34
  for (int i = st + threadIdx.x; i < ed; i += blockDim.x) {
D
dangqingqing 已提交
35
    int id = row_start + neg_indices[i];
36 37 38 39
    for (int k = 0; k < K; ++k) {
      out[id * K + k] = T(mismatch_value);
      out_wt[id * K + k] = WT(1.);
    }
40 41 42
  }
}

43
template <typename T, typename WT>
L
Leo Chen 已提交
44 45
struct NegTargetAssignFunctor<phi::GPUContext, T, WT> {
  void operator()(const phi::GPUContext& ctx,
46 47 48 49 50 51 52
                  const int* neg_indices,
                  const size_t* lod,
                  const int N,
                  const int M,
                  const int K,
                  const int mismatch_value,
                  T* out,
53
                  WT* out_wt) {
54
    const int block_size = 256;
55 56 57
    const int grid_size = N;
    NegTargetAssignKernel<T, WT><<<grid_size, block_size, 0, ctx.stream()>>>(
        neg_indices, lod, N, M, K, mismatch_value, out, out_wt);
58 59 60
  }
};

L
Leo Chen 已提交
61 62
template struct NegTargetAssignFunctor<phi::GPUContext, int, float>;
template struct NegTargetAssignFunctor<phi::GPUContext, float, float>;
63 64 65 66 67

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
L
Leo Chen 已提交
68 69 70
REGISTER_OP_CUDA_KERNEL(target_assign,
                        ops::TargetAssignKernel<phi::GPUContext, int, float>,
                        ops::TargetAssignKernel<phi::GPUContext, float, float>);