mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Made half floats usable on hardware that doesn't support them natively.
This commit is contained in:
@@ -19,30 +19,28 @@
|
||||
|
||||
using Eigen::Tensor;
|
||||
|
||||
#ifdef EIGEN_HAS_CUDA_FP16
|
||||
|
||||
void test_cuda_conversion() {
|
||||
Eigen::CudaStreamDevice stream;
|
||||
Eigen::GpuDevice gpu_device(&stream);
|
||||
int num_elem = 101;
|
||||
|
||||
|
||||
Tensor<float, 1> floats(num_elem);
|
||||
floats.setRandom();
|
||||
|
||||
float* d_float = (float*)gpu_device.allocate(num_elem * sizeof(float));
|
||||
half* d_half = (half*)gpu_device.allocate(num_elem * sizeof(half));
|
||||
Eigen::half* d_half = (Eigen::half*)gpu_device.allocate(num_elem * sizeof(Eigen::half));
|
||||
float* d_conv = (float*)gpu_device.allocate(num_elem * sizeof(float));
|
||||
|
||||
Eigen::TensorMap<Eigen::Tensor<float, 1>, Eigen::Aligned> gpu_float(
|
||||
d_float, num_elem);
|
||||
Eigen::TensorMap<Eigen::Tensor<half, 1>, Eigen::Aligned> gpu_half(
|
||||
Eigen::TensorMap<Eigen::Tensor<Eigen::half, 1>, Eigen::Aligned> gpu_half(
|
||||
d_half, num_elem);
|
||||
Eigen::TensorMap<Eigen::Tensor<float, 1>, Eigen::Aligned> gpu_conv(
|
||||
d_conv, num_elem);
|
||||
|
||||
gpu_device.memcpyHostToDevice(d_float, floats.data(), num_elem*sizeof(float));
|
||||
|
||||
gpu_half.device(gpu_device) = gpu_float.cast<half>();
|
||||
gpu_half.device(gpu_device) = gpu_float.cast<Eigen::half>();
|
||||
gpu_conv.device(gpu_device) = gpu_half.cast<float>();
|
||||
|
||||
Tensor<float, 1> initial(num_elem);
|
||||
@@ -60,14 +58,8 @@ void test_cuda_conversion() {
|
||||
gpu_device.deallocate(d_conv);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void test_cxx11_tensor_cast_float16_cuda()
|
||||
{
|
||||
#ifdef EIGEN_HAS_CUDA_FP16
|
||||
CALL_SUBTEST(test_cuda_conversion());
|
||||
#else
|
||||
std::cout << "Half floats are not supported by this version of cuda: skipping the test" << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user