-
Notifications
You must be signed in to change notification settings - Fork 4
/
filename.go
176 lines (156 loc) · 4.57 KB
/
filename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gguf_parser
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/gpustack/gguf-parser-go/util/funcx"
"github.com/gpustack/gguf-parser-go/util/ptr"
)
// GGUFFilename represents a GGUF filename,
// see https://github.com/ggerganov/ggml/blob/master/docs/gguf.md#gguf-naming-convention.
type GGUFFilename struct {
BaseName string `json:"baseName"`
SizeLabel string `json:"sizeLabel"`
FineTune string `json:"fineTune"`
Version string `json:"version"`
Encoding string `json:"encoding"`
Type string `json:"type"`
Shard *int `json:"shard,omitempty"`
ShardTotal *int `json:"shardTotal,omitempty"`
}
var GGUFFilenameRegex = regexp.MustCompile(`^(?P<BaseName>[A-Za-z\s][A-Za-z0-9._\s]*(?:(?:-(?:(?:[A-Za-z\s][A-Za-z0-9._\s]*)|(?:[0-9._\s]*)))*))-(?:(?P<SizeLabel>(?:\d+x)?(?:\d+\.)?\d+[A-Za-z](?:-[A-Za-z]+(\d+\.)?\d+[A-Za-z]+)?)(?:-(?P<FineTune>[A-Za-z][A-Za-z0-9\s_-]+[A-Za-z](?i:[^BFKIQ])))?)?(?:-(?P<Version>[vV]\d+(?:\.\d+)*))?(?i:-(?P<Encoding>(BF16|F32|F16|([KI]?Q[0-9][A-Z0-9_]*))))?(?:-(?P<Type>LoRA|vocab))?(?:-(?P<Shard>\d{5})-of-(?P<ShardTotal>\d{5}))?\.gguf$`) // nolint:lll
// ParseGGUFFilename parses the given GGUF filename string,
// and returns the GGUFFilename, or nil if the filename is invalid.
func ParseGGUFFilename(name string) *GGUFFilename {
n := name
if !strings.HasSuffix(n, ".gguf") {
n += ".gguf"
}
m := make(map[string]string)
{
r := GGUFFilenameRegex.FindStringSubmatch(n)
for i, ne := range GGUFFilenameRegex.SubexpNames() {
if i != 0 && i <= len(r) {
m[ne] = r[i]
}
}
}
if m["BaseName"] == "" {
return nil
}
var gn GGUFFilename
gn.BaseName = strings.ReplaceAll(m["BaseName"], "-", " ")
gn.SizeLabel = m["SizeLabel"]
gn.FineTune = m["FineTune"]
gn.Version = m["Version"]
gn.Encoding = m["Encoding"]
gn.Type = m["Type"]
if v := m["Shard"]; v != "" {
gn.Shard = ptr.To(parseInt(v))
}
if v := m["ShardTotal"]; v != "" {
gn.ShardTotal = ptr.To(parseInt(v))
}
return &gn
}
func (gn GGUFFilename) String() string {
if gn.BaseName == "" {
return ""
}
var sb strings.Builder
sb.WriteString(strings.ReplaceAll(gn.BaseName, " ", "-"))
if gn.SizeLabel != "" {
sb.WriteString("-")
sb.WriteString(gn.SizeLabel)
}
if gn.FineTune != "" {
sb.WriteString("-")
sb.WriteString(gn.FineTune)
}
if gn.Version != "" {
sb.WriteString("-")
sb.WriteString(gn.Version)
}
if gn.Encoding != "" {
sb.WriteString("-")
sb.WriteString(gn.Encoding)
}
if gn.Type != "" {
sb.WriteString("-")
sb.WriteString(gn.Type)
}
if m, n := ptr.Deref(gn.Shard, 0), ptr.Deref(gn.ShardTotal, 0); m > 0 && n > 0 {
sb.WriteString("-")
sb.WriteString(fmt.Sprintf("%05d", m))
sb.WriteString("-of-")
sb.WriteString(fmt.Sprintf("%05d", n))
}
sb.WriteString(".gguf")
return sb.String()
}
// IsShard returns true if the GGUF filename is a shard.
func (gn GGUFFilename) IsShard() bool {
return ptr.Deref(gn.Shard, 0) > 0 && ptr.Deref(gn.ShardTotal, 0) > 0
}
var ShardGGUFFilenameRegex = regexp.MustCompile(`^(?P<Prefix>.*)-(?:(?P<Shard>\d{5})-of-(?P<ShardTotal>\d{5}))\.gguf$`)
// IsShardGGUFFilename returns true if the given filename is a shard GGUF filename.
func IsShardGGUFFilename(name string) bool {
n := name
if !strings.HasSuffix(n, ".gguf") {
n += ".gguf"
}
m := make(map[string]string)
{
r := ShardGGUFFilenameRegex.FindStringSubmatch(n)
for i, ne := range ShardGGUFFilenameRegex.SubexpNames() {
if i != 0 && i <= len(r) {
m[ne] = r[i]
}
}
}
var shard, shardTotal int
if v := m["Shard"]; v != "" {
shard = parseInt(v)
}
if v := m["ShardTotal"]; v != "" {
shardTotal = parseInt(v)
}
return shard > 0 && shardTotal > 0
}
// CompleteShardGGUFFilename returns the list of shard GGUF filenames that are related to the given shard GGUF filename.
//
// Only available if the given filename is a shard GGUF filename.
func CompleteShardGGUFFilename(name string) []string {
n := name
if !strings.HasSuffix(n, ".gguf") {
n += ".gguf"
}
m := make(map[string]string)
{
r := ShardGGUFFilenameRegex.FindStringSubmatch(n)
for i, ne := range ShardGGUFFilenameRegex.SubexpNames() {
if i != 0 && i <= len(r) {
m[ne] = r[i]
}
}
}
var shard, shardTotal int
if v := m["Shard"]; v != "" {
shard = parseInt(v)
}
if v := m["ShardTotal"]; v != "" {
shardTotal = parseInt(v)
}
if shard <= 0 || shardTotal <= 0 {
return nil
}
names := make([]string, 0, shardTotal)
for i := 1; i <= shardTotal; i++ {
names = append(names, fmt.Sprintf("%s-%05d-of-%05d.gguf", m["Prefix"], i, shardTotal))
}
return names
}
func parseInt(v string) int {
return int(funcx.MustNoError(strconv.ParseInt(v, 10, 64)))
}