Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
F
faiss
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
faiss
Commits
01b2224b
Commit
01b2224b
authored
Jul 18, 2017
by
matthijs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
forgotten sources
parent
f7aedbdf
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
161 additions
and
0 deletions
+161
-0
IndexScalarQuantizer.cpp
IndexScalarQuantizer.cpp
+0
-0
IndexScalarQuantizer.h
IndexScalarQuantizer.h
+161
-0
No files found.
IndexScalarQuantizer.cpp
0 → 100644
View file @
01b2224b
This diff is collapsed.
Click to expand it.
IndexScalarQuantizer.h
0 → 100644
View file @
01b2224b
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the CC-by-NC license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifndef FAISS_INDEX_SCALAR_QUANTIZER_H
#define FAISS_INDEX_SCALAR_QUANTIZER_H
#include <stdint.h>
#include <vector>
#include "IndexIVF.h"
namespace
faiss
{
/**
* The uniform quantizer has a range [vmin, vmax]. The range can be
* the same for all dimensions (uniform) or specific per dimension
* (default).
*/
struct
ScalarQuantizer
{
enum
QuantizerType
{
QT_8bit
,
///< 8 bits per component
QT_4bit
,
///< 4 bits per component
QT_8bit_uniform
,
///< same, shared range for all dimensions
QT_4bit_uniform
,
};
QuantizerType
qtype
;
/** The uniform encoder can estimate the range of representable
* values of the unform encoder using different statistics. Here
* rs = rangestat_arg */
// rangestat_arg.
enum
RangeStat
{
RS_minmax
,
///< [min - rs*(max-min), max + rs*(max-min)]
RS_meanstd
,
///< [mean - std * rs, mean + std * rs]
RS_quantiles
,
///< [Q(rs), Q(1-rs)]
RS_optim
,
///< alternate optimization of reconstruction error
};
RangeStat
rangestat
;
float
rangestat_arg
;
/// dimension of input vectors
size_t
d
;
/// bytes per vector
size_t
code_size
;
/// trained values (including the range)
std
::
vector
<
float
>
trained
;
ScalarQuantizer
(
size_t
d
,
QuantizerType
qtype
);
ScalarQuantizer
();
void
train
(
size_t
n
,
const
float
*
x
);
/// same as compute_code for several vectors
void
compute_codes
(
const
float
*
x
,
uint8_t
*
codes
,
size_t
n
)
const
;
/// decode a vector from a given code (or n vectors if third argument)
void
decode
(
const
uint8_t
*
code
,
float
*
x
,
size_t
n
)
const
;
};
struct
IndexScalarQuantizer
:
Index
{
/// Used to encode the vectors
ScalarQuantizer
sq
;
/// Codes. Size ntotal * pq.code_size
std
::
vector
<
uint8_t
>
codes
;
size_t
code_size
;
/** Constructor.
*
* @param d dimensionality of the input vectors
* @param M number of subquantizers
* @param nbits number of bit per subvector index
*/
IndexScalarQuantizer
(
int
d
,
ScalarQuantizer
::
QuantizerType
qtype
,
MetricType
metric
=
METRIC_L2
);
IndexScalarQuantizer
();
void
train
(
idx_t
n
,
const
float
*
x
)
override
;
void
add
(
idx_t
n
,
const
float
*
x
)
override
;
void
search
(
idx_t
n
,
const
float
*
x
,
idx_t
k
,
float
*
distances
,
idx_t
*
labels
)
const
override
;
void
reset
()
override
;
void
reconstruct_n
(
idx_t
i0
,
idx_t
ni
,
float
*
recons
)
const
override
;
void
reconstruct
(
idx_t
key
,
float
*
recons
)
const
override
;
};
/** An IVF implementation where the components of the residuals are
* encoded with a scalar uniform quantizer. All distance computations
* are asymmetric, so the encoded vectors are decoded and approximate
* distances are computed.
*/
struct
IndexIVFScalarQuantizer
:
IndexIVF
{
ScalarQuantizer
sq
;
size_t
code_size
;
/// inverted list codes.
std
::
vector
<
std
::
vector
<
uint8_t
>
>
codes
;
IndexIVFScalarQuantizer
(
Index
*
quantizer
,
size_t
d
,
size_t
nlist
,
ScalarQuantizer
::
QuantizerType
qtype
,
MetricType
metric
=
METRIC_L2
);
IndexIVFScalarQuantizer
();
void
train_residual
(
idx_t
n
,
const
float
*
x
)
override
;
void
add_with_ids
(
idx_t
n
,
const
float
*
x
,
const
long
*
xids
)
override
;
void
search
(
idx_t
n
,
const
float
*
x
,
idx_t
k
,
float
*
distances
,
idx_t
*
labels
)
const
override
;
void
merge_from_residuals
(
IndexIVF
&
other
)
override
;
};
}
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment