-
Notifications
You must be signed in to change notification settings - Fork 587
/
Copy pathmain.cpp
194 lines (174 loc) · 7.33 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "inc/Helper/VectorSetReader.h"
#include "inc/Core/VectorIndex.h"
#include "inc/Core/Common.h"
#include "inc/Helper/SimpleIniReader.h"
#include <inc/Core/Common/DistanceUtils.h>
#include "inc/Quantizer/Training.h"
#include <memory>
using namespace SPTAG;
void QuantizeAndSave(std::shared_ptr<SPTAG::Helper::VectorSetReader>& vectorReader, std::shared_ptr<QuantizerOptions>& options, std::shared_ptr<SPTAG::COMMON::IQuantizer>& quantizer)
{
std::shared_ptr<SPTAG::VectorSet> set;
for (int i = 0; (set = vectorReader->GetVectorSet(i, i + options->m_trainingSamples))->Count() > 0; i += options->m_trainingSamples)
{
if (i % (options->m_trainingSamples *10) == 0 || i % options->m_trainingSamples != 0)
{
LOG(Helper::LogLevel::LL_Info, "Saving vector batch starting at %d\n", i);
}
std::shared_ptr<VectorSet> quantized_vectors;
if (options->m_normalized)
{
LOG(Helper::LogLevel::LL_Info, "Normalizing vectors.\n");
set->Normalize(options->m_threadNum);
}
ByteArray PQ_vector_array = ByteArray::Alloc(sizeof(std::uint8_t) * options->m_quantizedDim * set->Count());
quantized_vectors = std::make_shared<BasicVectorSet>(PQ_vector_array, VectorValueType::UInt8, options->m_quantizedDim, set->Count());
#pragma omp parallel for
for (int i = 0; i < set->Count(); i++)
{
quantizer->QuantizeVector(set->GetVector(i), (uint8_t*)quantized_vectors->GetVector(i));
}
ErrorCode code;
if ((code = quantized_vectors->AppendSave(options->m_outputFile)) != ErrorCode::Success)
{
LOG(Helper::LogLevel::LL_Error, "Failed to save quantized vectors, ErrorCode: %s.\n", SPTAG::Helper::Convert::ConvertToString(code).c_str());
exit(1);
}
if (!options->m_outputFullVecFile.empty())
{
if (ErrorCode::Success != set->AppendSave(options->m_outputFullVecFile))
{
LOG(Helper::LogLevel::LL_Error, "Failed to save uncompressed vectors.\n");
exit(1);
}
}
if (!options->m_outputReconstructVecFile.empty())
{
#pragma omp parallel for
for (int i = 0; i < set->Count(); i++)
{
quantizer->ReconstructVector((uint8_t*)quantized_vectors->GetVector(i), set->GetVector(i));
}
if (ErrorCode::Success != set->AppendSave(options->m_outputReconstructVecFile))
{
LOG(Helper::LogLevel::LL_Error, "Failed to save uncompressed vectors.\n");
exit(1);
}
}
}
}
int main(int argc, char* argv[])
{
std::shared_ptr<QuantizerOptions> options = std::make_shared<QuantizerOptions>(10000, true, 0.0f, SPTAG::QuantizerType::None, std::string(), -1, std::string(), std::string());
if (!options->Parse(argc - 1, argv + 1))
{
exit(1);
}
auto vectorReader = Helper::VectorSetReader::CreateInstance(options);
if (ErrorCode::Success != vectorReader->LoadFile(options->m_inputFiles))
{
LOG(Helper::LogLevel::LL_Error, "Failed to read input file.\n");
exit(1);
}
switch (options->m_quantizerType)
{
case QuantizerType::None:
{
std::shared_ptr<SPTAG::VectorSet> set;
for (int i = 0; (set = vectorReader->GetVectorSet(i, i + options->m_trainingSamples))->Count() > 0; i += options-> m_trainingSamples)
{
set->AppendSave(options->m_outputFile);
}
if (!options->m_outputMetadataFile.empty() && !options->m_outputMetadataIndexFile.empty())
{
auto metadataSet = vectorReader->GetMetadataSet();
if (metadataSet)
{
metadataSet->SaveMetadata(options->m_outputMetadataFile, options->m_outputMetadataIndexFile);
}
}
break;
}
case QuantizerType::PQQuantizer:
{
std::shared_ptr<COMMON::IQuantizer> quantizer;
auto fp_load = SPTAG::f_createIO();
if (fp_load == nullptr || !fp_load->Initialize(options->m_outputQuantizerFile.c_str(), std::ios::binary | std::ios::in))
{
auto set = vectorReader->GetVectorSet(0, options->m_trainingSamples);
ByteArray PQ_vector_array = ByteArray::Alloc(sizeof(std::uint8_t) * options->m_quantizedDim * set->Count());
std::shared_ptr<VectorSet> quantized_vectors = std::make_shared<BasicVectorSet>(PQ_vector_array, VectorValueType::UInt8, options->m_quantizedDim, set->Count());
LOG(Helper::LogLevel::LL_Info, "Quantizer Does not exist. Training a new one.\n");
switch (options->m_inputValueType)
{
#define DefineVectorValueType(Name, Type) \
case VectorValueType::Name: \
quantizer.reset(new COMMON::PQQuantizer<Type>(options->m_quantizedDim, 256, (DimensionType)(options->m_dimension/options->m_quantizedDim), false, TrainPQQuantizer<Type>(options, set, quantized_vectors))); \
break;
#include "inc/Core/DefinitionList.h"
#undef DefineVectorValueType
}
auto ptr = SPTAG::f_createIO();
if (ptr != nullptr && ptr->Initialize(options->m_outputQuantizerFile.c_str(), std::ios::binary | std::ios::out))
{
if (ErrorCode::Success != quantizer->SaveQuantizer(ptr))
{
LOG(Helper::LogLevel::LL_Error, "Failed to write quantizer file.\n");
exit(1);
}
}
}
else
{
quantizer = SPTAG::COMMON::IQuantizer::LoadIQuantizer(fp_load);
if (!quantizer)
{
LOG(Helper::LogLevel::LL_Error, "Failed to open existing quantizer file.\n");
exit(1);
}
quantizer->SetEnableADC(false);
}
QuantizeAndSave(vectorReader, options, quantizer);
auto metadataSet = vectorReader->GetMetadataSet();
if (metadataSet)
{
metadataSet->SaveMetadata(options->m_outputMetadataFile, options->m_outputMetadataIndexFile);
}
break;
}
case QuantizerType::OPQQuantizer:
{
std::shared_ptr<COMMON::IQuantizer> quantizer;
auto fp_load = SPTAG::f_createIO();
if (fp_load == nullptr || !fp_load->Initialize(options->m_outputQuantizerFile.c_str(), std::ios::binary | std::ios::in))
{
LOG(Helper::LogLevel::LL_Info, "Quantizer Does not exist. Not supported for OPQ.\n");
exit(1);
}
else
{
quantizer = SPTAG::COMMON::IQuantizer::LoadIQuantizer(fp_load);
if (!quantizer)
{
LOG(Helper::LogLevel::LL_Error, "Failed to open existing quantizer file.\n");
exit(1);
}
quantizer->SetEnableADC(false);
}
QuantizeAndSave(vectorReader, options, quantizer);
auto metadataSet = vectorReader->GetMetadataSet();
if (metadataSet)
{
metadataSet->SaveMetadata(options->m_outputMetadataFile, options->m_outputMetadataIndexFile);
}
break;
}
default:
{
LOG(Helper::LogLevel::LL_Error, "Failed to read quantizer type.\n");
exit(1);
}
}
}