1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #define EIGEN_TEST_NO_LONGDOUBLE
11 #define EIGEN_TEST_NO_COMPLEX
12 #define EIGEN_TEST_FUNC cxx11_tensor_cast_float16_cuda
13 #define EIGEN_DEFAULT_DENSE_INDEX_TYPE int
14 #define EIGEN_USE_GPU
15 
16 #if defined __CUDACC_VER__ && __CUDACC_VER__ >= 70500
17 #include <cuda_fp16.h>
18 #endif
19 #include "main.h"
20 #include <unsupported/Eigen/CXX11/Tensor>
21 
22 using Eigen::Tensor;
23 
test_cuda_conversion()24 void test_cuda_conversion() {
25   Eigen::CudaStreamDevice stream;
26   Eigen::GpuDevice gpu_device(&stream);
27   int num_elem = 101;
28 
29   Tensor<float, 1> floats(num_elem);
30   floats.setRandom();
31 
32   float* d_float = (float*)gpu_device.allocate(num_elem * sizeof(float));
33   Eigen::half* d_half = (Eigen::half*)gpu_device.allocate(num_elem * sizeof(Eigen::half));
34   float* d_conv = (float*)gpu_device.allocate(num_elem * sizeof(float));
35 
36   Eigen::TensorMap<Eigen::Tensor<float, 1>, Eigen::Aligned> gpu_float(
37       d_float, num_elem);
38   Eigen::TensorMap<Eigen::Tensor<Eigen::half, 1>, Eigen::Aligned> gpu_half(
39       d_half, num_elem);
40   Eigen::TensorMap<Eigen::Tensor<float, 1>, Eigen::Aligned> gpu_conv(
41       d_conv, num_elem);
42 
43   gpu_device.memcpyHostToDevice(d_float, floats.data(), num_elem*sizeof(float));
44 
45   gpu_half.device(gpu_device) = gpu_float.cast<Eigen::half>();
46   gpu_conv.device(gpu_device) = gpu_half.cast<float>();
47 
48   Tensor<float, 1> initial(num_elem);
49   Tensor<float, 1> final(num_elem);
50   gpu_device.memcpyDeviceToHost(initial.data(), d_float, num_elem*sizeof(float));
51   gpu_device.memcpyDeviceToHost(final.data(), d_conv, num_elem*sizeof(float));
52   gpu_device.synchronize();
53 
54   for (int i = 0; i < num_elem; ++i) {
55     VERIFY_IS_APPROX(initial(i), final(i));
56   }
57 
58   gpu_device.deallocate(d_float);
59   gpu_device.deallocate(d_half);
60   gpu_device.deallocate(d_conv);
61 }
62 
63 
test_fallback_conversion()64 void test_fallback_conversion() {
65   int num_elem = 101;
66   Tensor<float, 1> floats(num_elem);
67   floats.setRandom();
68 
69   Eigen::Tensor<Eigen::half, 1> halfs = floats.cast<Eigen::half>();
70   Eigen::Tensor<float, 1> conv = halfs.cast<float>();
71 
72   for (int i = 0; i < num_elem; ++i) {
73     VERIFY_IS_APPROX(floats(i), conv(i));
74   }
75 }
76 
77 
test_cxx11_tensor_cast_float16_cuda()78 void test_cxx11_tensor_cast_float16_cuda()
79 {
80   CALL_SUBTEST(test_cuda_conversion());
81   CALL_SUBTEST(test_fallback_conversion());
82 }
83