@@ -72,12 +72,35 @@ def quantize( # noqa C901
72
72
if qmode == "int8" :
73
73
# Add quantization mode options here: group size, bit width, etc.
74
74
return WeightOnlyInt8QuantHandler (model ).quantized_model ()
75
- elif qmode .startswith ("torchao:" ):
75
+ elif qmode .startswith ("torchao:fpa" ):
76
+ pattern = r"torchao:fpa(\d+)w"
77
+ matches = re .findall (pattern , qmode )
78
+ assert len (matches ) == 1 , f"Expected 1 match for pattern but got { len (matches )} "
79
+ bitwidth = int (matches [0 ][0 ])
80
+ _load_torchao_aten_lib (libname = "libtorchao_ops_mps_aten" )
81
+ from torchao .experimental .quant_api import UIntxWeightOnlyLinearQuantizer
82
+
83
+ with torch .no_grad ():
84
+ model = (
85
+ UIntxWeightOnlyLinearQuantizer (
86
+ device = "mps" ,
87
+ precision = torch .float32 ,
88
+ groupsize = group_size ,
89
+ bitwidth = bitwidth ,
90
+ )
91
+ .quantize (model )
92
+ .to ("cpu" )
93
+ )
94
+
95
+ if verbose :
96
+ print ("quantized model:" , model )
97
+ return model
98
+ elif qmode .startswith ("torchao:8da" ):
76
99
pattern = r"torchao:8da(\d+)w"
77
100
matches = re .findall (pattern , qmode )
78
101
assert len (matches ) == 1 , f"Expected 1 match for pattern but got { len (matches )} "
79
102
bitwidth = int (matches [0 ][0 ])
80
- _load_torchao_ops_aten ( )
103
+ _load_torchao_aten_lib ( libname = "libtorchao_ops_aten" )
81
104
from torchao .experimental .quant_api import Int8DynActIntxWeightLinearQuantizer
82
105
83
106
with torch .no_grad ():
@@ -729,7 +752,7 @@ def get_quant_embedding_transform(args):
729
752
bitwidth , group_size = args .embedding_quantize .split (":" )[1 ].split ("," )
730
753
group_size = int (group_size )
731
754
bitwidth = int (bitwidth )
732
- _load_torchao_ops_aten ( )
755
+ _load_torchao_aten_lib ( libname = "libtorchao_ops_aten" )
733
756
from torchao .experimental .quant_api import IntxWeightEmbeddingQuantizer
734
757
735
758
def _torchao_embedding_quantizer (model ):
@@ -785,15 +808,15 @@ def get_quant_weight_transform(args, dtype_override, verbose):
785
808
)
786
809
787
810
788
- def _load_torchao_ops_aten ( ):
811
+ def _load_torchao_aten_lib ( libname ):
789
812
import glob
790
813
import os
791
814
792
815
libs = glob .glob (
793
816
os .path .abspath (
794
817
os .path .join (
795
818
os .environ .get ("CMAKE_INSTALL_PREFIX" , "" ),
796
- "lib/libtorchao_ops_aten .*" ,
819
+ f "lib/{ libname } .*" ,
797
820
)
798
821
)
799
822
)
0 commit comments