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
change
sglang
Commits
a7efbb27
Unverified
Commit
a7efbb27
authored
Jul 02, 2025
by
Simon_CQK
Committed by
GitHub
Jul 01, 2025
Browse files
fix(model loader): use safe_open to prevent file handle leaks. (#7684)
parent
93b6785d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
4 deletions
+7
-4
python/sglang/srt/model_loader/weight_utils.py
python/sglang/srt/model_loader/weight_utils.py
+7
-4
No files found.
python/sglang/srt/model_loader/weight_utils.py
View file @
a7efbb27
...
@@ -460,10 +460,12 @@ def safetensors_weights_iterator(
...
@@ -460,10 +460,12 @@ def safetensors_weights_iterator(
if
disable_mmap
:
if
disable_mmap
:
with
open
(
st_file
,
"rb"
)
as
f
:
with
open
(
st_file
,
"rb"
)
as
f
:
result
=
safetensors
.
torch
.
load
(
f
.
read
())
result
=
safetensors
.
torch
.
load
(
f
.
read
())
for
name
,
param
in
result
.
items
():
yield
name
,
param
else
:
else
:
result
=
safetensors
.
torch
.
load_file
(
st_file
,
device
=
"cpu"
)
with
safetensors
.
safe_open
(
st_file
,
framework
=
"pt"
,
device
=
"cpu"
)
as
f
:
for
name
,
param
in
result
.
item
s
():
for
name
in
f
.
key
s
():
yield
name
,
param
yield
name
,
f
.
get_tensor
(
name
)
def
multi_thread_safetensors_weights_iterator
(
def
multi_thread_safetensors_weights_iterator
(
...
@@ -496,7 +498,8 @@ def multi_thread_safetensors_weights_iterator(
...
@@ -496,7 +498,8 @@ def multi_thread_safetensors_weights_iterator(
with
open
(
st_file
,
"rb"
)
as
f
:
with
open
(
st_file
,
"rb"
)
as
f
:
result
=
safetensors
.
torch
.
load
(
f
.
read
())
result
=
safetensors
.
torch
.
load
(
f
.
read
())
else
:
else
:
result
=
safetensors
.
torch
.
load_file
(
st_file
,
device
=
"cpu"
)
with
safetensors
.
safe_open
(
st_file
,
framework
=
"pt"
,
device
=
"cpu"
)
as
f
:
result
=
{
k
:
f
.
get_tensor
(
k
)
for
k
in
f
.
keys
()}
return
result
return
result
...
...
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