Skip to content

Commit 8a77917

Browse files
author
Mogball
committed
[Printing] Add flag to not print resources.
Often times, large weights for ML models will be stored as resources in MLIR. It is sometimes advantageous to control whether to print these resources for debugging purposes. For example, some models contain very big weights with millions of characters in printed size, which may slow down whatever text editor you are using. This diff adds a flag which allows users to disable printing resources in these scenarios. Reviewed By: Mogball Differential Revision: https://reviews.llvm.org/D157928
1 parent ebfdbdb commit 8a77917

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,9 @@ class OpPrintingFlags {
11391139
/// Return the size limit for printing large ElementsAttr.
11401140
std::optional<int64_t> getLargeElementsAttrLimit() const;
11411141

1142+
/// Return the size limit in chars for printing large resources.
1143+
std::optional<uint64_t> getLargeResourceStringLimit() const;
1144+
11421145
/// Return if debug information should be printed.
11431146
bool shouldPrintDebugInfo() const;
11441147

@@ -1165,6 +1168,9 @@ class OpPrintingFlags {
11651168
/// the upper limit.
11661169
std::optional<int64_t> elementsAttrElementLimit;
11671170

1171+
/// Elide printing large resources based on size of string.
1172+
std::optional<uint64_t> resourceStringCharLimit;
1173+
11681174
/// Print debug information.
11691175
bool printDebugInfoFlag : 1;
11701176
bool printDebugInfoPrettyFormFlag : 1;

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/Support/Regex.h"
4444
#include "llvm/Support/SaveAndRestore.h"
4545
#include "llvm/Support/Threading.h"
46+
#include "llvm/Support/raw_ostream.h"
4647

4748
#include <optional>
4849
#include <tuple>
@@ -140,6 +141,11 @@ struct AsmPrinterOptions {
140141
llvm::cl::desc("Elide ElementsAttrs with \"...\" that have "
141142
"more elements than the given upper limit")};
142143

144+
llvm::cl::opt<unsigned> elideResourceStringsIfLarger{
145+
"mlir-elide-resource-strings-if-larger",
146+
llvm::cl::desc(
147+
"Elide printing value of resources if string is too long in chars.")};
148+
143149
llvm::cl::opt<bool> printDebugInfoOpt{
144150
"mlir-print-debuginfo", llvm::cl::init(false),
145151
llvm::cl::desc("Print debug info in MLIR output")};
@@ -191,6 +197,8 @@ OpPrintingFlags::OpPrintingFlags()
191197
return;
192198
if (clOptions->elideElementsAttrIfLarger.getNumOccurrences())
193199
elementsAttrElementLimit = clOptions->elideElementsAttrIfLarger;
200+
if (clOptions->elideResourceStringsIfLarger.getNumOccurrences())
201+
resourceStringCharLimit = clOptions->elideResourceStringsIfLarger;
194202
printDebugInfoFlag = clOptions->printDebugInfoOpt;
195203
printDebugInfoPrettyFormFlag = clOptions->printPrettyDebugInfoOpt;
196204
printGenericOpFormFlag = clOptions->printGenericOpFormOpt;
@@ -262,6 +270,11 @@ std::optional<int64_t> OpPrintingFlags::getLargeElementsAttrLimit() const {
262270
return elementsAttrElementLimit;
263271
}
264272

273+
/// Return the size limit for printing large ElementsAttr.
274+
std::optional<uint64_t> OpPrintingFlags::getLargeResourceStringLimit() const {
275+
return resourceStringCharLimit;
276+
}
277+
265278
/// Return if debug information should be printed.
266279
bool OpPrintingFlags::shouldPrintDebugInfo() const {
267280
return printDebugInfoFlag;
@@ -3085,11 +3098,11 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
30853098
~ResourceBuilder() override = default;
30863099

30873100
void buildBool(StringRef key, bool data) final {
3088-
printFn(key, [&](raw_ostream &os) { p.os << (data ? "true" : "false"); });
3101+
printFn(key, [&](raw_ostream &os) { os << (data ? "true" : "false"); });
30893102
}
30903103

30913104
void buildString(StringRef key, StringRef data) final {
3092-
printFn(key, [&](raw_ostream &os) { p.printEscapedString(data); });
3105+
printFn(key, [&](raw_ostream &os) { os << "\"" << data << "\""; });
30933106
}
30943107

30953108
void buildBlob(StringRef key, ArrayRef<char> data,
@@ -3176,23 +3189,41 @@ void OperationPrinter::printResourceFileMetadata(
31763189
auto printFn = [&](StringRef key, ResourceBuilder::ValueFn valueFn) {
31773190
checkAddMetadataDict();
31783191

3179-
// Emit the top-level resource entry if we haven't yet.
3180-
if (!std::exchange(hadResource, true)) {
3181-
if (needResourceComma)
3182-
os << "," << newLine;
3183-
os << " " << dictName << "_resources: {" << newLine;
3184-
}
3185-
// Emit the parent resource entry if we haven't yet.
3186-
if (!std::exchange(hadEntry, true)) {
3187-
if (needEntryComma)
3192+
auto printFormatting = [&]() {
3193+
// Emit the top-level resource entry if we haven't yet.
3194+
if (!std::exchange(hadResource, true)) {
3195+
if (needResourceComma)
3196+
os << "," << newLine;
3197+
os << " " << dictName << "_resources: {" << newLine;
3198+
}
3199+
// Emit the parent resource entry if we haven't yet.
3200+
if (!std::exchange(hadEntry, true)) {
3201+
if (needEntryComma)
3202+
os << "," << newLine;
3203+
os << " " << name << ": {" << newLine;
3204+
} else {
31883205
os << "," << newLine;
3189-
os << " " << name << ": {" << newLine;
3206+
}
3207+
};
3208+
3209+
std::optional<uint64_t> charLimit =
3210+
printerFlags.getLargeResourceStringLimit();
3211+
if (charLimit.has_value()) {
3212+
std::string resourceStr;
3213+
llvm::raw_string_ostream ss(resourceStr);
3214+
valueFn(ss);
3215+
3216+
// Only print entry if it's string is small enough
3217+
if (resourceStr.size() > charLimit.value())
3218+
return;
3219+
3220+
printFormatting();
3221+
os << " " << key << ": " << resourceStr;
31903222
} else {
3191-
os << "," << newLine;
3223+
printFormatting();
3224+
os << " " << key << ": ";
3225+
valueFn(os);
31923226
}
3193-
3194-
os << " " << key << ": ";
3195-
valueFn(os);
31963227
};
31973228
ResourceBuilder entryBuilder(*this, printFn);
31983229
provider.buildResources(op, providerArgs..., entryBuilder);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Check printing with --mlir-elide-resource-strings-if-larger elides printing large resources
2+
3+
// RUN: mlir-opt %s --mlir-elide-resource-strings-if-larger=10| FileCheck %s
4+
5+
// To ensure we print the resource keys, have reference to them
6+
// CHECK: attr = dense_resource<blob1> : tensor<3xi64>
7+
"test.blob1op"() {attr = dense_resource<blob1> : tensor<3xi64> } : () -> ()
8+
9+
// CHECK-NEXT: attr = dense_resource<blob2> : tensor<3xi64>
10+
"test.blob2op"() {attr = dense_resource<blob2> : tensor<3xi64> } : () -> ()
11+
12+
// CHECK: {-#
13+
// CHECK-NEXT: external_resources: {
14+
// CHECK-NEXT: external: {
15+
// CHECK-NEXT: bool: true,
16+
// CHECK-NEXT: string: "string"
17+
// CHECK-NEXT: },
18+
// CHECK-NEXT: other_stuff: {
19+
// CHECK-NEXT: bool: true
20+
// CHECK-NEXT: }
21+
// CHECK-NEXT: }
22+
// CHECK-NEXT: #-}
23+
24+
{-#
25+
dialect_resources: {
26+
builtin: {
27+
blob1: "0x08000000010000000000000002000000000000000300000000000000",
28+
blob2: "0x08000000040000000000000005000000000000000600000000000000"
29+
}
30+
},
31+
external_resources: {
32+
external: {
33+
blob: "0x08000000010000000000000002000000000000000300000000000000",
34+
bool: true,
35+
string: "string"
36+
},
37+
other_stuff: {
38+
bool: true
39+
}
40+
}
41+
#-}

0 commit comments

Comments
 (0)