calib_compute.cu 10.1 KB
Newer Older
Z
Zhen Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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 <vector>
16

Z
Zhaolong Xing 已提交
17
#include "lite/backends/cuda/math/utils.h"
Z
Zhen Wang 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "lite/core/op_registry.h"
#include "lite/core/type_system.h"
#include "lite/kernels/cuda/calib_compute.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace cuda {

__global__ void Fp32ToInt8Kernel(const int num,
                                 const float scale,
                                 const float* input,
                                 int8_t* output) {
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < num) {
Z
Zhaolong Xing 已提交
33
    output[index] = lite::cuda::math::from_float<int8_t>(input[index] / scale);
Z
Zhen Wang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
  }
}

__global__ void Int8ToFp32Kernel(const int num,
                                 const float scale,
                                 const int8_t* input,
                                 float* output) {
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < num) {
    output[index] = input[index] * scale;
  }
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
__global__ void Fp32ToFp16Kernel(const int num,
                                 const float* input,
                                 half* output) {
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < num) {
    output[index] = __float2half(input[index]);
  }
}

__global__ void Fp16ToFp32Kernel(const int num,
                                 const half* input,
                                 float* output) {
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < num) {
    output[index] = lite::cuda::math::from_float<half>(input[index]);
  }
}

Z
Zhen Wang 已提交
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 94 95 96
void CalibComputeFp32ToInt8::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->As<CUDAContext>();
  auto stream = ctx.exec_stream();

  auto scale = param.scale;
  const auto* din = param.input->data<float>();
  auto* dout = param.output->mutable_data<int8_t>(TARGET(kCUDA));
  int num = static_cast<int>(param.input->numel());
  int threads = 1024;
  int blocks = (num + threads - 1) / threads;
  Fp32ToInt8Kernel<<<blocks, threads, 0, stream>>>(num, scale, din, dout);
  cudaError_t error = cudaGetLastError();
  CHECK(error == cudaSuccess) << cudaGetErrorString(error);
}

void CalibComputeInt8ToFp32::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->As<CUDAContext>();
  auto stream = ctx.exec_stream();

  auto scale = param.scale;
  const auto* din = param.input->data<int8_t>();
  auto* dout = param.output->mutable_data<float>(TARGET(kCUDA));
  int num = static_cast<int>(param.input->numel());
  int threads = 1024;
  int blocks = (num + threads - 1) / threads;
  Int8ToFp32Kernel<<<blocks, threads, 0, stream>>>(num, scale, din, dout);
  cudaError_t error = cudaGetLastError();
  CHECK(error == cudaSuccess) << cudaGetErrorString(error);
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
void CalibComputeFp32ToFp16::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->As<CUDAContext>();
  auto stream = ctx.exec_stream();

  const auto* din = param.input->data<float>();
  auto* dout = param.output->mutable_data<__half>(TARGET(kCUDA));
  int num = static_cast<int>(param.input->numel());
  int threads = 1024;
  int blocks = (num + threads - 1) / threads;
  param.output->set_lod(param.input->lod());
  Fp32ToFp16Kernel<<<blocks, threads, 0, stream>>>(num, din, dout);
  cudaError_t error = cudaGetLastError();
  CHECK(error == cudaSuccess) << cudaGetErrorString(error);
}

void CalibOnceComputeFp32ToFp16::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->As<CUDAContext>();
  auto stream = ctx.exec_stream();
  const auto* din = param.input->data<float>();
  auto* dout = param.output->mutable_data<__half>(TARGET(kCUDA));
  int num = static_cast<int>(param.input->numel());
  int threads = 1024;
  int blocks = (num + threads - 1) / threads;
  param.output->set_lod(param.input->lod());
  Fp32ToFp16Kernel<<<blocks, threads>>>(num, din, dout);

  // remove the unneeded fp32 weights.
  const_cast<lite::Tensor*>(param.input)->clear();

  cudaError_t error = cudaGetLastError();
  CHECK(error == cudaSuccess) << cudaGetErrorString(error);
}

void CalibComputeFp16ToFp32::Run() {
  auto& param = this->Param<param_t>();
  auto& ctx = this->ctx_->As<CUDAContext>();
  auto stream = ctx.exec_stream();

  const auto* din = param.input->data<__half>();
  auto* dout = param.output->mutable_data<float>(TARGET(kCUDA));
  int num = static_cast<int>(param.input->numel());
  int threads = 1024;
  int blocks = (num + threads - 1) / threads;
  param.output->set_lod(param.input->lod());
  Fp16ToFp32Kernel<<<blocks, threads, 0, stream>>>(num, din, dout);
  cudaError_t error = cudaGetLastError();
  CHECK(error == cudaSuccess) << cudaGetErrorString(error);
}

Z
Zhen Wang 已提交
148 149 150 151 152 153 154
}  // namespace cuda
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(calib,
                     kCUDA,
Z
Zhaolong Xing 已提交
155
                     kFloat,
Z
Zhen Wang 已提交
156 157 158 159
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeFp32ToInt8,
                     fp32_to_int8)
    .BindInput("Input",
Z
Zhaolong Xing 已提交
160 161 162 163 164 165 166
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kAny))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kInt8),
                                       DATALAYOUT(kAny))})
Z
Zhen Wang 已提交
167 168 169 170
    .Finalize();

REGISTER_LITE_KERNEL(calib,
                     kCUDA,
Z
Zhaolong Xing 已提交
171
                     kFloat,
Z
Zhen Wang 已提交
172 173 174 175
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeInt8ToFp32,
                     int8_to_fp32)
    .BindInput("Input",
Z
Zhaolong Xing 已提交
176 177 178
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kAny))})
Z
Zhen Wang 已提交
179
    .BindOutput("Out",
Z
Zhaolong Xing 已提交
180 181 182
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFloat),
                                       DATALAYOUT(kAny))})
Z
Zhen Wang 已提交
183 184
    .Finalize();

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
REGISTER_LITE_KERNEL(calib,
                     kCUDA,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeFp16ToFp32,
                     fp16_to_fp32)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kAny))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFloat),
                                       DATALAYOUT(kAny))})
    .Finalize();
REGISTER_LITE_KERNEL(calib,
                     kCUDA,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeFp32ToFp16,
                     fp32_to_fp16)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kAny))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFP16),
                                       DATALAYOUT(kAny))})
    .Finalize();

Z
Zhen Wang 已提交
216 217
REGISTER_LITE_KERNEL(calib_once,
                     kCUDA,
Z
Zhaolong Xing 已提交
218
                     kFloat,
Z
Zhen Wang 已提交
219 220 221 222
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeFp32ToInt8,
                     fp32_to_int8)
    .BindInput("Input",
Z
Zhaolong Xing 已提交
223 224 225 226 227 228 229
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kAny))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kInt8),
                                       DATALAYOUT(kAny))})
Z
Zhen Wang 已提交
230 231 232
    .Finalize();
REGISTER_LITE_KERNEL(calib_once,
                     kCUDA,
Z
Zhaolong Xing 已提交
233
                     kFloat,
Z
Zhen Wang 已提交
234 235 236 237
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeInt8ToFp32,
                     int8_to_fp32)
    .BindInput("Input",
Z
Zhaolong Xing 已提交
238 239 240
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kInt8),
                                      DATALAYOUT(kAny))})
Z
Zhen Wang 已提交
241
    .BindOutput("Out",
Z
Zhaolong Xing 已提交
242 243 244
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFloat),
                                       DATALAYOUT(kAny))})
Z
Zhen Wang 已提交
245
    .Finalize();
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

REGISTER_LITE_KERNEL(calib_once,
                     kCUDA,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibComputeFp16ToFp32,
                     fp16_to_fp32)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kAny))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFloat),
                                       DATALAYOUT(kAny))})
    .Finalize();
REGISTER_LITE_KERNEL(calib_once,
                     kCUDA,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::cuda::CalibOnceComputeFp32ToFp16,
                     fp32_to_fp16)
    .BindInput("Input",
               {LiteType::GetTensorTy(TARGET(kCUDA),
                                      PRECISION(kFloat),
                                      DATALAYOUT(kAny))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kCUDA),
                                       PRECISION(kFP16),
                                       DATALAYOUT(kAny))})
    .Finalize();