even better more structured throughput scheduling
Validate Operations / validate-operations (push) Has been cancelled
Validate Operations / validate-operations (push) Has been cancelled
plot adjustments validation adjustments
This commit is contained in:
@@ -55,6 +55,11 @@ add_pim_unittest(PimAbstractExecutionTest
|
||||
LINK_LIBS PRIVATE OMPimCompilerUtils
|
||||
)
|
||||
|
||||
add_pim_unittest(PipelineOptimizerTest
|
||||
PipelineOptimizerTest.cpp
|
||||
LINK_LIBS PRIVATE OMPimCompilerUtils
|
||||
)
|
||||
|
||||
add_pim_unittest(SpatialSchedulingTargetTest
|
||||
SpatialSchedulingTargetTest.cpp
|
||||
|
||||
|
||||
@@ -47,6 +47,27 @@ static std::string binary(const std::vector<pim_binary::InstructionRecord> &reco
|
||||
return result;
|
||||
}
|
||||
|
||||
static void testSearchGuidance(PimInstructionTiming target) {
|
||||
PimExecutionProgram program{{{E::Work, 29}, {E::Load, 65}}};
|
||||
PimPeriodicOptions options;
|
||||
options.verifyPeriodicState = true;
|
||||
auto plain = evaluatePimExecution(program, target, options);
|
||||
options.searchGuidance = true;
|
||||
auto guided = evaluatePimExecution(program, target, options);
|
||||
TEST_CHECK(plain.cost.primary == guided.cost.primary);
|
||||
TEST_CHECK(plain.cost.alternative == guided.cost.alternative);
|
||||
TEST_CHECK(plain.cost.convergenceProved() == guided.cost.convergenceProved());
|
||||
TEST_CHECK(guided.cost.processorTaskWorkNs.size() == target.processorCount);
|
||||
TEST_CHECK(guided.cost.processorTaskWorkNs.front() == 29);
|
||||
TEST_CHECK(guided.cost.processorCriticalDelayNs.size() == target.processorCount);
|
||||
TEST_CHECK(guided.cost.processorCriticalDelayNs.front() > 0);
|
||||
program.front()[1].constant = true;
|
||||
auto constants = evaluatePimExecution(program, target, options);
|
||||
TEST_CHECK(constants.cost.primary == plain.cost.primary);
|
||||
TEST_CHECK(constants.cost.alternative == plain.cost.alternative);
|
||||
TEST_CHECK(constants.cost.processorTaskWorkNs.front() > 29);
|
||||
}
|
||||
|
||||
static void testTypedResult(PimInstructionTiming target) {
|
||||
PimPeriodicOptions options;
|
||||
options.verifyPeriodicState = true;
|
||||
@@ -496,10 +517,14 @@ static void testReadOnlySpatial(PimInstructionTiming target, bool batched) {
|
||||
TEST_CHECK((*classes)[E::LocalCopy].getAsInteger() == 0);
|
||||
TEST_CHECK((*classes)[E::ScalarIssue].getAsInteger() == 0);
|
||||
TEST_CHECK(totalWork == report.getInteger("compute_and_local_work_ns"));
|
||||
auto softmax = spatial::SpatSoftmaxOp::create(builder, loc, type, input);
|
||||
auto softmax = spatial::SpatSoftmaxOp::create(builder, loc, type, local);
|
||||
auto secondSoftmax = spatial::SpatSoftmaxOp::create(builder, loc, type, softmax.getOutput());
|
||||
TEST_CHECK(succeeded(verify(module)));
|
||||
TEST_CHECK(analyzePimSpatialSchedule(module, target, spatialDetails()).getString("status") == "unavailable");
|
||||
softmax.erase();
|
||||
auto softmaxReport = analyzePimSpatialSchedule(module, target, spatialDetails());
|
||||
TEST_CHECK(softmaxReport.getString("status") == "estimated");
|
||||
TEST_CHECK(softmaxReport.getInteger("compute_and_local_work_ns") == totalWork);
|
||||
TEST_CHECK(softmaxReport.getInteger("global_read_requests") == report.getInteger("global_read_requests"));
|
||||
secondSoftmax.erase(); softmax.erase();
|
||||
source = tensor::EmptyOp::create(builder, loc, ArrayRef<int64_t>{1, 0, 1, 224}, builder.getF32Type());
|
||||
destination = tensor::EmptyOp::create(builder, loc, ArrayRef<int64_t>{1, 1, 224, 0}, builder.getF32Type());
|
||||
linalg::TransposeOp::create(builder, loc, source, destination, ArrayRef<int64_t>{0, 2, 3, 1});
|
||||
@@ -623,6 +648,40 @@ static void testPublicationAndOutput(PimInstructionTiming target) {
|
||||
TEST_CHECK((*(*runs)[0].getAsArray())[1].getAsInteger() == 60);
|
||||
TEST_CHECK((*(*runs)[1].getAsArray())[1].getAsInteger() == 5);
|
||||
TEST_CHECK((*(*runs)[1].getAsArray())[2].getAsInteger() == 4);
|
||||
|
||||
// Ablation removes standalone and fused synchronization, not host accesses.
|
||||
builder.setInsertionPoint(compute.getBody().front().getTerminator());
|
||||
Value readerId = arith::ConstantIndexOp::create(builder, loc, 1);
|
||||
Value eventRegister = arith::ConstantIndexOp::create(builder, loc, 2);
|
||||
spatial::SpatSyncOp::create(builder, loc, readerId, eventRegister);
|
||||
builder.setInsertionPoint(reader.getBody().front().getTerminator());
|
||||
eventRegister = arith::ConstantIndexOp::create(builder, loc, 2);
|
||||
spatial::SpatHostWaitLoadOp::create(builder, loc, slice.getType(),
|
||||
zero, one, zero, eventRegister, one);
|
||||
TEST_CHECK(succeeded(verify(module)));
|
||||
PimExecutionProgram reference, omitted;
|
||||
auto options = spatialDetails();
|
||||
options.executionTrace = &reference;
|
||||
auto synchronized = evaluatePimSpatialSchedule(module, target, options);
|
||||
TEST_CHECK(synchronized.cost.available());
|
||||
options.omitSynchronization = true;
|
||||
options.executionTrace = &omitted;
|
||||
auto ablation = evaluatePimSpatialSchedule(module, target, options);
|
||||
TEST_CHECK(ablation.cost.available());
|
||||
TEST_CHECK(ablation.report.getObject("event_counts")->getInteger("sync") == 0);
|
||||
TEST_CHECK(ablation.report.getObject("event_counts")->getInteger("wait") == 0);
|
||||
for (const char *metric : {"global_read_requests", "global_read_bytes",
|
||||
"global_write_requests", "global_write_bytes", "global_memory_service_ns"})
|
||||
TEST_CHECK(ablation.report.getInteger(metric) == synchronized.report.getInteger(metric));
|
||||
for (auto &sequence : reference)
|
||||
llvm::erase_if(sequence, [](const E &event) { return event.kind == E::Signal || event.kind == E::Wait; });
|
||||
for (const auto &sequence : omitted)
|
||||
for (const E &event : sequence)
|
||||
TEST_CHECK(event.kind != E::Signal && event.kind != E::Wait);
|
||||
options.executionTrace = nullptr;
|
||||
auto expected = evaluatePimExecution(reference, target, options);
|
||||
TEST_CHECK(ablation.cost.primary == expected.cost.primary);
|
||||
TEST_CHECK(ablation.cost.alternative == expected.cost.alternative);
|
||||
module.erase();
|
||||
}
|
||||
|
||||
@@ -850,6 +909,7 @@ int main() {
|
||||
for (int64_t period : {1, 5}) {
|
||||
testLateRecurrenceAuthority(hardware(period));
|
||||
testTypedResult(hardware(period));
|
||||
testSearchGuidance(hardware(period));
|
||||
testSharedEngine(hardware(period));
|
||||
testAccountingAndOverflow(hardware(period));
|
||||
testMatrixGeometry(hardware(period));
|
||||
|
||||
@@ -53,8 +53,8 @@ static void testEmptyGraphPreparation() {
|
||||
SchedulingTarget target;
|
||||
target.processorCount = target.residentWeightCapacity = 1;
|
||||
target.interProcessorLatencyNs = {0};
|
||||
for (bool collectCandidates : {false, true}) {
|
||||
auto prepared = prepareSpatialSchedules(*module, target, 1, collectCandidates);
|
||||
{
|
||||
auto prepared = prepareSpatialSchedules(*module, target, 1);
|
||||
TEST_CHECK(succeeded(prepared) && prepared->candidates.size() == 1);
|
||||
TEST_CHECK(prepared->candidates.front().dominanceOrderCompute.empty());
|
||||
}
|
||||
@@ -109,9 +109,9 @@ static void testSelectedLoweringFailure() {
|
||||
bufferization::func_ext::registerBufferizableOpInterfaceExternalModels(registry);
|
||||
context.appendDialectRegistry(registry);
|
||||
context.loadAllAvailableDialects();
|
||||
for (bool collectCandidates : {false, true}) {
|
||||
{
|
||||
OwningOpRef<ModuleOp> checkpoint = cast<ModuleOp>((*module)->clone());
|
||||
auto prepared = prepareSpatialSchedules(*checkpoint, target, 2, collectCandidates);
|
||||
auto prepared = prepareSpatialSchedules(*checkpoint, target, 2);
|
||||
TEST_CHECK(succeeded(prepared) && !prepared->candidates.empty());
|
||||
}
|
||||
PassManager manager(&context);
|
||||
@@ -233,6 +233,14 @@ static void testResourceRetries() {
|
||||
}, reason);
|
||||
TEST_CHECK(!selected && attempts == 1);
|
||||
TEST_CHECK(reason.find("recovery is not justified") != std::string::npos);
|
||||
// The original schedule succeeds alone. Adding unknown evidence must produce
|
||||
// an explicit incomplete comparison, never silently drop the alternative.
|
||||
attempts = 0;
|
||||
std::vector<WholeScheduleCost> single{original};
|
||||
selected = lowerSelectedPimCandidate(single, [&](size_t index) {
|
||||
++attempts; TEST_CHECK(index == 0); return PimCandidateResult::Success;
|
||||
}, reason);
|
||||
TEST_CHECK(selected == 0 && attempts == 1);
|
||||
for (unsigned unknown = 0; unknown < 4; ++unknown) {
|
||||
auto invalid = faster;
|
||||
if (unknown == 0) invalid.primary.reset();
|
||||
@@ -246,6 +254,7 @@ static void testResourceRetries() {
|
||||
return PimCandidateResult::Success;
|
||||
}, reason);
|
||||
TEST_CHECK(!selected && attempts == 0);
|
||||
TEST_CHECK(reason.find("cannot compare the complete candidate set") != std::string::npos);
|
||||
}
|
||||
coresCount = previousCores; crossbarSize = previousSize; crossbarCountInCore = previousCrossbars;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
#include "TestCheck.hpp"
|
||||
#include "src/Accelerators/PIM/Compiler/PimPeriodicAnalysis.hpp"
|
||||
#include "src/Accelerators/PIM/Compiler/PimInstructionTiming.hpp"
|
||||
#include "src/Accelerators/PIM/Dialect/Spatial/Passes/Transforms/MergeComputeNodes/Scheduling/PipelineScheduling.hpp"
|
||||
#include "src/Accelerators/PIM/Dialect/Spatial/Passes/Transforms/MergeComputeNodes/Scheduling/ScheduleEvaluation.hpp"
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
|
||||
using namespace onnx_mlir;
|
||||
using namespace onnx_mlir::spatial;
|
||||
using E = PimExecutionEvent;
|
||||
static bool sawJointMove = false, sawSuccessiveMoves = false;
|
||||
|
||||
// Test-only workloads use stage-boundary host transfers and local direct links.
|
||||
// The same queue
|
||||
// engine and complete-state evidence rank reference and searched schedules.
|
||||
static WholeScheduleCost queueCost(const ComputeGraph &graph,
|
||||
const MergeScheduleResult &schedule, const PimInstructionTiming &target) {
|
||||
PimExecutionProgram program(target.processorCount);
|
||||
std::vector<std::vector<size_t>> tasks(target.processorCount);
|
||||
for (size_t task = 0; task < graph.nodes.size(); ++task)
|
||||
tasks[schedule.computeToCpuMap.lookup(graph.nodes[task].instance)].push_back(task);
|
||||
for (auto &core : tasks)
|
||||
llvm::sort(core, [&](size_t a, size_t b) {
|
||||
return schedule.computeToCpuSlotMap.lookup(graph.nodes[a].instance)
|
||||
< schedule.computeToCpuSlotMap.lookup(graph.nodes[b].instance);
|
||||
});
|
||||
struct Transfer { size_t source, target, reader, writer, reg; bool host; };
|
||||
std::vector<Transfer> transfers;
|
||||
std::vector<std::set<size_t>> readers(target.processorCount), writers(target.processorCount);
|
||||
// Branched synthetic workloads use host routing to avoid inventing a second
|
||||
// blocking-transfer scheduler in this test-only objective.
|
||||
bool branching = llvm::any_of(graph.successors, [](const auto &edges) { return edges.size() > 1; });
|
||||
for (size_t task = 0; task < graph.nodes.size(); ++task)
|
||||
for (const auto &[predecessor, dependency] : graph.predecessors[task]) {
|
||||
size_t writer = schedule.computeToCpuMap.lookup(graph.nodes[predecessor].instance);
|
||||
size_t reader = schedule.computeToCpuMap.lookup(graph.nodes[task].instance);
|
||||
if (writer == reader) continue;
|
||||
bool host = branching || schedule.processorStages[writer] != schedule.processorStages[reader];
|
||||
transfers.push_back({predecessor, task, reader, writer, 1 + transfers.size(), host});
|
||||
if (host) { readers[writer].insert(reader); writers[reader].insert(writer); }
|
||||
}
|
||||
for (size_t core = 0; core < program.size(); ++core) {
|
||||
auto &events = program[core];
|
||||
for (size_t writer : writers[core]) events.emplace_back(E::Signal, 0, writer, 0, 1);
|
||||
if (schedule.processorStages[core] == 0) {
|
||||
size_t peers = 0;
|
||||
for (size_t peer = 0; peer < program.size(); ++peer)
|
||||
if (peer != core && schedule.processorStages[peer] == 0) {
|
||||
events.emplace_back(E::Signal, 0, peer, 100, 1); ++peers;
|
||||
}
|
||||
if (peers) events.emplace_back(E::Wait, 0, 0, 100, peers);
|
||||
}
|
||||
bool reuseWait = false;
|
||||
for (size_t task : tasks[core]) {
|
||||
for (const auto &transfer : transfers)
|
||||
if (transfer.target == task) {
|
||||
if (transfer.host) {
|
||||
events.emplace_back(E::Wait, 0, 0, transfer.reg, 1);
|
||||
events.emplace_back(E::Load, 64);
|
||||
} else events.emplace_back(E::Receive, 64, transfer.writer);
|
||||
}
|
||||
E work(E::Work, graph.nodes[task].cost); work.workClass = E::Vector;
|
||||
events.push_back(work);
|
||||
for (const auto &transfer : transfers)
|
||||
if (transfer.source == task) {
|
||||
if (!transfer.host) {
|
||||
events.emplace_back(E::Send, 64, transfer.reader); continue;
|
||||
}
|
||||
if (!reuseWait) {
|
||||
events.emplace_back(E::Wait, 0, 0, 0, readers[core].size()); reuseWait = true;
|
||||
}
|
||||
events.emplace_back(E::Store, 64);
|
||||
events.emplace_back(E::Signal, 0, transfer.reader, transfer.reg, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
PimPeriodicOptions options;
|
||||
options.verifyPeriodicState = true; options.searchGuidance = true;
|
||||
auto estimate = evaluatePimExecution(program, target, options);
|
||||
auto cost = std::move(estimate.cost);
|
||||
cost.loweringSupport = StaticLoweringSupport::Supported;
|
||||
cost.selectionMargin = 0.021;
|
||||
TEST_CHECK(cost.eligible());
|
||||
return cost;
|
||||
}
|
||||
|
||||
static PimInstructionTiming hardware(size_t profile) {
|
||||
PimInstructionTiming target;
|
||||
target.processorCount = 5;
|
||||
target.residentWeightCapacity = 2;
|
||||
target.estimatePipelineDependencySpan = [](llvm::ArrayRef<Cost> spans) { return spans.front(); };
|
||||
target.peerPermissionThenSourceRead = true;
|
||||
target.signalIssueNs = profile + 1;
|
||||
target.controlMessageBytes = 1;
|
||||
target.sharedMemoryWidthBytes = 64;
|
||||
target.sharedMemoryReadLatencyNs = 17 + profile * 9;
|
||||
target.sharedMemoryWriteLatencyNs = 23 + profile * 7;
|
||||
target.synchronizationRegisterCount = 128;
|
||||
target.sharedMemoryRequestLatencyNs.assign(5, 2);
|
||||
target.sharedMemoryResponseLatencyNs.assign(5, 3);
|
||||
target.interProcessorLatencyNs.resize(25);
|
||||
for (size_t a = 0; a < 5; ++a)
|
||||
for (size_t b = 0; b < 5; ++b) {
|
||||
const size_t irregular[] = {0, 1, 5, 6, 20};
|
||||
size_t x = profile ? irregular[a] : a, y = profile ? irregular[b] : b;
|
||||
target.interProcessorLatencyNs[a * 5 + b] = x > y ? x - y : y - x;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
static void runReference(size_t profile, size_t fixture) {
|
||||
auto target = hardware(profile);
|
||||
ComputeGraph graph;
|
||||
const std::vector<std::vector<Cost>> work{{900, 70, 800, 90, 700},
|
||||
{30, 700, 110, 600, 90}, {1000, 50, 40, 900, 60},
|
||||
{90, 80, 100, 110, 120}, {100, 800, 100, 800, 100},
|
||||
{900, 800, 40, 30, 20}};
|
||||
size_t count = work[fixture].size();
|
||||
graph.predecessors.resize(count); graph.successors.resize(count);
|
||||
for (size_t task = 0; task < count; ++task) {
|
||||
ResidentWeight weight; weight.opaqueLane = fixture == 4 ? task % 3 : task % 2;
|
||||
graph.nodes.push_back({{nullptr, static_cast<uint32_t>(task), 1}, work[fixture][task],
|
||||
{weight}, task});
|
||||
auto edge = [&](size_t source) {
|
||||
graph.predecessors[task].push_back({source, {64, 1}});
|
||||
graph.successors[source].push_back({task, {64, 1}});
|
||||
graph.publicationTransfers.push_back({source, task, {}, 0, {}, 64, 1});
|
||||
};
|
||||
if (fixture == 1) {
|
||||
if (task == 1 || task == 2) edge(0);
|
||||
if (task == 3) { edge(1); edge(2); }
|
||||
if (task == 4) edge(3);
|
||||
} else if (task) edge(task - 1);
|
||||
if (fixture == 2 && task == 4) edge(1);
|
||||
}
|
||||
if (fixture == 3) {
|
||||
target.sharedMemoryReadLatencyNs = 400;
|
||||
target.sharedMemoryWriteLatencyNs = 600;
|
||||
}
|
||||
std::string error;
|
||||
MergeScheduleResult initial;
|
||||
TEST_CHECK(mlir::succeeded(runPipelineScheduler(graph, initial, 3, target, error)));
|
||||
auto evaluate = [&](const MergeScheduleResult &schedule) { return queueCost(graph, schedule, target); };
|
||||
auto begin = std::chrono::steady_clock::now();
|
||||
auto search = optimizePipelineSchedule(graph, initial, 3, target, evaluate, error);
|
||||
double searchSeconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - begin).count();
|
||||
TEST_CHECK(mlir::succeeded(search));
|
||||
auto repeat = optimizePipelineSchedule(graph, initial, 3, target, evaluate, error);
|
||||
TEST_CHECK(mlir::succeeded(repeat) && search->schedules.size() == repeat->schedules.size());
|
||||
for (size_t i = 0; i < search->schedules.size(); ++i) {
|
||||
TEST_CHECK(search->schedules[i].computeToCpuMap == repeat->schedules[i].computeToCpuMap);
|
||||
TEST_CHECK(search->schedules[i].computeToCpuSlotMap == repeat->schedules[i].computeToCpuSlotMap);
|
||||
TEST_CHECK(search->schedules[i].processorStages == repeat->schedules[i].processorStages);
|
||||
TEST_CHECK(search->costs.empty() || search->costs[i].primary == repeat->costs[i].primary);
|
||||
TEST_CHECK(search->costs.empty() || search->costs[i].alternative == repeat->costs[i].alternative);
|
||||
}
|
||||
double reference = std::numeric_limits<double>::infinity();
|
||||
size_t referenceEvaluations = 0;
|
||||
std::vector<size_t> stages(count), budgets(3, 1);
|
||||
std::function<void(size_t)> partitions = [&](size_t task) {
|
||||
if (task != count) {
|
||||
for (size_t stage = 0; stage <= (task ? 2 : 0); ++stage) {
|
||||
stages[task] = stage; partitions(task + 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
std::vector<bool> used(3);
|
||||
for (size_t stage : stages) used[stage] = true;
|
||||
if (used[2] && !used[1]) return;
|
||||
for (size_t core0 = 1; core0 + 1 < target.processorCount; ++core0)
|
||||
for (size_t core1 = 1; core0 + core1 < target.processorCount; ++core1) {
|
||||
budgets = {core0, core1, target.processorCount - core0 - core1};
|
||||
MergeScheduleResult schedule;
|
||||
if (mlir::failed(packPipelineSchedule(graph, stages, budgets, target, schedule, error))) continue;
|
||||
auto cost = evaluate(schedule); ++referenceEvaluations;
|
||||
reference = std::min(reference, std::max(*cost.primary, *cost.alternative));
|
||||
}
|
||||
};
|
||||
auto referenceBegin = std::chrono::steady_clock::now();
|
||||
partitions(0);
|
||||
double referenceSeconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - referenceBegin).count();
|
||||
std::string reason;
|
||||
size_t winner = search->costs.empty() ? 0 : selectPipelineCandidate(search->costs, reason);
|
||||
auto selected = evaluate(search->schedules[winner]);
|
||||
double cost = std::max(*selected.primary, *selected.alternative);
|
||||
double bestEvaluated = cost;
|
||||
for (const auto &estimate : search->costs)
|
||||
bestEvaluated = std::min(bestEvaluated, std::max(*estimate.primary, *estimate.alternative));
|
||||
TEST_CHECK(std::isfinite(reference) && cost + 1e-6 >= reference);
|
||||
TEST_CHECK(bestEvaluated + 1e-6 >= reference);
|
||||
TEST_CHECK(cost <= reference * (1 + kPipelineSelectionMargin) + 1e-6);
|
||||
TEST_CHECK(search->costs.size() <= kPipelineQueueEvaluationBudget
|
||||
&& search->packingAttempts <= 4 * kPipelineQueueEvaluationBudget
|
||||
&& search->consideredAssignments <= 4 * kPipelineQueueEvaluationBudget);
|
||||
size_t depth = 0;
|
||||
for (size_t i = winner; i; i = search->parents[i]) {
|
||||
++depth;
|
||||
const auto ¤t = search->schedules[i], &parent = search->schedules[search->parents[i]];
|
||||
bool cutChanged = false;
|
||||
for (const auto &node : graph.nodes)
|
||||
cutChanged |= current.processorStages[current.computeToCpuMap.lookup(node.instance)]
|
||||
!= parent.processorStages[parent.computeToCpuMap.lookup(node.instance)];
|
||||
std::vector<size_t> currentBudgets(3), parentBudgets(3);
|
||||
for (size_t core = 0; core < target.processorCount; ++core) {
|
||||
++currentBudgets[current.processorStages[core]];
|
||||
++parentBudgets[parent.processorStages[core]];
|
||||
}
|
||||
sawJointMove |= cutChanged && currentBudgets != parentBudgets;
|
||||
}
|
||||
sawSuccessiveMoves |= depth >= 2;
|
||||
std::cout << "profile=" << profile << " fixture=" << fixture
|
||||
<< " reference_ns=" << reference << " selected_ns=" << cost
|
||||
<< " gap_percent=" << 100 * (cost / reference - 1)
|
||||
<< " best_evaluated_ns=" << bestEvaluated
|
||||
<< " best_evaluated_gap_percent=" << 100 * (bestEvaluated / reference - 1)
|
||||
<< " reference_evaluations=" << referenceEvaluations
|
||||
<< " search_evaluations=" << search->costs.size() << " refinement_depth=" << depth
|
||||
<< " search_seconds=" << searchSeconds << " reference_seconds=" << referenceSeconds
|
||||
<< " total_seconds=" << std::chrono::duration<double>(std::chrono::steady_clock::now() - begin).count()
|
||||
<< '\n';
|
||||
auto unavailable = [&](const MergeScheduleResult &) { WholeScheduleCost value; value.reason = "test unavailable timing"; return value; };
|
||||
auto unknown = optimizePipelineSchedule(graph, initial, 3, target, unavailable, error);
|
||||
TEST_CHECK(search->costs.empty() || (mlir::failed(unknown) && error.find("test unavailable timing") != std::string::npos));
|
||||
auto unproved = [&](const MergeScheduleResult &schedule) {
|
||||
auto cost = evaluate(schedule);
|
||||
cost.recurrence[0].status = ScheduleRecurrenceEvidence::Unproved;
|
||||
cost.reason = "test unproved recurrence";
|
||||
return cost;
|
||||
};
|
||||
auto invalid = optimizePipelineSchedule(graph, initial, 3, target, unproved, error);
|
||||
TEST_CHECK(search->costs.empty() || (mlir::failed(invalid) && error.find("test unproved recurrence") != std::string::npos));
|
||||
if (!profile && !fixture) {
|
||||
auto badGuidance = [&](const MergeScheduleResult &schedule) {
|
||||
auto cost = evaluate(schedule); cost.processorTaskWorkNs.resize(target.processorCount + 1);
|
||||
return cost;
|
||||
};
|
||||
TEST_CHECK(mlir::failed(optimizePipelineSchedule(graph, initial, 3, target, badGuidance, error)));
|
||||
TEST_CHECK(error.find("physical processor count") != std::string::npos);
|
||||
TEST_CHECK(mlir::failed(optimizePipelineSchedule(graph, initial, 3, target, evaluate, error, 0)));
|
||||
TEST_CHECK(mlir::failed(optimizePipelineSchedule(graph, initial, 3, target, evaluate, error, kPipelineQueueEvaluationBudget + 1)));
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
for (size_t profile = 0; profile < 2; ++profile)
|
||||
for (size_t fixture = 0; fixture < 6; ++fixture) runReference(profile, fixture);
|
||||
TEST_CHECK(sawJointMove && sawSuccessiveMoves);
|
||||
}
|
||||
@@ -74,7 +74,7 @@ static void checkProjection(ArrayRef<int64_t> shape, ArrayRef<int64_t> sizes,
|
||||
target.residentWeightCapacity = 1;
|
||||
target.interProcessorLatencyNs = {0};
|
||||
function.setName("main_graph");
|
||||
auto schedules = prepareSpatialSchedules(*module, target, 1, false);
|
||||
auto schedules = prepareSpatialSchedules(*module, target, 1);
|
||||
TEST_CHECK(succeeded(schedules) && schedules->candidates.size() == 1);
|
||||
IRMapping mapping;
|
||||
OwningOpRef<ModuleOp> scheduled = cast<ModuleOp>(module->getOperation()->clone(mapping));
|
||||
|
||||
@@ -578,9 +578,6 @@ static void testPipelineStageSpan() {
|
||||
assert(peer != core);
|
||||
assert(schedule.processorStages[core] == schedule.processorStages[peer]);
|
||||
}
|
||||
for (size_t stage = 0; stage < 2; ++stage)
|
||||
assert(stageWork[stage] <= 10 * static_cast<size_t>(
|
||||
llvm::count(schedule.processorStages, stage)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1617,6 +1614,57 @@ static void testBaselineConstantMemory() {
|
||||
assert(graph.baselineMemory.serviceNs == 46);
|
||||
}
|
||||
|
||||
static void testOrderedPackCostCurve() {
|
||||
// Heterogeneous weights, reuse, plateaus, capacity cuts, and excess cores.
|
||||
const std::vector<std::vector<size_t>> weights{{0, 1}, {1}, {2}, {}, {0, 2}, {1}};
|
||||
const std::vector<Cost> costs{7, 3, 11, 0, 5, 7};
|
||||
const std::vector<size_t> tasks{0, 1, 2, 3, 4, 5};
|
||||
for (size_t capacity : {size_t{2}, size_t{3}}) {
|
||||
Cost previous = std::numeric_limits<Cost>::max();
|
||||
bool plateau = false;
|
||||
for (size_t cores = 1; cores <= tasks.size() + 3; ++cores) {
|
||||
// Enumerate all ordered partitions for an independent small reference.
|
||||
Cost optimum = std::numeric_limits<Cost>::max();
|
||||
for (size_t cuts = 0; cuts < (size_t{1} << (tasks.size() - 1)); ++cuts) {
|
||||
size_t packs = 1;
|
||||
Cost work = 0, maximum = 0;
|
||||
llvm::SmallBitVector resident(3);
|
||||
bool fits = true;
|
||||
for (size_t task : tasks) {
|
||||
if (task && (cuts & (size_t{1} << (task - 1)))) {
|
||||
maximum = std::max(maximum, work);
|
||||
work = 0; resident.reset(); ++packs;
|
||||
}
|
||||
work += costs[task];
|
||||
for (size_t weight : weights[task]) resident.set(weight);
|
||||
fits &= resident.count() <= capacity;
|
||||
}
|
||||
if (fits && packs <= cores) optimum = std::min(optimum, std::max(maximum, work));
|
||||
}
|
||||
std::vector<size_t> assignment;
|
||||
Cost cost = estimateOrderedPackCost(costs, weights, 3, tasks, capacity, cores, &assignment);
|
||||
if (optimum == std::numeric_limits<Cost>::max()) {
|
||||
assert(assignment.empty());
|
||||
continue;
|
||||
}
|
||||
assert(cost == optimum && cost <= previous && assignment.size() == tasks.size());
|
||||
plateau |= cost == previous;
|
||||
previous = cost;
|
||||
std::vector<Cost> loads(cores);
|
||||
std::vector<llvm::SmallBitVector> resident(cores, llvm::SmallBitVector(3));
|
||||
for (size_t task : tasks) {
|
||||
assert(assignment[task] < cores);
|
||||
loads[assignment[task]] += costs[task];
|
||||
for (size_t weight : weights[task]) resident[assignment[task]].set(weight);
|
||||
}
|
||||
for (size_t core = 0; core < cores; ++core) {
|
||||
assert(loads[core] <= cost && resident[core].count() <= capacity);
|
||||
}
|
||||
}
|
||||
assert(plateau && previous == 11);
|
||||
}
|
||||
}
|
||||
|
||||
static void testPipelineSharedMemory() {
|
||||
mlir::MLIRContext context(mlir::MLIRContext::Threading::DISABLED);
|
||||
auto fragment = mlir::RankedTensorType::get({33}, mlir::IntegerType::get(&context, 8));
|
||||
@@ -1628,20 +1676,6 @@ static void testPipelineSharedMemory() {
|
||||
assert(shared != getHostPublicationIdentity(firstGeneration.getAsOpaquePointer(), 1, fragment, true));
|
||||
assert(shared != getHostPublicationIdentity(nextGeneration.getAsOpaquePointer(), 0, fragment, true));
|
||||
assert(!getHostPublicationIdentity(firstGeneration.getAsOpaquePointer(), 0, fragment, false));
|
||||
ScheduleCostSummary previous, candidate;
|
||||
previous.stageSpanNs = {25000};
|
||||
previous.maximumCoreWorkNs = 15000;
|
||||
previous.baselineMemory.serviceNs = 50000;
|
||||
candidate.stageSpanNs = {32000};
|
||||
candidate.maximumCoreWorkNs = 27000;
|
||||
candidate.baselineMemory.serviceNs = 40000;
|
||||
// Reduced memory and stage demand must not hide concentrated core work.
|
||||
assert(!isBetterPipelineSchedule(candidate, previous));
|
||||
candidate.maximumCoreWorkNs = 24000;
|
||||
assert(isBetterPipelineSchedule(candidate, previous));
|
||||
candidate.stageSpanNs = {36000};
|
||||
assert(!isBetterPipelineSchedule(candidate, previous));
|
||||
assert(!isBetterPipelineSchedule(previous, previous));
|
||||
ComputeGraph graph;
|
||||
graph.predecessors.resize(4);
|
||||
graph.successors.resize(4);
|
||||
@@ -1679,7 +1713,11 @@ static void testPipelineSharedMemory() {
|
||||
&& assignment[task] <= assignment[task - 1] + 1);
|
||||
auto memory = getPipelineBoundaryMemoryDemand(graph, assignment, target);
|
||||
assert(memory.readRequests == 1 && memory.writeRequests == 1);
|
||||
assert(memory.readBytes == (latency == 1 ? 16384 : 32));
|
||||
Cost expected = 0;
|
||||
for (const auto &transfer : graph.publicationTransfers)
|
||||
if (assignment[transfer.source] != assignment[transfer.target])
|
||||
expected += transfer.bytes * transfer.count;
|
||||
assert(memory.readBytes == expected);
|
||||
assert(memory.writeBytes == memory.readBytes);
|
||||
assert(schedule.costs.boundaryMemory.serviceNs == memory.serviceNs);
|
||||
}
|
||||
@@ -1787,6 +1825,7 @@ static void testPipelineRestartOrder() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
testOrderedPackCostCurve();
|
||||
testStridedPublicationMemory();
|
||||
testPipelineSharedMemory();
|
||||
testBaselineConstantMemory();
|
||||
|
||||
@@ -170,6 +170,11 @@ def main():
|
||||
assert vector['per_core'][0]['resource_service_ns'][1] == vector_cost
|
||||
issued = run([[bw, record(10, length=65)]], issue=True)
|
||||
assert issued['per_core'][0]['serial_work_ns'] == vector_cost + 2 * hardware['period']
|
||||
omitted = run([[record(21, length=65), bw, record(21, length=65),
|
||||
record(10, length=65), record(21, length=65)]], issue=True)
|
||||
assert omitted['per_core'][0]['serial_work_ns'] == issued['per_core'][0]['serial_work_ns']
|
||||
assert omitted['per_core'][0]['resource_service_ns'] == issued['per_core'][0]['resource_service_ns']
|
||||
assert omitted['opcode_service_ns']['vsoftmax'] == 0
|
||||
repeated = run([[bw] + [record(10, length=65)] * 10000])
|
||||
assert repeated['per_core'][0]['serial_work_ns'] == 10000 * vector_cost
|
||||
assert repeated['events'] < 10
|
||||
@@ -320,12 +325,42 @@ def main():
|
||||
measured = float(re.search(r'average latency:\s+(\S+)', result.stdout)[1]) * 1e6
|
||||
estimate = prediction['memory_controller']['interval_ns'] if controller else prediction['interval_estimate_ns']
|
||||
assert (round(estimate), round(measured)) == expected
|
||||
|
||||
# Omitting Softmax must leave timing, power, and energy unchanged.
|
||||
baseline = [bw, record(1), load(64), store(64), record(1)]
|
||||
omitted = [record(21, length=65)] + baseline[:2] + [record(21, length=65)] + baseline[2:] + [record(21, length=65)]
|
||||
predictions, measurements = [], []
|
||||
|
||||
def metrics(instructions, omitted_softmax=False):
|
||||
result = subprocess.run([args.pimsim, str(instructions), str(path), '--gui=false', '--fast'],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
assert result.stderr.count('Warning: vsoftmax has no timing model;') == int(omitted_softmax)
|
||||
return tuple(float(re.search(rf'{key}:\s+(\S+)', result.stdout)[1])
|
||||
for key in ('average latency', 'average power', 'average energy'))
|
||||
|
||||
for index, program in enumerate((baseline, omitted)):
|
||||
prediction = run([program], issue=True, controller=True)
|
||||
predictions.append(prediction['memory_controller']['interval_ns'])
|
||||
measurements.append(metrics(root, omitted_softmax=bool(index)))
|
||||
assert predictions[0] == predictions[1]
|
||||
json_directory = root / 'softmax-json'
|
||||
json_directory.mkdir(exist_ok=True)
|
||||
json_program = json_directory / 'program.json'
|
||||
scalar_json = {'op': 'sldi', 'rd': 0, 'imm': 0}
|
||||
softmax_json = {'op': 'vsoftmax', 'rd': 0, 'rs1': 0, 'len': 65}
|
||||
json_program.write_text(json.dumps({'config': json.loads((root / 'config.json').read_text()),
|
||||
'core0': [softmax_json, {'op': 'setbw', 'ibiw': 8, 'obiw': 8}, scalar_json,
|
||||
softmax_json, {'op': 'ld', 'rd': 0, 'rs1': 0, 'size': 64},
|
||||
{'op': 'st', 'rd': 0, 'rs1': 0, 'size': 64}, scalar_json, softmax_json]}))
|
||||
measurements.append(metrics(json_program, omitted_softmax=True))
|
||||
assert measurements[0] == measurements[1] == measurements[2]
|
||||
path.write_text(json.dumps(config))
|
||||
network.write_text(json.dumps(dict(latency=latency)))
|
||||
|
||||
run([[send(1), recv(1)], [send(0), recv(0)]], valid=False)
|
||||
run([[sync(1, 0)], [wait(0, 2)]], valid=False)
|
||||
run([[record(21, length=1)]], valid=False)
|
||||
run([[record(255, length=1)]], valid=False)
|
||||
run([[]], valid=False)
|
||||
unsupported = copy.deepcopy(config)
|
||||
unsupported['chip_config']['core_config']['rob_size'] = 2
|
||||
|
||||
@@ -81,7 +81,7 @@ def main():
|
||||
assert selected["single_candidate_analysis_skipped"] and selected["lowered_candidates"] == 1
|
||||
assert "spatial_cost" not in selected["candidates"][0]
|
||||
|
||||
# An unknown estimate cannot silently exclude any alternative.
|
||||
# Softmax remains in the functional program but has no oracle timing work.
|
||||
model = onnx.load(vgg)
|
||||
output = model.graph.output[0].name
|
||||
internal = output + "_before_softmax"
|
||||
@@ -95,10 +95,17 @@ def main():
|
||||
model.graph.node.append(helper.make_node("Softmax", [internal], [output], axis=-1))
|
||||
source = root / "vgg-softmax.onnx"
|
||||
onnx.save(model, source)
|
||||
report, program, _ = compile(source)
|
||||
assert report["generated_candidates"] > 1 and report["status"] == "selected"
|
||||
assert report["lowered_candidates"] == 1 and program
|
||||
assert all(candidate["eligible"] for candidate in report["candidates"])
|
||||
|
||||
# An unknown estimate cannot silently exclude any alternative.
|
||||
source = REPO / "validation/operations/matmul/yolo_attention/matmul_yolo_attention.onnx"
|
||||
report, program, diagnostics = compile(source, succeeds=False)
|
||||
assert report["generated_candidates"] > 1 and report["status"] == "incomplete-costs"
|
||||
assert report["generated_candidates"] >= 1 and report["status"] == "incomplete-costs"
|
||||
assert "cannot compare the complete candidate set" in diagnostics
|
||||
assert "unsupported abstract operation" in diagnostics
|
||||
assert "unavailable indirect" in diagnostics
|
||||
assert not program and "backend_attempts" not in report
|
||||
# Latency PEFT and earlier emission do not invoke candidate costing.
|
||||
report, program, _ = compile(vgg, pipeline=1)
|
||||
|
||||
@@ -5,6 +5,7 @@ import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import struct
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
@@ -19,6 +20,59 @@ def check_selected_program(out):
|
||||
return report
|
||||
|
||||
|
||||
def check_synchronization_ablation(root, compiler, env):
|
||||
vgg = REPO / "validation/networks/pimcomp_models/vgg8/vgg8-mnist-reconstructed.onnx"
|
||||
out = root / "synchronization-ablation"
|
||||
# Ablation keeps normal selection and costs omitted synchronization separately.
|
||||
for arch, cores, crossbars in (("arch-a", 168, 96), ("arch-b", 138, 128)):
|
||||
programs, evidence, schedules = [], [], []
|
||||
for disabled in (False, True):
|
||||
out.mkdir()
|
||||
command = [compiler, str(vgg), "-o", str(out / "model"), "--maccel=PIM",
|
||||
"--EmitPimCodegen", "--pipeline=4", f"--core-count={cores}",
|
||||
f"--crossbar-count={crossbars}", "--crossbar-size=128",
|
||||
"--mlir-disable-threading", "--pim-detect-communication-deadlock",
|
||||
f"--pim-target-config={REPO}/validation/pimsim_configs/pimcomp/{arch}/throughput_config_1000ms.json"]
|
||||
if disabled:
|
||||
command.append("--pim-disable-synchronization")
|
||||
result = subprocess.run(command, cwd=root, env=env, capture_output=True, text=True, timeout=90)
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
report = check_selected_program(out)
|
||||
assert report["generated_candidates"] > 1
|
||||
assert bool(report.get("synchronization_ablation")) == disabled
|
||||
if disabled:
|
||||
assert report["cost_model_program"] == "synchronized-reference"
|
||||
assert "report zero-cost SYNC/WAIT separately" in result.stderr
|
||||
ablation = report["backend_attempts"][0]["synchronization_ablation_cost"]
|
||||
assert ablation["cost_model_program"] == "synchronization-omitted"
|
||||
assert not ablation["used_for_selection"] and ablation["diagnostic_only"]
|
||||
assert all(candidate["eligible"] for candidate in report["candidates"])
|
||||
evidence.append((report["selection"], [(c["interval_ns"], c["alternative_tie_interval_ns"],
|
||||
c["eligible"], c["stage_core_budgets"], c["task_stage_runs"])
|
||||
for c in report["candidates"]]))
|
||||
schedules.append((out / "reports/spatial3_scheduled_no_comm.txt").read_bytes())
|
||||
program = {}
|
||||
sync_count = 0
|
||||
for path in (out / "pim").rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
data = path.read_bytes()
|
||||
if path.suffix == ".pim":
|
||||
magic, version, count = struct.unpack("<4sII", data[:12])
|
||||
assert magic == b"PIMB" and version == 1 and len(data) == 12 + 20 * count
|
||||
records = [data[i:i + 20] for i in range(12, len(data), 20)]
|
||||
sync_count += sum(record[0] in (31, 32) for record in records)
|
||||
data = b"".join(record for record in records if record[0] not in (31, 32))
|
||||
program[str(path.relative_to(out / "pim"))] = hashlib.sha256(data).hexdigest()
|
||||
assert (sync_count == 0) == disabled
|
||||
programs.append(program)
|
||||
shutil.rmtree(out)
|
||||
assert evidence[0] == evidence[1], arch
|
||||
assert schedules[0] == schedules[1], arch
|
||||
assert programs[0] == programs[1], arch
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--compiler", required=True)
|
||||
@@ -87,7 +141,7 @@ def main():
|
||||
result = subprocess.run(command, cwd=root, env=env, capture_output=True, text=True, timeout=90)
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
report = check_selected_program(out)
|
||||
assert report["selection"] != 0 and bool((out / "dialects").exists()) == diagnostics
|
||||
assert bool((out / "dialects").exists()) == diagnostics
|
||||
identity = {str(p.relative_to(out / "pim")): hashlib.sha256(p.read_bytes()).hexdigest()
|
||||
for p in (out / "pim").rglob("*") if p.is_file()}
|
||||
schedule = (out / "reports/spatial3_scheduled_no_comm.txt").read_bytes()
|
||||
@@ -114,22 +168,23 @@ def main():
|
||||
"--core-count=168", "--crossbar-count=96", "--crossbar-size=128", "--mlir-disable-threading",
|
||||
f"--pim-target-config={hardware}"]
|
||||
result = subprocess.run(command, cwd=root, env=env, capture_output=True, text=True, timeout=60)
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
assert result.returncode != 0, result.stdout + result.stderr
|
||||
assert "cannot compare the complete candidate set" in result.stderr
|
||||
report = json.loads((out / "reports/pipeline_selection.json").read_text())
|
||||
assert report["selection"] == 0 and report["single_candidate_analysis_skipped"]
|
||||
assert report["generated_candidates"] == 1 and report["lowered_candidates"] == 1
|
||||
assert "no cost comparison required" in report["reason"]
|
||||
assert report["temporary_disk_bytes"] == 0 and not list(root.glob("candidate*"))
|
||||
assert report["status"] == "incomplete-costs" and "backend_attempts" not in report
|
||||
assert "ROB capacity one" in report["reason"]
|
||||
shutil.rmtree(out)
|
||||
out.mkdir()
|
||||
command[-1] = f"--pim-target-config={config_path}"
|
||||
command += ["--pim-disable-synchronization"]
|
||||
result = subprocess.run(command, cwd=root, env=env, capture_output=True, text=True, timeout=60)
|
||||
assert result.returncode == 0, result.stdout + result.stderr
|
||||
result = subprocess.run([*command, "--pim-disable-synchronization"],
|
||||
cwd=root, env=env, capture_output=True, text=True, timeout=60)
|
||||
assert result.returncode != 0, result.stdout + result.stderr
|
||||
report = json.loads((out / "reports/pipeline_selection.json").read_text())
|
||||
assert report["selection"] == 0 and report["single_candidate_analysis_skipped"]
|
||||
assert report["generated_candidates"] == 1 and report["lowered_candidates"] == 1
|
||||
assert "no cost comparison required" in report["reason"]
|
||||
assert report["status"] == "incomplete-costs" and "backend_attempts" not in report
|
||||
assert "ROB capacity one" in report["reason"]
|
||||
shutil.rmtree(out)
|
||||
|
||||
check_synchronization_ablation(root, compiler, env)
|
||||
|
||||
assert not (root / "executed").exists(), "production compiler executed a diagnostic or simulator"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user