forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_sparse_to_dense_op.h
208 lines (179 loc) · 6 KB
/
batch_sparse_to_dense_op.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
// Copyright 2004-present Facebook. All Rights Reserved.
#ifndef CAFFE2_OPERATORS_BATCH_SPARSE_TO_DENSE_OP_H_
#define CAFFE2_OPERATORS_BATCH_SPARSE_TO_DENSE_OP_H_
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/utils/math.h"
namespace caffe2 {
template <typename T, class Context>
class BatchSparseToDenseOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
USE_DISPATCH_HELPER;
template <class... Args>
explicit BatchSparseToDenseOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...),
OP_SINGLE_ARG(int64_t, "dense_last_dim", dense_last_dim_, -1),
OP_SINGLE_ARG(T, "default_value", default_value_, static_cast<T>(0)) {}
bool RunOnDevice() override {
return DispatchHelper<TensorTypes<int32_t, int64_t>>::call(
this, Input(LENGTHS));
}
private:
template <typename TLen, typename TInd>
void FillInDenseValues(
const int64_t batch_size,
const int64_t indice_lengths,
const TLen* lengths_data,
const TInd* indices_data,
const T* values_data,
T* output_data,
Context* context);
template <typename TLen>
bool DoRunWithType() {
return DispatchHelper<
TensorTypes2<
int32_t,
int64_t,
GenericTensorImplementation>,
TLen>::call(this, Input(INDICES));
}
template <typename TLen, typename TInd>
bool DoRunWithType2() {
auto& lengths = Input(LENGTHS);
auto& indices = Input(INDICES);
auto& values = Input(VALUES);
CAFFE_ENFORCE_EQ(indices.numel(), values.numel());
CAFFE_ENFORCE_EQ(lengths.dim(), 1);
CAFFE_ENFORCE_EQ(indices.dim(), 1);
const TLen* lengths_data = lengths.template data<TLen>();
const TInd* indices_data = indices.template data<TInd>();
const T* values_data = values.template data<T>();
int64_t batch_size = lengths.numel();
vector<int64_t> output_shape = {batch_size};
if (InputSize() == 4) {
auto& shaper = Input(3);
CAFFE_ENFORCE_EQ(shaper.dim(), 2);
if (dense_last_dim_ == -1) {
dense_last_dim_ = shaper.size(1);
} else {
CAFFE_ENFORCE(
dense_last_dim_ == shaper.size(1),
"The last dim argument is not aligned with the shape input last dim");
}
} else {
CAFFE_ENFORCE(dense_last_dim_ >= 1, "The last dim of dense must be >= 1");
}
output_shape.push_back(dense_last_dim_);
auto* output = Output(0, output_shape, at::dtype<T>());
T* output_data = output->template mutable_data<T>();
math::Set(
output->numel(),
static_cast<T>(default_value_),
output_data,
&context_);
FillInDenseValues(
batch_size,
indices.numel(),
lengths_data,
indices_data,
values_data,
output_data,
&context_);
return true;
}
template <typename TLen>
bool DoRunWithOtherType2() {
CAFFE_THROW(
"BatchSparseToDense is not implemented on values of type ",
Input(VALUES).dtype().name(),
" with lengths of type ",
Input(LENGTHS).dtype().name(),
" and indices of type ",
Input(INDICES).dtype().name());
}
int64_t dense_last_dim_;
T default_value_;
INPUT_TAGS(LENGTHS, INDICES, VALUES);
// len_prefix_sum_ and len_prefix_tmp_ are buffers on the GPU. It is not used
// in the CPUContext implementation.
Tensor len_prefix_sum_{Context::GetDeviceType()};
Tensor len_prefix_tmp_{Context::GetDeviceType()};
};
template <typename T, class Context>
class BatchDenseToSparseOp : public Operator<Context> {
public:
USE_OPERATOR_CONTEXT_FUNCTIONS;
USE_DISPATCH_HELPER;
template <class... Args>
explicit BatchDenseToSparseOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...) {}
bool RunOnDevice() {
return DispatchHelper<TensorTypes<int32_t, int64_t>>::call(
this, Input(LENGTHS));
}
private:
template <typename TLen, typename TInd>
void FillInSparseValues(
const int64_t batch_size,
const int64_t indice_lengths,
const TLen* lengths_data,
const TInd* indices_data,
const T* dense_data,
T* output_data,
Context* context);
template <typename TLen>
bool DoRunWithType() {
return DispatchHelper<
TensorTypes2<
int32_t,
int64_t,
GenericTensorImplementation>,
TLen>::call(this, Input(INDICES));
}
template <typename TLen, typename TInd>
bool DoRunWithType2() {
auto& lengths = Input(LENGTHS);
auto& indices = Input(INDICES);
auto& dense = Input(DENSE);
CAFFE_ENFORCE_EQ(lengths.dim(), 1);
CAFFE_ENFORCE_EQ(indices.dim(), 1);
CAFFE_ENFORCE_EQ(dense.dim(), 2);
const TLen* lengths_data = lengths.template data<TLen>();
const TInd* indices_data = indices.template data<TInd>();
const T* dense_data = dense.template data<T>();
int64_t batch_size = lengths.numel();
CAFFE_ENFORCE_EQ(batch_size, dense.size(0));
dense_last_dim_ = dense.size(1);
vector<int64_t> output_shape = indices.sizes().vec();
auto* output = Output(0, output_shape, at::dtype<T>());
T* output_data = output->template mutable_data<T>();
FillInSparseValues(
batch_size,
indices.numel(),
lengths_data,
indices_data,
dense_data,
output_data,
&context_);
return true;
}
template <typename TLen>
bool DoRunWithOtherType2() {
CAFFE_THROW(
"BatchDenseToSparse is not implemented on values of type ",
Input(DENSE).dtype().name(),
" with lengths of type ",
Input(LENGTHS).dtype().name(),
" and indices of type ",
Input(INDICES).dtype().name());
}
int64_t dense_last_dim_{};
INPUT_TAGS(LENGTHS, INDICES, DENSE);
// len_prefix_sum_ and len_prefix_tmp_ are buffers on the GPU. It is not used
// in the CPUContext implementation.
Tensor len_prefix_sum_{Context::GetDeviceType()};
Tensor len_prefix_tmp_{Context::GetDeviceType()};
};
} // namespace caffe2
#endif // CAFFE2_OPERATORS_BATCH_SPARSE_TO_DENSE_OP_H_