sequence_scale.cu 4.0 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 38 39 40
  }
}

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

52 53 54
#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,
62
        seq_width);
63
#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);
70
#endif
71
    mix_vector.CopyToCPU();
W
wanghaoshuang 已提交
72 73 74
  }
};

0
0x45f 已提交
75 76 77
template <typename T>
class ScaleLoDTensorFunctor<phi::GPUContext, T> {
 public:
78 79
  void operator()(const phi::GPUContext& context,
                  const T* scales,
0
0x45f 已提交
80 81 82 83 84 85 86 87 88 89 90 91
                  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>),
92 93 94 95 96 97 98
        dim3(num_seq),
        dim3(PADDLE_CUDA_NUM_THREADS),
        0,
        context.stream(),
        seq_data,
        mix_vector.CUDAMutableData(context.GetPlace()),
        scales,
0
0x45f 已提交
99 100
        seq_width);
#else
101 102
    SequenceScaleKernel<T, PADDLE_CUDA_NUM_THREADS>
        <<<num_seq, PADDLE_CUDA_NUM_THREADS, 0, context.stream()>>>(
103 104 105
            seq_data,
            mix_vector.CUDAMutableData(context.GetPlace()),
            scales,
106
            seq_width);
0
0x45f 已提交
107 108 109 110 111
#endif
    mix_vector.CopyToCPU();
  }
};

W
wanghaoshuang 已提交
112
template class ScaleLoDTensorFunctor<platform::CUDADeviceContext, float>;
113
template class ScaleLoDTensorFunctor<platform::CUDADeviceContext, double>;
W
wanghaoshuang 已提交
114

0
0x45f 已提交
115 116 117
template class ScaleLoDTensorFunctor<phi::GPUContext, float>;
template class ScaleLoDTensorFunctor<phi::GPUContext, double>;

W
wanghaoshuang 已提交
118 119 120
}  // namespace math
}  // namespace operators
}  // namespace paddle