* 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 mix_subgraph_split.h
* \brief 将Mix子图拆分为多个独立的Cube和Vector子图,并重新分配subgraphID
*/
#ifndef PASS_MIX_SUBGRAPH_SPLIT_H
#define PASS_MIX_SUBGRAPH_SPLIT_H
#include "passes/pass_interface/pass.h"
#include "interface/function/function.h"
#include "interface/operation/operation.h"
#include "interface/program/program.h"
#include "interface/tensor/logical_tensor.h"
#include "tilefwk/tilefwk.h"
#include "passes/block_graph_pass/mix_subgraph_split/mix_subgraph_split_utils.h"
#include "passes/tile_graph_pass/subgraph_to_function.h"
#include "passes/block_graph_pass/mix_subgraph_split/mix_internal_components_analyzer.h"
#include "passes/block_graph_pass/mix_subgraph_split/mix_dependency_analyzer.h"
#include "passes/block_graph_pass/mix_subgraph_split/function_clone.h"
#include "passes/block_graph_pass/mix_subgraph_split/mix_call_operation_builder.h"
#include <unordered_map>
#include <set>
#include <vector>
namespace npu {
namespace tile_fwk {
struct MixSubgraphSplitResult {
uint64_t originalProgramID;
Function* originalFunction;
std::vector<uint64_t> newProgramIDs;
std::vector<Function*> newFunctions;
std::vector<InternalComponentInfo> components;
std::vector<Operation*> originalCallOps;
};
struct MixSubgraphInfo {
uint64_t programID;
Function* function;
std::vector<InternalComponentInfo> components;
std::vector<Operation*> originalCallOps;
FunctionHash hashValue;
bool isLocalFunction;
MixSubgraphInfo(
uint64_t pid, Function* func, std::vector<InternalComponentInfo> comp, std::vector<Operation*> ops,
FunctionHash hash, bool isLocal)
: programID(pid),
function(func),
components(std::move(comp)),
originalCallOps(std::move(ops)),
hashValue(hash),
isLocalFunction(isLocal)
{}
};
struct GlobalSplitRecord {
Function* originalLeafFunc;
std::vector<Function*> splitFunctions;
std::vector<uint64_t> programIDs;
std::vector<InternalComponentInfo> components;
uint64_t mixId;
std::shared_ptr<AnalyzerOutput> analyzerOutput;
GlobalSplitRecord() : originalLeafFunc(nullptr), mixId(0) {}
~GlobalSplitRecord() = default;
std::string GetSummary() const
{
std::stringstream ss;
ss << "GlobalSplitRecord{";
ss << "originalFunc=" << (originalLeafFunc ? originalLeafFunc->GetRawName() : "null");
ss << ", subFuncs=" << splitFunctions.size();
ss << ", components=" << components.size();
ss << ", mixId=" << mixId;
ss << ", hasAnalyzerOutput=" << (analyzerOutput ? "true" : "false");
ss << "}";
return ss.str();
}
bool IsValid() const
{
if (originalLeafFunc == nullptr)
return false;
if (splitFunctions.empty())
return false;
if (splitFunctions.size() != components.size())
return false;
return true;
}
};
class MixSubgraphSplit : public Pass {
public:
MixSubgraphSplit() : Pass("MixSubgraphSplit") { SetSupportedArches({NPUArch::DAV_3510}); }
~MixSubgraphSplit() override = default;
Status RunOnFunction(Function& function) override;
void Reset() override;
private:
MixInternalComponentsAnalyzer componentsAnalyzer_;
MixDependencyAnalyzer dependencyAnalyzer_;
MixCallOperationBuilder callOpBuilder_;
static std::unordered_map<FunctionHash, GlobalSplitRecord> globalSplitRecords_;
Function* currentRootFunc_ = nullptr;
void DisplayComponents(const std::vector<InternalComponentInfo>& components);
Status GenNewFunctions(
Function& rootFunc, Function* originalMixFunc, const std::vector<InternalComponentInfo>& components,
const std::vector<uint64_t>& newProgramIDs, SubgraphToFunction& subgraphToFunction,
std::vector<Function*>& newFunctions, uint64_t mixId, MixResourceType resourceType);
Status ApplySplitResultsWithRemap(
Function& function, const std::vector<MixSubgraphSplitResult>& splitResults,
const std::unordered_map<uint64_t, uint64_t>& programIDRemap,
const std::unordered_map<uint64_t, std::vector<uint64_t>>& mixSubgraphNewIDs);
void DeleteOriginalMixCallOps(Function& rootFunc, const std::vector<Operation*>& callOpsToDelete);
bool IsMixSubgraph(Function& leafFunc) const;
MixResourceType GetMixResourceType(Function& mixFunc) const;
Status GatherSubGraphInfo(
Function& function, std::vector<MixSubgraphInfo>& mixSubgraphs, std::set<uint64_t>& mixSubgraphIDsToDelete,
std::vector<Operation*>& callOpsToDelete);
Status CalculateSplit(
Function& function, std::vector<MixSubgraphInfo>& mixSubgraphs, std::set<uint64_t>& mixSubgraphIDsToDelete,
std::unordered_map<uint64_t, std::vector<uint64_t>>& mixSubgraphNewIDs,
std::unordered_map<uint64_t, uint64_t>& programIDRemap);
Status ExecuteSplit(
Function& function, std::vector<MixSubgraphInfo>& mixSubgraphs, std::vector<Operation*> callOpsToDelete,
std::unordered_map<uint64_t, std::vector<uint64_t>>& mixSubgraphNewIDs,
std::unordered_map<uint64_t, uint64_t>& programIDRemap);
Status ProcessLeafFunction(
Function& rootFunc, const MixSubgraphInfo& mixInfo, const std::vector<uint64_t>& newProgramIDs,
std::vector<MixSubgraphSplitResult>& splitResults);
void RecordSplitResult(
Function* leafFunc, const std::vector<Function*>& newFunctions, const std::vector<uint64_t>& newProgramIDs,
const std::vector<InternalComponentInfo>& components, uint64_t mixId,
const std::shared_ptr<AnalyzerOutput>& analyzerOutput);
void ApplyFinalDependencies(
const std::vector<Function*>& newFunctions,
const std::unordered_map<int, std::vector<SimpleTensorParam>>& allIncasts,
const std::unordered_map<int, std::vector<SimpleTensorParam>>& allOutcasts) const;
void ApplyIncastDependencies(
Function* leafFunc, int componentId, const std::vector<SimpleTensorParam>& incastParams) const;
void ApplyOutcastDependencies(
Function* leafFunc, int componentId, const std::vector<SimpleTensorParam>& outcastParams) const;
static std::atomic<uint64_t> globalNextMixId_;
static constexpr uint64_t INVALID_PROGRAM_ID = static_cast<uint64_t>(-1);
};
}
}
#endif