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
11d82d7b
Commit
11d82d7b
authored
Oct 13, 2023
by
Michael Yang
Browse files
update checkvram
parent
36fe2dee
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
11 deletions
+11
-11
llm/llama.go
llm/llama.go
+11
-11
No files found.
llm/llama.go
View file @
11d82d7b
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"time"
"time"
"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/format"
)
)
//go:embed llama.cpp/*/build/*/bin/*
//go:embed llama.cpp/*/build/*/bin/*
...
@@ -197,7 +198,7 @@ type llama struct {
...
@@ -197,7 +198,7 @@ type llama struct {
var
errNoGPU
=
errors
.
New
(
"nvidia-smi command failed"
)
var
errNoGPU
=
errors
.
New
(
"nvidia-smi command failed"
)
// CheckVRAM returns the
availabl
e VRAM in
MiB
on Linux machines with NVIDIA GPUs
// CheckVRAM returns the
fre
e VRAM in
bytes
on Linux machines with NVIDIA GPUs
func
CheckVRAM
()
(
int64
,
error
)
{
func
CheckVRAM
()
(
int64
,
error
)
{
cmd
:=
exec
.
Command
(
"nvidia-smi"
,
"--query-gpu=memory.free"
,
"--format=csv,noheader,nounits"
)
cmd
:=
exec
.
Command
(
"nvidia-smi"
,
"--query-gpu=memory.free"
,
"--format=csv,noheader,nounits"
)
var
stdout
bytes
.
Buffer
var
stdout
bytes
.
Buffer
...
@@ -207,7 +208,7 @@ func CheckVRAM() (int64, error) {
...
@@ -207,7 +208,7 @@ func CheckVRAM() (int64, error) {
return
0
,
errNoGPU
return
0
,
errNoGPU
}
}
var
free
int64
var
free
MiB
int64
scanner
:=
bufio
.
NewScanner
(
&
stdout
)
scanner
:=
bufio
.
NewScanner
(
&
stdout
)
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
line
:=
scanner
.
Text
()
line
:=
scanner
.
Text
()
...
@@ -216,15 +217,16 @@ func CheckVRAM() (int64, error) {
...
@@ -216,15 +217,16 @@ func CheckVRAM() (int64, error) {
return
0
,
fmt
.
Errorf
(
"failed to parse available VRAM: %v"
,
err
)
return
0
,
fmt
.
Errorf
(
"failed to parse available VRAM: %v"
,
err
)
}
}
free
+=
vram
free
MiB
+=
vram
}
}
if
free
*
1024
*
1024
<
2
*
1000
*
1000
*
1000
{
freeBytes
:=
freeMiB
*
1024
*
1024
if
freeBytes
<
2
*
format
.
GigaByte
{
log
.
Printf
(
"less than 2 GB VRAM available, falling back to CPU only"
)
log
.
Printf
(
"less than 2 GB VRAM available, falling back to CPU only"
)
free
=
0
free
MiB
=
0
}
}
return
free
,
nil
return
free
Bytes
,
nil
}
}
func
NumGPU
(
numLayer
,
fileSizeBytes
int64
,
opts
api
.
Options
)
int
{
func
NumGPU
(
numLayer
,
fileSizeBytes
int64
,
opts
api
.
Options
)
int
{
...
@@ -232,7 +234,7 @@ func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
...
@@ -232,7 +234,7 @@ func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
return
opts
.
NumGPU
return
opts
.
NumGPU
}
}
if
runtime
.
GOOS
==
"linux"
{
if
runtime
.
GOOS
==
"linux"
{
vramMib
,
err
:=
CheckVRAM
()
freeBytes
,
err
:=
CheckVRAM
()
if
err
!=
nil
{
if
err
!=
nil
{
if
err
.
Error
()
!=
"nvidia-smi command failed"
{
if
err
.
Error
()
!=
"nvidia-smi command failed"
{
log
.
Print
(
err
.
Error
())
log
.
Print
(
err
.
Error
())
...
@@ -241,15 +243,13 @@ func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
...
@@ -241,15 +243,13 @@ func NumGPU(numLayer, fileSizeBytes int64, opts api.Options) int {
return
0
return
0
}
}
freeVramBytes
:=
int64
(
vramMib
)
*
1024
*
1024
// 1 MiB = 1024^2 bytes
// Calculate bytes per layer
// Calculate bytes per layer
// TODO: this is a rough heuristic, better would be to calculate this based on number of layers and context size
// TODO: this is a rough heuristic, better would be to calculate this based on number of layers and context size
bytesPerLayer
:=
fileSizeBytes
/
numLayer
bytesPerLayer
:=
fileSizeBytes
/
numLayer
// max number of layers we can fit in VRAM, subtract 8% to prevent consuming all available VRAM and running out of memory
// max number of layers we can fit in VRAM, subtract 8% to prevent consuming all available VRAM and running out of memory
layers
:=
int
(
free
Vram
Bytes
/
bytesPerLayer
)
*
92
/
100
layers
:=
int
(
freeBytes
/
bytesPerLayer
)
*
92
/
100
log
.
Printf
(
"%d MiB VRAM available, loading up to %d GPU layers"
,
vramMib
,
layers
)
log
.
Printf
(
"%d MiB VRAM available, loading up to %d GPU layers"
,
freeBytes
,
layers
)
return
layers
return
layers
}
}
...
...
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