mirror of
https://github.com/lukaszraczylo/claude-mnemonic.git
synced 2026-10-21 06:37:02 +00:00
perf(embedding): embed gzipped ONNX runtime, gunzip at first use
This commit is contained in:
@@ -95,3 +95,6 @@ docs/dist
|
|||||||
plugin/.claude-plugin/plugin.json
|
plugin/.claude-plugin/plugin.json
|
||||||
plugin/.claude-plugin/marketplace.json
|
plugin/.claude-plugin/marketplace.json
|
||||||
user-prompt
|
user-prompt
|
||||||
|
|
||||||
|
# Compressed ONNX runtime libs are produced by scripts/download-onnx-libs.sh
|
||||||
|
internal/embedding/assets/lib/**/*.gz
|
||||||
|
|||||||
@@ -10,3 +10,9 @@ Mutate: Go code and build flags. Frozen: public API, DB schema, plugin manifest.
|
|||||||
|----|----------|-------------|---------------|-------|
|
|----|----------|-------------|---------------|-------|
|
||||||
| 01 | baseline | 102698416 | - | base |
|
| 01 | baseline | 102698416 | - | base |
|
||||||
| 02 | pkg/hooks: raw HTTP/1.0 client replaces net/http | 87603952 | -14.7% | KEEP |
|
| 02 | pkg/hooks: raw HTTP/1.0 client replaces net/http | 87603952 | -14.7% | KEEP |
|
||||||
|
| 03 | statusline: shared client, drop net/http | 84531056 | -3.5% vs 02 | KEEP |
|
||||||
|
| 04 | embed gzipped ONNX runtime libs, gunzip at first use | 57216880 | -32.3% vs 03 | KEEP |
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- Trial 04 needs `scripts/download-onnx-libs.sh` to produce `.gz` files; existing raw libs trigger a re-download.
|
||||||
|
- Extracted library verified byte-identical to the raw dylib (`cmp`).
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed assets/lib/darwin-arm64/libonnxruntime.dylib
|
//go:embed assets/lib/darwin-arm64/libonnxruntime.dylib.gz
|
||||||
var onnxRuntimeLib []byte
|
var onnxRuntimeLib []byte
|
||||||
|
|
||||||
const onnxRuntimeLibName = "libonnxruntime.dylib"
|
const onnxRuntimeLibName = "libonnxruntime.dylib"
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed assets/lib/linux-amd64/libonnxruntime.so
|
//go:embed assets/lib/linux-amd64/libonnxruntime.so.gz
|
||||||
var onnxRuntimeLib []byte
|
var onnxRuntimeLib []byte
|
||||||
|
|
||||||
//go:embed assets/lib/linux-amd64/libonnxruntime_providers_shared.so
|
//go:embed assets/lib/linux-amd64/libonnxruntime_providers_shared.so.gz
|
||||||
var onnxRuntimeProvidersLib []byte
|
var onnxRuntimeProvidersLib []byte
|
||||||
|
|
||||||
const onnxRuntimeLibName = "libonnxruntime.so"
|
const onnxRuntimeLibName = "libonnxruntime.so"
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed assets/lib/linux-arm64/libonnxruntime.so
|
//go:embed assets/lib/linux-arm64/libonnxruntime.so.gz
|
||||||
var onnxRuntimeLib []byte
|
var onnxRuntimeLib []byte
|
||||||
|
|
||||||
//go:embed assets/lib/linux-arm64/libonnxruntime_providers_shared.so
|
//go:embed assets/lib/linux-arm64/libonnxruntime_providers_shared.so.gz
|
||||||
var onnxRuntimeProvidersLib []byte
|
var onnxRuntimeProvidersLib []byte
|
||||||
|
|
||||||
const onnxRuntimeLibName = "libonnxruntime.so"
|
const onnxRuntimeLibName = "libonnxruntime.so"
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed assets/lib/windows-amd64/onnxruntime.dll
|
//go:embed assets/lib/windows-amd64/onnxruntime.dll.gz
|
||||||
var onnxRuntimeLib []byte
|
var onnxRuntimeLib []byte
|
||||||
|
|
||||||
const onnxRuntimeLibName = "onnxruntime.dll"
|
const onnxRuntimeLibName = "onnxruntime.dll"
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ package embedding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -134,7 +136,7 @@ func (m *bgeModel) Dimensions() int {
|
|||||||
return EmbeddingDim
|
return EmbeddingDim
|
||||||
}
|
}
|
||||||
|
|
||||||
// extractONNXLibrary extracts the embedded ONNX runtime library to a temp directory.
|
// extractONNXLibrary gunzips the embedded ONNX runtime library to a temp directory.
|
||||||
// Uses content hash to avoid re-extracting if already present.
|
// Uses content hash to avoid re-extracting if already present.
|
||||||
func extractONNXLibrary() (string, error) {
|
func extractONNXLibrary() (string, error) {
|
||||||
// Create a hash of the library content for cache key
|
// Create a hash of the library content for cache key
|
||||||
@@ -157,16 +159,14 @@ func extractONNXLibrary() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write main library
|
// Write main library
|
||||||
// #nosec G306 -- Shared library needs executable permission (0755) for dynamic linker
|
if err := writeGunzipped(libPath, onnxRuntimeLib); err != nil {
|
||||||
if err := os.WriteFile(libPath, onnxRuntimeLib, 0755); err != nil {
|
|
||||||
return "", fmt.Errorf("write library: %w", err)
|
return "", fmt.Errorf("write library: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write providers library if present (Linux only)
|
// Write providers library if present (Linux only)
|
||||||
if len(onnxRuntimeProvidersLib) > 0 && onnxRuntimeProvidersLibName != "" {
|
if len(onnxRuntimeProvidersLib) > 0 && onnxRuntimeProvidersLibName != "" {
|
||||||
providersPath := filepath.Join(cacheDir, onnxRuntimeProvidersLibName)
|
providersPath := filepath.Join(cacheDir, onnxRuntimeProvidersLibName)
|
||||||
// #nosec G306 -- Shared library needs executable permission (0755) for dynamic linker
|
if err := writeGunzipped(providersPath, onnxRuntimeProvidersLib); err != nil {
|
||||||
if err := os.WriteFile(providersPath, onnxRuntimeProvidersLib, 0755); err != nil {
|
|
||||||
return "", fmt.Errorf("write providers library: %w", err)
|
return "", fmt.Errorf("write providers library: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -626,3 +626,23 @@ func (s *Service) EmbedBatchWithContext(ctx context.Context, texts []string) ([]
|
|||||||
func (s *Service) Close() error {
|
func (s *Service) Close() error {
|
||||||
return s.model.Close()
|
return s.model.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writeGunzipped decompresses gz into path with the executable bit the dynamic linker needs.
|
||||||
|
func writeGunzipped(path string, gz []byte) error {
|
||||||
|
zr, err := gzip.NewReader(bytes.NewReader(gz))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() { _ = zr.Close() }()
|
||||||
|
|
||||||
|
// #nosec G302 G304 -- shared library needs 0755; path is under our own cache dir
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(f, zr); err != nil {
|
||||||
|
_ = f.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return f.Close()
|
||||||
|
}
|
||||||
|
|||||||
@@ -51,9 +51,20 @@ get_installed_version() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Gzip the libraries: Go embeds the .gz and gunzips once at first use, which
|
||||||
|
# shrinks the worker binary by roughly 25 MB.
|
||||||
|
compress_libs() {
|
||||||
|
local plat="$1"
|
||||||
|
local f
|
||||||
|
for f in "${ASSETS_DIR}/${plat}"/*.dylib "${ASSETS_DIR}/${plat}"/*.so "${ASSETS_DIR}/${plat}"/*.dll; do
|
||||||
|
[ -f "$f" ] && gzip -9 -n -f "$f"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
# Write version file after successful download
|
# Write version file after successful download
|
||||||
write_version_file() {
|
write_version_file() {
|
||||||
local plat="$1"
|
local plat="$1"
|
||||||
|
compress_libs "$plat"
|
||||||
echo "${ONNX_VERSION}" > "${ASSETS_DIR}/${plat}/.version"
|
echo "${ONNX_VERSION}" > "${ASSETS_DIR}/${plat}/.version"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,9 +121,9 @@ download_windows_amd64() {
|
|||||||
lib_exists() {
|
lib_exists() {
|
||||||
local plat="$1"
|
local plat="$1"
|
||||||
case "$plat" in
|
case "$plat" in
|
||||||
darwin-*) [ -f "${ASSETS_DIR}/${plat}/libonnxruntime.dylib" ] ;;
|
darwin-*) [ -f "${ASSETS_DIR}/${plat}/libonnxruntime.dylib.gz" ] ;;
|
||||||
linux-*) [ -f "${ASSETS_DIR}/${plat}/libonnxruntime.so" ] ;;
|
linux-*) [ -f "${ASSETS_DIR}/${plat}/libonnxruntime.so.gz" ] ;;
|
||||||
windows-*) [ -f "${ASSETS_DIR}/${plat}/onnxruntime.dll" ] ;;
|
windows-*) [ -f "${ASSETS_DIR}/${plat}/onnxruntime.dll.gz" ] ;;
|
||||||
*) return 1 ;;
|
*) return 1 ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user