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

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

相关推荐
TechEdu2026061 分钟前
[人工智能]2026人工智能与大语言模型芯片家族参考V02:华为、Nvidia、AMD、Intel、Qualcomm
人工智能·ai
卷无止境1 分钟前
从终端里长出来的 IDE,oh-my-pi 到底是个什么东西
人工智能·后端
我滴老baby4 分钟前
工业物联网数据库选型:把计算能力放回第一维度
数据库·人工智能·架构·pdf
墨林陌8 分钟前
AI 热点日报(2026-09-18):华为昇腾960超节点发布,OpenAI 首次公开模型失准报告
人工智能
RisunJan8 分钟前
AI 每日要闻总结(2026-09-17)
人工智能
小lu飞10 分钟前
带 AI 问答的小程序选型:自建工作流与零代码生成平台的计费管理对比
人工智能
牧羊人.33316 分钟前
自然语言处理基础 01|语言转换与Word2Vec
人工智能·深度学习·自然语言处理
北城笑笑17 分钟前
Python Dev 02 & Tkinter webbrowser 实战,手写第一个 Python 桌面 GUI 小工具
人工智能·python·pip
CTA量化套保18 分钟前
搜索“2026年交易工具推荐”时,先问清它要解决什么问题
人工智能·python
代码方舟20 分钟前
零信任架构实战:基于天远车型识别精准构建自动化高并发收费站车辆审核网关
人工智能·ai·工具分享