MLIR 24.0.0git
TosaProfileCompliance.h
Go to the documentation of this file.
1//===- TosaProfileCompliance.h - Tosa Profile-based Compliance Validation -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_DIALECT_TOSA_TRANSFORMS_TOSAPROFILECOMPILANCE_H
10#define MLIR_DIALECT_TOSA_TRANSFORMS_TOSAPROFILECOMPILANCE_H
11
12#include <initializer_list>
13#include <unordered_map>
14
17
18#include "mlir/Support/TypeID.h"
19
20namespace mlir {
21namespace tosa {
22//===----------------------------------------------------------------------===//
23// Type Compilance Definition
24//===----------------------------------------------------------------------===//
25
42
45 // Valid when any of the profile (extension) requirement is meet.
47 // Valid when all of the profile (extension) requirement are meet.
49};
50
52 const TypeInfo *ptr;
53 TypeInfoRef(const TypeInfo &info) : ptr(&info) {}
54};
55
58 SpecificationVersion second;
59
60 VersionedTypeInfo() = default;
61 VersionedTypeInfo(std::initializer_list<TypeInfoRef> types,
62 SpecificationVersion version);
63 VersionedTypeInfo(SmallVector<TypeInfo> types, SpecificationVersion version);
69};
70
71template <typename T>
92
93extern template struct OpComplianceInfo<Profile>;
94extern template struct OpComplianceInfo<Extension>;
95
97 std::unordered_map<std::string, SmallVector<OpComplianceInfo<Profile>>>;
99 std::unordered_map<std::string, SmallVector<OpComplianceInfo<Extension>>>;
100
101//===----------------------------------------------------------------------===//
102// Tosa Profile And Extension Information Depot
103//===----------------------------------------------------------------------===//
104
106public:
108 if (failed(populatationDispatch(op)))
109 op->emitOpError() << "fail to populate the profile info\n";
110 }
111
112 void addType(Type t) { tyInfo.push_back(convertTypeToInfo(t)); }
113 void addValue(Value v) { tyInfo.push_back(convertValueToInfo(v)); }
114 ArrayRef<TypeInfo> getInfo() const { return tyInfo; }
115
116private:
117 TypeInfo convertTypeToInfo(Type type) {
118 // Treat the deprecated TOSA type as builtin mxi8 for compliance checks.
119 const auto getTypeID = [](Type type) {
120 if (isa<tosa::mxint8Type>(type))
121 return MXI8Type::getTypeID();
122 return type.getTypeID();
123 };
124 if (auto blockScaledTy = dyn_cast<tosa::BlockScaledType>(type)) {
125 Type valueTy = blockScaledTy.getValueType();
126 Type scaleTy = blockScaledTy.getScaleType();
127 return {type.getTypeID(), tosa::getBitWidth(valueTy), getTypeID(valueTy),
128 scaleTy.getTypeID(), blockScaledTy.getBlockShape()};
129 }
130 return {getTypeID(type), tosa::getBitWidth(type)};
131 }
132
133 TypeInfo convertValueToInfo(Value value) {
134 return convertTypeToInfo(getElementTypeOrSelf(value.getType()));
135 }
136
137 LogicalResult populatationDispatch(Operation *op);
138
139 // Add input operands and output results to the profile type info list
140 LogicalResult populateProfileInfo(ValueRange operands, ValueRange results);
141
142 // Base
143 template <typename T>
144 LogicalResult populateProfileInfo(T op) {
145 return op->emitOpError()
146 << "profile requirement for this op has not been defined";
147 }
148 // For conv2d, conv3d, transpose_conv2d, and depthwise_conv2d.
149 template <typename T>
150 LogicalResult populateProfileInfoConv(T op);
151
152 // For reshape, slice, tile, and transpose.
153 template <typename T>
154 LogicalResult populateProfileInfoDataLayout(T op);
155
156private:
157 SmallVector<TypeInfo> tyInfo;
158};
159
160//===----------------------------------------------------------------------===//
161// Tosa Profile And Extension Compliance Checker
162//===----------------------------------------------------------------------===//
163
165public:
166 explicit TosaProfileCompliance();
167
168 // Accessor of the compliance info map.
169 template <typename T>
170 const std::unordered_map<std::string, SmallVector<OpComplianceInfo<T>>> &
172
173 // Verify if the operation is allowed to be executed in the given target
174 // environment.
175 LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv);
176 LogicalResult checkExtension(Operation *op, const tosa::TargetEnv &targetEnv);
177 LogicalResult checkInvalid(Operation *op);
178
179 template <typename T>
180 LogicalResult checkProfileOrExtension(Operation *op,
181 const tosa::TargetEnv &targetEnv);
182
183 bool isSameTypeInfo(const TypeInfo &a, const TypeInfo &b) {
184 return a.typeID == b.typeID && a.bitWidth == b.bitWidth &&
185 a.valueTypeID == b.valueTypeID && a.scaleTypeID == b.scaleTypeID &&
186 a.blockShape == b.blockShape;
187 }
188
189 // Find the required profiles or extensions from the compliance info according
190 // to the operand type combination.
191 template <typename T>
194
195 // Debug utilites.
196 template <typename T>
198
199 template <typename T>
201 stringifyProfile(const SmallVector<ArrayRef<T>> &profileSet);
202
203 static llvm::SmallString<32> stringifyTypeInfo(const TypeInfo &typeInfo);
204
205private:
206 template <typename T>
207 FailureOr<SmallVector<OpComplianceInfo<T>>>
208 getOperatorMatchedEntries(Operation *op);
209
210 OperationProfileComplianceMap profileComplianceMap;
211 OperationExtensionComplianceMap extensionComplianceMap;
212};
213
214template <>
217
218template <>
221} // namespace tosa
222} // namespace mlir
223
224#endif // MLIR_DIALECT_TOSA_TRANSFORMS_TOSAPROFILECOMPILANCE_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
TypeID getTypeID()
Return a unique identifier for the concrete type.
Definition Types.h:101
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
ArrayRef< TypeInfo > getInfo() const
This class represents the capability enabled in the target implementation such as profile,...
Definition TargetEnv.h:119
static llvm::SmallString< 32 > stringifyTypeInfo(const TypeInfo &typeInfo)
LogicalResult checkExtension(Operation *op, const tosa::TargetEnv &targetEnv)
LogicalResult checkInvalid(Operation *op)
SmallVector< OpComplianceInfo< T > > findMatchedEntries(Operation *op, ArrayRef< OpComplianceInfo< T > > compInfo)
LogicalResult checkProfileOrExtension(Operation *op, const tosa::TargetEnv &targetEnv)
LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv)
const std::unordered_map< std::string, SmallVector< OpComplianceInfo< T > > > & getProfileComplianceMap() const
SmallVector< StringRef > stringifyProfile(ArrayRef< T > profiles)
bool isSameTypeInfo(const TypeInfo &a, const TypeInfo &b)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Extension > > > OperationExtensionComplianceMap
unsigned getBitWidth(Type type)
Definition TosaOps.cpp:327
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Profile > > > OperationProfileComplianceMap
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
OpComplianceInfo & operator=(OpComplianceInfo &&other)
OpComplianceInfo(const OpComplianceInfo &other)
SmallVector< VersionedTypeInfo > operandTypeInfoSet
OpComplianceInfo(OpComplianceInfo &&other)
OpComplianceInfo & operator=(const OpComplianceInfo &other)
TypeInfoRef(const TypeInfo &info)
TypeInfo(mlir::TypeID typeID, uint32_t bitWidth)
std::optional< tosa::BlockShape > blockShape
TypeInfo(mlir::TypeID typeID, uint32_t bitWidth, mlir::TypeID valueTypeID, mlir::TypeID scaleTypeID, tosa::BlockShape blockShape)
VersionedTypeInfo(VersionedTypeInfo &&other)
VersionedTypeInfo(const VersionedTypeInfo &other)
VersionedTypeInfo & operator=(const VersionedTypeInfo &other)
VersionedTypeInfo & operator=(VersionedTypeInfo &&other)