1/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include "tensorflow/core/framework/common_shape_fns.h"
16#include "tensorflow/core/framework/op.h"
17#include "tensorflow/core/framework/shape_inference.h"
18
19namespace tensorflow {
20
21using shape_inference::DimensionHandle;
22using shape_inference::InferenceContext;
23using shape_inference::ShapeHandle;
24
25Status RaggedRangeShapeFn(InferenceContext* c);
26
27//==============================================================================
28// Registered Ops
29//==============================================================================
30
31REGISTER_OP("RaggedRange")
32 .Input("starts: T")
33 .Input("limits: T")
34 .Input("deltas: T")
35 .Output("rt_nested_splits: Tsplits")
36 .Output("rt_dense_values: T")
37 .Attr("T: {bfloat16, float, double, int32, int64} = DT_INT32")
38 .Attr("Tsplits: {int32, int64} = DT_INT64")
39 .SetShapeFn(RaggedRangeShapeFn);
40
41//==============================================================================
42// Shape Functions
43//==============================================================================
44
45Status RaggedRangeShapeFn(InferenceContext* c) {
46 // Check that all inputs (starts, limits, and deltas) have rank 0 or 1.
47 ShapeHandle starts = c->input(0);
48 ShapeHandle limits = c->input(1);
49 ShapeHandle deltas = c->input(2);
50 TF_RETURN_IF_ERROR(c->WithRankAtMost(starts, 1, &starts));
51 TF_RETURN_IF_ERROR(c->WithRankAtMost(limits, 1, &limits));
52 TF_RETURN_IF_ERROR(c->WithRankAtMost(deltas, 1, &deltas));
53
54 // For the inputs with rank 1, make sure shapes match.
55 DimensionHandle dim = c->UnknownDim();
56 if (c->Rank(starts) == 1) {
57 TF_RETURN_IF_ERROR(c->Merge(c->Dim(starts, 0), dim, &dim));
58 }
59 if (c->Rank(limits) == 1) {
60 TF_RETURN_IF_ERROR(c->Merge(c->Dim(limits, 0), dim, &dim));
61 }
62 if (c->Rank(deltas) == 1) {
63 TF_RETURN_IF_ERROR(c->Merge(c->Dim(deltas, 0), dim, &dim));
64 }
65
66 // If any input shape is known, then calculate `rt_nested_splits` shape.
67 int64_t rt_nested_splits_dim = InferenceContext::kUnknownDim;
68 if (c->ValueKnown(dim)) {
69 rt_nested_splits_dim = c->Value(dim) + 1;
70 } else if (c->Rank(starts) == 0 && c->Rank(limits) == 0 &&
71 c->Rank(deltas) == 0) {
72 rt_nested_splits_dim = 2;
73 }
74 c->set_output(0, c->Vector(rt_nested_splits_dim));
75
76 // `rt_dense_values` is rank 1, but size can't be calculated statically.
77 c->set_output(1, c->UnknownShapeOfRank(1));
78 return OkStatus();
79}
80
81} // namespace tensorflow
82