From 005e3e855f58608fd5b8d9f4f53b6253e47646c9 Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Thu, 11 Jul 2024 23:21:31 -0400 Subject: [PATCH 1/5] Write a constant value to single file without buffering to remove spikes in memory consumption. Signed-off-by: Haruki Imai --- .../KrnlToLLVM/ConvertKrnlToLLVM.cpp | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index c50d5e0eb8..b537fca7b2 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -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" @@ -520,8 +521,11 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, // 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 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 packedConst; KrnlGlobalOp op = globalOfInterest[i]; ArrayRef 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 pads(padSize, (char)0); packedConst.insert(packedConst.end(), pads.begin(), pads.end()); } - op.setOffsetAttr(b.getI64IntegerAttr(packedConst.size())); + op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize)); 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, From b30d88f36218426701382afa09c29ac39d3d739a Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Thu, 11 Jul 2024 23:45:57 -0400 Subject: [PATCH 2/5] Update comments. Signed-off-by: Haruki Imai --- src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index b537fca7b2..38667d2cba 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -516,7 +516,7 @@ 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 From f7fda4e2e168e4ca3aa5d2137d66e473aae9728b Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Fri, 12 Jul 2024 01:30:14 -0400 Subject: [PATCH 3/5] Fix offset. Signed-off-by: Haruki Imai --- src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index 38667d2cba..574bcb9b7d 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -543,7 +543,7 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, packedConst.insert(packedConst.end(), pads.begin(), pads.end()); } - op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize)); + op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize + packedConst.size())); op.removeValueAttr(); packedConst.insert(packedConst.end(), rawData.begin(), rawData.end()); outfile.write(packedConst.data(), packedConst.size()); From c3851ba0347d91d45d4e2d24f80337f547db159a Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Fri, 19 Jul 2024 04:41:09 -0400 Subject: [PATCH 4/5] Remove buffer for pading. Signed-off-by: Haruki Imai --- src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index 574bcb9b7d..ec86e1d967 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -519,13 +519,11 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, // 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. + // So every constants must be correctly aligned. Pads are added if necessary. 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 packedConst; KrnlGlobalOp op = globalOfInterest[i]; ArrayRef rawData = getRawData(op); @@ -540,14 +538,14 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, ((uint64_t)(totalConstSize / alignment) + 1) * alignment - totalConstSize; SmallVector pads(padSize, (char)0); - packedConst.insert(packedConst.end(), pads.begin(), pads.end()); + outfile.write(pads.data(), pads.size()); + totalConstSize += pads.size(); } - op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize + packedConst.size())); + op.setOffsetAttr(b.getI64IntegerAttr(totalConstSize)); op.removeValueAttr(); - packedConst.insert(packedConst.end(), rawData.begin(), rawData.end()); - outfile.write(packedConst.data(), packedConst.size()); - totalConstSize += packedConst.size(); + outfile.write(rawData.data(), rawData.size()); + totalConstSize += rawData.size(); } // No constant statisfying thresholds, do not store constants to file. From 0b7ed64731e99bb8617562a3111cfb6d591d3dfc Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Fri, 19 Jul 2024 04:52:07 -0400 Subject: [PATCH 5/5] int64_t to uint64_t Signed-off-by: Haruki Imai --- src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp index ec86e1d967..04cc4206c5 100644 --- a/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp +++ b/src/Conversion/KrnlToLLVM/ConvertKrnlToLLVM.cpp @@ -522,7 +522,7 @@ bool extractConstantsToFile(ModuleOp &module, std::string filepath, // So every constants must be correctly aligned. Pads are added if necessary. llvm::sys::fs::remove(filepath); std::ofstream outfile(filepath, std::ios::app | std::ios::binary); - int64_t totalConstSize = 0; + uint64_t totalConstSize = 0; for (int64_t i = globalOfInterest.size() - 1; i >= 0; --i) { KrnlGlobalOp op = globalOfInterest[i]; ArrayRef rawData = getRawData(op);