Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
Float16.cu
1
2 /**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the CC-by-NC license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10 // Copyright 2004-present Facebook. All Rights Reserved.
11
12 #include "Float16.cuh"
13 #include "nvidia/fp16_emu.cuh"
14 #include <thrust/execution_policy.h>
15 #include <thrust/transform.h>
16
17 #ifdef FAISS_USE_FLOAT16
18
19 namespace faiss { namespace gpu {
20
21 bool getDeviceSupportsFloat16Math(int device) {
22 const auto& prop = getDeviceProperties(device);
23
24 return (prop.major >= 6 ||
25 (prop.major == 5 && prop.minor >= 3));
26 }
27
28 struct FloatToHalf {
29 __device__ half operator()(float v) const { return __float2half(v); }
30 };
31
32 struct HalfToFloat {
33 __device__ float operator()(half v) const { return __half2float(v); }
34 };
35
36 void runConvertToFloat16(half* out,
37 const float* in,
38 size_t num,
39 cudaStream_t stream) {
40 thrust::transform(thrust::cuda::par.on(stream),
41 in, in + num, out, FloatToHalf());
42 }
43
44 void runConvertToFloat32(float* out,
45 const half* in,
46 size_t num,
47 cudaStream_t stream) {
48 thrust::transform(thrust::cuda::par.on(stream),
49 in, in + num, out, HalfToFloat());
50 }
51
52 half hostFloat2Half(float a) {
53 half h;
54 h.x = cpu_float2half_rn(a).x;
55 return h;
56 }
57
58 } } // namespace
59
60 #endif // FAISS_USE_FLOAT16