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

Change driver verify to check for fp16 and --fp16 #2334

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Changes from 1 commit
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
88 changes: 50 additions & 38 deletions src/driver/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* The MIT License (MIT)
ii The MIT License (MIT)
CharlieL7 marked this conversation as resolved.
Show resolved Hide resolved
*
* Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
*
Expand Down Expand Up @@ -41,6 +41,8 @@
#include <migraphx/json.hpp>
#include <migraphx/version.h>

#include <migraphx/instruction.hpp>
#include <migraphx/module_ref.hpp>
CharlieL7 marked this conversation as resolved.
Show resolved Hide resolved
#include <migraphx/dead_code_elimination.hpp>
#include <migraphx/eliminate_identity.hpp>
#include <migraphx/eliminate_pad.hpp>
Expand Down Expand Up @@ -537,25 +539,57 @@ struct params : command<params>
}
};

/**
* Gives tolerances based on user input (`rms_tol`, `atol`, `rtol` parameters) and defaults.
* Sets to fp16 tolerances if `quantize` input is fp16 or any fp16 instruction in found in the model.
*/
verify::tolerance get_tolerances(const program& p, precision quantize, std::optional<double> rms_tol, std::optional<double> atol, std::optional<double> rtol)
CharlieL7 marked this conversation as resolved.
Show resolved Hide resolved
{
bool has_fp16 = any_of(p.get_modules(), [](auto&& m) {
return any_of(*m, [](auto&& ins) {
return (ins.get_shape().type() == shape::half_type);
umangyadav marked this conversation as resolved.
Show resolved Hide resolved
});
});
migraphx::verify::tolerance result{};
if(has_fp16 or quantize == precision::fp16)
{
result.rms_tol = 8e-2;
result.atol = 4e-2;
result.rtol = 4e-2;
}
if(rms_tol)
{
result.rms_tol = *rms_tol;
}
if(atol)
{
result.atol = *atol;
}
if(rtol)
{
result.rtol = *rtol;
}
return result;
}

struct verify : command<verify>
{
compiler c;
// Set to -1. as nonsense initial value
double rms_tol = -1.0;
double atol = -1.0;
double rtol = -1.0;
std::optional<double> rms_tol;
std::optional<double> atol;
std::optional<double> rtol;
bool per_instruction = false;
bool reduce = false;
void parse(argument_parser& ap)
{
c.parse(ap);
ap(rms_tol, {"--rms-tol"}, ap.help("Tolerance for the RMS error (Default: 0.001)"));
ap(atol,
ap(*rms_tol, {"--rms-tol"}, ap.help("Tolerance for the RMS error"));
ap(*atol,
{"--atol"},
ap.help("Tolerance for the elementwise absolute difference (Default: 0.001)"));
ap(rtol,
ap.help("Tolerance for the elementwise absolute difference"));
ap(*rtol,
{"--rtol"},
ap.help("Tolerance for the elementwise relative difference (Default: 0.001)"));
ap.help("Tolerance for the elementwise relative difference"));
ap(per_instruction,
{"-i", "--per-instruction"},
ap.help("Verify each instruction"),
Expand All @@ -571,34 +605,7 @@ struct verify : command<verify>

auto t = c.ct.get_target();
auto m = c.parameters.generate(p, t, true, c.l.batch);

// TODO remove this and make the driver able to figure out datatype most used in the model
// then set the tolerances appropriately. Need to check here because c.to_fp16 only set
// after argument_parser.parse() is run. This code is complicated because there's not a
// good way to change the default tolerances after reading `--fp16` but before reading
// `--rms-tol`, `--atol`, and `--rtol`.
migraphx::verify::tolerance tols{};
if(c.to_fp16)
{
tols = migraphx::verify::tolerance{8e-2, 4e-2, 4e-2};
}
if(not float_equal(this->rms_tol, -1.0))
{
tols.rms_tol = this->rms_tol;
}
if(not float_equal(this->atol, -1.0))
{
tols.atol = this->atol;
}
if(not float_equal(this->rtol, -1.0))
{
tols.rtol = this->rtol;
}

std::cout << "rms_tol: " << tols.rms_tol << std::endl;
std::cout << "atol: " << tols.atol << std::endl;
std::cout << "rtol: " << tols.rtol << std::endl;


auto quantize = precision::fp32;
if(c.to_fp16)
{
Expand All @@ -609,6 +616,11 @@ struct verify : command<verify>
quantize = precision::int8;
}

auto tols = get_tolerances(p, quantize, rms_tol, atol, rtol);
std::cout << "rms_tol: " << tols.rms_tol << std::endl;
std::cout << "atol: " << tols.atol << std::endl;
std::cout << "rtol: " << tols.rtol << std::endl;

if(per_instruction)
{
verify_instructions(p, t, c.co, quantize, tols);
Expand Down
Loading