Files
TheRock/rocm-libraries/projects/miopen/test/verify.hpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

249 lines
7.0 KiB
C++

/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2017 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*******************************************************************************/
#ifndef GUARD_VERIFY_HPP
#define GUARD_VERIFY_HPP
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <miopen/float_equal.hpp>
#include <miopen/returns.hpp>
#include <numeric>
#include <miopen/bfloat16.hpp>
using half = half_float::half;
using hip_bfloat16 = bfloat16;
#include <hip_float8.hpp>
#include "tensor_holder.hpp"
namespace miopen {
// Compute the value of a range
template <class R>
using range_value = typename std::decay<decltype(*std::declval<R>().begin())>::type;
struct sum_fn
{
template <class T, class U>
auto operator()(T x, U y) const MIOPEN_RETURNS(x + y);
};
static constexpr sum_fn sum{};
struct max_fn
{
template <class T>
static T id(T x)
{
return x;
}
template <class T, class U>
auto operator()(T x, U y) const MIOPEN_RETURNS(max_fn::id(x > y ? x : y));
};
static constexpr max_fn max{};
namespace abs_diff_detail {
using std::fabs;
struct fn
{
template <class T, class U>
auto operator()(T x, U y) const MIOPEN_RETURNS(fabs(x - y));
};
} // namespace abs_diff_detail
static constexpr abs_diff_detail::fn abs_diff{};
struct not_finite_fn
{
template <class T, typename std::enable_if<(std::is_floating_point_v<T>), bool>::type = false>
bool operator()(T x) const
{
return !std::isfinite(x);
}
template <class T,
typename std::enable_if<
(std::is_same_v<typename std::remove_cv<T>::type, half_float::half>),
bool>::type = false>
bool operator()(T x) const
{
return !half_float::isfinite(x);
}
template <class T,
typename std::enable_if<(std::is_same_v<typename std::remove_cv<T>::type, bfloat16>),
bool>::type = false>
bool operator()(T x) const
{
return !std::isfinite(x); // bfloat16 has float() conversion operator
}
template <class T, typename std::enable_if<(std::is_integral_v<T>), bool>::type = false>
bool operator()(T x) const
{
std::ignore = x;
return false;
}
};
static constexpr not_finite_fn not_finite{};
template <class T, class U>
T as(T, U x)
{
return x;
}
struct compare_mag_fn
{
template <class T, class U>
bool operator()(T x, U y) const
{
using std::fabs;
return fabs(x) < fabs(y);
}
};
static constexpr compare_mag_fn compare_mag{};
struct square_diff_fn
{
template <class T, class U>
double operator()(T x, U y) const
{
double diff = static_cast<double>(x) - static_cast<double>(y);
return diff * diff;
}
};
static constexpr square_diff_fn square_diff{};
template <class T, std::enable_if_t<!std::is_floating_point_v<T>, bool> = true>
bool equal_values(T const& lhs, T const& rhs)
{
return lhs == rhs;
}
template <class T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
bool equal_values(T const& lhs, T const& rhs)
{
return miopen::float_equal_sentinel(lhs, rhs);
}
template <class R1>
bool range_empty(R1&& r1)
{
return r1.begin() == r1.end();
}
template <class R1>
auto range_distance(R1&& r1) MIOPEN_RETURNS(std::distance(r1.begin(), r1.end()));
template <class T>
bool range_zero(const std::vector<T>& r)
{
return std::all_of(r.begin(), r.end(), [](T x) { return equal_values(x, T()); });
}
template <class T>
bool range_zero(const tensor<T>& r)
{
return range_zero(r.data);
}
template <class R1, class R2, class T, class Reducer, class Product>
T range_product(R1&& r1, R2&& r2, T state, Reducer r, Product p)
{
return std::inner_product(r1.begin(), r1.end(), r2.begin(), state, r, p);
}
template <class R1, class R2, class Compare>
std::size_t mismatch_idx(R1&& r1, R2&& r2, Compare compare)
{
auto p = std::mismatch(r1.begin(), r1.end(), r2.begin(), compare);
return std::distance(r1.begin(), p.first);
}
template <class R1, class Predicate>
int64_t find_idx(R1&& r1, Predicate p)
{
auto it = std::find_if(r1.begin(), r1.end(), p);
if(it == r1.end())
return -1;
else
return std::distance(r1.begin(), it);
}
template <class R1, class R2>
double max_diff(R1&& r1, R2&& r2)
{
return range_product(r1, r2, 0.0, max, abs_diff);
}
template <class R1, class R2>
auto max_diff_v2(R1&& r1, R2&& r2)
{
using T = decltype(r1[0] - r2[0]);
auto abs_diff_func = [](auto x, auto y) { return x > y ? x - y : y - x; };
// BUG: deduced wrong datatype, half_float bug
if constexpr(std::is_same_v<T, half_float::detail::expr>)
return range_product(r1, r2, half_float::half(), max, abs_diff_func);
else
return range_product(r1, r2, T(), max, abs_diff_func);
}
template <class R1, class R2, class T>
std::size_t mismatch_diff(R1&& r1, R2&& r2, T diff)
{
return mismatch_idx(
r1,
r2,
std::bind(
float_equal, diff, std::bind(abs_diff, std::placeholders::_1, std::placeholders::_2)));
}
// Reference values are 'ref', observed (measured) values are 'obs'.
template <class RefType, class ObsType>
double rms_range(RefType&& ref, ObsType&& obs)
{
auto ref_size = range_distance(ref);
auto obs_size = range_distance(obs);
if(ref_size != obs_size)
return std::numeric_limits<double>::max();
if(ref_size == 0)
return 0.0;
double n = static_cast<double>(ref_size);
double square_difference = range_product(ref, obs, 0.0, sum_fn{}, square_diff);
double mag1 = static_cast<double>(*std::max_element(ref.begin(), ref.end(), compare_mag));
// If reference is all 0, avoid dividing by 0 when normalizing
mag1 = (mag1 == 0.0) ? 1.0 : std::fabs(mag1);
double normalizer = std::max(mag1, std::numeric_limits<double>::min());
return std::sqrt(square_difference) / (std::sqrt(n) * normalizer);
}
} // namespace miopen
#endif