10 #include "Float16.cuh"
11 #include "nvidia/fp16_emu.cuh"
12 #include <thrust/execution_policy.h>
13 #include <thrust/transform.h>
15 #ifdef FAISS_USE_FLOAT16
17 namespace faiss {
namespace gpu {
19 bool getDeviceSupportsFloat16Math(
int device) {
20 const auto& prop = getDeviceProperties(device);
22 return (prop.major >= 6 ||
23 (prop.major == 5 && prop.minor >= 3));
27 __device__ half operator()(
float v)
const {
return __float2half(v); }
31 __device__
float operator()(half v)
const {
return __half2float(v); }
34 void runConvertToFloat16(half* out,
37 cudaStream_t stream) {
38 thrust::transform(thrust::cuda::par.on(stream),
39 in, in + num, out, FloatToHalf());
42 void runConvertToFloat32(
float* out,
45 cudaStream_t stream) {
46 thrust::transform(thrust::cuda::par.on(stream),
47 in, in + num, out, HalfToFloat());
50 __half hostFloat2Half(
float a) {
51 #if CUDA_VERSION >= 9000
53 raw.x = cpu_float2half_rn(a).x;
57 h.x = cpu_float2half_rn(a).x;
64 #endif // FAISS_USE_FLOAT16