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

Adding base class for gtests #3321

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 29 additions & 10 deletions test/gtest/smoke_solver_ConvBinWinograd3x3U.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*******************************************************************************/
#include <tuple>
#include <string_view>

#include "test_base.hpp"
#include "gtest_common.hpp"

#include "../conv2d.hpp"
Expand All @@ -34,19 +34,19 @@ namespace {

auto GetTestCases()
{
const auto env = std::tuple{std::pair{MIOPEN_FIND_MODE, "normal"},
std::pair{MIOPEN_DEBUG_FIND_ONLY_SOLVER, "ConvBinWinograd3x3U"}};

const std::string vf = " --verbose --disable-backward-data --disable-backward-weights";
const std::string vb = " --verbose --disable-forward --disable-backward-weights";

return std::vector{
// clang-format off
//smoke_solver_ConvAsmImplicitGemmV4R1Dynamic
std::pair{env, vf + " --input 1 20 20 20 --weights 20 20 3 3 --pads_strides_dilations 1 1 1 1 1 1"},
std::pair{env, vb + " --input 1 20 20 20 --weights 20 20 3 3 --pads_strides_dilations 1 1 1 1 1 1"}
// clang-format on
};
std::pair{
env,
vf + " --input 1 20 20 20 --weights 20 20 3 3 --pads_strides_dilations 1 1 1 1 1 1"},
std::pair{
env,
vb + " --input 1 20 20 20 --weights 20 20 3 3 --pads_strides_dilations 1 1 1 1 1 1"}};
// clang-format on
}

using TestCase = decltype(GetTestCases())::value_type;
Expand All @@ -64,12 +64,31 @@ bool IsTestSupportedForDevice()

} // namespace

class GPU_Conv2dDefault_FP32 : public FloatTestCase<std::vector<TestCase>>
// Using TestBase
class GPU_Conv2dDefault_FP32 : public TestBase<GPU_Conv2dDefault_FP32>
{
public:
static std::vector<std::string> get_env_vars()
{
return {"MIOPEN_FIND_MODE", "MIOPEN_DEBUG_FIND_ONLY_SOLVER"};
}

static std::map<std::string, std::string> get_env_values()
{
return {{"MIOPEN_FIND_MODE", "normal"},
{"MIOPEN_DEBUG_FIND_ONLY_SOLVER", "ConvBinWinograd3x3U"}};
}
};

// Parameterized test implementation
TEST_P(GPU_Conv2dDefault_FP32, FloatTest_smoke_solver_ConvBinWinograd3x3U)
{
// Set environment variables dynamically
for(const auto& [var, val] : GPU_Conv2dDefault_FP32::get_env_values())
{
miopen::env::setEnvironmentVariable(var, val);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm afraid it doesn't work for cached env variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I have been trying different ways to integrate it without changing gtest_common.hpp - which will require changing large numbers of tests.

I am still looking into ways to do so with seamless integration into gtests...

}

if(IsTestSupportedForDevice() && !SkipTest())
{
invoke_with_params<conv2d_driver, GPU_Conv2dDefault_FP32>(default_check);
Expand All @@ -78,6 +97,6 @@ TEST_P(GPU_Conv2dDefault_FP32, FloatTest_smoke_solver_ConvBinWinograd3x3U)
{
GTEST_SKIP();
}
};
}

INSTANTIATE_TEST_SUITE_P(Smoke, GPU_Conv2dDefault_FP32, testing::Values(GetTestCases()));
74 changes: 74 additions & 0 deletions test/gtest/test_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
*
* MIT License
*
* Copyright (c) 2024 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.
*
*******************************************************************************/

#include <miopen/env.hpp>
#include <gtest/gtest.h>
#include <map>
#include <string>
#include <vector>
#include <random.hpp>

template <typename DerivedTest>
class TestBase : public ::testing::Test
{
protected:
std::map<std::string, std::string> original_env_vars;

void SetUp() override
{
prng::reset_seed();
save_env_vars();
}

void TearDown() override { restore_env_vars(); }

private:
void save_env_vars()
{
std::vector<std::string> env_vars = DerivedTest::get_env_vars();

for(const auto& var : env_vars)
{
auto value = miopen::env::getEnvironmentVariable(var);
original_env_vars[var] = value.value_or("");
}
}

void restore_env_vars()
{
for(const auto& [key, value] : original_env_vars)
{
if(value.empty())
{
miopen::env::clearEnvironmentVariable(key);
}
else
{
miopen::env::setEnvironmentVariable(key, value);
}
}
}
};