自定义 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)

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

相关推荐
szxinmai主板定制专家几秒前
ZYNQ PL+PS 架构拆解|工业自动化与电力物联网高速数据采集、预处理与协议上云
arm开发·人工智能·物联网·fpga开发·数据采集卡·rk3576
禁默2 分钟前
从“动作社区“到“技能商店“:机器人应用生态走到哪一步了
人工智能·机器人
话梅姐姐呀2 分钟前
IOT -- 设备入网与注册
大数据·网络·人工智能·物联网·iot
roman_日积跬步-终至千里3 分钟前
【数据Agent论文·表格事实与生成|15】LogicNLG:数值抄对了,逻辑结论就对了吗?
人工智能·深度学习·自然语言处理·数据分析
夜瞬4 分钟前
从零微调 Qwen3-8B:基于 LoRA 与 LLaMA-Factory 打造专属 AI 编程助教
人工智能·python·llama
碧海银沙音频科技研究院5 分钟前
面向长尾音频分类的知识蒸馏研究
人工智能·算法·音视频
zhilai17 分钟前
2026 企业 AI 办公工具选型指南:框架、全景与落地路径
人工智能
IT大白鼠8 分钟前
AI SSH 运维助手 V13.0内部版七项私有新特性全解
运维·人工智能·ssh
Dawson Zhu15 分钟前
大模型拒答率与错误率平衡术:五步构建可落地的安全与体验最优解
人工智能·语言模型·架构·aigc·agi
nap-joker16 分钟前
脑衰老的多模态特征:从描述性分析到基于机器学习的整合
人工智能·机器学习·多模态