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
|
// 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;
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() {
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 int64_t* lengths_data = lengths.template data<int64_t>();
const int64_t* indices_data = indices.template data<int64_t>();
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;
}
private:
void FillInDenseValues(
const int64_t batch_size,
const int64_t indice_lengths,
const int64_t* lengths_data,
const int64_t* indices_data,
const T* values_data,
T* output_data,
Context* context);
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;
template <class... Args>
explicit BatchDenseToSparseOp(Args&&... args)
: Operator<Context>(std::forward<Args>(args)...) {}
bool RunOnDevice() {
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 int64_t* lengths_data = lengths.template data<int64_t>();
const int64_t* indices_data = indices.template data<int64_t>();
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;
}
private:
void FillInSparseValues(
const int64_t batch_size,
const int64_t indice_lengths,
const int64_t* lengths_data,
const int64_t* indices_data,
const T* dense_data,
T* output_data,
Context* context);
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_
|