Caffe2 - C++ API
A deep learning, cross platform ML framework
tile_op.cc
1 #include "caffe2/operators/tile_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(Tile, TileOp<CPUContext>);
6 REGISTER_CPU_OPERATOR(TileGradient, TileGradientOp<float, CPUContext>);
7 
8 OPERATOR_SCHEMA(Tile)
9  .NumInputs(1, 3)
10  .NumOutputs(1)
11  .TensorInferenceFunction(
12  [](const OperatorDef& def, const vector<TensorShape>& in) {
13  vector<TensorShape> out(1);
14  out[0] = TensorShape(in[0]);
15  ArgumentHelper helper(def);
16 
17  auto tiles = helper.GetSingleArgument<int32_t>("tiles", 1);
18  auto axis = helper.GetSingleArgument<int32_t>("axis", 0);
19  if (in.size() > 1) {
20  // Tile or axis is specified as input; we can't determine
21  // the size
22  out[0].set_unknown_shape(true);
23  } else {
24  const auto canonical_axis =
25  canonical_axis_index_(axis, out[0].dims().size());
26  out[0].set_dims(
27  canonical_axis, out[0].dims().Get(canonical_axis) * tiles);
28  }
29  return out;
30  })
31  .SetDoc(R"DOC(
32 Constructs a tensor by tiling a given tensor along a specified axis.
33 
34 This operation creates a new tensor by replicating the input tensor 'tiles'
35 times along dimension 'axis'. The output tensor's 'axis'th dimension has
36 input.dims(axis) * tiles elements, and the values of input are replicated
37 'tiles' times along the 'axis'th dimension.
38 For example, tiling [[a b c d]] by tile=2, axis=0 produces
39 [[a b c d], [a b c d]].
40 )DOC")
41  .Arg("tiles", "Number of replicas")
42  .Arg("axis", "Axis to replicate along")
43  .Input(0, "data", "The input tensor.")
44  .Input(1, "tiles", "(optional) Number of replicas (overrides argument)")
45  .Input(2, "axis", "(optional) Axis to replicate along (overrides argument)")
46  .Output(
47  0,
48  "tiled_data",
49  "Tensor that will contain input replicated along the given axis.")
50  .InheritOnnxSchema("Tile");
51 
52 OPERATOR_SCHEMA(TileGradient).NumInputs(1, 3).NumOutputs(1);
53 
54 class GetTileGradient : public GradientMakerBase {
55  using GradientMakerBase::GradientMakerBase;
56  vector<OperatorDef> GetGradientDefs() override {
57  // Check whether the tiles/axis information was
58  // passed through input arguments
59  vector<std::string> g_inputs({GO(0)});
60  if (Def().input_size() > 1) {
61  g_inputs.push_back(I(1));
62  }
63  if (Def().input_size() > 2) {
64  g_inputs.push_back(I(2));
65  }
66  return SingleGradientDef(
67  "TileGradient", "", g_inputs, vector<string>{GI(0)});
68  }
69 };
70 
71 REGISTER_GRADIENT(Tile, GetTileGradient);
72 
73 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...