Add Half conversion of bit cast for SYCL kernel (#64340)
authorjohnlu <chengjun.lu@intel.com>
Wed, 8 Sep 2021 15:24:46 +0000 (08:24 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Wed, 8 Sep 2021 15:25:47 +0000 (08:25 -0700)
Summary:
## Motivation
Enhance the performance of Half/float conversion in SYCL kernels.

## Solution
Add the native SYCL half type to help convert the half from/to float in the kernel code.

## Additional Context
`__SYCL_DEVICE_ONLY__` is a MACRO only valid when compiling the kernel code for SYCL backend.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/64340

Reviewed By: gchanan

Differential Revision: D30720823

Pulled By: ezyang

fbshipit-source-id: e7e770d02df5b2d45da61d2fed3ba59383b3dc3a

c10/util/Half-inl.h

index 5547874..1982dc7 100644 (file)
 #include <hip/hip_fp16.h>
 #endif
 
+#ifdef __SYCL_DEVICE_ONLY__
+#include <CL/sycl.hpp>
+#endif
+
 namespace c10 {
 
 /// Constructors
@@ -19,6 +23,8 @@ namespace c10 {
 inline C10_HOST_DEVICE Half::Half(float value) {
 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
   x = __half_as_short(__float2half(value));
+#elif defined(__SYCL_DEVICE_ONLY__)
+  x = sycl::bit_cast<uint16_t>(sycl::half(value));
 #else
   x = detail::fp16_ieee_from_fp32_value(value);
 #endif
@@ -29,6 +35,8 @@ inline C10_HOST_DEVICE Half::Half(float value) {
 inline C10_HOST_DEVICE Half::operator float() const {
 #if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__)
   return __half2float(*reinterpret_cast<const __half*>(&x));
+#elif defined(__SYCL_DEVICE_ONLY__)
+  return float(sycl::bit_cast<sycl::half>(x));
 #else
   return detail::fp16_ieee_to_fp32_value(x);
 #endif