Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
6de5d032
Commit
6de5d032
authored
Aug 03, 2023
by
Michael Yang
Browse files
implement loading ggml lora adapters through the modelfile
parent
d791df75
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
13 deletions
+65
-13
llm/llama.go
llm/llama.go
+16
-1
llm/llm.go
llm/llm.go
+2
-2
parser/parser.go
parser/parser.go
+1
-1
server/images.go
server/images.go
+45
-8
server/routes.go
server/routes.go
+1
-1
No files found.
llm/llama.go
View file @
6de5d032
...
@@ -136,7 +136,7 @@ type llamaHyperparameters struct {
...
@@ -136,7 +136,7 @@ type llamaHyperparameters struct {
FileType
FileType
}
}
func
newLlama
(
model
string
,
opts
api
.
Options
)
(
*
llama
,
error
)
{
func
newLlama
(
model
string
,
adapters
[]
string
,
opts
api
.
Options
)
(
*
llama
,
error
)
{
if
_
,
err
:=
os
.
Stat
(
model
);
err
!=
nil
{
if
_
,
err
:=
os
.
Stat
(
model
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -161,6 +161,12 @@ func newLlama(model string, opts api.Options) (*llama, error) {
...
@@ -161,6 +161,12 @@ func newLlama(model string, opts api.Options) (*llama, error) {
params
.
embedding
=
C
.
bool
(
llm
.
EmbeddingOnly
)
params
.
embedding
=
C
.
bool
(
llm
.
EmbeddingOnly
)
params
.
rope_freq_base
=
C
.
float
(
llm
.
RopeFrequencyBase
)
params
.
rope_freq_base
=
C
.
float
(
llm
.
RopeFrequencyBase
)
params
.
rope_freq_scale
=
C
.
float
(
llm
.
RopeFrequencyScale
)
params
.
rope_freq_scale
=
C
.
float
(
llm
.
RopeFrequencyScale
)
if
len
(
adapters
)
>
0
&&
llm
.
UseMMap
{
log
.
Printf
(
"must disable mmap to use lora adapters"
)
params
.
use_mmap
=
C
.
bool
(
false
)
}
llm
.
params
=
&
params
llm
.
params
=
&
params
cModel
:=
C
.
CString
(
model
)
cModel
:=
C
.
CString
(
model
)
...
@@ -176,6 +182,15 @@ func newLlama(model string, opts api.Options) (*llama, error) {
...
@@ -176,6 +182,15 @@ func newLlama(model string, opts api.Options) (*llama, error) {
return
nil
,
errors
.
New
(
"failed to create context"
)
return
nil
,
errors
.
New
(
"failed to create context"
)
}
}
for
_
,
adapter
:=
range
adapters
{
cAdapter
:=
C
.
CString
(
adapter
)
defer
C
.
free
(
unsafe
.
Pointer
(
cAdapter
))
if
retval
:=
C
.
llama_model_apply_lora_from_file
(
llm
.
model
,
cAdapter
,
nil
,
C
.
int
(
llm
.
NumThread
));
retval
!=
0
{
return
nil
,
fmt
.
Errorf
(
"failed to load adapter %s"
,
adapter
)
}
}
// warm up the model
// warm up the model
bos
:=
[]
C
.
llama_token
{
C
.
llama_token_bos
()}
bos
:=
[]
C
.
llama_token
{
C
.
llama_token_bos
()}
C
.
llama_eval
(
llm
.
ctx
,
unsafe
.
SliceData
(
bos
),
C
.
int
(
len
(
bos
)),
0
,
C
.
int
(
opts
.
NumThread
))
C
.
llama_eval
(
llm
.
ctx
,
unsafe
.
SliceData
(
bos
),
C
.
int
(
len
(
bos
)),
0
,
C
.
int
(
opts
.
NumThread
))
...
...
llm/llm.go
View file @
6de5d032
...
@@ -19,7 +19,7 @@ type LLM interface {
...
@@ -19,7 +19,7 @@ type LLM interface {
Close
()
Close
()
}
}
func
New
(
model
string
,
opts
api
.
Options
)
(
LLM
,
error
)
{
func
New
(
model
string
,
adapters
[]
string
,
opts
api
.
Options
)
(
LLM
,
error
)
{
if
_
,
err
:=
os
.
Stat
(
model
);
err
!=
nil
{
if
_
,
err
:=
os
.
Stat
(
model
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -66,7 +66,7 @@ func New(model string, opts api.Options) (LLM, error) {
...
@@ -66,7 +66,7 @@ func New(model string, opts api.Options) (LLM, error) {
switch
ggml
.
ModelFamily
{
switch
ggml
.
ModelFamily
{
case
ModelFamilyLlama
:
case
ModelFamilyLlama
:
return
newLlama
(
model
,
opts
)
return
newLlama
(
model
,
adapters
,
opts
)
default
:
default
:
return
nil
,
fmt
.
Errorf
(
"unknown ggml type: %s"
,
ggml
.
ModelFamily
)
return
nil
,
fmt
.
Errorf
(
"unknown ggml type: %s"
,
ggml
.
ModelFamily
)
}
}
...
...
parser/parser.go
View file @
6de5d032
...
@@ -40,7 +40,7 @@ func Parse(reader io.Reader) ([]Command, error) {
...
@@ -40,7 +40,7 @@ func Parse(reader io.Reader) ([]Command, error) {
command
.
Args
=
string
(
fields
[
1
])
command
.
Args
=
string
(
fields
[
1
])
// copy command for validation
// copy command for validation
modelCommand
=
command
modelCommand
=
command
case
"LICENSE"
,
"TEMPLATE"
,
"SYSTEM"
,
"PROMPT"
,
"EMBED"
:
case
"LICENSE"
,
"TEMPLATE"
,
"SYSTEM"
,
"PROMPT"
,
"EMBED"
,
"ADAPTER"
:
command
.
Name
=
string
(
bytes
.
ToLower
(
fields
[
0
]))
command
.
Name
=
string
(
bytes
.
ToLower
(
fields
[
0
]))
command
.
Args
=
string
(
fields
[
1
])
command
.
Args
=
string
(
fields
[
1
])
case
"PARAMETER"
:
case
"PARAMETER"
:
...
...
server/images.go
View file @
6de5d032
...
@@ -31,13 +31,14 @@ type RegistryOptions struct {
...
@@ -31,13 +31,14 @@ type RegistryOptions struct {
}
}
type
Model
struct
{
type
Model
struct
{
Name
string
`json:"name"`
Name
string
`json:"name"`
ModelPath
string
ModelPath
string
Template
string
AdapterPaths
[]
string
System
string
Template
string
Digest
string
System
string
Options
map
[
string
]
interface
{}
Digest
string
Embeddings
[]
vector
.
Embedding
Options
map
[
string
]
interface
{}
Embeddings
[]
vector
.
Embedding
}
}
func
(
m
*
Model
)
Prompt
(
request
api
.
GenerateRequest
,
embedding
string
)
(
string
,
error
)
{
func
(
m
*
Model
)
Prompt
(
request
api
.
GenerateRequest
,
embedding
string
)
(
string
,
error
)
{
...
@@ -178,6 +179,8 @@ func GetModel(name string) (*Model, error) {
...
@@ -178,6 +179,8 @@ func GetModel(name string) (*Model, error) {
if
err
=
json
.
NewDecoder
(
file
)
.
Decode
(
&
model
.
Embeddings
);
err
!=
nil
{
if
err
=
json
.
NewDecoder
(
file
)
.
Decode
(
&
model
.
Embeddings
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
case
"application/vnd.ollama.image.adapter"
:
model
.
AdapterPaths
=
append
(
model
.
AdapterPaths
,
filename
)
case
"application/vnd.ollama.image.template"
:
case
"application/vnd.ollama.image.template"
:
bts
,
err
:=
os
.
ReadFile
(
filename
)
bts
,
err
:=
os
.
ReadFile
(
filename
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -330,6 +333,40 @@ func CreateModel(ctx context.Context, name string, path string, fn func(resp api
...
@@ -330,6 +333,40 @@ func CreateModel(ctx context.Context, name string, path string, fn func(resp api
return
err
return
err
}
}
embed
.
files
=
append
(
embed
.
files
,
embedFilePath
)
embed
.
files
=
append
(
embed
.
files
,
embedFilePath
)
case
"adapter"
:
fn
(
api
.
ProgressResponse
{
Status
:
fmt
.
Sprintf
(
"creating model %s layer"
,
c
.
Name
)})
fp
:=
c
.
Args
if
strings
.
HasPrefix
(
fp
,
"~/"
)
{
parts
:=
strings
.
Split
(
fp
,
"/"
)
home
,
err
:=
os
.
UserHomeDir
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to open file: %v"
,
err
)
}
fp
=
filepath
.
Join
(
home
,
filepath
.
Join
(
parts
[
1
:
]
...
))
}
// If filePath is not an absolute path, make it relative to the modelfile path
if
!
filepath
.
IsAbs
(
fp
)
{
fp
=
filepath
.
Join
(
filepath
.
Dir
(
path
),
fp
)
}
// create a model from this specified file
fn
(
api
.
ProgressResponse
{
Status
:
"creating model layer"
})
file
,
err
:=
os
.
Open
(
fp
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to open file: %v"
,
err
)
}
defer
file
.
Close
()
l
,
err
:=
CreateLayer
(
file
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to create layer: %v"
,
err
)
}
l
.
MediaType
=
"application/vnd.ollama.image.adapter"
layers
=
append
(
layers
,
l
)
case
"license"
:
case
"license"
:
fn
(
api
.
ProgressResponse
{
Status
:
fmt
.
Sprintf
(
"creating model %s layer"
,
c
.
Name
)})
fn
(
api
.
ProgressResponse
{
Status
:
fmt
.
Sprintf
(
"creating model %s layer"
,
c
.
Name
)})
mediaType
:=
fmt
.
Sprintf
(
"application/vnd.ollama.image.%s"
,
c
.
Name
)
mediaType
:=
fmt
.
Sprintf
(
"application/vnd.ollama.image.%s"
,
c
.
Name
)
...
@@ -452,7 +489,7 @@ func embeddingLayers(e EmbeddingParams) ([]*LayerReader, error) {
...
@@ -452,7 +489,7 @@ func embeddingLayers(e EmbeddingParams) ([]*LayerReader, error) {
}
}
e
.
opts
.
EmbeddingOnly
=
true
e
.
opts
.
EmbeddingOnly
=
true
llmModel
,
err
:=
llm
.
New
(
e
.
model
,
e
.
opts
)
llmModel
,
err
:=
llm
.
New
(
e
.
model
,
[]
string
{},
e
.
opts
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"load model to generate embeddings: %v"
,
err
)
return
nil
,
fmt
.
Errorf
(
"load model to generate embeddings: %v"
,
err
)
}
}
...
...
server/routes.go
View file @
6de5d032
...
@@ -63,7 +63,7 @@ func load(model *Model, reqOpts map[string]interface{}, sessionDuration time.Dur
...
@@ -63,7 +63,7 @@ func load(model *Model, reqOpts map[string]interface{}, sessionDuration time.Dur
loaded
.
Embeddings
=
model
.
Embeddings
loaded
.
Embeddings
=
model
.
Embeddings
}
}
llmModel
,
err
:=
llm
.
New
(
model
.
ModelPath
,
opts
)
llmModel
,
err
:=
llm
.
New
(
model
.
ModelPath
,
model
.
AdapterPaths
,
opts
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment