Caffe2 - C++ API
A deep learning, cross platform ML framework
extend_tensor_op.cc
1 #include <atomic>
2 #include <mutex>
3 #include <unordered_map>
4 #include <vector>
5 #include "caffe2/core/operator.h"
6 #include "caffe2/core/tensor.h"
7 #include "caffe2/utils/math.h"
8 
9 namespace caffe2 {
10 
11 namespace {
12 
13 template <class Context>
14 class ExtendTensorOp final : public Operator<Context> {
15  public:
16  USE_OPERATOR_CONTEXT_FUNCTIONS;
17  ExtendTensorOp(const OperatorDef& operator_def, Workspace* ws)
18  : Operator<Context>(operator_def, ws),
19  growthPct_(OperatorBase::GetSingleArgument<int>("growthPct", 40)) {}
20 
21  bool RunOnDevice() override {
22  auto& old_tensor = Input(0);
23  auto& indices = Input(1);
24  auto* new_tensor = Output(0);
25  CAFFE_ENFORCE(indices.ndim() >= 1);
26  CAFFE_ENFORCE(
27  &old_tensor == new_tensor, "First argument must be in-place.");
28  CAFFE_ENFORCE(new_tensor->ndim() == indices.ndim());
29  CAFFE_ENFORCE(indices.ndim() == new_tensor->ndim());
30 
31  auto oldSize = new_tensor->size();
32  auto maxElem = 1 +
33  *(std::max_element(
34  indices.template data<int>(),
35  indices.template data<int>() + indices.size()));
36 
37  auto extendSize = (TIndex)maxElem - oldSize;
38  if (extendSize > 0) {
39  new_tensor->Extend(extendSize, growthPct_, &context_);
40  if (!new_tensor->meta().ctor()) {
41  auto oldSizeBytes = oldSize * new_tensor->meta().itemsize();
42  auto* dst = (char*)new_tensor->raw_mutable_data() + oldSizeBytes;
43  math::Set<char, Context>(
44  new_tensor->nbytes() - oldSizeBytes, 0, dst, &context_);
45  }
46  }
47  return true;
48  }
49 
50  int growthPct_;
51 };
52 
53 REGISTER_CPU_OPERATOR(ExtendTensor, ExtendTensorOp<CPUContext>);
54 
55 OPERATOR_SCHEMA(ExtendTensor)
56  .NumInputs(2)
57  .NumOutputs(1)
58  .EnforceInplace({{0, 0}})
59  .SetDoc(R"DOC(
60 Extend input 0 if necessary based on max element in input 1.
61 Input 0 must be the same as output, that is, it is required to be in-place.
62 Input 0 may have to be re-allocated in order for accommodate to the new size.
63 Currently, an exponential growth ratio is used in order to ensure amortized
64 constant time complexity.
65 All except the outer-most dimension must be the same between input 0 and 1.
66 )DOC")
67  .Input(0, "tensor", "The tensor to be extended.")
68  .Input(
69  1,
70  "new_indices",
71  "The size of tensor will be extended based on max element in "
72  "new_indices.")
73  .Output(
74  0,
75  "extended_tensor",
76  "Same as input 0, representing the mutated tensor.");
77 }
78 } // namespace
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...