• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #ifndef TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_SHAPE_INFERENCE_UTILS_H_
17 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_SHAPE_INFERENCE_UTILS_H_
18 
19 #include <cstdint>
20 
21 #include "mlir/IR/Attributes.h"  // from @llvm-project
22 #include "mlir/IR/Operation.h"  // from @llvm-project
23 #include "mlir/IR/Types.h"  // from @llvm-project
24 #include "mlir/Interfaces/InferTypeOpInterface.h"  // from @llvm-project
25 #include "mlir/Support/LLVM.h"  // from @llvm-project
26 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
27 #include "tensorflow/core/framework/shape_inference.h"
28 
29 namespace mlir {
30 namespace TF {
31 
32 // Function that takes in a value and extracts a constant from it, if available.
33 // If the value cannot be resolved as a constant, a nullptr will be returned.
34 // Certain shape functions require constant values as arguments.
35 using OperandAsConstantFn = llvm::function_ref<Attribute(Value)>;
36 
37 // Function that takes in an operation result and computes a shape (can be
38 // partial) value. Certain shape functions require shape values as arguments.
39 using OpResultAsShapeFn =
40     llvm::function_ref<tensorflow::shape_inference::ShapeHandle(
41         tensorflow::shape_inference::InferenceContext&, OpResult)>;
42 
43 // Function that takes a result index and returns the element type. Element
44 // types are necessary for handle types (resource, variant).
45 using ResultElementTypeFn = llvm::function_ref<Type(int)>;
46 
47 // Runs TensorFlow shape inference associated to the op type registered in the
48 // TensorFlow op registry based on the Graph version, operands, and attributes.
49 // Invoking this shape function will create conversions of parameters to the
50 // TensorFlow Graph equivalent data structures and back to MLIR equivalent data
51 // structures. This does not use a natively implemented shape inference in MLIR,
52 // and instead is temporary until shape functions are reimplemented/migrated to
53 // being in MLIR instead of the TensorFlow op registry.
54 LogicalResult InferReturnTypeComponentsForTFOp(
55     Optional<Location> location, Operation* op, int64_t graph_version,
56     OperandAsConstantFn operand_as_constant_fn,
57     OpResultAsShapeFn op_result_as_shape_fn,
58     ResultElementTypeFn result_element_type_fn,
59     SmallVectorImpl<ShapedTypeComponents>& inferred_return_shapes);
60 
61 // Runs TensorFlow shape inference for an operation for a given Graph version.
62 // If an operation implements the `InferTypeOpInterface` or
63 // `InferShapedTypeOpInterface` interfaces, those are used instead but with
64 // derived attributes populated. Otherwise the above function is used but with
65 // default `operand_as_constant_fn` and `op_result_as_shape_fn` that only
66 // extracts a value if the operands are constant (no partial evaluation, and an
67 // empty `result_element_type_fn`. Element types with subtypes (DT_RESOURCE,
68 // DT_VARIANT) are not supported.
69 LogicalResult InferReturnTypeComponentsForTFOp(
70     Optional<Location> location, Operation* op, int64_t graph_version,
71     SmallVectorImpl<ShapedTypeComponents>& inferred_return_shapes);
72 
73 }  // namespace TF
74 }  // namespace mlir
75 
76 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_UTILS_SHAPE_INFERENCE_UTILS_H_
77