1 #ifndef CAFFE2_OPERATORS_DENSE_VECTOR_TO_ID_LIST_OP_H_ 2 #define CAFFE2_OPERATORS_DENSE_VECTOR_TO_ID_LIST_OP_H_ 6 #include "caffe2/core/context.h" 7 #include "caffe2/core/operator.h" 11 template <
class Context>
14 USE_OPERATOR_CONTEXT_FUNCTIONS;
17 template <
typename T,
typename M>
18 bool DoRunWithType() {
19 auto& input =
Input(0);
20 const auto* input_data = input.template data<T>();
22 CAFFE_ENFORCE_EQ(input.dim(), 2,
"Sample should be 2-D");
23 const auto batch_size = input.size(0);
24 const auto col_num = input.size(1);
26 auto* out_lengths = Output(0, {batch_size}, at::dtype<int32_t>());
28 auto* out_lengths_data = out_lengths->template mutable_data<int32_t>();
30 auto* out_values = Output(1, {batch_size * col_num}, at::dtype<M>());
32 auto* out_values_data = out_values->template mutable_data<M>();
36 for (
auto i = 0; i < batch_size; i++) {
38 for (
int j = 0; j < col_num; j++) {
39 if ((
int)(input_data[i * col_num + j] + 0.5) != 0) {
40 out_values_data[v_pos++] = j;
44 out_lengths_data[l_pos++] = length;
46 out_values->Resize(v_pos);
47 out_lengths->Resize(l_pos);
51 bool RunOnDevice()
override {
52 if (
Input(0).
template IsType<float>()) {
53 return DoRunWithType<float, int>();
56 "DenseVectorToIdList operator only supports 32-bit float, but",
57 " input was of type ",
58 Input(0).dtype().name());
65 #endif // CAFFE2_OPERATORS_DENSE_VECTOR_TO_ID_LIST_OP_H_ const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position 'idx' for this operator. ...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...