Debian Codegraph @

vertexai/internal/sentencepiece/README.md

raw
1 # go-sentencepiece
2
3 <p align="center">
4 <img alt="Logo" src="doc/toklogo2.png" />
5 </p>
6
7 ----
8
9 [![Go Reference](https://pkg.go.dev/badge/github.com/eliben/go-sentencepiece.svg)](https://pkg.go.dev/github.com/eliben/go-sentencepiece)
10
11 This is a pure Go implementation of encoding and decoding text with
12 the [SentencePiece tokenizer](https://github.com/google/sentencepiece).
13
14 "Encoding" is the operation used to split text into tokens, using
15 a trained tokenizer model. "Decoding" is the reverse process - converting
16 a list of tokens into the original text.
17
18 SentencePiece is a general family of tokenizers that is configured
19 by a protobuf configuration file. This repository currently focuses
20 on implementing just the functionality required to reproduce the
21 tokenization of [Gemma models](https://ai.google.dev/gemma) (the same
22 tokenizer is used for Google's proprietary Gemini family of models).
23 Specifically, it only implements BPE tokenization since this is what
24 Gemma uses.
25
26 ## Current status
27
28 This package should be ready to use for encoding text into tokens
29 using the Gemma tokenizer; it's been reasonably optimized and extensively
30 tested vs. the [SentencePiece Python bindings](https://pypi.org/project/sentencepiece/)
31 (see `system_test.go` in this repository).
32
33 If you find any problems or discrepancies, please open an issue.
34
35 ## Tokenizer configuration
36
37 The configuration file for the tokenizer is a protobuf (structured
38 data, serialized in the [protocol buffer format](https://protobuf.dev/))
39 that describes a trained tokenizer model; it includes
40 the complete learned vocabulary used for tokenization, as well as
41 other configuration information.
42
43 It is not part of this repository. Please fetch it from the
44 [official Gemma implementation repository](https://github.com/google/gemma_pytorch/tree/main/tokenizer).
45 `NewProcessor*` constructors will expect to read this file.
46
47 ## Developing
48
49 A protobuf is used to configure the tokenizer. The structure of the
50 protobuf is described by the `internal/model/sentencepiece_model.proto` file,
51 which is vendored from https://github.com/google/sentencepiece
52
53 To re-generate the `*.pb.go` file from it:
54
55 ```
56 $ cd internal/model
57 $ ./gen.sh
58 ```
59
60 The configuration protobuf itself is obtained as described in the
61 [Tokenizer configuration](#tokenizer-configuration) section. All
62 tests require the `MODELPATH` env var to point to a local
63 copy of the tokenizer configuration file.
64
65 ## Online demo
66
67 To see an in-browser demo of this tokenizer in action, visit
68 https://eliben.github.io/go-sentencepiece/
69
70 The Go code is compiled to WebAssembly and loaded from a small
71 JS program to allow interactive encoding of text.