From 11c6f602fdc41354528ab1e68201a83823d4dec2 Mon Sep 17 00:00:00 2001 From: Raghul PS Date: Sat, 17 Feb 2024 20:01:39 +0530 Subject: [PATCH 1/3] ONNX dump features --- MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp b/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp index 2bb9450e..a5070675 100755 --- a/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp +++ b/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp @@ -13,7 +13,10 @@ //===----------------------------------------------------------------------===// #include "MLModelRunner/ONNXModelRunner/ONNXModelRunner.h" +//#include "MLModelRunner/MLModelRunner.h" +#include "MLModelRunner/MLModelRunner.h" #include "SerDes/baseSerDes.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; namespace MLBridge { @@ -34,14 +37,25 @@ void ONNXModelRunner::addAgent(Agent *agent, std::string name) { } } +void passAgentInfo(std::string mode, std::string agentName, int action) { + std::error_code EC; + llvm::raw_fd_ostream fileStream("test-raw.txt", EC, llvm::sys::fs::OF_Append); + fileStream << mode << ": " << agentName << ": " << action << "\n"; +} + void ONNXModelRunner::computeAction(Observation &obs) { + //std::error_code EC; + //llvm::raw_fd_ostream fileStream("test-raw.txt", EC, llvm::sys::fs::OF_Append); while (true) { Action action; // current agent auto current_agent = this->agents[this->env->getNextAgent()]; action = current_agent->computeAction(obs); + passAgentInfo("input", this->env->getNextAgent(), action); this->env->step(action); + if (this->env->checkDone()) { + passAgentInfo("output", this->env->getNextAgent(), action); std::cout << "Done🎉\n"; break; } From a17b42d46a066a264004897dbe602ac5249566e6 Mon Sep 17 00:00:00 2001 From: Raghul PS Date: Sat, 17 Feb 2024 21:39:56 +0530 Subject: [PATCH 2/3] Dump Features Fixes --- .../ONNXModelRunner/ONNXModelRunner.cpp | 7 +- include/MLModelRunner/MLModelRunner.h | 64 ++++++++++++++++++- 2 files changed, 66 insertions(+), 5 deletions(-) mode change 100755 => 100644 MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp diff --git a/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp b/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp old mode 100755 new mode 100644 index a5070675..2474c335 --- a/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp +++ b/MLModelRunner/ONNXModelRunner/ONNXModelRunner.cpp @@ -44,8 +44,9 @@ void passAgentInfo(std::string mode, std::string agentName, int action) { } void ONNXModelRunner::computeAction(Observation &obs) { - //std::error_code EC; - //llvm::raw_fd_ostream fileStream("test-raw.txt", EC, llvm::sys::fs::OF_Append); + // std::error_code EC; + // llvm::raw_fd_ostream fileStream("test-raw.txt", EC, + // llvm::sys::fs::OF_Append); while (true) { Action action; // current agent @@ -53,7 +54,7 @@ void ONNXModelRunner::computeAction(Observation &obs) { action = current_agent->computeAction(obs); passAgentInfo("input", this->env->getNextAgent(), action); this->env->step(action); - + if (this->env->checkDone()) { passAgentInfo("output", this->env->getNextAgent(), action); std::cout << "Done🎉\n"; diff --git a/include/MLModelRunner/MLModelRunner.h b/include/MLModelRunner/MLModelRunner.h index d99a6c6f..23eff92a 100644 --- a/include/MLModelRunner/MLModelRunner.h +++ b/include/MLModelRunner/MLModelRunner.h @@ -39,19 +39,24 @@ #include "SerDes/baseSerDes.h" #include "SerDes/bitstreamSerDes.h" #include "SerDes/jsonSerDes.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/raw_ostream.h" #include +#include #include #include +#include +#include #include #include #ifndef C_LIBRARY #include "SerDes/protobufSerDes.h" #include "SerDes/tensorflowSerDes.h" +#include #endif namespace MLBridge { - /// MLModelRunner - The main interface for interacting with the ML models. class MLModelRunner { public: @@ -73,11 +78,33 @@ class MLModelRunner { void>::type evaluate(T &data, size_t &dataSize) { using BaseType = typename std::remove_pointer::type; - void *res = evaluateUntyped(); + auto *res = evaluateUntyped(); T ret = static_cast(malloc(SerDes->getMessageLength())); memcpy(ret, res, SerDes->getMessageLength()); dataSize = SerDes->getMessageLength() / sizeof(BaseType); data = ret; + std::error_code EC; + llvm::raw_fd_ostream fileStream("test-raw.txt", EC, + llvm::sys::fs::OF_Append); + dumpOutput(fileStream, ret, dataSize); + } + + template + void dumpOutput(llvm::raw_ostream &OS, T output_vec, int DataSize) { + + OS << "Dumping output" + << ": "; + for (auto i = 0; i < DataSize; i++) { + OS << output_vec[i] << " "; + } + OS << "\n"; + } + + template + void dumpOuput(llvm::raw_ostream &OS, T &var1, int DataSize) { + OS << "Dumping output" + << ": "; + OS << var1 << "\n"; } /// Type of the MLModelRunner @@ -87,7 +114,36 @@ class MLModelRunner { BaseSerDes::Kind getSerDesKind() const { return SerDesType; } virtual void requestExit() = 0; + std::promise *exit_requested; + + template + void passMetaInfo(llvm::raw_ostream &OS, std::pair &var1, + std::pair &...var2) { + OS << var1.first << ": " << var1.second << "\n"; + passMetaInfo(var2...); + } + + template + void dumpFeature(llvm::raw_ostream &OS, std::pair &var1) { + OS << "Dumping input" + << ": "; + OS << var1.first << ": " << var1.second << "\n"; + } + + template + void dumpFeature(llvm::raw_ostream &OS, + std::pair> &var1) { + OS << "Dumping input" + << ": "; + OS << var1.first << ": "; + for (const auto &elem : var1.second) { + OS << elem << " "; + } + OS << "\n"; + } + void dumpFeature(llvm::raw_ostream &OS, + std::pair> &var1) {} /// User-facing interface for setting the features to be sent to the model. /// The features are passed as a list of key-value pairs. /// The key is the name of the feature and the value is the value of the @@ -96,6 +152,10 @@ class MLModelRunner { void populateFeatures(std::pair &var1, std::pair &...var2) { SerDes->setFeature(var1.first, var1.second); + std::error_code EC; + llvm::raw_fd_ostream fileStream("test-raw.txt", EC, + llvm::sys::fs::OF_Append); + dumpFeature(fileStream, var1); populateFeatures(var2...); } From 8ddb2fe212dba36e34083f7a9d2959277ea55fd9 Mon Sep 17 00:00:00 2001 From: Raghul PS Date: Sat, 17 Feb 2024 22:57:18 +0530 Subject: [PATCH 3/3] Message type fixed for dump --- include/MLModelRunner/MLModelRunner.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/MLModelRunner/MLModelRunner.h b/include/MLModelRunner/MLModelRunner.h index 23eff92a..2b87e692 100644 --- a/include/MLModelRunner/MLModelRunner.h +++ b/include/MLModelRunner/MLModelRunner.h @@ -78,7 +78,7 @@ class MLModelRunner { void>::type evaluate(T &data, size_t &dataSize) { using BaseType = typename std::remove_pointer::type; - auto *res = evaluateUntyped(); + void *res = evaluateUntyped(); T ret = static_cast(malloc(SerDes->getMessageLength())); memcpy(ret, res, SerDes->getMessageLength()); dataSize = SerDes->getMessageLength() / sizeof(BaseType); @@ -142,8 +142,9 @@ class MLModelRunner { OS << "\n"; } - void dumpFeature(llvm::raw_ostream &OS, - std::pair> &var1) {} + void dumpFeature( + llvm::raw_ostream &OS, + std::pair> &var1) {} /// User-facing interface for setting the features to be sent to the model. /// The features are passed as a list of key-value pairs. /// The key is the name of the feature and the value is the value of the