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 <thrust/execution_policy.h>
14 #include <thrust/transform.h>
15
16 #ifdef FAISS_USE_FLOAT16
17
18 namespace faiss { namespace gpu {
19
20 bool getDeviceSupportsFloat16Math(int device) {
21 const auto& prop = getDeviceProperties(device);
22
23 return (prop.major >= 6 ||
24 (prop.major == 5 && prop.minor >= 3));
25 }
26
27 struct FloatToHalf {
28 __device__ half operator()(float v) const { return __float2half(v); }
29 };
30
31 struct HalfToFloat {
32 __device__ float operator()(half v) const { return __half2float(v); }
33 };
34
35 void runConvertToFloat16(half* out,
36 const float* in,
37 size_t num,
38 cudaStream_t stream) {
39 thrust::transform(thrust::cuda::par.on(stream),
40 in, in + num, out, FloatToHalf());
41 }
42
43 void runConvertToFloat32(float* out,
44 const half* in,
45 size_t num,
46 cudaStream_t stream) {
47 thrust::transform(thrust::cuda::par.on(stream),
48 in, in + num, out, HalfToFloat());
49 }
50
51 // FIXME: replace
52 /*
53 Copyright (c) 2015, Norbert Juffa
54 All rights reserved.
55 Redistribution and use in source and binary forms, with or without
56 modification, are permitted provided that the following conditions
57 are met:
58 1. Redistributions of source code must retain the above copyright
59 notice, this list of conditions and the following disclaimer.
60 2. Redistributions in binary form must reproduce the above copyright
61 notice, this list of conditions and the following disclaimer in the
62 documentation and/or other materials provided with the distribution.
63 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 */
75
76 half hostFloat2Half(float a) {
77 uint32_t ia;
78 uint16_t ir;
79 memcpy(&ia, &a, sizeof(float));
80
81 ir = (ia >> 16) & 0x8000;
82 if ((ia & 0x7f800000) == 0x7f800000) {
83 if ((ia & 0x7fffffff) == 0x7f800000) {
84 ir |= 0x7c00; /* infinity */
85 } else {
86 ir = 0x7fff; /* canonical NaN */
87 }
88 } else if ((ia & 0x7f800000) >= 0x33000000) {
89 int shift = (int)((ia >> 23) & 0xff) - 127;
90 if (shift > 15) {
91 ir |= 0x7c00; /* infinity */
92 } else {
93 ia = (ia & 0x007fffff) | 0x00800000; /* extract mantissa */
94 if (shift < -14) { /* denormal */
95 ir |= ia >> (-1 - shift);
96 ia = ia << (32 - (-1 - shift));
97 } else { /* normal */
98 ir |= ia >> (24 - 11);
99 ia = ia << (32 - (24 - 11));
100 ir = ir + ((14 + shift) << 10);
101 }
102 /* IEEE-754 round to nearest of even */
103 if ((ia > 0x80000000) || ((ia == 0x80000000) && (ir & 1))) {
104 ir++;
105 }
106 }
107 }
108
109 half ret;
110 memcpy(&ret, &ir, sizeof(half));
111 return ret;
112 }
113
114 } } // namespace
115
116 #endif // FAISS_USE_FLOAT16