75 Star 584 Fork 1.1K

Ascend/pytorch

使用Thread发起transformers中model的generate方法时,报错

DONE
Bug-Report
创建于  
2024-02-27 09:35

一、问题现象(附报错日志上下文):
在主线程初始化模型后,使用Thread发起新的线程进行generate时报错,无法使用流式输出

二、软件版本:
-- CANN 版本 (e.g., CANN 3.0.x,5.x.x): 7.0.RC1
--Tensorflow/Pytorch/MindSpore 版本:torch==2.1.0
--Python 版本 (e.g., Python 3.7.5):3.9
-- MindStudio版本 (e.g., MindStudio 2.0.0 (beta3)):不涉及
--操作系统版本 (e.g., Ubuntu 18.04): 5.10.0-60.18.0.50.h1002.eulerosv2r11.aarch64

三、测试步骤:

from threading import Thread

import torch
import torch_npu
from transformers import AutoModel, AutoTokenizer, TextIteratorStreamer

model = AutoModel.from_pretrained("...some local model path", trust_remote_code=True).eval().npu()
tokenizer = AutoTokenizer.from_pretrained("...some local model path as model", trust_remote_code=True)
prompt = "some prompt string"
inputs = tokenizer(prompt, return_tensors="pt")
inputs_ids = inputs["input_ids"].npu()
streamer = TextIteratorStreamer(tokenizer)

generate_args = {
        "inputs": inputs_ids,
        "temperature": 0.1,
        "max_new_tokens": 4096,
        "do_sample": True,
        "top_p": 0.7,
        "streamer": streamer
}

thread = Thread(target=model.generate, kwargs=generate_args)
# ERROR
thread.start()

四、日志信息:
Exception in thread Thread-2:
Traceback (most recent call last):
File "/root/anaconda3/envs/ci39/lib/python3.9/threading.py", line 980, in _bootstrap_inner
self.run()
File "/root/anaconda3/envs/ci39/lib/python3.9/threading.py", line 917, in run
self._target(*self._args, **self._kwargs)
File "/root/anaconda3/envs/ci39/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
File "/root/anaconda3/envs/ci39/lib/python3.9/site-packages/transformers/generation/utils.py", line 1392, in generate
model_kwargs["attention_mask"] = self._prepare_attention_mask_for_generation(
File "/root/anaconda3/envs/ci39/lib/python3.9/site-packages/transformers/generation/utils.py", line 476, in _prepare_attention_mask_for_generation
is_pad_token_in_inputs = (pad_token_id is not None) and (pad_token_id in inputs)
File "/root/anaconda3/envs/ci39/lib/python3.9/site-packages/torch/_tensor.py", line 1059, in contains
return (element == self).any().item() # type: ignore[union-attr]
RuntimeError: allocate:torch_npu/csrc/core/npu/NPUCachingAllocator.cpp:2050 NPU error, error code is 107002
[Error]: The context is empty.
Check whether acl.rt.set_context or acl.rt.set_device is called.
EE1001: The argument is invalid.Reason: rtGetDevMsg execute failed, reason=[context pointer null]
Solution: 1.Check the input parameter range of the function. 2.Check the function invocation relationship.
TraceBack (most recent call last):
ctx is NULL![FUNC:GetDevErrMsg][FILE:api_impl.cc][LINE:4290]
The argument is invalid.Reason: rtGetDevMsg execute failed, reason=[context pointer null]

可直接内网联系工号 466015
请根据自己的运行环境参考以下方式搜集日志信息,如果涉及到算子开发相关的问题,建议也提供UT/ST测试和单算子集成测试相关的日志。

日志提供方式:
将日志打包后作为附件上传。若日志大小超出附件限制,则可上传至外部网盘后提供链接。

获取方法请参考wiki:
https://gitee.com/ascend/modelzoo/wikis/如何获取日志和计算图?sort_id=4097825

评论 (2)

chenmai1102 创建了Bug-Report 1年前
chenmai1102 修改了描述 1年前
展开全部操作日志

在新起的线程里torch_npu.npu.set_device(device)一次

可以了 感谢

Destiny 任务状态TODO 修改为Analysing 1年前
chenmai1102 任务状态Analysing 修改为DONE 1年前

登录 后才可以发表评论

状态
负责人
项目
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
预计工期 (小时)
参与者(2)
Destiny-wx1103340 6584616 chenmai1102 1584610327
Python
1
https://gitee.com/ascend/pytorch.git
git@gitee.com:ascend/pytorch.git
ascend
pytorch
pytorch

搜索帮助