Skip to content
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

[onert/odc] Auto-compilation. Minmax threshold and quantization Readiness #14372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions runtime/onert/core/include/odc/IQuantizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define __ONERT_ODC_IQUANTIZER_H__

#include "odc/QuantizeType.h"
#include <cstdint>

namespace onert
{
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions runtime/onert/core/include/odc/QuantizeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions runtime/onert/core/src/odc/QuantizeManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions runtime/onert/odc/MinMaxReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions runtime/onert/odc/MinMaxReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
37 changes: 35 additions & 2 deletions runtime/onert/odc/Quantizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Quantizer.h"

#include "Embedder.h"
#include "MinMaxReader.h"

#include <util/ConfigSource.h>
#include <luci/ImporterEx.h>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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});
}

Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions runtime/onert/odc/Quantizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down