Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
vllm_cscc
Commits
6e74fd49
Unverified
Commit
6e74fd49
authored
Apr 28, 2025
by
Alex Wu
Committed by
GitHub
Apr 28, 2025
Browse files
Support loading transformers models with named parameters (#16868)
Signed-off-by:
Alex
<
alexwu@character.ai
>
parent
dcbac4cb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
0 deletions
+23
-0
vllm/model_executor/models/transformers.py
vllm/model_executor/models/transformers.py
+23
-0
No files found.
vllm/model_executor/models/transformers.py
View file @
6e74fd49
...
...
@@ -166,6 +166,9 @@ class TransformersModel(nn.Module):
# Initialize buffers (e.g. rotary embedding inverse frequency)
self
.
init_buffers
(
self
.
model
)
# Initialize parameters
self
.
init_parameters
(
self
.
model
)
# Move remaining meta tensors to device (should happen last)
self
.
meta_to_empty
(
self
.
model
)
...
...
@@ -298,6 +301,25 @@ class TransformersModel(nn.Module):
for
child
in
module
.
children
():
self
.
init_buffers
(
child
)
def
init_parameters
(
self
,
module
:
nn
.
Module
):
"""
If a `parameter` is on the `meta` device, then its parent
`module` is the original module created by:
```python
with torch.device("meta"):
self.model: PreTrainedModel = AutoModel.from_config(...)
```
"""
for
name
,
param
in
module
.
named_parameters
(
recurse
=
False
):
if
param
.
device
==
torch
.
device
(
"meta"
):
new_param
=
nn
.
Parameter
(
torch
.
empty_like
(
param
.
data
,
device
=
self
.
device_config
.
device
))
setattr
(
module
,
name
,
new_param
)
for
child
in
module
.
children
():
self
.
init_parameters
(
child
)
def
meta_to_empty
(
self
,
module
:
nn
.
Module
):
tensors
=
list
(
chain
(
module
.
buffers
(),
module
.
parameters
()))
if
tensors
and
all
(
t
.
device
==
torch
.
device
(
"meta"
)
for
t
in
tensors
):
...
...
@@ -342,6 +364,7 @@ class TransformersModel(nn.Module):
def
load_weights
(
self
,
weights
:
Iterable
[
tuple
[
str
,
torch
.
Tensor
]])
->
set
[
str
]:
params_dict
=
dict
(
self
.
named_parameters
())
loaded_params
=
set
[
str
]()
for
name
,
loaded_weight
in
weights
:
# Use "model" instead of base_model_prefix because
...
...
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