2024-03-07 05:01:51 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cmp"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"slices"
|
2024-04-15 18:26:42 +00:00
|
|
|
"strings"
|
2024-03-07 05:01:51 +00:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
2024-03-26 20:04:17 +00:00
|
|
|
"github.com/ollama/ollama/convert/sentencepiece"
|
|
|
|
"github.com/ollama/ollama/llm"
|
2024-03-07 05:01:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Params struct {
|
2024-04-24 03:17:04 +00:00
|
|
|
Architectures []string `json:"architectures"`
|
|
|
|
VocabSize int `json:"vocab_size"`
|
|
|
|
HiddenSize int `json:"hidden_size"` // n_embd
|
|
|
|
HiddenLayers int `json:"num_hidden_layers"` // n_layer
|
|
|
|
ContextSize int `json:"max_position_embeddings"`
|
|
|
|
IntermediateSize int `json:"intermediate_size"`
|
|
|
|
AttentionHeads int `json:"num_attention_heads"` // n_head
|
|
|
|
KeyValHeads int `json:"num_key_value_heads"`
|
|
|
|
NormEPS float64 `json:"rms_norm_eps"`
|
|
|
|
BoSTokenID int `json:"bos_token_id"`
|
|
|
|
EoSTokenID int `json:"eos_token_id"`
|
|
|
|
HeadDimension int `json:"head_dim"`
|
|
|
|
PaddingTokenID int `json:"pad_token_id"`
|
|
|
|
RopeFrequencyBase float64 `json:"rope_theta"`
|
|
|
|
|
|
|
|
Experts int `json:"num_local_experts"`
|
|
|
|
ExpertsUsed int `json:"num_experts_per_tok"`
|
2024-03-29 01:54:01 +00:00
|
|
|
|
|
|
|
ByteOrder
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByteOrder interface {
|
|
|
|
binary.ByteOrder
|
|
|
|
binary.AppendByteOrder
|
2024-03-07 05:01:51 +00:00
|
|
|
}
|
|
|
|
|
2024-04-01 23:14:53 +00:00
|
|
|
type ModelArch interface {
|
|
|
|
GetTensors() error
|
|
|
|
LoadVocab() error
|
|
|
|
WriteGGUF() (string, error)
|
|
|
|
}
|
|
|
|
|
2024-04-15 18:26:42 +00:00
|
|
|
type ModelFormat interface {
|
|
|
|
GetLayerName(string) (string, error)
|
|
|
|
GetTensors(string, *Params) ([]llm.Tensor, error)
|
|
|
|
GetParams(string) (*Params, error)
|
|
|
|
GetModelArch(string, string, *Params) (ModelArch, error)
|
|
|
|
}
|
|
|
|
|
2024-04-01 23:14:53 +00:00
|
|
|
type ModelData struct {
|
|
|
|
Path string
|
|
|
|
Name string
|
|
|
|
Params *Params
|
|
|
|
Vocab *Vocab
|
|
|
|
Tensors []llm.Tensor
|
2024-04-15 18:26:42 +00:00
|
|
|
Format ModelFormat
|
2024-04-01 23:14:53 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 18:26:42 +00:00
|
|
|
func GetModelFormat(dirname string) (ModelFormat, error) {
|
|
|
|
files, err := filepath.Glob(filepath.Join(dirname, "*"))
|
2024-03-07 05:01:51 +00:00
|
|
|
if err != nil {
|
2024-03-29 01:54:01 +00:00
|
|
|
return nil, err
|
2024-03-07 05:01:51 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 18:26:42 +00:00
|
|
|
for _, fn := range files {
|
|
|
|
slog.Debug(fmt.Sprintf("file = %s", fn))
|
|
|
|
if strings.HasSuffix(fn, ".safetensors") {
|
|
|
|
return &SafetensorFormat{}, nil
|
|
|
|
} else if strings.HasSuffix(fn, ".bin") {
|
|
|
|
slog.Debug("model is torch")
|
|
|
|
return &TorchFormat{}, nil
|
2024-03-07 05:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 18:26:42 +00:00
|
|
|
return nil, fmt.Errorf("couldn't determine model format")
|
2024-03-07 05:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Details on gguf's tokenizer can be found at:
|
|
|
|
// https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#tokenizer
|
|
|
|
type Vocab struct {
|
|
|
|
Tokens []string
|
|
|
|
Scores []float32
|
|
|
|
Types []int32
|
|
|
|
}
|
|
|
|
|
2024-04-15 18:26:42 +00:00
|
|
|
func LoadSentencePieceTokens(dirpath string, params *Params) (*Vocab, error) {
|
2024-03-07 05:01:51 +00:00
|
|
|
slog.Info(fmt.Sprintf("reading vocab from %s", filepath.Join(dirpath, "tokenizer.model")))
|
|
|
|
in, err := os.ReadFile(filepath.Join(dirpath, "tokenizer.model"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// To regenerate sentencepiece from the protobufs use:
|
|
|
|
// protoc -I=./ --go_out=./ sentencepiece_model.proto
|
|
|
|
modelProto := &sentencepiece.ModelProto{}
|
|
|
|
if err := proto.Unmarshal(in, modelProto); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := &Vocab{
|
|
|
|
Tokens: make([]string, 0),
|
|
|
|
Scores: make([]float32, 0),
|
|
|
|
Types: make([]int32, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
pieces := modelProto.GetPieces()
|
|
|
|
for _, p := range pieces {
|
|
|
|
v.Tokens = append(v.Tokens, p.GetPiece())
|
|
|
|
v.Scores = append(v.Scores, p.GetScore())
|
|
|
|
t := p.GetType()
|
2024-03-29 01:54:01 +00:00
|
|
|
switch t {
|
|
|
|
case sentencepiece.ModelProto_SentencePiece_UNKNOWN:
|
|
|
|
case sentencepiece.ModelProto_SentencePiece_CONTROL:
|
|
|
|
case sentencepiece.ModelProto_SentencePiece_UNUSED:
|
|
|
|
case sentencepiece.ModelProto_SentencePiece_BYTE:
|
|
|
|
default:
|
|
|
|
t = sentencepiece.ModelProto_SentencePiece_NORMAL
|
|
|
|
}
|
2024-03-07 05:01:51 +00:00
|
|
|
v.Types = append(v.Types, int32(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Info(fmt.Sprintf("vocab size: %d", len(v.Tokens)))
|
|
|
|
|
|
|
|
// add any additional tokens
|
|
|
|
addIn, err := os.ReadFile(filepath.Join(dirpath, "added_tokens.json"))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return v, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Info("reading user defined tokens")
|
|
|
|
|
|
|
|
var extraTokenData map[string]int
|
|
|
|
if err := json.Unmarshal(addIn, &extraTokenData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type token struct {
|
|
|
|
key string
|
|
|
|
pos int
|
|
|
|
}
|
|
|
|
|
|
|
|
extraTokens := make([]token, 0)
|
|
|
|
for k, id := range extraTokenData {
|
|
|
|
extraTokens = append(extraTokens, token{k, id})
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.SortFunc(extraTokens, func(a, b token) int {
|
|
|
|
return cmp.Compare(a.pos, b.pos)
|
|
|
|
})
|
|
|
|
|
|
|
|
numToks := len(v.Tokens)
|
|
|
|
|
|
|
|
for cnt, t := range extraTokens {
|
|
|
|
// the token id should match the specific index for the total number of tokens
|
|
|
|
if t.pos != cnt+numToks {
|
|
|
|
return nil, fmt.Errorf("token ID '%d' for '%s' doesn't match total token size", t.pos, t.key)
|
|
|
|
}
|
|
|
|
v.Tokens = append(v.Tokens, t.key)
|
|
|
|
v.Scores = append(v.Scores, -1000.0)
|
|
|
|
v.Types = append(v.Types, int32(llm.GGUFTokenUserDefined))
|
|
|
|
}
|
|
|
|
slog.Info(fmt.Sprintf("vocab size w/ extra tokens: %d", len(v.Tokens)))
|
|
|
|
|
2024-04-15 18:26:42 +00:00
|
|
|
if params.VocabSize > len(v.Tokens) {
|
|
|
|
missingTokens := params.VocabSize - len(v.Tokens)
|
2024-03-29 01:54:01 +00:00
|
|
|
slog.Warn(fmt.Sprintf("vocab is missing %d tokens", missingTokens))
|
|
|
|
for cnt := 0; cnt < missingTokens; cnt++ {
|
|
|
|
v.Tokens = append(v.Tokens, fmt.Sprintf("<dummy%05d>", cnt+1))
|
|
|
|
v.Scores = append(v.Scores, -1)
|
|
|
|
v.Types = append(v.Types, int32(llm.GGUFTokenUserDefined))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 05:01:51 +00:00
|
|
|
return v, nil
|
|
|
|
}
|