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 SmallVector<TypeInfo> getInfo() { return tyInfo; }
115
116private:
117 TypeInfo convertTypeToInfo(Type type) {
118 if (auto blockScaledTy = dyn_cast<tosa::BlockScaledType>(type)) {
119 Type valueTy = blockScaledTy.getValueType();
120 Type scaleTy = blockScaledTy.getScaleType();
121 return {type.getTypeID(), tosa::getBitWidth(valueTy), valueTy.getTypeID(),
122 scaleTy.getTypeID(), blockScaledTy.getBlockShape()};
123 }
124 return {type.getTypeID(), tosa::getBitWidth(type)};
125 }
126
127 TypeInfo convertValueToInfo(Value value) {
128 return convertTypeToInfo(getElementTypeOrSelf(value.getType()));
129 }
130
131 LogicalResult populatationDispatch(Operation *op);
132
133 // Add input operands and output results to the profile type info list
134 LogicalResult populateProfileInfo(ValueRange operands, ValueRange results);
135
136 // Base
137 template <typename T>
138 LogicalResult populateProfileInfo(T op) {
139 return op->emitOpError()
140 << "profile requirement for this op has not been defined";
141 }
142 // For conv2d, conv3d, transpose_conv2d, and depthwise_conv2d.
143 template <typename T>
144 LogicalResult populateProfileInfoConv(T op);
145
146 // For reshape, slice, tile, and transpose.
147 template <typename T>
148 LogicalResult populateProfileInfoDataLayout(T op);
149
150private:
151 SmallVector<TypeInfo> tyInfo;
152};
153
154//===----------------------------------------------------------------------===//
155// Tosa Profile And Extension Compliance Checker
156//===----------------------------------------------------------------------===//
157
159public:
160 explicit TosaProfileCompliance();
161
162 // Accessor of the compliance info map.
163 template <typename T>
164 std::unordered_map<std::string, SmallVector<OpComplianceInfo<T>>>
166 // Only profile and extension compliance info are provided.
167 return {};
168 }
169
170 // Verify if the operation is allowed to be executed in the given target
171 // environment.
172 LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv);
173 LogicalResult checkExtension(Operation *op, const tosa::TargetEnv &targetEnv);
174 LogicalResult checkInvalid(Operation *op);
175
176 template <typename T>
177 LogicalResult checkProfileOrExtension(Operation *op,
178 const tosa::TargetEnv &targetEnv);
179
181 return a.typeID == b.typeID && a.bitWidth == b.bitWidth &&
182 a.valueTypeID == b.valueTypeID && a.scaleTypeID == b.scaleTypeID &&
183 a.blockShape == b.blockShape;
184 }
185
186 // Find the required profiles or extensions from the compliance info according
187 // to the operand type combination.
188 template <typename T>
191
192 // Debug utilites.
193 template <typename T>
195
196 template <typename T>
198 stringifyProfile(const SmallVector<ArrayRef<T>> &profileSet);
199
200 static llvm::SmallString<32> stringifyTypeInfo(const TypeInfo &typeInfo);
201
202private:
203 template <typename T>
204 FailureOr<SmallVector<OpComplianceInfo<T>>>
205 getOperatorMatchedEntries(Operation *op);
206
207 OperationProfileComplianceMap profileComplianceMap;
208 OperationExtensionComplianceMap extensionComplianceMap;
209};
210} // namespace tosa
211} // namespace mlir
212
213#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
SmallVector< TypeInfo > getInfo()
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)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< T > > > getProfileComplianceMap()
SmallVector< OpComplianceInfo< T > > findMatchedEntries(Operation *op, SmallVector< OpComplianceInfo< T > > compInfo)
LogicalResult checkProfileOrExtension(Operation *op, const tosa::TargetEnv &targetEnv)
LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv)
SmallVector< StringRef > stringifyProfile(ArrayRef< T > profiles)
bool isSameTypeInfo(TypeInfo a, TypeInfo b)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Extension > > > OperationExtensionComplianceMap
unsigned getBitWidth(Type type)
Definition TosaOps.cpp:331
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)