Files
TheRock/rocm-libraries/projects/hipblaslt/tensilelite/include/Tensile/GranularitySelectionLibrary.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

289 lines
11 KiB
C++

/*******************************************************************************
*
* MIT License
*
* Copyright (C) 2022-2023 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.
*
*******************************************************************************/
#pragma once
#include <set>
#include <vector>
#include <Tensile/Debug.hpp>
#include <Tensile/PredicateDebugger.hpp>
#include <Tensile/Properties.hpp>
#include <Tensile/Utils.hpp>
#include <Tensile/PropertyMatching.hpp>
#include <tensilelitehost/export.h>
namespace TensileLite
{
/**
* \ingroup SolutionLibrary
*/
struct ExactSelectionTableEntry
{
std::vector<size_t> key;
int value;
};
/**
* \ingroup SolutionLibrary
*
* Compares the tile sizes of each kernel, the dimensions of the problem,
* and the number of compute units on the target GPU to select a kernel
* that fits the best on the GPU with the lowest amount of waste
* ("granularity loss").
*/
template <typename MyProblem, typename MySolution = typename MyProblem::Solution>
struct GranularitySelectionLibrary : public SolutionLibrary<MyProblem, MySolution>
{
std::map<int, std::shared_ptr<MySolution>> solutions;
std::map<std::vector<size_t>, int> exactMap;
static std::string Type()
{
return "GranularitySelection";
}
virtual std::string type() const override
{
return Type();
}
virtual std::string description() const override
{
std::string rv = this->type();
return rv;
}
virtual std::shared_ptr<MySolution> getSolutionByIndex(MyProblem const& problem,
Hardware const& hardware,
const int index) const override
{
if(index >= solutions.size())
{
return std::shared_ptr<MySolution>();
}
return solutions.at(index);
}
virtual std::shared_ptr<MySolution> findBestSolution(MyProblem const& problem,
Hardware const& hardware,
double* fitness
= nullptr) const override
{
const bool debug = Debug::Instance().printPropertyEvaluation();
std::vector<size_t> key;
size_t M = problem.freeSizeA(0);
key.push_back(M);
size_t N = problem.freeSizeB(0);
key.push_back(N);
size_t NumBatches = problem.batchSize(0);
key.push_back(NumBatches);
size_t K = problem.boundSize(0);
key.push_back(K);
auto exactMatch = exactMap.find(key);
if(exactMatch != this->exactMap.end())
{
int index = exactMatch->second;
auto rv = solutions.at(index);
Task task(hardware, problem, *rv);
bool predicateMatch = (*rv->problemPredicate)(problem)
&& (*rv->taskPredicate)(task)
&& (*rv->hardwarePredicate)(hardware);
if(debug)
{
PredicateDebugger::printHeader(
std::cout, "Granularity (Exact): " + rv->name());
rv->hardwarePredicate->debugEval(hardware, std::cout);
rv->problemPredicate->debugEval(problem, std::cout);
rv->taskPredicate->debugEval(task, std::cout);
PredicateDebugger::printFooter(std::cout, predicateMatch);
}
if(predicateMatch)
{
return rv;
}
}
double bestPerformance = 0.0;
std::shared_ptr<MySolution> bestSolution;
for(auto const& row : solutions)
{
auto myPerformance
= row.second->projectedPerformance(problem, hardware).speedGFlops;
if(debug)
{
std::cout << row.second->description() << ": " << myPerformance;
}
if(myPerformance > bestPerformance)
{
Task task(hardware, problem, *(row.second));
bool predicateMatch = (*row.second->problemPredicate)(problem)
&& (*row.second->taskPredicate)(task)
&& (*row.second->hardwarePredicate)(hardware);
if(debug)
{
PredicateDebugger::printHeader(
std::cout, "Granularity: " + row.second->name());
row.second->hardwarePredicate->debugEval(hardware, std::cout);
row.second->problemPredicate->debugEval(problem, std::cout);
row.second->taskPredicate->debugEval(task, std::cout);
PredicateDebugger::printFooter(std::cout, predicateMatch);
}
if(predicateMatch)
{
bestPerformance = myPerformance;
bestSolution = row.second;
}
}
}
return bestSolution;
}
virtual SolutionSet<MySolution>
findAllSolutions(MyProblem const& problem,
Hardware const& hardware,
SolutionLibrarySearchType searchType
= SolutionLibrarySearchType::DEFAULT) const override
{
bool debug = Debug::Instance().printPropertyEvaluation();
SolutionSet<MySolution> rv;
for(auto const& row : solutions)
{
Task task(hardware, problem, *(row.second));
bool predicateMatch = softwarePredicate(searchType, task, hardware, *(row.second), problem)
&& (*row.second->hardwarePredicate)(hardware);
if(debug)
{
PredicateDebugger::printHeader(std::cout, "Granularity: " + row.second->name());
row.second->hardwarePredicate->debugEval(hardware, std::cout);
if(searchType == SolutionLibrarySearchType::DEFAULT)
{
row.second->problemPredicate->debugEval(problem, std::cout);
row.second->taskPredicate->debugEval(task, std::cout);
}
PredicateDebugger::printFooter(std::cout, predicateMatch);
}
if(predicateMatch)
{
rv.insert(row.second);
}
}
return rv;
}
virtual SolutionSet<MySolution>
findAllSolutionsGroupedGemm(std::vector<MyProblem> const& problems,
Hardware const& hardware,
SolutionLibrarySearchType searchType
= SolutionLibrarySearchType::DEFAULT) const override
{
bool debug = Debug::Instance().printPropertyEvaluation();
SolutionSet<MySolution> rv;
for(auto const& row : solutions)
{
bool useSolution = false;
if((*row.second->hardwarePredicate)(hardware))
{
useSolution = true;
if(searchType == SolutionLibrarySearchType::DEFAULT)
{
size_t ws = (*row.second).requiredWorkspaceSizeGroupedGemm(problems, hardware);
for(size_t idx = 0; idx < problems.size(); idx++)
{
auto problem = problems[idx];
Task task(hardware, problem, *(row.second));
problem.setWorkspaceSizeGroupedGemm(ws);
problem.setGroupedGemmCount(problems.size());
if(!(*row.second->problemPredicate)(problem)
|| !(*row.second->taskPredicate)(task))
useSolution = false;
}
}
else if(searchType == SolutionLibrarySearchType::GEMM_TYPE_ONLY)
{
if(!isGemmTypeSame((*row.second), problems[0]))
useSolution = false;
}
if(useSolution)
{
if((*row.second).requiredHostWorkspaceSizePerProblem
== static_cast<size_t>(-1))
{
(*row.second).requiredHostWorkspaceSizePerProblem
= (*row.second)
.requiredHostSizeGroupedGemmSingle(problems[0], hardware);
}
rv.insert(row.second);
}
}
if(debug)
{
PredicateDebugger::printHeader(
std::cout, "Granularity (Grouped): " + row.second->name());
row.second->hardwarePredicate->debugEval(hardware, std::cout);
if(searchType == SolutionLibrarySearchType::DEFAULT)
{
for(size_t idx = 0; idx < problems.size(); idx++)
{
auto problem = problems[idx];
Task task(hardware, problem, *(row.second));
row.second->problemPredicate->debugEval(problem, std::cout);
row.second->taskPredicate->debugEval(task, std::cout);
}
}
PredicateDebugger::printFooter(std::cout, useSolution);
}
}
return rv;
}
};
} // namespace TensileLite