| @@ -51,6 +51,7 @@ std::string GetReadableTypeName(const std::type_info& type) | |||
| 51 | {typeid(std::vector<int64_t>), "list[int64]"}, | 51 | {typeid(std::vector<int64_t>), "list[int64]"}, |
| 52 | {typeid(std::vector<double>), "list[double]"}, | 52 | {typeid(std::vector<double>), "list[double]"}, |
| 53 | {typeid(std::vector<std::string>), "list[string]"}, | 53 | {typeid(std::vector<std::string>), "list[string]"}, |
| 54 | + {typeid(std::vector<std::pair<std::string, std::string>>), "list[list[string]]"}, | ||
| 54 | {typeid(std::map<int64_t, int64_t>), "dict[int64, int64]"}, | 55 | {typeid(std::map<int64_t, int64_t>), "dict[int64, int64]"}, |
| 55 | {typeid(CubeTile), "CubeTile"}, | 56 | {typeid(CubeTile), "CubeTile"}, |
| 56 | {typeid(ConvTile), "ConvTile"}, | 57 | {typeid(ConvTile), "ConvTile"}, |
| @@ -291,6 +292,12 @@ void ValidateConfigValueType(const std::string& key, const std::any& value) | |||
| 291 | return; | 292 | return; |
| 292 | } | 293 | } |
| 293 | 294 | ||
| 295 | + // ready_on_host_tensors accepts both list[string] and list[list[string]] forms. | ||
| 296 | + if (key == std::string("runtime.") + READY_ON_HOST_TENSORS && | ||
| 297 | + value.type() == typeid(std::vector<std::pair<std::string, std::string>>)) { | ||
| 298 | + return; | ||
| 299 | + } | ||
| 300 | + | ||
| 294 | std::stringstream os; | 301 | std::stringstream os; |
| 295 | os << "Option '" << key << "' has invalid type. Expected " << GetReadableTypeName(expectedType) << ", but got " | 302 | os << "Option '" << key << "' has invalid type. Expected " << GetReadableTypeName(expectedType) << ", but got " |
| 296 | << GetReadableTypeName(value.type()); | 303 | << GetReadableTypeName(value.type()); |
| @@ -640,6 +647,8 @@ template void SetOptionsNg<std::map<int, int>>(const std::string& key, const std | |||
| 640 | template void SetOptionsNg<std::map<long, long>>(const std::string& key, const std::map<long, long>& value); | 647 | template void SetOptionsNg<std::map<long, long>>(const std::string& key, const std::map<long, long>& value); |
| 641 | template void SetOptionsNg<std::vector<int>>(const std::string& key, const std::vector<int>& value); | 648 | template void SetOptionsNg<std::vector<int>>(const std::string& key, const std::vector<int>& value); |
| 642 | template void SetOptionsNg<std::vector<std::string>>(const std::string& key, const std::vector<std::string>& value); | 649 | template void SetOptionsNg<std::vector<std::string>>(const std::string& key, const std::vector<std::string>& value); |
| 650 | +template void SetOptionsNg<std::vector<std::pair<std::string, std::string>>>( | ||
| 651 | + const std::string& key, const std::vector<std::pair<std::string, std::string>>& value); | ||
| 643 | template void SetOptionsNg<std::vector<double>>(const std::string& key, const std::vector<double>& value); | 652 | template void SetOptionsNg<std::vector<double>>(const std::string& key, const std::vector<double>& value); |
| 644 | template void SetOptionsNg<std::vector<int64_t>>(const std::string& key, const std::vector<int64_t>& value); | 653 | template void SetOptionsNg<std::vector<int64_t>>(const std::string& key, const std::vector<int64_t>& value); |
| 645 | 654 | ||
| @@ -360,8 +360,8 @@ | |||
| 360 | "type": "string" | 360 | "type": "string" |
| 361 | }, | 361 | }, |
| 362 | "label": { | 362 | "label": { |
| 363 | - "zh": "标记在Host端准备好的Tensor名称列表", | 363 | + "zh": "标记在Host端准备好的Tensor名称列表,支持list[string]或list[tuple[str, str]]", |
| 364 | - "en": "Specify the list of Tensor names that are ready on Host" | 364 | + "en": "Specify Tensor names ready on Host. Supports list[string] or list[tuple[str, str]]" |
| 365 | } | 365 | } |
| 366 | }, | 366 | }, |
| 367 | "device_sched_parallelism": { | 367 | "device_sched_parallelism": { |
| @@ -369,6 +369,9 @@ struct DyndevFunctionAttribute { | |||
| 369 | }; | 369 | }; |
| 370 | std::unordered_map<Function*, ValueDependDesc> valueDependDescDict; | 370 | std::unordered_map<Function*, ValueDependDesc> valueDependDescDict; |
| 371 | 371 | ||
| 372 | + std::vector<std::pair<std::string, std::string>> readyOnHostCpuPairs; | ||
| 373 | + std::vector<size_t> valueDependInputIndices; | ||
| 374 | + | ||
| 372 | struct FunctionGroup { | 375 | struct FunctionGroup { |
| 373 | /* loop */ | 376 | /* loop */ |
| 374 | OrderedSet<Function*> loopList; | 377 | OrderedSet<Function*> loopList; |
| @@ -561,7 +561,8 @@ std::string SymbolicExpressionTable::BuildExpressionTempVarInit(int indent) | |||
| 561 | 561 | ||
| 562 | bool SymbolicExpressionTable::CheckExprDependCore(const RawSymbolicScalarPtr& raw, | 562 | bool SymbolicExpressionTable::CheckExprDependCore(const RawSymbolicScalarPtr& raw, |
| 563 | const std::unordered_map<std::string, bool>& tensorNameToDependCore, | 563 | const std::unordered_map<std::string, bool>& tensorNameToDependCore, |
| 564 | - std::unordered_map<RawSymbolicScalarPtr, bool>& valDependMap) | 564 | + std::unordered_map<RawSymbolicScalarPtr, bool>& valDependMap, |
| 565 | + std::unordered_set<std::string>& valueDependTensorNames) | ||
| 565 | { | 566 | { |
| 566 | switch (raw->Kind()) { | 567 | switch (raw->Kind()) { |
| 567 | case SymbolicScalarKind::T_SCALAR_SYMBOLIC_IMMEDIATE: | 568 | case SymbolicScalarKind::T_SCALAR_SYMBOLIC_IMMEDIATE: |
| @@ -587,6 +588,7 @@ bool SymbolicExpressionTable::CheckExprDependCore(const RawSymbolicScalarPtr& ra | |||
| 587 | auto argExpr = operandList[1]; | 588 | auto argExpr = operandList[1]; |
| 588 | const std::string& argName = std::dynamic_pointer_cast<RawSymbolicSymbol>(argExpr)->Name(); | 589 | const std::string& argName = std::dynamic_pointer_cast<RawSymbolicSymbol>(argExpr)->Name(); |
| 589 | FE_LOGI("[RunCmd] Value depend tensor name:%s", argName.c_str()); | 590 | FE_LOGI("[RunCmd] Value depend tensor name:%s", argName.c_str()); |
| 591 | + valueDependTensorNames.insert(argName); | ||
| 590 | auto it = tensorNameToDependCore.find(argName); | 592 | auto it = tensorNameToDependCore.find(argName); |
| 591 | FE_ASSERT(FeError::NOT_EXIST, it != tensorNameToDependCore.end()) | 593 | FE_ASSERT(FeError::NOT_EXIST, it != tensorNameToDependCore.end()) |
| 592 | << "Tensor " << argName << " not found in tensorNameToDependCore"; | 594 | << "Tensor " << argName << " not found in tensorNameToDependCore"; |
| @@ -596,7 +598,7 @@ bool SymbolicExpressionTable::CheckExprDependCore(const RawSymbolicScalarPtr& ra | |||
| 596 | } | 598 | } |
| 597 | // Recursively check all operands | 599 | // Recursively check all operands |
| 598 | for (const auto& operand : expr->OperandList()) { | 600 | for (const auto& operand : expr->OperandList()) { |
| 599 | - if (CheckExprDependCore(operand, tensorNameToDependCore, valDependMap)) { | 601 | + if (CheckExprDependCore(operand, tensorNameToDependCore, valDependMap, valueDependTensorNames)) { |
| 600 | return true; | 602 | return true; |
| 601 | } | 603 | } |
Blocker: 递归中 operand 命中即 ![]() ![]() | |||
| 602 | } | 604 | } |
| @@ -1003,7 +1003,8 @@ struct SymbolicExpressionTable { | |||
| 1003 | 1003 | ||
| 1004 | static bool CheckExprDependCore(const RawSymbolicScalarPtr& ss, | 1004 | static bool CheckExprDependCore(const RawSymbolicScalarPtr& ss, |
| 1005 | const std::unordered_map<std::string, bool>& tensorNameToDependCore, | 1005 | const std::unordered_map<std::string, bool>& tensorNameToDependCore, |
| 1006 | - std::unordered_map<RawSymbolicScalarPtr, bool>& valDependMap); | 1006 | + std::unordered_map<RawSymbolicScalarPtr, bool>& valDependMap, |
| 1007 | + std::unordered_set<std::string>& valueDependTensorNames); | ||
| 1007 | 1008 | ||
| 1008 | private: | 1009 | private: |
| 1009 | static std::string BuildSymbolName(const std::string& name); | 1010 | static std::string BuildSymbolName(const std::string& name); |
| @@ -392,45 +392,110 @@ static void BuildConstructAssembleNeedAllocRuntimeSlots(FunctionCache& cache, Fu | |||
| 392 | } | 392 | } |
| 393 | } | 393 | } |
| 394 | 394 | ||
| 395 | +void ValDependTensorMeta::CheckValueDependCpuTensor() | ||
| 396 | +{ | ||
| 397 | + if (valueDependTensorNames.empty()) { | ||
| 398 | + return; | ||
| 399 | + } | ||
| 400 | + auto currDynFunc = Program::GetInstance().GetCurrentDynamicFunction(); | ||
| 401 | + if (currDynFunc == nullptr) { | ||
| 402 | + return; | ||
| 403 | + } | ||
| 404 | + auto currDynFuncAttr = currDynFunc->GetDyndevAttribute(); | ||
| 405 | + if (currDynFuncAttr == nullptr) { | ||
| 406 | + return; | ||
| 407 | + } | ||
| 408 | + for (const auto& name : valueDependTensorNames) { | ||
| 409 | + std::string rawName = name; | ||
| 410 | + if (CheckArgPrefix(rawName)) { | ||
| 411 | + rawName = rawName.substr(std::string(SPECIAL_SYMBOL_NAME_ARG_PREFIX).size()); | ||
| 412 | + } | ||
| 413 | + bool hasCpu = false; | ||
| 414 | + for (const auto& pair : currDynFuncAttr->readyOnHostCpuPairs) { | ||
| 415 | + if (pair.first == rawName) { | ||
| 416 | + hasCpu = true; | ||
| 417 | + break; | ||
| 418 | + } | ||
| 419 | + } | ||
| 420 | + if (!hasCpu) { | ||
| 421 | + disableCtrlFlowCache = true; | ||
backend 里控制流生成时候也有设置为 true的地方, 两者统一没 disableCtrlFlowCache = true; cputensor 检查语义应该重点关注从 true disableCtrlFlowCache 变成false吧, ![]() ![]() | |||
| 422 | + break; | ||
| 423 | + } | ||
| 424 | + } | ||
| 425 | +} | ||
| 426 | + | ||
| 395 | void InsertWaitCoreStart(SymbolicExpressionTable* exprTable, std::ostringstream& controlFlowOss, | 427 | void InsertWaitCoreStart(SymbolicExpressionTable* exprTable, std::ostringstream& controlFlowOss, |
| 396 | ValDependTensorMeta& valDependTensorMeta, int indent) | 428 | ValDependTensorMeta& valDependTensorMeta, int indent) |
| 397 | { | 429 | { |
| 398 | if (exprTable == nullptr) { | 430 | if (exprTable == nullptr) { |
| 399 | return; | 431 | return; |
| 400 | } | 432 | } |
| 401 | - bool needSync = false; | 433 | + bool needWaitAicoreStart = false; |
| 402 | const auto& primaryExprs = exprTable->GetPrimaryExpressionSet(); | 434 | const auto& primaryExprs = exprTable->GetPrimaryExpressionSet(); |
| 403 | for (const auto& expr : primaryExprs) { | 435 | for (const auto& expr : primaryExprs) { |
| 404 | if (expr == nullptr) { | 436 | if (expr == nullptr) { |
| 405 | continue; | 437 | continue; |
| 406 | } | 438 | } |
| 407 | if (exprTable->CheckExprDependCore(expr, valDependTensorMeta.tensorNameToDependCore, | 439 | if (exprTable->CheckExprDependCore(expr, valDependTensorMeta.tensorNameToDependCore, |
| 408 | - valDependTensorMeta.valDependMap)) { | 440 | + valDependTensorMeta.valDependMap, |
| 409 | - needSync = true; | 441 | + valDependTensorMeta.valueDependTensorNames)) { |
| 442 | + needWaitAicoreStart = true; | ||
老代码 break 为什么丢了 ![]() ![]() | |||
| 410 | break; | 443 | break; |
| 411 | } | 444 | } |
| 412 | } | 445 | } |
如果needWaitAicoreStart 是true 就不用再走 cputensor 检查 ![]() ![]() | |||
| 413 | - if (needSync) { | 446 | + if (needWaitAicoreStart) { |
| 414 | controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; | 447 | controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; |
MarkValueDependDisableCache(ctx, func); InsertWaitCoreStartForLoopBounds(attr, ctx.controlFlowOss, ctx.valDependTensorMeta, indent); 场景考虑了没 ![]() ![]() | |||
| 415 | } | 448 | } |
| 416 | } | 449 | } |
| 417 | 450 | ||
| 418 | void GetReadyOnHostTensorsSet(std::unordered_set<int>& readyOnHostTensorsSet) | 451 | void GetReadyOnHostTensorsSet(std::unordered_set<int>& readyOnHostTensorsSet) |
| 419 | { | 452 | { |
| 420 | - const auto& readyOnHostTensors = config::GetRuntimeOption<std::vector<std::string>>(READY_ON_HOST_TENSORS); | 453 | + const std::any& configAny = ConfigManagerNg::CurrentScope()->GetAnyConfig(std::string("runtime.") + |
| 454 | + READY_ON_HOST_TENSORS); | ||
| 421 | auto attr = Program::GetInstance().GetCurrentDynamicFunction()->GetDyndevAttribute(); | 455 | auto attr = Program::GetInstance().GetCurrentDynamicFunction()->GetDyndevAttribute(); |
| 422 | auto inputSize = attr->startArgsInputLogicalTensorList.size(); | 456 | auto inputSize = attr->startArgsInputLogicalTensorList.size(); |
| 423 | - for (const auto& tensorStr : readyOnHostTensors) { | 457 | + |
| 458 | + auto findTensorIndex = [&](const std::string& tensorStr) -> size_t { | ||
| 424 | size_t i = 0; | 459 | size_t i = 0; |
| 425 | for (; i < inputSize; i++) { | 460 | for (; i < inputSize; i++) { |
| 426 | if (tensorStr == attr->startArgsInputLogicalTensorList[i]->Symbol()) { | 461 | if (tensorStr == attr->startArgsInputLogicalTensorList[i]->Symbol()) { |
| 427 | - MACHINE_LOGI("Tensor[%zu][%s] is ready on host.", i, tensorStr.c_str()); | 462 | + return i; |
| 428 | - readyOnHostTensorsSet.insert(i); | 463 | + } |
| 429 | - break; | 464 | + } |
| 465 | + return inputSize; | ||
| 466 | + }; | ||
| 467 | + | ||
| 468 | + if (configAny.type() == typeid(std::vector<std::string>)) { | ||
| 469 | + const auto& readyOnHostTensors = std::any_cast<const std::vector<std::string>&>(configAny); | ||
| 470 | + for (const auto& tensorStr : readyOnHostTensors) { | ||
| 471 | + size_t idx = findTensorIndex(tensorStr); | ||
| 472 | + ASSERT(DevCommonErr::PARAM_CHECK_FAILED, idx < inputSize) | ||
| 473 | + << "Tensor " << tensorStr << " not found in input list, please check [ready_on_host_tensors] config."; | ||
| 474 | + MACHINE_LOGI("Tensor[%zu][%s] is ready on host.", idx, tensorStr.c_str()); | ||
| 475 | + readyOnHostTensorsSet.insert(idx); | ||
| 476 | + } | ||
| 477 | + } else if (configAny.type() == typeid(std::vector<std::pair<std::string, std::string>>)) { | ||
| 478 | + const auto& readyOnHostPairs = std::any_cast<const std::vector<std::pair<std::string, std::string>>&>( | ||
| 479 | + configAny); | ||
| 480 | + for (const auto& pair : readyOnHostPairs) { | ||
| 481 | + const std::string& deviceName = pair.first; | ||
| 482 | + const std::string& cpuName = pair.second; | ||
| 483 | + size_t idx = findTensorIndex(deviceName); | ||
| 484 | + ASSERT(DevCommonErr::PARAM_CHECK_FAILED, idx < inputSize) | ||
| 485 | + << "Tensor " << deviceName << " not found in input list, please check [ready_on_host_tensors] config."; | ||
| 486 | + readyOnHostTensorsSet.insert(idx); | ||
| 487 | + if (cpuName.empty()) { | ||
| 488 | + MACHINE_LOGI("Tensor[%zu][%s] is ready on host (no cpu tensor).", idx, deviceName.c_str()); | ||
| 489 | + } else { | ||
| 490 | + size_t cpuIdx = findTensorIndex(cpuName); | ||
| 491 | + ASSERT(DevCommonErr::PARAM_CHECK_FAILED, cpuIdx < inputSize) | ||
| 492 | + << "Tensor " << cpuName << " not found in input list, please check [ready_on_host_tensors] config."; | ||
| 493 | + MACHINE_LOGI("Tensor[%zu][%s] is ready on host, cpu tensor[%zu][%s].", idx, deviceName.c_str(), cpuIdx, | ||
| 494 | + cpuName.c_str()); | ||
| 495 | + attr->readyOnHostCpuPairs.push_back({deviceName, cpuName}); | ||
| 496 | + attr->valueDependInputIndices.push_back(cpuIdx); | ||
| 430 | } | 497 | } |
| 431 | } | 498 | } |
| 432 | - ASSERT(DevCommonErr::PARAM_CHECK_FAILED, i < inputSize) | ||
| 433 | - << "Tensor " << tensorStr << " not found in input list, please check [ready_on_host_tensors] config."; | ||
| 434 | } | 499 | } |
| 435 | } | 500 | } |
| 436 | static std::string Arm64TargetTool(const std::string& bin) | 501 | static std::string Arm64TargetTool(const std::string& bin) |
| @@ -495,7 +560,7 @@ static void FillL2PrefetchInfo(std::shared_ptr<DyndevFunctionAttribute> attr) | |||
| 495 | } | 560 | } |
| 496 | } | 561 | } |
| 497 | 562 | ||
| 498 | -static void SetDyndevProgBinary(Function* function, bool disableCtrlFlowCache, | 563 | +static void SetDyndevProgBinary(Function* function, bool disableCtrlFlowCache, bool hasValueDepend, |
| 499 | const std::unordered_map<int, dynamic::SlotMaskEntry>* stitchUpdateSlotMaskMap) | 564 | const std::unordered_map<int, dynamic::SlotMaskEntry>* stitchUpdateSlotMaskMap) |
| 500 | { | 565 | { |
| 501 | if (function == nullptr || function->GetDyndevAttribute() == nullptr) { | 566 | if (function == nullptr || function->GetDyndevAttribute() == nullptr) { |
| @@ -509,6 +574,7 @@ static void SetDyndevProgBinary(Function* function, bool disableCtrlFlowCache, | |||
| 509 | dynamic::DevAscendProgram* devProg = reinterpret_cast<dynamic::DevAscendProgram*>(&dynAttrPtr->devProgBinary[0]); | 574 | dynamic::DevAscendProgram* devProg = reinterpret_cast<dynamic::DevAscendProgram*>(&dynAttrPtr->devProgBinary[0]); |
| 510 | dynamic::EncodeDevAscendProgram(function, size, devProg, stitchUpdateSlotMaskMap); | 575 | dynamic::EncodeDevAscendProgram(function, size, devProg, stitchUpdateSlotMaskMap); |
| 511 | devProg->disableCtrlFlowCache = disableCtrlFlowCache ? 1 : 0; | 576 | devProg->disableCtrlFlowCache = disableCtrlFlowCache ? 1 : 0; |
| 577 | + devProg->hasValueDepend = hasValueDepend ? 1 : 0; | ||
| 512 | 578 | ||
| 513 | if (config::GetPassDefaultConfig(npu::tile_fwk::KEY_PRINT_PROGRAM, false)) { | 579 | if (config::GetPassDefaultConfig(npu::tile_fwk::KEY_PRINT_PROGRAM, false)) { |
| 514 | devProg->DumpFile(config::LogTopFolder() + "/program.tifwkbintxt"); | 580 | devProg->DumpFile(config::LogTopFolder() + "/program.tifwkbintxt"); |
| @@ -818,6 +884,7 @@ static void RunBuildControlFlowStage(IrBackendContext& irBackendCtx, FunctionCac | |||
| 818 | EmitControlFlowSources(cache, linker, function, attr, expName, exprSrcFiles, valDependTensorMeta, | 884 | EmitControlFlowSources(cache, linker, function, attr, expName, exprSrcFiles, valDependTensorMeta, |
| 819 | controlFlowOss, expressionOss, slotIdxMapping); | 885 | controlFlowOss, expressionOss, slotIdxMapping); |
| 820 | } | 886 | } |
| 887 | + valDependTensorMeta.CheckValueDependCpuTensor(); | ||
| 821 | expressionOss << "#endif/*TILE_FWK_EXPRESSION_H*/" | 888 | expressionOss << "#endif/*TILE_FWK_EXPRESSION_H*/" |
| 822 | << "\n"; | 889 | << "\n"; |
| 823 | 890 | ||
| @@ -899,7 +966,8 @@ static bool RunCompileAicoreKernelStage(Function* function, std::map<uint64_t, F | |||
| 899 | #endif | 966 | #endif |
| 900 | 967 | ||
| 901 | static void RunEncodeStage(Function* function, const std::shared_ptr<DyndevFunctionAttribute>& attr, Linker& linker, | 968 | static void RunEncodeStage(Function* function, const std::shared_ptr<DyndevFunctionAttribute>& attr, Linker& linker, |
| 902 | - EncodeDevAscendFunctionParam& encodeDevAscendFunctionParam, bool disableCtrlFlowCache) | 969 | + EncodeDevAscendFunctionParam& encodeDevAscendFunctionParam, bool disableCtrlFlowCache, |
| 970 | + bool hasValueDepend) | ||
| 903 | { | 971 | { |
| 904 | const int hmStep = MonitorManager::Instance().AllocHostMachineStepIndex(); | 972 | const int hmStep = MonitorManager::Instance().AllocHostMachineStepIndex(); |
| 905 | MonitorStageScope encodeScope(STAGE_HOST_MACHINE, hmStep, STAGE_DYNDEV_ENCODE, | 973 | MonitorStageScope encodeScope(STAGE_HOST_MACHINE, hmStep, STAGE_DYNDEV_ENCODE, |
| @@ -950,7 +1018,7 @@ static void RunEncodeStage(Function* function, const std::shared_ptr<DyndevFunct | |||
| 950 | DevAscendFunction* funcBin = reinterpret_cast<DevAscendFunction*>(&attr->devEncodeList[devRootKey][0]); | 1018 | DevAscendFunction* funcBin = reinterpret_cast<DevAscendFunction*>(&attr->devEncodeList[devRootKey][0]); |
| 951 | funcBin->RefilterStitchUpdateSlotLists(stitchUpdateSlotMaskMap); | 1019 | funcBin->RefilterStitchUpdateSlotLists(stitchUpdateSlotMaskMap); |
| 952 | } | 1020 | } |
| 953 | - SetDyndevProgBinary(function, disableCtrlFlowCache, &stitchUpdateSlotMaskMap); | 1021 | + SetDyndevProgBinary(function, disableCtrlFlowCache, hasValueDepend, &stitchUpdateSlotMaskMap); |
| 954 | } | 1022 | } |
| 955 | 1023 | ||
| 956 | static void RunCodeGenStage(const std::shared_ptr<DyndevFunctionAttribute>& attr, | 1024 | static void RunCodeGenStage(const std::shared_ptr<DyndevFunctionAttribute>& attr, |
| @@ -1059,7 +1127,8 @@ static void CompileDyndevFunction(Function* function, FunctionCache& cache, [[ma | |||
| 1059 | attr->kernelBinary = ReadFile(kernelPath); | 1127 | attr->kernelBinary = ReadFile(kernelPath); |
| 1060 | MACHINE_LOGD("KernelBinary size[%zu] bytes.", attr->kernelBinary.size()); | 1128 | MACHINE_LOGD("KernelBinary size[%zu] bytes.", attr->kernelBinary.size()); |
| 1061 | 1129 | ||
| 1062 | - RunEncodeStage(function, attr, linker, encodeDevAscendFunctionParam, valDependTensorMeta.disableCtrlFlowCache); | 1130 | + RunEncodeStage(function, attr, linker, encodeDevAscendFunctionParam, valDependTensorMeta.disableCtrlFlowCache, |
| 1131 | + valDependTensorMeta.hasValueDepend); | ||
| 1063 | } | 1132 | } |
| 1064 | 1133 | ||
| 1065 | MachineTask* GenCode(MachineTask* task, FunctionCache& cache) | 1134 | MachineTask* GenCode(MachineTask* task, FunctionCache& cache) |
| @@ -137,7 +137,11 @@ void ForceLinkLibraryCompiler(); | |||
| 137 | struct ValDependTensorMeta { | 137 | struct ValDependTensorMeta { |
| 138 | std::unordered_map<std::string, bool> tensorNameToDependCore; | 138 | std::unordered_map<std::string, bool> tensorNameToDependCore; |
| 139 | std::unordered_map<RawSymbolicScalarPtr, bool> valDependMap; | 139 | std::unordered_map<RawSymbolicScalarPtr, bool> valDependMap; |
| 140 | + std::unordered_set<std::string> valueDependTensorNames; | ||
| 141 | + bool hasValueDepend{false}; | ||
| 140 | bool disableCtrlFlowCache{false}; | 142 | bool disableCtrlFlowCache{false}; |
| 143 | + | ||
| 144 | + void CheckValueDependCpuTensor(); | ||
| 141 | }; | 145 | }; |
| 142 | 146 | ||
| 143 | void InsertWaitCoreStart(SymbolicExpressionTable* exprTable, std::ostringstream& controlFlowOss, | 147 | void InsertWaitCoreStart(SymbolicExpressionTable* exprTable, std::ostringstream& controlFlowOss, |
| @@ -85,16 +85,21 @@ void InsertWaitCoreStartForLoopBounds(const std::shared_ptr<DynloopFunctionAttri | |||
| 85 | int indent) | 85 | int indent) |
| 86 | { | 86 | { |
| 87 | const SymbolicScalar* loopBounds[] = {&attr->Begin(), &attr->End(), &attr->Step()}; | 87 | const SymbolicScalar* loopBounds[] = {&attr->Begin(), &attr->End(), &attr->Step()}; |
| 88 | + bool needWaitAicoreStart = false; | ||
| 88 | for (const SymbolicScalar* boundExpr : loopBounds) { | 89 | for (const SymbolicScalar* boundExpr : loopBounds) { |
| 89 | if (!boundExpr->IsValid() || boundExpr->IsImmediate()) { | 90 | if (!boundExpr->IsValid() || boundExpr->IsImmediate()) { |
| 90 | continue; | 91 | continue; |
| 91 | } | 92 | } |
| 92 | if (SymbolicExpressionTable::CheckExprDependCore(boundExpr->Raw(), valDependTensorMeta.tensorNameToDependCore, | 93 | if (SymbolicExpressionTable::CheckExprDependCore(boundExpr->Raw(), valDependTensorMeta.tensorNameToDependCore, |
| 93 | - valDependTensorMeta.valDependMap)) { | 94 | + valDependTensorMeta.valDependMap, |
| 94 | - controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; | 95 | + valDependTensorMeta.valueDependTensorNames)) { |
| 95 | - return; | 96 | + needWaitAicoreStart = true; |
| 97 | + break; | ||
| 96 | } | 98 | } |
| 97 | } | 99 | } |
同上修改 ![]() ![]() | |||
| 100 | + if (needWaitAicoreStart) { | ||
| 101 | + controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; | ||
| 102 | + } | ||
| 98 | } | 103 | } |
| 99 | 104 | ||
| 100 | void GenerateExpression(SymbolicExpressionTable* exprTable, int devRootKey, const std::string& expName, | 105 | void GenerateExpression(SymbolicExpressionTable* exprTable, int devRootKey, const std::string& expName, |
| @@ -174,7 +179,9 @@ void MarkValueDependDisableCache(ControlFlowEmitCtx& ctx, Function* keyFunc) | |||
| 174 | } | 179 | } |
| 175 | auto valueDependDesc = currDynFuncAttr->valueDependDescDict[keyFunc]; | 180 | auto valueDependDesc = currDynFuncAttr->valueDependDescDict[keyFunc]; |
| 176 | if (valueDependDesc.getInputDataCount + valueDependDesc.getTensorDataCount != 0) { | 181 | if (valueDependDesc.getInputDataCount + valueDependDesc.getTensorDataCount != 0) { |
| 177 | - // Value-depend: disable host ctrl-flow cache only; do not emit CACHESTOP stitch. | 182 | + ctx.valDependTensorMeta.hasValueDepend = true; |
| 183 | + } | ||
| 184 | + if (valueDependDesc.getTensorDataCount != 0) { | ||
| 178 | ctx.valDependTensorMeta.disableCtrlFlowCache = true; | 185 | ctx.valDependTensorMeta.disableCtrlFlowCache = true; |
| 179 | } | 186 | } |
| 180 | } | 187 | } |
| @@ -230,7 +230,9 @@ void InsertCacheStopForContrlFlow(IrBackendContext& ctx, const ir::ForStmt* forS | |||
| 230 | } | 230 | } |
| 231 | auto valueDependDesc = currDynFuncAttr->valueDependDescDict[loopFunc]; | 231 | auto valueDependDesc = currDynFuncAttr->valueDependDescDict[loopFunc]; |
| 232 | if (valueDependDesc.getInputDataCount + valueDependDesc.getTensorDataCount != 0) { | 232 | if (valueDependDesc.getInputDataCount + valueDependDesc.getTensorDataCount != 0) { |
| 233 | - // Value-depend: disable host ctrl-flow cache only; do not emit CACHESTOP stitch. | 233 | + valDependTensorMeta.hasValueDepend = true; |
| 234 | + } | ||
| 235 | + if (valueDependDesc.getTensorDataCount != 0) { | ||
| 234 | valDependTensorMeta.disableCtrlFlowCache = true; | 236 | valDependTensorMeta.disableCtrlFlowCache = true; |
| 235 | } | 237 | } |
| 236 | } | 238 | } |
| @@ -242,16 +244,21 @@ void InsertWaitAicoreStartForControlFlow(const ir::ForStmt* forStmt, int indent, | |||
| 242 | SymbolicScalar stopScalar = ExprPtrToSymbolicScalar(forStmt->stop_); | 244 | SymbolicScalar stopScalar = ExprPtrToSymbolicScalar(forStmt->stop_); |
| 243 | SymbolicScalar stepScalar = ExprPtrToSymbolicScalar(forStmt->step_); | 245 | SymbolicScalar stepScalar = ExprPtrToSymbolicScalar(forStmt->step_); |
| 244 | const SymbolicScalar* loopBounds[] = {&startScalar, &stopScalar, &stepScalar}; | 246 | const SymbolicScalar* loopBounds[] = {&startScalar, &stopScalar, &stepScalar}; |
| 247 | + bool needWaitAicoreStart = false; | ||
| 245 | for (const SymbolicScalar* boundExpr : loopBounds) { | 248 | for (const SymbolicScalar* boundExpr : loopBounds) { |
| 246 | if (!boundExpr->IsValid() || boundExpr->IsImmediate()) { | 249 | if (!boundExpr->IsValid() || boundExpr->IsImmediate()) { |
| 247 | continue; | 250 | continue; |
| 248 | } | 251 | } |
| 249 | if (SymbolicExpressionTable::CheckExprDependCore(boundExpr->Raw(), valDependTensorMeta.tensorNameToDependCore, | 252 | if (SymbolicExpressionTable::CheckExprDependCore(boundExpr->Raw(), valDependTensorMeta.tensorNameToDependCore, |
| 250 | - valDependTensorMeta.valDependMap)) { | 253 | + valDependTensorMeta.valDependMap, |
| 251 | - controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; | 254 | + valDependTensorMeta.valueDependTensorNames)) { |
| 255 | + needWaitAicoreStart = true; | ||
| 252 | break; | 256 | break; |
| 253 | } | 257 | } |
| 254 | } | 258 | } |
| 259 | + if (needWaitAicoreStart) { | ||
| 260 | + controlFlowOss << std::setw(indent * TABSIZE) << ' ' << "WaitAicoreStart(startArgs);\n"; | ||
| 261 | + } | ||
| 255 | } | 262 | } |
| 256 | 263 | ||
| 257 | void FindExprFromIRStmt(IrBackendContext& ctx, FunctionCache& cache, Linker& linker, const ir::StmtPtr& stmt, | 264 | void FindExprFromIRStmt(IrBackendContext& ctx, FunctionCache& cache, Linker& linker, const ir::StmtPtr& stmt, |
| @@ -26,6 +26,35 @@ | |||
| 26 | #include "machine/utils/dynamic/dev_encode_program.h" | 26 | #include "machine/utils/dynamic/dev_encode_program.h" |
| 27 | #include "machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h" | 27 | #include "machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h" |
| 28 | 28 | ||
| 29 | +namespace { | ||
| 30 | + | ||
| 31 | +uint64_t ComputeValueDependHash(const std::vector<npu::tile_fwk::dynamic::DeviceTensorData>& tensors, | ||
| 32 | + const std::vector<size_t>& indices) | ||
| 33 | +{ | ||
| 34 | + uint64_t h = 14695981039346656037ull; | ||
这是啥? ![]() ![]() | |||
| 35 | + for (size_t idx : indices) { | ||
| 36 | + if (idx >= tensors.size()) { | ||
| 37 | + continue; | ||
| 38 | + } | ||
| 39 | + const auto& shape = tensors[idx].GetShape(); | ||
| 40 | + for (auto dim : shape) { | ||
| 41 | + h ^= static_cast<uint64_t>(dim); | ||
| 42 | + h *= 1099511628211ull; | ||
| 43 | + } | ||
| 44 | + auto addr = reinterpret_cast<const uint8_t*>(tensors[idx].GetAddr()); | ||
| 45 | + int64_t dataSize = tensors[idx].GetDataSize(); | ||
| 46 | + if (addr != nullptr && dataSize > 0) { | ||
| 47 | + for (int64_t i = 0; i < dataSize; i++) { | ||
| 48 | + h ^= addr[i]; | ||
| 49 | + h *= 1099511628211ull; | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + return h; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +} // namespace | ||
| 57 | + | ||
| 29 | namespace npu::tile_fwk::dynamic { | 58 | namespace npu::tile_fwk::dynamic { |
| 30 | 59 | ||
| 31 | CtrlFlowCacheManager& CtrlFlowCacheManager::Instance() | 60 | CtrlFlowCacheManager& CtrlFlowCacheManager::Instance() |
| @@ -43,14 +72,33 @@ uint8_t* CtrlFlowCacheManager::FindOrBuildDevCache(KernelBinary* kernel, std::ve | |||
| 43 | kernel->SetCtrlFlowCacheReplay(false); | 72 | kernel->SetCtrlFlowCacheReplay(false); |
| 44 | return nullptr; | 73 | return nullptr; |
| 45 | } | 74 | } |
| 46 | - auto devCache = kernel->FindCtrlFlowCache(tensors, true); | 75 | + const auto& valueDependIndices = kernel->GetValueDependInputIndices(); |
| 47 | - if (devCache == nullptr) { | 76 | + if (!kernel->HasValueDepend()) { |
| 48 | - AclModeGuard guard(AclMdlRICaptureMode::RELAXED); | 77 | + // No value-depend: original shape-based cache reuse logic (unchanged). |
| 49 | - devCache = kernel->BuildControlFlowCache(tensors, true); | 78 | + auto devCache = kernel->FindCtrlFlowCache(tensors, true); |
| 79 | + if (devCache == nullptr) { | ||
| 80 | + AclModeGuard guard(AclMdlRICaptureMode::RELAXED); | ||
| 81 | + devCache = kernel->BuildControlFlowCache(tensors, true); | ||
| 82 | + } | ||
| 83 | + // Host-built cache is already isActivated when copied to device; restore runs on first launch. | ||
| 84 | + kernel->SetCtrlFlowCacheReplay(devCache != nullptr); | ||
| 85 | + COMPILER_LOGD("find ctrlflow cache: %p", devCache); | ||
| 86 | + return devCache; | ||
| 50 | } | 87 | } |
| 51 | - // Host-built cache is already isActivated when copied to device; restore runs on first launch. | 88 | + // Value-depend with cpu tensors: single-slot cache keyed by data+shape hash. |
| 89 | + uint64_t currentHash = ComputeValueDependHash(tensors, valueDependIndices); | ||
| 90 | + uint64_t cachedHash = kernel->GetCachedCtrlFlowHash(); | ||
| 91 | + if (cachedHash == currentHash && kernel->GetValueDependDevCache() != nullptr) { | ||
| 92 | + kernel->SetCtrlFlowCacheReplay(true); | ||
| 93 | + return kernel->GetValueDependDevCache(); | ||
| 94 | + } | ||
| 95 | + // Hash mismatch or first build: free old, rebuild, update hash. | ||
| 96 | + kernel->FreeAndClearValueDependCache(); | ||
Blocker: hash 变化路径直接 FreeAndClearValueDependCache()(内部 RuntimeFree 旧 devCache)。上一次 launch 已下发的 AICPU 任务可能仍在异步使用旧 cache,立即释放会造成 device 侧 use-after-free(原 shape-based 路径从不释放 originShapeCaches,正是为避免此问题)。建议释放前先同步相关 stream,或按 hash 保留多 slot cache 池延迟回收。 ![]() ![]() | |||
| 97 | + AclModeGuard guard(AclMdlRICaptureMode::RELAXED); | ||
| 98 | + auto devCache = kernel->BuildControlFlowCache(tensors, true, false); | ||
Major: value-depend rebuild 路径未调用 SetCtrlFlowCacheReplay(true)(no-value-depend 分支 build 后有 ![]() ![]() | |||
| 99 | + kernel->SetValueDependDevCache(devCache); | ||
| 100 | + kernel->SetCachedCtrlFlowHash(currentHash); | ||
| 52 | kernel->SetCtrlFlowCacheReplay(devCache != nullptr); | 101 | kernel->SetCtrlFlowCacheReplay(devCache != nullptr); |
| 53 | - COMPILER_LOGD("find ctrlflow cache: %p", devCache); | ||
| 54 | return devCache; | 102 | return devCache; |
| 55 | } | 103 | } |
| 56 | 104 | ||
| @@ -14,6 +14,7 @@ | |||
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | #include "machine/runtime/launcher/emulation_launcher.h" | 16 | #include "machine/runtime/launcher/emulation_launcher.h" |
| 17 | +#include <map> | ||
| 17 | #include <thread> | 18 | #include <thread> |
| 18 | #include "tilefwk/error_code.h" | 19 | #include "tilefwk/error_code.h" |
| 19 | #include "tilefwk/error.h" | 20 | #include "tilefwk/error.h" |
| @@ -77,6 +78,28 @@ static int EmulationLaunchOnce(DeviceKernelArgs& kArgs) | |||
| 77 | return 0; | 78 | return 0; |
| 78 | } | 79 | } |
| 79 | 80 | ||
| 81 | +std::map<size_t, uintptr_t> BuildDevIdxToCpuAddrMap(const std::shared_ptr<DyndevFunctionAttribute>& dynAttr, | ||
| 82 | + const std::vector<DeviceTensorData>& inputList) | ||
| 83 | +{ | ||
| 84 | + std::map<size_t, uintptr_t> devIdxToCpuAddr; | ||
| 85 | + if (dynAttr == nullptr || dynAttr->readyOnHostCpuPairs.empty()) { | ||
| 86 | + return devIdxToCpuAddr; | ||
| 87 | + } | ||
| 88 | + auto inputLogicalSize = dynAttr->startArgsInputLogicalTensorList.size(); | ||
| 89 | + std::map<std::string, size_t> nameToIdx; | ||
| 90 | + for (size_t i = 0; i < inputLogicalSize && i < inputList.size(); i++) { | ||
| 91 | + nameToIdx[dynAttr->startArgsInputLogicalTensorList[i]->Symbol()] = i; | ||
| 92 | + } | ||
| 93 | + for (const auto& [deviceName, cpuName] : dynAttr->readyOnHostCpuPairs) { | ||
| 94 | + auto devIt = nameToIdx.find(deviceName); | ||
| 95 | + auto cpuIt = nameToIdx.find(cpuName); | ||
| 96 | + if (devIt != nameToIdx.end() && cpuIt != nameToIdx.end()) { | ||
| 97 | + devIdxToCpuAddr[devIt->second] = reinterpret_cast<uintptr_t>(inputList[cpuIt->second].GetAddr()); | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + return devIdxToCpuAddr; | ||
| 101 | +} | ||
| 102 | + | ||
| 80 | int EmulationLauncher::EmulationBuildControlFlowCache(DeviceKernelArgs& kArgs) | 103 | int EmulationLauncher::EmulationBuildControlFlowCache(DeviceKernelArgs& kArgs) |
| 81 | { | 104 | { |
| 82 | auto* devProg = (DevAscendProgram*)(kArgs.cfgdata); | 105 | auto* devProg = (DevAscendProgram*)(kArgs.cfgdata); |
| @@ -294,11 +317,17 @@ int EmulationLauncher::BuildControlFlowCache(Function* function, EmulationMemory | |||
| 294 | } else { | 317 | } else { |
| 295 | std::vector<DeviceTensorData> inputDeviceDataList; | 318 | std::vector<DeviceTensorData> inputDeviceDataList; |
| 296 | std::vector<DeviceTensorData> outputDeviceDataList; | 319 | std::vector<DeviceTensorData> outputDeviceDataList; |
| 320 | + | ||
| 321 | + auto devIdxToCpuAddr = BuildDevIdxToCpuAddrMap(function->GetDyndevAttribute(), inputList); | ||
| 322 | + | ||
| 297 | uintptr_t index = 0; | 323 | uintptr_t index = 0; |
| 298 | - for (auto& input : inputList) { | 324 | + for (size_t i = 0; i < inputList.size(); i++) { |
| 299 | - inputDeviceDataList.emplace_back(input.GetDataType(), | 325 | + uintptr_t addr = CONTROL_FLOW_CACHE_BASE_ADDR + index * CONTROL_FLOW_CACHE_TENSOR_SIZE; |
| 300 | - CONTROL_FLOW_CACHE_BASE_ADDR + index * CONTROL_FLOW_CACHE_TENSOR_SIZE, | 326 | + auto it = devIdxToCpuAddr.find(i); |
| 301 | - input.GetShape()); | 327 | + if (it != devIdxToCpuAddr.end()) { |
| 328 | + addr = it->second; | ||
| 329 | + } | ||
| 330 | + inputDeviceDataList.emplace_back(inputList[i].GetDataType(), addr, inputList[i].GetShape()); | ||
| 302 | index++; | 331 | index++; |
| 303 | } | 332 | } |
| 304 | for (auto& output : outputList) { | 333 | for (auto& output : outputList) { |
| @@ -16,6 +16,7 @@ | |||
| 16 | #ifndef SRC_MACHINE_EMULATION_LAUNCHER_H | 16 | #ifndef SRC_MACHINE_EMULATION_LAUNCHER_H |
| 17 | #define SRC_MACHINE_EMULATION_LAUNCHER_H | 17 | #define SRC_MACHINE_EMULATION_LAUNCHER_H |
| 18 | 18 | ||
| 19 | +#include <map> | ||
| 19 | #include <vector> | 20 | #include <vector> |
| 20 | 21 | ||
| 21 | #include "interface/configs/config_manager.h" | 22 | #include "interface/configs/config_manager.h" |
| @@ -24,6 +25,10 @@ | |||
| 24 | #include "machine/runtime/launcher/device_launcher_types.h" | 25 | #include "machine/runtime/launcher/device_launcher_types.h" |
| 25 | 26 | ||
| 26 | namespace npu::tile_fwk::dynamic { | 27 | namespace npu::tile_fwk::dynamic { |
| 28 | + | ||
| 29 | +std::map<size_t, uintptr_t> BuildDevIdxToCpuAddrMap(const std::shared_ptr<DyndevFunctionAttribute>& dynAttr, | ||
| 30 | + const std::vector<DeviceTensorData>& inputList); | ||
| 31 | + | ||
| 27 | class EmulationLauncher { | 32 | class EmulationLauncher { |
| 28 | public: | 33 | public: |
| 29 | static int EmulationLaunchOnceWithHostTensorData(Function* function, const std::vector<DeviceTensorData>& inputList, | 34 | static int EmulationLaunchOnceWithHostTensorData(Function* function, const std::vector<DeviceTensorData>& inputList, |
| @@ -102,6 +102,7 @@ KernelBinary::~KernelBinary() | |||
| 102 | std::free(reinterpret_cast<void*>(runtimeDynamicCellMatchHostAddr_)); | 102 | std::free(reinterpret_cast<void*>(runtimeDynamicCellMatchHostAddr_)); |
| 103 | } | 103 | } |
| 104 | UnregisterKernelBinary(kernelBin); | 104 | UnregisterKernelBinary(kernelBin); |
| 105 | + FreeAndClearValueDependCache(); | ||
| 105 | for (auto& cache : originShapeCaches) { | 106 | for (auto& cache : originShapeCaches) { |
| 106 | DeviceLauncher::FreeControlFlowCache(cache.devCache); | 107 | DeviceLauncher::FreeControlFlowCache(cache.devCache); |
| 107 | } | 108 | } |
| @@ -151,7 +152,8 @@ uint8_t* KernelBinary::FindCtrlFlowCache(std::vector<DeviceTensorData>& inputs, | |||
| 151 | return nullptr; | 152 | return nullptr; |
| 152 | } | 153 | } |
| 153 | 154 | ||
| 154 | -uint8_t* KernelBinary::BuildControlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape) | 155 | +uint8_t* KernelBinary::BuildControlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape, |
| 156 | + bool storeToCache) | ||
| 155 | { | 157 | { |
| 156 | DeviceLauncherConfig config; | 158 | DeviceLauncherConfig config; |
| 157 | DeviceLauncher::DeviceLauncherConfigFillDeviceInfo(config); | 159 | DeviceLauncher::DeviceLauncherConfigFillDeviceInfo(config); |
| @@ -175,10 +177,12 @@ uint8_t* KernelBinary::BuildControlFlowCache(std::vector<DeviceTensorData>& inpu | |||
| 175 | 177 | ||
| 176 | uint8_t* devCache = DeviceLauncher::CopyControlFlowCache(ctrlCache); | 178 | uint8_t* devCache = DeviceLauncher::CopyControlFlowCache(ctrlCache); |
| 177 | COMPILER_LOGD("control flow cache: %p", devCache); | 179 | COMPILER_LOGD("control flow cache: %p", devCache); |
| 178 | - if (isOriginShape) { | 180 | + if (storeToCache) { |
| 179 | - originShapeCaches.emplace_back(inputs, devCache); | 181 | + if (isOriginShape) { |
| 180 | - } else { | 182 | + originShapeCaches.emplace_back(inputs, devCache); |
| 181 | - inferShapeCaches.emplace_back(inputs, devCache); | 183 | + } else { |
| 184 | + inferShapeCaches.emplace_back(inputs, devCache); | ||
| 185 | + } | ||
| 182 | } | 186 | } |
| 183 | return devCache; | 187 | return devCache; |
| 184 | } | 188 | } |
| @@ -290,9 +294,27 @@ Function* KernelBinary::GetFunction() { return dynFunc.get(); } | |||
| 290 | 294 | ||
| 291 | const std::string& KernelBinary::GetKernelname() const { return kernelName_; } | 295 | const std::string& KernelBinary::GetKernelname() const { return kernelName_; } |
| 292 | 296 | ||
| 297 | +std::vector<size_t> KernelBinary::emptyIndices_; | ||
| 298 | + | ||
| 293 | bool KernelBinary::DisableHostCtrlFlowCacheBuild() const | 299 | bool KernelBinary::DisableHostCtrlFlowCacheBuild() const |
| 294 | { | 300 | { |
| 295 | - return devProg != nullptr && devProg->disableCtrlFlowCache != 0; | 301 | + if (devProg == nullptr) { |
| 302 | + return true; | ||
| 303 | + } | ||
| 304 | + return devProg->disableCtrlFlowCache != 0; | ||
| 305 | +} | ||
| 306 | + | ||
| 307 | +const std::vector<size_t>& KernelBinary::GetValueDependInputIndices() const | ||
| 308 | +{ | ||
| 309 | + return devProg != nullptr ? devProg->valueDependInputIndices : emptyIndices_; | ||
| 310 | +} | ||
| 311 | + | ||
| 312 | +void KernelBinary::FreeAndClearValueDependCache() | ||
| 313 | +{ | ||
| 314 | + if (valueDependDevCache_ != nullptr) { | ||
| 315 | + DeviceLauncher::FreeControlFlowCache(valueDependDevCache_); | ||
| 316 | + valueDependDevCache_ = nullptr; | ||
| 317 | + } | ||
| 296 | } | 318 | } |
| 297 | 319 | ||
| 298 | uint64_t KernelBinary::GetMaxDynamicAssembleOutcastMem() const | 320 | uint64_t KernelBinary::GetMaxDynamicAssembleOutcastMem() const |
| @@ -48,7 +48,7 @@ public: | |||
| 48 | 48 | ||
| 49 | uint8_t* FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape); | 49 | uint8_t* FindCtrlFlowCache(std::vector<std::vector<int64_t>>& inputs, bool isOriginShape); |
| 50 | uint8_t* FindCtrlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape); | 50 | uint8_t* FindCtrlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape); |
| 51 | - uint8_t* BuildControlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape); | 51 | + uint8_t* BuildControlFlowCache(std::vector<DeviceTensorData>& inputs, bool isOriginShape, bool storeToCache = true); |
| 52 | 52 | ||
| 53 | int64_t GetWorkspaceSize(const std::vector<DeviceTensorData>& tensors); | 53 | int64_t GetWorkspaceSize(const std::vector<DeviceTensorData>& tensors); |
| 54 | 54 | ||
| @@ -61,6 +61,13 @@ public: | |||
| 61 | Function* GetFunction(); | 61 | Function* GetFunction(); |
| 62 | const std::string& GetKernelname() const; | 62 | const std::string& GetKernelname() const; |
| 63 | bool DisableHostCtrlFlowCacheBuild() const; | 63 | bool DisableHostCtrlFlowCacheBuild() const; |
| 64 | + bool HasValueDepend() const { return devProg != nullptr && devProg->hasValueDepend != 0; } | ||
| 65 | + const std::vector<size_t>& GetValueDependInputIndices() const; | ||
| 66 | + uint64_t GetCachedCtrlFlowHash() const { return cachedCtrlFlowHash_; } | ||
| 67 | + void SetCachedCtrlFlowHash(uint64_t hash) { cachedCtrlFlowHash_ = hash; } | ||
| 68 | + uint8_t* GetValueDependDevCache() const { return valueDependDevCache_; } | ||
| 69 | + void SetValueDependDevCache(uint8_t* cache) { valueDependDevCache_ = cache; } | ||
| 70 | + void FreeAndClearValueDependCache(); | ||
| 64 | uint64_t GetMaxDynamicAssembleOutcastMem() const; | 71 | uint64_t GetMaxDynamicAssembleOutcastMem() const; |
| 65 | uint64_t GetMaxDynamicCellMatchTableMem() const; | 72 | uint64_t GetMaxDynamicCellMatchTableMem() const; |
| 66 | uint64_t GetRuntimeDynamicCellMatchAddr() const; | 73 | uint64_t GetRuntimeDynamicCellMatchAddr() const; |
| @@ -132,6 +139,9 @@ private: | |||
| 132 | RtTaskCfgInfo rtTaskCfg_; | 139 | RtTaskCfgInfo rtTaskCfg_; |
| 133 | std::vector<void*> kernelArgs_; | 140 | std::vector<void*> kernelArgs_; |
| 134 | AicpuHostInput hostInfo_; | 141 | AicpuHostInput hostInfo_; |
| 142 | + uint64_t cachedCtrlFlowHash_{0}; | ||
| 143 | + uint8_t* valueDependDevCache_{nullptr}; | ||
| 144 | + static std::vector<size_t> emptyIndices_; | ||
| 135 | }; | 145 | }; |
| 136 | 146 | ||
| 137 | } // namespace npu::tile_fwk::dynamic | 147 | } // namespace npu::tile_fwk::dynamic |
| @@ -3116,6 +3116,7 @@ struct EncodeDevAscendProgramInfo { | |||
| 3116 | fillContent, stitchUpdateSlotMaskMap); | 3116 | fillContent, stitchUpdateSlotMaskMap); |
| 3117 | devProg->InitPrefetchInfoList(initOffset, dyndevAttr->l2InfoList, fillContent); | 3117 | devProg->InitPrefetchInfoList(initOffset, dyndevAttr->l2InfoList, fillContent); |
| 3118 | devProg->InitDisableL2List(initOffset, dyndevAttr->disableL2List, fillContent); | 3118 | devProg->InitDisableL2List(initOffset, dyndevAttr->disableL2List, fillContent); |
| 3119 | + devProg->valueDependInputIndices = dyndevAttr->valueDependInputIndices; | ||
| 3119 | devProg->dataSize = initOffset - reinterpret_cast<uintdevptr_t>(devProg->data); | 3120 | devProg->dataSize = initOffset - reinterpret_cast<uintdevptr_t>(devProg->data); |
| 3120 | } | 3121 | } |
| 3121 | }; | 3122 | }; |
| @@ -115,6 +115,8 @@ struct DevAscendProgram { | |||
| 115 | uint32_t stitchMaxFunctionNum{0}; | 115 | uint32_t stitchMaxFunctionNum{0}; |
| 116 | uint32_t ctrlFlowCacheSize{0}; | 116 | uint32_t ctrlFlowCacheSize{0}; |
| 117 | uint32_t disableCtrlFlowCache{0}; | 117 | uint32_t disableCtrlFlowCache{0}; |
| 118 | + uint8_t hasValueDepend{0}; | ||
| 119 | + std::vector<size_t> valueDependInputIndices; | ||
Major: DevAscendProgram 是序列化字节流结构(经 reinterpret_cast 到 devProgBinary 使用,无 placement-new,靠 resize 清零内存;RelocProgram 只处理 DevRelocVector,不处理堆指针;字节流还会落盘 program.tifwkbin 并打包进 kernel bundle)。新增 ![]() ![]() | |||
| 118 | uint32_t rootFuncMaxCallOpsize{0}; | 120 | uint32_t rootFuncMaxCallOpsize{0}; |
| 119 | uint32_t cellMatchTagSeq_{0}; | 121 | uint32_t cellMatchTagSeq_{0}; |
| 120 | // Seed to UINT32_MAX so the first AdvanceStitchCacheEpoch wraps packed epoch to 0 and memset's virgin cache. | 122 | // Seed to UINT32_MAX so the first AdvanceStitchCacheEpoch wraps packed epoch to 0 and memset's virgin cache. |
| @@ -65,7 +65,11 @@ TEST(KernelBinaryTest, DefaultStateGetters) | |||
| 65 | EXPECT_EQ(helper.kb->GetRuntimeDynamicCellMatchAddr(), 0u); | 65 | EXPECT_EQ(helper.kb->GetRuntimeDynamicCellMatchAddr(), 0u); |
| 66 | EXPECT_EQ(helper.kb->GetRuntimeDynamicCellMatchCapacity(), 0u); | 66 | EXPECT_EQ(helper.kb->GetRuntimeDynamicCellMatchCapacity(), 0u); |
| 67 | EXPECT_EQ(helper.kb->GetSyncMode(), 0u); | 67 | EXPECT_EQ(helper.kb->GetSyncMode(), 0u); |
| 68 | - EXPECT_FALSE(helper.kb->DisableHostCtrlFlowCacheBuild()); | 68 | + EXPECT_TRUE(helper.kb->DisableHostCtrlFlowCacheBuild()); |
| 69 | + EXPECT_FALSE(helper.kb->HasValueDepend()); | ||
| 70 | + EXPECT_TRUE(helper.kb->GetValueDependInputIndices().empty()); | ||
| 71 | + EXPECT_EQ(helper.kb->GetCachedCtrlFlowHash(), 0u); | ||
| 72 | + EXPECT_EQ(helper.kb->GetValueDependDevCache(), nullptr); | ||
| 69 | } | 73 | } |
| 70 | 74 | ||
| 71 | TEST(KernelBinaryTest, RefReturningGetters) | 75 | TEST(KernelBinaryTest, RefReturningGetters) |
| @@ -101,7 +105,7 @@ TEST(KernelBinaryTest, DisableHostCtrlFlowCacheBuild_WithDevProg) | |||
| 101 | helper.kb->devProg = &prog; | 105 | helper.kb->devProg = &prog; |
| 102 | EXPECT_TRUE(helper.kb->DisableHostCtrlFlowCacheBuild()); | 106 | EXPECT_TRUE(helper.kb->DisableHostCtrlFlowCacheBuild()); |
| 103 | helper.kb->devProg = nullptr; | 107 | helper.kb->devProg = nullptr; |
| 104 | - EXPECT_FALSE(helper.kb->DisableHostCtrlFlowCacheBuild()); | 108 | + EXPECT_TRUE(helper.kb->DisableHostCtrlFlowCacheBuild()); |
| 105 | } | 109 | } |
| 106 | 110 | ||
| 107 | TEST(KernelBinaryTest, ResetRuntimeDynamicCellMatchPool_AllCases) | 111 | TEST(KernelBinaryTest, ResetRuntimeDynamicCellMatchPool_AllCases) |
| @@ -120,13 +120,15 @@ TEST_F(TestExprBatchGenerator, CheckExprDependCoreTest) | |||
| 120 | RawSymbolicScalarPtr getInputDataExpr = std::make_shared<RawSymbolicExpression>(SymbolicOpcode::T_MOP_CALL, | 120 | RawSymbolicScalarPtr getInputDataExpr = std::make_shared<RawSymbolicExpression>(SymbolicOpcode::T_MOP_CALL, |
| 121 | operands); | 121 | operands); |
| 122 | bool dependsCore = SymbolicExpressionTable::CheckExprDependCore( | 122 | bool dependsCore = SymbolicExpressionTable::CheckExprDependCore( |
| 123 | - getInputDataExpr, valDependTensorMeta.tensorNameToDependCore, valDependTensorMeta.valDependMap); | 123 | + getInputDataExpr, valDependTensorMeta.tensorNameToDependCore, valDependTensorMeta.valDependMap, |
| 124 | + valDependTensorMeta.valueDependTensorNames); | ||
| 124 | ASSERT_TRUE(dependsCore); | 125 | ASSERT_TRUE(dependsCore); |
| 125 | 126 | ||
| 126 | valDependTensorMeta.tensorNameToDependCore[testTensorName] = false; | 127 | valDependTensorMeta.tensorNameToDependCore[testTensorName] = false; |
| 127 | valDependTensorMeta.valDependMap.clear(); | 128 | valDependTensorMeta.valDependMap.clear(); |
| 128 | dependsCore = SymbolicExpressionTable::CheckExprDependCore( | 129 | dependsCore = SymbolicExpressionTable::CheckExprDependCore( |
| 129 | - getInputDataExpr, valDependTensorMeta.tensorNameToDependCore, valDependTensorMeta.valDependMap); | 130 | + getInputDataExpr, valDependTensorMeta.tensorNameToDependCore, valDependTensorMeta.valDependMap, |
| 131 | + valDependTensorMeta.valueDependTensorNames); | ||
| 130 | ASSERT_FALSE(dependsCore); | 132 | ASSERT_FALSE(dependsCore); |
| 131 | } | 133 | } |
| 132 | 134 | ||
| @@ -279,7 +279,8 @@ TEST_F(TestSuite_IrBackend, InsertCacheStopForContrlFlow_AllCases) | |||
| 279 | 279 | ||
| 280 | ValDependTensorMeta meta; | 280 | ValDependTensorMeta meta; |
| 281 | InsertCacheStopForContrlFlow(irBackendCtx, AsForStmt(forStmt).get(), fixture.dynFunc.get(), meta); | 281 | InsertCacheStopForContrlFlow(irBackendCtx, AsForStmt(forStmt).get(), fixture.dynFunc.get(), meta); |
| 282 | - EXPECT_TRUE(meta.disableCtrlFlowCache); | 282 | + EXPECT_TRUE(meta.hasValueDepend); |
| 283 | + EXPECT_FALSE(meta.disableCtrlFlowCache); | ||
| 283 | } | 284 | } |
| 284 | } | 285 | } |
| 285 | 286 | ||
| @@ -0,0 +1,165 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file test_ready_on_host_tensors_machine.cpp | ||
| 13 | + * \brief Test GetReadyOnHostTensorsSet with list[list[string]] form. | ||
| 14 | + */ | ||
| 15 | +#include "gtest/gtest.h" | ||
| 16 | + | ||
| 17 | +#include <memory> | ||
| 18 | +#include <string> | ||
| 19 | +#include <unordered_set> | ||
| 20 | +#include <utility> | ||
| 21 | +#include <vector> | ||
| 22 | + | ||
| 23 | +#include "interface/configs/config_manager.h" | ||
| 24 | +#include "interface/function/function.h" | ||
| 25 | +#include "interface/program/program.h" | ||
| 26 | +#include "interface/tensor/logical_tensor.h" | ||
| 27 | +#include "machine/host/backend.h" | ||
| 28 | +#include "tilefwk/tilefwk.h" | ||
| 29 | + | ||
| 30 | +using namespace npu::tile_fwk; | ||
| 31 | + | ||
| 32 | +class TestReadyOnHostTensorsMachine : public testing::Test { | ||
| 33 | +public: | ||
| 34 | + void SetUp() override { config::Reset(); } | ||
| 35 | + void TearDown() override | ||
| 36 | + { | ||
| 37 | + Program::GetInstance().SetCurrentDynamicFunction(nullptr); | ||
| 38 | + Program::GetInstance().Reset(); | ||
| 39 | + config::Reset(); | ||
| 40 | + } | ||
| 41 | +}; | ||
| 42 | + | ||
| 43 | +TEST_F(TestReadyOnHostTensorsMachine, GetReadyOnHostTensorsSetListListForm) | ||
| 44 | +{ | ||
| 45 | + // 1. Setup jit scope and config with list[list[string]] form. | ||
| 46 | + ConfigManagerNg::JitScopeGuard guard("jit_scope", std::map<std::string, std::any>{}); | ||
| 47 | + auto scope = ConfigManagerNg::CurrentScope(); | ||
| 48 | + scope->UpdateValueWithAny(std::string("runtime.") + READY_ON_HOST_TENSORS, | ||
| 49 | + std::vector<std::pair<std::string, std::string>>{{"a", "a_cpu"}, {"b", "b_cpu"}}); | ||
| 50 | + | ||
| 51 | + // 2. Create Function + DyndevFunctionAttribute and set as current dynamic function. | ||
| 52 | + Program::GetInstance().Reset(); | ||
| 53 | + auto dynFunc = std::make_shared<Function>(Program::GetInstance(), "test_magic", "test", nullptr); | ||
| 54 | + dynFunc->SetFunctionType(FunctionType::DYNAMIC); | ||
| 55 | + dynFunc->SetGraphType(GraphType::TENSOR_GRAPH); | ||
| 56 | + auto dynAttr = std::make_shared<DyndevFunctionAttribute>(); | ||
| 57 | + dynFunc->SetDyndevAttribute(dynAttr); | ||
| 58 | + Program::GetInstance().InsertFuncToFunctionMap(dynFunc->GetMagicName(), dynFunc); | ||
| 59 | + Program::GetInstance().SetCurrentDynamicFunction(dynFunc.get()); | ||
| 60 | + | ||
| 61 | + // 3. Create LogicalTensors and add to startArgsInputLogicalTensorList. | ||
| 62 | + // Indices: 0="a", 1="a_cpu", 2="b", 3="b_cpu" | ||
| 63 | + Shape shape = {32, 32}; | ||
| 64 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 65 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "a")); | ||
| 66 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 67 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "a_cpu")); | ||
| 68 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 69 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "b")); | ||
| 70 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 71 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "b_cpu")); | ||
| 72 | + | ||
| 73 | + // 4. Call GetReadyOnHostTensorsSet. | ||
| 74 | + std::unordered_set<int> readyOnHostTensorsSet; | ||
| 75 | + GetReadyOnHostTensorsSet(readyOnHostTensorsSet); | ||
| 76 | + | ||
| 77 | + // 5. Verify results. | ||
| 78 | + EXPECT_EQ(readyOnHostTensorsSet.size(), 2); | ||
| 79 | + EXPECT_TRUE(readyOnHostTensorsSet.count(0) > 0); | ||
| 80 | + EXPECT_TRUE(readyOnHostTensorsSet.count(2) > 0); | ||
| 81 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs.size(), 2); | ||
| 82 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs[0].first, "a"); | ||
| 83 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs[0].second, "a_cpu"); | ||
| 84 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs[1].first, "b"); | ||
| 85 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs[1].second, "b_cpu"); | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +TEST_F(TestReadyOnHostTensorsMachine, GetReadyOnHostTensorsSetLegacyListForm) | ||
| 89 | +{ | ||
| 90 | + // 1. Setup jit scope and config with legacy list[string] form. | ||
| 91 | + ConfigManagerNg::JitScopeGuard guard("jit_scope", std::map<std::string, std::any>{}); | ||
| 92 | + auto scope = ConfigManagerNg::CurrentScope(); | ||
| 93 | + scope->UpdateValueWithAny(std::string("runtime.") + READY_ON_HOST_TENSORS, std::vector<std::string>{"a", "b"}); | ||
| 94 | + | ||
| 95 | + // 2. Create Function + DyndevFunctionAttribute and set as current dynamic function. | ||
| 96 | + Program::GetInstance().Reset(); | ||
| 97 | + auto dynFunc = std::make_shared<Function>(Program::GetInstance(), "test_magic", "test", nullptr); | ||
| 98 | + dynFunc->SetFunctionType(FunctionType::DYNAMIC); | ||
| 99 | + dynFunc->SetGraphType(GraphType::TENSOR_GRAPH); | ||
| 100 | + auto dynAttr = std::make_shared<DyndevFunctionAttribute>(); | ||
| 101 | + dynFunc->SetDyndevAttribute(dynAttr); | ||
| 102 | + Program::GetInstance().InsertFuncToFunctionMap(dynFunc->GetMagicName(), dynFunc); | ||
| 103 | + Program::GetInstance().SetCurrentDynamicFunction(dynFunc.get()); | ||
| 104 | + | ||
| 105 | + // 3. Create LogicalTensors and add to startArgsInputLogicalTensorList. | ||
| 106 | + // Indices: 0="a", 1="b" | ||
| 107 | + Shape shape = {32, 32}; | ||
| 108 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 109 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "a")); | ||
| 110 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 111 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "b")); | ||
| 112 | + | ||
| 113 | + // 4. Call GetReadyOnHostTensorsSet. | ||
| 114 | + std::unordered_set<int> readyOnHostTensorsSet; | ||
| 115 | + GetReadyOnHostTensorsSet(readyOnHostTensorsSet); | ||
| 116 | + | ||
| 117 | + // 5. Verify results: set populated, pairs empty (legacy form has no cpu versions). | ||
| 118 | + EXPECT_EQ(readyOnHostTensorsSet.size(), 2); | ||
| 119 | + EXPECT_TRUE(readyOnHostTensorsSet.count(0) > 0); | ||
| 120 | + EXPECT_TRUE(readyOnHostTensorsSet.count(1) > 0); | ||
| 121 | + EXPECT_TRUE(dynAttr->readyOnHostCpuPairs.empty()); | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +TEST_F(TestReadyOnHostTensorsMachine, GetReadyOnHostTensorsSetListListWithEmptyCpuForm) | ||
| 125 | +{ | ||
| 126 | + // 1. Setup jit scope and config with list[list[string]] form, where "a" has an empty cpu name. | ||
| 127 | + ConfigManagerNg::JitScopeGuard guard("jit_scope", std::map<std::string, std::any>{}); | ||
| 128 | + auto scope = ConfigManagerNg::CurrentScope(); | ||
| 129 | + scope->UpdateValueWithAny(std::string("runtime.") + READY_ON_HOST_TENSORS, | ||
| 130 | + std::vector<std::pair<std::string, std::string>>{{"a", ""}, {"b", "b_cpu"}}); | ||
| 131 | + | ||
| 132 | + // 2. Create Function + DyndevFunctionAttribute and set as current dynamic function. | ||
| 133 | + Program::GetInstance().Reset(); | ||
| 134 | + auto dynFunc = std::make_shared<Function>(Program::GetInstance(), "test_magic", "test", nullptr); | ||
| 135 | + dynFunc->SetFunctionType(FunctionType::DYNAMIC); | ||
| 136 | + dynFunc->SetGraphType(GraphType::TENSOR_GRAPH); | ||
| 137 | + auto dynAttr = std::make_shared<DyndevFunctionAttribute>(); | ||
| 138 | + dynFunc->SetDyndevAttribute(dynAttr); | ||
| 139 | + Program::GetInstance().InsertFuncToFunctionMap(dynFunc->GetMagicName(), dynFunc); | ||
| 140 | + Program::GetInstance().SetCurrentDynamicFunction(dynFunc.get()); | ||
| 141 | + | ||
| 142 | + // 3. Create LogicalTensors and add to startArgsInputLogicalTensorList. | ||
| 143 | + // Indices: 0="a", 1="b", 2="b_cpu" | ||
| 144 | + Shape shape = {32, 32}; | ||
| 145 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 146 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "a")); | ||
| 147 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 148 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "b")); | ||
| 149 | + dynAttr->startArgsInputLogicalTensorList.push_back( | ||
| 150 | + std::make_shared<LogicalTensor>(*dynFunc, DataType::DT_FP32, shape, TileOpFormat::TILEOP_ND, "b_cpu")); | ||
| 151 | + | ||
| 152 | + // 4. Call GetReadyOnHostTensorsSet. | ||
| 153 | + std::unordered_set<int> readyOnHostTensorsSet; | ||
| 154 | + GetReadyOnHostTensorsSet(readyOnHostTensorsSet); | ||
| 155 | + | ||
| 156 | + // 5. Verify results: both in set, but only "b" has cpu pair. | ||
| 157 | + EXPECT_EQ(readyOnHostTensorsSet.size(), 2); | ||
| 158 | + EXPECT_TRUE(readyOnHostTensorsSet.count(0) > 0); | ||
| 159 | + EXPECT_TRUE(readyOnHostTensorsSet.count(1) > 0); | ||
| 160 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs.size(), 1); | ||
| 161 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs[0].first, "b"); | ||
| 162 | + EXPECT_EQ(dynAttr->readyOnHostCpuPairs[0].second, "b_cpu"); | ||
| 163 | + EXPECT_EQ(dynAttr->valueDependInputIndices.size(), 1); | ||
| 164 | + EXPECT_EQ(dynAttr->valueDependInputIndices[0], 2); | ||
| 165 | +} | ||
| @@ -224,6 +224,34 @@ void BindControllerUtils(py::module_& m) | |||
| 224 | m.def("ClearSpan", &ir::Span::ClearCurrent); | 224 | m.def("ClearSpan", &ir::Span::ClearCurrent); |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | +static std::string GetElemTypeName(const py::handle& elem) | ||
| 228 | +{ | ||
| 229 | + return py::str(py::type::handle_of(elem).attr("__name__")).cast<std::string>(); | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +static std::any ConvertListList(const std::string& key, const py::list& lst) | ||
没看到导出 python 接口的代码? ![]() ![]() | |||
| 233 | +{ | ||
| 234 | + std::vector<std::pair<std::string, std::string>> pairs; | ||
| 235 | + for (size_t i = 0; i < lst.size(); ++i) { | ||
| 236 | + if (!py::isinstance<py::list>(lst[i])) { | ||
| 237 | + throw py::type_error("Option '" + key + "' has invalid list element type at index " + std::to_string(i) + | ||
| 238 | + ". Expected list, but got " + GetElemTypeName(lst[i])); | ||
| 239 | + } | ||
| 240 | + py::list innerList = py::cast<py::list>(lst[i]); | ||
| 241 | + if (innerList.size() != 2) { | ||
| 242 | + throw py::type_error("Option '" + key + "' has invalid inner list length at index " + std::to_string(i) + | ||
| 243 | + ". Expected 2 elements [device_name, cpu_name], but got " + | ||
| 244 | + std::to_string(innerList.size())); | ||
| 245 | + } | ||
| 246 | + if (!py::isinstance<py::str>(innerList[0]) || !py::isinstance<py::str>(innerList[1])) { | ||
| 247 | + throw py::type_error("Option '" + key + "' has invalid inner list element type at index " + | ||
| 248 | + std::to_string(i) + ". Expected [str, str]"); | ||
| 249 | + } | ||
| 250 | + pairs.push_back({py::cast<std::string>(innerList[0]), py::cast<std::string>(innerList[1])}); | ||
| 251 | + } | ||
| 252 | + return pairs; | ||
| 253 | +} | ||
| 254 | + | ||
| 227 | std::any ConvertPyList(const std::string& key, const py::list& lst) | 255 | std::any ConvertPyList(const std::string& key, const py::list& lst) |
| 228 | { | 256 | { |
| 229 | if (lst.size() == 0) { | 257 | if (lst.size() == 0) { |
| @@ -269,6 +297,8 @@ std::any ConvertPyList(const std::string& key, const py::list& lst) | |||
| 269 | doubleVec.push_back(py::cast<double>(lst[i])); | 297 | doubleVec.push_back(py::cast<double>(lst[i])); |
| 270 | } | 298 | } |
| 271 | return doubleVec; | 299 | return doubleVec; |
| 300 | + } else if (py::isinstance<py::list>(lst[0])) { | ||
| 301 | + return ConvertListList(key, lst); | ||
| 272 | } | 302 | } |
| 273 | throw py::type_error("Unsupported list element type for key: " + key); | 303 | throw py::type_error("Unsupported list element type for key: " + key); |
| 274 | } | 304 | } |
| @@ -508,6 +538,17 @@ py::object AnyToPyObject(const std::any& val) | |||
| 508 | {typeid(std::vector<int64_t>), [](const std::any& a) { return py::cast(AnyCast<std::vector<int64_t>>(a)); }}, | 538 | {typeid(std::vector<int64_t>), [](const std::any& a) { return py::cast(AnyCast<std::vector<int64_t>>(a)); }}, |
| 509 | {typeid(std::vector<std::string>), | 539 | {typeid(std::vector<std::string>), |
| 510 | [](const std::any& a) { return py::cast(AnyCast<std::vector<std::string>>(a)); }}, | 540 | [](const std::any& a) { return py::cast(AnyCast<std::vector<std::string>>(a)); }}, |
| 541 | + {typeid(std::vector<std::pair<std::string, std::string>>), | ||
| 542 | + [](const std::any& a) { | ||
| 543 | + py::list outer; | ||
| 544 | + for (const auto& p : AnyCast<std::vector<std::pair<std::string, std::string>>>(a)) { | ||
| 545 | + py::list inner; | ||
| 546 | + inner.append(p.first); | ||
| 547 | + inner.append(p.second); | ||
| 548 | + outer.append(inner); | ||
| 549 | + } | ||
| 550 | + return outer; | ||
| 551 | + }}, | ||
| 511 | {typeid(std::vector<double>), [](const std::any& a) { return py::cast(AnyCast<std::vector<double>>(a)); }}, | 552 | {typeid(std::vector<double>), [](const std::any& a) { return py::cast(AnyCast<std::vector<double>>(a)); }}, |
| 512 | {typeid(std::map<int64_t, int64_t>), | 553 | {typeid(std::map<int64_t, int64_t>), |
| 513 | [](const std::any& a) { return py::cast(AnyCast<std::map<int64_t, int64_t>>(a)); }}, | 554 | [](const std::any& a) { return py::cast(AnyCast<std::map<int64_t, int64_t>>(a)); }}, |
| @@ -168,15 +168,15 @@ TensorDeviceInfo ConvertSingleTensor(const py::object& torchTensor, const py::ob | |||
| 168 | return deviceInfo; | 168 | return deviceInfo; |
| 169 | } | 169 | } |
| 170 | 170 | ||
| 171 | -int ValidateDeviceAndReturnIndex(const TensorDeviceInfo& deviceInfo) | 171 | +int ValidateDeviceAndReturnIndex(const std::optional<TensorDeviceInfo>& deviceInfo) |
| 172 | { | 172 | { |
| 173 | if (config::GetRuntimeOption<int64_t>(CFG_RUN_MODE) == CFG_RUN_MODE_SIM) { | 173 | if (config::GetRuntimeOption<int64_t>(CFG_RUN_MODE) == CFG_RUN_MODE_SIM) { |
| 174 | return 0; | 174 | return 0; |
| 175 | } | 175 | } |
| 176 | - if (deviceInfo.type != "npu") { | 176 | + if (!deviceInfo.has_value() || deviceInfo->type != "npu") { |
| 177 | throw std::runtime_error("Not npu device"); | 177 | throw std::runtime_error("Not npu device"); |
| 178 | } | 178 | } |
| 179 | - return deviceInfo.index; | 179 | + return deviceInfo->index; |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | } // namespace | 182 | } // namespace |
| @@ -203,14 +203,18 @@ int TorchTensorConverter::Convert(py::sequence& tensors, py::sequence& tensor_de | |||
| 203 | 203 | ||
| 204 | TensorDeviceInfo tensorDeviceInfo = ConvertSingleTensor(torchTensor, tensorDef, torch_npu, tensors_data.back()); | 204 | TensorDeviceInfo tensorDeviceInfo = ConvertSingleTensor(torchTensor, tensorDef, torch_npu, tensors_data.back()); |
| 205 | 205 | ||
| 206 | + // Skip device consistency check for cpu tensors (used as host-side data for value-depend). | ||
| 207 | + if (tensorDeviceInfo.type == "cpu") { | ||
| 208 | + continue; | ||
| 209 | + } | ||
| 210 | + | ||
| 206 | if (!commonDeviceInfo.has_value()) { | 211 | if (!commonDeviceInfo.has_value()) { |
| 207 | commonDeviceInfo.emplace(std::move(tensorDeviceInfo)); | 212 | commonDeviceInfo.emplace(std::move(tensorDeviceInfo)); |
| 208 | } else if (!IsSameDevice(*commonDeviceInfo, tensorDeviceInfo)) { | 213 | } else if (!IsSameDevice(*commonDeviceInfo, tensorDeviceInfo)) { |
| 209 | throw std::runtime_error("All input tensors must be on the same device"); | 214 | throw std::runtime_error("All input tensors must be on the same device"); |
| 210 | } | 215 | } |
| 211 | } | 216 | } |
| 212 | - | 217 | + return ValidateDeviceAndReturnIndex(commonDeviceInfo); |
| 213 | - return ValidateDeviceAndReturnIndex(*commonDeviceInfo); | ||
| 214 | } | 218 | } |
| 215 | 219 | ||
| 216 | size_t ValidateInputs(py::sequence& tensors, py::sequence& tensorDefs) | 220 | size_t ValidateInputs(py::sequence& tensors, py::sequence& tensorDefs) |
| @@ -128,6 +128,21 @@ def test_wrapper_option_type_mismatch_error(setter, pattern): | |||
| 128 | "Option 'runtime.ready_on_host_tensors' has invalid list element type at index 1. " | 128 | "Option 'runtime.ready_on_host_tensors' has invalid list element type at index 1. " |
| 129 | "Expected str, but got int", | 129 | "Expected str, but got int", |
| 130 | ), | 130 | ), |
| 131 | + ( | ||
| 132 | + {"runtime_options": {"ready_on_host_tensors": [["a", "b", "c"]]}}, | ||
| 133 | + "Option 'runtime.ready_on_host_tensors' has invalid inner list length at index 0. " | ||
| 134 | + "Expected 2 elements \\[device_name, cpu_name\\], but got 3", | ||
| 135 | + ), | ||
| 136 | + ( | ||
| 137 | + {"runtime_options": {"ready_on_host_tensors": [["a", 1]]}}, | ||
| 138 | + "Option 'runtime.ready_on_host_tensors' has invalid inner list element type at index 0. " | ||
| 139 | + "Expected \\[str, str\\]", | ||
| 140 | + ), | ||
| 141 | + ( | ||
| 142 | + {"runtime_options": {"ready_on_host_tensors": ["a", ["b", "c", "d"]]}}, | ||
| 143 | + "Option 'runtime.ready_on_host_tensors' has invalid list element type at index 1. " | ||
| 144 | + "Expected str, but got list", | ||
| 145 | + ), | ||
| 131 | ], | 146 | ], |
| 132 | ) | 147 | ) |
| 133 | def test_set_options_type_mismatch_error(options_kwargs, pattern): | 148 | def test_set_options_type_mismatch_error(options_kwargs, pattern): |


之前方案好像没有这个