float16_bfloat16_cuda_test.cu 8.5 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// Copyright (c) 2021 CINN 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 <glog/logging.h>
#include <gtest/gtest.h>

#include <random>
#include <vector>

#include "paddle/cinn/common/bfloat16.h"
#include "paddle/cinn/common/float16.h"

namespace cinn {
namespace common {

#define CUDA_CALL(func)                                            \
  {                                                                \
    auto status = func;                                            \
    if (status != cudaSuccess) {                                   \
      LOG(FATAL) << "CUDA Error : " << cudaGetErrorString(status); \
    }                                                              \
  }

class CudaMem {
 public:
  CudaMem() = default;

  void* mutable_data(size_t bytes) {
    CHECK_GT(bytes, 0) << "Cannot allocate empty memory!";
    if (ptr) {
      CHECK_EQ(bytes, bytes_) << "Try allocate memory twice!";
      return ptr;
    }
    CUDA_CALL(cudaMalloc(&ptr, bytes));
    bytes_ = bytes;
    return ptr;
  }

  template <typename T>
  T* mutable_data(size_t num) {
    return reinterpret_cast<T*>(mutable_data(num * sizeof(T)));
  }

  void* data() const {
    CHECK(ptr) << "Try get nullptr!";
    return ptr;
  }

  template <typename T>
  T* data() const {
    return reinterpret_cast<T*>(data());
  }

65 66 67
  void MemcpyFromHost(const void* src,
                      size_t bytes,
                      cudaStream_t stream = nullptr) {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    CHECK_LE(bytes, bytes_) << "Too many data need copy";
    CUDA_CALL(cudaMemcpyAsync(ptr, src, bytes, cudaMemcpyHostToDevice, stream));
  }

  void MemcpyToHost(void* dst, size_t bytes, cudaStream_t stream = nullptr) {
    CHECK_LE(bytes, bytes_) << "Too many data need copy";
    CUDA_CALL(cudaMemcpyAsync(dst, ptr, bytes, cudaMemcpyDeviceToHost, stream));
  }

  ~CudaMem() {
    if (ptr) {
      cudaFree(ptr);
    }
    bytes_ = 0;
  }

 private:
  void* ptr{nullptr};
  size_t bytes_{0};
};

89 90 91
__global__ void cast_fp32_to_fp16_cuda_kernel(const float* input,
                                              const int num,
                                              float16* out) {
92 93 94 95 96 97
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    out[idx] = float16(input[idx]);
  }
}

98 99 100
__global__ void cast_fp16_to_fp32_cuda_kernel(const float16* input,
                                              const int num,
                                              float* out) {
101 102 103 104 105 106
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    out[idx] = float(input[idx]);
  }
}

107 108 109 110
__global__ void test_fp16_cuda_kernel(const float16* x,
                                      const float16* y,
                                      const int num,
                                      float16* out) {
111 112 113 114 115 116 117 118 119
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    float16 x_i = x[idx], y_i = y[idx];
    x_i += float16(1);

    out[idx] = (x_i + y_i) * (x_i - y_i);
  }
}

120 121 122
__global__ void cast_fp32_to_bf16_cuda_kernel(const float* input,
                                              const int num,
                                              bfloat16* out) {
123 124 125 126 127 128
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    out[idx] = bfloat16(input[idx]);
  }
}

129 130 131
__global__ void cast_bf16_to_fp32_cuda_kernel(const bfloat16* input,
                                              const int num,
                                              float* out) {
132 133 134 135 136 137
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    out[idx] = float(input[idx]);
  }
}

138 139 140 141
__global__ void test_bf16_cuda_kernel(const bfloat16* x,
                                      const bfloat16* y,
                                      const int num,
                                      bfloat16* out) {
142 143 144 145 146 147 148 149 150
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    bfloat16 x_i = x[idx], y_i = y[idx];
    x_i += bfloat16(1);

    out[idx] = (x_i + y_i) * (x_i - y_i);
  }
}

151 152 153 154
__global__ void test_fp32_cuda_kernel(const float* x,
                                      const float* y,
                                      const int num,
                                      float* out) {
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx < num) {
    float x_i = x[idx], y_i = y[idx];
    x_i += 1.0f;

    out[idx] = (x_i + y_i) * (x_i - y_i);
  }
}

TEST(FP16_BF16, basic_cuda) {
#ifdef CUDA_VERSION
  LOG(INFO) << "CUDA version: " << CUDA_VERSION;
#endif

  int num = 2048;

  cudaStream_t stream;
  CUDA_CALL(cudaStreamCreate(&stream));

  dim3 block = 1024;
175
  dim3 grid = (num + block.x - 1) / block.x;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

  std::vector<float> x_fp32_host(num), y_fp32_host(num);
  {  // step1 : generate input data
    std::random_device r;
    std::default_random_engine eng(r());
    std::uniform_real_distribution<float> dis(1e-5f, 1.0f);

    for (int i = 0; i < num; ++i) {
      x_fp32_host[i] = dis(eng);
      y_fp32_host[i] = dis(eng);
    }
  }

  CudaMem x_fp32_device, y_fp32_device, out_fp32_device;
  {  // step2 : compute fp32 result
191 192
    auto x_fp32_ptr = x_fp32_device.mutable_data<float>(num);
    auto y_fp32_ptr = y_fp32_device.mutable_data<float>(num);
193 194
    auto out_fp32_ptr = out_fp32_device.mutable_data<float>(num);

195 196 197 198
    x_fp32_device.MemcpyFromHost(
        x_fp32_host.data(), num * sizeof(float), stream);
    y_fp32_device.MemcpyFromHost(
        y_fp32_host.data(), num * sizeof(float), stream);
199

200 201
    test_fp32_cuda_kernel<<<grid, block, 0, stream>>>(
        x_fp32_ptr, y_fp32_ptr, num, out_fp32_ptr);
202 203 204 205 206 207
  }

  CudaMem x_fp16_device, y_fp16_device, out_fp16_device;
  CudaMem x_bf16_device, y_bf16_device, out_bf16_device;
  {  // step3 : compute fp16/bf16 result
    // step3.1 : compute fp16 result
208 209
    auto x_fp16_ptr = x_fp16_device.mutable_data<float16>(num);
    auto y_fp16_ptr = y_fp16_device.mutable_data<float16>(num);
210 211
    auto out_fp16_ptr = out_fp16_device.mutable_data<float16>(num);

212 213 214 215
    cast_fp32_to_fp16_cuda_kernel<<<grid, block, 0, stream>>>(
        x_fp32_device.data<float>(), num, x_fp16_ptr);
    cast_fp32_to_fp16_cuda_kernel<<<grid, block, 0, stream>>>(
        y_fp32_device.data<float>(), num, y_fp16_ptr);
216

217 218
    test_fp16_cuda_kernel<<<grid, block, 0, stream>>>(
        x_fp16_ptr, y_fp16_ptr, num, out_fp16_ptr);
219 220

    // step3.2 : compute bf16 result
221 222
    auto x_bf16_ptr = x_bf16_device.mutable_data<bfloat16>(num);
    auto y_bf16_ptr = y_bf16_device.mutable_data<bfloat16>(num);
223 224
    auto out_bf16_ptr = out_bf16_device.mutable_data<bfloat16>(num);

225 226 227 228
    cast_fp32_to_bf16_cuda_kernel<<<grid, block, 0, stream>>>(
        x_fp32_device.data<float>(), num, x_bf16_ptr);
    cast_fp32_to_bf16_cuda_kernel<<<grid, block, 0, stream>>>(
        y_fp32_device.data<float>(), num, y_bf16_ptr);
229

230 231
    test_bf16_cuda_kernel<<<grid, block, 0, stream>>>(
        x_bf16_ptr, y_bf16_ptr, num, out_bf16_ptr);
232 233 234 235 236 237 238
  }

  CudaMem fp32res_fp16_device;
  CudaMem fp32res_bf16_device;
  {  // step4 : cast fp16/bf16 result to fp32 result
    // step4.1 : cast fp16 result to fp32 result
    auto fp32res_fp16_ptr = fp32res_fp16_device.mutable_data<float>(num);
239 240
    cast_fp16_to_fp32_cuda_kernel<<<grid, block, 0, stream>>>(
        out_fp16_device.data<float16>(), num, fp32res_fp16_ptr);
241 242 243

    // step4.2 : cast bf16 result to fp32 result
    auto fp32res_bf16_ptr = fp32res_bf16_device.mutable_data<float>(num);
244 245
    cast_bf16_to_fp32_cuda_kernel<<<grid, block, 0, stream>>>(
        out_bf16_device.data<bfloat16>(), num, fp32res_bf16_ptr);
246 247 248 249
  }

  std::vector<float> out_fp32_host(num), out_fp16_host(num), out_bf16_host(num);
  {  // step5 : copy result from device to host
250 251 252 253 254 255
    out_fp32_device.MemcpyToHost(
        out_fp32_host.data(), num * sizeof(float), stream);
    fp32res_fp16_device.MemcpyToHost(
        out_fp16_host.data(), num * sizeof(float), stream);
    fp32res_bf16_device.MemcpyToHost(
        out_bf16_host.data(), num * sizeof(float), stream);
256 257 258 259 260 261 262 263 264 265 266 267 268 269
  }

  CUDA_CALL(cudaStreamSynchronize(stream));

  for (int i = 0; i < num; ++i) {
    ASSERT_NEAR(out_fp32_host[i], out_fp16_host[i], 1e-2f);
    ASSERT_NEAR(out_fp32_host[i], out_bf16_host[i], 1e-1f);
  }

  CUDA_CALL(cudaStreamDestroy(stream));
}

}  // namespace common
}  // namespace cinn