• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/framework/common_shape_fns.h"
17 #include "tensorflow/core/framework/op.h"
18 #include "tensorflow/core/framework/shape_inference.h"
19 
20 namespace tensorflow {
21 
22 using shape_inference::DimensionHandle;
23 using shape_inference::InferenceContext;
24 using shape_inference::ShapeHandle;
25 
26 REGISTER_OP("StringToHashBucketFast")
27     .Input("input: string")
28     .Output("output: int64")
29     .Attr("num_buckets: int >= 1")
30     .SetShapeFn(shape_inference::UnchangedShape);
31 
32 REGISTER_OP("StringToHashBucketStrong")
33     .Input("input: string")
34     .Output("output: int64")
35     .Attr("num_buckets: int >= 1")
36     .Attr("key: list(int)")
37     .SetShapeFn(shape_inference::UnchangedShape);
38 
39 REGISTER_OP("StringToHashBucket")
40     .Input("string_tensor: string")
41     .Output("output: int64")
42     .Attr("num_buckets: int >= 1")
43     .SetShapeFn(shape_inference::UnchangedShape);
44 
45 REGISTER_OP("ReduceJoin")
46     .Input("inputs: string")
47     .Input("reduction_indices: int32")
48     .Attr("keep_dims: bool = false")
49     .Attr("separator: string = ''")
50     .Output("output: string")
51     .SetShapeFn(shape_inference::ReductionShape);
52 
53 REGISTER_OP("AsString")
54     .Input("input: T")
55     .Output("output: string")
56     .Attr("T: {int32, int64, complex64, float, double, bool, int8}")
57     .Attr("precision: int = -1")
58     .Attr("scientific: bool = false")
59     .Attr("shortest: bool = false")
60     .Attr("width: int = -1")
61     .Attr("fill: string = ''")
62     .SetShapeFn(shape_inference::UnchangedShape);
63 
64 REGISTER_OP("StringJoin")
65     .Input("inputs: N * string")
66     .Attr("N: int")
67     .Attr("separator: string = ''")
68     .Output("output: string")
__anon5bd2fe870102(InferenceContext* c) 69     .SetShapeFn([](InferenceContext* c) {
70       // If all inputs are scalars, then return a scalar.
71       bool all_scalar = true;
72       for (int i = 0; i < c->num_inputs(); ++i) {
73         if (c->Rank(c->input(i)) != 0) all_scalar = false;
74       }
75       if (all_scalar) {
76         c->set_output(0, c->Scalar());
77         return Status::OK();
78       }
79 
80       // At least one input is unknown or a scalar.
81       // Merge the non-scalars to find the output shape.
82       // Don't merge inputs with unknown rank, as they can actually be scalars
83       // or the output shape.
84       ShapeHandle out = c->UnknownShape();
85       for (int i = 0; i < c->num_inputs(); ++i) {
86         if (c->RankKnown(c->input(i)) && c->Rank(c->input(i)) != 0) {
87           TF_RETURN_IF_ERROR(c->Merge(out, c->input(i), &out));
88         }
89       }
90       c->set_output(0, out);
91       return Status::OK();
92     });
93 
94 REGISTER_OP("StringSplit")
95     .Input("input: string")
96     .Input("delimiter: string")
97     .Output("indices: int64")
98     .Output("values: string")
99     .Output("shape: int64")
100     .Attr("skip_empty: bool = true")
__anon5bd2fe870202(InferenceContext* c) 101     .SetShapeFn([](InferenceContext* c) {
102       ShapeHandle unused;
103       TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 1, &unused));
104       TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &unused));
105 
106       c->set_output(0, c->Matrix(InferenceContext::kUnknownDim, 2));
107       c->set_output(1, c->Vector(InferenceContext::kUnknownDim));
108       c->set_output(2, c->Vector(2));
109       return Status::OK();
110     });
111 
112 REGISTER_OP("EncodeBase64")
113     .Input("input: string")
114     .Output("output: string")
115     .Attr("pad: bool = false")
116     .SetShapeFn(shape_inference::UnchangedShape);
117 
118 REGISTER_OP("DecodeBase64")
119     .Input("input: string")
120     .Output("output: string")
121     .SetShapeFn(shape_inference::UnchangedShape);
122 
123 REGISTER_OP("Substr")
124     .Input("input: string")
125     .Input("pos: T")
126     .Input("len: T")
127     .Output("output: string")
128     .Attr("T: {int32, int64}")
__anon5bd2fe870302(InferenceContext* c) 129     .SetShapeFn([](InferenceContext* c) {
130       ShapeHandle pos_shape = c->input(1);
131       ShapeHandle len_shape = c->input(2);
132       ShapeHandle unused;
133       // Check that pos/len have same rank
134       TF_RETURN_IF_ERROR(c->WithRank(pos_shape, c->Rank(len_shape), &unused));
135       // Check that dimensions are equal
136       for (int32 i = 0; i < c->Rank(pos_shape); ++i) {
137         DimensionHandle pos_dim = c->Dim(pos_shape, i);
138         DimensionHandle len_dim = c->Dim(len_shape, i);
139         if (c->Value(pos_dim) != c->Value(len_dim)) {
140           return errors::InvalidArgument(
141               "pos and len shapes must match: ", c->DebugString(pos_shape),
142               " vs. ", c->DebugString(len_shape));
143         }
144       }
145       // c->input(0) is the ShapeHandle to input strings
146       // BroadcastBinaryOpShapeFn infers shape from c->input(0) and c->input(1).
147       return shape_inference::BroadcastBinaryOpShapeFn(c);
148     });
149 
150 }  // namespace tensorflow
151