10 #include "nvidia/fp16_emu.cuh"
11 #include <thrust/execution_policy.h>
12 #include <thrust/transform.h>
14 #ifdef FAISS_USE_FLOAT16
16 namespace faiss {
namespace gpu {
18 bool getDeviceSupportsFloat16Math(
int device) {
19 const auto& prop = getDeviceProperties(device);
21 return (prop.major >= 6 ||
22 (prop.major == 5 && prop.minor >= 3));
26 __device__ half operator()(
float v)
const {
return __float2half(v); }
30 __device__
float operator()(half v)
const {
return __half2float(v); }
33 void runConvertToFloat16(half* out,
36 cudaStream_t stream) {
37 thrust::transform(thrust::cuda::par.on(stream),
38 in, in + num, out, FloatToHalf());
41 void runConvertToFloat32(
float* out,
44 cudaStream_t stream) {
45 thrust::transform(thrust::cuda::par.on(stream),
46 in, in + num, out, HalfToFloat());
49 __half hostFloat2Half(
float a) {
50 #if CUDA_VERSION >= 9000
52 raw.x = cpu_float2half_rn(a).x;
56 h.x = cpu_float2half_rn(a).x;
63 #endif // FAISS_USE_FLOAT16