MLIR 22.0.0git
TosaProfileCompliance.cpp
Go to the documentation of this file.
1//===--- TosaProfileCompliance.cpp - Tosa Profile 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
10#include "llvm/ADT/StringExtras.h"
11
12using namespace mlir;
13using namespace mlir::tosa;
14
16 const TypeInfo boolT = {mlir::IntegerType::getTypeID(), 1};
17 const TypeInfo i4T = {mlir::IntegerType::getTypeID(), 4};
18 const TypeInfo i8T = {mlir::IntegerType::getTypeID(), 8};
19 const TypeInfo i16T = {mlir::IntegerType::getTypeID(), 16};
20 const TypeInfo i32T = {mlir::IntegerType::getTypeID(), 32};
21 const TypeInfo i48T = {mlir::IntegerType::getTypeID(), 48};
22 const TypeInfo i64T = {mlir::IntegerType::getTypeID(), 64};
23 const TypeInfo bf16T = {mlir::BFloat16Type::getTypeID(), 16};
24 const TypeInfo fp16T = {mlir::Float16Type::getTypeID(), 16};
25 const TypeInfo fp32T = {mlir::Float32Type::getTypeID(), 32};
26 const TypeInfo fp8e4m3T = {mlir::Float8E4M3FNType::getTypeID(), 8};
27 const TypeInfo fp8e5m2T = {mlir::Float8E5M2Type::getTypeID(), 8};
28
29 // micro-scaling formats
30 const TypeInfo fp6e2m3T = {mlir::Float6E2M3FNType::getTypeID(), 6};
31 const TypeInfo fp6e3m2T = {mlir::Float6E3M2FNType::getTypeID(), 6};
32 const TypeInfo fp4e2m1T = {mlir::Float4E2M1FNType::getTypeID(), 4};
33 const TypeInfo fp8ue8m0T = {mlir::Float8E8M0FNUType::getTypeID(), 8};
34 const TypeInfo mxint8T = {mlir::tosa::mxint8Type::getTypeID(), 8};
35
36// The profile-based compliance content below is auto-generated by a script
37// in https://git.mlplatform.org/tosa/specification.git
39 // End of auto-generated metadata
40}
41
42template <>
46
47template <>
50 return extensionComplianceMap;
51}
52
53// Base populating function
54LogicalResult ProfileInfoDepot::populateProfileInfo(ValueRange operands,
55 ValueRange results) {
56 for (const auto &operand : operands)
57 addValue(operand);
58 for (const auto &result : results)
60 return success();
61}
62
63template <>
64LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ConcatOp op) {
65 addValue(op.getInput1().front());
66 addValue(op.getOutput());
67 return success();
68}
69
70template <>
71LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::AvgPool2dOp op) {
72 addValue(op.getInput());
73 addValue(op.getInputZp());
74 addValue(op.getOutputZp());
75 addType(op.getAccType());
76 addValue(op.getOutput());
77 return success();
78}
79
80template <typename T>
81LogicalResult ProfileInfoDepot::populateProfileInfoConv(T op) {
82 addValue(op.getInput());
83 addValue(op.getWeight());
84 addValue(op.getBias());
85 addValue(op.getInputZp());
86 addValue(op.getWeightZp());
87 addType(op.getAccType());
88 addValue(op.getOutput());
89 return success();
90}
91
92template <>
93LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::Conv2DOp op) {
94 return populateProfileInfoConv(op);
95}
96
97template <>
98LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::Conv3DOp op) {
99 return populateProfileInfoConv(op);
100}
101
102template <>
103LogicalResult
104ProfileInfoDepot::populateProfileInfo(tosa::TransposeConv2DOp op) {
105 return populateProfileInfoConv(op);
106}
107
108template <>
109LogicalResult
110ProfileInfoDepot::populateProfileInfo(tosa::DepthwiseConv2DOp op) {
111 return populateProfileInfoConv(op);
112}
113
114template <>
115LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::PadOp op) {
116 addValue(op.getInput1());
117 addValue(op.getPadConst());
118 addValue(op.getOutput());
119 return success();
120}
121
122template <typename T>
123LogicalResult ProfileInfoDepot::populateProfileInfoDataLayout(T op) {
124 addValue(op.getInput1());
125 addValue(op.getOutput());
126 return success();
127}
128
129template <>
130LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ReshapeOp op) {
131 return populateProfileInfoDataLayout(op);
132}
133
134template <>
135LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::SliceOp op) {
136 return populateProfileInfoDataLayout(op);
137}
138
139template <>
140LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::TileOp op) {
141 return populateProfileInfoDataLayout(op);
142}
143
144template <>
145LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::TransposeOp op) {
146 return populateProfileInfoDataLayout(op);
147}
148
149template <>
150LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::GatherOp op) {
151 addValue(op.getValues());
152 addValue(op.getIndices());
153 addValue(op.getOutput());
154 return success();
155}
156
157template <>
158LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ScatterOp op) {
159 addValue(op.getValuesIn());
160 addValue(op.getIndices());
161 addValue(op.getInput());
162 addValue(op.getValuesOut());
163 return success();
164}
165
166template <>
167LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::MulOp op) {
168 addValue(op.getInput1());
169 addValue(op.getInput2());
170 addValue(op.getOutput());
171 return success();
172}
173
174template <>
175LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ResizeOp op) {
176 addValue(op.getInput());
177 addValue(op.getOutput());
178 return success();
179}
180
181template <>
182LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::SelectOp op) {
183 addValue(op.getOnTrue());
184 addValue(op.getOnFalse());
185 addValue(op.getOutput());
186 return success();
187}
188
189template <>
190LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::RescaleOp op) {
191 addValue(op.getInput());
192 addValue(op.getInputZp());
193 addValue(op.getOutputZp());
194 addValue(op.getOutput());
195 return success();
196}
197
198template <>
199LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::MatMulOp op) {
200 addValue(op.getA());
201 addValue(op.getB());
202 addValue(op.getAZp());
203 addValue(op.getBZp());
204 addValue(op.getOutput());
205 return success();
206}
207
208template <>
209LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::VariableOp op) {
210 addType(op.getType());
211 return success();
212}
213
214template <>
215LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::VariableWriteOp op) {
216 addValue(op.getInput1());
217 return success();
218}
219
220LogicalResult ProfileInfoDepot::populatationDispatch(Operation *op) {
221// This helper function only populates the info for the customised operands.
222#define POPULATE_PROFILE_INFO_CUSTOM(tosaOp) \
223 if (isa<tosa::tosaOp##Op>(op)) { \
224 return populateProfileInfo(cast<tosa::tosaOp##Op>(op)); \
225 }
226
227#define POPULATE_PROFILE_INFO_SKIP(tosaOp) \
228 if (isa<tosa::tosaOp##Op>(op)) \
229 return success();
230
231// This helper function populates the info for all operands.
232#define POPULATE_PROFILE_INFO_COMMON(tosaOp) \
233 if (isa<tosa::tosaOp##Op>(op)) { \
234 return populateProfileInfo(op->getOperands(), op->getResults()); \
235 }
236
237 // Skip irrelevant operands when they are independent and not tied to any
238 // specific profile/extension.
240 POPULATE_PROFILE_INFO_CUSTOM(TransposeConv2D)
243 POPULATE_PROFILE_INFO_CUSTOM(DepthwiseConv2D)
258 POPULATE_PROFILE_INFO_CUSTOM(VariableWrite)
259
260 // For the most of tosa operators, all operands are profile/extension related
261 // and hence are all considered in this profile-based compilance check.
262 POPULATE_PROFILE_INFO_COMMON(MatmulTBlockScaled)
266 POPULATE_PROFILE_INFO_COMMON(CastFromBlockScaled)
267 POPULATE_PROFILE_INFO_COMMON(CastToBlockScaled)
279 POPULATE_PROFILE_INFO_COMMON(ArithmeticRightShift)
284 POPULATE_PROFILE_INFO_COMMON(LogicalLeftShift)
285 POPULATE_PROFILE_INFO_COMMON(LogicalRightShift)
308 POPULATE_PROFILE_INFO_COMMON(ReduceProduct)
311 POPULATE_PROFILE_INFO_COMMON(GreaterEqual)
315 POPULATE_PROFILE_INFO_COMMON(VariableRead)
316
317 // Type Invariant Extension, a capability extension that is independent
318 // of the data type, meaning any compatible type can be used. No type
319 // constraint for those operations.
324
325 return failure();
326}
327
328//===----------------------------------------------------------------------===//
329// Tosa Profile And Extension Compliance Checker
330//===----------------------------------------------------------------------===//
331
332template <typename T>
333FailureOr<OpComplianceInfo<T>>
334TosaProfileCompliance::getOperatorDefinition(Operation *op) {
335 const std::string opName = op->getName().getStringRef().str();
336 const auto complianceMap = getProfileComplianceMap<T>();
337 const auto it = complianceMap.find(opName);
338 if (it == complianceMap.end())
339 return {};
340
341 return findMatchedEntry<T>(op, it->second);
342}
343
344template <typename T>
346 Operation *op, const tosa::TargetEnv &targetEnv,
347 const SmallVector<ArrayRef<T>> &specRequiredModeSet) {
348
349 // None of profile requirement is set in the specification.
350 if (specRequiredModeSet.size() == 0)
351 return success();
352
353 const auto maybeOpDefinition = getOperatorDefinition<T>(op);
354 if (failed(maybeOpDefinition)) {
355 // Operators such as control-flow and shape ops do not have an operand type
356 // restriction. When the profile compliance information of operation is not
357 // found, confirm if the target have enabled the profile required from the
358 // specification.
359 int modeCount = 0;
360 for (const auto &cands : specRequiredModeSet) {
361 if (targetEnv.allowsAnyOf(cands))
362 return success();
363 modeCount += cands.size();
364 }
365
366 op->emitOpError() << "illegal: requires"
367 << (modeCount > 1 ? " any of " : " ") << "["
368 << llvm::join(stringifyProfile<T>(specRequiredModeSet),
369 ", ")
370 << "] but not enabled in target\n";
371
372 return failure();
373 }
374
375 // Find the required profiles or extensions according to the operand type
376 // combination.
377 const auto opDefinition = maybeOpDefinition.value();
378 const SmallVector<T> opRequiredMode = opDefinition.mode;
379 const CheckCondition condition = opDefinition.condition;
380
381 if (opRequiredMode.size() == 0) {
382 // No matched restriction found.
383 return success();
384 }
385
386 if (condition == CheckCondition::allOf &&
387 !targetEnv.allowsAllOf(opRequiredMode)) {
388 op->emitOpError() << "illegal: requires"
389 << (opRequiredMode.size() > 1 ? " all of " : " ") << "["
390 << llvm::join(stringifyProfile<T>(opRequiredMode), ", ")
391 << "] but not enabled in target\n";
392 return failure();
393 }
394
395 if (condition == CheckCondition::anyOf &&
396 !targetEnv.allowsAnyOf(opRequiredMode)) {
397 op->emitOpError() << "illegal: requires"
398 << (opRequiredMode.size() > 1 ? " any of " : " ") << "["
399 << llvm::join(stringifyProfile<T>(opRequiredMode), ", ")
400 << "] but not enabled in target\n";
401 return failure();
402 }
403
404 // Each extension can contain a list of profiles that it works with, usually
405 // have the same data type.
406 if constexpr (std::is_same_v<T, Extension>) {
407 for (const auto &mode : opRequiredMode) {
409 if (!targetEnv.allowsAnyOf(coProfs)) {
410 op->emitOpError() << "illegal: requires ["
411 << llvm::join(stringifyProfile<Profile>(coProfs),
412 ", ")
413 << "] to work with but not enabled in target\n";
414 return failure();
415 }
416 }
417 }
418
419 // Ensure the profile inference match the profile knowledge of the
420 // specification.
421 for (const auto &cands : specRequiredModeSet) {
422 for (const auto &mode : opRequiredMode) {
423 if (!llvm::is_contained(cands, mode)) {
424 op->emitOpError() << "illegal: requires ["
425 << llvm::join(stringifyProfile<T>(opRequiredMode),
426 ", ")
427 << "] but not included in the profile compliance ["
428 << llvm::join(
429 stringifyProfile<T>(specRequiredModeSet), ", ")
430 << "]\n";
431 return failure();
432 }
433 }
434 }
435
436 // Ensure the matched op compliance version does not exceed the target
437 // specification version.
438 const VersionedTypeInfo versionedTypeInfo =
439 opDefinition.operandTypeInfoSet[0];
440 const TosaSpecificationVersion complianceVersion{versionedTypeInfo.second};
441 const TosaSpecificationVersion targetVersion{targetEnv.getSpecVersion()};
442 if (!targetVersion.isBackwardsCompatibleWith(complianceVersion)) {
443 op->emitOpError() << "illegal: the target specification version ("
444 << stringifyVersion(targetVersion)
445 << ") is not backwards compatible with the op compliance "
446 "specification version ("
447 << stringifyVersion(complianceVersion) << ")\n";
448 return failure();
449 }
450
451 return success();
452}
453
454LogicalResult
456 const tosa::TargetEnv &targetEnv) {
457 if (auto interface = dyn_cast<tosa::QueryProfileInterface>(op))
458 return checkProfileOrExtension<Profile>(op, targetEnv,
459 interface.getProfiles());
460
461 return success();
462}
463
464LogicalResult
466 const tosa::TargetEnv &targetEnv) {
467 if (auto interface = dyn_cast<tosa::QueryExtensionInterface>(op))
468 return checkProfileOrExtension<Extension>(op, targetEnv,
469 interface.getExtensions());
470
471 return success();
472}
473
475 const auto maybeProfDef = getOperatorDefinition<Profile>(op);
476 const auto maybeExtDef = getOperatorDefinition<Extension>(op);
477 if (failed(maybeProfDef) && failed(maybeExtDef))
478 return success();
479
480 const bool hasEntry =
481 (succeeded(maybeProfDef) && !maybeProfDef->mode.empty()) ||
482 (succeeded(maybeExtDef) && !maybeExtDef->mode.empty());
483 if (!hasEntry) {
484 std::string message;
485 llvm::raw_string_ostream os(message);
486 os << "illegal: operation operand/result data types did not align with any "
487 "profile or extension, got (";
488
489 ProfileInfoDepot depot(op);
490 SmallVector<TypeInfo> current = depot.getInfo();
491 for (const auto &typeInfo : llvm::drop_end(current))
492 os << stringifyTypeInfo(typeInfo) << ",";
493 os << stringifyTypeInfo(current.back()) << ")";
494
495 // avoid polluting the error message output by outputting only
496 // the best match
497 const std::string opName = op->getName().getStringRef().str();
498 int maxMatches = -1;
499 SmallVector<TypeInfo> bestTypeInfo;
500 const auto searchBestMatch = [&](auto map) {
501 for (const auto &complianceInfos : map[opName]) {
502 for (const auto &versionedTypeInfos :
503 complianceInfos.operandTypeInfoSet) {
504 const SmallVector<TypeInfo> typeInfos = versionedTypeInfos.first;
505 const int matches = llvm::count_if(
506 llvm::zip_equal(current, typeInfos), [&](const auto zipType) {
507 return isSameTypeInfo(std::get<0>(zipType),
508 std::get<1>(zipType));
509 });
510 if (matches > maxMatches) {
511 maxMatches = matches;
512 bestTypeInfo = typeInfos;
513 }
514 }
515 }
516 };
517 searchBestMatch(getProfileComplianceMap<Profile>());
518 searchBestMatch(getProfileComplianceMap<Extension>());
519
520 os << ", did you mean (";
521 for (const auto &typeInfo : llvm::drop_end(bestTypeInfo))
522 os << stringifyTypeInfo(typeInfo) << ",";
523 os << stringifyTypeInfo(bestTypeInfo.back()) << ")? ";
524 os << "Otherwise, please refer to the 'supported data types' for '"
525 << opName << "' in the specification.";
526 op->emitOpError(message);
527 return failure();
528 }
529
530 return success();
531}
532
533// Find the profiles or extensions requirement according to the signature of
534// type of the operand list.
535template <typename T>
538 assert(compInfo.size() != 0 &&
539 "profile-based compliance information is empty");
540
541 // Populate the type of profile/extension relevant operands.
542 ProfileInfoDepot depot(op);
543 SmallVector<TypeInfo> present = depot.getInfo();
544 if (present.size() == 0)
545 return {};
546
547 for (size_t i = 0; i < compInfo.size(); i++) {
548 SmallVector<VersionedTypeInfo> sets = compInfo[i].operandTypeInfoSet;
549 for (const auto &set : sets) {
550 SmallVector<TypeInfo> expected = set.first;
551 assert(present.size() == expected.size() &&
552 "the entries for profile-based compliance do not match between "
553 "the generated metadata and the type definition retrieved from "
554 " the operation");
555
556 bool isFound = true;
557 // Compare the type signature between the given operation and the
558 // compliance metadata.
559 for (size_t j = 0; j < expected.size(); j++) {
560 if (!isSameTypeInfo(present[j], expected[j])) {
561 // Verify the next mode set from the list.
562 isFound = false;
563 break;
564 }
565 }
566
567 if (isFound == true) {
568 SmallVector<VersionedTypeInfo> typeInfoSet{set};
569 OpComplianceInfo<T> info{compInfo[i].mode, typeInfoSet,
570 compInfo[i].condition};
571 return info;
572 }
573 }
574 }
575
576 return {};
577}
578
579// Debug utilites.
580template <typename T>
583 SmallVector<StringRef> debugStrings;
584 for (const auto &profile : profiles) {
585 if constexpr (std::is_same_v<T, Profile>)
586 debugStrings.push_back(tosa::stringifyProfile(profile));
587 else
588 debugStrings.push_back(tosa::stringifyExtension(profile));
589 }
590 return debugStrings;
591}
592
593template <typename T>
595 const SmallVector<ArrayRef<T>> &profileSet) {
596 SmallVector<StringRef> debugStrings;
597
598 for (const auto &profiles : profileSet) {
599 auto tempStrings = stringifyProfile<T>(profiles);
600 llvm::append_range(debugStrings, tempStrings);
601 }
602
603 return debugStrings;
604}
605
608 if (typeInfo.typeID == mlir::IntegerType::getTypeID()) {
609 return {"i" + llvm::utostr(typeInfo.bitWidth)};
610 }
611 if (typeInfo.typeID == mlir::Float16Type::getTypeID()) {
612 return {"f16"};
613 } else if (typeInfo.typeID == mlir::Float32Type::getTypeID()) {
614 return {"f32"};
615 } else if (typeInfo.typeID == mlir::BFloat16Type::getTypeID()) {
616 return {"bf16"};
617 } else if (typeInfo.typeID == mlir::Float8E4M3FNType::getTypeID()) {
618 return {"fp8e4m3"};
619 } else if (typeInfo.typeID == mlir::Float8E5M2Type::getTypeID()) {
620 return {"fp8e5m2"};
621 } else if (typeInfo.typeID == mlir::Float6E2M3FNType::getTypeID()) {
622 return {"fp6e2m3"};
623 } else if (typeInfo.typeID == mlir::Float6E3M2FNType::getTypeID()) {
624 return {"fp6e3m2"};
625 } else if (typeInfo.typeID == mlir::Float4E2M1FNType::getTypeID()) {
626 return {"fp4e2m1"};
627 } else if (typeInfo.typeID == mlir::Float8E8M0FNUType::getTypeID()) {
628 return {"fp8e8m0"};
629 } else if (typeInfo.typeID == tosa::mxint8Type::getTypeID()) {
630 return {"mxint8"};
631 }
632 llvm_unreachable("unknown type");
633}
return success()
#define POPULATE_PROFILE_INFO_CUSTOM(tosaOp)
#define POPULATE_PROFILE_INFO_COMMON(tosaOp)
#define POPULATE_PROFILE_INFO_SKIP(tosaOp)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Profile > > > OperationProfileComplianceMap
std::pair< SmallVector< TypeInfo >, SpecificationVersion > VersionedTypeInfo
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Extension > > > OperationExtensionComplianceMap
@ Gather
SmallVector< TypeInfo > getInfo()
bool isSameTypeInfo(TypeInfo a, TypeInfo b)
LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv)
SmallVector< Profile > getCooperativeProfiles(Extension ext)
OpComplianceInfo< T > findMatchedEntry(Operation *op, SmallVector< OpComplianceInfo< T > > compInfo)
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)
static llvm::SmallString< 7 > stringifyTypeInfo(const TypeInfo &typeInfo)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< T > > > getProfileComplianceMap()
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:119
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 abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents the capability enabled in the target implementation such as profile,...
Definition TargetEnv.h:97
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:130
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:126
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:110
A thin wrapper around the SpecificationVersion enum to represent and provide utilities around the TOS...
Definition TargetEnv.h:55
bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const
Definition TargetEnv.h:64
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:15
Include the generated interface declarations.
@ Mul
RHS of mul is always a constant or a symbolic expression.
Definition AffineExpr.h:43
mlir::TypeID typeID
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.