• 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/IR/Ops.h"  // from @llvm-project
23 #include "mlir/IR/BuiltinTypes.h"  // from @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 TensorFlow op does not have a `data_format`
39 // attribute (then default to "NDHWC"), or its `data_format` attribute is
40 // "NDHWC".
TFDataFormatIsNDHWC(Operation * op)41 inline bool TFDataFormatIsNDHWC(Operation *op) {
42   auto attr = op->getAttrOfType<StringAttr>("data_format");
43   return !attr || attr.getValue() == "NDHWC";
44 }
45 
46 // Returns true if the given `op`
47 //   * has an attribute with the given `name`,
48 //   * and the attribute is an integer list of the form [1, X, Y, 1],
49 // and writes X, Y as 32-bit integer attribute to `x`, `y`.
50 bool TFIntListIs1XY1(Operation *op, StringRef name, IntegerAttr *x,
51                      IntegerAttr *y);
52 
53 // Returns true if the attribute is an integer list of the form [1, X, Y, 1],
54 bool TFIntListIs1XY1(const ArrayAttr &attr);
55 
56 // Returns true if the given `op`
57 //   * has an attribute with the given `name`,
58 //   * and the attribute is an integer list of the form [1, X, Y, Z, 1],
59 // and writes X, Y as 32-bit integer attribute to `x`, `y`, z.
60 bool TFIntListIs1XYZ1(Operation *op, StringRef name, IntegerAttr *x,
61                       IntegerAttr *y, IntegerAttr *z);
62 
63 // Returns true if every element of the attribute is 1. All elements of `attr`
64 // must be `IntegerAttr`.
65 bool TFIntListIsAllOnes(const ArrayAttr &attr);
66 
67 // Returns true iff the given value is a float32 tensor.
68 // is "DT_FLOAT".
TFTypeIsFloat32Tensor(Value value)69 inline bool TFTypeIsFloat32Tensor(Value value) {
70   auto tensorType = value.getType().dyn_cast<TensorType>();
71   if (!tensorType) return false;
72   return tensorType.getElementType().isF32();
73 }
74 
75 // Returns true iff the given value is a bf16 tensor.
TFTypeIsBFloat16Tensor(Value value)76 inline bool TFTypeIsBFloat16Tensor(Value value) {
77   auto tensorType = value.getType().dyn_cast<TensorType>();
78   if (!tensorType) return false;
79   return tensorType.getElementType().isBF16();
80 }
81 
82 // Returns true iff the given value is a f16 tensor.
TFTypeIsHalfTensor(Value value)83 inline bool TFTypeIsHalfTensor(Value value) {
84   auto tensorType = value.getType().dyn_cast<TensorType>();
85   if (!tensorType) return false;
86   return tensorType.getElementType().isF16();
87 }
88 
89 // Returns true iff the given value is a f16 or bf16 tensor.
TFTypeIsBFloat16OrHalfTensor(Value value)90 inline bool TFTypeIsBFloat16OrHalfTensor(Value value) {
91   return TFTypeIsBFloat16Tensor(value) || TFTypeIsHalfTensor(value);
92 }
93 
94 // Returns true iff the given TensorFlow op has a `padding` attribute whose
95 // value is "SAME" or "VALID", and writes the attribute to `padding`.
TFPaddingIsSameOrValid(Operation * op,StringAttr * padding)96 inline bool TFPaddingIsSameOrValid(Operation *op, StringAttr *padding) {
97   auto padding_attr = op->getAttrOfType<StringAttr>("padding");
98   if (padding_attr.getValue() != "SAME" && padding_attr.getValue() != "VALID")
99     return false;
100   *padding = padding_attr;
101   return true;
102 }
103 
104 /// Returns whether the given `a` and `b` have broadcast-compatible
105 /// types.
106 bool IsBroadcastableElementsAttrs(mlir::Attribute a, mlir::Attribute b);
107 // Returns true if every dimension of the attribute is 1 except the last one.
108 bool IsDimensionsDegenerateExceptLastOne(mlir::Attribute val);
109 // Returns true if every element is 1 except the last one.
110 bool IsDimensionsDegenerateExceptLastOne(ArrayRef<int64_t> elements_shape);
111 
112 }  // end namespace TFL
113 }  // end namespace mlir
114 
115 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_UTILS_VALIDATORS_H_
116