1 /* Copyright 2019 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/common_runtime/eager/shape_inference.h"
17
18 #include <vector>
19
20 #include "tensorflow/core/common_runtime/eager/tensor_handle.h"
21 #include "tensorflow/core/framework/op.h"
22 #include "tensorflow/core/framework/shape_inference.h"
23 #include "tensorflow/core/public/version.h"
24
25 namespace tensorflow {
26 namespace eager {
27
RunShapeInference(const NodeDef & ndef,const FunctionLibraryDefinition & lib_def,const gtl::InlinedVector<TensorHandle *,4> & inputs,const gtl::InlinedVector<TensorHandle *,2> & retvals)28 Status RunShapeInference(const NodeDef& ndef,
29 const FunctionLibraryDefinition& lib_def,
30 const gtl::InlinedVector<TensorHandle*, 4>& inputs,
31 const gtl::InlinedVector<TensorHandle*, 2>& retvals) {
32 const tensorflow::OpRegistrationData* op_reg_data;
33 // TODO(b/141209983): Consider adding a shape inference cache.
34 // FunctionLibraryDefinition::LookUp delegates to global OpRegistry
35 // if op is not a function.
36 TF_RETURN_IF_ERROR(lib_def.LookUp(ndef.op(), &op_reg_data));
37 if (op_reg_data->shape_inference_fn == nullptr) return OkStatus();
38
39 shape_inference::InferenceContext ic(
40 TF_GRAPH_DEF_VERSION, ndef, op_reg_data->op_def,
41 std::vector<shape_inference::ShapeHandle>(inputs.size()), {}, {}, {});
42 for (size_t i = 0; i < inputs.size(); i++) {
43 shape_inference::ShapeHandle shape;
44 TF_RETURN_IF_ERROR(inputs[i]->InferenceShape(&ic, &shape));
45 ic.SetInput(i, shape);
46 }
47
48 TF_RETURN_IF_ERROR(ic.Run(op_reg_data->shape_inference_fn));
49 CHECK_EQ(ic.num_outputs(), retvals.size());
50 for (int i = 0; i < ic.num_outputs(); i++) {
51 shape_inference::ShapeHandle shape_handle = ic.output(i);
52 retvals[i]->SetInferenceShape(&ic, shape_handle);
53 }
54 // TODO(slebedev): populate TensorHandle::handle_dtypes_and_shapes.
55 return OkStatus();
56 }
57
58 } // namespace eager
59 } // namespace tensorflow
60