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
f2ba1311
Unverified
Commit
f2ba1311
authored
Oct 10, 2023
by
Bruce MacDonald
Committed by
GitHub
Oct 10, 2023
Browse files
improve vram safety with 5% vram memory buffer (#724)
* check free memory not total * wait for subprocess to exit
parent
65dcd0ce
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
7 deletions
+13
-7
llm/llama.go
llm/llama.go
+13
-7
No files found.
llm/llama.go
View file @
f2ba1311
...
...
@@ -191,7 +191,7 @@ var errNoGPU = errors.New("nvidia-smi command failed")
// CheckVRAM returns the available VRAM in MiB on Linux machines with NVIDIA GPUs
func
CheckVRAM
()
(
int64
,
error
)
{
cmd
:=
exec
.
Command
(
"nvidia-smi"
,
"--query-gpu=memory.
total
"
,
"--format=csv,noheader,nounits"
)
cmd
:=
exec
.
Command
(
"nvidia-smi"
,
"--query-gpu=memory.
free
"
,
"--format=csv,noheader,nounits"
)
var
stdout
bytes
.
Buffer
cmd
.
Stdout
=
&
stdout
err
:=
cmd
.
Run
()
...
...
@@ -199,7 +199,7 @@ func CheckVRAM() (int64, error) {
return
0
,
errNoGPU
}
var
total
int64
var
free
int64
scanner
:=
bufio
.
NewScanner
(
&
stdout
)
for
scanner
.
Scan
()
{
line
:=
scanner
.
Text
()
...
...
@@ -208,10 +208,10 @@ func CheckVRAM() (int64, error) {
return
0
,
fmt
.
Errorf
(
"failed to parse available VRAM: %v"
,
err
)
}
total
+=
vram
free
+=
vram
}
return
total
,
nil
return
free
,
nil
}
func
NumGPU
(
numLayer
,
fileSizeBytes
int64
,
opts
api
.
Options
)
int
{
...
...
@@ -228,14 +228,14 @@ func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
return
0
}
total
VramBytes
:=
int64
(
vramMib
)
*
1024
*
1024
// 1 MiB = 1024^2 bytes
free
VramBytes
:=
int64
(
vramMib
)
*
1024
*
1024
// 1 MiB = 1024^2 bytes
// Calculate bytes per layer
// TODO: this is a rough heuristic, better would be to calculate this based on number of layers and context size
bytesPerLayer
:=
fileSizeBytes
/
numLayer
// max number of layers we can fit in VRAM
layers
:=
int
(
total
VramBytes
/
bytesPerLayer
)
// max number of layers we can fit in VRAM
, subtract 5% to prevent consuming all available VRAM and running out of memory
layers
:=
int
(
free
VramBytes
/
bytesPerLayer
)
*
95
/
100
log
.
Printf
(
"%d MiB VRAM available, loading up to %d GPU layers"
,
vramMib
,
layers
)
return
layers
...
...
@@ -367,7 +367,13 @@ func waitForServer(llm *llama) error {
}
func
(
llm
*
llama
)
Close
()
{
// signal the sub-process to terminate
llm
.
Cancel
()
// wait for the command to exit to prevent race conditions with the next run
if
err
:=
llm
.
Cmd
.
Wait
();
err
!=
nil
{
log
.
Printf
(
"llama runner exited: %v"
,
err
)
}
}
func
(
llm
*
llama
)
SetOptions
(
opts
api
.
Options
)
{
...
...
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