1 | /* Copyright 2018 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/op.h" |
17 | #include "tensorflow/core/framework/shape_inference.h" |
18 | |
19 | namespace tensorflow { |
20 | |
21 | using tensorflow::shape_inference::InferenceContext; |
22 | using tensorflow::shape_inference::ShapeHandle; |
23 | |
24 | REGISTER_OP("EncodeProto" ) |
25 | .Input("sizes: int32" ) |
26 | .Input("values: Tinput_types" ) |
27 | .Attr("field_names: list(string)" ) |
28 | .Attr("message_type: string" ) |
29 | .Attr("descriptor_source: string = 'local://'" ) |
30 | .Attr("Tinput_types: list(type)" ) |
31 | .Output("bytes: string" ) |
32 | .SetShapeFn([](InferenceContext* c) { |
33 | int first_field_index = 1; |
34 | int num_fields = c->num_inputs() - 1; |
35 | |
36 | ShapeHandle output; |
37 | for (int i = num_fields - 1; i >= 0; --i) { |
38 | ShapeHandle input = c->input(first_field_index + i); |
39 | TF_RETURN_IF_ERROR(c->WithRankAtLeast(input, 2, &input)); |
40 | ShapeHandle inner; |
41 | TF_RETURN_IF_ERROR(c->Subshape(input, 0, -1, &inner)); |
42 | TF_RETURN_IF_ERROR(c->Merge(inner, output, &output)); |
43 | } |
44 | |
45 | c->set_output(0, output); |
46 | return OkStatus(); |
47 | }); |
48 | |
49 | } // namespace tensorflow |
50 | |