1/* Copyright 2020 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
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
20namespace tensorflow {
21
22using shape_inference::InferenceContext;
23using shape_inference::ShapeHandle;
24
25Status DenseCountSparseOutputShapeFn(InferenceContext *c) {
26 auto values = c->input(0);
27 auto weights = c->input(1);
28 ShapeHandle output;
29 auto num_weights = c->NumElements(weights);
30 if (c->ValueKnown(num_weights) && c->Value(num_weights) == 0) {
31 output = values;
32 } else {
33 TF_RETURN_IF_ERROR(c->Merge(weights, values, &output));
34 }
35 auto rank = c->Rank(output);
36 auto nvals = c->UnknownDim();
37 c->set_output(0, c->Matrix(nvals, rank)); // out.indices
38 c->set_output(1, c->Vector(nvals)); // out.values
39 c->set_output(2, c->Vector(rank)); // out.dense_shape
40 return OkStatus();
41}
42
43Status SparseCountSparseOutputShapeFn(InferenceContext *c) {
44 ShapeHandle unused;
45 TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 2, &unused));
46 auto rank = c->Dim(c->input(0), 1);
47 auto nvals = c->UnknownDim();
48 c->set_output(0, c->Matrix(nvals, rank)); // out.indices
49 c->set_output(1, c->Vector(nvals)); // out.values
50 c->set_output(2, c->Vector(rank)); // out.dense_shape
51 return OkStatus();
52}
53
54Status RaggedCountSparseOutputShapeFn(InferenceContext *c) {
55 int32_t rank = c->Rank(c->input(1));
56 if (rank != c->kUnknownRank) {
57 ++rank; // Add the ragged dimension
58 }
59 auto nvals = c->UnknownDim();
60 c->set_output(0, c->Matrix(nvals, rank)); // out.indices
61 c->set_output(1, c->Vector(nvals)); // out.values
62 c->set_output(2, c->Vector(rank)); // out.dense_shape
63 return OkStatus();
64}
65
66REGISTER_OP("DenseCountSparseOutput")
67 .Input("values: T")
68 .Input("weights: output_type")
69 .Attr("T: {int32, int64}")
70 .Attr("minlength: int >= -1 = -1")
71 .Attr("maxlength: int >= -1 = -1")
72 .Attr("binary_output: bool")
73 .Attr("output_type: {int32, int64, float, double}")
74 .SetShapeFn(DenseCountSparseOutputShapeFn)
75 .Output("output_indices: int64")
76 .Output("output_values: output_type")
77 .Output("output_dense_shape: int64");
78
79REGISTER_OP("SparseCountSparseOutput")
80 .Input("indices: int64")
81 .Input("values: T")
82 .Input("dense_shape: int64")
83 .Input("weights: output_type")
84 .Attr("T: {int32, int64}")
85 .Attr("minlength: int >= -1 = -1")
86 .Attr("maxlength: int >= -1 = -1")
87 .Attr("binary_output: bool")
88 .Attr("output_type: {int32, int64, float, double}")
89 .SetShapeFn(SparseCountSparseOutputShapeFn)
90 .Output("output_indices: int64")
91 .Output("output_values: output_type")
92 .Output("output_dense_shape: int64");
93
94REGISTER_OP("RaggedCountSparseOutput")
95 .Input("splits: int64")
96 .Input("values: T")
97 .Input("weights: output_type")
98 .Attr("T: {int32, int64}")
99 .Attr("minlength: int >= -1 = -1")
100 .Attr("maxlength: int >= -1 = -1")
101 .Attr("binary_output: bool")
102 .Attr("output_type: {int32, int64, float, double}")
103 .SetShapeFn(RaggedCountSparseOutputShapeFn)
104 .Output("output_indices: int64")
105 .Output("output_values: output_type")
106 .Output("output_dense_shape: int64");
107
108} // namespace tensorflow
109