Repository navigation
Expand file tree
/
Copy pathFeaHash.cpp
More file actions
282 lines (224 loc) · 8.39 KB
/
Copy pathFeaHash.cpp
File metadata and controls
282 lines (224 loc) · 8.39 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/// file: core.cpp
#include <memory>
#include <cmath>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <functional>
#include <absl/strings/str_split.h>
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include "feather/BiDict.h"
#include "feather/utils.h"
#include "feather/FeaHash.h"
namespace feather {
FeaHash::FeaHash() {
spdlog::info("Initializing `FeaHash`...");
}
FeaHash::FeaHash(const std::string& conf_path, const bool reversible) {
spdlog::info("Initializing `FeaHash` with config '{0}'...", conf_path);
std::string line;
std::ifstream conf_file(conf_path);
while (std::getline(conf_file, line)) {
std::vector<std::string> conf_line = absl::StrSplit(line, " ");
this->SchemaLineRegister(conf_line);
}
//if (reversible) { this->Hash2IndexDictBuild(conf_path); }
if (reversible) { this->Hash2IndexDictBuild(); }
}
void FeaHash::Hash2IndexDictBuild() {
std::map<int64_t, int32_t> slot_sorter;
for (auto iter = this->name2slot.begin();
iter != this->name2slot.end(); ++iter) {
const FeaSlot* slot = &(iter->second);
int64_t slot_id = slot->GetSlotID();
int32_t bucket_num = slot->GetBucketSize();
slot_sorter[slot_id] = bucket_num;
}
for (auto iter = slot_sorter.begin();
iter != slot_sorter.end(); iter++) {
int64_t slot_id = iter->first;
int32_t bucket_num = iter->second;
for (int32_t i = 0; i < bucket_num; ++i) {
int64_t fea_hash = slot_id * pow(10, this->bucket_code_digits_) + i;
this->hash2index_dict_->Register(
{std::to_string(fea_hash), std::to_string(this->fea_index++)});
}
}
}
void FeaHash::SchemaLineRegister(
const std::vector<std::string>& schema_line) {
std::string fea_name = schema_line[0];
int64_t slot_id = std::stol(schema_line[1]);
int32_t bucket_num = std::stoi(schema_line[2]);
int8_t fea_type = std::stoi(schema_line[3]);
std::string slot_id_str = std::to_string(slot_id);
if (this->slot_digits != -1
&& (slot_id_str.size() != this->slot_digits
|| slot_id_str.size() != schema_line[1].size())
) {
spdlog::error(
"Slot ID illegal, each slot-id should has same digits, \
or its hight digit could not be zero.");
throw "Slot ID illegal.";
}
this->slot2name_dict_->Register({fea_name, slot_id_str});
this->slot_digits = slot_id_str.size();
this->SlotRegister(fea_name, slot_id, bucket_num, fea_type);
}
std::string FeaHash::FeaHash2FeaIndexStr(const int64_t fea_hash) {
return this->hash2index_dict_->Map(
"fea_hash", "fea_index", std::to_string(fea_hash))[0];
}
int32_t FeaHash::FeaHash2FeaIndex(const int64_t fea_hash) {
return std::stoi(this->FeaHash2FeaIndexStr(fea_hash));
}
const FeaSlot* FeaHash::GetSlot(const std::string& fea_name) {
FeaSlot* slot = nullptr;
if (this->name2slot.find(fea_name) != this->name2slot.end()) {
slot = &(this->name2slot[fea_name]);
}
return slot;
}
int32_t FeaHash::SlotRegister(const std::string& fea_name,
const int32_t slot_id, const int32_t bucket_size,
const int16_t slot_type) {
if (this->name2slot.find(fea_name) == this->name2slot.end()) {
this->name2slot.emplace(
fea_name, FeaSlot(fea_name, slot_id, bucket_size, slot_type));
}
this->slot_num += 1;
this->bucket_code_digits_ = std::max(
(int32_t)std::to_string(bucket_size).size(), this->bucket_code_digits_);
spdlog::info(
"Registered type {0} feature '{1}' to slot {2} with {3} as bucket-size.",
slot_type, fea_name, slot_id, bucket_size);
return 0;
}
int16_t FeaHash::FeaValCheck(const std::string& name) {
if (this->name2slot.find(name) == this->name2slot.end()) {
throw "Feature " + name + " should be registered to a slot first";
}
return 0;
}
int16_t FeaHash::FeaValCheck(
const std::string& name, FeaValue* val) {
if (this->FeaValCheck(name) != 0) {
return this->FeaValCheck(name);
} else if (val->GetType() == 2 && val != nullptr) {
if (this->name2slot[name].GetBucketSize()
!= val->GetVecValue()->size()) {
throw "Size or vector feature " + name
+ " should be same with its slot's bucket-size.";
}
}
return 0;
}
std::vector<int64_t> FeaHash::GetFeaHash(
const std::string& fea_name, const std::string& fea_value,
uint16_t hash_type) {
return this->Fea2FeaHash<std::string>(fea_name, fea_value, hash_type);
}
std::vector<int64_t> FeaHash::GetFeaHash(
const std::string& fea_name, const int32_t fea_value,
uint16_t hash_type) {
return this->Fea2FeaHash<int32_t>(fea_name, fea_value, hash_type);
}
std::vector<int64_t> FeaHash::GetFeaHash(
const std::string& fea_name, const float fea_value,
uint16_t hash_type) {
return this->Fea2FeaHash<float>(fea_name, fea_value, hash_type);
}
std::vector<int64_t> FeaHash::GetFeaHash(
const std::string& fea_name, const std::vector<float>& fea_value,
uint16_t hash_type) {
return this->Fea2FeaHash< std::vector<float> >(fea_name, fea_value, hash_type);
}
std::vector<int64_t> FeaHash::GetFeaHash(
const std::string& fea_name, const std::vector<std::string>& fea_value,
uint16_t hash_type) {
return this->Fea2FeaHash< std::vector<std::string> >(fea_name, fea_value, hash_type);
}
std::vector<int64_t> FeaHash::GetFeaHash(
const std::string& fea_name, const std::vector<int32_t>& fea_value,
uint16_t hash_type) {
return this->Fea2FeaHash< std::vector<int32_t> >(fea_name, fea_value, hash_type);
}
template<typename VAL_TYPE>
std::vector<int64_t> FeaHash::Fea2FeaHash(
const std::string& fea_name, const VAL_TYPE& fea_value,
uint16_t hash_type) {
std::vector<int64_t> fea_val_hash;
int8_t fea_type = this->name2slot[fea_name].GetType();
FeaValue fea_value_(fea_value, fea_type);
if (this->FeaValCheck(fea_name, &fea_value_) == 0) {
FeaSlot* fea_slot_ = &(this->name2slot[fea_name]);
fea_val_hash = this->FeaVal2FeaHash(&fea_value_, fea_slot_, hash_type);
}
return fea_val_hash;
}
std::string FeaHash::FeaHash2FeaName(const int64_t fea_hash) {
std::string fea_hash_str = std::to_string(fea_hash);
std::string slot = fea_hash_str.substr(
0, fea_hash_str.size() - this->bucket_code_digits_);
return this->slot2name_dict_->Map("slot", "fea_name", slot)[0];
}
const std::unordered_map<std::string, FeaSlot>* FeaHash::GetSlots() {
return &(this->name2slot);
}
/*
const nlohmann::json& FeaHash::GetMeta() {
return this->fea_hash;
}
*/
int32_t FeaHash::GetFeaBucketCodeLength() {
return this->bucket_code_digits_;
}
std::vector<int32_t> FeaHash::FeaVal2FeaHashBucket(
FeaValue* fea_val, FeaSlot* fea_slot, uint16_t hash_type) {
std::vector<int32_t> hash_bucket_id;
std::vector<int64_t> fea_hash = fea_val->GetHash(hash_type);
hash_bucket_id.resize(fea_hash.size());
if (fea_val->GetType() == 1 && (
fea_hash.size() != 1 || fea_slot->GetBucketSize() != 1)) {
throw "Continuous feature's hash-bucket size must be 1";
}
if (fea_val->GetType() == 2
&& fea_hash.size() != fea_slot->GetBucketSize()) {
throw "Vector feature's hash-bucket size must be same to its dim";
}
/// TODO@202108271359: Using SSE or AVX
for (int32_t i=0; i < fea_hash.size(); ++i) {
/// TODO@202108271400: Too much memory for id style feature.
// hash_bucket_id[i] = fea_slot->ValRegister(
// fea_val->GetDescreteValue(), fea_hash[i]);
hash_bucket_id[i] = llabs(fea_hash[i]) % fea_slot->GetBucketSize();
}
return hash_bucket_id;
}
std::vector<std::string> FeaHash::FeaVal2FeaHashBucketCode(
FeaValue* fea_val, FeaSlot* fea_slot, uint16_t hash_type) {
std::vector<std::string> fea_bucket_code;
std::vector<int32_t> hash_bucket_id = this->FeaVal2FeaHashBucket(
fea_val, fea_slot, hash_type);
fea_bucket_code.resize(hash_bucket_id.size());
for (int32_t i = 0; i < hash_bucket_id.size(); ++i) {
fea_bucket_code[i] = num2str_code<int32_t>(
hash_bucket_id[i], this->bucket_code_digits_);
}
return fea_bucket_code;
}
std::vector<int64_t> FeaHash::FeaVal2FeaHash(
FeaValue* fea_val, FeaSlot* fea_slot, uint16_t hash_type) {
std::vector<int64_t> fea_val_hash;
int32_t slot_id = fea_slot->GetSlotID();
std::vector<std::string> fea_bucket_code = this->FeaVal2FeaHashBucketCode(
fea_val, fea_slot, hash_type);
fea_val_hash.resize(fea_bucket_code.size());
for (int32_t i = 0; i < fea_bucket_code.size(); ++i) {
fea_val_hash[i] = std::stol(
std::to_string(slot_id) + fea_bucket_code[i]);
}
return fea_val_hash;
}
} // namespace feather