Files
TheRock/rocm-libraries/shared/tensile/Tensile/Source/client/source/SolutionIterator.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

255 lines
9.5 KiB
C++

/*******************************************************************************
*
* MIT License
*
* Copyright (C) 2020-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* 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 "SolutionIterator.hpp"
#include "ResultReporter.hpp"
namespace Tensile
{
namespace Client
{
std::shared_ptr<SolutionIterator> SolutionIterator::Default(
std::shared_ptr<MasterSolutionLibrary<ContractionProblem>> library,
std::shared_ptr<Hardware> hardware,
po::variables_map const& args)
{
bool bestSolution = args["best-solution"].as<bool>();
if(bestSolution)
{
return std::make_shared<BestSolutionIterator>(library, hardware);
}
else
{
int firstSolutionIdx = args["solution-start-idx"].as<int>();
int numSolutions = args["num-solutions"].as<int>();
return std::make_shared<AllSolutionsIterator>(
library,
hardware,
firstSolutionIdx,
numSolutions,
AllSolutionsIterator::CreateCriteria(library, hardware, args));
}
}
SolutionIterator::SolutionIterator(
std::shared_ptr<MasterSolutionLibrary<ContractionProblem>> library,
std::shared_ptr<Hardware> hardware)
: m_library(library)
, m_hardware(hardware)
{
}
void SolutionIterator::preProblem(ContractionProblem const& problem)
{
m_problem = problem;
}
bool SolutionIterator::checkSolution(ContractionSolution const& solution)
{
if(!(*solution.hardwarePredicate)(*m_hardware))
{
m_reporter->report(ResultKey::Validation, "WRONG_HARDWARE");
if(m_reporter->logAtLevel(LogLevel::Verbose))
{
std::ostringstream msg;
solution.hardwarePredicate->debugEval(*m_hardware, msg);
msg << std::endl;
m_reporter->log(LogLevel::Verbose, msg.str());
}
return false;
}
// Test if the persistent kernel is eligible for the current hw and solution
m_problem.checkPersistentKernelEligibility(solution, *m_hardware);
Task task(*m_hardware, m_problem, solution);
if(!(*solution.problemPredicate)(m_problem) || !(*solution.taskPredicate)(task))
{
m_reporter->report(ResultKey::Validation, "DID_NOT_SATISFY_ASSERTS");
if(m_reporter->logAtLevel(LogLevel::Verbose))
{
std::ostringstream msg;
solution.problemPredicate->debugEval(m_problem, msg);
msg << std::endl;
solution.taskPredicate->debugEval(task, msg);
msg << std::endl;
m_reporter->log(LogLevel::Verbose, msg.str());
}
return false;
}
return true;
}
bool SolutionIterator::runCurrentSolution()
{
auto solution = getSolution();
return checkSolution(*solution);
}
AllSolutionsIterator::RunCriteria AllSolutionsIterator::CreateCriteria(
std::shared_ptr<MasterSolutionLibrary<ContractionProblem>> library,
std::shared_ptr<Hardware> hardware,
po::variables_map const& args)
{
RunCriteria criteria;
double granThresh = args["granularity-threshold"].as<double>();
if(granThresh > 0.0)
{
criteria.push_back([granThresh](ContractionProblem const& problem,
Hardware const& hardware,
ContractionSolution const& solution) {
auto projPerf = solution.projectedPerformance(problem, hardware);
return projPerf.granularities.totalGranularity >= granThresh;
});
}
return criteria;
}
AllSolutionsIterator::AllSolutionsIterator(
std::shared_ptr<MasterSolutionLibrary<ContractionProblem>> library,
std::shared_ptr<Hardware> hardware,
int firstSolutionIdx,
int numSolutions,
RunCriteria runCriteria)
: SolutionIterator(library, hardware)
, m_runCriteria(runCriteria)
{
m_firstSolutionIdx = firstSolutionIdx;
if(m_firstSolutionIdx < 0)
m_firstSolutionIdx = library->solutions.begin()->first;
if(numSolutions < 0)
{
auto iter = library->solutions.rbegin();
m_lastSolutionIdx = iter->first;
}
else
{
m_lastSolutionIdx = m_firstSolutionIdx + numSolutions - 1;
}
m_currentSolutionIdx = m_firstSolutionIdx;
}
void AllSolutionsIterator::preProblem(ContractionProblem const& problem)
{
SolutionIterator::preProblem(problem);
m_currentSolutionIdx = m_firstSolutionIdx;
}
void AllSolutionsIterator::postProblem() {}
void AllSolutionsIterator::preSolution(ContractionSolution const& solution)
{
m_reporter->report(ResultKey::SolutionLibraryIndex, solution.libraryLogicIndex);
m_reporter->report(ResultKey::SolutionIndex, m_currentSolutionIdx);
m_reporter->report(ResultKey::SolutionProgress,
concatenate(m_currentSolutionIdx, "/", m_lastSolutionIdx));
}
void AllSolutionsIterator::postSolution()
{
m_currentSolutionIdx++;
}
bool AllSolutionsIterator::moreSolutionsInProblem() const
{
return m_currentSolutionIdx <= m_lastSolutionIdx;
}
std::shared_ptr<ContractionSolution> AllSolutionsIterator::getSolution()
{
auto iter = m_library->solutions.find(m_currentSolutionIdx);
if(iter == m_library->solutions.end())
return std::shared_ptr<ContractionSolution>();
return iter->second;
}
bool AllSolutionsIterator::runCurrentSolution()
{
auto solution = getSolution();
if(!checkSolution(*solution))
return false;
for(auto const& criterion : m_runCriteria)
{
if(!criterion(m_problem, *m_hardware, *solution))
return false;
}
return true;
}
BestSolutionIterator::BestSolutionIterator(
std::shared_ptr<MasterSolutionLibrary<ContractionProblem>> library,
std::shared_ptr<Hardware> hardware)
: SolutionIterator(library, hardware)
{
}
void BestSolutionIterator::preProblem(ContractionProblem const& problem)
{
SolutionIterator::preProblem(problem);
m_currentSolution = m_library->findBestSolution(m_problem, *m_hardware);
m_usedCurrentSolution = false;
}
void BestSolutionIterator::postProblem() {}
void BestSolutionIterator::preSolution(ContractionSolution const& solution)
{
m_reporter->report(ResultKey::SolutionLibraryIndex, solution.libraryLogicIndex);
m_reporter->report(ResultKey::SolutionIndex, 0);
m_reporter->report(ResultKey::SolutionProgress, "1/1");
}
void BestSolutionIterator::postSolution()
{
m_usedCurrentSolution = true;
}
bool BestSolutionIterator::moreSolutionsInProblem() const
{
return !m_usedCurrentSolution;
}
std::shared_ptr<ContractionSolution> BestSolutionIterator::getSolution()
{
return m_currentSolution;
}
} // namespace Client
} // namespace Tensile