/root/doris/contrib/faiss/faiss/impl/CodePacker.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <faiss/MetricType.h> |
11 | | |
12 | | namespace faiss { |
13 | | |
14 | | /** |
15 | | * Packing consists in combining a fixed number of codes of constant size |
16 | | * (code_size) into a block of data where they may (or may not) be interleaved |
17 | | * for efficient consumption by distance computation kernels. This exists for |
18 | | * the "fast_scan" indexes on CPU and for some GPU kernels. |
19 | | */ |
20 | | struct CodePacker { |
21 | | size_t code_size; // input code size in bytes |
22 | | size_t nvec; // number of vectors per block |
23 | | size_t block_size; // size of one block in bytes (>= code_size * nvec) |
24 | | |
25 | | // pack a single code to a block |
26 | | virtual void pack_1( |
27 | | const uint8_t* |
28 | | flat_code, // code to write to the block, size code_size |
29 | | size_t offset, // offset in the block (0 <= offset < nvec) |
30 | | uint8_t* block // block to write to (size block_size) |
31 | | ) const = 0; |
32 | | |
33 | | // unpack a single code from a block |
34 | | virtual void unpack_1( |
35 | | const uint8_t* block, // block to read from (size block_size) |
36 | | size_t offset, // offset in the block (0 <= offset < nvec) |
37 | | uint8_t* flat_code // where to write the resulting code, size |
38 | | // code_size |
39 | | ) const = 0; |
40 | | |
41 | | // pack all code in a block |
42 | | virtual void pack_all( |
43 | | const uint8_t* flat_codes, // codes to write to the block, size |
44 | | // (nvec * code_size) |
45 | | uint8_t* block // block to write to (size block_size) |
46 | | ) const; |
47 | | |
48 | | // unpack all code in a block |
49 | | virtual void unpack_all( |
50 | | const uint8_t* block, // block to read from (size block_size) |
51 | | uint8_t* flat_codes // where to write the resulting codes size (nvec |
52 | | // * code_size) |
53 | | ) const; |
54 | | |
55 | 0 | virtual ~CodePacker() {} |
56 | | }; |
57 | | |
58 | | /** Trivial code packer where codes are stored one by one */ |
59 | | struct CodePackerFlat : CodePacker { |
60 | | explicit CodePackerFlat(size_t code_size); |
61 | | |
62 | | void pack_1(const uint8_t* flat_code, size_t offset, uint8_t* block) |
63 | | const final; |
64 | | void unpack_1(const uint8_t* block, size_t offset, uint8_t* flat_code) |
65 | | const final; |
66 | | |
67 | | void pack_all(const uint8_t* flat_codes, uint8_t* block) const final; |
68 | | void unpack_all(const uint8_t* block, uint8_t* flat_codes) const final; |
69 | | }; |
70 | | |
71 | | } // namespace faiss |