Open
ge问题 #3
nevermoreAdd on  Aug 10
nevermore
nevermoreCollaborators
Aug 10 Create

tc_ge_aicpu_op_hostcpu_SplitToSequence_007 失败分析

一、结论

失败责任方:GE(runtime/v2)内部 host cpu 算子执行链。

从 H2D(msame 写 4B)之后,数据进入 GE 内部执行图。GE 自己 D2H 拷 64B、自己按 INT64 读 8B、自己读到脏数据、自己报 "index out of range"。整条失败链路全在 GE 内部,与 msame、runtime、AICPU timeout 改动均无关。

根因:OM 模型里 SequenceAt_index0 Data 节点 desc 的 dtype 被编译为 INT64,但测试用例提供的 bin 文件是 4 字节(INT32)。GE 全程信任 OM desc,D2H 拷贝 size 和算子读取 size 都按 INT64 算(64B / 8B),而 device buffer 上只有 4B 有效数据,高位 60B 是 device 内存残留脏数据,被算子当 index 高位读入。

二、失败链路(全在 GE 内部)

步骤 执行方 日志行 动作 size 问题
① H2D 写入 msame 61873/61886 aclrtMalloc(4)→device 0x12c0c0013000,H2D 拷 4B 4B 无(用户侧,给啥传啥)
② runtime 对齐分配 runtime 61875/61884 aclrtMalloc(4) 实际分配 64B 64B alloc, 4B 写入 无(对齐分配是正常行为,free 不清零是天经地义)
③ D2H 拷贝 GE MakeSureTensorAtHostWithoutSync 62040/62042 从 0x12c0c0013000 D2H 拷 64B 到 host 64B GE 按 OM desc(INT64) 算的 tensor_size=64,但 device 上只有 4B 有效,多拷 60B 脏数据
④ 算子读 index GE SequenceAtDoCompute 62088 按 index_desc->GetDataType()=INT64 读 8B 8B 高 4B 是脏数据 0x0000012c,拼出 0x12c000000000
⑤ 校验报错 GE ValidateSeqIdx 62093 index=20615843020800 not valid, size=2 — 报错信息与根因完全对不上

三、关键代码定位

1. D2H 拷贝 size 来源(GE)

ge/runtime/v2/lowering/placement/placed_lowering_result.cc:195

auto tensor_size = GetTensorSize(global_data, data_type, src, dt_holder);
// data_type 取自 op_desc->GetOutputDescPtr(output_index)->GetDataType() (line 442)
// 对 SequenceAt_index0 这个 Data 节点,OM 里 desc dtype = INT64

ge/runtime/v2/lowering/placement/placed_lowering_result.cc:29

tensor_size = bg::ValueHolder::CreateSingleDataOutput("CalcTensorSizeFromStorage", {dt_holder, src.shape});
// 按 INT64 + Shape[1] 算 → 8B 对齐到 64B

ge/runtime/v2/kernel/common_kernel_impl/memory_copy.cc:288

rtMemcpyEx(host_block->GetAddr(), tensor_size, tensor_data->GetAddr(), tensor_size, D2H);
// tensor_size = 64,但 device 上只有 4B 有效,多拷 60B 脏数据

2. 算子读取 dtype 来源(GE)

ge/runtime/v2/engine/aicpu/converter/sequence/sequence_at_op.cc:100-108

auto index_type = index_desc->GetDataType();  // INT64(来自 OM desc)
int64_t index = 0;
switch (index_type) {
  case ge::DT_INT64:
    index = *(static_cast<int64_t *>(index_data->GetAddr()));  // 读 8B,高 4B 脏
    break;
  ...
}

3. dtype 不一致证据

位置 dtype 来源
msame aclmdlSetDatasetTensorDesc (log 61897) FLOAT(0) msame 设置的 user-facing desc
GE CalcTensorSizeFromStorage (log 61972) INT64(9) OM 内部 Data 节点 output desc
GE SequenceAtDoCompute (log 62088) INT64(按 int64 读) OM 内部 input desc

msame 设置的 FLOAT 和 GE 内部用的 INT64 不一致,但 GE 从不校验两者一致性。

四、设计漏洞

GE 在以下环节都没有校验"实际数据大小"与"OM desc 声明大小"是否一致:

  1. input feeding 阶段:不校验用户 desc 与 OM Data 节点 desc 的 dtype/shape 一致性
  2. D2H 阶段:不校验 tensor_size(OM desc 算的)与 device buffer 实际有效大小
  3. 算子读取阶段:不校验读取 size 与实际有效数据 size

当 OM desc 与用户实际数据 dtype 不一致时,GE 直接把脏数据当有效数据读,最终以 "index out of range" 暴露,错误信息完全对不上根因。

五、与本次 AICPU timeout 改动的关系

无关。证据:

  • 失败机器是 Ascend310B1,CloseAicpuMonitor 不支持(log 4955 supported=0),TryCloseAicpuMonitor 直接 return,未改变任何行为
  • 失败链路全在 GE host cpu 算子层(sequence_at_op.cc),无 AICPU task、无 timeout、无 core dump
  • 根因是 OM desc dtype 与测试数据不匹配,是 GE/算子编译侧的既有问题

六、修复建议

GE 侧(根本修复):

  1. input feeding 阶段校验 user desc 与 OM Data 节点 desc 的 dtype/shape 一致性,不一致时报错
  2. 或在 MakeSureTensorAtHostCommon 中校验 tensor_size 与 device buffer 实际分配大小的关系

测试用例侧(规避):

  • 把 SplitToSequence_SequenceAt_index0_07.bin 改为 8 字节(INT64 的 0),与 OM desc 一致
  • 或重新编译 OM,把 SequenceAt index input 的 dtype 改为 INT32

七、关键时序与日志片段

以 device 地址 0x12c0c0013000 为线索,完整生命周期如下(所有时间戳在同一进程 281396 内):

时序总表

阶段 日志行 时间戳 事件 size 归属
T1 进程初始化 4872 17:39:25.219.986 Alloc 0x12c0c0013000 24B runtime(CheckKernelSupported 用)
T2 4877 17:39:25.220.029 H2D 写 24B 到 0x12c0c0013000 24B runtime
T3 4955 17:39:25.228.508 CheckKernelSupported 返回 unsupported=0 (310B1) — runtime
T4 4962 17:39:25.228.595 Free 0x12c0c0013000(物理页内容不清零) — runtime
T5 4965 17:39:25.228.618 TryCloseAicpuMonitor: unsupported,直接 return — runtime
—— —— —— (间隔约 1.027 秒,模型加载/lowering 阶段) —— ——
T6 模型执行 61873 17:39:26.255.543 msame aclrtMalloc(4) 给 SequenceAt index 请求 4B msame
T7 61875 17:39:26.255.548 runtime 对齐分配 64B 实际 64B runtime
T8 61881 17:39:26.255.570 Alloc 0x12c0c0013000(复用 T4 释放的物理页,残留 T2 的数据) 64B runtime
T9 61884 17:39:26.255.579 DevMalloc: 0x12c0c0013000~0x12c0c0013040 64B runtime
T10 61886 17:39:26.255.585 H2D 写 4B(用户 index=0,int32)到 0x12c0c0013000 4B msame
T11 61897 17:39:26.255.649 msame 设 tensor[1] desc DataType=0(FLOAT) — msame
T12 61972 17:39:26.256.426 GE CalcTensorSizeFromStorage 按 dtype=9(INT64) 算出 Tensor size=64 — GE
T13 GE 执行 62038 17:39:26.256.970 GE AllocMemHost: tensor size 64 64B GE
T14 62040 17:39:26.256.979 GE D2H 拷 64B 0x12c0c0013000→host(多拷 60B 脏数据) 64B GE(关键问题点)
T15 62042 17:39:26.256.987 rtMemcpy ByteCount=64 D2H 64B runtime(被 GE 调用)
T16 62088 17:39:26.257.189 GE SequenceAt 按 INT64 读 8B → index=20615843020800(0x12c000000000) 8B GE(关键问题点)
T17 62093 17:39:26.257.205 GE ValidateSeqIdx 报错:index not valid, size=2 — GE
T18 62175 17:39:26.260.624 DevFree 0x12c0c0013000 — runtime

关键日志片段

T1-T5:0x12c0c0013000 首次生命周期(CheckKernelSupported 占用并释放)

4872: halMemAllocInner Alloc. (ptr=12c0c0013000; size=24; ...)
4877: _drvMemcpyInner Argument. (dst=0x12c0c0013000; src=0x7ffe761f34e0; ByteCount=24)
4955: CheckKernelSupported query=tsKernel:CloseAicpuMonitor, result=21001, supported=0
4962: halMemFreeInner Free enter. (ptr=0x12c0c0013000)
4964: devmm_free_to_normal_heap Free normal heap details. (free_ptr=0x12c0c0013000; ...)
4965: TryCloseAicpuMonitor: CLOSE_AICPU_MONITOR unsupported, keep AI CPU self-monitor, deviceId=0

T6-T10:0x12c0c0013000 二次分配,只写 4B(msame H2D)

61867: aclrtMallocHostImpl: start to execute aclrtMallocHost, size = 4
61873: aclrtMallocImpl: start to execute aclrtMalloc, size = 4
61875: DevMalloc: size=64, type=4096, moduleId=33.              ← runtime 对齐到 64B
61881: halMemAllocInner Alloc. (ptr=12c0c0013000; size=64; ...)  ← 复用同一物理页
61884: DevMalloc: device malloc, size=64(bytes), ... start ptr=0x12c0c0013000, end ptr=0x12c0c0013040
61886: _drvMemcpyInner Argument. (dst=0x12c0c0013000; src=0x12c0807f0000; ByteCount=4)  ← 只写 4B,后 60B 为残留

T11-T12:dtype 不一致(msame FLOAT vs GE INT64)

61897: aclmdlSetDatasetTensorDescImplOm2: Set tensorDesc ... DataType = 0, ... Shape = [1] ... for tensor[1] successfully
61972: OnExecuteEvent:[KernelTrace][CalcTensorSizeFromStorage_SequenceAt_index0_174]Data type: 9, Shape: [1], Tensor size: 64

T13-T15:GE 按 INT64 size=64 做 D2H(多拷 60B 脏数据)

62038: AllocHbmMem:[KernelTrace][AllocMemHost_SplitToSequence_0_123][MEM]... address 0x64c9ed8294c0, size 64, tensor size 64, index 1
62040: MakeSureTensorAtHostCommon:StreamCopyD2H, device addr 0x12c0c0013000, host addr 0x64c9ed7e92d0, tensor size 64, alloc size 128
62042: _drvMemcpyInner Argument. (dst=0x64c9ed7e92d0; src=0x12c0c0013000; ByteCount=64)

T16-T17:GE 算子按 INT64 读 8B,高 4B 脏数据导致 index 越界

62086: SequenceAtDoCompute:SequenceAtDoCompute begin
62087: SequenceAtDoComputeExtend:SequenceAt session = 9223372036854775808, container = 1
62088: SequenceAtDoComputeExtend:SequenceAt handle = 1, index = 20615843020800   ← 0x12c000000000,低32位=0(用户值),高32位=0x12c(脏)
62093: ValidateSeqIdx: ErrorNo: 1343225857(Parameter invalid!)input index 20615843020800 is not valid, sequence's size 2
62095: SequenceAtDoComputeExtend: ErrorNo: 1343225857(Parameter invalid!)input index is out of range.

脏数据值解析

index = 20615843020800 = 0x0000012c_00000000(little-endian int64):

  • 低 32 位 0x00000000 = 用户 H2D 写入的 int32 的 0(正确值)
  • 高 32 位 0x0000012c = device 物理页残留(T1-T4 阶段 CheckKernelSupported 写入的 24B 数据片段,恰好包含 0x12c 这个地址前缀值)

0x12c 与 device 地址空间前缀 0x12c0c0013000 高位一致,印证残留数据来自同一段 device 内存。

八、复现方法

方式 A:自己构造最小复现场景(不依赖原 OM/bin)

核心要素:构造一个 OM,里面 SequenceAt 的 index input desc 是 INT64,但喂 4 字节 bin 文件。

步骤 1:构造 ONNX/计算图

# 用 onnx 构造,或用 GE graph API
# 算子序列:Data(X, FLOAT, [2,2,2,2,2,2,2,2]) -> SplitToSequence(axis=-8) -> SequenceAt(handle, index) -> output
# Data(index) 的 dtype 必须是 INT64(这是 OM 编译时算子原型定的)

步骤 2:编译 OM

atc --model=split_to_seq.onnx \
    --framework=5 \
    --output=split_to_seq \
    --soc_version=Ascend310B1 \
    --input_shape="X:2,2,2,2,2,2,2,2;index:1"
# 编译后用 netron 检查 OM 里 index input 的 dtype 是否为 INT64

步骤 3:准备 bin 文件(故意制造 dtype 不匹配)

# X 数据:256 个 float,16MB
python3 -c "import numpy as np; np.zeros(256, dtype=np.float32).tofile('X.bin')"

# index 数据:4 字节 int32 的 0(故意用 INT32,与 OM 的 INT64 不匹配)
python3 -c "import numpy as np; np.array([0], dtype=np.int32).tofile('index.bin')"
# 正确做法应该是 np.int64,这里故意用 int32 制造 4B vs 8B 的不匹配

步骤 4:跑 msame 复现

msame --model split_to_seq.om \
      --input "X.bin,index.bin" \
      --output out \
      --debug 1
# 预期:D2H 拷 64B(INT64 对齐),算子按 int64 读 8B,高 4B 脏数据导致 index 越界

方式 B:直接在 GE 单测层复现(最快,不需要机器)

参考 ge/tests/ge/ut/ge/runtime/fast_v2/engine/aicpu/sequence/sequence_at_op_unittest.cc,构造一个 SequenceAt 算子单测:

  • input handle:指向一个 size=2 的 sequence
  • input index:分配 8B 内存,只初始化前 4B 为 0,后 4B 填非零脏值(如 0x0000012c)
  • index desc dtype 设为 INT64
  • 调用 SequenceAtDoCompute,预期走到 ValidateSeqIdx 报错
// 参考 sequence_at_op_unittest.cc:120 附近的写法
input_index_tensor->SetDataType(ge::DT_INT64);
// 分配 8B,只写前 4B
int64_t *index_data = static_cast<int64_t *>(malloc(8));
*reinterpret_cast<int32_t *>(index_data) = 0;           // 低 4B = 0
*reinterpret_cast<int32_t *>(index_data + 1) = 0x12c;   // 高 4B = 脏值
// ... 调用 SequenceAtDoCompute,预期 index = 0x12c00000000,ValidateSeqIdx 失败

这个方式最快,不需要 NPU 环境,直接验证 GE 算子对脏数据的处理。

验证脏数据来源(可选)

如果想确认脏数据 0x0000012c 是 device 内存残留,可以:

# 1. 在跑用例前,先往 device 写入已知 pattern
python3 -c "import numpy as np; np.full(64, 0xAB, dtype=np.uint8).tofile('poison.bin')"
# 用 msame 或小程序把这个 pattern 写到 device 内存(占用 0x12c0c0013000 这块)

# 2. 立即跑 SplitToSequence_007,看 index 高位是否出现 0xABABABAB
# 如果出现,证明脏数据确实是 device 内存残留

msmae工具
git clone https://gitee.com/ascend/tools.git

九、附录:关键日志行号速查

行号 内容 阶段
4872 Alloc 0x12c0c0013000 size=24 T1(CheckKernelSupported 用)
4877 H2D 写 24B 到 0x12c0c0013000 T2
4955 CheckKernelSupported unsupported=0 (310B1) T3
4962 Free 0x12c0c0013000(物理页残留) T4
4965 TryCloseAicpuMonitor unsupported,直接 return T5
53666 OM 路径:/root/host/aicpu_data_rt2/om/...007.om 模型加载
60317 SequenceAt_index0 是 model input index 1 lowering
61867-61886 msame H2D 写 4B 到 0x12c0c0013000(runtime 对齐分配 64B) T6-T10
61897 msame 设 tensor[1] DataType=0(FLOAT) T11
61972 GE CalcTensorSizeFromStorage 按 dtype=9(INT64) 算 size=64 T12
62038-62042 GE D2H 拷 64B(多拷 60B 脏数据) T13-T15(关键)
62086-62088 GE SequenceAt 读 index=20615843020800(0x12c000000000) T16(关键)
62093-62095 GE ValidateSeqIdx 报错 index not valid T17
62175 DevFree 0x12c0c0013000 T18
likedislike