Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-tanvir-1211 committed Feb 2, 2024
1 parent 7985cbd commit ec2884c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
8 changes: 7 additions & 1 deletion benchmark/portblas/blas3/trsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ void run(benchmark::State& state, blas::SB_Handle* sb_handle_ptr, char side,
}

std::ostringstream err_stream;
if (!utils::compare_vectors(b_temp, x_ref, err_stream, "", true)) {
const char* en_joint_matrix = std::getenv("SB_ENABLE_JOINT_MATRIX");
if (!utils::compare_vectors(b_temp, x_ref, err_stream, "",
(en_joint_matrix != NULL) &&
(std::is_same<scalar_t, float>::value) &&
(*en_joint_matrix == '1')
? 2
: 1)) {
const std::string& err_str = err_stream.str();
state.SkipWithError(err_str.c_str());
*success = false;
Expand Down
54 changes: 20 additions & 34 deletions common/include/common/float_comparison.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,20 @@ scalar_t clamp_to_limits(scalar_t v) {
* Indicates the tolerated margin for relative differences
*/
template <typename scalar_t>
inline scalar_t getRelativeErrorMargin(const bool is_trsm) {
inline scalar_t getRelativeErrorMargin(const int32_t margin_multiplier = 1) {
/* Measured empirically with gemm. The dimensions of the matrices (even k)
* don't seem to have an impact on the observed relative differences
* In the cases where the relative error is relevant (non close to zero),
* relative differences of up to 0.002 were observed for float
*/
scalar_t margin = 0.005;
if (is_trsm) {
const char* en_joint_matrix = std::getenv("SB_ENABLE_JOINT_MATRIX");
if (en_joint_matrix != NULL && std::is_same<scalar_t, float>::value &&
*en_joint_matrix == '1') {
// increase error margin for mixed precision calculation
// for trsm operator.
margin = 0.009f;
}
}
return margin;
// increase error margin for mixed precision calculation
// for trsm operator.
return margin * margin_multiplier;
}

template <>
inline double getRelativeErrorMargin<double>(const bool) {
inline double getRelativeErrorMargin<double>(const int32_t) {
/* Measured empirically with gemm. The dimensions of the matrices (even k)
* don't seem to have an impact on the observed relative differences
* In the cases where the relative error is relevant (non close to zero),
Expand All @@ -152,7 +145,7 @@ inline double getRelativeErrorMargin<double>(const bool) {
#ifdef BLAS_DATA_TYPE_HALF

template <>
inline cl::sycl::half getRelativeErrorMargin<cl::sycl::half>(const bool) {
inline cl::sycl::half getRelativeErrorMargin<cl::sycl::half>(const int32_t) {
// Measured empirically with gemm
return 0.05f;
}
Expand All @@ -162,27 +155,19 @@ inline cl::sycl::half getRelativeErrorMargin<cl::sycl::half>(const bool) {
* scalars are close to 0)
*/
template <typename scalar_t>
inline scalar_t getAbsoluteErrorMargin(const bool is_trsm) {
inline scalar_t getAbsoluteErrorMargin(const int32_t margin_multiplier = 1) {
/* Measured empirically with gemm.
* In the cases where the relative error is irrelevant (close to zero),
* absolute differences of up to 0.0006 were observed for float
*/
scalar_t margin = 0.001f;
if (is_trsm) {
const char* en_joint_matrix = std::getenv("SB_ENABLE_JOINT_MATRIX");
if (en_joint_matrix != NULL && std::is_same<scalar_t, float>::value &&
*en_joint_matrix == '1') {
// increase error margin for mixed precision calculation
// for trsm operator.
margin = 0.009f;
}
}

return margin;
// increase error margin for mixed precision calculation
// for trsm operator.
return margin * margin_multiplier;
}

template <>
inline double getAbsoluteErrorMargin<double>(const bool) {
inline double getAbsoluteErrorMargin<double>(const int32_t) {
/* Measured empirically with gemm.
* In the cases where the relative error is irrelevant (close to zero),
* absolute differences of up to 10^-12 were observed for double
Expand All @@ -192,7 +177,7 @@ inline double getAbsoluteErrorMargin<double>(const bool) {
#ifdef BLAS_DATA_TYPE_HALF

template <>
inline cl::sycl::half getAbsoluteErrorMargin<cl::sycl::half>(const bool) {
inline cl::sycl::half getAbsoluteErrorMargin<cl::sycl::half>(const int32_t) {
// Measured empirically with gemm.
return 1.0f;
}
Expand All @@ -203,7 +188,7 @@ inline cl::sycl::half getAbsoluteErrorMargin<cl::sycl::half>(const bool) {
*/
template <typename scalar_t, typename epsilon_t = scalar_t>
inline bool almost_equal(scalar_t const& scalar1, scalar_t const& scalar2,
const bool is_trsm = false) {
const int32_t margin_multiplier = 1) {
// Shortcut, also handles case where both are zero
if (scalar1 == scalar2) {
return true;
Expand All @@ -218,13 +203,14 @@ inline bool almost_equal(scalar_t const& scalar1, scalar_t const& scalar2,

// Close to zero, the relative error doesn't work, use absolute error
if (scalar1 == scalar_t{0} || scalar2 == scalar_t{0} ||
absolute_diff < getAbsoluteErrorMargin<epsilon_t>(is_trsm)) {
return (absolute_diff < getAbsoluteErrorMargin<epsilon_t>(is_trsm));
absolute_diff < getAbsoluteErrorMargin<epsilon_t>(margin_multiplier)) {
return (absolute_diff <
getAbsoluteErrorMargin<epsilon_t>(margin_multiplier));
}
// Use relative error
const auto absolute_sum = utils::abs(scalar1) + utils::abs(scalar2);
return (absolute_diff / absolute_sum) <
getRelativeErrorMargin<epsilon_t>(is_trsm);
getRelativeErrorMargin<epsilon_t>(margin_multiplier);
}

/**
Expand All @@ -239,15 +225,15 @@ inline bool compare_vectors(std::vector<scalar_t> const& vec,
std::vector<scalar_t> const& ref,
std::ostream& err_stream = std::cerr,
std::string end_line = "\n",
const bool is_trsm = false) {
const int32_t margin_multiplier = 1) {
if (vec.size() != ref.size()) {
err_stream << "Error: tried to compare vectors of different sizes"
<< std::endl;
return false;
}

for (int i = 0; i < vec.size(); ++i) {
if (!almost_equal<scalar_t, epsilon_t>(vec[i], ref[i], is_trsm)) {
if (!almost_equal<scalar_t, epsilon_t>(vec[i], ref[i], margin_multiplier)) {
err_stream << "Value mismatch at index " << i << ": " << vec[i]
<< "; expected " << ref[i] << end_line;
return false;
Expand All @@ -268,7 +254,7 @@ template <typename scalar_t, typename epsilon_t = scalar_t>
inline bool compare_vectors(std::vector<std::complex<scalar_t>> const& vec,
std::vector<std::complex<scalar_t>> const& ref,
std::ostream& err_stream = std::cerr,
std::string end_line = "\n", bool is_trsm = false) {
std::string end_line = "\n") {
if (vec.size() != ref.size()) {
err_stream << "Error: tried to compare vectors of different sizes"
<< std::endl;
Expand Down
7 changes: 6 additions & 1 deletion test/unittest/blas3/blas3_trsm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ void run_test(const combination_t<scalar_t> combi) {
blas::helper::copy_to_host<scalar_t>(q, b_gpu, B.data(), B.size());
sb_handle.wait(event);

bool isAlmostEqual = utils::compare_vectors(cpu_B, B, std::cerr, "", true);
bool isAlmostEqual = utils::compare_vectors(
cpu_B, B, std::cerr, "",
(en_joint_matrix != NULL) && (std::is_same<scalar_t, float>::value) &&
(*en_joint_matrix == '1')
? 2
: 1);

ASSERT_TRUE(isAlmostEqual);

Expand Down

0 comments on commit ec2884c

Please sign in to comment.