-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathparse.go
More file actions
189 lines (168 loc) · 5.18 KB
/
parse.go
File metadata and controls
189 lines (168 loc) · 5.18 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
package bundle
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/docker/model-runner/pkg/distribution/modelpack"
"github.com/docker/model-runner/pkg/distribution/types"
)
// errFoundModelFile is a sentinel error used to stop filepath.Walk early after
// finding the first matching model file.
var errFoundModelFile = fmt.Errorf("found model file")
// Parse returns the Bundle at the given rootDir
func Parse(rootDir string) (*Bundle, error) {
if fi, err := os.Stat(rootDir); err != nil || !fi.IsDir() {
return nil, fmt.Errorf("inspect bundle root dir: %w", err)
}
// Check if model subdirectory exists - required for new bundle format
// If it doesn't exist, this is an old bundle format that needs to be recreated
modelDir := filepath.Join(rootDir, ModelSubdir)
if _, err := os.Stat(modelDir); os.IsNotExist(err) {
return nil, fmt.Errorf("bundle uses old format (missing %s subdirectory), will be recreated", ModelSubdir)
}
ggufPath, err := findGGUFFile(modelDir)
if err != nil {
return nil, err
}
safetensorsPath, err := findSafetensorsFile(modelDir)
if err != nil {
return nil, err
}
ddufPath, err := findDDUFFile(modelDir)
if err != nil {
return nil, err
}
// Ensure at least one model weight format is present
if ggufPath == "" && safetensorsPath == "" && ddufPath == "" {
return nil, fmt.Errorf("no supported model weights found (neither GGUF, safetensors, nor DDUF)")
}
mmprojPath, err := findMultiModalProjectorFile(modelDir)
if err != nil {
return nil, err
}
templatePath, err := findChatTemplateFile(modelDir)
if err != nil {
return nil, err
}
// Runtime config stays at bundle root
cfg, err := parseRuntimeConfig(rootDir)
if err != nil {
return nil, err
}
return &Bundle{
dir: rootDir,
mmprojPath: mmprojPath,
ggufFile: ggufPath,
safetensorsFile: safetensorsPath,
ddufFile: ddufPath,
runtimeConfig: cfg,
chatTemplatePath: templatePath,
}, nil
}
// parseRuntimeConfig parses the runtime config from the bundle.
// Natively supports both Docker format and ModelPack format without conversion.
func parseRuntimeConfig(rootDir string) (types.ModelConfig, error) {
raw, err := os.ReadFile(filepath.Join(rootDir, "config.json"))
if err != nil {
return nil, fmt.Errorf("read runtime config: %w", err)
}
// Detect and parse based on format
if modelpack.IsModelPackConfig(raw) {
var mp modelpack.Model
if err := json.Unmarshal(raw, &mp); err != nil {
return nil, fmt.Errorf("decode ModelPack runtime config: %w", err)
}
return &mp, nil
}
// Docker format
var cfg types.Config
if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, fmt.Errorf("decode Docker runtime config: %w", err)
}
return &cfg, nil
}
// findModelFile finds a supported model file by extension. It prefers a
// top-level match in modelDir and falls back to a recursive search when needed.
// Hidden files are ignored.
func findModelFile(modelDir, ext string) (string, error) {
pattern := filepath.Join(modelDir, "[^.]*"+ext)
paths, err := filepath.Glob(pattern)
if err != nil {
return "", fmt.Errorf("find %s files: %w", ext, err)
}
if len(paths) > 0 {
return filepath.Base(paths[0]), nil
}
var firstFound string
walkErr := filepath.Walk(
modelDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
// Propagate filesystem errors so callers can distinguish them
// from the case where no matching files are present.
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ext ||
strings.HasPrefix(info.Name(), ".") {
return nil
}
rel, relErr := filepath.Rel(modelDir, path)
if relErr != nil {
// Treat a bad relative path as a real error instead of
// silently ignoring it, so malformed bundles surface to the
// caller.
return relErr
}
firstFound = rel
return errFoundModelFile
},
)
if walkErr != nil && !errors.Is(walkErr, errFoundModelFile) {
return "", fmt.Errorf("walk for %s files: %w", ext, walkErr)
}
return firstFound, nil
}
func findGGUFFile(modelDir string) (string, error) {
// GGUF files are optional.
return findModelFile(modelDir, ".gguf")
}
func findSafetensorsFile(modelDir string) (string, error) {
// Safetensors files are optional.
return findModelFile(modelDir, ".safetensors")
}
func findDDUFFile(modelDir string) (string, error) {
// DDUF files are optional.
return findModelFile(modelDir, ".dduf")
}
func findMultiModalProjectorFile(modelDir string) (string, error) {
mmprojPaths, err := filepath.Glob(filepath.Join(modelDir, "[^.]*.mmproj"))
if err != nil {
return "", err
}
if len(mmprojPaths) == 0 {
return "", nil
}
if len(mmprojPaths) > 1 {
return "", fmt.Errorf("found multiple .mmproj files, but only 1 is supported")
}
return filepath.Base(mmprojPaths[0]), nil
}
func findChatTemplateFile(modelDir string) (string, error) {
templatePaths, err := filepath.Glob(filepath.Join(modelDir, "[^.]*.jinja"))
if err != nil {
return "", err
}
if len(templatePaths) == 0 {
return "", nil
}
if len(templatePaths) > 1 {
return "", fmt.Errorf("found multiple template files, but only 1 is supported")
}
return filepath.Base(templatePaths[0]), nil
}