tokenizers

package module
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: MIT Imports: 3 Imported by: 13

README

Tokenizers

Go bindings for the HuggingFace Tokenizers library.

Installation

make build to build libtokenizers.a that you need to run your application that uses bindings.

Using pre-built binaries

Build your Go application using pre-built native binaries: docker build --platform=linux/amd64 -f example/Dockerfile .

Available binaries:

Getting started

TLDR: working example.

Load a tokenizer from a JSON config:

import "github.com/daulet/tokenizers"

tk, err := tokenizers.FromFile("./data/bert-base-uncased.json")
if err != nil {
    return err
}
// release native resources
defer tk.Close()

Encode text and decode tokens:

fmt.Println("Vocab size:", tk.VocabSize())
// Vocab size: 30522
fmt.Println(tk.Encode("brown fox jumps over the lazy dog", false))
// [2829 4419 14523 2058 1996 13971 3899] [brown fox jumps over the lazy dog]
fmt.Println(tk.Encode("brown fox jumps over the lazy dog", true))
// [101 2829 4419 14523 2058 1996 13971 3899 102] [[CLS] brown fox jumps over the lazy dog [SEP]]
fmt.Println(tk.Decode([]uint32{2829, 4419, 14523, 2058, 1996, 13971, 3899}, true))
// brown fox jumps over the lazy dog

Benchmarks

go test . -bench=. -benchmem -benchtime=10s

goos: darwin
goarch: arm64
pkg: github.com/daulet/tokenizers
BenchmarkEncodeNTimes-10     	  996556	     11851 ns/op	     116 B/op	       6 allocs/op
BenchmarkEncodeNChars-10      1000000000	     2.446 ns/op	       0 B/op	       0 allocs/op
BenchmarkDecodeNTimes-10     	 7286056	      1657 ns/op	     112 B/op	       4 allocs/op
BenchmarkDecodeNTokens-10    	65191378	     211.0 ns/op	       7 B/op	       0 allocs/op
PASS
ok  	github.com/daulet/tokenizers	126.681s

Contributing

Please refer to CONTRIBUTING.md for information on how to contribute a PR to this project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EncodeOption added in v0.7.0

type EncodeOption func(eo *EncodeOptions)

func WithReturnAllAttributes added in v0.7.0

func WithReturnAllAttributes() EncodeOption

func WithReturnAttentionMask added in v0.7.0

func WithReturnAttentionMask() EncodeOption

func WithReturnSpecialTokensMask added in v0.7.0

func WithReturnSpecialTokensMask() EncodeOption

func WithReturnTokens added in v0.7.0

func WithReturnTokens() EncodeOption

func WithReturnTypeIDs added in v0.7.0

func WithReturnTypeIDs() EncodeOption

type EncodeOptions added in v0.7.0

type EncodeOptions struct {
	AddSpecialTokens C.bool

	ReturnTypeIDs           C.bool
	ReturnTokens            C.bool
	ReturnSpecialTokensMask C.bool
	ReturnAttentionMask     C.bool
}

type Encoding added in v0.7.0

type Encoding struct {
	IDs               []uint32
	TypeIDs           []uint32
	SpecialTokensMask []uint32
	AttentionMask     []uint32
	Tokens            []string
}

type Tokenizer

type Tokenizer struct {
	// contains filtered or unexported fields
}

func FromBytes added in v0.2.0

func FromBytes(data []byte) (*Tokenizer, error)

func FromBytesWithTruncation added in v0.4.0

func FromBytesWithTruncation(data []byte, maxLen uint32, dir TruncationDirection) (*Tokenizer, error)

func FromFile

func FromFile(path string) (*Tokenizer, error)

func (*Tokenizer) Close

func (t *Tokenizer) Close() error

func (*Tokenizer) Decode

func (t *Tokenizer) Decode(tokenIDs []uint32, skipSpecialTokens bool) string

func (*Tokenizer) Encode

func (t *Tokenizer) Encode(str string, addSpecialTokens bool) ([]uint32, []string)

func (*Tokenizer) EncodeWithOptions added in v0.7.0

func (t *Tokenizer) EncodeWithOptions(str string, addSpecialTokens bool, opts ...EncodeOption) Encoding

func (*Tokenizer) VocabSize

func (t *Tokenizer) VocabSize() uint32

type TruncationDirection added in v0.4.0

type TruncationDirection int
const (
	TruncationDirectionLeft TruncationDirection = iota
	TruncationDirectionRight
)

Directories

Path Synopsis
example module
release module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL