Files
TheRock/rocm-libraries/shared/rocroller/test/unit/CostTest.cpp
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

212 lines
7.2 KiB
C++

// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <rocRoller/CodeGen/CopyGenerator.hpp>
#include <rocRoller/Scheduling/Costs/Cost.hpp>
#include <rocRoller/Scheduling/Costs/LinearWeightedCost.hpp>
#include <rocRoller/Scheduling/Costs/MinNopsCost.hpp>
#include <rocRoller/Utilities/Error.hpp>
#include <rocRoller/Utilities/Generator.hpp>
#include "GenericContextFixture.hpp"
#include "SourceMatcher.hpp"
#define Inst(opcode) Instruction(opcode, {}, {}, {}, "")
using namespace rocRoller;
namespace rocRollerTest
{
struct CostTest : public GenericContextFixture
{
GPUArchitectureTarget targetArchitecture() override
{
return {GPUArchitectureGFX::GFX90A};
}
};
TEST_F(CostTest, WaitCntNopCostTest)
{
Instruction inst;
auto cost
= Component::Get<Scheduling::Cost>(Scheduling::CostFunction::WaitCntNop, m_context);
auto const& arch = m_context->targetArchitecture();
{
auto status = Scheduling::InstructionStatus();
EXPECT_NEAR(cost->cost(inst, status), 0.0, 1e-12);
}
{
auto status = Scheduling::InstructionStatus::Nops(1);
EXPECT_GT(cost->cost(inst, status), 0.0);
}
{
auto status = Scheduling::InstructionStatus::Wait(WaitCount::LoadCnt(arch, 3));
EXPECT_GT(cost->cost(inst, status), 0.0);
}
{
auto status = Scheduling::InstructionStatus::Wait(WaitCount::EXPCnt(arch, 3));
EXPECT_GT(cost->cost(inst, status), 0.0);
}
{
auto status = Scheduling::InstructionStatus::Wait(WaitCount::KMCnt(arch, 3));
EXPECT_GT(cost->cost(inst, status), 0.0);
}
}
TEST_F(CostTest, MinNopsCostTest)
{
auto v = createRegisters(Register::Type::Vector, DataType::Float, 6);
auto a = createRegisters(Register::Type::Accumulator, DataType::Float, 2);
auto generator_pre = [&]() -> Generator<Instruction> {
co_yield_(Instruction("v_or_b32", {v[2]}, {v[0], v[1]}, {}, ""));
};
auto generator_one = [&]() -> Generator<Instruction> {
co_yield_(Instruction("s_add_u32", {}, {}, {}, ""));
co_yield_(Instruction("v_mfma_f32_16x16x4f32", {a[0]}, {v[0], v[2], a[0]}, {}, ""));
};
auto generator_two = [&]() -> Generator<Instruction> {
co_yield_(Instruction("v_mfma_f32_16x16x4f32", {a[1]}, {v[0], v[2], a[1]}, {}, ""));
};
auto cost = Component::Get<Scheduling::Cost>(Scheduling::CostFunction::MinNops, m_context);
{
std::vector<Generator<Instruction>> generators;
generators.push_back(generator_one());
generators.push_back(generator_two());
std::vector<Generator<Instruction>::iterator> iterators;
size_t n = generators.size();
iterators.reserve(n);
for(auto& seq : generators)
{
iterators.emplace_back(seq.begin());
}
m_context->schedule(generator_pre());
auto result = (*cost)(iterators);
EXPECT_EQ(result.size(), generators.size());
EXPECT_EQ(std::get<0>(result[0]), 0);
EXPECT_EQ(std::get<0>(result[1]), 1);
EXPECT_EQ(std::get<1>(result[0]), 0);
EXPECT_GT(std::get<1>(result[1]), 0);
++iterators[0];
result = (*cost)(iterators);
EXPECT_EQ(result.size(), generators.size());
EXPECT_EQ(std::get<0>(result[0]), 0);
EXPECT_EQ(std::get<0>(result[1]), 1);
EXPECT_GT(std::get<1>(result[0]), 0);
EXPECT_GT(std::get<1>(result[1]), 0);
}
{
std::vector<Generator<Instruction>> generators;
generators.push_back(generator_two());
generators.push_back(generator_one());
std::vector<Generator<Instruction>::iterator> iterators;
size_t n = generators.size();
iterators.reserve(n);
for(auto& seq : generators)
{
iterators.emplace_back(seq.begin());
}
m_context->schedule(generator_pre());
auto result = (*cost)(iterators);
EXPECT_EQ(result.size(), generators.size());
EXPECT_EQ(std::get<0>(result[0]), 1);
EXPECT_EQ(std::get<0>(result[1]), 0);
EXPECT_EQ(std::get<1>(result[0]), 0);
EXPECT_GT(std::get<1>(result[1]), 0);
++iterators[1];
result = (*cost)(iterators);
EXPECT_EQ(result.size(), generators.size());
EXPECT_EQ(std::get<0>(result[0]), 0);
EXPECT_EQ(std::get<0>(result[1]), 1);
EXPECT_GT(std::get<1>(result[0]), 0);
EXPECT_GT(std::get<1>(result[1]), 0);
}
}
TEST_F(CostTest, UniformCostTest)
{
auto v = createRegisters(Register::Type::Vector, DataType::Float, 6);
auto a = createRegisters(Register::Type::Accumulator, DataType::Float, 2);
auto generator_pre = [&]() -> Generator<Instruction> {
co_yield_(Instruction("v_or_b32", {v[2]}, {v[0], v[1]}, {}, ""));
};
auto generator_one = [&]() -> Generator<Instruction> {
co_yield_(Instruction("s_add_u32", {}, {}, {}, ""));
co_yield_(Instruction("v_mfma_f32_16x16x4f32", {a[0]}, {v[0], v[2], a[0]}, {}, ""));
};
auto generator_two = [&]() -> Generator<Instruction> {
co_yield_(Instruction("v_mfma_f32_16x16x4f32", {a[1]}, {v[0], v[2], a[1]}, {}, ""));
};
auto cost = Component::Get<Scheduling::Cost>(Scheduling::CostFunction::Uniform, m_context);
std::vector<Generator<Instruction>> generators;
generators.push_back(generator_one());
generators.push_back(generator_two());
std::vector<Generator<Instruction>::iterator> iterators;
size_t n = generators.size();
iterators.reserve(n);
for(auto& seq : generators)
{
iterators.emplace_back(seq.begin());
}
m_context->schedule(generator_pre());
auto result = (*cost)(iterators);
EXPECT_EQ(result.size(), n);
for(int i = 0; i < n; i++)
{
EXPECT_EQ(std::get<0>(result[i]), i);
EXPECT_EQ(std::get<1>(result[i]), 0.0);
}
}
TEST_F(CostTest, NoneCostTest)
{
std::shared_ptr<Scheduling::Cost> cost;
EXPECT_THROW(cost
= Component::Get<Scheduling::Cost>(Scheduling::CostFunction::None, m_context),
FatalError);
}
TEST_F(CostTest, LinearWeightedSimple)
{
Component::ComponentFactoryBase::ClearAllCaches();
EXPECT_NE(Component::Get<Scheduling::Cost>(Scheduling::CostFunction::LinearWeightedSimple,
m_context),
nullptr);
}
TEST_F(CostTest, NonexistentSchedulerWeightsFile)
{
Component::ComponentFactoryBase::ClearAllCaches();
Settings::getInstance()->set(Settings::SchedulerWeights, "/dev/null/foo");
EXPECT_THROW(
Component::Get<Scheduling::Cost>(Scheduling::CostFunction::LinearWeighted, m_context),
FatalError);
}
}