Skip to content

Commit

Permalink
Merge branch 'feature/auto_select_qtmtd' of github.com:curioyang/nnca…
Browse files Browse the repository at this point in the history
…se into feature/auto_select_qtmtd
  • Loading branch information
curioyang committed May 13, 2022
2 parents 9305b59 + e59ffa2 commit 7397d80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 48 deletions.
27 changes: 9 additions & 18 deletions src/evaluator/quantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ static std::vector<float> smooth_distribution(const std::vector<float> &p, const
{
auto it = p.begin();
std::generate(is_zeros.begin(), is_zeros.end(),
[&it]()
{ return static_cast<size_t>(*(it++) == 0.f); });
[&it]() { return static_cast<size_t>(*(it++) == 0.f); });
}
{
auto it = p.begin();
std::generate(is_nonzeros.begin(), is_nonzeros.end(),
[&it]()
{ return static_cast<size_t>(*(it++) != 0.f); });
[&it]() { return static_cast<size_t>(*(it++) != 0.f); });
}
size_t n_zeros = std::accumulate(is_zeros.begin(), is_zeros.end(), 0);
size_t n_nonzeros = p.size() - n_zeros;
Expand Down Expand Up @@ -144,8 +142,7 @@ void run_kld_m2(std::vector<float> &src_bins_, std::optional<std::pair<size_t, s
src_bins_ = smooth(src_bins_);
auto min_kld = std::numeric_limits<float>::max();

auto kld = [&](size_t lower_threshold, size_t upper_threshold)
{
auto kld = [&](size_t lower_threshold, size_t upper_threshold) {
auto src_range = upper_threshold - lower_threshold;
auto src_per_bin = src_range / dest_bins;

Expand Down Expand Up @@ -176,8 +173,7 @@ void run_kld_m2(std::vector<float> &src_bins_, std::optional<std::pair<size_t, s
auto end = start + src_per_bin;
auto count = 0.f;

count += std::count_if(ref_dist.begin() + start, ref_dist.begin() + end, [](float v)
{ return v; });
count += std::count_if(ref_dist.begin() + start, ref_dist.begin() + end, [](float v) { return v; });
if (!count)
continue;
auto upsample_value = q_dist[i] / count;
Expand All @@ -193,8 +189,7 @@ void run_kld_m2(std::vector<float> &src_bins_, std::optional<std::pair<size_t, s
std::vector<float> ups2_q_dist(src_bins_.size());
// left outliers
auto count = 0.f;
count += std::count_if(src_bins_.begin(), src_bins_.begin() + lower_threshold + src_per_bin, [](float v)
{ return v; });
count += std::count_if(src_bins_.begin(), src_bins_.begin() + lower_threshold + src_per_bin, [](float v) { return v; });
auto value = std::reduce(src_bins_.begin(), src_bins_.begin() + lower_threshold + src_per_bin) / count;
for (size_t i = 0; i < lower_threshold + src_per_bin; i++)
{
Expand All @@ -205,8 +200,7 @@ void run_kld_m2(std::vector<float> &src_bins_, std::optional<std::pair<size_t, s
std::copy(ups_q_dist.begin() + src_per_bin, ups_q_dist.end() - src_per_bin, ups2_q_dist.begin() + lower_threshold + src_per_bin);
// right outliers
count = 0.f;
count += std::count_if(src_bins_.begin() + upper_threshold - src_per_bin, src_bins_.end(), [](float v)
{ return v; });
count += std::count_if(src_bins_.begin() + upper_threshold - src_per_bin, src_bins_.end(), [](float v) { return v; });
value = std::reduce(src_bins_.begin() + upper_threshold - src_per_bin, src_bins_.end()) / count;
for (size_t i = upper_threshold - src_per_bin; i < src_bins_.size(); i++)
{
Expand Down Expand Up @@ -520,8 +514,7 @@ fixed_mul quantizer::get_fixed_mul(float value, int32_t max_bits, uint8_t max_sh

void quantizer::set_model_output_range(ir::graph &graph)
{
auto visitor = make_relay_ir_visitor([&](node &node)
{
auto visitor = make_relay_ir_visitor([&](node &node) {
if (node.runtime_opcode() == op_output_node)
{
auto it = quant_ranges_.find(node.input_at(0).connection());
Expand All @@ -535,8 +528,7 @@ void quantizer::set_model_output_range(ir::graph &graph)

void quantizer::broadcast_output(ir::graph &graph, const std::unordered_set<node_opcode> &ops)
{
auto visitor = make_relay_ir_visitor([&](node &node)
{
auto visitor = make_relay_ir_visitor([&](node &node) {
if (node.inputs().size() == 1)
{
auto it = quant_ranges_.find(node.input_at(0).connection());
Expand Down Expand Up @@ -669,8 +661,7 @@ void quantizer::histogram::finish()
count += (end - right_lower);
}

count += std::count_if(range_dist.begin() + left_upper, range_dist.begin() + right_lower, [](float v)
{ return v; });
count += std::count_if(range_dist.begin() + left_upper, range_dist.begin() + right_lower, [](float v) { return v; });
if (!count)
continue;
auto upsample_value = q_dist[i] / count;
Expand Down
46 changes: 16 additions & 30 deletions src/nncase/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,31 +438,27 @@ class compiler_impl : public compiler
void pre_process(ir::graph &graph, compile_options &cmp_options)
{
using namespace ir::transforms;
run_passes("pre_process", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ pmgr.add_pass<pre_process_transform>(
cmp_options.mean, cmp_options.std, cmp_options.input_range, cmp_options.input_shape, cmp_options.swapRB, input_layout_, cmp_options.input_type, cmp_options.quant_type, real_inlayout_, cmp_options.letterbox_value); });
run_passes("pre_process", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { pmgr.add_pass<pre_process_transform>(
cmp_options.mean, cmp_options.std, cmp_options.input_range, cmp_options.input_shape, cmp_options.swapRB, input_layout_, cmp_options.input_type, cmp_options.quant_type, real_inlayout_, cmp_options.letterbox_value); });
}
void post_process(ir::graph &graph, compile_options &cmp_options)
{
using namespace ir::transforms;
run_passes("post_process", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ pmgr.add_pass<post_process_transform>(
cmp_options.output_layout, real_outlayout_); });
run_passes("post_process", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { pmgr.add_pass<post_process_transform>(
cmp_options.output_layout, real_outlayout_); });
}

void optimize_target_independent(ir::graph &graph)
{
run_passes("target_indep", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ target_->register_target_independent_passes(module_type, pmgr); });
run_passes("target_indep", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { target_->register_target_independent_passes(module_type, pmgr); });
}

void optimize_merge_module_regions(ir::graph &graph)
{
std::cout << "7.1. Merge module regions..." << std::endl;
using namespace ir::transforms;

run_passes("mark_noaction", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{
run_passes("mark_noaction", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr) {
pmgr.add_pass<make_slice_no_action_pass>();
pmgr.add_pass<make_concat_no_action_pass>();
pmgr.add_pass<make_bitcast_no_action_pass>(); });
Expand All @@ -480,17 +476,15 @@ class compiler_impl : public compiler
void optimize_benchmark(ir::graph &graph)
{
using namespace ir::transforms;
run_passes("mark_noaction", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ pmgr.add_pass<optimize_benchmark_pass>(); });
run_passes("mark_noaction", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { pmgr.add_pass<optimize_benchmark_pass>(); });
dump_graph(graph, "optimize_benchmark");
}

void optimize_buffer_fusion(ir::graph &graph)
{
using namespace ir::transforms;

run_passes("buffer_fusion", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{
run_passes("buffer_fusion", graph, [&]([[maybe_unused]] const module_type_t &module_type, ir::transforms::pass_manager &pmgr) {
pmgr.add_pass<add_copy_to_concat_pass>();
pmgr.add_pass<add_copy_to_slice_pass>();
pmgr.add_pass<add_copy_to_output_pass>();
Expand All @@ -507,34 +501,29 @@ class compiler_impl : public compiler
{
using namespace ir::transforms;

run_passes("target_dependent_after_buffer_fusion", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{
run_passes("target_dependent_after_buffer_fusion", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr) {
target_->register_target_dependent_after_buffer_fusion_passes(module_type, pmgr);
pmgr.add_pass<make_bitcast_no_action_pass>(); });
}

void optimize_target_dependent(ir::graph &graph, bool use_ptq)
{
run_passes("target_dep", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ target_->register_target_dependent_passes(module_type, pmgr, use_ptq, compile_options_.split_w_to_act); });
run_passes("target_dep", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { target_->register_target_dependent_passes(module_type, pmgr, use_ptq, compile_options_.split_w_to_act); });
}

void optimize_target_dependent_after_quant(ir::graph &graph)
{
run_passes("target_dep_after_quant", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ target_->register_target_dependent_after_quantization_passes(module_type, pmgr); });
run_passes("target_dep_after_quant", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { target_->register_target_dependent_after_quantization_passes(module_type, pmgr); });
}

void add_quantize_annotation(ir::graph &graph)
{
run_passes("quantize_annotation", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr)
{ target_->register_quantize_annotation_passes(module_type, pmgr); });
run_passes("quantize_annotation", graph, [&](const module_type_t &module_type, ir::transforms::pass_manager &pmgr) { target_->register_quantize_annotation_passes(module_type, pmgr); });
}

void quantize_graph(ir::graph &graph, ir::evaluator &evaluator)
{
auto graph_runner = [&](ir::graph &graph)
{
auto graph_runner = [&](ir::graph &graph) {
ir::transforms::pass_manager pmgr(graph, *target_);
auto quant = evaluator.quantizer(graph.module_type());
if (compile_options_.input_type != "float32" && compile_options_.preprocess == true)
Expand Down Expand Up @@ -579,15 +568,13 @@ class compiler_impl : public compiler

if (step != eval_step::after_import)
{
auto calib_method = std::visit([](auto &options)
{ return to_calibrate_method(options.calibrate_method); },
auto calib_method = std::visit([](auto &options) { return to_calibrate_method(options.calibrate_method); },
ptq_options_);
evaluator.enable_ptq(*target_, calib_method);
}
else
{
auto calib_method = std::visit([](auto &options)
{ return to_calibrate_method(options.calibrate_method); },
auto calib_method = std::visit([](auto &options) { return to_calibrate_method(options.calibrate_method); },
dump_range_options_);
evaluator.enable_ptq(*target_, calib_method);
}
Expand Down Expand Up @@ -753,8 +740,7 @@ class compiler_impl : public compiler
template <class Callable>
void run_passes(std::string_view name, ir::graph &root_graph, Callable &&register_passes)
{
auto graph_runner = [&](ir::graph &graph)
{
auto graph_runner = [&](ir::graph &graph) {
ir::transforms::pass_manager pmgr(graph, *target_);
if (compile_options_.dump_ir)
pmgr.dump_dir(compile_options_.dump_dir);
Expand Down

0 comments on commit 7397d80

Please sign in to comment.