-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathsetup.go
More file actions
218 lines (176 loc) · 6.16 KB
/
Copy pathsetup.go
File metadata and controls
218 lines (176 loc) · 6.16 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package onnx
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/Achno/gowall/config"
"github.com/Achno/gowall/internal/logger"
"github.com/Achno/gowall/utils"
ort "github.com/yalue/onnxruntime_go"
)
var onnxRuntimeVersion = config.OnnxRuntimeVersion
// SharedLibraryName returns the platform-specific shared library name
func SharedLibraryName() string {
switch runtime.GOOS {
case "linux":
return fmt.Sprintf("libonnxruntime.so.%s", onnxRuntimeVersion)
case "freebsd":
return fmt.Sprintf("libonnxruntime.so")
case "darwin":
return fmt.Sprintf("libonnxruntime.%s.dylib", onnxRuntimeVersion)
case "windows":
return "onnxruntime.dll"
default:
return ""
}
}
func ensureRuntimeAvailable() (string, error) {
runtimePath, err := CheckOnnxRuntimeInstalled()
if err == nil {
return runtimePath, nil
}
prompt := fmt.Sprintf("%s ◈ ONNX Runtime is not installed. Would you like to set it up?%s", utils.BlueColor, utils.ResetColor)
if !utils.Confirm(prompt) {
return "", fmt.Errorf("onnx runtime download declined")
}
if err := SetupOnnxRuntime(); err != nil {
return "", err
}
return CheckOnnxRuntimeInstalled()
}
func ensureEnvironment(libraryPath string) error {
ort.SetSharedLibraryPath(libraryPath)
if err := ort.InitializeEnvironment(); err != nil {
return fmt.Errorf("initialize onnxruntime: %w", err)
}
return nil
}
func ensureModelAvailable(model Model) (string, error) {
baseDir := config.GowallConfig.OnnxModelFolderPath
modelPath := modelCachePath(baseDir, model)
if _, err := os.Stat(modelPath); err == nil {
return modelPath, nil
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("check model path %q: %w", modelPath, err)
}
prompt := fmt.Sprintf("%s ◈ Model %q is missing. Download it?%s", utils.BlueColor, model.Name(), utils.ResetColor)
if !utils.Confirm(prompt) {
return "", fmt.Errorf("model download declined for %q", model.Name())
}
// Get file size for progress info
size := utils.GetRemoteFileSize(model.DownloadURL())
sizeInfo := ""
if size != "" {
sizeInfo = fmt.Sprintf(" size: %s,", size)
}
logger.Print(fmt.Sprintf("%s ➜ Downloading %s,%s sit back and relax%s", utils.BlueColor, model.Name(), sizeInfo, utils.ResetColor))
if err := os.MkdirAll(filepath.Dir(modelPath), 0o755); err != nil {
return "", fmt.Errorf("create model directory: %w", err)
}
if err := model.Download(DownloadOptions{DestPath: modelPath}); err != nil {
return "", fmt.Errorf("download model %q: %w", model.Name(), err)
}
if _, err := os.Stat(modelPath); err != nil {
return "", fmt.Errorf("model %q was not written to %s: %w", model.Name(), modelPath, err)
}
logger.Print(fmt.Sprintf("%s ➜ Process complete. Model %s downloaded%s", utils.BlueColor, model.Name(), utils.ResetColor))
return modelPath, nil
}
func runtimeDownloadURL() (string, error) {
urls := map[string]string{
"linux": fmt.Sprintf("https://github.com/microsoft/onnxruntime/releases/download/v%s/onnxruntime-linux-x64-%s.tgz", onnxRuntimeVersion, onnxRuntimeVersion),
"darwin": fmt.Sprintf("https://github.com/microsoft/onnxruntime/releases/download/v%s/onnxruntime-osx-arm64-%s.tgz", onnxRuntimeVersion, onnxRuntimeVersion),
"windows": fmt.Sprintf("https://github.com/microsoft/onnxruntime/releases/download/v%s/onnxruntime-win-x64-%s.zip", onnxRuntimeVersion, onnxRuntimeVersion),
}
url, exists := urls[runtime.GOOS]
if !exists {
return "", fmt.Errorf("unsupported OS: %s. Only available for linux, mac, windows", runtime.GOOS)
}
return url, nil
}
// SetupOnnxRuntime downloads and extracts only the ONNX runtime shared library
func SetupOnnxRuntime() error {
destFolder := config.GowallConfig.OnnxRuntimeFolderPath
url, err := runtimeDownloadURL()
if err != nil {
return err
}
size := utils.GetRemoteFileSize(url)
sizeInfo := ""
if size != "" {
sizeInfo = fmt.Sprintf(" size: %s,", size)
}
logger.Print(fmt.Sprintf("%s ➜ Downloading ONNX Runtime,%s sit back and relax%s", utils.BlueColor, sizeInfo, utils.ResetColor))
if err := os.MkdirAll(destFolder, 0755); err != nil {
return fmt.Errorf("create destination folder: %w", err)
}
// Download archive to temp location
archiveName := filepath.Base(url)
archivePath := filepath.Join(destFolder, archiveName)
if err := utils.DownloadUrl(url, archivePath); err != nil {
return fmt.Errorf("download: %w", err)
}
defer os.Remove(archivePath)
// Extract only the shared library
libName := SharedLibraryName()
libPath := filepath.Join(destFolder, libName)
if err := utils.ExtractSingleFile(archivePath, libPath, libName); err != nil {
return fmt.Errorf("extract shared library: %w", err)
}
logger.Print(utils.BlueColor + " ➜ Process complete. ONNX Runtime setup" + utils.ResetColor)
return nil
}
// CheckOnnxRuntimeInstalled checks if the ONNX runtime shared library is available
func CheckOnnxRuntimeInstalled() (string, error) {
destFolder := config.GowallConfig.OnnxRuntimeFolderPath
if runtime.GOOS == "freebsd" {
destFolder = filepath.Join("/usr/local", "lib")
}
libName := SharedLibraryName()
if libName == "" {
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
// Library should be directly in the folder now
libPath := filepath.Join(destFolder, libName)
if _, err := os.Stat(libPath); err != nil {
if os.IsNotExist(err) {
return "", fmt.Errorf("onnx runtime library not found at %s", libPath)
}
return "", fmt.Errorf("error checking library: %w", err)
}
return libPath, nil
}
// modelCachePath returns ~/.u2net/<model>.onnx
func modelCachePath(baseDir string, model Model) string {
name := strings.TrimSpace(model.Name())
fileName := name
if filepath.Ext(fileName) == "" {
fileName += ".onnx"
}
return filepath.Join(baseDir, fileName)
}
func namesFromIO(infos []ort.InputOutputInfo) []string {
names := make([]string, 0, len(infos))
for _, info := range infos {
names = append(names, info.Name)
}
return names
}
func cloneIOInfo(infos []ort.InputOutputInfo) []ort.InputOutputInfo {
if len(infos) == 0 {
return nil
}
cloned := make([]ort.InputOutputInfo, len(infos))
copy(cloned, infos)
return cloned
}
func destroyValues(values []ort.Value) {
for _, value := range values {
if value == nil {
continue
}
value.Destroy()
}
}