golang实现Huggingface transformer pipelines的ONNX运行时插件hugot的使用

Golang实现Huggingface Transformer Pipelines的ONNX运行时插件Hugot的使用

Hugot是一个用于在Go应用程序中运行Transformer Pipelines推理和训练的库,兼容Hugging Face的Transformer模型,并支持ONNX格式。

什么是Hugot

Hugot提供了一种简单、可扩展且无麻烦的方式在Go应用程序中运行Transformer Pipelines,如Hugging Face的Transformers Pipelines。它支持以下功能:

  • 嵌入向量
  • 文本分类
  • 命名实体识别
  • 微调等AI用例

主要特点

  1. Hugging Face兼容性:使用Python Hugging Face Transformer库训练和测试的模型可以导出为ONNX格式,并通过Hugot Pipelines获得与Python版本相同的预测结果
  2. 高性能生产使用:仅支持ONNX模型
  3. 在自有硬件上运行:无需调用REST API或设置Python RPC服务
  4. 简单性:Hugot API允许轻松部署Pipelines而无需编写自己的推理或训练代码

安装和使用

作为库使用

import (
	"github.com/knights-analytics/hugot"
	"github.com/knights-analytics/hugot/pipelines"
)

func check(err error) {
    if err != nil {
        panic(err.Error())
    }
}

func main() {
    // 启动一个新会话
    session, err := hugot.NewGoSession()
	// 使用XLA后端(需要构建标签"XLA"或"ALL"):
	// session, err := NewXLASession()
	// 使用ORT后端(需要构建标签"ORT"或"ALL"):
	// session, err := NewORTSession()
	// 默认在/usr/lib/onnxruntime.so查找onnxruntime.so库
    // 如果onnxruntime.so在其他位置,可以使用WithOnnxLibraryPath明确指定
    // session, err := hugot.NewORTSession(WithOnnxLibraryPath("/path/to/onnxruntime.so"))
	check(err)
	
    // 成功创建的hugot会话在使用完毕后需要销毁
    defer func (session *hugot.Session) {
    err := session.Destroy()
    check(err)
    }(session)

    // 下载一个ONNX情感测试分类模型到当前目录
    // 注意:如果使用NODOWNLOAD构建标签编译库,将排除下载器
    modelPath, err := hugot.DownloadModel("KnightsAnalytics/distilbert-base-uncased-finetuned-sst-2-english", "./models/", hugot.NewDownloadOptions())
    check(err)

    // 创建文本分类Pipeline的配置
    config := TextClassificationConfig{
    ModelPath: modelPath,
    Name:      "testPipeline",
    }
    // 创建Pipeline
    // 注意:Pipeline也会添加到会话对象中,以便一次性销毁所有Pipeline
    sentimentPipeline, err := NewPipeline(session, config)
    check(err)

    // 现在可以在字符串批次上使用Pipeline进行预测
    batch := []string{"This movie is disgustingly good !", "The director tried too much"}
    batchResult, err := sentimentPipeline.RunPipeline(batch)
    check(err)

    // 处理结果
    s, err := json.Marshal(batchResult)
    check(err)
    fmt.Println(string(s))
}
// 输出: {"ClassificationOutputs":[[{"Label":"POSITIVE","Score":0.9998536}],[{"Label":"NEGATIVE","Score":0.99752176}]]}

作为CLI使用

安装Hugot CLI:

curl https://raw.githubusercontent.com/knights-analytics/hugot/main/scripts/install-hugot-cli.sh | bash

运行模型:

hugot run --model=KnightsAnalytics/distilbert-base-uncased-finetuned-sst-2-english --input=/path/to/input.jsonl --output=/path/to/folder/output --type=textClassification

输入文件格式示例:

{"input": "The director tried too much"}
{"input": "The film was excellent"}

硬件加速

Hugot支持以下加速器后端:

  • CUDA(在ONNX Runtime和OpenXLA上测试)
  • TensorRT(未测试,仅在ONNX Runtime中可用)
  • DirectML(未测试,仅在ONNX Runtime中可用)
  • CoreML(未测试,仅在ONNX Runtime中可用)
  • OpenVINO(未测试,仅在ONNX Runtime中可用)

CUDA配置示例

opts := []options.WithOption{
  options.WithOnnxLibraryPath("/usr/lib64/onnxruntime-gpu/libonnxruntime.so"),
  options.WithCuda(map[string]string{
    "device_id": "0",
  }),
}
session, err := NewORTSession(opts...)

训练和微调Pipeline

Hugot支持Transformer Pipeline的训练和微调(beta版),目前仅支持FeatureExtractionPipeline。

训练数据集格式示例:

{"sentence1": "The quick brown fox jumps over the lazy dog", "sentence2": "A quick brown fox jumps over a lazy dog", "score": 1}
{"sentence1": "The quick brown fox jumps over the lazy dog", "sentence2": "A quick brown cow jumps over a lazy caterpillar", "score": 0.5}

性能调优

对于最大吞吐量,建议:

  1. 从多个goroutine(每个核心1个)调用单个共享的Hugot Pipeline
  2. 使用通道传递输入数据
  3. 使用以下设置:
session, err := hugot.NewORTSession(
	hugot.WithInterOpNumThreads(1),
	hugot.WithIntraOpNumThreads(1),
	hugot.WithCpuMemArena(false),
	hugot.WithMemPattern(false),
)

文件系统

Hugot使用抽象文件系统,支持各种操作系统文件系统。要使用S3等对象存储,请导入适当的afsc插件:

import _ "github.com/viant/afsc/s3"

当前支持的Pipeline

  • featureExtraction
  • textClassification
  • tokenClassification
  • zeroShotClassification

局限性

  • 目前仅在amd64-linux上构建/测试
  • 仅支持上述Pipeline

Hugot是Knights Analytics团队开发的产品,用于自动化AI驱动的数据管理。


更多关于golang实现Huggingface transformer pipelines的ONNX运行时插件hugot的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang实现Huggingface transformer pipelines的ONNX运行时插件hugot的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用hugot实现Huggingface Transformers的ONNX运行时插件

hugot是一个为Huggingface Transformers设计的ONNX运行时插件,它允许你将预训练的Transformer模型转换为ONNX格式并在Go环境中高效运行。下面我将介绍如何使用hugot在Go中实现这一功能。

安装hugot

首先,你需要安装hugot库:

go get github.com/neurosnap/hugot

基本使用示例

1. 加载ONNX模型

package main

import (
	"fmt"
	"log"

	"github.com/neurosnap/hugot"
	"github.com/neurosnap/hugot/pipelines"
)

func main() {
	// 初始化hugot环境
	hugot.Init()

	// 加载ONNX模型
	modelPath := "path/to/your/model.onnx"
	tokenizerPath := "path/to/your/tokenizer.json"
	
	// 创建文本分类管道
	classifier, err := pipelines.NewTextClassification(modelPath, tokenizerPath)
	if err != nil {
		log.Fatalf("Failed to create classifier: %v", err)
	}
	defer classifier.Close()

	// 准备输入文本
	text := "I love using Huggingface transformers with Go!"

	// 进行分类预测
	result, err := classifier.Predict(text)
	if err != nil {
		log.Fatalf("Prediction failed: %v", err)
	}

	// 输出结果
	fmt.Printf("Input: %s\n", text)
	fmt.Printf("Predicted label: %s (confidence: %.2f)\n", 
		result.Label, result.Score)
}

2. 文本生成示例

func textGenerationExample() {
	// 加载文本生成模型
	modelPath := "path/to/your/generation_model.onnx"
	tokenizerPath := "path/to/your/generation_tokenizer.json"
	
	generator, err := pipelines.NewTextGeneration(modelPath, tokenizerPath)
	if err != nil {
		log.Fatalf("Failed to create generator: %v", err)
	}
	defer generator.Close()

	// 输入提示
	prompt := "The future of AI is"
	
	// 生成文本
	result, err := generator.Generate(prompt, 
		pipelines.WithMaxLength(50),
		pipelines.WithTemperature(0.7))
	if err != nil {
		log.Fatalf("Generation failed: %v", err)
	}

	fmt.Printf("Prompt: %s\n", prompt)
	fmt.Printf("Generated text: %s\n", result.Text)
}

高级功能

1. 自定义预处理和后处理

func customPipelineExample() {
	// 创建自定义管道
	pipe := &pipelines.Pipeline{
		Preprocess: func(text string) (interface{}, error) {
			// 自定义预处理逻辑
			return map[string]interface{}{
				"input_ids": []int32{1, 2, 3, 4}, // 示例ID
				"attention_mask": []int32{1, 1, 1, 1},
			}, nil
		},
		Postprocess: func(output interface{}) (interface{}, error) {
			// 自定义后处理逻辑
			return "custom result", nil
		},
	}

	// 加载ONNX模型
	model, err := hugot.NewONNXModel("path/to/model.onnx")
	if err != nil {
		log.Fatal(err)
	}
	defer model.Close()

	// 运行管道
	result, err := pipe.Run("input text", model)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)
}

2. 批处理支持

func batchProcessingExample() {
	classifier, err := pipelines.NewTextClassification(
		"path/to/model.onnx",
		"path/to/tokenizer.json",
	)
	if err != nil {
		log.Fatal(err)
	}
	defer classifier.Close()

	// 批处理输入
	texts := []string{
		"This is a positive sentence.",
		"I really dislike this product.",
		"The weather is nice today.",
	}

	// 批量预测
	results, err := classifier.BatchPredict(texts)
	if err != nil {
		log.Fatal(err)
	}

	for i, result := range results {
		fmt.Printf("Text %d: %s\n", i+1, texts[i])
		fmt.Printf("  Label: %s, Score: %.2f\n", result.Label, result.Score)
	}
}

性能优化技巧

  1. 启用并行计算
// 在初始化时设置并行度
hugot.Init(hugot.WithNumThreads(4))
  1. 使用GPU加速(如果可用):
// 检查GPU可用性
if hugot.HasGPU() {
    hugot.Init(hugot.WithGPU())
}
  1. 模型量化
// 加载量化模型
model, err := hugot.NewONNXModel("path/to/quantized_model.onnx")

注意事项

  1. 在使用hugot前,你需要先将Huggingface模型转换为ONNX格式。可以使用Python的transformers.onnx包完成这一转换。

  2. ONNX模型和tokenizer文件需要与Go程序一起部署。

  3. 由于Go的ONNX运行时限制,某些高级的PyTorch/TensorFlow操作可能无法完全支持。

  4. 对于大型模型,确保你的系统有足够的内存。

hugot为Go开发者提供了一个高效的方式来在生产环境中部署Huggingface模型,特别是在需要高性能和低延迟的场景下。通过ONNX运行时,你可以获得接近原生Python实现的性能,同时享受Go语言的部署便利性。

回到顶部