178 lines
6.3 KiB
Plaintext
178 lines
6.3 KiB
Plaintext
/* Copyright (c) 2022 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 "paddle/phi/kernels/funcs/cross_entropy.h"
|
|
|
|
#include "paddle/phi/backends/gpu/gpu_context.h"
|
|
#include "paddle/phi/backends/gpu/gpu_device_function.h"
|
|
#include "paddle/phi/backends/gpu/gpu_dnn.h"
|
|
#include "paddle/phi/backends/gpu/gpu_primitives.h"
|
|
#include "paddle/phi/core/utils/data_type.h"
|
|
#include "paddle/phi/kernels/funcs/math.h"
|
|
|
|
namespace phi {
|
|
namespace funcs {
|
|
|
|
template <typename T, typename LabelT>
|
|
__global__ void CrossEntropyKernel(T* Y,
|
|
const T* X,
|
|
const LabelT* label,
|
|
const int N,
|
|
const int D,
|
|
const int ignore_index) {
|
|
CUDA_KERNEL_LOOP(i, N) {
|
|
auto lbl = static_cast<int64_t>(label[i]);
|
|
PADDLE_ENFORCE(lbl >= 0 && lbl < D || lbl == ignore_index,
|
|
"The value of label[%d] expected >= 0 and < %ld, or == %ld, "
|
|
"but got %ld. Please check input value.",
|
|
i,
|
|
D,
|
|
ignore_index,
|
|
lbl);
|
|
Y[i] = ignore_index == lbl
|
|
? static_cast<T>(0)
|
|
: -funcs::TolerableValue<T>()(funcs::real_log(X[i * D + lbl]));
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
__global__ void SoftCrossEntropyKernel(T* Y,
|
|
const T* X,
|
|
const T* label,
|
|
const int class_num) {
|
|
int64_t tid = threadIdx.x;
|
|
T val(0);
|
|
|
|
int64_t idx = static_cast<int64_t>(blockIdx.x) * class_num + tid;
|
|
int64_t end = static_cast<int64_t>(blockIdx.x) * class_num + class_num;
|
|
for (; idx < end; idx += blockDim.x) {
|
|
val += funcs::TolerableValue<T>()(funcs::real_log(X[idx])) * label[idx];
|
|
}
|
|
|
|
val = phi::backends::gpu::reduceSum(val, tid, blockDim.x);
|
|
if (threadIdx.x == 0) {
|
|
Y[blockIdx.x] = -val;
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
struct HardLabelCrossEntropyCUDAFunctorImpl {
|
|
public:
|
|
HardLabelCrossEntropyCUDAFunctorImpl(T* loss_data,
|
|
const T* prob_data,
|
|
const void* label_data,
|
|
const int batch_size,
|
|
const int class_num,
|
|
const int ignore_index,
|
|
const int block_size,
|
|
gpuStream_t stream)
|
|
: loss_data_(loss_data),
|
|
prob_data_(prob_data),
|
|
label_data_(label_data),
|
|
batch_size_(batch_size),
|
|
class_num_(class_num),
|
|
ignore_index_(ignore_index),
|
|
block_size_(block_size),
|
|
stream_(stream) {}
|
|
|
|
template <typename U>
|
|
void apply() const {
|
|
int grid_size = (batch_size_ + block_size_ - 1) / block_size_;
|
|
CrossEntropyKernel<T, U><<<grid_size, block_size_, 0, stream_>>>(
|
|
loss_data_,
|
|
prob_data_,
|
|
static_cast<const U*>(label_data_),
|
|
batch_size_,
|
|
class_num_,
|
|
ignore_index_);
|
|
}
|
|
|
|
private:
|
|
T* loss_data_;
|
|
const T* prob_data_;
|
|
const void* label_data_;
|
|
const int batch_size_;
|
|
const int class_num_;
|
|
const int ignore_index_;
|
|
const int block_size_;
|
|
gpuStream_t stream_;
|
|
};
|
|
|
|
template <typename DeviceContext, typename T>
|
|
void CrossEntropyFunctor<DeviceContext, T>::operator()(
|
|
const DeviceContext& dev_ctx,
|
|
DenseTensor* out,
|
|
const DenseTensor* prob,
|
|
const DenseTensor* labels,
|
|
const bool softLabel,
|
|
const int ignore_index,
|
|
const int64_t axis_dim) {
|
|
int64_t batch_size = prob->dims()[0];
|
|
int64_t class_num = prob->dims()[1];
|
|
|
|
// Handle zero-size tensor: early return to avoid invalid CUDA kernel launch
|
|
if (batch_size == 0 || class_num == 0) {
|
|
dev_ctx.template Alloc<T>(out);
|
|
return;
|
|
}
|
|
|
|
T* loss_data = dev_ctx.template Alloc<T>(out);
|
|
const T* prob_data = prob->data<T>();
|
|
// TODO(large-tensor): CUDA grid dims not support int64
|
|
PADDLE_ENFORCE_LE_INT_MAX(batch_size, "batch_size");
|
|
PADDLE_ENFORCE_LE_INT_MAX(class_num, "class_num");
|
|
int batch_size_int = static_cast<int>(batch_size);
|
|
int class_num_int = static_cast<int>(class_num);
|
|
|
|
constexpr int kMaxBlockDim = 512;
|
|
|
|
// big tensor currently not supported
|
|
PADDLE_ENFORCE_LE(out->numel(),
|
|
(1LL << 31) - 1,
|
|
::common::errors::PreconditionNotMet(
|
|
"out's numel too large "
|
|
"allowed size is 2 ^ 31 - 1 elements, but got %lld",
|
|
out->numel()));
|
|
|
|
if (softLabel) {
|
|
const T* label_data = labels->data<T>();
|
|
int block = class_num_int > kMaxBlockDim
|
|
? kMaxBlockDim
|
|
: pow(2, static_cast<int>(std::log2(class_num_int)));
|
|
|
|
SoftCrossEntropyKernel<T><<<batch_size_int, block, 0, dev_ctx.stream()>>>(
|
|
loss_data, prob_data, label_data, class_num_int);
|
|
} else {
|
|
HardLabelCrossEntropyCUDAFunctorImpl<T> functor(loss_data,
|
|
prob_data,
|
|
labels->data(),
|
|
batch_size_int,
|
|
class_num_int,
|
|
ignore_index,
|
|
kMaxBlockDim,
|
|
dev_ctx.stream());
|
|
phi::VisitDataType(labels->dtype(), functor);
|
|
}
|
|
}
|
|
|
|
template class CrossEntropyFunctor<GPUContext, float>;
|
|
template class CrossEntropyFunctor<GPUContext, double>;
|
|
template class CrossEntropyFunctor<GPUContext, phi::float16>;
|
|
#if defined(PADDLE_WITH_CUDA) && CUDNN_VERSION_MIN(8, 1, 0)
|
|
template class CrossEntropyFunctor<GPUContext, phi::bfloat16>;
|
|
#endif
|
|
|
|
} // namespace funcs
|
|
} // namespace phi
|