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

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

相关推荐
大模型任我行6 分钟前
腾讯:预测散度掩码提升LLM强化学习
人工智能·语言模型·自然语言处理·论文笔记
程序员-李俞7 分钟前
向量引擎接入自研 API 中转网关:鉴权、限流、熔断和审计日志复盘
服务器·人工智能·大模型·api·ai编程·ai api
雪碧聊技术8 分钟前
软件定义三维近存AI芯片发布——国产算力走出“不依赖先进制程”的独特路线
人工智能
夜瞬9 分钟前
内生可解释性:从黑盒深度模型到可理解、可干预的智能系统
人工智能·python
额恩669 分钟前
阶段一:Vue 2 单页应用基础
人工智能·深度学习·机器学习
大模型丫丫12 分钟前
Skill-Agent 如何实践:从概念到落地的完整指南
java·大数据·人工智能
工业HMI实战笔记12 分钟前
【拯救HMI】:边缘计算在工业自动化中的落地:低延迟控制的实现路径
人工智能·自动化·边缘计算
Ivanqhz18 分钟前
预训练 Embedding + 轻量级线上模型
人工智能·机器学习·embedding
问商十三载20 分钟前
2026大模型GEO优化体系:3层链路提收录,零成本提34%引用率附工具包
大数据·前端·人工智能
小码哥哥20 分钟前
企业AI知识库本地部署的安全真相
人工智能·安全