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

driver verify bisect #3460

Merged
merged 22 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions src/driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ struct verify : command<verify>
std::optional<double> rtol;
bool per_instruction = false;
bool reduce = false;
bool bisect = false;
verify_options vo;
void parse(argument_parser& ap)
{
Expand All @@ -607,6 +608,7 @@ struct verify : command<verify>
ap.help("Verify each instruction"),
ap.set_value(true));
ap(reduce, {"-r", "--reduce"}, ap.help("Reduce program and verify"), ap.set_value(true));
ap(bisect, {"-b", "--bisect"}, ap.help("Bisect program and verify"), ap.set_value(true));
ap(vo.ref_use_double,
{"--ref-use-double"},
ap.help("Convert floating point values to double on ref"),
Expand Down Expand Up @@ -644,6 +646,10 @@ struct verify : command<verify>
{
verify_reduced_program(p, t, c.co, vo, m, tols);
}
else if (bisect) {
std::cout << "Bisect selected" << std::endl;
verify_bisected_program(p, t, c.co, vo, m, tols);
}
else
{
verify_program(c.l.file, p, t, c.co, vo, m, tols);
Expand Down
54 changes: 53 additions & 1 deletion src/driver/verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
return output;
}

void verify_program(const std::string& name,
bool verify_program(const std::string& name,
const program& p,
const target& t,
compile_options options,
Expand Down Expand Up @@ -144,6 +144,7 @@
}
if(passed)
std::cout << "MIGraphX verification passed successfully." << std::endl;
return passed;
}

void verify_instructions(const program& prog,
Expand Down Expand Up @@ -237,6 +238,57 @@
}
}

bool verify_bisect(program p,
int mid_n,
const target& t,
compile_options options,
verify_options vo,
const parameter_map& inputs,
verify::tolerance tols)
{
auto* mm = p.get_main_module();
auto mid = std::next(mm->begin(), mid_n);
mm->remove_instructions(mid, mm->end());
std::cout << "Verify up to: " << mid_n << std::endl;
std::cout << p << std::endl;
try
{
return verify_program(std::to_string(mid_n), p, t, options, vo, inputs, tols);
}
catch(const std::exception& e)
{
std::cout << "FAILED: " << mid_n << std::endl;
std::cout << "Exception: " << e.what() << std::endl;
return false;
}

}

void verify_bisected_program(const program& p,
const target& t,
compile_options options,
verify_options vo,
const parameter_map& inputs,
verify::tolerance tols)
{
const auto* mm = p.get_main_module();
auto right = std::distance(mm->begin(), mm->end());
std::size_t left = 0;
bool passed;

Check warning on line 277 in src/driver/verify.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

style: The scope of the variable 'passed' can be reduced. [variableScope]

std::cout << "Bisect Verify steps: " << right << std::endl;
while (left <= right) {
std::size_t mid = left + (right - left) / 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to skip the instructions that are const foldable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably create a vector to do the mapping:

std::vector<std::size_t> trim_map;
std::size_t i = 0;
for(const auto& ins:*mm)
{
    if(not ins.can_eval())
        trim_map.push_back(i);
    i++;
}

Then do the bisection over trim_map and then use it to get the actual trim value: verify_bisect(p, trim_map[mid], t, options, vo, inputs, tols).

passed = verify_bisect(p, mid, t, options, vo, inputs, tols);
if (passed) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}


} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx
8 changes: 7 additions & 1 deletion src/driver/verify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ verify::tolerance get_tolerances(const program& p,
std::optional<double> atol,
std::optional<double> rtol);

void verify_program(const std::string& name,
bool verify_program(const std::string& name,
const program& p,
const target& t,
compile_options options = compile_options{},
Expand All @@ -56,6 +56,12 @@ void verify_reduced_program(const program& p,
verify_options vo = verify_options{},
const parameter_map& inputs = {},
verify::tolerance tols = verify::tolerance{});
void verify_bisected_program(const program& p,
const target& t,
compile_options options = compile_options{},
verify_options vo = verify_options{},
const parameter_map& inputs = {},
verify::tolerance tols = verify::tolerance{});

} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
Expand Down
Loading