Elffuss BraidHub

kikocis/ncnn public

master · 6.8 KiBRawPermalink
1/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16// This file defines the op traits used in the MLIR TensorFlow dialect.
17
18#ifndef TENSORFLOW_COMPILER_MLIR_TENSORFLOW_IR_TF_TRAITS_H_
19#define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_IR_TF_TRAITS_H_
20
21#include "mlir/IR/BuiltinTypes.h" // from @llvm-project
22#include "mlir/IR/OpDefinition.h" // from @llvm-project
23#include "mlir/IR/TypeUtilities.h" // from @llvm-project
24#include "mlir/Interfaces/SideEffectInterfaces.h" // from @llvm-project
25#include "mlir/Support/LogicalResult.h" // from @llvm-project
26#include "tf_types.h"
27
28namespace mlir {
29namespace OpTrait {
30namespace TF {
31
32// Verifies if 'ref_type' is a REF type corresponding to 'type'.
33static inline LogicalResult VerifyRefTypeMatch(mlir::Type type,
34 mlir::Type maybe_ref_type)
35{
36 if (auto ref_type = maybe_ref_type.dyn_cast<mlir::TF::TensorFlowRefType>())
37 return success(ref_type.RemoveRef().getTypeID() == type.getTypeID());
38 return failure();
39}
40
41// This class provides verification for ops that are known to have the same
42// result types and all operands are either of the same type as result or a REF
43// type corresponding to the result type.
44// TODO(jpienaar): Update the name and the description.
45template<typename ConcreteType>
46class OperandsSameAsResultsTypeOrRef
47 : public TraitBase<ConcreteType, OperandsSameAsResultsTypeOrRef>
48{
49public:
50 static LogicalResult verifyTrait(Operation* op)
51 {
52 LogicalResult shapeMatch = impl::verifySameOperandsAndResultShape(op);
53 if (failed(shapeMatch)) return shapeMatch;
54 Type type = op->getResult(0).getType();
55 // Verify that the first result type is same as the rest of the results.
56 // We skip the comparison against itself.
57 for (auto result_type : llvm::drop_begin(op->getResultTypes(), 1))
58 {
59 if (!mlir::TF::HasCompatibleElementTypes(type, result_type))
60 return op->emitOpError()
61 << "requires all return types to have compatible element types";
62 }
63 for (auto operand_type : op->getOperandTypes())
64 {
65 if (!mlir::TF::HasCompatibleElementTypes(
66 operand_type, type, /*may_ignore_ref_type_lhs=*/true))
67 return op->emitError() << "requires all operands and results to have "
68 "compatible element types";
69 }
70 return success();
71 }
72};
73
74namespace detail {
75inline LogicalResult verifySameOperandsAndResultElementTypeResolveRef(
76 Operation* op)
77{
78 Type element_type;
79 if (op->getNumResults() > 0)
80 {
81 element_type = mlir::TF::GetElementTypeOrSelfResolveRef(op->getResult(0).getType());
82 }
83 else if (op->getNumOperands() > 0)
84 {
85 element_type = mlir::TF::GetElementTypeOrSelfResolveRef(op->getOperand(0).getType());
86 }
87 else
88 {
89 // Nothing to check.
90 return success();
91 }
92 // Verify that all result element types are compatible to `element_type`.
93 for (const auto& result_type : op->getResultTypes())
94 {
95 if (mlir::TF::GetElementTypeOrSelfResolveRef(result_type) != element_type)
96 {
97 return op->emitOpError(
98 "requires compatible element types for all operands and results");
99 }
100 }
101 // Verify that all operand element types are compatible to `element_type`.
102 for (const auto& operand_type : op->getOperandTypes())
103 {
104 if (mlir::TF::GetElementTypeOrSelfResolveRef(operand_type) != element_type)
105 {
106 return op->emitOpError(
107 "requires compatible element types for all operands and results");
108 }
109 }
110 return success();
111}
112} // namespace detail
113
114// Verifies that op has the same operand and result element types (or type
115// itself, if scalar) after resolving reference types (i.e., after converting
116// reference types to their corresponding TensorFlow or standard types).
117template<typename ConcreteType>
118class SameOperandsAndResultElementTypeResolveRef
119 : public TraitBase<ConcreteType,
120 SameOperandsAndResultElementTypeResolveRef>
121{
122public:
123 static LogicalResult verifyTrait(Operation* op)
124 {
125 return detail::verifySameOperandsAndResultElementTypeResolveRef(op);
126 }
127};
128
129// Verifies that op has the same operand and result types after resolving
130// reference types (i.e., after converting reference types to their
131// corresponding TensorFlow or standard types).
132template<typename ConcreteType>
133class SameOperandsAndResultTypeResolveRef
134 : public TraitBase<ConcreteType, SameOperandsAndResultTypeResolveRef>
135{
136public:
137 static LogicalResult verifyTrait(Operation* op)
138 {
139 if (failed(impl::verifySameOperandsAndResultShape(op))) return failure();
140 return detail::verifySameOperandsAndResultElementTypeResolveRef(op);
141 }
142};
143
144// Layout agnostic operations do not depend on the operands data layout (data
145// format), as and example all element wise operations are layout agnostic.
146template<typename ConcreteType>
147class LayoutAgnostic : public TraitBase<ConcreteType, LayoutAgnostic>
148{
149};
150
151// Trait to indicate operations that cannot be duplicated as they might carry
152// certain state around within their implementations.
153template<typename ConcreteType>
154class CannotDuplicate : public TraitBase<ConcreteType, CannotDuplicate>
155{
156public:
157 static LogicalResult verifyTrait(Operation* op)
158 {
159 if (MemoryEffectOpInterface::hasNoEffect(op))
160 return op->emitError(
161 "operations with no side effects cannot have CannotDuplicate trait");
162 return success();
163 }
164};
165
166// Trait to indicate an operation cannot be constant folded.
167template<typename ConcreteType>
168class NoConstantFold : public TraitBase<ConcreteType, NoConstantFold>
169{
170};
171
172// Coefficient-wise binary operation with implicit broadcasting support, for
173// example tf.Sub operation.
174template<typename ConcreteType>
175class CwiseBinary : public TraitBase<ConcreteType, CwiseBinary>
176{
177};
178
179// Coefficient-wise unary operation, for example tf.Sqrt operation.
180template<typename ConcreteType>
181class CwiseUnary : public TraitBase<ConcreteType, CwiseUnary>
182{
183};
184
185} // namespace TF
186} // namespace OpTrait
187} // namespace mlir
188
189#endif // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_IR_TF_TRAITS_H_