parent
40aee48a80
commit
97509b68e7
@ -0,0 +1,59 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/operators/math/cross_entropy.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace math {
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
template <typename T, int MajorType = Eigen::RowMajor,
|
||||
typename IndexType = Eigen::DenseIndex>
|
||||
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
|
||||
|
||||
template <typename T>
|
||||
class CrossEntropyFunctor<platform::CPUPlace, T> {
|
||||
public:
|
||||
void operator()(const framework::ExecutionContext& ctx,
|
||||
framework::Tensor* out, const framework::Tensor* prob,
|
||||
const framework::Tensor* labels, const bool softLabel) {
|
||||
const int batch_size = prob->dims()[0];
|
||||
if (softLabel) {
|
||||
auto in = EigenMatrix<T>::From(*prob);
|
||||
auto lbl = EigenMatrix<T>::From(*labels);
|
||||
auto loss = EigenMatrix<T>::From(*out);
|
||||
|
||||
loss.device(ctx.GetEigenDevice<platform::CPUPlace>()) =
|
||||
-((lbl * in.log().unaryExpr(math::TolerableValue<T>()))
|
||||
.sum(Eigen::DSizes<int, 1>(1))
|
||||
.reshape(Eigen::DSizes<int, 2>(batch_size, 1)));
|
||||
} else {
|
||||
const int class_num = prob->dims()[1];
|
||||
const T* prob_data = prob->data<T>();
|
||||
T* loss_data = out->data<T>();
|
||||
|
||||
const int* label_data = labels->data<int>();
|
||||
for (int i = 0; i < batch_size; ++i) {
|
||||
int index = i * class_num + label_data[i];
|
||||
loss_data[i] = -math::TolerableValue<T>()(std::log(prob_data[index]));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template class CrossEntropyFunctor<platform::CPUPlace, float>;
|
||||
} // namespace math
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,111 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/operators/math/cross_entropy.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace math {
|
||||
|
||||
namespace {
|
||||
template <typename T>
|
||||
__global__ void CrossEntropyKernel(T* Y, const T* X, const int* label,
|
||||
const int N, const int D) {
|
||||
// TOOD(qingqing) define CUDA_1D_KERNEL_LOOP macro in a common file.
|
||||
// CUDA_1D_KERNEL_LOOP(i, N) {
|
||||
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < N;
|
||||
i += blockDim.x * gridDim.x) {
|
||||
PADDLE_ASSERT(label[i] >= 0 && label[i] < D);
|
||||
Y[i] = -math::TolerableValue<T>()(log(X[i * D + label[i]]));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
__device__ __forceinline__ T sum_single_warp(T val) {
|
||||
val += __shfl_down(val, 16);
|
||||
val += __shfl_down(val, 8);
|
||||
val += __shfl_down(val, 4);
|
||||
val += __shfl_down(val, 2);
|
||||
val += __shfl_down(val, 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
__global__ void SoftCrossEntropyKernel(T* Y, const T* X, const T* label,
|
||||
const int class_num) {
|
||||
int tid = threadIdx.x;
|
||||
extern __shared__ T d_sum[];
|
||||
d_sum[tid] = 0;
|
||||
|
||||
int cur_idx = tid;
|
||||
int next_idx = blockIdx.x * class_num + tid;
|
||||
while (cur_idx < class_num) {
|
||||
d_sum[tid] +=
|
||||
math::TolerableValue<T>()(std::log(X[next_idx])) * label[next_idx];
|
||||
next_idx += blockDim.x;
|
||||
cur_idx += blockDim.x;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
for (unsigned int stride = blockDim.x >> 1; stride >= 32; stride >>= 1) {
|
||||
if (tid < stride) d_sum[tid] += d_sum[tid + stride];
|
||||
__syncthreads();
|
||||
}
|
||||
|
||||
T val = d_sum[tid];
|
||||
val = sum_single_warp<T>(val);
|
||||
if (tid == 0) Y[blockIdx.x] = -val;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
using Tensor = framework::Tensor;
|
||||
|
||||
template <typename T>
|
||||
class CrossEntropyFunctor<platform::GPUPlace, T> {
|
||||
public:
|
||||
void operator()(const framework::ExecutionContext& ctx,
|
||||
framework::Tensor* out, const framework::Tensor* prob,
|
||||
const framework::Tensor* labels, bool softLabel) {
|
||||
const T* prob_data = prob->data<T>();
|
||||
T* loss_data = out->mutable_data<T>(ctx.GetPlace());
|
||||
|
||||
int batch_size = prob->dims()[0];
|
||||
int class_num = prob->dims()[1];
|
||||
|
||||
if (softLabel) {
|
||||
const T* label_data = labels->data<T>();
|
||||
int block = class_num > 512 ? 512 : pow(2, int(std::log2(class_num)));
|
||||
|
||||
SoftCrossEntropyKernel<
|
||||
T><<<batch_size, block, block * sizeof(T),
|
||||
reinterpret_cast<const platform::CUDADeviceContext&>(
|
||||
ctx.device_context())
|
||||
.stream()>>>(loss_data, prob_data, label_data, class_num);
|
||||
} else {
|
||||
const int* label_data = labels->data<int>();
|
||||
int block = 512;
|
||||
int grid = (batch_size + block - 1) / block;
|
||||
CrossEntropyKernel<T><<<
|
||||
grid, block, 0, reinterpret_cast<const platform::CUDADeviceContext&>(
|
||||
ctx.device_context())
|
||||
.stream()>>>(loss_data, prob_data, label_data,
|
||||
batch_size, class_num);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template class CrossEntropyFunctor<platform::GPUPlace, float>;
|
||||
} // namespace math
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
@ -0,0 +1,48 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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. */
|
||||
|
||||
#pragma once
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/operator.h"
|
||||
#include "paddle/framework/tensor.h"
|
||||
#include "paddle/platform/hostdevice.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
namespace math {
|
||||
|
||||
template <typename T>
|
||||
struct TolerableValue {
|
||||
HOSTDEVICE T operator()(const T& x) const {
|
||||
PADDLE_ASSERT(std::is_floating_point<T>::value);
|
||||
const T kApproInf = 1e20;
|
||||
|
||||
if (x == INFINITY) return kApproInf;
|
||||
if (x == -INFINITY) return -kApproInf;
|
||||
return x;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Place, typename T>
|
||||
class CrossEntropyFunctor {
|
||||
public:
|
||||
// (TODO caoying) it is much better to use DeviceContext as the first
|
||||
// parameter.
|
||||
void operator()(const framework::ExecutionContext& context,
|
||||
framework::Tensor* out, const framework::Tensor* prob,
|
||||
const framework::Tensor* labels, const bool softLabel);
|
||||
};
|
||||
} // namespace math
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Loading…
Reference in new issue