Caffe2 - C++ API
A deep learning, cross platform ML framework
expand_op.cc
1 #include "caffe2/operators/expand_op.h"
2 
3 #include <algorithm>
4 #include <functional>
5 #include <vector>
6 
7 #include <caffe2/utils/math.h>
8 
9 namespace caffe2 {
10 
11 REGISTER_CPU_OPERATOR(
12  Expand,
13  ExpandOp<
14  TensorTypes<std::int32_t, std::int64_t, float, double>,
15  CPUContext>);
16 
17 REGISTER_CPU_OPERATOR(
18  ExpandGradient,
19  ExpandGradientOp<
20  TensorTypes<std::int32_t, std::int64_t, float, double>,
21  CPUContext>);
22 
23 OPERATOR_SCHEMA(Expand)
24  .NumInputs(2)
25  .NumOutputs(1)
26  .SetDoc(R"DOC(
27  Broadcast the input tensor to a materialized new tensor using given shape.
28  Broadcast rule is similar to "numpy.array(input) * numpy.ones(shape)":
29  Dimensions are right alignment;
30  Two corresponding dimensions must have the same value, or one of them
31  equals to 1.
32  In order to align with PyTorch's `expand`, `shape` is allowed to have entries
33  equal to -1, which means to preserve the size of the corresponding dimension
34  in `X` (so it's actually equivalent to equal to 1).
35 )DOC")
36  .Input(0, "X", "(*Tensor`<NumericType>`*): input tensor")
37  .Input(1, "shape", "(*Tensor`<int>`*): expand shape")
38  .Output(0, "Y", "(*Tensor`<NumericType>`*): expanded tensor");
39 
40 OPERATOR_SCHEMA(ExpandGradient).NumInputs(2).NumOutputs(1);
41 
42 namespace {
43 
44 class GetExpandGradient final : public GradientMakerBase {
45  using GradientMakerBase::GradientMakerBase;
46  std::vector<OperatorDef> GetGradientDefs() override {
47  return SingleGradientDef(
48  "ExpandGradient",
49  "",
50  std::vector<string>{GO(0), I(0)},
51  std::vector<string>{GI(0)});
52  }
53 };
54 
55 } // namespace
56 
57 REGISTER_GRADIENT(Expand, GetExpandGradient);
58 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13