forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneHot.h
More file actions
234 lines (198 loc) · 9.86 KB
/
Copy pathOneHot.h
File metadata and controls
234 lines (198 loc) · 9.86 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
/*
* Copyright 2021 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SRC_RUNTIME_LOCAL_KERNELS_ONEHOT_H
#define SRC_RUNTIME_LOCAL_KERNELS_ONEHOT_H
#include <runtime/local/context/DaphneContext.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Matrix.h>
#include <runtime/local/datastructures/ValueTypeUtils.h>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <cstddef>
#include <cstdint>
#include <cstring>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTRes, class DTArg> struct OneHot {
static void apply(DTRes *&res, const DTArg *arg, const DenseMatrix<int64_t> *info, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTRes, class DTArg>
void oneHot(DTRes *&res, const DTArg *arg, const DenseMatrix<int64_t> *info, DCTX(ctx)) {
OneHot<DTRes, DTArg>::apply(res, arg, info, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VT> struct OneHot<DenseMatrix<VT>, DenseMatrix<VT>> {
static void apply(DenseMatrix<VT> *&res, const DenseMatrix<VT> *arg, const DenseMatrix<int64_t> *info, DCTX(ctx)) {
if (info->getNumRows() != 1) {
throw std::runtime_error("OneHot - parameter 'info' must be a row matrix");
}
const size_t numColsArg = arg->getNumCols();
if (numColsArg != info->getNumCols()) {
throw std::runtime_error("OneHot - parameter 'info' must provide information for each "
"column of parameter arg");
}
size_t numColsRes = 0;
const int64_t *valuesInfo = info->getValues();
for (size_t c = 0; c < numColsArg; c++) {
const int64_t numDistinct = valuesInfo[c];
if (numDistinct == -1)
numColsRes++;
else if (numDistinct > 0)
numColsRes += numDistinct;
else if (numDistinct != 0)
throw std::runtime_error("OneHot: parameter 'info' must be an "
"integer greater or equal than -1");
}
if (numColsRes == 0)
throw std::runtime_error("OneHot: parameter 'info' must contain at "
"least one non-zero entry");
const size_t numRows = arg->getNumRows();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRows, numColsRes, false);
const VT *valuesArg = arg->getValues();
VT *valuesRes = res->getValues();
const size_t rowSkipArg = arg->getRowSkip();
const size_t rowSkipRes = res->getRowSkip();
for (size_t r = 0; r < numRows; r++) {
size_t cRes = 0;
for (size_t cArg = 0; cArg < numColsArg; cArg++) {
const int64_t numDistinct = valuesInfo[cArg];
if (numDistinct == -1)
// retain value from argument matrix
valuesRes[cRes++] = valuesArg[cArg];
else if (numDistinct != 0) {
// one-hot encode value from argument matrix
memset(valuesRes + cRes, VT(0), numDistinct * sizeof(VT));
const size_t argVal = static_cast<const size_t>(valuesArg[cArg]);
if (argVal >= 0 && argVal < static_cast<size_t>(numDistinct))
valuesRes[cRes + argVal] = 1;
else
throw std::out_of_range("OneHot: arg values that are encoded (info value "
"!= -1) must be positive and smaller than the "
"corresponding info value");
cRes += numDistinct;
}
}
valuesArg += rowSkipArg;
valuesRes += rowSkipRes;
}
}
};
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix<string>
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg>
void oneHotString(DenseMatrix<VTRes> *&res, const DenseMatrix<VTArg> *arg, const DenseMatrix<int64_t> *info,
DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numColsArg = arg->getNumCols();
const size_t rowSkipArg = arg->getRowSkip();
auto recode_result = DataObjectFactory::create<DenseMatrix<VTRes>>(numRows, numColsArg, false);
VTRes *valuesRes = recode_result->getValues();
const VTArg *valuesArg = arg->getValues();
// Recode arg with string elements to a dense matrix based on indices without ordering
for (size_t cArg = 0; cArg < numColsArg; cArg++) {
std::unordered_map<VTArg, size_t> firstIndexMap;
for (size_t r = 0; r < numRows; r++) {
size_t value_index = (rowSkipArg * r) + cArg;
if (firstIndexMap.find(valuesArg[value_index]) == firstIndexMap.end()) {
firstIndexMap[valuesArg[value_index]] = firstIndexMap.size();
}
valuesRes[value_index] = VTRes(firstIndexMap[valuesArg[value_index]]);
}
}
// call oneHot with recoded matrix as arg
oneHot(res, recode_result, info, ctx);
DataObjectFactory::destroy(recode_result);
}
template <typename VT> struct OneHot<DenseMatrix<VT>, DenseMatrix<std::string>> {
static void apply(DenseMatrix<VT> *&res, const DenseMatrix<std::string> *arg, const DenseMatrix<int64_t> *info,
DCTX(ctx)) {
oneHotString<VT, std::string>(res, arg, info, ctx);
}
};
template <typename VT> struct OneHot<DenseMatrix<VT>, DenseMatrix<FixedStr16>> {
static void apply(DenseMatrix<VT> *&res, const DenseMatrix<FixedStr16> *arg, const DenseMatrix<int64_t> *info,
DCTX(ctx)) {
oneHotString<VT, FixedStr16>(res, arg, info, ctx);
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix
// ----------------------------------------------------------------------------
template <typename VT> struct OneHot<Matrix<VT>, Matrix<VT>> {
static void apply(Matrix<VT> *&res, const Matrix<VT> *arg, const Matrix<int64_t> *info, DCTX(ctx)) {
const size_t numColsArg = arg->getNumCols();
const size_t numRows = arg->getNumRows();
if (info->getNumRows() != 1)
throw std::runtime_error("OneHot: parameter 'info' must be a row matrix");
if (numColsArg != info->getNumCols())
throw std::runtime_error("OneHot: parameter 'info' must provide information for each "
"column of parameter arg");
size_t numColsRes = 0;
for (size_t c = 0; c < numColsArg; c++) {
const int64_t numDistinct = info->get(0, c);
if (numDistinct == -1)
++numColsRes;
else if (numDistinct > 0)
numColsRes += numDistinct;
else if (numDistinct != 0)
throw std::runtime_error("OneHot: parameter 'info' must be an "
"integer greater or equal than -1");
}
if (numColsRes == 0)
throw std::runtime_error("OneHot: parameter 'info' must contain at "
"least one non-zero entry");
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRows, numColsRes, false);
res->prepareAppend();
for (size_t r = 0; r < numRows; ++r) {
size_t cRes = 0;
for (size_t cArg = 0; cArg < numColsArg; ++cArg) {
const int64_t numDistinct = info->get(0, cArg);
if (numDistinct == -1)
// retain value from argument matrix
res->append(r, cRes++, arg->get(r, cArg));
else if (numDistinct != 0) {
// one-hot encode value from argument matrix
// skipped values are set to 0
const size_t argVal = static_cast<const size_t>(arg->get(r, cArg));
if (argVal >= 0 && argVal < static_cast<size_t>(numDistinct))
res->append(r, cRes + argVal, 1);
else
throw std::out_of_range("OneHot: arg values that are encoded (info value "
"!= -1) "
"must be positive and smaller than the "
"corresponding info value");
cRes += numDistinct;
}
}
}
res->finishAppend();
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_ONEHOT_H