* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file test_reduce_copy.cpp
* \brief Unit test for ReduceCopy pass.
*/
#include <vector>
#include <string>
#include "gtest/gtest.h"
#include "tilefwk/data_type.h"
#include "tilefwk/tilefwk_op.h"
#include "tilefwk/platform.h"
#include "interface/function/function.h"
#define private public
#include "passes/tile_graph_pass/graph_partition/reduce_copy.h"
#undef private
#include "tilefwk/tilefwk.h"
#include "interface/inner/tilefwk.h"
#include "passes/pass_mgr/pass_manager.h"
#include "interface/configs/config_manager.h"
#include "computational_graph_builder.h"
namespace npu {
namespace tile_fwk {
class ReduceCopyTest : public testing::Test {
public:
static void SetUpTestCase() {}
static void TearDownTestCase() {}
void SetUp() override
{
Program::GetInstance().Reset();
config::Reset();
config::SetHostOption(COMPILE_STAGE, CS_EXECUTE_GRAPH);
config::SetHostConfig(KEY_STRATEGY, "ReduceCopyTestStrategy");
Platform::Instance().GetSoc().SetNPUArch(NPUArch::DAV_3510);
}
void TearDown() override { Platform::Instance().GetSoc().SetNPUArch(NPUArch::DAV_UNKNOWN); }
};
void BuildMatmulAddBranch(ComputationalGraphBuilder& G, int brId, std::vector<std::string>& incasts,
std::vector<std::string>& outcasts)
{
std::vector<int64_t> tileShape{16, 16};
std::string br = std::to_string(brId);
std::vector<std::string> tensorNames{"tRA" + br, "tRB" + br, "tL1A" + br, "tL1B" + br,
"tA" + br, "tB" + br, "tC" + br, "tUB" + br};
std::vector<Opcode> opCodes{Opcode::OP_VIEW, Opcode::OP_VIEW, Opcode::OP_L1_TO_L0A,
Opcode::OP_L1_TO_L0B, Opcode::OP_A_MUL_B, Opcode::OP_CONVERT};
std::vector<std::vector<std::string>> ioperands{{"tRA" + br}, {"tRB" + br}, {"tL1A" + br},
{"tL1B" + br}, {"tA" + br, "tB" + br}, {"tC" + br}};
std::vector<std::vector<std::string>> ooperands{{"tL1A" + br}, {"tL1B" + br}, {"tA" + br},
{"tB" + br}, {"tC" + br}, {"tUB" + br}};
std::vector<std::string> opNames{"view" + br, "view2" + br, "toA" + br, "toB" + br, "matmul" + br, "convert" + br};
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, tileShape, tensorNames), true);
EXPECT_EQ(G.AddOps(opCodes, ioperands, ooperands, opNames, true), true);
for (auto aicOp : opNames) {
G.GetOp(aicOp)->SetAttr(OpAttributeKey::isCube, true);
}
incasts.push_back("tRA" + br);
incasts.push_back("tRB" + br);
const int Num50 = 50;
for (auto opName : opNames) {
G.GetOp(opName)->UpdateSubgraphID(brId);
G.GetOp(opName)->UpdateLatency(Num50);
}
const int Num2 = 2;
for (int k = 0; k < Num2; k++) {
std::string brv1 = std::to_string(brId + 1 + k);
std::vector<std::string> tensorNamesV1{"add1" + brv1, "add2" + brv1, "out" + brv1};
std::vector<Opcode> opCodesV1{Opcode::OP_ADDS, Opcode::OP_ADDS, Opcode::OP_ASSEMBLE};
std::vector<std::vector<std::string>> ioperandsV1{
{"tUB" + br},
{"add1" + brv1},
{"add2" + brv1},
};
std::vector<std::vector<std::string>> ooperandsV1{
{"add1" + brv1},
{"add2" + brv1},
{"out" + brv1},
};
std::vector<std::string> opNamesV1{"add1" + brv1, "add2" + brv1, "assemble" + brv1};
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, tileShape, tensorNamesV1), true);
EXPECT_EQ(G.AddOps(opCodesV1, ioperandsV1, ooperandsV1, opNamesV1, true), true);
outcasts.push_back("out" + brv1);
for (auto opName : opNamesV1) {
G.GetOp(opName)->UpdateSubgraphID(brId + 1 + k);
G.GetOp(opName)->UpdateLatency(Num50);
G.GetOp(opName)->SetAttr(OpAttributeKey::isCube, false);
}
}
}
void BuildMatmulAddsGraph(ComputationalGraphBuilder& G)
{
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
const int Num3 = 3;
BuildMatmulAddBranch(G, 0, incasts, outcasts);
BuildMatmulAddBranch(G, Num3, incasts, outcasts);
Function* function = G.GetFunction();
const int Num6 = 6;
function->SetTotalSubGraphCount(Num6);
EXPECT_EQ(G.SetInCast(incasts), true);
EXPECT_EQ(G.SetOutCast(outcasts), true);
}
TEST_F(ReduceCopyTest, TestCase0)
{
ComputationalGraphBuilder G;
BuildMatmulAddsGraph(G);
Function* function = G.GetFunction();
ASSERT_NE(function, nullptr);
ReduceCopyMerge merger;
function->paramConfigs_.autoMixPartition = 1;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
void BuildConnectMatmul(ComputationalGraphBuilder& G, int brId, std::vector<std::string>& incasts,
std::vector<std::string>& outcasts)
{
std::vector<int64_t> tileShape{16, 16};
const int Num50 = 50;
std::string br = std::to_string(brId);
std::vector<std::string> tensorNames{"tRA" + br, "tRB" + br, "tL1A" + br, "tL1B" + br,
"tA" + br, "tB" + br, "tC" + br, "tGM" + br};
std::vector<Opcode> opCodes{Opcode::OP_VIEW, Opcode::OP_VIEW, Opcode::OP_L1_TO_L0A,
Opcode::OP_L1_TO_L0B, Opcode::OP_A_MUL_B, Opcode::OP_ASSEMBLE};
std::vector<std::vector<std::string>> ioperands{{"tRA" + br}, {"tRB" + br}, {"tL1A" + br},
{"tL1B" + br}, {"tA" + br, "tB" + br}, {"tC" + br}};
std::vector<std::vector<std::string>> ooperands{{"tL1A" + br}, {"tL1B" + br}, {"tA" + br},
{"tB" + br}, {"tC" + br}, {"tGM" + br}};
std::vector<std::string> opNames{"view" + br, "view2" + br, "toA" + br, "toB" + br, "matmul" + br, "convert" + br};
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, tileShape, tensorNames), true);
EXPECT_EQ(G.AddOps(opCodes, ioperands, ooperands, opNames, true), true);
incasts.push_back("tRA" + br);
incasts.push_back("tRB" + br);
for (auto opName : opNames) {
G.GetOp(opName)->UpdateSubgraphID(brId);
G.GetOp(opName)->UpdateLatency(Num50);
G.GetOp(opName)->SetAttr(OpAttributeKey::isCube, true);
}
std::string br2 = std::to_string(brId + 1);
std::vector<std::string> tensorNames2{"tRB" + br2, "tL1A" + br2, "tL1B" + br2, "tA" + br2,
"tB" + br2, "tC" + br2, "tGM" + br2};
std::vector<Opcode> opCodes2{Opcode::OP_VIEW, Opcode::OP_VIEW, Opcode::OP_L1_TO_L0A,
Opcode::OP_L1_TO_L0B, Opcode::OP_A_MUL_B, Opcode::OP_ASSEMBLE};
std::vector<std::vector<std::string>> ioperands2{
{"tGM" + br}, {"tRB" + br2}, {"tL1A" + br2}, {"tL1B" + br2}, {"tA" + br2, "tB" + br2}, {"tC" + br2}};
std::vector<std::vector<std::string>> ooperands2{{"tL1A" + br2}, {"tL1B" + br2}, {"tA" + br2},
{"tB" + br2}, {"tC" + br2}, {"tGM" + br2}};
std::vector<std::string> opNames2{"view" + br2, "view2" + br2, "toA" + br2,
"toB" + br2, "matmul" + br2, "convert" + br2};
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, tileShape, tensorNames2), true);
EXPECT_EQ(G.AddOps(opCodes, ioperands2, ooperands2, opNames2, true), true);
incasts.push_back("tRB" + br2);
outcasts.push_back("tGM" + br2);
for (auto opName : opNames2) {
G.GetOp(opName)->UpdateSubgraphID(brId + 1);
G.GetOp(opName)->UpdateLatency(Num50);
}
}
void BuildConnectVector(ComputationalGraphBuilder& G, int brId, std::vector<std::string>& incasts,
std::vector<std::string>& outcasts)
{
std::vector<int64_t> tileShape{16, 16};
std::string br = std::to_string(brId);
std::vector<std::string> tensorNames{"tin" + br, "tadds1" + br, "tout" + br};
std::vector<Opcode> opCodes{Opcode::OP_ADDS, Opcode::OP_ADDS};
std::vector<std::vector<std::string>> ioperands{{"tin" + br}, {"tadds1" + br}};
std::vector<std::vector<std::string>> ooperands{{"tadds1" + br}, {"tout" + br}};
std::vector<std::string> opNames{"adds1" + br, "adds2" + br};
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, tileShape, tensorNames), true);
EXPECT_EQ(G.AddOps(opCodes, ioperands, ooperands, opNames, true), true);
incasts.push_back("tin" + br);
outcasts.push_back("tout" + br);
const int Num50 = 50;
G.GetOp("adds1" + br)->UpdateSubgraphID(brId);
G.GetOp("adds1" + br)->UpdateLatency(Num50);
G.GetOp("adds1" + br)->SetAttr(OpAttributeKey::isCube, false);
G.GetOp("adds2" + br)->UpdateSubgraphID(brId + 1);
G.GetOp("adds2" + br)->UpdateLatency(Num50);
G.GetOp("adds2" + br)->SetAttr(OpAttributeKey::isCube, false);
}
void BuildConnect(ComputationalGraphBuilder& G)
{
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
const int Num2 = 2;
BuildConnectMatmul(G, 0, incasts, outcasts);
BuildConnectVector(G, Num2, incasts, outcasts);
Function* function = G.GetFunction();
const int Num4 = 4;
function->SetTotalSubGraphCount(Num4);
EXPECT_EQ(G.SetInCast(incasts), true);
EXPECT_EQ(G.SetOutCast(outcasts), true);
}
TEST_F(ReduceCopyTest, TestCase1)
{
ComputationalGraphBuilder G;
BuildConnect(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
merger.RunOnFunction(*function);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, TestCase2)
{
ComputationalGraphBuilder G;
BuildConnect(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 1;
G.GetOp("adds12")->scopeInfo_.cvFuseId = 0;
G.GetOp("adds22")->scopeInfo_.cvFuseId = 0;
ReduceCopyMerge merger;
merger.RunOnFunction(*function);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, TestCase3)
{
ComputationalGraphBuilder G;
BuildConnect(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 1;
const int largeNum = 2e7;
G.GetOp("matmul0")->UpdateLatency(largeNum);
ReduceCopyMerge merger;
merger.RunOnFunction(*function);
const int Num4 = 4;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num4);
}
static std::string AddCubeMatmulFrom(ComputationalGraphBuilder& G, int sg, const std::string& inA,
const std::string& inB);
static std::string AddCubeMatmulSG(ComputationalGraphBuilder& G, int sg, std::vector<std::string>& incasts)
{
std::vector<int64_t> sh{16, 16};
std::string b = std::to_string(sg);
std::string inA = "tRA" + b;
std::string inB = "tRB" + b;
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {inA, inB}), true);
incasts.push_back(inA);
incasts.push_back(inB);
return AddCubeMatmulFrom(G, sg, inA, inB);
}
static std::string AddIncast(ComputationalGraphBuilder& G, const std::string& name, std::vector<std::string>& incasts)
{
std::vector<int64_t> sh{16, 16};
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, name), true);
incasts.push_back(name);
return name;
}
static std::string AddCubeMatmulFrom(ComputationalGraphBuilder& G, int sg, const std::string& inA,
const std::string& inB)
{
std::vector<int64_t> sh{16, 16};
std::string b = std::to_string(sg);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tVA" + b, "tVB" + b, "tL0A" + b, "tL0B" + b, "tC" + b}), true);
EXPECT_EQ(
G.AddOps({Opcode::OP_VIEW, Opcode::OP_VIEW, Opcode::OP_L1_TO_L0A, Opcode::OP_L1_TO_L0B, Opcode::OP_A_MUL_B},
{{inA}, {inB}, {"tVA" + b}, {"tVB" + b}, {"tL0A" + b, "tL0B" + b}},
{{"tVA" + b}, {"tVB" + b}, {"tL0A" + b}, {"tL0B" + b}, {"tC" + b}},
{"viewA" + b, "viewB" + b, "toA" + b, "toB" + b, "mm" + b}, true),
true);
const int Num50 = 50;
for (auto& n : std::vector<std::string>{"viewA" + b, "viewB" + b, "toA" + b, "toB" + b, "mm" + b}) {
G.GetOp(n)->UpdateSubgraphID(sg);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, true);
}
return "tC" + b;
}
static std::string AddVecSG(ComputationalGraphBuilder& G, int sg, const std::string& in, const std::string& out)
{
std::vector<int64_t> sh{16, 16};
std::string b = std::to_string(sg);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"vAdd" + b, out}), true);
std::string addsName = "vadds" + b;
std::string asmName = "vasm" + b;
EXPECT_EQ(G.AddOps({Opcode::OP_ADDS, Opcode::OP_ASSEMBLE}, {{in}, {"vAdd" + b}}, {{"vAdd" + b}, {out}},
{addsName, asmName}, true),
true);
const int Num50 = 50;
for (auto& n : std::vector<std::string>{addsName, asmName}) {
G.GetOp(n)->UpdateSubgraphID(sg);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
return out;
}
static void AddVecSinkSG(ComputationalGraphBuilder& G, int sg, const std::vector<std::string>& inputs,
const std::string& outName, std::vector<std::string>& outcasts)
{
std::vector<int64_t> sh{16, 16};
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, outName), true);
const int Num50 = 50;
std::vector<std::string> addOuts;
for (size_t i = 0; i < inputs.size(); i++) {
std::string b = std::to_string(sg) + "_" + std::to_string(i);
std::string tName = "sinkadd" + b;
std::string opName = "vadds" + b;
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, tName), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ADDS, {inputs[i]}, {tName}, opName, true), true);
G.GetOp(opName)->UpdateSubgraphID(sg);
G.GetOp(opName)->UpdateLatency(Num50);
G.GetOp(opName)->SetAttr(OpAttributeKey::isCube, false);
addOuts.push_back(tName);
}
std::string asmName = "vasm" + std::to_string(sg);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, addOuts, {outName}, asmName, true), true);
G.GetOp(asmName)->UpdateSubgraphID(sg);
G.GetOp(asmName)->UpdateLatency(Num50);
G.GetOp(asmName)->SetAttr(OpAttributeKey::isCube, false);
outcasts.push_back(outName);
}
TEST_F(ReduceCopyTest, SinkProtectedWhenMultiProducerRoots)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
AddCubeMatmulFrom(G, 0, AddIncast(G, "QA0", incasts), AddIncast(G, "QB0", incasts));
AddVecSG(G, 1, "tC0", "v1a");
std::string c2a = AddCubeMatmulFrom(G, 2, "v1a", AddIncast(G, "VA0", incasts));
AddCubeMatmulFrom(G, 3, AddIncast(G, "QA1", incasts), AddIncast(G, "QB1", incasts));
AddVecSG(G, 4, "tC3", "v1b");
std::string c2b = AddCubeMatmulFrom(G, 5, "v1b", AddIncast(G, "VA1", incasts));
AddVecSinkSG(G, 6, {c2a, c2b}, "sinkGlmOut", outcasts);
const int Num5 = 5;
for (auto& n : std::vector<std::string>{"vadds6_0", "vadds6_1", "vasm6"}) {
G.GetOp(n)->UpdateLatency(Num5);
}
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(7);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, SinkMergesWhenProducersCollapse)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::string c = AddCubeMatmulSG(G, 0, incasts);
std::string v1 = AddVecSG(G, 1, c, "v1");
std::string v2 = AddVecSG(G, 2, c, "v2");
AddVecSinkSG(G, 3, {v1, v2}, "sinkGqaOut", outcasts);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(4);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
TEST_F(ReduceCopyTest, OutputStageBypassMergesAllSinks)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::string c0 = AddCubeMatmulSG(G, 0, incasts);
std::string c1 = AddCubeMatmulSG(G, 1, incasts);
AddVecSinkSG(G, 2, {c0, c1}, "sinkA", outcasts);
AddVecSinkSG(G, 3, {c0, c1}, "sinkB", outcasts);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(4);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
static MergeInput BuildSimpleMergeInput(int numSubgraph, const std::vector<std::set<int>>& outGraph,
const std::vector<std::vector<int>>& groups)
{
MergeInput input;
input.numSubgraph = numSubgraph;
input.maxLatency = 1e7;
input.aivRatio = {1e-6, 1e6};
input.subgraphAICLatency.assign(numSubgraph, 50);
input.subgraphAIVLatency.assign(numSubgraph, 50);
input.subgraphAICOpNum.assign(numSubgraph, 1);
input.subgraphAIVOpNum.assign(numSubgraph, 1);
input.maxSubgraphAICOpNum = 2000;
input.maxSubgraphAIVOpNum = 2240;
input.subGraphOutGraph = outGraph;
input.mergeGroup = groups;
input.isEnforceMergeGroup.assign(groups.size(), true);
input.isValidMergeGroup.assign(groups.size(), true);
return input;
}
TEST_F(ReduceCopyTest, MixGraphMerger_AICOpNumLimitRejectsMerge)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {}, {3}, {}}, {{0, 1}, {2, 3}});
input.subgraphAICOpNum = {30, 30, 5, 5};
input.subgraphAIVOpNum = {30, 30, 5, 5};
input.maxSubgraphAICOpNum = 50;
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1, 2, 3};
EXPECT_FALSE(merger.CheckLatencyConstraint({0, 1}));
EXPECT_TRUE(merger.CheckLatencyConstraint({2, 3}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_AIVOpNumLimitRejectsMerge)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {}, {3}, {}}, {{0, 1}, {2, 3}});
input.subgraphAICOpNum = {30, 30, 5, 5};
input.subgraphAIVOpNum = {30, 30, 5, 5};
input.maxSubgraphAIVOpNum = 50;
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1, 2, 3};
EXPECT_FALSE(merger.CheckLatencyConstraint({0, 1}));
EXPECT_TRUE(merger.CheckLatencyConstraint({2, 3}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_OpNumLimitBoundaryEqualPasses)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {}, {3}, {}}, {{0, 1}, {2, 3}});
input.subgraphAICOpNum = {30, 30, 5, 5};
input.subgraphAIVOpNum = {30, 30, 5, 5};
input.maxSubgraphAICOpNum = 60;
input.maxSubgraphAIVOpNum = 60;
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1, 2, 3};
EXPECT_TRUE(merger.CheckLatencyConstraint({0, 1}));
EXPECT_TRUE(merger.CheckLatencyConstraint({2, 3}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_ApplyMergeToGraphUpdatesCache)
{
std::vector<std::set<int>> outGraph{{1}, {}, {3}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1}, {2, 3}});
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 2);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_EQ(output.subgraphIdUpdated[2], output.subgraphIdUpdated[3]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_CacheRestoreAfterRejection)
{
std::vector<std::set<int>> outGraph{{1, 3}, {2}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 2}, {0, 1}});
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num2 = 2;
EXPECT_EQ(output.numSubgraphUpdated, Num2);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_EQ(output.subgraphIdUpdated[1], output.subgraphIdUpdated[2]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[3]);
}
TEST_F(ReduceCopyTest, RunOnFunction_InvalidCustomFallsBackToDefaultLevel)
{
ComputationalGraphBuilder G;
BuildMatmulAddsGraph(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 50;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
TEST_F(ReduceCopyTest, RunOnFunction_CustomTotalOpNumMerges)
{
ComputationalGraphBuilder G;
BuildMatmulAddsGraph(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 101;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
TEST_F(ReduceCopyTest, MixGraphMerger_CacheConsistentWithFullRebuild)
{
std::vector<std::set<int>> outGraph{{1, 3}, {2}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 2}, {0, 1}});
MixGraphMerger merger;
merger.Merge(input);
std::vector<std::set<int>> freshOut, freshIn;
merger.BuildMergedGraph(freshOut, freshIn);
for (int i = 0; i < input.numSubgraph; i++) {
EXPECT_EQ(merger.mCachedOutGraph[i], freshOut[i]) << "outGraph mismatch at node " << i;
EXPECT_EQ(merger.mCachedInGraph[i], freshIn[i]) << "inGraph mismatch at node " << i;
}
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeRejectsExternalDdrTensorUse)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{101, {0, 2}, {0, 2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeRejectsExternalConsumerOnly)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{103, {0}, {0, 2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeAllowsInternalDdrTensorUse)
{
std::vector<std::set<int>> outGraph{{1}, {}};
MergeInput input = BuildSimpleMergeInput(2, outGraph, {{0, 1}});
input.boundaryTensors = {{102, {0}, {1}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num1 = 1;
EXPECT_EQ(output.numSubgraphUpdated, Num1);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeAllowsSameScopeExternalProducer)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{202, {0, 2}, {1}, true, {5, 5}, {5}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num2 = 2;
EXPECT_EQ(output.numSubgraphUpdated, Num2);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeAllowsSameScopeExternalConsumer)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{205, {0}, {1, 2}, true, {5}, {5, 5}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num2 = 2;
EXPECT_EQ(output.numSubgraphUpdated, Num2);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeRejectsMixedScopeExternalEndpoints)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{206, {0, 2}, {1, 2}, true, {5, 5}, {5, 7}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeRejectsForeignScopeExternalProducer)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{201, {0, 2}, {1}, true, {5, 7}, {5}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeRejectsScopelessExternalProducer)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{203, {0, 2}, {1}, true, {5, -1}, {5}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforcedMergeRejectsScopelessInnerEndpoint)
{
std::vector<std::set<int>> outGraph{{1}, {}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1}});
input.boundaryTensors = {{204, {0, 2}, {1}, true, {5, 5}, {-1}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {0}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, DdrPredictRejectsMultiAssembleProducerOnUbTensor)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::vector<int64_t> sh{16, 16};
std::string c0 = AddCubeMatmulSG(G, 0, incasts);
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, MemoryType::MEM_UB, "T"), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {c0}, {"T"}, "asm0", true), true);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tCast0", "out0"}), true);
EXPECT_EQ(G.AddOp(Opcode::OP_CAST, {"T"}, {"tCast0"}, "cast0", true), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"tCast0"}, {"out0"}, "asmOut0", true), true);
AddIncast(G, "in1", incasts);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"in1"}, {"T"}, "asm1", true), true);
AddVecSG(G, 2, "out0", "v2");
outcasts.push_back("v2");
const int Num50 = 50;
G.GetOp("asm0")->UpdateSubgraphID(0);
G.GetOp("asm0")->UpdateLatency(Num50);
G.GetOp("asm0")->SetAttr(OpAttributeKey::isCube, true);
for (auto& n : std::vector<std::string>{"cast0", "asmOut0"}) {
G.GetOp(n)->UpdateSubgraphID(0);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
const int largeNum = 2e7;
G.GetOp("asm1")->UpdateSubgraphID(1);
G.GetOp("asm1")->UpdateLatency(largeNum);
G.GetOp("asm1")->SetAttr(OpAttributeKey::isCube, false);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(3);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, DdrPredictNotTriggeredWhenProducerMixNonMoveOp)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::vector<int64_t> sh{16, 16};
std::string c0 = AddCubeMatmulSG(G, 0, incasts);
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, MemoryType::MEM_UB, "T"), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {c0}, {"T"}, "asm0", true), true);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tCast0", "out0"}), true);
EXPECT_EQ(G.AddOp(Opcode::OP_CAST, {"T"}, {"tCast0"}, "cast0", true), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"tCast0"}, {"out0"}, "asmOut0", true), true);
AddIncast(G, "in1", incasts);
EXPECT_EQ(G.AddOp(Opcode::OP_ADD, {"in1", "in1"}, {"T"}, "add1", true), true);
AddVecSG(G, 2, "out0", "v2");
outcasts.push_back("v2");
const int Num50 = 50;
G.GetOp("asm0")->UpdateSubgraphID(0);
G.GetOp("asm0")->UpdateLatency(Num50);
G.GetOp("asm0")->SetAttr(OpAttributeKey::isCube, true);
for (auto& n : std::vector<std::string>{"cast0", "asmOut0"}) {
G.GetOp(n)->UpdateSubgraphID(0);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
const int largeNum = 2e7;
G.GetOp("add1")->UpdateSubgraphID(1);
G.GetOp("add1")->UpdateLatency(largeNum);
G.GetOp("add1")->SetAttr(OpAttributeKey::isCube, false);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(3);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
TEST_F(ReduceCopyTest, DdrPredictRejectsCrossCoreCopyOutProducerOnUbTensor)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::vector<int64_t> sh{16, 16};
std::string c0 = AddCubeMatmulSG(G, 0, incasts);
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, MemoryType::MEM_L1, "tL1"), true);
EXPECT_EQ(G.AddOp(Opcode::OP_COPY_OUT, {c0}, {"tL1"}, "cpOut0", true), true);
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, MemoryType::MEM_UB, "T"), true);
EXPECT_EQ(G.AddOp(Opcode::OP_COPY_OUT, {"tL1"}, {"T"}, "crossCoreCopy", true), true);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tCast0", "out0"}), true);
EXPECT_EQ(G.AddOp(Opcode::OP_CAST, {"T"}, {"tCast0"}, "cast0", true), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"tCast0"}, {"out0"}, "asmOut0", true), true);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tCast1", "out1"}), true);
EXPECT_EQ(G.AddOp(Opcode::OP_CAST, {"T"}, {"tCast1"}, "cast1", true), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"tCast1"}, {"out1"}, "asmOut1", true), true);
AddVecSG(G, 2, "out0", "v2");
outcasts.push_back("v2");
const int Num50 = 50;
for (auto& n : std::vector<std::string>{"cpOut0", "crossCoreCopy"}) {
G.GetOp(n)->UpdateSubgraphID(0);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, true);
}
for (auto& n : std::vector<std::string>{"cast0", "asmOut0"}) {
G.GetOp(n)->UpdateSubgraphID(0);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
const int largeNum = 2e7;
for (auto& n : std::vector<std::string>{"cast1", "asmOut1"}) {
G.GetOp(n)->UpdateSubgraphID(1);
G.GetOp(n)->UpdateLatency(largeNum);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(3);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, DdrPredictRejectsSingleAssembleProducerOnUbTensor)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::vector<int64_t> sh{16, 16};
std::string c0 = AddCubeMatmulSG(G, 0, incasts);
EXPECT_EQ(G.AddTensor(DataType::DT_FP32, sh, MemoryType::MEM_UB, "T"), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {c0}, {"T"}, "asm0", true), true);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tCast0", "out0"}), true);
EXPECT_EQ(G.AddOp(Opcode::OP_CAST, {"T"}, {"tCast0"}, "cast0", true), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"tCast0"}, {"out0"}, "asmOut0", true), true);
EXPECT_EQ(G.AddTensors(DataType::DT_FP32, sh, {"tCast1", "out1"}), true);
EXPECT_EQ(G.AddOp(Opcode::OP_CAST, {"T"}, {"tCast1"}, "cast1", true), true);
EXPECT_EQ(G.AddOp(Opcode::OP_ASSEMBLE, {"tCast1"}, {"out1"}, "asmOut1", true), true);
AddVecSG(G, 2, "out0", "v2");
outcasts.push_back("v2");
const int Num50 = 50;
G.GetOp("asm0")->UpdateSubgraphID(0);
G.GetOp("asm0")->UpdateLatency(Num50);
G.GetOp("asm0")->SetAttr(OpAttributeKey::isCube, true);
for (auto& n : std::vector<std::string>{"cast0", "asmOut0"}) {
G.GetOp(n)->UpdateSubgraphID(0);
G.GetOp(n)->UpdateLatency(Num50);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
const int largeNum = 2e7;
for (auto& n : std::vector<std::string>{"cast1", "asmOut1"}) {
G.GetOp(n)->UpdateSubgraphID(1);
G.GetOp(n)->UpdateLatency(largeNum);
G.GetOp(n)->SetAttr(OpAttributeKey::isCube, false);
}
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(3);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, MixGraphMerger_AutoMixSkippedWhenScopedOpExists)
{
std::vector<std::set<int>> outGraph{{1}, {}, {3}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1}, {2, 3}});
input.isEnforceMergeGroup = {true, false};
input.hasScopedOp = true;
input.boundaryTensors = {{300, {0}, {1}, false, {}, {}}, {301, {2}, {3}, false, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {1}, {1}};
MergeInput noScopeInput = input;
noScopeInput.hasScopedOp = false;
noScopeInput.isEnforceMergeGroup = {false, false};
MixGraphMerger noScopeMerger;
MergeOutput noScopeOutput = noScopeMerger.Merge(noScopeInput);
const int Num2 = 2;
EXPECT_EQ(noScopeOutput.numSubgraphUpdated, Num2);
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num3 = 3;
EXPECT_EQ(output.numSubgraphUpdated, Num3);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[2], output.subgraphIdUpdated[3]);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[2]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_AutoMixSkippedWithoutEnforceGroup)
{
std::vector<std::set<int>> outGraph{{1}, {}, {3}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1}, {2, 3}});
input.isEnforceMergeGroup = {false, false};
input.hasScopedOp = true;
input.boundaryTensors = {{300, {0}, {1}, false, {}, {}}, {301, {2}, {3}, false, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}, {1}, {1}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
const int Num4 = 4;
EXPECT_EQ(output.numSubgraphUpdated, Num4);
EXPECT_NE(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_NE(output.subgraphIdUpdated[2], output.subgraphIdUpdated[3]);
}
TEST_F(ReduceCopyTest, MixGraphMerger_EnforceOpNumExceedsWarnsButMerges)
{
std::vector<std::set<int>> outGraph{{1}, {}};
MergeInput input = BuildSimpleMergeInput(2, outGraph, {{0, 1}});
input.subgraphAICOpNum = {1500, 600};
input.subgraphAIVOpNum = {800, 500};
input.maxSubgraphAICOpNum = 2000;
input.maxSubgraphAIVOpNum = 1000;
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1};
merger.WarnIfEnforceOpNumExceeds({0, 1});
MergeOutput output = merger.Merge(input);
const int Num1 = 1;
EXPECT_EQ(output.numSubgraphUpdated, Num1);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
}
namespace {
constexpr int kExpLatencyCap = 250000;
}
TEST_F(ReduceCopyTest, MixGraphMerger_LatencyCap250kBoundaryCheck)
{
std::vector<std::set<int>> outGraph{{1}, {}, {3}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1}, {2, 3}});
input.maxLatency = kExpLatencyCap;
input.subgraphAICLatency = {130000, 130000, 60000, 60000};
input.subgraphAIVLatency = {10, 10, 10, 10};
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1, 2, 3};
EXPECT_FALSE(merger.CheckLatencyConstraint({0, 1}));
EXPECT_TRUE(merger.CheckLatencyConstraint({2, 3}));
}
TEST_F(ReduceCopyTest, Merge_LatencyCap250kBlocksOversizeChain)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1, 2}});
input.isEnforceMergeGroup = {false};
input.maxLatency = kExpLatencyCap;
input.subgraphAICLatency = {120000, 120000, 120000};
input.subgraphAIVLatency = {10, 10, 10};
input.boundaryTensors = {{100, {0}, {1}, true, {}, {}}, {101, {1}, {2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0, 1}, {1}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 3);
}
TEST_F(ReduceCopyTest, Merge_LatencyCap250kAllowsChainUnderLimit)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1, 2}});
input.isEnforceMergeGroup = {false};
input.maxLatency = kExpLatencyCap;
input.subgraphAICLatency = {60000, 60000, 60000};
input.subgraphAIVLatency = {10, 10, 10};
input.boundaryTensors = {{100, {0}, {1}, true, {}, {}}, {101, {1}, {2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0, 1}, {1}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, MixGraphMerger_LoopPathConsistency_MixedMembershipRejected)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {2}, {3}, {}}, {});
input.subgraphLoopPaths = {{7}, {7}, {}, {}};
MixGraphMerger merger;
merger.mInput = input;
merger.mRootLoopPaths = input.subgraphLoopPaths;
merger.mParent = {0, 1, 2, 3};
merger.mRootToBoundaryTensorIds.assign(input.numSubgraph, {});
EXPECT_FALSE(merger.CheckLoopPathConsistency({0, 1, 2}));
EXPECT_FALSE(merger.CheckLoopPathConsistency({0, 2}));
EXPECT_TRUE(merger.CheckLoopPathConsistency({0, 1}));
EXPECT_TRUE(merger.CheckLoopPathConsistency({2, 3}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_LoopPathConsistencyDisabledWithoutSlotInfo)
{
MergeInput input = BuildSimpleMergeInput(3, {{1}, {2}, {}}, {});
input.subgraphLoopPaths = {};
MixGraphMerger merger;
merger.mInput = input;
merger.mRootLoopPaths = input.subgraphLoopPaths;
merger.mParent = {0, 1, 2};
EXPECT_TRUE(merger.CheckLoopPathConsistency({0, 1, 2}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_LoopPathConsistencyDataEdgeExempted)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {2}, {}, {}}, {});
input.subgraphLoopPaths = {{7}, {7}, {}, {}};
input.boundaryTensors = {{100, {0}, {2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {}, {0}, {}};
MixGraphMerger merger;
merger.mInput = input;
merger.mRootLoopPaths = input.subgraphLoopPaths;
merger.mParent = {0, 1, 2, 3};
merger.mRootToBoundaryTensorIds = input.subgraphToBoundaryTensorIds;
EXPECT_TRUE(merger.CheckLoopPathConsistency({0, 2}));
EXPECT_FALSE(merger.CheckLoopPathConsistency({1, 2}));
EXPECT_TRUE(merger.CheckLoopPathConsistency({0, 1}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_LoopPathConsistencySharedLoopExempted)
{
MergeInput input = BuildSimpleMergeInput(3, {{1}, {2}, {}}, {});
input.subgraphLoopPaths = {{7}, {7, 8}, {}};
MixGraphMerger merger;
merger.mInput = input;
merger.mRootLoopPaths = input.subgraphLoopPaths;
merger.mParent = {0, 1, 2};
merger.mRootToBoundaryTensorIds.assign(input.numSubgraph, {});
EXPECT_TRUE(merger.CheckLoopPathConsistency({0, 1}));
EXPECT_FALSE(merger.CheckLoopPathConsistency({0, 2}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_LoopPathConsistencyCrossRingDataEdgeRejected)
{
MergeInput input = BuildSimpleMergeInput(2, {{1}, {}}, {});
input.subgraphLoopPaths = {{7}, {8}};
input.boundaryTensors = {{100, {0}, {1}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0}};
MixGraphMerger merger;
merger.mInput = input;
merger.mRootLoopPaths = input.subgraphLoopPaths;
merger.mParent = {0, 1};
merger.mRootToBoundaryTensorIds = input.subgraphToBoundaryTensorIds;
EXPECT_FALSE(merger.CheckLoopPathConsistency({0, 1}));
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardRejectsParallelFanout)
{
std::vector<std::set<int>> outGraph{{1, 2, 3}, {}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {0}, {2}, true, {}, {}}, {102, {0}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 4);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardAllowsChainedBranches)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {3}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {1}, {2}, true, {}, {}}, {102, {2}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0, 1}, {1, 2}, {2}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardExemptsSameRingBranches)
{
std::vector<std::set<int>> outGraph{{1, 2, 3}, {}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.subgraphLoopPaths = {{7}, {7}, {7}, {7}};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {0}, {2}, true, {}, {}}, {102, {0}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, Merge_LoopPathGateBlocksBranchAbsorption)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1, 2}});
input.isEnforceMergeGroup = {false};
input.subgraphLoopPaths = {{7}, {7}, {}};
input.boundaryTensors = {{100, {0}, {1}, true, {}, {}}, {101, {1}, {2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0, 1}, {1}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 3);
}
TEST_F(ReduceCopyTest, Merge_LoopPathGateAllowsSamePathChain)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1, 2}});
input.isEnforceMergeGroup = {false};
input.subgraphLoopPaths = {{7}, {7}, {7}};
input.boundaryTensors = {{100, {0}, {1}, true, {}, {}}, {101, {1}, {2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0, 1}, {1}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, Merge_NoSlotInfoGateDisabledMergesAll)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{0, 1, 2}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {{100, {0}, {1}, true, {}, {}}, {101, {1}, {2}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0}, {0, 1}, {1}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, RunOnFunction_CarrySlotGateAbsorbsDataCoupledBranch)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::string carryIn = AddIncast(G, "carryIn", incasts);
std::string k0 = AddIncast(G, "k0", incasts);
std::string c0 = AddCubeMatmulFrom(G, 0, carryIn, k0);
std::string carryOut = AddVecSG(G, 1, c0, "carryOut");
outcasts.push_back(carryOut);
std::string v2 = AddVecSG(G, 2, carryOut, "v2");
outcasts.push_back(v2);
std::string k3 = AddIncast(G, "k3", incasts);
(void)AddCubeMatmulFrom(G, 3, v2, k3);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(4);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
auto slotScope = std::make_shared<TensorSlotScope>(function);
slotScope->ioslot.incastSlot = {{7}, {}};
slotScope->ioslot.outcastSlot = {{7}, {}};
function->SetSlotScope(slotScope);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
TEST_F(ReduceCopyTest, RunOnFunction_SharedEndpointSlotsGroupIntoSameRing)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::string carryInA = AddIncast(G, "carryInA", incasts);
std::string carryInB = AddIncast(G, "carryInB", incasts);
std::string c0 = AddCubeMatmulFrom(G, 0, carryInA, carryInB);
std::string o7 = AddVecSG(G, 1, c0, "o7");
outcasts.push_back(o7);
std::string o8 = AddVecSG(G, 2, c0, "o8");
outcasts.push_back(o8);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(3);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
auto slotScope = std::make_shared<TensorSlotScope>(function);
slotScope->ioslot.incastSlot = {{7}, {8}};
slotScope->ioslot.outcastSlot = {{7}, {8}};
function->SetSlotScope(slotScope);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
TEST_F(ReduceCopyTest, RunOnFunction_IndependentRingsRejectCrossRingMerge)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::string carryInA = AddIncast(G, "carryInA", incasts);
std::string k0 = AddIncast(G, "k0", incasts);
std::string c0 = AddCubeMatmulFrom(G, 0, carryInA, k0);
std::string o7 = AddVecSG(G, 1, c0, "o7");
outcasts.push_back(o7);
std::string carryInB = AddIncast(G, "carryInB", incasts);
std::string c2 = AddCubeMatmulFrom(G, 2, o7, carryInB);
std::string o8 = AddVecSG(G, 3, c2, "o8");
outcasts.push_back(o8);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(4);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
auto slotScope = std::make_shared<TensorSlotScope>(function);
slotScope->ioslot.incastSlot = {{7}, {}, {8}};
slotScope->ioslot.outcastSlot = {{7}, {8}};
function->SetSlotScope(slotScope);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
TEST_F(ReduceCopyTest, Merge_RootRotationAccumulatesLoopPathsOnRealRoot)
{
std::vector<std::set<int>> outGraph{{1}, {2}, {}};
MergeInput input = BuildSimpleMergeInput(3, outGraph, {{1, 2}, {0, 1}});
input.subgraphLoopPaths = {{9}, {7}, {7}};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
EXPECT_EQ(output.subgraphIdUpdated[0], output.subgraphIdUpdated[1]);
EXPECT_EQ(output.subgraphIdUpdated[1], output.subgraphIdUpdated[2]);
EXPECT_EQ(merger.FindParent(0), 1);
std::set<int> expected{7, 9};
EXPECT_EQ(merger.mRootLoopPaths[merger.FindParent(0)], expected);
}
TEST_F(ReduceCopyTest, SinkMergesWhenBalancedNotDwarfing)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
AddCubeMatmulFrom(G, 0, AddIncast(G, "QA0", incasts), AddIncast(G, "QB0", incasts));
AddVecSG(G, 1, "tC0", "v1a");
std::string c2a = AddCubeMatmulFrom(G, 2, "v1a", AddIncast(G, "VA0", incasts));
AddCubeMatmulFrom(G, 3, AddIncast(G, "QA1", incasts), AddIncast(G, "QB1", incasts));
AddVecSG(G, 4, "tC3", "v1b");
std::string c2b = AddCubeMatmulFrom(G, 5, "v1b", AddIncast(G, "VA1", incasts));
AddVecSinkSG(G, 6, {c2a, c2b}, "sinkGlmOut", outcasts);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(7);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
TEST_F(ReduceCopyTest, SinkRejectsPureConvergencePoint)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
AddCubeMatmulFrom(G, 0, AddIncast(G, "QA0", incasts), AddIncast(G, "QB0", incasts));
AddVecSG(G, 1, "tC0", "v1a");
std::string c2a = AddCubeMatmulFrom(G, 2, "v1a", AddIncast(G, "VA0", incasts));
AddCubeMatmulFrom(G, 3, AddIncast(G, "QA1", incasts), AddIncast(G, "QB1", incasts));
AddVecSG(G, 4, "tC3", "v1b");
std::string c2b = AddCubeMatmulFrom(G, 5, "v1b", AddIncast(G, "VA1", incasts));
AddVecSinkSG(G, 6, {c2a, c2b}, "sinkGlmOut", outcasts);
Function* function = G.GetFunction();
const int Num0 = 0;
for (auto& op : function->Operations()) {
if (op.GetSubgraphID() == 6) {
op.UpdateLatency(Num0);
}
}
function->SetTotalSubGraphCount(7);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num3 = 3;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num3);
}
TEST_F(ReduceCopyTest, SinkMergesWhenUnbalancedLossAbsorbable)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
AddCubeMatmulFrom(G, 0, AddIncast(G, "QA0", incasts), AddIncast(G, "QB0", incasts));
AddVecSG(G, 1, "tC0", "v1a");
std::string c2a = AddCubeMatmulFrom(G, 2, "v1a", AddIncast(G, "VA0", incasts));
AddCubeMatmulFrom(G, 3, AddIncast(G, "QA1", incasts), AddIncast(G, "QB1", incasts));
AddVecSG(G, 4, "tC3", "v1b");
std::string c2b = AddCubeMatmulFrom(G, 5, "v1b", AddIncast(G, "VA1", incasts));
AddVecSinkSG(G, 6, {c2a, c2b}, "sinkGlmOut", outcasts);
Function* function = G.GetFunction();
const int Num5 = 5;
for (auto& op : function->Operations()) {
if (op.GetSubgraphID() >= 3 && op.GetSubgraphID() <= 5) {
op.UpdateLatency(Num5);
}
}
function->SetTotalSubGraphCount(7);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
TEST_F(ReduceCopyTest, SinkBoundaryLossEqualRatioAllows)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
AddCubeMatmulFrom(G, 0, AddIncast(G, "QA0", incasts), AddIncast(G, "QB0", incasts));
AddVecSG(G, 1, "tC0", "v1a");
std::string c2a = AddCubeMatmulFrom(G, 2, "v1a", AddIncast(G, "VA0", incasts));
AddCubeMatmulFrom(G, 3, AddIncast(G, "QA1", incasts), AddIncast(G, "QB1", incasts));
AddVecSG(G, 4, "tC3", "v1b");
std::string c2b = AddCubeMatmulFrom(G, 5, "v1b", AddIncast(G, "VA1", incasts));
AddVecSinkSG(G, 6, {c2a, c2b}, "sinkGlmOut", outcasts);
Function* function = G.GetFunction();
const int Num10 = 10;
const int Num5 = 5;
for (auto& op : function->Operations()) {
if (op.GetSubgraphID() <= 5) {
op.UpdateLatency(Num10);
} else if (op.GetSubgraphID() == 6) {
op.UpdateLatency(Num5);
}
}
function->SetTotalSubGraphCount(7);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardExemptsPureVecLightFanout)
{
std::vector<std::set<int>> outGraph{{1, 2, 3}, {}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {0}, {2}, true, {}, {}}, {102, {0}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
input.subgraphAICOpNum = {2, 0, 0, 0};
input.subgraphAICLatency = {800, 0, 0, 0};
input.subgraphAIVLatency = {100, 50, 50, 50};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardKeepsCubeBranchFanoutRejected)
{
std::vector<std::set<int>> outGraph{{1, 2, 3}, {}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {0}, {2}, true, {}, {}}, {102, {0}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
input.subgraphAICOpNum = {2, 1, 0, 0};
input.subgraphAICLatency = {800, 50, 0, 0};
input.subgraphAIVLatency = {100, 50, 50, 50};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 4);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardRejectsHeavyPureVecFanout)
{
std::vector<std::set<int>> outGraph{{1, 2, 3}, {}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {0}, {2}, true, {}, {}}, {102, {0}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
input.subgraphAICOpNum = {2, 0, 0, 0};
input.subgraphAICLatency = {20, 0, 0, 0};
input.subgraphAIVLatency = {20, 500, 400, 300};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 4);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardPureVecBoundaryEqualRatioAllows)
{
std::vector<std::set<int>> outGraph{{1, 2, 3}, {}, {}, {}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {0}, {1}, true, {}, {}}, {101, {0}, {2}, true, {}, {}}, {102, {0}, {3}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
input.subgraphAICOpNum = {2, 0, 0, 0};
input.subgraphAICLatency = {25, 0, 0, 0};
input.subgraphAIVLatency = {0, 500, 100, 100};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardKeepsCubeBranchFaninRejected)
{
std::vector<std::set<int>> outGraph{{}, {0}, {0}, {0}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {1}, {0}, true, {}, {}}, {101, {2}, {0}, true, {}, {}}, {102, {3}, {0}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
input.subgraphAICOpNum = {2, 1, 0, 0};
input.subgraphAICLatency = {800, 50, 0, 0};
input.subgraphAIVLatency = {100, 50, 50, 50};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 4);
}
TEST_F(ReduceCopyTest, MixGraphMerger_HingeGuardExemptsPureVecLightFanin)
{
std::vector<std::set<int>> outGraph{{}, {0}, {0}, {0}};
MergeInput input = BuildSimpleMergeInput(4, outGraph, {{0, 1, 2, 3}});
input.isEnforceMergeGroup = {false};
input.boundaryTensors = {
{100, {1}, {0}, true, {}, {}}, {101, {2}, {0}, true, {}, {}}, {102, {3}, {0}, true, {}, {}}};
input.subgraphToBoundaryTensorIds = {{0, 1, 2}, {0}, {1}, {2}};
input.subgraphAICOpNum = {2, 0, 0, 0};
input.subgraphAICLatency = {800, 0, 0, 0};
input.subgraphAIVLatency = {100, 50, 50, 50};
MixGraphMerger merger;
MergeOutput output = merger.Merge(input);
EXPECT_EQ(output.numSubgraphUpdated, 1);
}
TEST_F(ReduceCopyTest, ReduceCopyMerge_MapAutoMixPartitionToLimits)
{
int maxAIC = 0;
int maxAIV = 0;
int maxTotal = 0;
EXPECT_FALSE(ReduceCopyMerge::MapAutoMixPartitionToLimits(0, maxAIC, maxAIV, maxTotal));
EXPECT_EQ(maxAIC, 2000);
EXPECT_EQ(maxAIV, 2240);
EXPECT_EQ(maxTotal, 0);
EXPECT_TRUE(ReduceCopyMerge::MapAutoMixPartitionToLimits(1, maxAIC, maxAIV, maxTotal));
EXPECT_EQ(maxAIC, 2000);
EXPECT_EQ(maxAIV, 2240);
EXPECT_EQ(maxTotal, 0);
EXPECT_TRUE(ReduceCopyMerge::MapAutoMixPartitionToLimits(2, maxAIC, maxAIV, maxTotal));
EXPECT_EQ(maxAIC, 1700);
EXPECT_EQ(maxAIV, 400);
EXPECT_EQ(maxTotal, 0);
EXPECT_TRUE(ReduceCopyMerge::MapAutoMixPartitionToLimits(3, maxAIC, maxAIV, maxTotal));
EXPECT_EQ(maxAIC, 1700);
EXPECT_EQ(maxAIV, 400);
EXPECT_EQ(maxTotal, 0);
EXPECT_TRUE(ReduceCopyMerge::MapAutoMixPartitionToLimits(100, maxAIC, maxAIV, maxTotal));
EXPECT_EQ(maxAIC, 1700);
EXPECT_EQ(maxAIV, 400);
EXPECT_EQ(maxTotal, 0);
EXPECT_TRUE(ReduceCopyMerge::MapAutoMixPartitionToLimits(101, maxAIC, maxAIV, maxTotal));
EXPECT_EQ(maxAIC, 0);
EXPECT_EQ(maxAIV, 0);
EXPECT_EQ(maxTotal, 101);
}
TEST_F(ReduceCopyTest, MixGraphMerger_TotalOpNumLimitRejectsMerge)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {}, {3}, {}}, {{0, 1}, {2, 3}});
input.subgraphAICOpNum = {30, 30, 5, 5};
input.subgraphAIVOpNum = {30, 30, 5, 5};
input.maxSubgraphTotalOpNum = 50;
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1, 2, 3};
EXPECT_FALSE(merger.CheckLatencyConstraint({0, 1}));
EXPECT_TRUE(merger.CheckLatencyConstraint({2, 3}));
}
TEST_F(ReduceCopyTest, RunOnFunction_DefaultLevelMerges)
{
ComputationalGraphBuilder G;
BuildMatmulAddsGraph(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 2;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
TEST_F(ReduceCopyTest, RunOnFunction_HighLevelMerges)
{
ComputationalGraphBuilder G;
BuildMatmulAddsGraph(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num2 = 2;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num2);
}
TEST_F(ReduceCopyTest, MixGraphMerger_ZeroSideLimitSkipsSideCheck)
{
MergeInput input = BuildSimpleMergeInput(4, {{1}, {}, {3}, {}}, {{0, 1}, {2, 3}});
input.subgraphAICOpNum = {30, 30, 5, 5};
input.subgraphAIVOpNum = {30, 30, 5, 5};
input.maxSubgraphAICOpNum = 0;
input.maxSubgraphAIVOpNum = 0;
input.maxSubgraphTotalOpNum = 50;
MixGraphMerger merger;
merger.mInput = input;
merger.mParent = {0, 1, 2, 3};
EXPECT_FALSE(merger.CheckLatencyConstraint({0, 1}));
EXPECT_TRUE(merger.CheckLatencyConstraint({2, 3}));
}
TEST_F(ReduceCopyTest, RunOnFunction_DisabledKeepsSubgraphs)
{
ComputationalGraphBuilder G;
BuildMatmulAddsGraph(G);
Function* function = G.GetFunction();
function->paramConfigs_.autoMixPartition = 0;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
EXPECT_EQ(function->GetTotalSubGraphCount(), 6);
}
TEST_F(ReduceCopyTest, RunOnFunction_NoSlotScopeMergesAll)
{
ComputationalGraphBuilder G;
std::vector<std::string> incasts;
std::vector<std::string> outcasts;
std::string carryIn = AddIncast(G, "carryIn", incasts);
std::string k0 = AddIncast(G, "k0", incasts);
std::string c0 = AddCubeMatmulFrom(G, 0, carryIn, k0);
std::string carryOut = AddVecSG(G, 1, c0, "carryOut");
outcasts.push_back(carryOut);
std::string v2 = AddVecSG(G, 2, carryOut, "v2");
outcasts.push_back(v2);
std::string k3 = AddIncast(G, "k3", incasts);
(void)AddCubeMatmulFrom(G, 3, v2, k3);
Function* function = G.GetFunction();
function->SetTotalSubGraphCount(4);
ASSERT_EQ(G.SetInCast(incasts), true);
ASSERT_EQ(G.SetOutCast(outcasts), true);
function->paramConfigs_.autoMixPartition = 1;
ReduceCopyMerge merger;
EXPECT_EQ(merger.RunOnFunction(*function), SUCCESS);
const int Num1 = 1;
EXPECT_EQ(function->GetTotalSubGraphCount(), Num1);
}
}
}