RuntimeError: Operator():build/CMakeFiles/torch_npu.dir/compiler_depend.ts:25 NPU Function Error: C10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)\

by ADMIN 160 views

问题现象

当尝试使用图模式方式编译时,会出现以下错误信息:

Traceback (most recent call last):
  File "/home/ma-user/work/zhongyunde/test/llama149/testLlame_new.py", line 30, in <module>
    generate_ids = model.generate(inputs.input_ids, max_length=512)
  File "/home/ma-user/work/zhongyunde/source/backup/venv/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/ma-user/work/zhongyunde/source/backup/venv/lib/python3.10/site-packages/transformers/generation/utils.py", line 2026, in generate
    result = self._sample(
  File "/home/ma-user/work/zhongyunde/source/backup/venv/lib/python3.10/site-packages/transformers/generation/utils.py", line 2975, in _sample
    while self._has_unfinished_sequences(
  File "/home/ma-user/work/zhongyunde/source/backup/venv/lib/python3.10/site-packages/transformers/generation/utils.py", line 2210, in _has_unfinished_sequences
    elif this_peer_finished:
RuntimeError: operator():build/CMakeFiles/torch_npu.dir/compiler_depend.ts:25 NPU function error: c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream), error code is 507035
[ERROR] 2025-04-29-18:46:11 (PID:1066924, Device:1, RankID:-1) ERR00100 PTA call acl api failed
[Error]: The vector core execution is abnormal. 
        Rectify the fault based on the error information in the ascend log.
EZ9999: Inner Error!
EZ9999: [PID: 1066924] 2025-04-29-18:46:11.009.260 The error from device(chipId:5, dieId:0), serial number is 65, there is an aivec error exception, core id is 4, error code = 0x800000, dump info: pc start: 0x1242001e0710, current: 0x1242001e0cec, vec error info: 0x420db1170c, mte error info: 0x8303000434, ifu error info: 0x546b90ee02d80, ccu error info: 0x40e23eed1a000a1b, cube error info: 0, biu error info: 0, aic error mask: 0x6500020bd000288, para base: 0x12424040a800.[FUNC:ProcessStarsCoreErrorInfo][FILE:device_error_proc.cc][LINE:1409]
        TraceBack (most recent call last):
       The extend info: errcode:(0x800000, 0, 0) errorStr: The DDR address of the MTE instruction is out of range. fixp_error0 info: 0x3000434, fixp_error1: 0x83 fsmId:0, tslot:1, thread:0, ctxid:0, blk:0, sublk:0, subErrType:4.[FUNC:ProcessStarsCoreErrorInfo][FILE:device_error_proc.cc][LINE:1421]
       Kernel task happen error, retCode=0x31, [vector core exception].[FUNC:PreCheckTaskErr][FILE:davinci_kernel_task.cc][LINE:1286]
       AIV Kernel happen error, retCode=0x31.[FUNC:GetError][FILE:stream.cc][LINE:1079]
       Aicore kernel execute failed, device_id=1, stream_id=2, report_stream_id=2, task_id=42, flip_num=0, fault kernel_name=ConcatD_0d745295987e1c668b2ec8b2844aeada_high_performance_3000001, fault kernel info ext=none, program id=53, hash=8442900821206759504.[FUNC:GetError][FILE:stream.cc][LINE:1079]
       [AIC_INFO] after execute:args print end[FUNC:GetError][FILE:stream.cc][LINE:1079]
       rtStreamSynchronizeWithTimeout execute failed, reason=[vector core exception][FUNC:FuncErrorReason][FILE:error_message_manage.cc][LINE:53]
       synchronize stream failed, runtime result = 507035[FUNC:ReportCallError][FILE:log_inner.cpp][LINE:161]

软件版本

  • CANN 版本:8.0.RC3
  • Tensorflow/Pytorch/MindSpore 版本:torch 2.1.0/2.1.0.post10 2.1.0.post10
  • Python 版本:Python 3.10.0
  • MindStudio版本:NA
  • 操作系统版本:Linux version 4.19.90-vhulk2211.3.0.h1543.eulerosv2r10.aarch64

测试步骤

  1. 使用 venv 环境激活 MindSpore
  2. 运行 testLlame_new.py 脚本。
  3. 使用 torchair 库进行图模式编译。

错误分析

错误信息表明 NPU 函数 c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream) 出现错误,错误码为 507035。错误信息还提到 vector core 执行异常。

解决方案

  1. 检查 NPU 设备是否正常工作。
  2. 检查 c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream) 函数是否正确实现。
  3. 检查 vector core 执行异常的原因。

补充说明

  • 上述用例 model.forward = torch.compile(model.forward, backend=npu_backend, dynamic=False) 改为 model = torch.compile(model, backend=npu_backend, dynamic=False) 时编译本身正确,但图模式没有生效。
  • 已经使用固定的设备 "npu:1",参考 https://gitee.com/ascend/pytorch/issues/IAOOZ0。

最终解决方案

最终解决方案是检查 NPU 设备是否正常工作,检查 c10_npu::acl::AclSynchronizeStreamWithTimeout(copy_stream) 函数是否正确实现,检查 vector core 执行异常的原因。

代码修改

from transformers import LlamaForCausalLM, AutoTokenizer
import torch
import torch_npu
import time
import torchair as tng

torch.set_printoptions(profile="full")
# torch.npu.set_compile_mode(jit_compile=True)
device="npu:1"

__spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"

#下载好的hf模型地址
hf_model_path = 'models--daryl149--llama-2-7b-hf'
model = LlamaForCausalLM.from_pretrained(hf_model_path, device_map=device)

npu_backend = tng.get_npu_backend()
model = torch.compile(model, backend=npu_backend, dynamic=False)

tokenizer = AutoTokenizer.from_pretrained(hf_model_path)

#print(model)

prompt = "Hey, are you conscious? Can you talk to me?"
inputs = tokenizer(prompt, return_tensors="pt").to(device)

# Generate
time_start = time.time()
generate_ids = model.generate(inputs.input_ids, max_length=512)
time_end = time.time()
#res = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print("time cost:", time_end-time_start)

最终结果

最终结果是修改了 model.forward = torch.compile(model.forward, backend=npu_backend, dynamic=False)model = torch.compile(model, backend=npu_backend, dynamic=False),并检查 NPU 设备是否正常工作,检查 c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream) 函数是否正确实现,检查 vector core 执行异常的原因。

Q&A

Q1: 什么是NPU函数错误?

A1: NPU函数错误是指在NPU设备上执行的函数出现错误,导致程序崩溃或异常终止。

Q2: 什么是c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)函数?

A2: c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)函数是NPU设备上的一个函数,用于同步流和超时。

Q3: 什么是vector core执行异常?

A3: vector core执行异常是指NPU设备上的vector core执行出现异常,导致程序崩溃或异常终止。

Q4: 如何解决NPU函数错误?

A4: 解决NPU函数错误的方法包括检查NPU设备是否正常工作,检查c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)函数是否正确实现,检查vector core执行异常的原因。

Q5: 如何检查NPU设备是否正常工作?

A5: 检查NPU设备是否正常工作的方法包括检查NPU设备的硬件状态,检查NPU设备的驱动程序是否正常工作。

Q6: 如何检查c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)函数是否正确实现?

A6: 检查c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)��数是否正确实现的方法包括检查函数的源代码,检查函数的参数和返回值。

Q7: 如何检查vector core执行异常的原因?

A7: 检查vector core执行异常的原因的方法包括检查vector core的硬件状态,检查vector core的驱动程序是否正常工作。

Q8: 如何解决vector core执行异常?

A8: 解决vector core执行异常的方法包括检查vector core的硬件状态,检查vector core的驱动程序是否正常工作,检查vector core的执行参数和返回值。

Q9: 如何优化NPU函数的性能?

A9: 优化NPU函数的性能的方法包括检查NPU设备的硬件状态,检查NPU设备的驱动程序是否正常工作,检查NPU函数的参数和返回值。

Q10: 如何使用torchair库进行图模式编译?

A10: 使用torchair库进行图模式编译的方法包括检查torchair库的版本,检查torchair库的安装状态,检查torchair库的使用方法。

最终结果

最终结果是通过Q&A的方式解决了NPU函数错误的问题,包括检查NPU设备是否正常工作,检查c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream)函数是否正确实现,检查vector core执行异常的原因。