"include/ck/utility/functional.hpp" did not exist on "37f4e2b6d8dc45f151f7de81b4d0e35c2804b77f"
Unverified Commit 3c1e91dc authored by kahmed10's avatar kahmed10 Committed by GitHub
Browse files

Add fp16 verify to driver (#988)

Allows --fp16 to be used in the driver to compare the target fp16 result and the ref fp32 result.
parent 636bce89
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <migraphx/type_name.hpp> #include <migraphx/type_name.hpp>
#include <migraphx/functional.hpp> #include <migraphx/functional.hpp>
#include <migraphx/stringutils.hpp> #include <migraphx/stringutils.hpp>
#include <migraphx/rank.hpp>
namespace migraphx { namespace migraphx {
namespace driver { namespace driver {
...@@ -106,10 +107,22 @@ struct argument_parser ...@@ -106,10 +107,22 @@ struct argument_parser
return to_string_range(x); return to_string_range(x);
} }
template <class T>
auto as_string_value(rank<1>, const T& x) -> decltype(to_string(x))
{
return to_string(x);
}
template <class T>
std::string as_string_value(rank<0>, const T&)
{
throw std::runtime_error("Can't convert to string");
}
template <class T, MIGRAPHX_REQUIRES(not is_multi_value<T>{})> template <class T, MIGRAPHX_REQUIRES(not is_multi_value<T>{})>
std::string as_string_value(const T& x) std::string as_string_value(const T& x)
{ {
return to_string(x); return as_string_value(rank<1>{}, x);
} }
template <class T, class... Fs> template <class T, class... Fs>
...@@ -122,10 +135,11 @@ struct argument_parser ...@@ -122,10 +135,11 @@ struct argument_parser
return false; return false;
}}); }});
argument& arg = arguments.back(); argument& arg = arguments.back();
arg.type = migraphx::get_type_name<T>(); arg.type = migraphx::get_type_name<T>();
arg.default_value = as_string_value(x);
migraphx::each_args([&](auto f) { f(x, arg); }, fs...); migraphx::each_args([&](auto f) { f(x, arg); }, fs...);
if(not arg.default_value.empty() and arg.nargs > 0)
arg.default_value = as_string_value(x);
} }
template <class... Fs> template <class... Fs>
......
#include "verify.hpp"
#include "argument_parser.hpp" #include "argument_parser.hpp"
#include "command.hpp" #include "command.hpp"
#include "verify.hpp" #include "precision.hpp"
#include "perf.hpp" #include "perf.hpp"
#include "models.hpp" #include "models.hpp"
#include "marker_roctx.hpp" #include "marker_roctx.hpp"
...@@ -288,14 +289,12 @@ struct compiler_target ...@@ -288,14 +289,12 @@ struct compiler_target
struct compiler struct compiler
{ {
static const int q_fp16 = 1;
static const int q_int8 = 2;
loader l; loader l;
program_params parameters; program_params parameters;
compiler_target ct; compiler_target ct;
bool offload_copy = false; bool offload_copy = false;
bool fast_math = true; bool fast_math = true;
int quantize = 0; precision quantize = precision::fp32;
std::vector<std::string> fill0; std::vector<std::string> fill0;
std::vector<std::string> fill1; std::vector<std::string> fill1;
...@@ -312,8 +311,8 @@ struct compiler ...@@ -312,8 +311,8 @@ struct compiler
{"--disable-fast-math"}, {"--disable-fast-math"},
ap.help("Disable fast math optimization"), ap.help("Disable fast math optimization"),
ap.set_value(false)); ap.set_value(false));
ap(quantize, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(q_fp16)); ap(quantize, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(precision::fp16));
ap(quantize, {"--int8"}, ap.help("Quantize for int8"), ap.set_value(q_int8)); ap(quantize, {"--int8"}, ap.help("Quantize for int8"), ap.set_value(precision::int8));
} }
auto params(const program& p) { return parameters.generate(p, ct.get_target(), offload_copy); } auto params(const program& p) { return parameters.generate(p, ct.get_target(), offload_copy); }
...@@ -325,11 +324,11 @@ struct compiler ...@@ -325,11 +324,11 @@ struct compiler
if(p.is_compiled()) if(p.is_compiled())
return p; return p;
auto t = ct.get_target(); auto t = ct.get_target();
if(quantize == q_fp16) if(quantize == precision::fp16)
{ {
quantize_fp16(p); quantize_fp16(p);
} }
else if(quantize == q_int8) else if(quantize == precision::int8)
{ {
quantize_int8(p, t, {params(p)}); quantize_int8(p, t, {params(p)});
} }
...@@ -377,6 +376,7 @@ struct verify : command<verify> ...@@ -377,6 +376,7 @@ struct verify : command<verify>
bool reduce = false; bool reduce = false;
bool offload_copy = false; bool offload_copy = false;
bool fast_math = true; bool fast_math = true;
precision quantize = precision::fp32;
void parse(argument_parser& ap) void parse(argument_parser& ap)
{ {
l.parse(ap); l.parse(ap);
...@@ -396,6 +396,7 @@ struct verify : command<verify> ...@@ -396,6 +396,7 @@ struct verify : command<verify>
ap.help("Verify each instruction"), ap.help("Verify each instruction"),
ap.set_value(true)); ap.set_value(true));
ap(reduce, {"-r", "--reduce"}, ap.help("Reduce program and verify"), ap.set_value(true)); ap(reduce, {"-r", "--reduce"}, ap.help("Reduce program and verify"), ap.set_value(true));
ap(quantize, {"--fp16"}, ap.help("Quantize for fp16"), ap.set_value(precision::fp16));
} }
void run() void run()
...@@ -412,15 +413,15 @@ struct verify : command<verify> ...@@ -412,15 +413,15 @@ struct verify : command<verify>
if(per_instruction) if(per_instruction)
{ {
verify_instructions(p, t, options, tolerance); verify_instructions(p, t, options, quantize, tolerance);
} }
else if(reduce) else if(reduce)
{ {
verify_reduced_program(p, t, options, m, tolerance); verify_reduced_program(p, t, options, quantize, m, tolerance);
} }
else else
{ {
verify_program(l.file, p, t, options, m, tolerance); verify_program(l.file, p, t, options, quantize, m, tolerance);
} }
} }
}; };
......
#ifndef MIGRAPHX_GUARD_RTGLIB_PRECISION_HPP
#define MIGRAPHX_GUARD_RTGLIB_PRECISION_HPP
namespace migraphx {
namespace driver {
inline namespace MIGRAPHX_INLINE_NS {
enum class precision
{
fp32,
fp16,
int8
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace driver
} // namespace migraphx
#endif
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <migraphx/verify_args.hpp> #include <migraphx/verify_args.hpp>
#include <migraphx/instruction.hpp> #include <migraphx/instruction.hpp>
#include <migraphx/compile_options.hpp> #include <migraphx/compile_options.hpp>
#include <migraphx/quantization.hpp>
namespace migraphx { namespace migraphx {
namespace driver { namespace driver {
...@@ -19,9 +20,16 @@ std::vector<argument> run_ref(program p, const parameter_map& inputs) ...@@ -19,9 +20,16 @@ std::vector<argument> run_ref(program p, const parameter_map& inputs)
return out; return out;
} }
std::vector<argument> std::vector<argument> run_target(program p,
run_target(program p, const target& t, const compile_options& options, const parameter_map& inputs) const target& t,
const compile_options& options,
precision quantize,
const parameter_map& inputs)
{ {
if(quantize == precision::fp16)
{
quantize_fp16(p);
}
p.compile(t, options); p.compile(t, options);
parameter_map m; parameter_map m;
...@@ -43,24 +51,24 @@ void verify_program(const std::string& name, ...@@ -43,24 +51,24 @@ void verify_program(const std::string& name,
const program& p, const program& p,
const target& t, const target& t,
compile_options options, compile_options options,
precision quantize,
const parameter_map& inputs, const parameter_map& inputs,
double tolerance) double tolerance)
{ {
auto x = run_ref(p, inputs); auto x = run_ref(p, inputs);
auto y = run_target(p, t, options, inputs); auto y = run_target(p, t, options, quantize, inputs);
std::size_t output_num = x.size(); std::size_t output_num = x.size();
for(std::size_t i = 0; i < output_num; ++i) for(std::size_t i = 0; i < output_num; ++i)
{ {
verify_args(name, x[i], y[i], tolerance); verify_args(name, x[i], y[i], tolerance);
} }
// std::cout << "cpu: " << x << std::endl;
// std::cout << "gpu: " << y << std::endl;
} }
void verify_instructions(const program& prog, void verify_instructions(const program& prog,
const target& t, const target& t,
compile_options options, compile_options options,
precision quantize,
double tolerance) double tolerance)
{ {
const auto* mm_prog = prog.get_main_module(); const auto* mm_prog = prog.get_main_module();
...@@ -92,7 +100,8 @@ void verify_instructions(const program& prog, ...@@ -92,7 +100,8 @@ void verify_instructions(const program& prog,
{ {
std::cout << "Verify: " << ins.name() << std::endl; std::cout << "Verify: " << ins.name() << std::endl;
std::cout << p << std::endl; std::cout << p << std::endl;
verify_program(ins.name(), p, t, options, create_param_map(p, false), tolerance); verify_program(
ins.name(), p, t, options, quantize, create_param_map(p, false), tolerance);
} }
catch(...) catch(...)
{ {
...@@ -106,6 +115,7 @@ void verify_reduced(program p, ...@@ -106,6 +115,7 @@ void verify_reduced(program p,
int n, int n,
const target& t, const target& t,
compile_options options, compile_options options,
precision quantize,
const parameter_map& inputs, const parameter_map& inputs,
double tolerance) double tolerance)
{ {
...@@ -114,12 +124,13 @@ void verify_reduced(program p, ...@@ -114,12 +124,13 @@ void verify_reduced(program p,
mm->remove_instructions(last, mm->end()); mm->remove_instructions(last, mm->end());
std::cout << "Verify: " << std::endl; std::cout << "Verify: " << std::endl;
std::cout << p << std::endl; std::cout << p << std::endl;
verify_program(std::to_string(n), p, t, options, inputs, tolerance); verify_program(std::to_string(n), p, t, options, quantize, inputs, tolerance);
} }
void verify_reduced_program(const program& p, void verify_reduced_program(const program& p,
const target& t, const target& t,
compile_options options, compile_options options,
precision quantize,
const parameter_map& inputs, const parameter_map& inputs,
double tolerance) double tolerance)
{ {
...@@ -127,7 +138,7 @@ void verify_reduced_program(const program& p, ...@@ -127,7 +138,7 @@ void verify_reduced_program(const program& p,
auto n = std::distance(mm->begin(), mm->end()); auto n = std::distance(mm->begin(), mm->end());
for(std::size_t i = 0; i < n; i++) for(std::size_t i = 0; i < n; i++)
{ {
verify_reduced(p, i, t, options, inputs, tolerance); verify_reduced(p, i, t, options, quantize, inputs, tolerance);
} }
} }
......
#ifndef MIGRAPHX_GUARD_RTGLIB_DRIVER_VERIFY_HPP #ifndef MIGRAPHX_GUARD_RTGLIB_DRIVER_VERIFY_HPP
#define MIGRAPHX_GUARD_RTGLIB_DRIVER_VERIFY_HPP #define MIGRAPHX_GUARD_RTGLIB_DRIVER_VERIFY_HPP
#include "precision.hpp"
#include <migraphx/program.hpp> #include <migraphx/program.hpp>
namespace migraphx { namespace migraphx {
...@@ -11,15 +12,18 @@ void verify_program(const std::string& name, ...@@ -11,15 +12,18 @@ void verify_program(const std::string& name,
const program& p, const program& p,
const target& t, const target& t,
compile_options options = compile_options{}, compile_options options = compile_options{},
precision quantize = precision::fp32,
const parameter_map& inputs = {}, const parameter_map& inputs = {},
double tolerance = 100); double tolerance = 100);
void verify_instructions(const program& prog, void verify_instructions(const program& prog,
const target& t, const target& t,
compile_options options = compile_options{}, compile_options options = compile_options{},
precision quantize = precision::fp32,
double tolerance = 80); double tolerance = 80);
void verify_reduced_program(const program& p, void verify_reduced_program(const program& p,
const target& t, const target& t,
compile_options options = compile_options{}, compile_options options = compile_options{},
precision quantize = precision::fp32,
const parameter_map& inputs = {}, const parameter_map& inputs = {},
double tolerance = 80); double tolerance = 80);
......
...@@ -168,7 +168,8 @@ inline std::string to_string_range(const std::initializer_list<T>& r) ...@@ -168,7 +168,8 @@ inline std::string to_string_range(const std::initializer_list<T>& r)
} }
template <class T> template <class T>
inline std::string to_string(const T& x) inline auto to_string(const T& x)
-> decltype((std::declval<std::stringstream>() << x), std::string{})
{ {
std::stringstream ss; std::stringstream ss;
ss << x; ss << x;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment