sequence_scale.cu 3.7 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>
W
wanghaoshuang 已提交
26 27
__global__ void SequenceScaleKernel(T* seq, size_t* lod, const T* scales,
                                    const size_t seq_width) {
28 29 30 31
  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;
32
    seq[idx] *= scales[blockIdx.x];
W
wanghaoshuang 已提交
33 34 35 36 37 38
  }
}

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

49 50 51 52
#ifdef PADDLE_WITH_HIP
    hipLaunchKernelGGL(
        HIP_KERNEL_NAME(SequenceScaleKernel<T, PADDLE_CUDA_NUM_THREADS>),
        dim3(num_seq), dim3(PADDLE_CUDA_NUM_THREADS), 0, context.stream(),
53 54
        seq_data, mix_vector.CUDAMutableData(context.GetPlace()), scales,
        seq_width);
55
#else
56 57
    SequenceScaleKernel<T, PADDLE_CUDA_NUM_THREADS><<<
        num_seq, PADDLE_CUDA_NUM_THREADS, 0, context.stream()>>>(
58 59
        seq_data, mix_vector.CUDAMutableData(context.GetPlace()), scales,
        seq_width);
60
#endif
61
    mix_vector.CopyToCPU();
W
wanghaoshuang 已提交
62 63 64
  }
};

0
0x45f 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
template <typename T>
class ScaleLoDTensorFunctor<phi::GPUContext, T> {
 public:
  void operator()(const phi::GPUContext& context, const T* scales,
                  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>),
        dim3(num_seq), dim3(PADDLE_CUDA_NUM_THREADS), 0, context.stream(),
        seq_data, mix_vector.CUDAMutableData(context.GetPlace()), scales,
        seq_width);
#else
    SequenceScaleKernel<T, PADDLE_CUDA_NUM_THREADS><<<
        num_seq, PADDLE_CUDA_NUM_THREADS, 0, context.stream()>>>(
        seq_data, mix_vector.CUDAMutableData(context.GetPlace()), scales,
        seq_width);
#endif
    mix_vector.CopyToCPU();
  }
};

W
wanghaoshuang 已提交
94
template class ScaleLoDTensorFunctor<platform::CUDADeviceContext, float>;
95
template class ScaleLoDTensorFunctor<platform::CUDADeviceContext, double>;
W
wanghaoshuang 已提交
96

0
0x45f 已提交
97 98 99
template class ScaleLoDTensorFunctor<phi::GPUContext, float>;
template class ScaleLoDTensorFunctor<phi::GPUContext, double>;

W
wanghaoshuang 已提交
100 101 102
}  // namespace math
}  // namespace operators
}  // namespace paddle