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
afb2eead
Commit
afb2eead
authored
Mar 19, 2026
by
Your Name
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix: properly unload text models before loading image model
parent
6eb3b9b8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
0 deletions
+35
-0
images.py
codai/api/images.py
+35
-0
No files found.
codai/api/images.py
View file @
afb2eead
...
...
@@ -260,6 +260,41 @@ async def create_image_generation(request: ImageGenerationRequest, http_request:
# Try to load if not cached
if
pipeline
is
None
:
# FIRST: Clean up any loaded text models to free VRAM
# This must happen BEFORE attempting to load any new model
print
(
"Cleaning up text models to free VRAM for image generation..."
)
for
key
in
list
(
multi_model_manager
.
models
.
keys
()):
if
key
.
startswith
(
"image:"
):
continue
# Unload any other model (text, audio, etc.) to free VRAM
model_to_cleanup
=
multi_model_manager
.
models
.
get
(
key
)
if
model_to_cleanup
is
not
None
:
print
(
f
"Unloading '{key}' from VRAM to make room for image model"
)
try
:
if
hasattr
(
model_to_cleanup
,
'cleanup'
)
and
callable
(
getattr
(
model_to_cleanup
,
'cleanup'
)):
model_to_cleanup
.
cleanup
()
elif
hasattr
(
model_to_cleanup
,
'model'
)
and
model_to_cleanup
.
model
is
not
None
:
if
hasattr
(
model_to_cleanup
.
model
,
'cleanup'
):
model_to_cleanup
.
model
.
cleanup
()
except
Exception
as
e
:
print
(
f
"Warning during cleanup of '{key}': {e}"
)
del
multi_model_manager
.
models
[
key
]
# Also try to unload the main model_manager backend if it's loaded
try
:
from
codai.models.manager
import
model_manager
if
model_manager
.
backend
is
not
None
:
print
(
"Unloading main model from VRAM..."
)
if
hasattr
(
model_manager
.
backend
,
'unload'
):
model_manager
.
backend
.
unload
()
elif
hasattr
(
model_manager
.
backend
,
'model'
)
and
model_manager
.
backend
.
model
is
not
None
:
if
hasattr
(
model_manager
.
backend
.
model
,
'unload'
):
model_manager
.
backend
.
model
.
unload
()
# Reset backend to None
model_manager
.
backend
=
None
except
Exception
as
e
:
print
(
f
"Warning during main model cleanup: {e}"
)
# Try diffusers first
try
:
from
diffusers
import
StableDiffusionPipeline
,
StableDiffusionXLPipeline
...
...
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