diff --git a/xformer/Transforms/Options.h b/xformer/Transforms/Options.h index fdafed74f..3b82763c6 100644 --- a/xformer/Transforms/Options.h +++ b/xformer/Transforms/Options.h @@ -14,7 +14,8 @@ extern llvm::cl::opt enableBetaFloatOption; extern llvm::cl::opt threadCountOption; extern llvm::cl::opt weightsFilenameOption; extern llvm::cl::opt loadExternallyIfLargerOption; -extern llvm::cl::opt tileLoadOption; +extern llvm::cl::opt weightsAsArrayOption; +extern llvm::cl::opt weightsInExternalMemory; extern llvm::cl::opt maxLoadExternalSizeOption; extern llvm::cl::opt convQuantErrorThresholdOption; extern llvm::cl::opt convForceErrorCheckOption; diff --git a/xformer/Transforms/WriteWeights.cpp b/xformer/Transforms/WriteWeights.cpp index 1e73e3bf3..e832cb636 100644 --- a/xformer/Transforms/WriteWeights.cpp +++ b/xformer/Transforms/WriteWeights.cpp @@ -145,9 +145,10 @@ void WriteWeights::runOnOperation() { patterns.insert(&tensorsVec, ctx); (void)applyPatternsAndFoldGreedily(func, std::move(patterns)); - if (tileLoadOption) { + if (weightsAsArrayOption) { if (failed(utils::writeTileServerDataToFile(weightsFilenameOption, - tensorsVec))) { + tensorsVec, + weightsInExternalMemory))) { f.emitError("Failed to write tile data!"); signalPassFailure(); return; diff --git a/xformer/Utils/FileIO.cpp b/xformer/Utils/FileIO.cpp index 549633542..b06445ef2 100644 --- a/xformer/Utils/FileIO.cpp +++ b/xformer/Utils/FileIO.cpp @@ -36,35 +36,51 @@ LogicalResult writeWeightsToFile(const std::string &filename, LogicalResult writeTileServerDataToFile(const std::string &filename, - std::vector> tensorsVec) { + std::vector> tensorsVec, + bool placeInExternalMemory) { // Add header auto tileHeader = utils::tileRamHeader(); tensorsVec.insert(tensorsVec.begin(), tileHeader); - std::ostringstream out; - out << R"(#ifndef TILESERVERGEN_H -#define TILESERVERGEN_H + std::ostringstream cOut; + cOut << R"(#include )"; -const int8_t tile_server_weights[] = { -)"; + if (placeInExternalMemory) { + cOut << "\n\n" << R"(__attribute__ ((section(".ExtMem.data"))))" << "\n"; + } + + cOut << "const int8_t tile_server_weights[] = {\n"; int lineEnding = 0; + int weightsSize = 0; for (auto const &tensor : tensorsVec) { for (auto const &i : tensor) { - out << (int)i << ", "; + cOut << (int)i << ", "; lineEnding++; + weightsSize++; if (lineEnding > 80) { - out << "\n"; + cOut << "\n"; lineEnding = 0; } } } - out << R"(}; + cOut << R"(}; +)"; + + if (failed(utils::writeDataToFile(filename + ".c", cOut.str()))) { + return failure(); + } + + std::ostringstream hOut; + hOut << R"(#ifndef TILESERVERGEN_H +#define TILESERVERGEN_H + +#define TILE_SERVER_WEIGHTS_SIZE ()" << weightsSize << R"(U) #endif // TILESERVERGEN_H )"; - return utils::writeDataToFile(filename, out.str()); + return utils::writeDataToFile(filename + ".h", hOut.str()); } LogicalResult getFlatBufferStringFromMLIR( diff --git a/xformer/Utils/FileIO.h b/xformer/Utils/FileIO.h index c38c7675b..a1345d389 100644 --- a/xformer/Utils/FileIO.h +++ b/xformer/Utils/FileIO.h @@ -15,7 +15,8 @@ LogicalResult writeWeightsToFile(const std::string &filename, LogicalResult writeTileServerDataToFile(const std::string &filename, - std::vector> tensorsVec); + std::vector> tensorsVec, + bool placeInExternalMemory); LogicalResult getFlatBufferStringFromMLIR( mlir::ModuleOp module, std::map metadata, diff --git a/xformer/XCoreOptMain.cpp b/xformer/XCoreOptMain.cpp index 10f80a74f..682d2dad4 100644 --- a/xformer/XCoreOptMain.cpp +++ b/xformer/XCoreOptMain.cpp @@ -72,8 +72,12 @@ cl::alias aliasWeightsFilenameOption("f", cl::desc("Alias for --xcore-weights-file"), cl::aliasopt(weightsFilenameOption)); -cl::opt tileLoadOption("xcore-load-tile", - cl::desc("Enable loading weights from a tile."), +cl::opt weightsAsArrayOption("xcore-write-weights-as-array", + cl::desc("Write the weights in the form of an array in a source file (creates .c/.h files with as the file name)."), + cl::init(false), cl::cat(XformerCategory)); + +cl::opt weightsInExternalMemory("xcore-weights-in-external-memory", + cl::desc("Annotate the generated weights array with an attribute to place it in external memory."), cl::init(false), cl::cat(XformerCategory)); cl::opt loadExternallyIfLargerOption( @@ -453,11 +457,18 @@ int main(int argc, char **argv) { "Please specify an output filename using the -o option!"); } - if (mlir::xcore::tileLoadOption.getNumOccurrences() > 0 && + if (mlir::xcore::weightsAsArrayOption.getNumOccurrences() > 0 && mlir::xcore::threadCountOption < 4) { + // TODO: This feels wrong considering this is just about the export format of the weights return failedMessage("Please specify at least four threads using " "xcore-thread-count option when using the " - "xcore-load-tile option!"); + "xcore-write-weights-as-array option!"); + } + + if (mlir::xcore::weightsInExternalMemory.getNumOccurrences() > 0 && + mlir::xcore::weightsAsArrayOption.getNumOccurrences() == 0) { + return failedMessage("Please specify the xcore-write-weights-as-array" + "when using the xcore-weights-in-external-memory option!"); } if (mlir::xcore::loadExternallyIfLargerOption.getNumOccurrences() > 0 &&