|
| 1 | +/*! |
| 2 | + * Copyright (c) 2022 Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE file in the project root for |
| 4 | + * license information. |
| 5 | + */ |
| 6 | + |
| 7 | +#ifdef USE_CUDA_EXP |
| 8 | + |
| 9 | +#include <LightGBM/cuda/cuda_algorithms.hpp> |
| 10 | + |
| 11 | +#include "cuda_regression_metric.hpp" |
| 12 | + |
| 13 | +namespace LightGBM { |
| 14 | + |
| 15 | +template <typename CUDA_METRIC, bool USE_WEIGHTS> |
| 16 | +__global__ void EvalKernel(const data_size_t num_data, const label_t* labels, const label_t* weights, |
| 17 | + const double* scores, double* reduce_block_buffer) { |
| 18 | + __shared__ double shared_mem_buffer[32]; |
| 19 | + const data_size_t index = static_cast<data_size_t>(threadIdx.x + blockIdx.x * blockDim.x); |
| 20 | + double point_metric = 0.0; |
| 21 | + if (index < num_data) { |
| 22 | + point_metric = CUDA_METRIC::MetricOnPointCUDA(labels[index], scores[index]); |
| 23 | + } |
| 24 | + const double block_sum_point_metric = ShuffleReduceSum<double>(point_metric, shared_mem_buffer, NUM_DATA_PER_EVAL_THREAD); |
| 25 | + reduce_block_buffer[blockIdx.x] = block_sum_point_metric; |
| 26 | + if (USE_WEIGHTS) { |
| 27 | + double weight = 0.0; |
| 28 | + if (index < num_data) { |
| 29 | + weight = static_cast<double>(weights[index]); |
| 30 | + const double block_sum_weight = ShuffleReduceSum<double>(weight, shared_mem_buffer, NUM_DATA_PER_EVAL_THREAD); |
| 31 | + reduce_block_buffer[blockIdx.x + blockDim.x] = block_sum_weight; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +template <typename HOST_METRIC, typename CUDA_METRIC> |
| 37 | +double CUDARegressionMetricInterface<HOST_METRIC, CUDA_METRIC>::LaunchEvalKernel(const double* score) const { |
| 38 | + const int num_blocks = (this->num_data_ + NUM_DATA_PER_EVAL_THREAD - 1) / NUM_DATA_PER_EVAL_THREAD; |
| 39 | + if (this->cuda_weights_ != nullptr) { |
| 40 | + EvalKernel<CUDA_METRIC, true><<<num_blocks, NUM_DATA_PER_EVAL_THREAD>>>( |
| 41 | + this->num_data_, this->cuda_labels_, this->cuda_weights_, score, reduce_block_buffer_.RawData()); |
| 42 | + } else { |
| 43 | + EvalKernel<CUDA_METRIC, false><<<num_blocks, NUM_DATA_PER_EVAL_THREAD>>>( |
| 44 | + this->num_data_, this->cuda_labels_, this->cuda_weights_, score, reduce_block_buffer_.RawData()); |
| 45 | + } |
| 46 | + ShuffleReduceSumGlobal<double, double>(reduce_block_buffer_.RawData(), num_blocks, reduce_block_buffer_inner_.RawData()); |
| 47 | + double sum_loss = 0.0; |
| 48 | + CopyFromCUDADeviceToHost<double>(&sum_loss, reduce_block_buffer_inner_.RawData(), 1, __FILE__, __LINE__); |
| 49 | + double sum_weight = static_cast<double>(this->num_data_); |
| 50 | + if (this->cuda_weights_ != nullptr) { |
| 51 | + ShuffleReduceSumGlobal<double, double>(reduce_block_buffer_.RawData() + num_blocks, num_blocks, reduce_block_buffer_inner_.RawData()); |
| 52 | + CopyFromCUDADeviceToHost<double>(&sum_weight, reduce_block_buffer_inner_.RawData(), 1, __FILE__, __LINE__); |
| 53 | + } |
| 54 | + return this->AverageLoss(sum_loss, sum_weight); |
| 55 | +} |
| 56 | + |
| 57 | +template double CUDARegressionMetricInterface<RMSEMetric, CUDARMSEMetric>::LaunchEvalKernel(const double* score) const; |
| 58 | + |
| 59 | +} // namespace LightGBM |
| 60 | + |
| 61 | +#endif // USE_CUDA_EXP |
0 commit comments