* -------------------------------------------------------------------------
* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* 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 FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
#include "MindIELLMParallelStrategyAlgorithm.h"
namespace Dic::Module::Summary {
const std::unordered_map<std::string, std::string> MindIELLMParallelStrategyAlgorithm::tokenExceptEp = {
{DP_GROUP, DP_GROUP}, {TP_GROUP, TP_GROUP}, {PP_GROUP, PP_GROUP}};
const std::unordered_map<std::string, std::string> MindIELLMParallelStrategyAlgorithm::tokenWithEp = {
{MOE_TP_GROUP, MOE_TP_GROUP_NAME}, {EP_GROUP, EP_GROUP_NAME}};
MindIELLMParallelStrategyAlgorithm::MindIELLMParallelStrategyAlgorithm() {
commInfoHandlers[DIMENSIONS_TP] =
std::bind(&MindIELLMParallelStrategyAlgorithm::ReduceCommTpDimensionDef, this, std::placeholders::_1);
commInfoHandlers[DIMENSIONS_PP] =
std::bind(&MindIELLMParallelStrategyAlgorithm::ReduceCommPpDimensionDef, this, std::placeholders::_1);
}
MindIELLMParallelStrategyAlgorithm::~MindIELLMParallelStrategyAlgorithm() = default;
bool MindIELLMParallelStrategyAlgorithm::UpdateParallelDimension(
const std::string &tmpDimension, const ParallelStrategyConfig &tmpConfig, std::string &err) {
CalStrategyConfig(tmpDimension, tmpConfig);
if (tmpConfig.algorithm == MINDIE_LLM_TP_DP_EP_PP_MOETP_ALG) {
paraOrder = {TP_PARA, DP_PARA, PP_PARA};
paraOrderWithEp = {TP_PARA, DP_PARA, PP_PARA, MOE_TP_PARA, EP_PARA};
} else {
err = "Failed to update parallel view. Unexpected algorithm for the MindIE-LLM.";
SetSummaryError(ErrorCode::UPDATE_PARALLEL_VIEW_FAILED);
return false;
}
bool res = UpdateShowMap(err);
if (res) {
UpdateOrderAndParallelSize();
UpdateElementSize();
}
return res;
}
void MindIELLMParallelStrategyAlgorithm::UpdateOrderAndParallelSize() {
updatedOrder = paraOrder;
updatedOrder.erase(std::remove_if(updatedOrder.begin(), updatedOrder.end(),
[this](const std::string &group) { return !(paraDetailsMap[group].isShown); }),
updatedOrder.end());
parallelSize.clear();
for (const auto ¶ : updatedOrder) {
parallelSize.push_back(paraDetailsMap[para].size);
}
if (!paraDetailsMap[EP_PARA].isShown) {
return;
}
updatedOrderWithEp = {MOE_TP_PARA, EP_PARA, PP_PARA};
updatedOrderWithEp.erase(std::remove_if(updatedOrderWithEp.begin(), updatedOrderWithEp.end(),
[this](const std::string &group) { return !(paraDetailsMap[group].isShown); }),
updatedOrderWithEp.end());
parallelSizeWithEp.clear();
for (const auto ¶ : updatedOrderWithEp) {
parallelSizeWithEp.push_back(paraDetailsMap[para].size);
}
}
void MindIELLMParallelStrategyAlgorithm::SetIndicatorAttr() {
if (dimension == DIMENSIONS_TP) {
SetTpIndicatorAttr();
} else if (dimension == DIMENSIONS_PP) {
SetPpIndicatorAttr();
} else if (dimension == DIMENSIONS_DP) {
SetDpIndicatorAttr();
} else {
Server::ServerLog::Error("Failed to set indicator attributes for the MindIE-LLM. Unexpected dimension.");
}
}
void MindIELLMParallelStrategyAlgorithm::UpdateIndexAttributes(
std::unordered_map<std::string, uint32_t> &indexAttributes) {
std::string curIndex;
for (const auto &curPara : paraOrder) {
if (indexAttributes[curPara + STR_INDEX] < paraDetailsMap[curPara].size - 1) {
indexAttributes[curPara + STR_INDEX]++;
break;
}
if (curPara != paraOrder.back()) {
indexAttributes[curPara + STR_INDEX] = 0;
}
}
if (dimension != DIMENSIONS_TP) {
return;
}
static std::vector<std::string> updateIndexListForMoe = {MOE_TP_PARA, EP_PARA};
for (const auto &curPara : updateIndexListForMoe) {
if (indexAttributes[curPara + STR_INDEX] < paraDetailsMap[curPara].size - 1) {
indexAttributes[curPara + STR_INDEX]++;
break;
}
indexAttributes[curPara + STR_INDEX] = 0;
}
}
void MindIELLMParallelStrategyAlgorithm::GetPerArrangement(
uint32_t index, std::unordered_map<std::string, uint32_t> &indexAttributes) {
Element element;
element.index = index;
if (index != 0) {
UpdateIndexAttributes(indexAttributes);
}
element.indexAttributes = indexAttributes;
element.name = GetElementName(indexAttributes);
element.position = GetElementPosition(indexAttributes);
element.ranks = GetElementContainRanks(index, indexAttributes, element.formattedRanks);
data.arrangements.push_back(element);
}
bool MindIELLMParallelStrategyAlgorithm::GetConnectionsByToken(std::string &err, ParaMode mode) {
std::unordered_map<std::string, std::string> tmpToken;
if (mode == ParaMode::TP_DP_PP) {
tmpToken = tokenExceptEp;
} else {
tmpToken = tokenWithEp;
}
for (const auto &[token, groupName] : tmpToken) {
bool hasTokenGroup = true;
std::vector<std::string> parallelGroups = StringUtil::Split(token, "-");
for (const auto &group : parallelGroups) {
if (!paraDetailsMap[group].isShown) {
hasTokenGroup = false;
break;
}
}
if (!hasTokenGroup) {
continue;
}
allGroupsType ranks{};
if (mode == ParaMode::TP_DP_PP) {
ranks = ParallelStrategyAlgorithmHelper::GetAllGroupsRanksByToken(
parallelGroups, parallelSize, updatedOrder, wordSize);
} else {
ranks = ParallelStrategyAlgorithmHelper::GetAllGroupsRanksByToken(
parallelGroups, parallelSizeWithEp, updatedOrderWithEp, wordSize);
}
if (ranks.empty()) {
err = "Failed to get connections by token list for Megatron. Group name: " + groupName;
return false;
}
for (const auto &rank : ranks) {
data.connections.emplace_back(groupName, rank, std::vector<std::string>{});
}
}
return true;
}
bool MindIELLMParallelStrategyAlgorithm::GetConnectionsByTokenList(std::string &err) {
if (wordSize == 1) {
err = "Failed to get connections for the MindIE-LLM. Parallel strategy configs have not been updated yet.";
SetSummaryError(ErrorCode::GET_ALGORITHM_CONNECTIONS_FAILED);
return false;
}
if (!GetConnectionsByToken(err, ParaMode::TP_DP_PP)) {
return false;
}
if (dimension == DIMENSIONS_TP && paraDetailsMap[EP_PARA].isShown &&
!GetConnectionsByToken(err, ParaMode::MOE_TP_EP_PP)) {
return false;
}
if (dimension == DIMENSIONS_TP) {
allCommunicationGroups = data.connections;
}
return true;
}
bool MindIELLMParallelStrategyAlgorithm::GenerateArrangementByDimension(std::string &err) {
ClearArrangementData();
SetIndicatorAttr();
std::unordered_map<std::string, uint32_t> indexAttributes;
for (const auto ¶ : paraOrder) {
indexAttributes[para + STR_INDEX] = 0;
}
indexAttributes[MOE_TP_INDEX] = 0;
indexAttributes[EP_INDEX] = 0;
indexAttributes[CP_INDEX] = 0;
for (uint32_t index = 0; index < elementSize; index++) {
GetPerArrangement(index, indexAttributes);
}
if (!GetConnectionsByTokenList(err)) {
return false;
}
return true;
}
bool MindIELLMParallelStrategyAlgorithm::GetPerformanceIndicatorByDimension(
const GetPerformanceIndicatorParam &performanceParams,
const std::unordered_map<std::uint32_t, StepStatistic> &statistic, std::vector<IndicatorDataStruct> &indicatorData,
std::string &err) {
if (!(strategyConfig == performanceParams.config)) {
err = "Failed to get parallelism performance indicator for the MindIE-LLM. Unexpected parallel config.";
return false;
}
tpSize = strategyConfig.tpSize;
wordSize = strategyConfig.tpSize * strategyConfig.ppSize * strategyConfig.dpSize;
if (performanceParams.dimension == DIMENSIONS_TP) {
CalculatePerformanceDataWithTpDimension(statistic, indicatorData);
return true;
}
ReduceTpPerformance(statistic);
if (performanceParams.dimension == DIMENSIONS_PP) {
CalculatePerformanceDataWithCpDimension(indicatorData);
return true;
}
ReduceCpPerformance();
ReducePpPerformanceForPpLast();
if (performanceParams.dimension == DIMENSIONS_DP) {
GetPerformanceResponseDataWithDpDimension(reducePpStatistic, indicatorData);
return true;
}
err = "Failed to get parallelism performance indicator for the MindIE-LLM. Unexpected dimension.";
return false;
}
void MindIELLMParallelStrategyAlgorithm::CalAdviceInfo(
const std::string &dimension, std::vector<std::string> &advices, std::vector<IndicatorDataStruct> &indicatorData) {
BaseParallelStrategyAlgorithm::CalAdviceInfo(dimension, advices, indicatorData);
}
std::vector<Connection> MindIELLMParallelStrategyAlgorithm::GetAllCommunicationGroups(std::string &err) {
if (allCommunicationGroups.empty() && !GetConnectionsByTokenList(err)) {
return {};
}
return allCommunicationGroups;
}
CommInfoMap MindIELLMParallelStrategyAlgorithm::GetCommInfoByDimension(
const CommInfoMap &expandCommInfos, const std::string &dimension) {
auto res = BaseParallelStrategyAlgorithm::GetCommInfoByDimension(expandCommInfos, dimension);
if (dimension == DIMENSIONS_TP) {
return res;
}
for (auto &item : res) {
auto &commInfo = item.second;
commInfo.erase(std::remove_if(commInfo.begin(), commInfo.end(),
[](const CommInfoUnderRank &info) {
return (info.pgName == MOE_TP_GROUP_NAME) || (info.pgName == EP_GROUP_NAME);
}),
commInfo.end());
}
return res;
}
}