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
162 lines
6.5 KiB
C++
162 lines
6.5 KiB
C++
// Copyright Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include <map>
|
|
#include <unordered_set>
|
|
#include <vector>
|
|
|
|
#include <rocRoller/Expression.hpp>
|
|
#include <rocRoller/ExpressionTransformations.hpp>
|
|
#include <rocRoller/KernelGraph/KernelGraph.hpp>
|
|
#include <rocRoller/KernelGraph/Transforms/LoopOverTileNumbers.hpp>
|
|
#include <rocRoller/KernelGraph/Utils.hpp>
|
|
#include <rocRoller/Utilities/Logging.hpp>
|
|
|
|
namespace rocRoller
|
|
{
|
|
namespace KernelGraph
|
|
{
|
|
using namespace ControlGraph;
|
|
using namespace CoordinateGraph;
|
|
using GD = rocRoller::Graph::Direction;
|
|
|
|
std::string LoopOverTileNumbers::name() const
|
|
{
|
|
return concatenate("LoopOverTileNumbers");
|
|
}
|
|
|
|
LoopOverTileNumbers::LoopOverTileNumbers(std::vector<int> const& dims,
|
|
std::vector<uint> const& tileNumberCoordSizes,
|
|
uint numIteratedTiles,
|
|
std::string const& topLoop,
|
|
ContextPtr context)
|
|
: m_dimensions(dims)
|
|
, m_topLoop(topLoop)
|
|
, m_context(context)
|
|
{
|
|
for(auto s : tileNumberCoordSizes)
|
|
m_tileNumberCoordSizes.push_back(Expression::literal(s));
|
|
m_numIteratedTileNumbers = Expression::literal(numIteratedTiles);
|
|
}
|
|
|
|
//
|
|
// Stage
|
|
//
|
|
// Look for all leaf MacroTileNumbers with matching
|
|
// sub-dimension.
|
|
//
|
|
// Matches are: tile->dim in m_dimensions.
|
|
//
|
|
void LoopOverTileNumbers::stage(KernelGraph const& graph)
|
|
{
|
|
// Find all dangling MacroTileNumber dimensions associated
|
|
// with the requested dimensions
|
|
for(auto dimension : m_dimensions)
|
|
{
|
|
auto danglingTileNumberPredicate = [&](int tag) {
|
|
auto maybeTileNumber = graph.coordinates.get<MacroTileNumber>(tag);
|
|
if(!maybeTileNumber)
|
|
return false;
|
|
if(maybeTileNumber->dim != dimension)
|
|
return false;
|
|
if(std::empty(graph.coordinates.getNeighbours<GD::Upstream>(tag))
|
|
|| std::empty(graph.coordinates.getNeighbours<GD::Downstream>(tag)))
|
|
return true;
|
|
return false;
|
|
};
|
|
|
|
m_tileNumberCoords[dimension]
|
|
= graph.coordinates.findElements(danglingTileNumberPredicate)
|
|
.to<std::unordered_set>();
|
|
}
|
|
}
|
|
|
|
//
|
|
// Commit
|
|
//
|
|
void LoopOverTileNumbers::commit(KernelGraph& graph)
|
|
{
|
|
auto numTotalTiles = Expression::literal(1u);
|
|
for(auto d : m_dimensions)
|
|
numTotalTiles = numTotalTiles * m_tileNumberCoordSizes[d];
|
|
numTotalTiles = simplify(numTotalTiles);
|
|
|
|
auto [forLoopCoord, forLoopOp]
|
|
= rangeFor(graph, m_numIteratedTileNumbers, "TileLoop", DataType::UInt32);
|
|
|
|
// Create forward/reverse tile-numbers for each dimension
|
|
// and attach to all staged tile-number coordinates
|
|
std::vector<int> tileNumbersUp, tileNumbersDown;
|
|
for(auto d : m_dimensions)
|
|
{
|
|
auto up = graph.coordinates.addElement(
|
|
MacroTileNumber(d, m_tileNumberCoordSizes[d], nullptr));
|
|
auto down = graph.coordinates.addElement(
|
|
MacroTileNumber(d, m_tileNumberCoordSizes[d], nullptr));
|
|
|
|
for(auto tileNumTag : m_tileNumberCoords.at(d))
|
|
{
|
|
if(std::empty(graph.coordinates.getNeighbours<GD::Upstream>(tileNumTag)))
|
|
graph.coordinates.addElement(PassThrough(), {down}, {tileNumTag});
|
|
if(std::empty(graph.coordinates.getNeighbours<GD::Downstream>(tileNumTag)))
|
|
graph.coordinates.addElement(PassThrough(), {tileNumTag}, {up});
|
|
}
|
|
|
|
tileNumbersUp.push_back(up);
|
|
tileNumbersDown.push_back(down);
|
|
}
|
|
|
|
// Create foward/reverse transforms from (Workgroup,
|
|
// ForLoop) to new tile-numbers
|
|
auto workgroups = simplify(numTotalTiles / m_numIteratedTileNumbers);
|
|
|
|
auto initUp = graph.coordinates.addElement(MacroTileNumber(0, workgroups, nullptr));
|
|
auto flatUp = graph.coordinates.addElement(Linear(numTotalTiles, nullptr));
|
|
graph.coordinates.addElement(Flatten(), {forLoopCoord, initUp}, {flatUp});
|
|
graph.coordinates.addElement(Tile(), std::vector<int>{flatUp}, tileNumbersDown);
|
|
|
|
auto initDown = graph.coordinates.addElement(MacroTileNumber(0, workgroups, nullptr));
|
|
auto flatDown = graph.coordinates.addElement(Linear(numTotalTiles, nullptr));
|
|
graph.coordinates.addElement(Tile(), {flatDown}, {forLoopCoord, initDown});
|
|
graph.coordinates.addElement(Flatten(), tileNumbersUp, std::vector<int>{flatDown});
|
|
|
|
// Insert ForLoop into graph
|
|
insertWithBody(graph, m_topLoopOp, forLoopOp);
|
|
}
|
|
|
|
//
|
|
// Apply
|
|
//
|
|
KernelGraph LoopOverTileNumbers::apply(KernelGraph const& original)
|
|
{
|
|
TIMER(t, "KernelGraph::LoopOverTileNumbers");
|
|
|
|
// Make sure we can find the top-for-loop location
|
|
auto findTopLoopPredicate = [&](int tag) -> bool {
|
|
auto maybeForLoop = original.control.get<ForLoopOp>(tag);
|
|
if(!maybeForLoop)
|
|
return false;
|
|
if(maybeForLoop->loopName == m_topLoop)
|
|
return true;
|
|
return false;
|
|
};
|
|
auto maybeTopLoopOp = original.control.findNodes(
|
|
*original.control.roots().begin(), findTopLoopPredicate, GD::Downstream);
|
|
if(maybeTopLoopOp.empty())
|
|
{
|
|
rocRoller::Log::getLogger()->warn(
|
|
"Unable to find ForLoop '{}' during LoopOverTileNumbers pass. "
|
|
"LoopOverTileNumbers transform skipped.",
|
|
m_topLoop);
|
|
return original;
|
|
}
|
|
m_topLoopOp = *maybeTopLoopOp.take(1).only();
|
|
|
|
auto graph = original;
|
|
stage(graph);
|
|
commit(graph);
|
|
return graph;
|
|
}
|
|
}
|
|
}
|