Caffe2 - C++ API
A deep learning, cross platform ML framework
sparse_matrix_reshape_op.h
1 #ifndef CAFFE2_OPERATORS_SPARSE_MATRIX_RESHAPE_H_
2 #define CAFFE2_OPERATORS_SPARSE_MATRIX_RESHAPE_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/operator.h"
6 #include "caffe2/utils/math.h"
7 
8 namespace caffe2 {
9 
10 template <class Context>
11 class SparseMatrixReshapeOp : public Operator<Context> {
12  public:
13  USE_OPERATOR_CONTEXT_FUNCTIONS;
14  SparseMatrixReshapeOp(const OperatorDef& operator_def, Workspace* ws)
15  : Operator<Context>(operator_def, ws) {
16  CAFFE_ENFORCE(
17  OperatorBase::HasArgument("old_shape"),
18  "Argument `old_shape` is missing.");
19  CAFFE_ENFORCE(
20  OperatorBase::HasArgument("new_shape"),
21  "Argument `new_shape` is missing.");
22 
23  vector<TIndex> old_shape =
24  OperatorBase::GetRepeatedArgument<TIndex>("old_shape");
25  vector<TIndex> new_shape =
26  OperatorBase::GetRepeatedArgument<TIndex>("new_shape");
27 
28  CAFFE_ENFORCE(
29  old_shape.size() == 2,
30  "Argument `old_shape` must contain exactly two integers.");
31  CAFFE_ENFORCE(
32  new_shape.size() == 2,
33  "Argument `new_shape` must contain exactly two integers.");
34 
35  CAFFE_ENFORCE(
36  old_shape[1] > 0,
37  "The second dimension in argument `old_shape` must be positive.");
38 
39  old_stride_ = old_shape[1];
40 
41  if (old_shape[0] == -1) {
42  CAFFE_ENFORCE(
43  new_shape[1] > 0,
44  "The second dimension in `new_shape` must be positive.");
45  } else {
46  CAFFE_ENFORCE(
47  old_shape[0] > 0,
48  "The first dimension in `old_shape` must be positive.");
49 
50  TIndex matrix_size = old_shape[0] * old_shape[1];
51 
52  if (new_shape[0] == -1) {
53  CAFFE_ENFORCE(
54  new_shape[1] > 0,
55  "Only one dimension in argument `new_shape` can be -1.");
56  CAFFE_ENFORCE(
57  matrix_size % new_shape[1] == 0,
58  "Argument `new_shape` does not agree with `old_shape`.");
59  } else {
60  CAFFE_ENFORCE(
61  new_shape[0] > 0 && (new_shape[1] == -1 || new_shape[1] > 0),
62  "Dimensions in argument `new_shape` must be positive or -1.");
63  if (new_shape[1] == -1) {
64  CAFFE_ENFORCE(
65  matrix_size % new_shape[0] == 0,
66  "Argument `new_shape` does not agree with `old_shape`.");
67  new_shape[1] = matrix_size / new_shape[0];
68  } else {
69  CAFFE_ENFORCE(
70  new_shape[0] * new_shape[1] == matrix_size,
71  "Argument `new_shape` does not agree with `old_shape`.");
72  }
73  }
74  }
75  new_stride_ = new_shape[1];
76  }
77 
78  bool RunOnDevice() override {
79  auto& old_col = Input(0);
80  CAFFE_ENFORCE(old_col.ndim() == 1, "Row index tensor must be 1-D.");
81  auto& old_row = Input(1);
82  CAFFE_ENFORCE(old_row.ndim() == 1, "Column index tensor must be 1-D.");
83 
84  const auto nnz = old_col.size();
85  CAFFE_ENFORCE(
86  old_row.size() == nnz,
87  "Column and row tensors must have the same size.");
88 
89  auto* new_col = Output(0);
90  auto* new_row = Output(1);
91  new_col->Resize(nnz);
92  new_row->Resize(nnz);
93 
94  const auto* old_col_data = old_col.template data<TIndex>();
95  const auto* old_row_data = old_row.template data<int>();
96 
97  auto* new_col_data = new_col->template mutable_data<TIndex>();
98  auto* new_row_data = new_row->template mutable_data<int>();
99 
100  for (int i = 0; i < nnz; ++i) {
101  TIndex offset = old_row_data[i] * old_stride_ + old_col_data[i];
102  new_row_data[i] = offset / new_stride_;
103  new_col_data[i] = offset % new_stride_;
104  }
105 
106  return true;
107  }
108 
109  private:
110  TIndex old_stride_;
111  TIndex new_stride_;
112 };
113 
114 } // namespace caffe2
115 
116 #endif // CAFFE2_OPERATORS_SPARSE_MATRIX_RESHAPE_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.
Definition: operator.h:37