-
Notifications
You must be signed in to change notification settings - Fork 329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write a constant value to a file without buffering #2874
Changes from 5 commits
005e3e8
b30d88f
f7fda4e
7be482f
575aa4a
bd30682
c3851ba
0b7ed64
d7d3055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ | |
#include "llvm/ADT/TypeSwitch.h" | ||
#include "llvm/Support/Debug.h" | ||
#include "llvm/Support/Endian.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/Support/Path.h" | ||
|
||
#include "onnx/onnx_pb.h" | ||
|
@@ -515,13 +516,16 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, | |
return (leftAlign < rightAlign); | ||
}); | ||
|
||
// Pack all constants into a single buffer in order to save to file. | ||
// Store each constant into single file. | ||
// Constants with the highest alignment will be packed first in the file. | ||
// The file will be mmaped later at runtime and aligned at the page boundary, | ||
// So every constants must be correctly aligned in the packed constant. Pads | ||
// are added if necessary. | ||
std::vector<char> packedConst; | ||
llvm::sys::fs::remove(filepath); | ||
std::ofstream outfile(filepath, std::ios::app | std::ios::binary); | ||
int64_t totalConstSize = 0; | ||
for (int64_t i = globalOfInterest.size() - 1; i >= 0; --i) { | ||
std::vector<char> packedConst; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name |
||
KrnlGlobalOp op = globalOfInterest[i]; | ||
ArrayRef<char> rawData = getRawData(op); | ||
|
||
|
@@ -531,27 +535,25 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, | |
alignment = op.getAlignment().value(); | ||
|
||
// Padding if necessary. | ||
if ((alignment > 0) && (packedConst.size() % alignment != 0)) { | ||
if ((alignment > 0) && (totalConstSize % alignment != 0)) { | ||
uint64_t padSize = | ||
((uint64_t)(packedConst.size() / alignment) + 1) * alignment - | ||
packedConst.size(); | ||
((uint64_t)(totalConstSize / alignment) + 1) * alignment - | ||
totalConstSize; | ||
SmallVector<char> pads(padSize, (char)0); | ||
packedConst.insert(packedConst.end(), pads.begin(), pads.end()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can reduce memory more by not using a padded buffer but two writes to a file: one for padding and one for rawData. For example. // Padding the current data for correct alignment.
uint64_t padSize = 0;
if ((alignment > 0) && (totalConstSize % alignment != 0)) {
padSize = ((uint64_t)(totalConstSize / alignment) + 1) * alignment - totalConstSize;
SmallVector<char> pads(padSize, (char)0);
outfile.write(pads.data(), pads.size());
}
// Write the constant to a file.
uint64_t constSize = rawData.size();
outfile.write(rawData.data(), constSize);
// Update the total size and op's offset.
totalConstSize += padSize + constSize;
op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize));
op.removeValueAttr(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smart, please do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your comments! I updated.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
} | ||
|
||
op.setOffsetAttr(b.getI64IntegerAttr(packedConst.size())); | ||
op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize + packedConst.size())); | ||
op.removeValueAttr(); | ||
packedConst.insert(packedConst.end(), rawData.begin(), rawData.end()); | ||
outfile.write(packedConst.data(), packedConst.size()); | ||
totalConstSize += packedConst.size(); | ||
} | ||
|
||
// No constant statisfying thresholds, do not store constants to file. | ||
if (packedConst.empty()) | ||
if (totalConstSize == 0) | ||
return false; | ||
|
||
// Save to file. | ||
std::ofstream outfile(filepath, std::ofstream::binary); | ||
outfile.write(packedConst.data(), packedConst.size()); | ||
|
||
// Create a global op to store the filename in the IR. | ||
OpBuilder::InsertionGuard guard(b); | ||
b.setInsertionPointToStart(module.getBody()); | ||
|
@@ -564,7 +566,8 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, | |
create.llvm.globalOp(llvmI64Ty, | ||
/*isConstant=*/true, LLVM::Linkage::Internal, | ||
EXTERNAL_CONSTANT_PREFIX + "filesize", | ||
b.getI64IntegerAttr(packedConst.size())); | ||
b.getI64IntegerAttr(totalConstSize)); | ||
|
||
// Create a global to store isLE. | ||
bool isLE = llvm::endianness::native == llvm::endianness::little; | ||
create.llvm.globalOp(llvmI8Ty, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint64_t
is more suitable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks!