拥抱脸
v0.1.0
用于下载( hub
)、标记化( tokenizers
)和(未来工作)HuggingFace 模型转换( models
)的简单 API?使用 GoMLX 的模型。
实验和开发中:虽然hub
包一直稳定, tokenizers
和未来models
仍在紧张开发中。
import ("github.com/gomlx/go-huggingface/hub""github.com/gomlx/go-huggingface/tokenizers")var ( // 用于测试的模型 ID.hfModelIDs = []string{ "google/gemma-2 -2b-it", "句子变压器/all-MiniLM-L6-v2", “protectai/deberta-v3-base-zeroshot-v1-onnx”、“KnightsAnalytics/distilbert-base-uncased-finetuned-sst-2-english”、“KnightsAnalytics/distilbert-NER”、“SamLowe/roberta-base-go_emotions” -onnx”, } hfAuthToken = os.Getenv("HF_TOKEN") // 在 Huggingface.co 中创建 HuggingFace 身份验证令牌,以允许下载模型。)
for _, modelID := range hfModelIDs { fmt.Printf("n%s:n", modelID) repo := hub.New(modelID).WithAuth(hfAuthToken) for fileName, err := range repo.IterFileNames() { if err != nil { 恐慌(err) } fmt.Printf("t%sn", fileName) } }
for _, modelID := range hfModelIDs { fmt.Printf("n%s:n", modelID) repo := hub.New(modelID).WithAuth(hfAuthToken) config, err := tokenizers.GetConfig(repo) if err != nil { 恐慌(err) } fmt.Printf("ttokenizer_class=%sn", config.TokenizerClass) }
google/gemma-2-2b-it
进行标记化仅在标记生成器文件尚未缓存时才会输出“已下载”消息,因此仅在第一次:
repo := hub.New("google/gemma-2-2b-it").WithAuth(hfAuthToken)tokenizer, err := tokenizers.New(repo)if err != nil {panic(err) }句子 := "书在桌子上。"tokens := tokenizer.Encode(sentence)fmt.Printf("句子:t%sn",句子)fmt.Printf("令牌:t%vn", tokens)
Downloaded 1/1 files, 4.2 MB downloaded Sentence: The book is on the table. Tokens: [651 2870 603 611 573 3037 235265]
sentence-transformers/all-MiniLM-L6-v2
ONNX 模型只有前 3 行实际上是在演示go-huggingface
。其余行使用github.com/gomlx/onnx-gomlx
解析 ONNX 模型并将其转换为 GoMLX,然后github.com/gomlx/gomlx
执行几个句子的转换后的模型。
// 获取 ONNX model.repo := hub.New("sentence-transformers/all-MiniLM-L6-v2").WithAuth(hfAuthToken)onnxFilePath, err := repo.DownloadFile("onnx/model.onnx")if err != nil { 恐慌(err) }onnxModel, err := onnx.ReadFile(onnxFilePath)if err != nil { panic(err) }// 将 ONNX 变量转换为 GoMLX 上下文(存储变量):ctx := context.New()err = onnxModel.VariablesToContext(ctx)if err ! = nil { panic(err) }// 测试 input.sentences := []string{ "这是一个例句", "每句话都进行转换"}inputIDs := [][]int64{ {101、2023、2003、2019、2742、6251、102}、 { 101, 2169, 6251, 2003, 4991, 102, 0}}tokenTypeID := [][]int64{ {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}}attentionMask := [][]int64{ {1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 0}}// 使用 model.embeddings 执行 GoMLX 图 := context.ExecOnce( backends.New(), ctx, func (ctx *context.Context, inputs [] *graph.Node) *graph.Node { modelOutputs := onnxModel.CallGraph(ctx, input[0].Graph(), map[string]*graph.Node{ "input_ids": 输入[0], "attention_mask": 输入[1], "token_type_ids": 输入[2]}) 返回 modelOutputs[0] }, inputIDs,attentionMask,tokenTypeIDs)fmt.Printf(“句子:t%qn”,句子)fmt.Printf(“嵌入:t%sn”,嵌入)
Sentences: ["This is an example sentence" "Each sentence is converted"] Embeddings: [2][7][384]float32{ {{0.0366, -0.0162, 0.1682, ..., 0.0554, -0.1644, -0.2967}, {0.7239, 0.6399, 0.1888, ..., 0.5946, 0.6206, 0.4897}, {0.0064, 0.0203, 0.0448, ..., 0.3464, 1.3170, -0.1670}, ..., {0.1479, -0.0643, 0.1457, ..., 0.8837, -0.3316, 0.2975}, {0.5212, 0.6563, 0.5607, ..., -0.0399, 0.0412, -1.4036}, {1.0824, 0.7140, 0.3986, ..., -0.2301, 0.3243, -1.0313}}, {{0.2802, 0.1165, -0.0418, ..., 0.2711, -0.1685, -0.2961}, {0.8729, 0.4545, -0.1091, ..., 0.1365, 0.4580, -0.2042}, {0.4752, 0.5731, 0.6304, ..., 0.6526, 0.5612, -1.3268}, ..., {0.6113, 0.7920, -0.4685, ..., 0.0854, 1.0592, -0.2983}, {0.4115, 1.0946, 0.2385, ..., 0.8984, 0.3684, -0.7333}, {0.1374, 0.5555, 0.2678, ..., 0.5426, 0.4665, -0.5284}}}