Merged
fix(codegen): Fix operandWithMagic out-of-bounds initialization #5678
anyangleAdd on Aug 25
fix(codegen): Fix operandWithMagic out-of-bounds initialization #5678
Merged
anyangleAdd on Aug 25
Total: 9 files changed+28-22
@@ -58,18 +58,25 @@ encode阶段`InitRawTensorAndMemoryRequirement`断言:`Shape size mismatch`或
58 58 
59Host侧映射寄存器地址失败:`Map reg addr fail, maybe others are using current device`。59Host侧映射寄存器地址失败:`Map reg addr fail, maybe others are using current device`。
60 60 
61+> **定性**:多数为设备寄存器映射的**瞬态争用**,不是硬件故障。同设备短间隔重试常可恢复;勿在未诊断前扫卡换设备。
62+ 
61**可能原因**63**可能原因**
62 64 
63-- 当前设备(DEVICE_ID)被其他进程占用,寄存器映射冲突。65+- 多进程/多worker/多容器/多agent并发映射同一`DEVICE_ID`,映射窗口冲突(最常见)。
64-- 同一设备上有残留进程未释放资源。66+- 同一设备上有残留进程未释放`/dev/davinci*`上下文。
65-- 多进程/多容器同时映射同一设备寄存器。
66 67 
67**处理方式**68**处理方式**
68 69 
69-1. 执行`npu-smi info`查看设备占用情况,确认目标DEVICE_ID是否被其他进程占用。70+1. **先原地重试(优先)**
70-2. 清理占用设备的残留进程:`fuser -v /dev/davinci<DEVICE_ID>`定位并`kill`。71+ 保持`TILE_FWK_DEVICE_ID`(或`ASCEND_DEVICE_ID`)不变,短间隔重跑同一用例(建议2–3次)。多数F71008会在重试后消失。
71-3. 在容器/多进程场景下,确保`ASCEND_DEVICE_ID`或`DEVICE_ID`环境变量设置正确且不冲突。72+2. **全量表诊断(仍失败再做)**
72-4. 若问题持续,尝试重启NPU驱动或切换空闲设备。73+ - 执行`npu-smi info`查看整体占用。
74+ - `fuser -v /dev/davinci<CHIP_ID>`定位占用进程;timeout kill后需确认残留进程已清理干净。
75+ - HBM占用约3–9GB往往表示仍有存活NPU上下文。
76+ - 容器/多进程场景下,确认各进程设备号设置正确且不冲突。
77+3. **受设备约束时:等待+同设备重试**
78+ **禁止**多agent/进程互相扫空闲卡换设备——并发扫卡会制造连环F71008(历史可致整机多卡挂死)。
79+4. **仅当确认长期独占或硬件异常时**,再切换空闲设备或重启NPU驱动。
73 80 
74## F7100B SYNC_FAILED81## F7100B SYNC_FAILED
75 82 
@@ -63,9 +63,8 @@ public:
63 isMainBlock(ctx.isMainBlock),63 isMainBlock(ctx.isMainBlock),
64 isDynamicAligned(ctx.isDynamicAligned)64 isDynamicAligned(ctx.isDynamicAligned)
65 {65 {
66- operandWithMagic.reserve(MAX_OPERANDS);66+ operandWithMagic.resize(MAX_OPERANDS, INVALID_TENSOR_MAGIC);
67 for (size_t i = 0; i < MAX_OPERANDS; i++) {67 for (size_t i = 0; i < MAX_OPERANDS; i++) {
68- operandWithMagic[i] = INVALID_TENSOR_MAGIC;
69 operandType[i] = BUF_UNKNOWN;68 operandType[i] = BUF_UNKNOWN;
70 operandDtype[i] = DataType::DT_BOTTOM;69 operandDtype[i] = DataType::DT_BOTTOM;
71 // In COA(Call Operation Attribute), 0-index is the callee's cce info. So the tensor list starts from 1.70 // In COA(Call Operation Attribute), 0-index is the callee's cce info. So the tensor list starts from 1.
@@ -493,7 +493,7 @@ std::string CodeGenOpNPU::GenMemCopyVar(bool isCopyLocalToGM, bool isSpillToGm,
493 }493 }
494 494 
495 ASSERT(OperErr::OPERAND_TYPE_UNSUPPORTED, false)495 ASSERT(OperErr::OPERAND_TYPE_UNSUPPORTED, false)
496- << "GenMemCopyVar: cannot support current localType!!!" << localType;496+ << "GenMemCopyVar: cannot support current localType!!! localType=" << localType;
497 return {};497 return {};
498}498}
499 499 
@@ -92,7 +92,7 @@ std::string CodeGenOpNPU::GenGatherInL1() const
92 ASSERT(GenCodeErr::DATA_TYPE_UNSUPPORTED, offsetsDtypeStr == "int64_t" || offsetsDtypeStr == "int32_t")92 ASSERT(GenCodeErr::DATA_TYPE_UNSUPPORTED, offsetsDtypeStr == "int64_t" || offsetsDtypeStr == "int32_t")
93 << "offsetsDtypeStr must be int64_t or int32_t!";93 << "offsetsDtypeStr must be int64_t or int32_t!";
94 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find("op_attr_blocksize") != opAttrs.end())94 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find("op_attr_blocksize") != opAttrs.end())
95- << "GenGatherOp: There is nop blocksize attribute here";95+ << "GenGatherOp: There is no blocksize attribute here";
96 const int64_t blockSize = AnyCast<int64_t>(opAttrs.at("op_attr_blocksize"));96 const int64_t blockSize = AnyCast<int64_t>(opAttrs.at("op_attr_blocksize"));
97 97 
98 auto startOffset = opAttrs.at(OpAttributeKey::startOffset);98 auto startOffset = opAttrs.at(OpAttributeKey::startOffset);
@@ -312,7 +312,7 @@ std::string CodeGenOpNPU::PrintGatherInUBLayout() const
312 std::string coord4BlockTable = PrintCoord(blockTableDim, coordCpblockTableOffset);312 std::string coord4BlockTable = PrintCoord(blockTableDim, coordCpblockTableOffset);
313 std::vector<std::string> paramList;313 std::vector<std::string> paramList;
314 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find(OpAttributeKey::blockSize) != opAttrs.end())314 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find(OpAttributeKey::blockSize) != opAttrs.end())
315- << "GenGatherOp: There is nop blockSize attribute here";315+ << "GenGatherOp: There is no blockSize attribute here";
316 const int64_t blockSize = AnyCast<int64_t>(opAttrs.at(OpAttributeKey::blockSize));316 const int64_t blockSize = AnyCast<int64_t>(opAttrs.at(OpAttributeKey::blockSize));
317 paramList.emplace_back(std::to_string(blockSize));317 paramList.emplace_back(std::to_string(blockSize));
318 std::string templateParam = JoinString(paramList, CONN_COMMA);318 std::string templateParam = JoinString(paramList, CONN_COMMA);
@@ -335,7 +335,7 @@ std::string CodeGenOpNPU::PrintGatherInUBDynamicUnaligned() const
335 ASSERT(GenCodeErr::DATA_TYPE_MISMATCHED, resultDtypeStr == paramDtypeStr)335 ASSERT(GenCodeErr::DATA_TYPE_MISMATCHED, resultDtypeStr == paramDtypeStr)
336 << "resultDtypeStr and paramDtypeStr must be same!";336 << "resultDtypeStr and paramDtypeStr must be same!";
337 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find(OpAttributeKey::blockSize) != opAttrs.end())337 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find(OpAttributeKey::blockSize) != opAttrs.end())
338- << "GenGatherOp: There is nop blockSize attribute here";338+ << "GenGatherOp: There is no blockSize attribute here";
339 const int64_t blockSize = AnyCast<int64_t>(opAttrs.at(OpAttributeKey::blockSize));339 const int64_t blockSize = AnyCast<int64_t>(opAttrs.at(OpAttributeKey::blockSize));
340 auto outputRawShapes = rawShape[ID0];340 auto outputRawShapes = rawShape[ID0];
341 auto paramRawShapes = rawShape[ID1];341 auto paramRawShapes = rawShape[ID1];
@@ -917,7 +917,7 @@ std::string CodeGenOpNPU::GenOpCode() const
917 ret += InsertOpComment(tileOpSourceCode);917 ret += InsertOpComment(tileOpSourceCode);
918 918 
919 if (forBlkMgr_ == nullptr || !forBlkMgr_->IsInLoop()) {919 if (forBlkMgr_ == nullptr || !forBlkMgr_->IsInLoop()) {
920- CODEGEN_LOGI_FULL("op codegen result: \n, %s", ret.c_str());920+ CODEGEN_LOGI_FULL("op codegen result: \n%s", ret.c_str());
921 return ret;921 return ret;
922 }922 }
923 923 
@@ -935,7 +935,7 @@ std::string CodeGenOpNPU::GenOpCode() const
935 935 
936 ret = forBlkMgr_->Print();936 ret = forBlkMgr_->Print();
937 forBlkMgr_->OutLoop();937 forBlkMgr_->OutLoop();
938- CODEGEN_LOGI_FULL("op codegen result: \n, %s", ret.c_str());938+ CODEGEN_LOGI_FULL("op codegen result: \n%s", ret.c_str());
939 return ret;939 return ret;
940}940}
941 941 
@@ -947,7 +947,7 @@ std::string CodeGenOpNPU::GetLastUse() const
947 std::vector<int64_t> val;947 std::vector<int64_t> val;
948 GetOpAttr(OpAttributeKey::lastUse, val);948 GetOpAttr(OpAttributeKey::lastUse, val);
949 int valSize = val.size();949 int valSize = val.size();
950- ASSERT(OperErr::ATTRIBUTE_INVALID, valSize != 0) << "GetLastUse error!!!";950+ ASSERT(OperErr::ATTRIBUTE_INVALID, valSize != 0) << "GetLastUse error, valSize is " << valSize;
951 std::ostringstream oss;951 std::ostringstream oss;
952 oss << "LastUse" << valSize << "Dim";952 oss << "LastUse" << valSize << "Dim";
953 oss << WrapParamByAngleBrackets(val);953 oss << WrapParamByAngleBrackets(val);
@@ -139,7 +139,7 @@ std::string CodeGenOpNPU::GenGatherFromUBOp() const
139 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);139 std::string dVar = sm->QueryVarNameByTensorMagic(operandWithMagic[ID0]);
140 140 
141 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find("op_attr_axis") != opAttrs.end())141 ASSERT(OperErr::ATTRIBUTE_INVALID, opAttrs.find("op_attr_axis") != opAttrs.end())
142- << "GenGatherOp: There is nop axis attribute here";142+ << "GenGatherOp: There is no axis attribute here";
143 const int64_t axis = AnyCast<int64_t>(opAttrs.at("op_attr_axis"));143 const int64_t axis = AnyCast<int64_t>(opAttrs.at("op_attr_axis"));
144 // shape: dst, src0, src1144 // shape: dst, src0, src1
145 int dim = rawShape[ID1].size();145 int dim = rawShape[ID1].size();
@@ -412,8 +412,8 @@ std::string CodeGenOpNPU::GenScatterElementSOp() const
412 std::string dstDtypeStr = DataType2CCEStr(dstDtype);412 std::string dstDtypeStr = DataType2CCEStr(dstDtype);
413 std::string src0DtypeStr = DataType2CCEStr(src0Dtype);413 std::string src0DtypeStr = DataType2CCEStr(src0Dtype);
414 std::string src1DtypeStr = DataType2CCEStr(src1Dtype);414 std::string src1DtypeStr = DataType2CCEStr(src1Dtype);
415- CODEGEN_LOGI("GenScatterElementSOp, dstDtypeStr%s", dstDtypeStr.c_str());415+ CODEGEN_LOGI("GenScatterElementSOp, dstDtypeStr: %s", dstDtypeStr.c_str());
416- CODEGEN_LOGI("GenScatterElementSOp, src1DtypeStr%s", src1DtypeStr.c_str());416+ CODEGEN_LOGI("GenScatterElementSOp, src1DtypeStr: %s", src1DtypeStr.c_str());
417 417 
418 AppendLocalBufVarOffsetInOrder(dstVar, src0Var, src1Var);418 AppendLocalBufVarOffsetInOrder(dstVar, src0Var, src1Var);
419 419 
@@ -385,7 +385,7 @@ std::string CodeGenOpNPU::PrintRowSumlineTileTensor() const
385 reduceAxis = AnyCast<int64_t>(axis);385 reduceAxis = AnyCast<int64_t>(axis);
386 }386 }
387 ASSERT(OperErr::ATTRIBUTE_INVALID, ((reduceAxis >= 0) && (reduceAxis < (int(rawShape[ID2].size()) - 1))))387 ASSERT(OperErr::ATTRIBUTE_INVALID, ((reduceAxis >= 0) && (reduceAxis < (int(rawShape[ID2].size()) - 1))))
388- << "unsupported reduce axis";388+ << "unsupported reduce axis: " << reduceAxis;
389 reduceAxis += SHAPE_DIM5 - rawShape[0].size();389 reduceAxis += SHAPE_DIM5 - rawShape[0].size();
390 std::ostringstream oss;390 std::ostringstream oss;
391 oss << tileOpName;391 oss << tileOpName;
@@ -286,7 +286,7 @@ public:
286 auto res = tensorMap_.insert({magicNum, tensor});286 auto res = tensorMap_.insert({magicNum, tensor});
287 if (!res.second) {287 if (!res.second) {
288 ASSERT(GenCodeErr::TENSOR_MAGIC_CONFLICT, tensor == tensorMap_[magicNum])288 ASSERT(GenCodeErr::TENSOR_MAGIC_CONFLICT, tensor == tensorMap_[magicNum])
289- << "!!! ERROR !!! tensor magic : " << magicNum289+ << "tensor magic : " << magicNum
290 << " is conflicted!!!\ninsert tensor key: " << FormatAllocKey(CreateAllocKey(tensor))290 << " is conflicted!!!\ninsert tensor key: " << FormatAllocKey(CreateAllocKey(tensor))
291 << "\ntensor dump info -- " << tensor->Dump()291 << "\ntensor dump info -- " << tensor->Dump()
292 << "\nexisted tensor key: " << FormatAllocKey(CreateAllocKey(tensorMap_[magicNum]))292 << "\nexisted tensor key: " << FormatAllocKey(CreateAllocKey(tensorMap_[magicNum]))
@@ -31,7 +31,7 @@ std::vector<int64_t> NormalizeShape(const std::vector<int64_t>& shapeVec, unsign
31{31{
32 std::vector<int64_t> normalizedVec(dim, 1);32 std::vector<int64_t> normalizedVec(dim, 1);
33 for (size_t i = 0; i < shapeVec.size(); i++) {33 for (size_t i = 0; i < shapeVec.size(); i++) {
34- ASSERT(OperErr::TENSOR_DIM_EXCEEDED, i < dim) << "exceed dimension limit!";34+ ASSERT(OperErr::TENSOR_DIM_EXCEEDED, i < dim) << "exceed dimension limit! index=" << i << ", dim=" << dim;
35 normalizedVec[i] = shapeVec[shapeVec.size() - 1 - i];35 normalizedVec[i] = shapeVec[shapeVec.size() - 1 - i];
36 }36 }
37 std::reverse(normalizedVec.begin(), normalizedVec.end());37 std::reverse(normalizedVec.begin(), normalizedVec.end());