-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMedianInit.h
260 lines (199 loc) · 9.65 KB
/
MedianInit.h
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
/*-
* Nathan Lay
* AI Resource at National Cancer Institute
* National Institutes of Health
* April 2021
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#ifndef MEDIANINIT_H
#define MEDIANINIT_H
#include <iostream>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <type_traits>
#include "torch/extension.h"
#include "HingeTreeCommon.h"
#include "HingeTrieCommon.h"
template<typename RealType>
struct Vertex {
int64_t i64Ordinal = -1;
RealType threshold = RealType();
// Partition
const RealType **p_begin = nullptr;
const RealType **p_end = nullptr;
};
// Weights only used to deduce properties of trees
template<typename RealType, typename TreeTraitsType>
std::vector<std::vector<Vertex<RealType>>> FromPyTorch(torch::Tensor inThresholds, torch::Tensor inOrdinals, torch::Tensor inWeights);
template<typename RealType, typename TreeTraitsType>
bool ToPyTorch(const std::vector<std::vector<Vertex<RealType>>> &vTrees, torch::Tensor inThresholds, torch::Tensor inOrdinals, torch::Tensor inWeights);
template<typename RealType, typename TreeTraitsType>
bool InitMedianSplits(std::vector<std::vector<Vertex<RealType>>> &vTrees, torch::Tensor inData, torch::Tensor inWeights);
template<typename RealType, typename TreeTraitsType>
std::vector<std::vector<Vertex<RealType>>> FromPyTorch(torch::Tensor inThresholds, torch::Tensor inOrdinals, torch::Tensor inWeights) {
typedef Vertex<RealType> VertexType;
if (inThresholds.dim() != 2 || inOrdinals.dim() != 2 || inWeights.dim() < 2)
return std::vector<std::vector<VertexType>>();
if (inThresholds.sizes() != inOrdinals.sizes() || inWeights.sizes()[0] != inThresholds.sizes()[0])
return std::vector<std::vector<VertexType>>();
const int64_t i64NumTrees = inWeights.sizes()[0];
const int64_t i64NumLeavesPerTree = inWeights.sizes()[1];
const int64_t i64TreeDepth = TreeTraitsType::ComputeDepth(i64NumLeavesPerTree);
if (i64TreeDepth > TreeTraitsType::GetMaxDepth() || inThresholds.sizes()[1] != TreeTraitsType::GetThresholdCount(i64TreeDepth))
return std::vector<std::vector<VertexType>>();
const int64_t i64NumDecisionsPerTree = inThresholds.sizes()[1];
const RealType * const p_inThresholds = inThresholds.data_ptr<RealType>();
const int64_t * const p_inOrdinals = inOrdinals.data_ptr<int64_t>();
std::vector<std::vector<VertexType>> vTrees;
vTrees.reserve(i64NumTrees);
std::vector<VertexType> vVertices;
for (int64_t i = 0; i < i64NumTrees; ++i) {
vVertices.clear();
vVertices.reserve(i64NumDecisionsPerTree);
for (int64_t j = 0; j < i64NumDecisionsPerTree; ++j) {
VertexType stVertex;
stVertex.i64Ordinal = p_inOrdinals[i*i64NumDecisionsPerTree + j];
stVertex.threshold = p_inThresholds[i*i64NumDecisionsPerTree + j];
vVertices.push_back(stVertex);
}
vTrees.emplace_back(std::move(vVertices));
}
return vTrees;
}
template<typename RealType, typename TreeTraitsType>
bool ToPyTorch(const std::vector<std::vector<Vertex<RealType>>> &vTrees, torch::Tensor inThresholds, torch::Tensor inOrdinals, torch::Tensor inWeights) {
//typedef Vertex<RealType> VertexType;
if (inThresholds.dim() != 2 || inOrdinals.dim() != 2 || inWeights.dim() < 2)
return false;
if (inThresholds.sizes() != inOrdinals.sizes() || inWeights.sizes()[0] != inThresholds.sizes()[0])
return false;
const int64_t i64NumTrees = inWeights.sizes()[0];
const int64_t i64NumLeavesPerTree = inWeights.sizes()[1];
const int64_t i64TreeDepth = TreeTraitsType::ComputeDepth(i64NumLeavesPerTree);
if (i64TreeDepth > TreeTraitsType::GetMaxDepth() || inThresholds.sizes()[1] != TreeTraitsType::GetThresholdCount(i64TreeDepth))
return false;
const int64_t i64NumDecisionsPerTree = inThresholds.sizes()[1];
if (i64NumTrees != (int64_t)vTrees.size() || (int64_t)vTrees[0].size() != i64NumDecisionsPerTree)
return false;
RealType * const p_inThresholds = inThresholds.data_ptr<RealType>();
int64_t * const p_inOrdinals = inOrdinals.data_ptr<int64_t>();
for (int64_t i = 0; i < i64NumTrees; ++i) {
for (int64_t j = 0; j < i64NumDecisionsPerTree; ++j) {
p_inThresholds[i*i64NumDecisionsPerTree + j] = vTrees[i][j].threshold;
p_inOrdinals[i*i64NumDecisionsPerTree + j] = vTrees[i][j].i64Ordinal; // Shouldn't need to assign this, but it won't hurt
}
}
return true;
}
template<typename RealType, typename TreeTraitsType>
bool InitMedianSplits(std::vector<std::vector<Vertex<RealType>>> &vTrees, torch::Tensor inData, torch::Tensor inWeights) {
typedef c10::IntArrayRef IntArrayRef;
typedef Vertex<RealType> VertexType;
if (inData.dim() < 2 || inWeights.dim() < 2)
return false;
if (inWeights.sizes()[0] != (int)vTrees.size())
return false;
const int64_t i64NumTrees = inWeights.sizes()[0];
const int64_t i64NumLeavesPerTree = inWeights.sizes()[1];
const int64_t i64TreeDepth = TreeTraitsType::ComputeDepth(i64NumLeavesPerTree);
const int64_t i64BatchSize = inData.sizes()[0];
const int64_t i64NumChannels = inData.sizes()[1];
const int64_t i64NumDecisionsPerTree = TreeTraitsType::GetThresholdCount(i64TreeDepth);
if (i64TreeDepth > TreeTraitsType::GetMaxDepth() || (int64_t)vTrees[0].size() != i64NumDecisionsPerTree)
return false;
const RealType * const p_inData = inData.data_ptr<RealType>();
int64_t i64InnerDataNum = 1;
{
auto inDataSlice = inData.sizes().slice(2);
i64InnerDataNum = std::accumulate(inDataSlice.begin(), inDataSlice.end(), (int64_t)1, std::multiplies<IntArrayRef::value_type>());
}
// Sanity check ordinals
for (int64_t i = 0; i < i64NumTrees; ++i) {
const std::vector<VertexType> &vVertices = vTrees[i];
for (int64_t j = 0; j < i64NumDecisionsPerTree; ++j) {
const VertexType &stVertex = vVertices[j];
if (stVertex.i64Ordinal < 0 || stVertex.i64Ordinal >= i64NumChannels)
return false;
}
}
// Make row pointers
std::vector<const RealType *> vRows;
vRows.reserve(i64BatchSize*i64InnerDataNum);
for (int64_t b = 0; b < i64BatchSize; ++b) {
for (int64_t k = 0; k < i64InnerDataNum; ++k) {
vRows.push_back(p_inData + ((b*i64NumChannels + 0)*i64InnerDataNum + k));
}
}
// Setup root verices
for (int64_t i = 0; i < i64NumTrees; ++i) {
std::vector<VertexType> &vVertices = vTrees[i];
vVertices[0].p_begin = vRows.data();
vVertices[0].p_end = vVertices[0].p_begin + vRows.size();
}
if (std::is_same<TreeTraitsType, bleak::HingeFernCommon<RealType>>::value) {
for (int64_t i = 0; i < i64NumTrees; ++i) {
std::vector<VertexType> &vVertices = vTrees[i];
for (int64_t j = 0; j < i64TreeDepth; ++j) {
VertexType &stVertex = vVertices[j];
stVertex.p_begin = vVertices[0].p_begin;
stVertex.p_end = vVertices[0].p_end;
const RealType **p_split = stVertex.p_begin + (stVertex.p_end - stVertex.p_begin)/2;
std::nth_element(stVertex.p_begin, p_split, stVertex.p_end,
[&stVertex, i64InnerDataNum](const RealType *a, const RealType *b) -> bool {
return a[stVertex.i64Ordinal * i64InnerDataNum] > b[stVertex.i64Ordinal * i64InnerDataNum];
});
stVertex.threshold = (*p_split)[stVertex.i64Ordinal * i64InnerDataNum];
}
}
}
else if (std::is_same<TreeTraitsType, bleak::HingeTreeCommon<RealType>>::value || std::is_same<TreeTraitsType, bleak::HingeTrieCommon<RealType>>::value) {
for (int64_t i = 0; i < i64NumTrees; ++i) {
std::vector<VertexType> &vVertices = vTrees[i];
for (int64_t j = 0; j < i64NumDecisionsPerTree; ++j) {
VertexType &stVertex = vVertices[j];
if (stVertex.p_begin == stVertex.p_end) {
std::cerr << "Warning: Empty sample in vertex? Skipping..." << std::endl;
continue; // Should not happen?
}
const RealType **p_split = stVertex.p_begin + (stVertex.p_end - stVertex.p_begin)/2;
std::nth_element(stVertex.p_begin, p_split, stVertex.p_end,
[&stVertex, i64InnerDataNum](const RealType *a, const RealType *b) -> bool {
return a[stVertex.i64Ordinal * i64InnerDataNum] > b[stVertex.i64Ordinal * i64InnerDataNum];
});
stVertex.threshold = (*p_split)[stVertex.i64Ordinal * i64InnerDataNum];
const int64_t i64LeftChild = 2*j + 1;
if (i64LeftChild+1 < (int)vVertices.size()) {
// Partition so that larger comes before smaller
p_split = std::partition(stVertex.p_begin, stVertex.p_end,
[&stVertex, i64InnerDataNum](const RealType *row) -> bool {
return row[stVertex.i64Ordinal * i64InnerDataNum] > stVertex.threshold;
});
// Smaller partition to left child
vVertices[i64LeftChild].p_begin = p_split;
vVertices[i64LeftChild].p_end = stVertex.p_end;
// Larger partition to right child
vVertices[i64LeftChild+1].p_begin = stVertex.p_begin;
vVertices[i64LeftChild+1].p_end = p_split;
}
}
}
}
else { // Not supported?
return false;
}
return true;
}
#endif // !MEDIANINIT_H