Merge pull request #221 from jmorganca/embed-metal

embed ggml-metal.metal
This commit is contained in:
Michael Yang 2023-07-27 17:24:41 -07:00 committed by GitHub
commit e5b26c3aa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View file

@ -1 +0,0 @@
llama/ggml-metal.metal

View file

@ -86,6 +86,7 @@ llama_token llama_sample(
import "C"
import (
"bytes"
"embed"
"errors"
"fmt"
"io"
@ -99,6 +100,9 @@ import (
"github.com/jmorganca/ollama/api"
)
//go:embed ggml-metal.metal
var fs embed.FS
type LLM struct {
params *C.struct_llama_context_params
model *C.struct_llama_model

53
llama/llama_darwin.go Normal file
View file

@ -0,0 +1,53 @@
package llama
import (
"errors"
"io"
"log"
"os"
"path/filepath"
)
func init() {
if err := initBackend(); err != nil {
log.Printf("WARNING: GPU could not be initialized correctly: %v", err)
log.Printf("WARNING: falling back to CPU")
}
}
func initBackend() error {
exec, err := os.Executable()
if err != nil {
return err
}
exec, err = filepath.EvalSymlinks(exec)
if err != nil {
return err
}
metal := filepath.Join(filepath.Dir(exec), "ggml-metal.metal")
if _, err := os.Stat(metal); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
dst, err := os.Create(filepath.Join(filepath.Dir(exec), "ggml-metal.metal"))
if err != nil {
return err
}
defer dst.Close()
src, err := fs.Open("ggml-metal.metal")
if err != nil {
return err
}
defer src.Close()
if _, err := io.Copy(dst, src); err != nil {
return err
}
}
return nil
}