-
Notifications
You must be signed in to change notification settings - Fork 4
/
permutohedral-inl.h
211 lines (179 loc) · 6.29 KB
/
permutohedral-inl.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
/*!
* Copyright (c) 2016 by Contributors
* \file permutohedral-inl.h
* \brief
* \author Junyuan Xie
*/
#ifndef MXNET_OPERATOR_PERMUTOHEDRAL_INL_H_
#define MXNET_OPERATOR_PERMUTOHEDRAL_INL_H_
#include <dmlc/logging.h>
#include <mxnet/operator.h>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <utility>
#include "../../src/operator/mshadow_op.h"
#include "../../src/operator/operator_common.h"
#include "modified_permutohedral.h"
#include "cu_hash_table.h"
namespace mxnet {
namespace op {
namespace permutohedral {
enum PermutohedralOpInputs {kData, kPos};
enum PermutohedralOpOutputs {kOut, kNorm};
enum PermutohedralOpTemps {kTemp};
enum PermutohedralOpAuxs {kScale};
struct Pair{
int32_t index;
float weight;
};
} // namespace blockgrad
struct PermutohedralParam : public dmlc::Parameter<PermutohedralParam> {
bool normalize;
DMLC_DECLARE_PARAMETER(PermutohedralParam) {
DMLC_DECLARE_FIELD(normalize).set_default(false)
.describe("normalize output");
}
};
class PermutohedralOp : public Operator {
public:
explicit PermutohedralOp(PermutohedralParam param) {
this->param_ = param;
}
virtual void Forward(const OpContext &ctx,
const std::vector<TBlob> &in_data,
const std::vector<OpReqType> &req,
const std::vector<TBlob> &out_data,
const std::vector<TBlob> &aux_args);
virtual void Backward(const OpContext &ctx,
const std::vector<TBlob> &out_grad,
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_data,
const std::vector<OpReqType> &req,
const std::vector<TBlob> &in_grad,
const std::vector<TBlob> &aux_args);
private:
PermutohedralParam param_;
permutohedral::ModifiedPermutohedral lattice_;
}; // class PermutohedralOp
#if defined(__CUDACC__)
template<int key_size>
class CuPermutohedralOp : public Operator {
public:
explicit CuPermutohedralOp(PermutohedralParam param) {
this->param_ = param;
init_ = false;
}
virtual void Forward(const OpContext &ctx,
const std::vector<TBlob> &in_data,
const std::vector<OpReqType> &req,
const std::vector<TBlob> &out_data,
const std::vector<TBlob> &aux_args);
virtual void Backward(const OpContext &ctx,
const std::vector<TBlob> &out_grad,
const std::vector<TBlob> &in_data,
const std::vector<TBlob> &out_data,
const std::vector<OpReqType> &req,
const std::vector<TBlob> &in_grad,
const std::vector<TBlob> &aux_args);
private:
bool init_;
PermutohedralParam param_;
permutohedral::ModifiedPermutohedral lattice_;
int batch_size_, data_size_, val_size_, n_elements_, n_keys_, lblock_, nblock_;
mshadow::Tensor<gpu, 1, int32_t> entries_;
mshadow::Tensor<gpu, 2, int16_t> keys_;
mshadow::Tensor<gpu, 2, float> vals_, new_vals_;
mshadow::Tensor<gpu, 1, permutohedral::Pair> matrix_;
void GetTempSpace(const OpContext &ctx, int val_size);
void Filter(cudaStream_t stream, permutohedral::CuHashTable<key_size> table, bool normalize, int val_size,
float *scale, float *data, float *pos, float *out, float *norm);
}; // class PermutohedralOp
#endif // __CUDACC__
template<typename xpu>
Operator *CreateOp(PermutohedralParam param, int key_size);
#if DMLC_USE_CXX11
class PermutohedralProp : public OperatorProperty {
public:
void Init(const std::vector<std::pair<std::string, std::string> >& kwargs) override {
param_.Init(kwargs);
}
std::map<std::string, std::string> GetParams() const override {
return param_.__DICT__();
}
std::vector<std::string> ListArguments() const override {
return {"data", "pos"};
}
std::vector<std::string> ListAuxiliaryStates() const override {
return {"bias"};
}
std::vector<std::string> ListOutputs() const override {
return {"output", "norm"};
}
int NumVisibleOutputs() const override {
return 1;
}
int NumOutputs() const override {
return 2;
}
bool InferShape(std::vector<TShape> *in_shape,
std::vector<TShape> *out_shape,
std::vector<TShape> *aux_shape) const override {
using namespace mshadow;
CHECK_EQ(in_shape->size(), 2);
TShape val_shape = in_shape->at(permutohedral::kData);
TShape pos_shape = in_shape->at(permutohedral::kPos);
CHECK_EQ(val_shape.ndim(), 4);
CHECK_EQ(pos_shape.ndim(), 4);
CHECK_EQ(val_shape[0], pos_shape[0]);
CHECK_EQ(val_shape[2], pos_shape[2]);
CHECK_EQ(val_shape[3], pos_shape[3]);
out_shape->clear();
out_shape->push_back(val_shape);
val_shape[1] = 1;
out_shape->push_back(val_shape);
aux_shape->clear();
aux_shape->push_back(Shape1(pos_shape[1]));
return true;
}
OperatorProperty* Copy() const override {
auto ptr = new PermutohedralProp();
ptr->param_ = param_;
return ptr;
}
std::string TypeString() const override {
return "Permutohedral";
}
std::vector<ResourceRequest> ForwardResource(
const std::vector<TShape> &in_shape) const override {
return {ResourceRequest::kTempSpace};
}
std::vector<ResourceRequest> BackwardResource(
const std::vector<TShape> &in_shape) const override {
return {ResourceRequest::kTempSpace};
}
std::vector<int> DeclareBackwardDependency(
const std::vector<int> &out_grad,
const std::vector<int> &in_data,
const std::vector<int> &out_data) const override {
return {out_grad[permutohedral::kOut],
in_data[permutohedral::kData],
in_data[permutohedral::kPos],
out_data[permutohedral::kOut],
out_data[permutohedral::kNorm]
};
}
Operator* CreateOperator(Context ctx) const override {
LOG(FATAL) << "Not Implemented.";
return NULL;
}
Operator* CreateOperatorEx(Context ctx, std::vector<TShape> *in_shape,
std::vector<int> *in_type) const override;
private:
PermutohedralParam param_;
}; // class PermutohedralProperty
#endif // DMLC_USE_CXX11
} // namespace op
} // namespace mxnet
#endif // MXNET_OPERATOR_PERMUTOHEDRAL_INL_H_