diff --git a/runtime/onert/core/include/odc/IQuantizer.h b/runtime/onert/core/include/odc/IQuantizer.h index 3491d218547..4381a68ad11 100644 --- a/runtime/onert/core/include/odc/IQuantizer.h +++ b/runtime/onert/core/include/odc/IQuantizer.h @@ -18,6 +18,7 @@ #define __ONERT_ODC_IQUANTIZER_H__ #include "odc/QuantizeType.h" +#include namespace onert { @@ -30,6 +31,25 @@ class IQuantizer virtual ~IQuantizer() = default; virtual int quantize(const char *in, const char *out, QuantizeType qtype) = 0; + + /** + * @brief Set the number of minmax records enough for quantization + */ + virtual void setMinMaxRecordsThreshold(uint32_t value) = 0; + + /** + * @brief Checking the number of minmax records enough for quantization + * + * @return True if ready, False otherwise + */ + virtual bool readyForQuantize() = 0; + + /** + * @brief Delete minmax file + * + * @return True if there were no errors, False otherwise + */ + virtual bool deleteMinMaxFile() = 0; }; } // namespace odc diff --git a/runtime/onert/core/include/odc/QuantizeManager.h b/runtime/onert/core/include/odc/QuantizeManager.h index 257b86b0cc3..a1be4867570 100644 --- a/runtime/onert/core/include/odc/QuantizeManager.h +++ b/runtime/onert/core/include/odc/QuantizeManager.h @@ -69,6 +69,24 @@ class QuantizeManager */ bool quantize(const std::string &model_path); + /** + * @brief Set the number of minmax records enough for quantization + * @return @c true if success, otherwise @c false + */ + bool setMinMaxRecordsThreshold(uint32_t value); + + /** + * @brief checking minmax recording count and threshold for quantization + * @return @c true if ready, otherwise @c false + */ + bool readyForQuantize(); + + /** + * @brief Delete MinMax File of on-device compiler + * @return Return true if file removed successfully + */ + bool deleteMinMaxFile(); + private: std::string _export_model_path = ""; QuantizeType _qtype = ODC_QTYPE_NOT_SET; diff --git a/runtime/onert/core/src/odc/QuantizeManager.cc b/runtime/onert/core/src/odc/QuantizeManager.cc index fc5725b911c..9428c679cd0 100644 --- a/runtime/onert/core/src/odc/QuantizeManager.cc +++ b/runtime/onert/core/src/odc/QuantizeManager.cc @@ -46,5 +46,41 @@ bool QuantizeManager::quantize(const std::string &model_path) return (result == 0); } +bool QuantizeManager::setMinMaxRecordsThreshold(uint32_t value) +{ + auto &quantize_loader = QuantizerLoader::instance(); + if (quantize_loader.loadLibrary() != 0) + return false; + + auto quantizer = quantize_loader.get(); + quantizer->setMinMaxRecordsThreshold(value); + + return true; +} + +bool QuantizeManager::readyForQuantize() +{ + auto &quantize_loader = QuantizerLoader::instance(); + if (quantize_loader.loadLibrary() != 0) + return false; + + auto quantizer = quantize_loader.get(); + bool result = quantizer->readyForQuantize(); + + return result; +} + +bool QuantizeManager::deleteMinMaxFile() +{ + auto &quantize_loader = QuantizerLoader::instance(); + if (quantize_loader.loadLibrary() != 0) + return false; + + auto quantizer = quantize_loader.get(); + bool result = quantizer->deleteMinMaxFile(); + + return result; +} + } // namespace odc } // namespace onert diff --git a/runtime/onert/odc/MinMaxReader.cc b/runtime/onert/odc/MinMaxReader.cc index c9c92148eb5..ee5446ac52f 100644 --- a/runtime/onert/odc/MinMaxReader.cc +++ b/runtime/onert/odc/MinMaxReader.cc @@ -236,5 +236,23 @@ MinMaxVectors MinMaxReader::readInput(uint32_t model_idx, uint32_t subg_idx, return mmv; } +uint32_t MinMaxReader::readNumRuns() const +{ + // Find file to read + auto file = std::fopen(_filepath.c_str(), "rb"); + if (!file) + throw std::runtime_error("Cannot open file: " + _filepath); + + checkHeader(file); + + // Read num_run + uint32_t num_run = 0; + readMMFile(&num_run, sizeof(uint32_t), 1, file, "Cannot read num_run from file"); + + std::fclose(file); + + return num_run; +} + } // namespace odc } // namespace onert diff --git a/runtime/onert/odc/MinMaxReader.h b/runtime/onert/odc/MinMaxReader.h index 98c79fc3f02..44bce892e07 100644 --- a/runtime/onert/odc/MinMaxReader.h +++ b/runtime/onert/odc/MinMaxReader.h @@ -72,6 +72,13 @@ class MinMaxReader */ MinMaxVectors readInput(uint32_t model_idx, uint32_t subg_idx, uint32_t input_idx) const; + /** + * @brief Returns minmax recording count + * + * @return minmax recording count + */ + uint32_t readNumRuns() const; + private: std::string _filepath; }; diff --git a/runtime/onert/odc/Quantizer.cc b/runtime/onert/odc/Quantizer.cc index 3c7b13f3a05..d66c9e027fb 100644 --- a/runtime/onert/odc/Quantizer.cc +++ b/runtime/onert/odc/Quantizer.cc @@ -17,6 +17,7 @@ #include "Quantizer.h" #include "Embedder.h" +#include "MinMaxReader.h" #include #include @@ -76,6 +77,11 @@ void fillQuantizeOptionParam(QuantizerOptions *options, QuantizeType qtype) } } +std::string getMinMaxFilePath() +{ + return util::getConfigString(util::config::WORKSPACE_DIR) + "/minmax.bin"; +} + } // namespace int Quantizer::quantize(const char *in, const char *out, QuantizeType qtype) @@ -111,8 +117,7 @@ int Quantizer::quantize(const char *in, const char *out, QuantizeType qtype) } // Record minmax by minmax-embedder - // TODO use workspace to find minmax file - auto minmax_path = util::getConfigString(util::config::WORKSPACE_DIR) + "/minmax.bin"; + auto minmax_path = getMinMaxFilePath(); Embedder().embed(module.get(), minmax_path, {1.f, 99.f}); } @@ -153,5 +158,33 @@ int Quantizer::quantize(const char *in, const char *out, QuantizeType qtype) return 0; } +bool Quantizer::readyForQuantize() +{ + std::string minmax_path = getMinMaxFilePath(); + + MinMaxReader mmr{minmax_path}; + + uint32_t numRuns = mmr.readNumRuns(); + if (_minmax_threshold != 0 && numRuns >= _minmax_threshold) + return true; + else + return false; +} + +bool Quantizer::deleteMinMaxFile() +{ + int result = 0; + + std::string minmax_path = getMinMaxFilePath(); + result = std::remove(minmax_path.c_str()); + + if (!result) + { + return true; + } + else + return false; +} + } // namespace odc } // namespace onert diff --git a/runtime/onert/odc/Quantizer.h b/runtime/onert/odc/Quantizer.h index c87794bec5c..00f90ad3dd5 100644 --- a/runtime/onert/odc/Quantizer.h +++ b/runtime/onert/odc/Quantizer.h @@ -31,6 +31,29 @@ class Quantizer : public IQuantizer ~Quantizer() = default; int quantize(const char *in, const char *out, QuantizeType qtype) override; + + /** + * @brief Set the number of minmax records enough for quantization + */ + void setMinMaxRecordsThreshold(uint32_t value) { _minmax_threshold = value; }; + + /** + * @brief Checking the number of minmax records enough for quantization (comparison with + * threshold) + * + * @return True if ready, False otherwise + */ + bool readyForQuantize() override; + + /** + * @brief Delete minmax file + * + * @return True if there were no errors, False otherwise + */ + bool deleteMinMaxFile() override; + +private: + uint32_t _minmax_threshold = 0; }; } // namespace odc