Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
coderai
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexlab
coderai
Commits
d9a5d274
Commit
d9a5d274
authored
Mar 01, 2026
by
Stefy Lanza (nextime / spora )
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add --offload-strategy parameter for NVIDIA backend with 4 strategy options
parent
10d10573
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
21 deletions
+45
-21
coderai
coderai
+45
-21
No files found.
coderai
View file @
d9a5d274
...
@@ -593,8 +593,40 @@ class NvidiaBackend(ModelBackend):
...
@@ -593,8 +593,40 @@ class NvidiaBackend(ModelBackend):
model_name_lower
=
model_name
.
lower
()
model_name_lower
=
model_name
.
lower
()
return
any
(
indicator
in
model_name_lower
for
indicator
in
moe_indicators
)
return
any
(
indicator
in
model_name_lower
for
indicator
in
moe_indicators
)
def
_get_vram_percentages_for_gpu
(
self
,
model_name
:
str
=
""
)
->
list
:
def
_get_vram_percentages_for_strategy
(
self
,
strategy
:
str
,
is_moe
:
bool
,
total_vram_gb
:
float
)
->
list
:
"""Get VRAM percentage steps based on GPU memory size and model type."""
"""Get VRAM percentage steps based on offload strategy."""
if
strategy
==
"conservative"
:
print
(
f
" Using conservative offload strategy - minimal VRAM usage for maximum stability"
)
if
is_moe
:
return
[
0.70
,
0.65
,
0.60
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
return
[
0.80
,
0.75
,
0.70
,
0.65
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
elif
strategy
==
"balanced"
:
print
(
f
" Using balanced offload strategy - good performance with reasonable stability"
)
if
is_moe
:
return
[
0.75
,
0.70
,
0.65
,
0.60
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
return
[
0.85
,
0.80
,
0.75
,
0.70
,
0.65
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
elif
strategy
==
"aggressive"
:
print
(
f
" Using aggressive offload strategy - maximize VRAM usage for performance"
)
if
is_moe
:
return
[
0.85
,
0.80
,
0.75
,
0.70
,
0.65
,
0.60
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
return
[
0.95
,
0.90
,
0.85
,
0.80
,
0.75
,
0.70
,
0.65
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
else
:
#
auto
if
total_vram_gb
<
3
:
print
(
f
" Detected small GPU ({total_vram_gb:.1f}GB), using aggressive VRAM usage (99% start)"
)
return
[
0.99
,
0.95
,
0.90
,
0.85
,
0.75
,
0.65
,
0.50
,
0.35
,
0.20
,
0.0
]
elif
total_vram_gb
<=
8
:
print
(
f
" Detected medium GPU ({total_vram_gb:.1f}GB), using high VRAM usage (96% start)"
)
return
[
0.96
,
0.90
,
0.85
,
0.75
,
0.65
,
0.50
,
0.35
,
0.20
,
0.0
]
else
:
if
is_moe
:
print
(
f
" Detected large GPU ({total_vram_gb:.1f}GB), using MoE-safe VRAM usage (80% start)"
)
return
[
0.80
,
0.75
,
0.70
,
0.65
,
0.60
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
else
:
print
(
f
" Detected large GPU ({total_vram_gb:.1f}GB), using conservative VRAM usage (93% start)"
)
return
[
0.93
,
0.85
,
0.75
,
0.65
,
0.50
,
0.35
,
0.20
,
0.0
]
def
_get_vram_percentages_for_gpu
(
self
,
model_name
:
str
=
""
,
strategy
:
str
=
"auto"
)
->
list
:
"""Get VRAM percentage steps based on GPU memory size, model type, and offload strategy."""
import
torch
import
torch
if
not
torch
.
cuda
.
is_available
():
if
not
torch
.
cuda
.
is_available
():
...
@@ -611,24 +643,7 @@ class NvidiaBackend(ModelBackend):
...
@@ -611,24 +643,7 @@ class NvidiaBackend(ModelBackend):
if
is_moe
:
if
is_moe
:
print
(
f
" Detected MoE model, using extra conservative VRAM limits for generation headroom"
)
print
(
f
" Detected MoE model, using extra conservative VRAM limits for generation headroom"
)
#
Determine
starting
percentage
based
on
VRAM
size
and
model
type
return
self
.
_get_vram_percentages_for_strategy
(
strategy
,
is_moe
,
total_vram_gb
)
if
total_vram_gb
<
3
:
#
Small
GPUs
(<
3
GB
):
start
with
99
%
print
(
f
" Detected small GPU ({total_vram_gb:.1f}GB), using aggressive VRAM usage (99% start)"
)
return
[
0.99
,
0.95
,
0.90
,
0.85
,
0.75
,
0.65
,
0.50
,
0.35
,
0.20
,
0.0
]
elif
total_vram_gb
<=
8
:
#
Medium
GPUs
(
3
-
8
GB
):
start
with
96
%
print
(
f
" Detected medium GPU ({total_vram_gb:.1f}GB), using high VRAM usage (96% start)"
)
return
[
0.96
,
0.90
,
0.85
,
0.75
,
0.65
,
0.50
,
0.35
,
0.20
,
0.0
]
else
:
#
Large
GPUs
(>
8
GB
)
if
is_moe
:
#
MoE
models
need
more
headroom
for
generation
activations
print
(
f
" Detected large GPU ({total_vram_gb:.1f}GB), using MoE-safe VRAM usage (80% start)"
)
return
[
0.80
,
0.75
,
0.70
,
0.65
,
0.60
,
0.50
,
0.40
,
0.30
,
0.20
,
0.0
]
else
:
print
(
f
" Detected large GPU ({total_vram_gb:.1f}GB), using conservative VRAM usage (93% start)"
)
return
[
0.93
,
0.85
,
0.75
,
0.65
,
0.50
,
0.35
,
0.20
,
0.0
]
def
load_model
(
self
,
model_name
:
str
,
**
kwargs
)
->
None
:
def
load_model
(
self
,
model_name
:
str
,
**
kwargs
)
->
None
:
"""Load the model using HuggingFace Transformers with automatic OOM handling."""
"""Load the model using HuggingFace Transformers with automatic OOM handling."""
...
@@ -640,6 +655,7 @@ class NvidiaBackend(ModelBackend):
...
@@ -640,6 +655,7 @@ class NvidiaBackend(ModelBackend):
load_in_8bit
=
kwargs
.
get
(
'load_in_8bit'
,
False
)
load_in_8bit
=
kwargs
.
get
(
'load_in_8bit'
,
False
)
manual_ram_gb
=
kwargs
.
get
(
'manual_ram_gb'
)
manual_ram_gb
=
kwargs
.
get
(
'manual_ram_gb'
)
flash_attn
=
kwargs
.
get
(
'flash_attn'
,
False
)
flash_attn
=
kwargs
.
get
(
'flash_attn'
,
False
)
offload_strategy
=
kwargs
.
get
(
'offload_strategy'
,
'auto'
)
#
Store
RAM
limit
for
use
in
_get_gpu_memory_map
#
Store
RAM
limit
for
use
in
_get_gpu_memory_map
self
.
_pending_ram_gb
=
manual_ram_gb
self
.
_pending_ram_gb
=
manual_ram_gb
...
@@ -698,7 +714,7 @@ class NvidiaBackend(ModelBackend):
...
@@ -698,7 +714,7 @@ class NvidiaBackend(ModelBackend):
# Try loading with automatic fallback on OOM
# Try loading with automatic fallback on OOM
model = None
model = None
vram_percentages = self._get_vram_percentages_for_gpu(model_name)
vram_percentages = self._get_vram_percentages_for_gpu(model_name
, offload_strategy
)
first_vram_pct = vram_percentages[0] if vram_percentages else 0.93
first_vram_pct = vram_percentages[0] if vram_percentages else 0.93
for vram_pct in vram_percentages:
for vram_pct in vram_percentages:
...
@@ -2017,6 +2033,13 @@ def parse_args():
...
@@ -2017,6 +2033,13 @@ def parse_args():
action="store_true",
action="store_true",
help="Use Flash Attention 2 (NVIDIA backend only, requires flash-attn package)",
help="Use Flash Attention 2 (NVIDIA backend only, requires flash-attn package)",
)
)
parser.add_argument(
"--offload-strategy",
type=str,
choices=["auto", "conservative", "balanced", "aggressive"],
default="auto",
help="Offload strategy for NVIDIA backend (default: auto)",
)
parser.add_argument(
parser.add_argument(
"--n-gpu-layers",
"--n-gpu-layers",
type=int,
type=int,
...
@@ -2119,6 +2142,7 @@ def main():
...
@@ -2119,6 +2142,7 @@ def main():
'
load_in_8bit
': args.load_in_8bit,
'
load_in_8bit
': args.load_in_8bit,
'
manual_ram_gb
': args.ram,
'
manual_ram_gb
': args.ram,
'
flash_attn
': args.flash_attn,
'
flash_attn
': args.flash_attn,
'
offload_strategy
': args.offload_strategy,
'
n_gpu_layers
': args.n_gpu_layers,
'
n_gpu_layers
': args.n_gpu_layers,
'
n_ctx
': args.n_ctx,
'
n_ctx
': args.n_ctx,
'
main_gpu
': args.vulkan_device,
'
main_gpu
': args.vulkan_device,
...
...
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