sequence_scale.cu 2.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/sequence_scale.h"
16
#include "paddle/fluid/platform/device/gpu/gpu_primitives.h"
0
0x45f 已提交
17
#include "paddle/phi/backends/gpu/gpu_context.h"
W
wanghaoshuang 已提交
18 19 20 21 22

namespace paddle {
namespace operators {
namespace math {

23 24 25
using platform::PADDLE_CUDA_NUM_THREADS;

template <typename T, int BlockSize>
26 27 28
__global__ void SequenceScaleKernel(T* seq,
                                    size_t* lod,
                                    const T* scales,
W
wanghaoshuang 已提交
29
                                    const size_t seq_width) {
30 31 32 33
  for (int i = threadIdx.x;
       i < (lod[blockIdx.x + 1] - lod[blockIdx.x]) * seq_width;
       i += BlockSize) {
    int idx = lod[blockIdx.x] * seq_width + i;
34
    seq[idx] *= scales[blockIdx.x];
W
wanghaoshuang 已提交
35 36 37
  }
}

0
0x45f 已提交
38 39 40
template <typename T>
class ScaleLoDTensorFunctor<phi::GPUContext, T> {
 public:
41 42
  void operator()(const phi::GPUContext& context,
                  const T* scales,
0
0x45f 已提交
43 44 45 46 47 48 49 50 51 52 53 54
                  framework::LoDTensor* seq) {
    const size_t level = 0;
    auto lod = seq->lod();
    const size_t num_seq = lod[level].size() - 1;
    const size_t seq_width = seq->numel() / seq->dims()[0];
    auto abs_offset_lod = framework::ToAbsOffset(lod);
    T* seq_data = seq->mutable_data<T>(context.GetPlace());
    paddle::framework::MixVector<size_t> mix_vector(&(abs_offset_lod[level]));

#ifdef PADDLE_WITH_HIP
    hipLaunchKernelGGL(
        HIP_KERNEL_NAME(SequenceScaleKernel<T, PADDLE_CUDA_NUM_THREADS>),
55 56 57 58 59 60 61
        dim3(num_seq),
        dim3(PADDLE_CUDA_NUM_THREADS),
        0,
        context.stream(),
        seq_data,
        mix_vector.CUDAMutableData(context.GetPlace()),
        scales,
0
0x45f 已提交
62 63
        seq_width);
#else
64 65
    SequenceScaleKernel<T, PADDLE_CUDA_NUM_THREADS>
        <<<num_seq, PADDLE_CUDA_NUM_THREADS, 0, context.stream()>>>(
66 67 68
            seq_data,
            mix_vector.CUDAMutableData(context.GetPlace()),
            scales,
69
            seq_width);
0
0x45f 已提交
70 71 72 73 74 75 76 77
#endif
    mix_vector.CopyToCPU();
  }
};

template class ScaleLoDTensorFunctor<phi::GPUContext, float>;
template class ScaleLoDTensorFunctor<phi::GPUContext, double>;

W
wanghaoshuang 已提交
78 79 80
}  // namespace math
}  // namespace operators
}  // namespace paddle