You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用onnxruntime_go部署LLM模型时无法获取正确结果求助

使用onnxruntime_go部署Qwen-0.5B LLM模型时输出异常

问题背景

  • 采用onnxruntime_go库在生产环境部署Qwen-0.5B模型
  • Tokenizer功能验证正常,编码输出与Python版本完全一致
  • ONNX模型导出无误,可通过Netron正常查看结构
  • 运行Go代码后无法得到正确结果,返回异常的outputData

相关代码

package main

import (
    "fmt"
    "log"

    "github.com/whitezhang/tokenizer"
    "github.com/whitezhang/tokenizer/pretrained"
    ort "github.com/yalue/onnxruntime_go"
)

func IntSliceToInt64Slice(a []int) []int64 {
    b := make([]int64, len(a))
    for i, v := range a {
        b[i] = int64(v)
    }
    return b
}

func main() {
    // This line _may_ be optional; by default the library will try to load
    // "onnxruntime.dll" on Windows, and "onnxruntime.so" on any other system.
    // For stability, it is probably a good idea to always set this explicitly.
    ort.SetSharedLibraryPath("offline/lib/libonnxruntime.so")

    err := ort.InitializeEnvironment()
    if err != nil {
        panic(err)
    }
    defer ort.DestroyEnvironment()

    configFile, err := tokenizer.CachedPath("./", "tokenizer.json")
    fmt.Println(configFile)
    if err != nil {
        panic(err)
    }

    tk, err := pretrained.FromFile(configFile)
    if err != nil {
        panic(err)
    }

    sentence := `1+2=?`
    en, err := tk.EncodeSingle(sentence)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("tokens: %q\n", en.Tokens)
    fmt.Printf("ids: %v\n", en.Ids)
    fmt.Printf("attention_mask: %v\n", en.AttentionMask)
    fmt.Printf("offsets: %v\n", en.Offsets)
    idsInt64 := IntSliceToInt64Slice(en.Ids)
    amInt64 := IntSliceToInt64Slice(en.AttentionMask)

    inputTensor, err := ort.NewTensor(ort.NewShape(int64(len(en.Ids))), idsInt64)
    defer inputTensor.Destroy()
    attentionMaskTensor, err := ort.NewTensor(ort.NewShape(int64(len(amInt64))), amInt64)
    defer attentionMaskTensor.Destroy()

    outputShape := ort.NewShape(768)
    outputTensor, err := ort.NewEmptyTensor[float32](outputShape)
    defer outputTensor.Destroy()

    session, err := ort.NewAdvancedSession("tools/model/data/qwen_0.5B",
        []string{"input"}, []string{"output"},
        []ort.Value{inputTensor}, []ort.Value{outputTensor}, nil)
    defer session.Destroy()
    fmt.Println(err)

    // Calling Run() will run the network, reading the current contents of the
    // input tensors and modifying the contents of the output tensors.
    err = session.Run()

    // Get a slice view of the output tensor's data.
    outputData := outputTensor.GetData()
    fmt.Println(outputData)

}

异常现象

运行代码后输出的outputData为大量无意义的float32数值,并非模型预期生成的token预测结果(输出内容为一串杂乱的浮点数值)

内容的提问来源于stack exchange,提问作者Wyatt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:40:05