Caffe2 - C++ API
A deep learning, cross platform ML framework
partition_ops.cc
1 #include "caffe2/operators/partition_ops.h"
2 
3 namespace caffe2 {
4 namespace {
5 
6 REGISTER_CPU_OPERATOR(Partition, PartitionOp);
7 REGISTER_CPU_OPERATOR(LengthsPartition, LengthsPartitionOp);
8 REGISTER_CPU_OPERATOR(GatherByKey, GatherByKeyOp);
9 
10 OPERATOR_SCHEMA(GatherByKey)
11  .NumInputs(2, INT_MAX)
12  .NumOutputs(1)
13  .SetDoc(R"DOC(
14 Inverse operation of Partition.
15 
16 Takes the original, full 'keys' tensor followed by sharded value tensors,
17 and returns the full value tensor, combined using the same hash used in
18 Partition.
19 )DOC")
20  .Input(
21  0,
22  "keys",
23  "The first input is the full keys tensor"
24  " (same as the first input of Partition).")
25  .Input(
26  1,
27  "sharded_values",
28  "Subsequented inputs are sharded values tensors.")
29  .Output(0, "values", "Reconstructed values tensor.");
30 
31 OPERATOR_SCHEMA(Partition)
32  .NumInputsOutputs([](int in, int out) {
33  return in > 0 && out > 0 && out % in == 0;
34  })
35  .SetDoc(R"DOC(
36 Splits the input int tensor into multiple ones according to the first tensor.
37 
38 Takes the first input and partitions it to shards according to the remainder of
39 values modulo the number of partitions. It requires that the first tensor is of
40 integral type. The number of partitions is derived as (num_output / num_input).
41 
42 If additional inputs are present they must have the same shape as the first
43 input, optionally with extra trailing dimensions. They will be partitioned
44 accordingly to the first input.
45 
46 Optional arg 'pack_first_input' transforms the first tensor values as
47 X_ij / num_partitions.
48 
49 Outputs are ordered as
50 X_0_part_0, X_1_part_0, ..., X_N-1_part_0, X_0_part_1, ..., X_N-1_part_K-1
51 )DOC")
52  .Arg(
53  "pack_first_input",
54  "(int, default 0) If set, the operator transforms "
55  "the first tensor values as floor(X_ij / num_partitions)")
56  .Input(
57  0,
58  "input",
59  "Input tensor containing data to be partitioned. The "
60  "number of input tensors might be greater than 1 but must have the "
61  "same shape as the previous tensors.")
62  .Output(
63  0,
64  "partitions",
65  "Output Partitions. The number of output tensors has to be a "
66  "multiple of the number of input tensors.");
67 
68 OPERATOR_SCHEMA(LengthsPartition)
69  .NumInputsOutputs([](int in, int out) {
70  return in >= 2 && out > 0 && out % in == 0;
71  })
72  .SetDoc(R"DOC(
73 LengthsPartition splits the input int tensor into multiple ones according to the
74 second tensor. The first dimension is expected to be the tensor that describes
75 lengths of the elements.
76 
77 Takes the second input and partitions it to shards according to the remainder of
78 values modulo the number of partitions. It requires the second tensor to be
79 a 1D-tensor of the integral type. The first tensor should be 1D-tensor of int32
80 that would represent the lengths of the elements in the input. The number of
81 partitions is derived as (num_output / num_input).
82 
83 If additional inputs are present they must have the same shape as the first
84 input, optionally with extra trailing dimensions. They will be partitioned
85 accordingly to the first input.
86 
87 Optional arg 'pack_first_input' transforms the first tensor values as
88 X_ij / num_partitions.
89 
90 Outputs are ordered as
91 X_0_part_0, X_1_part_0, ..., X_N-1_part_0, X_0_part_1, ..., X_N-1_part_K-1
92 )DOC")
93  .Arg(
94  "pack_first_input",
95  "(int, default 0) If set, the operator transforms "
96  "the first tensor values as floor(X_ij / num_partitions)")
97  .Input(
98  0,
99  "input",
100  "Input tensor containing data to be partitioned. The "
101  "number of input tensors might be greater than 1 but must have the "
102  "same shape as the previous tensors.")
103  .Output(
104  0,
105  "partitions",
106  "Output Partitions. The number of output tensors has to be a "
107  "multiple of the number of input tensors.");
108 
109 // This should actually have gradient, but for now nothing uses it.
110 // Because gradient computation right now is not input/output aware it can't be
111 // GRADIENT_NOT_IMPLEMENTEDYET
112 NO_GRADIENT(Partition);
113 NO_GRADIENT(LengthsPartition);
114 } // namespace
115 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13