cxx11_tensor_of_float16_sycl.cpp File Reference
#include "main.h"
#include <unsupported/Eigen/CXX11/Tensor>

Macros

#define EIGEN_TEST_NO_LONGDOUBLE
 
#define EIGEN_TEST_NO_COMPLEX
 
#define EIGEN_DEFAULT_DENSE_INDEX_TYPE   int
 
#define EIGEN_USE_SYCL
 
#define EIGEN_SYCL_HALF_SUPPORT
 

Functions

void test_gpu_numext (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_conversion (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_unary (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_elementwise (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_trancendental (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_contractions (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_reductions (const Eigen::SyclDevice &sycl_device, int size1, int size2, int redux)
 
void test_gpu_reductions (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_full_reductions (const Eigen::SyclDevice &sycl_device)
 
void test_gpu_forced_evals (const Eigen::SyclDevice &sycl_device)
 
 EIGEN_DECLARE_TEST (cxx11_tensor_of_float16_sycl)
 

Macro Definition Documentation

◆ EIGEN_DEFAULT_DENSE_INDEX_TYPE

#define EIGEN_DEFAULT_DENSE_INDEX_TYPE   int

◆ EIGEN_SYCL_HALF_SUPPORT

#define EIGEN_SYCL_HALF_SUPPORT

◆ EIGEN_TEST_NO_COMPLEX

#define EIGEN_TEST_NO_COMPLEX

◆ EIGEN_TEST_NO_LONGDOUBLE

#define EIGEN_TEST_NO_LONGDOUBLE

◆ EIGEN_USE_SYCL

#define EIGEN_USE_SYCL

Function Documentation

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( cxx11_tensor_of_float16_sycl  )
377  {
378  for (const auto& s : Eigen::get_sycl_supported_devices()) {
379  QueueInterface queueInterface(s);
380  auto sycl_device = Eigen::SyclDevice(&queueInterface);
381 
382  CALL_SUBTEST_1(test_gpu_numext(sycl_device));
383  CALL_SUBTEST_1(test_gpu_conversion(sycl_device));
384  CALL_SUBTEST_1(test_gpu_unary(sycl_device));
385  CALL_SUBTEST_1(test_gpu_elementwise(sycl_device));
388  CALL_SUBTEST_3(test_gpu_reductions(sycl_device));
391  }
392 }
void test_gpu_reductions(const Eigen::SyclDevice &sycl_device, int size1, int size2, int redux)
Definition: cxx11_tensor_of_float16_sycl.cpp:263
void test_gpu_trancendental(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:139
void test_gpu_contractions(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:224
void test_gpu_forced_evals(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:340
void test_gpu_full_reductions(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:306
void test_gpu_elementwise(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:106
void test_gpu_conversion(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:54
void test_gpu_numext(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:26
void test_gpu_unary(const Eigen::SyclDevice &sycl_device)
Definition: cxx11_tensor_of_float16_sycl.cpp:79
RealScalar s
Definition: level1_cplx_impl.h:130
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, s, test_gpu_contractions(), test_gpu_conversion(), test_gpu_elementwise(), test_gpu_forced_evals(), test_gpu_full_reductions(), test_gpu_numext(), test_gpu_reductions(), test_gpu_trancendental(), and test_gpu_unary().

◆ test_gpu_contractions()

void test_gpu_contractions ( const Eigen::SyclDevice &  sycl_device)
224  {
225  int rows = 23;
226  int cols = 23;
227  int num_elem = rows * cols;
228 
229  float* d_float1 = (float*)sycl_device.allocate(num_elem * sizeof(float));
230  float* d_float2 = (float*)sycl_device.allocate(num_elem * sizeof(float));
231  Eigen::half* d_res_half = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
232  Eigen::half* d_res_float = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
233 
238 
239  gpu_float1.device(sycl_device) = gpu_float1.random() - gpu_float1.constant(0.5f);
240  gpu_float2.device(sycl_device) = gpu_float2.random() - gpu_float2.constant(0.5f);
241 
242  typedef typename Tensor<float, 2>::DimensionPair DimPair;
244  gpu_res_float.device(sycl_device) = gpu_float1.contract(gpu_float2, dims).cast<Eigen::half>();
245  gpu_res_half.device(sycl_device) = gpu_float1.cast<Eigen::half>().contract(gpu_float2.cast<Eigen::half>(), dims);
246 
247  Tensor<Eigen::half, 2> half_prec(rows, cols);
248  Tensor<Eigen::half, 2> full_prec(rows, cols);
249  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, num_elem * sizeof(Eigen::half));
250  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, num_elem * sizeof(Eigen::half));
251  sycl_device.synchronize();
252 
253  for (int i = 0; i < rows; ++i) {
254  for (int j = 0; j < cols; ++j) {
255  std::cout << "Checking contract " << i << " " << j << full_prec(i, j) << " " << half_prec(i, j) << std::endl;
256  if (numext::abs(full_prec(i, j) - half_prec(i, j)) > Eigen::half(1e-2f)) {
257  VERIFY_IS_APPROX(full_prec(i, j), half_prec(i, j));
258  }
259  }
260  }
261 }
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
int i
Definition: BiCGSTAB_step_by_step.cpp:9
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
static int num_elem(char *strv, unsigned elem_len, int term_char, int num_term)
Definition: cfortran.h:639
A tensor expression mapping an existing array of data.
Definition: TensorMap.h:33
The tensor class.
Definition: Tensor.h:68
Tensor< float, 1 >::DimensionPair DimPair
Definition: cxx11_tensor_contraction.cpp:17
@ Aligned
Definition: Constants.h:242
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
std::array< T, N > array
Definition: EmulateArray.h:231
Definition: Half.h:139
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References abs(), Eigen::Aligned, cols, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, j, num_elem(), rows, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_conversion()

void test_gpu_conversion ( const Eigen::SyclDevice &  sycl_device)
54  {
55  int num_elem = 101;
56 
57  float* d_float = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
58  Eigen::half* d_half = static_cast<Eigen::half*>(sycl_device.allocate(num_elem * sizeof(Eigen::half)));
59  float* d_conv = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
60 
64 
65  gpu_float.device(sycl_device) = gpu_float.random();
66  gpu_half.device(sycl_device) = gpu_float.cast<Eigen::half>();
67  gpu_conv.device(sycl_device) = gpu_half.cast<float>();
68 
69  Tensor<float, 1> initial(num_elem);
71  sycl_device.memcpyDeviceToHost(initial.data(), d_float, num_elem * sizeof(float));
72  sycl_device.memcpyDeviceToHost(final.data(), d_conv, num_elem * sizeof(float));
73 
74  for (int i = 0; i < num_elem; ++i) {
75  VERIFY_IS_APPROX(initial(i), final(i));
76  }
77 }
int data[]
Definition: Map_placement_new.cpp:1

References Eigen::Aligned, data, Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_elementwise()

void test_gpu_elementwise ( const Eigen::SyclDevice &  sycl_device)
106  {
107  int num_elem = 101;
108 
109  float* d_float1 = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
110  float* d_float2 = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
111  float* d_res_half = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
112  float* d_res_float = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
113 
116  Eigen::TensorMap<Tensor<float, 1>, Eigen::Aligned> gpu_res_half(d_res_half, num_elem);
117  Eigen::TensorMap<Tensor<float, 1>, Eigen::Aligned> gpu_res_float(d_res_float, num_elem);
118 
119  gpu_float1.device(sycl_device) = gpu_float1.random();
120  gpu_float2.device(sycl_device) = gpu_float2.random();
121  gpu_res_float.device(sycl_device) = (gpu_float1 + gpu_float2) * gpu_float1;
122  gpu_res_half.device(sycl_device) =
123  ((gpu_float1.cast<Eigen::half>() + gpu_float2.cast<Eigen::half>()) * gpu_float1.cast<Eigen::half>())
124  .cast<float>();
125 
126  Tensor<float, 1> half_prec(num_elem);
127  Tensor<float, 1> full_prec(num_elem);
128 
129  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, num_elem * sizeof(float));
130  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, num_elem * sizeof(float));
131 
132  for (int i = 0; i < num_elem; ++i) {
133  std::cout << "Checking elemwise " << i << ": full prec = " << full_prec(i) << " vs half prec = " << half_prec(i)
134  << std::endl;
135  VERIFY_IS_APPROX(static_cast<Eigen::half>(full_prec(i)), static_cast<Eigen::half>(half_prec(i)));
136  }
137 }

References Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_forced_evals()

void test_gpu_forced_evals ( const Eigen::SyclDevice &  sycl_device)
340  {
341  int num_elem = 101;
342 
343  float* d_float = (float*)sycl_device.allocate(num_elem * sizeof(float));
344  float* d_res_half1 = (float*)sycl_device.allocate(num_elem * sizeof(float));
345  float* d_res_half2 = (float*)sycl_device.allocate(num_elem * sizeof(float));
346  float* d_res_float = (float*)sycl_device.allocate(num_elem * sizeof(float));
347 
352 
353  Eigen::array<int, 1> no_bcast;
354  no_bcast[0] = 1;
355 
356  gpu_float.device(sycl_device) = gpu_float.random() - gpu_float.constant(0.5f);
357  gpu_res_float.device(sycl_device) = gpu_float.abs();
358  gpu_res_half1.device(sycl_device) = gpu_float.cast<Eigen::half>().abs().eval().cast<float>();
359  gpu_res_half2.device(sycl_device) = gpu_float.cast<Eigen::half>().abs().broadcast(no_bcast).eval().cast<float>();
360 
361  Tensor<float, 1> half_prec1(num_elem);
362  Tensor<float, 1> half_prec2(num_elem);
363  Tensor<float, 1> full_prec(num_elem);
364  sycl_device.memcpyDeviceToHost(half_prec1.data(), d_res_half1, num_elem * sizeof(float));
365  sycl_device.memcpyDeviceToHost(half_prec2.data(), d_res_half2, num_elem * sizeof(float));
366  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, num_elem * sizeof(float));
367  sycl_device.synchronize();
368 
369  for (int i = 0; i < num_elem; ++i) {
370  std::cout << "Checking forced eval " << i << full_prec(i) << " vs " << half_prec1(i) << " vs " << half_prec2(i)
371  << std::endl;
372  VERIFY_IS_APPROX(full_prec(i), half_prec1(i));
373  VERIFY_IS_APPROX(full_prec(i), half_prec2(i));
374  }
375 }
@ Unaligned
Definition: Constants.h:235

References abs(), Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), Eigen::Unaligned, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_full_reductions()

void test_gpu_full_reductions ( const Eigen::SyclDevice &  sycl_device)
306  {
307  int size = 13;
308  int num_elem = size * size;
309 
310  float* d_float = (float*)sycl_device.allocate(num_elem * sizeof(float));
311  Eigen::half* d_res_half = (Eigen::half*)sycl_device.allocate(1 * sizeof(Eigen::half));
312  Eigen::half* d_res_float = (Eigen::half*)sycl_device.allocate(1 * sizeof(Eigen::half));
313 
317 
318  gpu_float.device(sycl_device) = gpu_float.random();
319 
320  gpu_res_float.device(sycl_device) = gpu_float.sum().cast<Eigen::half>();
321  gpu_res_half.device(sycl_device) = gpu_float.cast<Eigen::half>().sum();
322 
323  Tensor<Eigen::half, 0> half_prec;
324  Tensor<Eigen::half, 0> full_prec;
325  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, sizeof(Eigen::half));
326  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, sizeof(Eigen::half));
327  sycl_device.synchronize();
328 
329  VERIFY_IS_APPROX(full_prec(), half_prec());
330 
331  gpu_res_float.device(sycl_device) = gpu_float.maximum().cast<Eigen::half>();
332  gpu_res_half.device(sycl_device) = gpu_float.cast<Eigen::half>().maximum();
333  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, sizeof(Eigen::half));
334  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, sizeof(Eigen::half));
335  sycl_device.synchronize();
336 
337  VERIFY_IS_APPROX(full_prec(), half_prec());
338 }
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:102

References Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), num_elem(), size, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_numext()

void test_gpu_numext ( const Eigen::SyclDevice &  sycl_device)
26  {
27  int num_elem = 101;
28 
29  float* d_float = static_cast<float*>(sycl_device.allocate(num_elem * sizeof(float)));
30  bool* d_res_half = static_cast<bool*>(sycl_device.allocate(num_elem * sizeof(bool)));
31  bool* d_res_float = static_cast<bool*>(sycl_device.allocate(num_elem * sizeof(bool)));
32 
34  Eigen::TensorMap<Tensor<bool, 1>, Eigen::Aligned> gpu_res_half(d_res_half, num_elem);
35  Eigen::TensorMap<Tensor<bool, 1>, Eigen::Aligned> gpu_res_float(d_res_float, num_elem);
36 
37  gpu_float.device(sycl_device) = gpu_float.random() - gpu_float.constant(0.5f);
38  gpu_res_float.device(sycl_device) = gpu_float.unaryExpr(Eigen::internal::scalar_isnan_op<float>());
39  gpu_res_half.device(sycl_device) =
40  gpu_float.cast<Eigen::half>().unaryExpr(Eigen::internal::scalar_isnan_op<Eigen::half>());
41 
42  Tensor<bool, 1> half_prec(num_elem);
43  Tensor<bool, 1> full_prec(num_elem);
44 
45  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, num_elem * sizeof(bool));
46  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, num_elem * sizeof(bool));
47 
48  for (int i = 0; i < num_elem; ++i) {
49  std::cout << "Checking numext " << i << std::endl;
50  VERIFY_IS_EQUAL(full_prec(i), half_prec(i));
51  }
52 }
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:367
Template functor to compute whether a scalar is NaN.
Definition: functors/UnaryFunctors.h:994

References Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), and VERIFY_IS_EQUAL.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_reductions() [1/2]

void test_gpu_reductions ( const Eigen::SyclDevice &  sycl_device)
295  {
296  test_gpu_reductions(sycl_device, 13, 13, 0);
297  test_gpu_reductions(sycl_device, 13, 13, 1);
298 
299  test_gpu_reductions(sycl_device, 35, 36, 0);
300  test_gpu_reductions(sycl_device, 35, 36, 1);
301 
302  test_gpu_reductions(sycl_device, 36, 35, 0);
303  test_gpu_reductions(sycl_device, 36, 35, 1);
304 }

References test_gpu_reductions().

◆ test_gpu_reductions() [2/2]

void test_gpu_reductions ( const Eigen::SyclDevice &  sycl_device,
int  size1,
int  size2,
int  redux 
)
263  {
264  std::cout << "Reducing " << size1 << " by " << size2 << " tensor along dim " << redux << std::endl;
265 
266  int num_elem = size1 * size2;
267  int result_size = (redux == 1 ? size1 : size2);
268 
269  float* d_float = (float*)sycl_device.allocate(num_elem * sizeof(float));
270  Eigen::half* d_res_half = (Eigen::half*)sycl_device.allocate(result_size * sizeof(Eigen::half));
271  Eigen::half* d_res_float = (Eigen::half*)sycl_device.allocate(result_size * sizeof(Eigen::half));
272 
273  Eigen::TensorMap<Eigen::Tensor<float, 2>, Eigen::Aligned> gpu_float(d_float, size1, size2);
274  Eigen::TensorMap<Eigen::Tensor<Eigen::half, 1>, Eigen::Aligned> gpu_res_half(d_res_half, result_size);
275  Eigen::TensorMap<Eigen::Tensor<Eigen::half, 1>, Eigen::Aligned> gpu_res_float(d_res_float, result_size);
276 
277  gpu_float.device(sycl_device) = gpu_float.random() * 2.0f;
278 
279  Eigen::array<int, 1> redux_dim = {redux};
280  gpu_res_float.device(sycl_device) = gpu_float.sum(redux_dim).cast<Eigen::half>();
281  gpu_res_half.device(sycl_device) = gpu_float.cast<Eigen::half>().sum(redux_dim);
282 
283  Tensor<Eigen::half, 1> half_prec(result_size);
284  Tensor<Eigen::half, 1> full_prec(result_size);
285  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, result_size * sizeof(Eigen::half));
286  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, result_size * sizeof(Eigen::half));
287  sycl_device.synchronize();
288 
289  for (int i = 0; i < result_size; ++i) {
290  std::cout << "EXPECTED " << full_prec(i) << " GOT " << half_prec(i) << std::endl;
291  VERIFY_IS_APPROX(full_prec(i), half_prec(i));
292  }
293 }

References Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST(), and test_gpu_reductions().

◆ test_gpu_trancendental()

void test_gpu_trancendental ( const Eigen::SyclDevice &  sycl_device)
139  {
140  int num_elem = 101;
141 
142  float* d_float1 = (float*)sycl_device.allocate(num_elem * sizeof(float));
143  float* d_float2 = (float*)sycl_device.allocate(num_elem * sizeof(float));
144  float* d_float3 = (float*)sycl_device.allocate(num_elem * sizeof(float));
145  Eigen::half* d_res1_half = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
146  Eigen::half* d_res1_float = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
147  Eigen::half* d_res2_half = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
148  Eigen::half* d_res2_float = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
149  Eigen::half* d_res3_half = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
150  Eigen::half* d_res3_float = (Eigen::half*)sycl_device.allocate(num_elem * sizeof(Eigen::half));
151 
163 
164  gpu_float1.device(sycl_device) = gpu_float1.random() - gpu_float1.constant(0.5f);
165  gpu_float2.device(sycl_device) = gpu_float2.random() + gpu_float1.constant(0.5f);
166  gpu_float3.device(sycl_device) = gpu_float3.random();
167  gpu_res1_float.device(sycl_device) = gpu_float1.exp().cast<Eigen::half>();
168  gpu_res2_float.device(sycl_device) = gpu_float2.log().cast<Eigen::half>();
169  gpu_res3_float.device(sycl_device) = gpu_float3.log1p().cast<Eigen::half>();
170  gpu_res4_float.device(sycl_device) = gpu_float3.expm1().cast<Eigen::half>();
171 
172  gpu_res1_half.device(sycl_device) = gpu_float1.cast<Eigen::half>();
173  gpu_res1_half.device(sycl_device) = gpu_res1_half.exp();
174 
175  gpu_res2_half.device(sycl_device) = gpu_float2.cast<Eigen::half>();
176  gpu_res2_half.device(sycl_device) = gpu_res2_half.log();
177 
178  gpu_res3_half.device(sycl_device) = gpu_float3.cast<Eigen::half>();
179  gpu_res3_half.device(sycl_device) = gpu_res3_half.log1p();
180 
181  gpu_res3_half.device(sycl_device) = gpu_float3.cast<Eigen::half>();
182  gpu_res3_half.device(sycl_device) = gpu_res3_half.expm1();
183 
184  Tensor<float, 1> input1(num_elem);
185  Tensor<Eigen::half, 1> half_prec1(num_elem);
186  Tensor<Eigen::half, 1> full_prec1(num_elem);
187  Tensor<float, 1> input2(num_elem);
188  Tensor<Eigen::half, 1> half_prec2(num_elem);
189  Tensor<Eigen::half, 1> full_prec2(num_elem);
190  Tensor<float, 1> input3(num_elem);
191  Tensor<Eigen::half, 1> half_prec3(num_elem);
192  Tensor<Eigen::half, 1> full_prec3(num_elem);
193  sycl_device.memcpyDeviceToHost(input1.data(), d_float1, num_elem * sizeof(float));
194  sycl_device.memcpyDeviceToHost(input2.data(), d_float2, num_elem * sizeof(float));
195  sycl_device.memcpyDeviceToHost(input3.data(), d_float3, num_elem * sizeof(float));
196  sycl_device.memcpyDeviceToHost(half_prec1.data(), d_res1_half, num_elem * sizeof(Eigen::half));
197  sycl_device.memcpyDeviceToHost(full_prec1.data(), d_res1_float, num_elem * sizeof(Eigen::half));
198  sycl_device.memcpyDeviceToHost(half_prec2.data(), d_res2_half, num_elem * sizeof(Eigen::half));
199  sycl_device.memcpyDeviceToHost(full_prec2.data(), d_res2_float, num_elem * sizeof(Eigen::half));
200  sycl_device.memcpyDeviceToHost(half_prec3.data(), d_res3_half, num_elem * sizeof(Eigen::half));
201  sycl_device.memcpyDeviceToHost(full_prec3.data(), d_res3_float, num_elem * sizeof(Eigen::half));
202  sycl_device.synchronize();
203 
204  for (int i = 0; i < num_elem; ++i) {
205  std::cout << "Checking elemwise exp " << i << " input = " << input1(i) << " full = " << full_prec1(i)
206  << " half = " << half_prec1(i) << std::endl;
207  VERIFY_IS_APPROX(full_prec1(i), half_prec1(i));
208  }
209  for (int i = 0; i < num_elem; ++i) {
210  std::cout << "Checking elemwise log " << i << " input = " << input2(i) << " full = " << full_prec2(i)
211  << " half = " << half_prec2(i) << std::endl;
212  if (std::abs(input2(i) - 1.f) < 0.05f) // log lacks accuracy nearby 1
213  VERIFY_IS_APPROX(full_prec2(i) + Eigen::half(0.1f), half_prec2(i) + Eigen::half(0.1f));
214  else
215  VERIFY_IS_APPROX(full_prec2(i), half_prec2(i));
216  }
217  for (int i = 0; i < num_elem; ++i) {
218  std::cout << "Checking elemwise plog1 " << i << " input = " << input3(i) << " full = " << full_prec3(i)
219  << " half = " << half_prec3(i) << std::endl;
220  VERIFY_IS_APPROX(full_prec3(i), half_prec3(i));
221  }
222 }

References abs(), Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().

◆ test_gpu_unary()

void test_gpu_unary ( const Eigen::SyclDevice &  sycl_device)
79  {
80  int num_elem = 101;
81 
82  float* d_float = (float*)sycl_device.allocate(num_elem * sizeof(float));
83  float* d_res_half = (float*)sycl_device.allocate(num_elem * sizeof(float));
84  float* d_res_float = (float*)sycl_device.allocate(num_elem * sizeof(float));
85 
89 
90  gpu_float.device(sycl_device) = gpu_float.random() - gpu_float.constant(0.5f);
91  gpu_res_float.device(sycl_device) = gpu_float.abs();
92  gpu_res_half.device(sycl_device) = gpu_float.cast<Eigen::half>().abs().cast<float>();
93 
94  Tensor<float, 1> half_prec(num_elem);
95  Tensor<float, 1> full_prec(num_elem);
96  sycl_device.memcpyDeviceToHost(half_prec.data(), d_res_half, num_elem * sizeof(float));
97  sycl_device.memcpyDeviceToHost(full_prec.data(), d_res_float, num_elem * sizeof(float));
98  sycl_device.synchronize();
99 
100  for (int i = 0; i < num_elem; ++i) {
101  std::cout << "Checking unary " << i << std::endl;
102  VERIFY_IS_APPROX(full_prec(i), half_prec(i));
103  }
104 }

References abs(), Eigen::Aligned, Eigen::Tensor< Scalar_, NumIndices_, Options_, IndexType_ >::data(), Eigen::TensorBase< Derived, AccessLevel >::device(), i, num_elem(), and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().