MLIR 23.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 <unordered_map>
13
16
17#include "mlir/Support/TypeID.h"
18
19using namespace mlir;
20using namespace mlir::tosa;
21
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 std::pair<SmallVector<TypeInfo>, SpecificationVersion>;
53
54template <typename T>
56 // Certain operations require multiple modes enabled.
57 // e.g. cast bf16 to fp8e4m3 requires EXT-BF16 and EXT-FP8E4M3.
61};
62
64 std::unordered_map<std::string, SmallVector<OpComplianceInfo<Profile>>>;
66 std::unordered_map<std::string, SmallVector<OpComplianceInfo<Extension>>>;
67
68//===----------------------------------------------------------------------===//
69// Tosa Profile And Extension Information Depot
70//===----------------------------------------------------------------------===//
71
73public:
75 if (failed(populatationDispatch(op)))
76 op->emitOpError() << "fail to populate the profile info\n";
77 }
78
79 void addType(Type t) { tyInfo.push_back(convertTypeToInfo(t)); }
80 void addValue(Value v) { tyInfo.push_back(convertValueToInfo(v)); }
81 SmallVector<TypeInfo> getInfo() { return tyInfo; }
82
83private:
84 TypeInfo convertTypeToInfo(Type type) {
85 if (auto blockScaledTy = dyn_cast<tosa::BlockScaledType>(type)) {
86 Type valueTy = blockScaledTy.getValueType();
87 Type scaleTy = blockScaledTy.getScaleType();
88 return {type.getTypeID(), tosa::getBitWidth(valueTy), valueTy.getTypeID(),
89 scaleTy.getTypeID(), blockScaledTy.getBlockShape()};
90 }
91 return {type.getTypeID(), tosa::getBitWidth(type)};
92 }
93
94 TypeInfo convertValueToInfo(Value value) {
95 return convertTypeToInfo(getElementTypeOrSelf(value.getType()));
96 }
97
98 LogicalResult populatationDispatch(Operation *op);
99
100 // Add input operands and output results to the profile type info list
101 LogicalResult populateProfileInfo(ValueRange operands, ValueRange results);
102
103 // Base
104 template <typename T>
105 LogicalResult populateProfileInfo(T op) {
106 return op->emitOpError()
107 << "profile requirement for this op has not been defined";
108 }
109 // For conv2d, conv3d, transpose_conv2d, and depthwise_conv2d.
110 template <typename T>
111 LogicalResult populateProfileInfoConv(T op);
112
113 // For reshape, slice, tile, and transpose.
114 template <typename T>
115 LogicalResult populateProfileInfoDataLayout(T op);
116
117private:
118 SmallVector<TypeInfo> tyInfo;
119};
120
121//===----------------------------------------------------------------------===//
122// Tosa Profile And Extension Compliance Checker
123//===----------------------------------------------------------------------===//
124
126public:
127 explicit TosaProfileCompliance();
128
129 // Accessor of the compliance info map.
130 template <typename T>
131 std::unordered_map<std::string, SmallVector<OpComplianceInfo<T>>>
133 // Only profile and extension compliance info are provided.
134 return {};
135 }
136
137 // Verify if the operation is allowed to be executed in the given target
138 // environment.
139 LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv);
140 LogicalResult checkExtension(Operation *op, const tosa::TargetEnv &targetEnv);
141 LogicalResult checkInvalid(Operation *op);
142
143 template <typename T>
144 LogicalResult checkProfileOrExtension(
145 Operation *op, const tosa::TargetEnv &targetEnv,
146 const SmallVector<ArrayRef<T>> &specDefinedProfileSet);
147
149 return a.typeID == b.typeID && a.bitWidth == b.bitWidth &&
150 a.valueTypeID == b.valueTypeID && a.scaleTypeID == b.scaleTypeID &&
151 a.blockShape == b.blockShape;
152 }
153
154 // Find the required profiles or extensions from the compliance info according
155 // to the operand type combination.
156 template <typename T>
159
160 // Debug utilites.
161 template <typename T>
163
164 template <typename T>
166 stringifyProfile(const SmallVector<ArrayRef<T>> &profileSet);
167
168 static llvm::SmallString<32> stringifyTypeInfo(const TypeInfo &typeInfo);
169
170private:
171 template <typename T>
172 FailureOr<SmallVector<OpComplianceInfo<T>>>
173 getOperatorMatchedEntries(Operation *op);
174
175 OperationProfileComplianceMap profileComplianceMap;
176 OperationExtensionComplianceMap extensionComplianceMap;
177};
178
179#endif // MLIR_DIALECT_TOSA_TRANSFORMS_TOSAPROFILECOMPILANCE_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Profile > > > OperationProfileComplianceMap
std::pair< SmallVector< TypeInfo >, SpecificationVersion > VersionedTypeInfo
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Extension > > > OperationExtensionComplianceMap
ProfileInfoDepot(Operation *op)
SmallVector< TypeInfo > getInfo()
bool isSameTypeInfo(TypeInfo a, TypeInfo b)
LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv)
static llvm::SmallString< 32 > stringifyTypeInfo(const TypeInfo &typeInfo)
LogicalResult checkExtension(Operation *op, const tosa::TargetEnv &targetEnv)
LogicalResult checkInvalid(Operation *op)
SmallVector< StringRef > stringifyProfile(ArrayRef< T > profiles)
LogicalResult checkProfileOrExtension(Operation *op, const tosa::TargetEnv &targetEnv, const SmallVector< ArrayRef< T > > &specDefinedProfileSet)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< T > > > getProfileComplianceMap()
SmallVector< OpComplianceInfo< T > > findMatchedEntries(Operation *op, SmallVector< OpComplianceInfo< T > > compInfo)
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
Type getType() const
Return the type of this value.
Definition Value.h:105
This class represents the capability enabled in the target implementation such as profile,...
Definition TargetEnv.h:119
unsigned getBitWidth(Type type)
Definition TosaOps.cpp:631
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
SmallVector< VersionedTypeInfo > operandTypeInfoSet
SmallVector< T > mode
mlir::TypeID valueTypeID
TypeInfo(mlir::TypeID typeID, uint32_t bitWidth)
TypeInfo(mlir::TypeID typeID, uint32_t bitWidth, mlir::TypeID valueTypeID, mlir::TypeID scaleTypeID, tosa::BlockShape blockShape)
mlir::TypeID scaleTypeID
std::optional< tosa::BlockShape > blockShape
mlir::TypeID typeID