-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.cpp
More file actions
351 lines (286 loc) · 10.5 KB
/
base.cpp
File metadata and controls
351 lines (286 loc) · 10.5 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <cstdint>
#include <omp.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include "helpers.cpp"
namespace py = pybind11;
typedef bool bool_t;
typedef float float32_t;
typedef double float64_t;
#define C_CONTIGUOUS py::array::c_style
#define F_CONTIGUOUS py::array::f_style
#define PY_GIL_RELEASE py::gil_scoped_release release;
#define PY_GIL_ACQUIRE py::gil_scoped_acquire acquire;
class PolynomialFeatures
{
public:
size_t degree;
size_t n_jobs;
bool_t interaction_only;
bool_t include_bias;
py::array_t<size_t> plan;
PolynomialFeatures(size_t degree,
bool_t interaction_only,
bool_t include_bias,
size_t n_jobs);
void init_plan(size_t n_features);
template <class T>
py::array_t<T, C_CONTIGUOUS> C2C(const py::array_t<T, C_CONTIGUOUS> &X) const;
template <class T>
py::array_t<T, F_CONTIGUOUS> C2F(const py::array_t<T, C_CONTIGUOUS> &X) const;
template <class T>
py::array_t<T, F_CONTIGUOUS> F2F(const py::array_t<T, F_CONTIGUOUS> &X) const;
template <class T>
py::array_t<T, C_CONTIGUOUS> F2C(const py::array_t<T, F_CONTIGUOUS> &X) const;
};
PolynomialFeatures::PolynomialFeatures(size_t degree,
bool_t interaction_only,
bool_t include_bias,
size_t n_jobs)
: degree(degree),
interaction_only(interaction_only),
include_bias(include_bias),
n_jobs(n_jobs) {}
void PolynomialFeatures::init_plan(size_t n_features)
{
plan = create_plan(
polynomial_basis<int64_t>(n_features,
degree,
interaction_only,
include_bias));
}
template <class T>
py::array_t<T, C_CONTIGUOUS> PolynomialFeatures::C2C(const py::array_t<T, C_CONTIGUOUS> &X) const
{
size_t n_samples = X.shape(0);
size_t n_features_in = X.shape(1);
size_t n_features_out = plan.shape(0) + n_features_in + include_bias;
auto XT = py::array_t<T, C_CONTIGUOUS>({n_samples, n_features_out});
PY_GIL_RELEASE
auto X_stride = X.strides(0) / sizeof(T);
auto XT_stride = XT.strides(0) / sizeof(T);
auto X_data = X.data();
auto XT_data = XT.mutable_data();
auto XT_data_b = XT_data + include_bias;
auto plan_data = plan.data();
#pragma omp parallel num_threads(n_jobs)
{
#pragma omp for
for (auto i = 0; i < n_samples; ++i)
{
if (include_bias)
{
XT_data[i * XT_stride] = 1;
}
auto XT_data_i = XT_data_b + i * XT_stride;
auto X_data_i = X_data + i * X_stride;
for (auto j = 0; j < n_features_in; ++j)
{
XT_data_i[j] = X_data_i[j];
}
XT_data_i = XT_data + i * XT_stride;
auto plan_data_i = plan_data;
for (auto j = 0; j < plan.shape(0); ++j)
{
auto col1 = *plan_data_i++;
auto col2 = *plan_data_i++;
auto col3 = *plan_data_i++;
XT_data_i[col1] = XT_data_i[col2] * XT_data_i[col3];
}
}
}
PY_GIL_ACQUIRE
return XT;
}
template <class T>
py::array_t<T, C_CONTIGUOUS> PolynomialFeatures::F2C(const py::array_t<T, F_CONTIGUOUS> &X) const
{
size_t n_samples = X.shape(0);
size_t n_features_in = X.shape(1);
size_t n_features_out = plan.shape(0) + n_features_in + include_bias;
auto XT = py::array_t<T, C_CONTIGUOUS>({n_samples, n_features_out});
PY_GIL_RELEASE
auto X_stride = X.strides(1) / sizeof(T);
auto XT_stride = XT.strides(0) / sizeof(T);
auto X_data = X.data();
auto XT_data = XT.mutable_data();
auto XT_data_b = XT_data + include_bias;
auto plan_data = plan.data();
#pragma omp parallel num_threads(n_jobs)
{
#pragma omp for
for (auto i = 0; i < n_samples; ++i)
{
if (include_bias)
{
XT_data[i * XT_stride] = 1;
}
auto XT_data_i = XT_data_b + i * XT_stride;
auto X_data_i = X_data + i;
for (auto j = 0; j < n_features_in; ++j)
{
XT_data_i[j] = X_data_i[j * X_stride];
}
XT_data_i = XT_data + i * XT_stride;
auto plan_data_i = plan_data;
for (auto j = 0; j < plan.shape(0); ++j)
{
auto col1 = *plan_data_i++;
auto col2 = *plan_data_i++;
auto col3 = *plan_data_i++;
XT_data_i[col1] = XT_data_i[col2] * XT_data_i[col3];
}
}
}
PY_GIL_ACQUIRE
return XT;
}
template <class T>
py::array_t<T, F_CONTIGUOUS> PolynomialFeatures::F2F(const py::array_t<T, F_CONTIGUOUS> &X) const
{
size_t n_samples = X.shape(0);
size_t n_features_in = X.shape(1);
size_t n_features_out = plan.shape(0) + n_features_in + include_bias;
auto XT = py::array_t<T, F_CONTIGUOUS>({n_samples, n_features_out});
PY_GIL_RELEASE
auto X_stride = X.strides(1) / sizeof(T);
auto XT_stride = XT.strides(1) / sizeof(T);
auto plan_stride = plan.strides(0) / sizeof(int64_t);
auto X_data = X.data();
auto XT_data = XT.mutable_data();
auto XT_data_b = XT_data + include_bias * XT_stride;
auto plan_data = plan.data();
#pragma omp parallel num_threads(n_jobs)
{
if (include_bias)
{
#pragma omp for nowait
for (auto i = 0; i < n_samples; ++i)
{
XT_data[i] = 1;
}
}
for (auto j = 0; j < n_features_in; ++j)
{
auto X_data_j = X_data + j * X_stride;
auto XT_data_j = XT_data_b + j * XT_stride;
#pragma omp for
for (auto i = 0; i < n_samples; ++i)
{
XT_data_j[i] = X_data_j[i];
}
}
for (auto j = 0; j < plan.shape(0); ++j)
{
auto col1 = plan_data[j * plan_stride + 0];
auto col2 = plan_data[j * plan_stride + 1];
auto col3 = plan_data[j * plan_stride + 2];
auto XT_data_j1 = XT_data + col1 * XT_stride;
auto XT_data_j2 = XT_data + col2 * XT_stride;
auto XT_data_j3 = XT_data + col3 * XT_stride;
#pragma omp for
for (auto i = 0; i < n_samples; ++i)
{
XT_data_j1[i] = XT_data_j2[i] * XT_data_j3[i];
}
}
}
PY_GIL_ACQUIRE
return XT;
}
template <class T>
py::array_t<T, F_CONTIGUOUS> PolynomialFeatures::C2F(const py::array_t<T, C_CONTIGUOUS> &X) const
{
size_t n_samples = X.shape(0);
size_t n_features_in = X.shape(1);
size_t n_features_out = plan.shape(0) + n_features_in + include_bias;
auto XT = py::array_t<T, F_CONTIGUOUS>({n_samples, n_features_out});
PY_GIL_RELEASE
auto X_stride = X.strides(0) / sizeof(T);
auto XT_stride = XT.strides(1) / sizeof(T);
auto plan_stride = plan.strides(0) / sizeof(int64_t);
auto X_data = X.data();
auto XT_data = XT.mutable_data();
auto XT_data_b = XT_data + include_bias * XT_stride;
auto plan_data = plan.data();
#pragma omp parallel num_threads(n_jobs)
{
if (include_bias)
{
#pragma omp for nowait
for (auto i = 0; i < n_samples; ++i)
{
XT_data[i] = 1;
}
}
#pragma omp for
for (auto i = 0; i < n_samples; ++i)
{
auto X_data_i = X_data + i * X_stride;
auto XT_data_i = XT_data_b + i;
for (auto j = 0; j < n_features_in; ++j)
{
XT_data_i[j * XT_stride] = X_data_i[j];
}
}
for (auto j = 0; j < plan.shape(0); ++j)
{
auto col1 = plan_data[j * plan_stride + 0];
auto col2 = plan_data[j * plan_stride + 1];
auto col3 = plan_data[j * plan_stride + 2];
auto XT_data_j1 = XT_data + col1 * XT_stride;
auto XT_data_j2 = XT_data + col2 * XT_stride;
auto XT_data_j3 = XT_data + col3 * XT_stride;
#pragma omp for
for (auto i = 0; i < n_samples; ++i)
{
XT_data_j1[i] = XT_data_j2[i] * XT_data_j3[i];
}
}
}
PY_GIL_ACQUIRE
return XT;
}
PYBIND11_MODULE(base, handle)
{
py::class_<PolynomialFeatures>(handle, "PolynomialFeatures")
.def(py::init<size_t, bool_t, bool_t, size_t>(),
py::arg("degree"),
py::arg("interaction_only"),
py::arg("include_bias"),
py::arg("n_jobs"))
.def("init_plan", &PolynomialFeatures::init_plan,
py::arg("n_features"))
.def("C2C32", &PolynomialFeatures::C2C<float32_t>,
py::arg("X"))
.def("C2C64", &PolynomialFeatures::C2C<float64_t>,
py::arg("X"))
.def("F2C32", &PolynomialFeatures::F2C<float32_t>,
py::arg("X"))
.def("F2C64", &PolynomialFeatures::F2C<float64_t>,
py::arg("X"))
.def("F2F32", &PolynomialFeatures::F2F<float32_t>,
py::arg("X"))
.def("F2F64", &PolynomialFeatures::F2F<float64_t>,
py::arg("X"))
.def("C2F32", &PolynomialFeatures::C2F<float32_t>,
py::arg("X"))
.def("C2F64", &PolynomialFeatures::C2F<float64_t>,
py::arg("X"))
.def_readwrite("interaction_only", &PolynomialFeatures::interaction_only)
.def_readwrite("include_bias", &PolynomialFeatures::include_bias)
.def_readwrite("n_jobs", &PolynomialFeatures::n_jobs)
.def_readwrite("plan", &PolynomialFeatures::plan);
handle.def("exponents_matrix", &exponents_matrix<int64_t>,
py::arg("n"),
py::arg("k"),
py::arg("include_zeros"));
handle.def("polynomial_basis", &polynomial_basis<int64_t>,
py::arg("n_features"),
py::arg("degree"),
py::arg("interaction_only"),
py::arg("include_bias"));
handle.def("create_plan", &create_plan<int64_t>,
py::arg("basis"));
}