自定义 bert 在 onnxruntime 推理错误:TypeError: run(): incompatible function arguments

自定义 bert 在 onnxruntime 推理错误:TypeError: run(): incompatible function arguments

自定义 bert 在 onnxruntime 推理错误:TypeError: run(): incompatible function arguments

推理代码

复制代码
    # text embedding
    toks = self.tokenizer([text])
    if self.debug:
        print('toks', toks)

    text_embed = self.text_model_session.run(output_names=['output'], input_feed=toks)

错误提示

复制代码
Traceback (most recent call last):
  File "/xx/workspace/model/test_onnx.py", line 90, in <module>
    res = inferencer.inference(text, img_path)
  File "/xx/workspace/model/test_onnx.py", line 58, in inference
    text_embed = self.text_model_session.run(output_names=['output'], input_feed=toks)
  File "/xx/miniconda3/envs/py39/lib/python3.9/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 220, in run
    return self._sess.run(output_names, input_feed, run_options)
TypeError: run(): incompatible function arguments. The following argument types are supported:
    1. (self: onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession, arg0: List[str], arg1: Dict[str, object], arg2: onnxruntime.capi.onnxruntime_pybind11_state.RunOptions) -> List[object]

Invoked with: <onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession object at 0x7f975ded1570>, ['output'], {'input_ids': array([[ 101, 3899,  102]]), 'token_type_ids': array([[0, 0, 0]]), 'attention_mask': array([[1, 1, 1]])}, None

核心错误

复制代码
TypeError: run(): incompatible function arguments. The following argument types are supported:
    1. (self: onnxruntime.capi.onnxruntime_pybind11_state.InferenceSession, arg0: List[str], arg1: Dict[str, object], arg2: onnxruntime.capi.onnxruntime_pybind11_state.RunOptions) -> List[object]

解决方法

核对参数

arg0: Liststr

arg1: Dictstr, object

对应的参数

复制代码
output_names=['output'], input_feed=toks

arg0='output' 参数类型正确

arg1=toks 表面看参数也正常,打印看看toks的每个值的类型

type(toks'input_ids') 输出为 <class 'torch.Tensor'>, 实际需要输入类型为 <class 'numpy.ndarray'>

修改代码

复制代码
    # text embedding
    toks = self.tokenizer([text])
    if self.debug:
        print('toks', toks)
    
    text_input = {}
    text_input['input_ids'] = toks['input_ids'].numpy()
    text_input['token_type_ids'] = toks['token_type_ids'].numpy()
    text_input['attention_mask'] = toks['attention_mask'].numpy()
    text_embed = self.text_model_session.run(output_names=['output'], input_feed=text_input)

再次执行代码,正常运行,无报错!!

相关推荐
工业涂料百问几秒前
【市场格局】系列(四)汽车、船舶、风电涂料赛道对比:1400亿工业涂料里,哪个细分最“肥“?
人工智能·汽车
桃西西呀2 分钟前
红酒标签上的 87 分是怎么算出来的?我拿 1599 瓶真酒把线性回归和逻辑回归拆开讲
人工智能·机器学习·llm
帅哥的AI自修课5 分钟前
你的AI开发还是单打独斗?MetaGPT焊死「多智能体协作」,从角色分工到代码生成一篇打通
人工智能
HZZD_HZZD8 分钟前
商业综合体分户计量与能源计费:业态差异、公区分摊与转供电合规
大数据·人工智能·能源
DevOps老兵9 分钟前
AI Infra实战11:模型部署Pipeline——CI/CD自动化
人工智能·ci/cd·ai·自动化·gitlab
一线数智15 分钟前
制造业的下一场竞争,不在减人,而在重新定义人与机器的关系
人工智能
网络研究院16 分钟前
告别野蛮生长!苹果 Apple Music 重拳监管 AI 音乐
人工智能·媒体·音乐·标注·歌曲·执行·监管
xlq2232217 分钟前
Ai大模型接入sdk day5
人工智能
Austin_YB20 分钟前
如何用 AI全程托管开发(Agent + SAP MCP 工具链 + 自定义技能)把功能说明书变成系统功能
人工智能
python零基础入门小白28 分钟前
LangGraph智能体实战:如何用Langfuse构建AI运行时全链路可观测系统?
人工智能·学习·ai·chatgpt·程序员·大模型·智能体