• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This header file defines common validators used by TFLite transformation
17 // passes to validate op attributes or values.
18 
19 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_UTILS_VALIDATORS_H_
20 #define TENSORFLOW_COMPILER_MLIR_LITE_UTILS_VALIDATORS_H_
21 
22 #include "mlir/Dialect/StandardOps/Ops.h"  // TF:llvm-project
23 #include "mlir/IR/StandardTypes.h"  // TF:llvm-project
24 
25 namespace mlir {
26 namespace TFL {
27 
28 // TODO(jpienaar): Change these to being one of these variants and/or generate
29 // these predicates.
30 
31 // Returns true if the given TensorFlow op does not have a `data_format`
32 // attribute (then default to "NHWC"), or its `data_format` attribute is "NHWC".
TFDataFormatIsNHWC(Operation * op)33 inline bool TFDataFormatIsNHWC(Operation *op) {
34   auto attr = op->getAttrOfType<StringAttr>("data_format");
35   return !attr || attr.getValue() == "NHWC";
36 }
37 
38 // Returns true if the given `op`
39 //   * has an attribute with the given `name`,
40 //   * and the attribute is an integer list of the form [1, X, Y, 1],
41 // and writes X, Y as 32-bit integer attribute to `x`, `y`.
42 bool TFIntListIs1XY1(Operation *op, StringRef name, IntegerAttr *x,
43                      IntegerAttr *y);
44 
45 // Returns true if the attribute is an integer list of the form [1, X, Y, 1],
46 bool TFIntListIs1XY1(const ArrayAttr &attr);
47 
48 // Returns true if every element of the attribute is 1. All elements of `attr`
49 // must be `IntegerAttr`.
50 bool TFIntListIsAllOnes(const ArrayAttr &attr);
51 
52 // Returns true iff the given value is a float tensor.
53 // is "DT_FLOAT".
TFTypeIsFloatTensor(Value value)54 inline bool TFTypeIsFloatTensor(Value value) {
55   auto tensorType = value.getType().dyn_cast<TensorType>();
56   if (!tensorType) return false;
57   return tensorType.getElementType().isa<FloatType>();
58 }
59 
60 // Returns true iff the given TensorFlow op has a `padding` attribute whose
61 // value is "SAME" or "VALID", and writes the attribute to `padding`.
TFPaddingIsSameOrValid(Operation * op,StringAttr * padding)62 inline bool TFPaddingIsSameOrValid(Operation *op, StringAttr *padding) {
63   auto padding_attr = op->getAttrOfType<StringAttr>("padding");
64   if (padding_attr.getValue() != "SAME" && padding_attr.getValue() != "VALID")
65     return false;
66   *padding = padding_attr;
67   return true;
68 }
69 
70 /// Returns whether the given `a` and `b` have broadcast-compatible
71 /// types.
72 bool IsBroadcastableElementsAttrs(mlir::Attribute a, mlir::Attribute b);
73 
74 }  // end namespace TFL
75 }  // end namespace mlir
76 
77 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_UTILS_VALIDATORS_H_
78