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
79747367
Unverified
Commit
79747367
authored
May 14, 2025
by
Jerry Zhang
Committed by
GitHub
May 14, 2025
Browse files
Add support for loading torchao models with `AOPerModuleConfig` (#17826)
Signed-off-by:
Jerry Zhang
<
jerryzh168@gmail.com
>
parent
2fc9075b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
10 deletions
+37
-10
tests/quantization/test_torchao.py
tests/quantization/test_torchao.py
+15
-3
vllm/model_executor/layers/quantization/torchao.py
vllm/model_executor/layers/quantization/torchao.py
+22
-7
No files found.
tests/quantization/test_torchao.py
View file @
79747367
...
...
@@ -31,9 +31,6 @@ def test_pre_quantized_model(vllm_runner):
])
def
test_opt_125m_int4wo_model_loading_with_params
(
vllm_runner
,
pt_load_map_location
):
"""
Test loading roberta-base model with no lm_head.
"""
torch
.
_dynamo
.
reset
()
model_name
=
"jerryzh168/opt-125m-int4wo"
with
vllm_runner
(
model_name
=
model_name
,
...
...
@@ -47,5 +44,20 @@ def test_opt_125m_int4wo_model_loading_with_params(vllm_runner,
print
(
output
)
@
pytest
.
mark
.
skipif
(
not
TORCHAO_AVAILABLE
,
reason
=
"torchao is not available"
)
def
test_opt_125m_int4wo_model_per_module_quant
(
vllm_runner
):
torch
.
_dynamo
.
reset
()
model_name
=
"jerryzh168/opt-125m-int4wo-per-module"
with
vllm_runner
(
model_name
=
model_name
,
quantization
=
"torchao"
,
dtype
=
"bfloat16"
,
pt_load_map_location
=
"cuda:0"
)
as
llm
:
output
=
llm
.
generate_greedy
([
"The capital of France is"
],
max_tokens
=
32
)
assert
output
print
(
output
)
if
__name__
==
"__main__"
:
pytest
.
main
([
__file__
])
vllm/model_executor/layers/quantization/torchao.py
View file @
79747367
...
...
@@ -5,10 +5,11 @@ import torch
import
torch.nn.functional
as
F
from
torch.nn.parameter
import
Parameter
from
vllm.model_executor.layers.linear
import
LinearBase
,
LinearMethodBase
from
vllm.model_executor.layers.linear
import
(
LinearBase
,
LinearMethodBase
,
UnquantizedLinearMethod
)
from
vllm.model_executor.layers.quantization
import
QuantizationMethods
from
vllm.model_executor.layers.quantization.base_config
import
(
QuantizationConfig
)
QuantizationConfig
,
QuantizeMethodBase
)
from
vllm.model_executor.utils
import
set_weight_attrs
...
...
@@ -55,11 +56,25 @@ class TorchAOConfig(QuantizationConfig):
return
cls
(
ao_config
)
def
get_quant_method
(
self
,
layer
:
torch
.
nn
.
Module
,
prefix
:
str
)
->
Optional
[
"TorchAOLinearMethod"
]:
if
isinstance
(
layer
,
LinearBase
):
return
TorchAOLinearMethod
(
self
)
prefix
:
str
)
->
Optional
[
"QuantizeMethodBase"
]:
if
not
isinstance
(
layer
,
LinearBase
):
return
None
from
torchao.quantization
import
AOPerModuleConfig
module_fqn
=
prefix
if
isinstance
(
self
.
torchao_config
,
AOPerModuleConfig
):
module_fqn_to_config
=
self
.
torchao_config
.
module_fqn_to_config
c
=
module_fqn_to_config
.
get
(
module_fqn
)
or
module_fqn_to_config
.
get
(
"_default"
,
None
)
if
c
is
not
None
:
current_torchao_config
=
TorchAOConfig
(
c
)
return
TorchAOLinearMethod
(
current_torchao_config
)
else
:
return
UnquantizedLinearMethod
()
return
TorchAOLinearMethod
(
self
)
def
get_scaled_act_names
(
self
)
->
list
[
str
]:
return
[]
...
...
@@ -75,7 +90,7 @@ def torchao_quantize_param_data(param: torch.Tensor,
"""
from
torchao.core.config
import
AOBaseConfig
from
torchao.quantization
import
quantize_
assert
isinstance
(
torchao_config
,
AOBaseConfig
)
assert
isinstance
(
torchao_config
,
AOBaseConfig
)
,
f
"
{
torchao_config
}
"
dummy_linear
=
torch
.
nn
.
Linear
(
param
.
shape
[
1
],
param
.
shape
[
0
],
bias
=
False
)
dummy_linear
.
weight
=
param
quantize_
(
dummy_linear
,
torchao_config
)
...
...
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