Skip to content

Commit

Permalink
[luci/pass] Process alternate layer params (#11647)
Browse files Browse the repository at this point in the history
This will enable to process altnernate layer params.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
Co-authored-by: Hyukjin Jeong <[email protected]>
  • Loading branch information
seanshpark and jinevening authored Oct 4, 2023
1 parent 65640c0 commit 46a971d
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions compiler/luci/pass/src/CircleQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <map>
#include <memory>
#include <string>
#include <unordered_set>

namespace
{
Expand Down Expand Up @@ -313,6 +314,66 @@ bool QuantizeOptionsImpl::query(Algorithm algo)

} // namespace

namespace
{

bool is_valid_params(loco::Graph *g, LayerParams &lps)
{
// no same name in lps
std::unordered_set<std::string> us;
for (auto &lp : lps)
{
if (us.find(lp->name) != us.end())
throw std::runtime_error("Duplicate name found in configuration: " + lp->name);
us.emplace(lp->name);
}

// all name should be found in graph
for (auto &lp : lps)
{
auto name = lp->name;
bool found = false;
for (auto node : loco::active_nodes(loco::output_nodes(g)))
{
auto cnode = loco::must_cast<luci::CircleNode *>(node);
if (cnode->opcode() == luci::CircleOpcode::CIRCLEOUTPUT)
continue;

if (cnode->name() == name)
{
found = true;
break;
}
}
if (not found)
return false;
}
return true;
}

LayerParams find_valid_params(loco::Graph *g, LayerParamsSet &lpss)
{
// valid condition: there should be only one LayerParams that is OK
uint32_t valid_count = 0;
LayerParams params;
for (auto &lps : lpss)
{
if (is_valid_params(g, lps))
{
valid_count++;
params = lps;
}
}
if (valid_count != 1)
throw std::runtime_error(
"Configuration file has layer names (and alternates) that can be mapped in multiple or no "
"ways. Please update configuration file to have only one valid name mapping.");

return params;
}

} // namespace

namespace luci
{

Expand Down Expand Up @@ -341,6 +402,7 @@ void CircleQuantizer::quantize(loco::Graph *g) const
_options->param(Options::AlgorithmParameters::Quantize_output_model_dtype);
auto granularity = _options->param(Options::AlgorithmParameters::Quantize_granularity);
auto layer_params = _options->layer_params(Options::AlgorithmParameters::Quantize_layer_params);
auto layer_params_set = _options->layer_params_set();

if (!in_array(to_lower_case(input_model_dtype), fakeq_supported_input_model_dtype))
throw std::runtime_error("Unsupported input type. List of supported input type: " +
Expand All @@ -358,6 +420,11 @@ void CircleQuantizer::quantize(loco::Graph *g) const
str_to_dtype(output_model_dtype) != loco::DataType::U8)
throw std::runtime_error("Layer-wise quantization only supports uint8 dtype.");

if (layer_params_set.size() > 1u)
{
layer_params = find_valid_params(g, layer_params_set);
}

// Check dtype/granularity of layer params
for (auto layer_param : layer_params)
{
Expand Down Expand Up @@ -436,6 +503,7 @@ void CircleQuantizer::quantize(loco::Graph *g) const
_options->param(Options::AlgorithmParameters::Quantize_TF_style_maxpool) == "True";

auto layer_params = _options->layer_params(Options::AlgorithmParameters::Quantize_layer_params);
auto layer_params_set = _options->layer_params_set();

if (!in_array(to_lower_case(input_model_dtype), qwmm_supported_input_model_dtype))
throw std::runtime_error("Unsupported input type. List of supported input types: " +
Expand Down Expand Up @@ -467,6 +535,11 @@ void CircleQuantizer::quantize(loco::Graph *g) const
str_to_dtype(output_model_dtype) != loco::DataType::U8)
throw std::runtime_error("Layer-wise quantization only supports uint8 dtype.");

if (layer_params_set.size() > 1u)
{
layer_params = find_valid_params(g, layer_params_set);
}

// Check dtype/granularity of layer params
for (auto layer_param : layer_params)
{
Expand Down

0 comments on commit 46a971d

Please sign in to comment.