aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/reduce_slice_ops/ops/reduce_slice_ops.cc
blob: 92879ab5356623dfa82fce8dff8db4d3036ae46c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"

namespace tensorflow {

using shape_inference::DimensionHandle;
using shape_inference::InferenceContext;
using shape_inference::ShapeHandle;

namespace {

Status ReduceSliceShapeFn(InferenceContext* c) {
  ShapeHandle handle;
  DimensionHandle dimhandle;
  DimensionHandle dim_axis = c->UnknownDim();
  // "axis" must be a scala
  TF_RETURN_IF_ERROR(c->WithRank(c->input(2), 0, &handle));
  // "data" must have rank at least 1
  TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(0), 1, &handle));
  // "indices" must have have rank 1 or rank 2 with the number of columns must
  // be 2
  if (c->RankKnown(c->input(1))) {
    TF_RETURN_IF_ERROR(c->WithRankAtLeast(c->input(1), 1, &handle));
    TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(1), 2, &handle));
    if (c->Rank(c->input(1)) == 1) {
      // if "indices" is a vector of 0 elements, then the axis dimension of
      // output tensor should be of dimension 0.
      DimensionHandle raw_dim_axis;
      TF_RETURN_IF_ERROR(c->Max(c->Dim(c->input(1), 0), 1, &raw_dim_axis));
      TF_RETURN_IF_ERROR(c->Subtract(raw_dim_axis, 1, &dim_axis));
    } else {  // c->Rank(c->input(1)) == 2
      TF_RETURN_IF_ERROR(
          c->Merge(c->Dim(c->input(1), 1), c->MakeDim(2), &dimhandle));
      dim_axis = c->Dim(c->input(1), 0);
    }
  }
  // shape of output tensor
  const Tensor* _axis = c->input_tensor(2);
  if (nullptr == _axis) {
    c->set_output(0, c->UnknownShapeOfRank(c->Rank(c->input(0))));
  } else {
    int64 axis = _axis->scalar<int64>()();
    TF_RETURN_IF_ERROR(c->ReplaceDim(handle, axis, dim_axis, &handle));
    c->set_output(0, handle);
  }
  return Status::OK();
}

}  // namespace

REGISTER_OP("ReduceSliceSum")
    .Input("data: T")
    .Input("indices: Tindices")
    .Input("axis: int64")
    .Output("output: T")
    .Attr("T: numbertype")
    .Attr("Tindices: {int32,int64}")
    .SetShapeFn(ReduceSliceShapeFn)
    .Doc(R"doc(
Dynamically sum over the first dimension of a tensor according to start and end
indices specified at 'index'.

For example:

```prettyprint
# if 'data' is [[   1,   2,   3]
                [  40,  50,  60]
                [ 700, 800, 900]
                [1000,2000,3000]],

and 'indices' is [[0,1]
                  [1,1]
                  [0,2]],

the output will be [[ 1, 2, 3]
                    [ 0, 0, 0]
                    [41,52,63]].
```

The data must be at least rank 1. The indices must be of shape (?,2) where the
first column is start indices and the second column is end indices. The end indices
are not included in the reduce operation, which means, if you want to do a reduce
over indices 0,1,2, then you should have start index 0 and end index 3. If end
index is smaller than or equal to start, the result will be zero. If end index is
out of bounds, then the reduce operation will automatically stop at the bound, so
feel free to put a large number as your end of your index if you want to do the
reduction until the bound.

data: The source of data where the computation will be taken from.
indices: start, end indices that controls which part to be included.
T: the type of data.
Tindices: the type of indices, must be int32 or int64.
output: the computed sum values.
)doc");

REGISTER_OP("ReduceSliceProd")
    .Input("data: T")
    .Input("indices: Tindices")
    .Input("axis: int64")
    .Output("output: T")
    .Attr("T: numbertype")
    .Attr("Tindices: {int32,int64}")
    .SetShapeFn(ReduceSliceShapeFn)
    .Doc(R"doc(
Dynamically compute the product over the first dimension of a tensor according
to start and end indices specified at 'indices'.

For example:

```prettyprint
# if 'data' is [[   1,   2,   3]
                [  40,  50,  60]
                [ 700, 800, 900]
                [1000,2000,3000]],

and 'indices' is [[0,1]
                  [1,1]
                  [0,2]],

the output will be [[ 1,  2,  3]
                    [ 1,  1,  1]
                    [40,100,180]].
```

The data must be at least rank 1. The indices can be of shape (?,2) where the
first column is start indices and the second column is end indices. The end indices
are not included in the reduce operation, which means, if you want to do a reduce
over indices 0,1,2, then you should have start index 0 and end index 3. If end
index is smaller than or equal to start, the result will be 1. If end index is
out of bounds, then the reduce operation will automatically stop at the bound, so
feel free to put a large number as your end of your index if you want to do the
reduction until the bound. The indices can also be of shape (?), in this case, the
start index of i will be the element at i, then end index of i will be the element
at i+1. That is:

```prettyprint
indices = [0,5,11,115]

is equivalent to

indices = [ [0,5],
            [5,11],
            [11,115]]
```

data: The source of data where the computation will be taken from.
indices: start, end indices that controls which part to be included.
T: the type of data.
Tindices: the type of indices, must be int32 or int64.
output: the computed product values.
)doc");

REGISTER_OP("ReduceSliceMax")
    .Input("data: T")
    .Input("indices: Tindices")
    .Input("axis: int64")
    .Output("output: T")
    .Attr("T: numbertype")
    .Attr("Tindices: {int32,int64}")
    .SetShapeFn(ReduceSliceShapeFn)
    .Doc(R"doc(
Dynamically compute the maximum over the first dimension of a tensor according
to start and end indices specified at "indices".

For example:

```prettyprint
# if 'data' is [[   1,  20,   3]
                [ 400,   5,  60]
                [  70,   8, 900]
                [1000,2000,3000]],

and 'indices' is [[0,1]
                  [1,1]
                  [0,2]],

the output will be [[          1,         20,          3]
                    [ -BIG_VALUE, -BIG_VALUE, -BIG_VALUE]
                    [        400,         20,         60]].
```

The data must be at least rank 1. The indices can be of shape (?,2) where the
first column is start indices and the second column is end indices. The end indices
are not included in the reduce operation, which means, if you want to do a reduce
over indices 0,1,2, then you should have start index 0 and end index 3. If end
index is smaller than or equal to start, the result will be 1. If end index is
out of bounds, then the reduce operation will automatically stop at the bound, so
feel free to put a large number as your end of your index if you want to do the
reduction until the bound. The indices can also be of shape (?), in this case, the
start index of i will be the element at i, then end index of i will be the element
at i+1. That is:

```prettyprint
indices = [0,5,11,115]

is equivalent to

indices = [ [0,5],
            [5,11],
            [11,115]]
```

data: The source of data where the computation will be taken from.
indices: start, end indices that controls which part to be included.
T: the type of data.
Tindices: the type of indices, must be int32 or int64.
output: the computed product values.
)doc");

REGISTER_OP("ReduceSliceMin")
    .Input("data: T")
    .Input("indices: Tindices")
    .Input("axis: int64")
    .Output("output: T")
    .Attr("T: numbertype")
    .Attr("Tindices: {int32,int64}")
    .SetShapeFn(ReduceSliceShapeFn)
    .Doc(R"doc(
Dynamically compute the minimum over the first dimension of a tensor according
to start and end indices specified at 'indices'.

For example:

```prettyprint
# if 'data' is [[   1,  20,   3]
                [ 400,   5,  60]
                [  70,   8, 900]
                [1000,2000,3000]],

and 'indices' is [[0,1]
                  [1,1]
                  [0,2]],

the output will be [[          1,         20,          3]
                    [ +BIG_VALUE, +BIG_VALUE, +BIG_VALUE]
                    [          1,          5,          3]].
```

The data must be at least rank 1. The indices can be of shape (?,2) where the
first column is start indices and the second column is end indices. The end indices
are not included in the reduce operation, which means, if you want to do a reduce
over indices 0,1,2, then you should have start index 0 and end index 3. If end
index is smaller than or equal to start, the result will be 1. If end index is
out of bounds, then the reduce operation will automatically stop at the bound, so
feel free to put a large number as your end of your index if you want to do the
reduction until the bound. The indices can also be of shape (?), in this case, the
start index of i will be the element at i, then end index of i will be the element
at i+1. That is:

```prettyprint
indices = [0,5,11,115]

is equivalent to

indices = [ [0,5],
            [5,11],
            [11,115]]
```

data: The source of data where the computation will be taken from.
indices: start, end indices that controls which part to be included.
T: the type of data.
Tindices: the type of indices, must be int32 or int64.
output: the computed product values.
)doc");

}  // namespace tensorflow