scale.cu 4.1 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
// Copyright (c) 2019 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.

#include "iostream"
#include "lite/backends/cuda/math/scale.h"
#include "lite/backends/cuda/math/utils.h"

namespace paddle {
namespace lite {
namespace cuda {
namespace math {

24 25 26 27 28 29 30 31 32 33 34 35
template <typename T>
__global__ void scale_kernel(int num, const T* in, T* out, const float scale) {
  int tid = blockIdx.x * blockDim.x + threadIdx.x;
  if (tid < num) {
#if __CUDA_ARCH__ >= 350
    out[tid] = __ldg(in + tid) * scale;
#else
    out[tid] = in[tid] * scale;
#endif
  }
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
__global__ void fp32_scale_nhwc4_kernel(int num,
                                        const float4* in,
                                        float4* out,
                                        const float4* scale,
                                        int N,
                                        int K,
                                        int H,
                                        int W) {
  int tid = blockIdx.x * blockDim.x + threadIdx.x;
  if (tid < num) {
    int scale_idx = tid % K;
    const float4 scale_ptr = scale[scale_idx];
    const float4 in_ptr = in[tid];
    float4 packed_val;

    packed_val.x = in_ptr.x * scale_ptr.x;
    packed_val.y = in_ptr.y * scale_ptr.y;
    packed_val.z = in_ptr.z * scale_ptr.z;
    packed_val.w = in_ptr.w * scale_ptr.w;
    out[tid] = packed_val;
  }
}

Z
Zhaolong Xing 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
__global__ void fp32_scale_nhwc_kernel(int num,
                                       const float* in,
                                       float* out,
                                       const float* scale,
                                       int N,
                                       int C,
                                       int H,
                                       int W) {
  int tid = blockIdx.x * blockDim.x + threadIdx.x;
  if (tid < num) {
    int idx = tid % C;
#if __CUDA_ARCH__ >= 350
    out[tid] = __ldg(in + tid) * __ldg(scale + idx);
#else
    out[tid] = in[tid] * scale[idx];
#endif
  }
}

void fp32_scale_nhwc(int num,
                     const void* in,
                     void* out,
                     const void* scale,
                     int N,
                     int C,
                     int H,
                     int W,
                     cudaStream_t stream) {
87
  int thread = 256;
Z
Zhaolong Xing 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  if (C % 4 == 0) {
    int block = (num / 4 + thread - 1) / thread;
    fp32_scale_nhwc4_kernel<<<block, thread, 0, stream>>>(
        num / 4,
        static_cast<const float4*>(in),
        static_cast<float4*>(out),
        static_cast<const float4*>(scale),
        N,
        C / 4,
        H,
        W);
  } else {
    int block = (num + thread - 1) / thread;
    fp32_scale_nhwc_kernel<<<block, thread, 0, stream>>>(
        num,
        static_cast<const float*>(in),
        static_cast<float*>(out),
        static_cast<const float*>(scale),
        N,
        C,
        H,
        W);
  }

112 113 114 115
  cudaError_t error = cudaGetLastError();
  if (error != cudaSuccess) std::cout << cudaGetErrorString(error);
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
template <typename T>
void scale(int num, const T* in, T* out, float scale, cudaStream_t stream) {
  int thread = 256;
  int block = (num + thread - 1) / thread;
  scale_kernel<<<block, thread, 0, stream>>>(num, in, out, scale);
}

template <typename T>
void scale(int num, const T* in, T* out, float scale) {
  int thread = 256;
  int block = (num + thread - 1) / thread;
  scale_kernel<<<block, thread>>>(num, in, out, scale);
}

template void scale(int num, const float*, float*, float, cudaStream_t);
template void scale(int num, const float*, float*, float);

133 134 135 136
}  // namespace math
}  // namespace cuda
}  // namespace lite
}  // namespace paddle