@@ -72,12 +72,35 @@ def quantize( # noqa C901
7272 if qmode == "int8" :
7373 # Add quantization mode options here: group size, bit width, etc.
7474 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" ):
7699 pattern = r"torchao:8da(\d+)w"
77100 matches = re .findall (pattern , qmode )
78101 assert len (matches ) == 1 , f"Expected 1 match for pattern but got { len (matches )} "
79102 bitwidth = int (matches [0 ][0 ])
80- _load_torchao_ops_aten ( )
103+ _load_torchao_aten_lib ( libname = "libtorchao_ops_aten" )
81104 from torchao .experimental .quant_api import Int8DynActIntxWeightLinearQuantizer
82105
83106 with torch .no_grad ():
@@ -729,7 +752,7 @@ def get_quant_embedding_transform(args):
729752 bitwidth , group_size = args .embedding_quantize .split (":" )[1 ].split ("," )
730753 group_size = int (group_size )
731754 bitwidth = int (bitwidth )
732- _load_torchao_ops_aten ( )
755+ _load_torchao_aten_lib ( libname = "libtorchao_ops_aten" )
733756 from torchao .experimental .quant_api import IntxWeightEmbeddingQuantizer
734757
735758 def _torchao_embedding_quantizer (model ):
@@ -785,15 +808,15 @@ def get_quant_weight_transform(args, dtype_override, verbose):
785808 )
786809
787810
788- def _load_torchao_ops_aten ( ):
811+ def _load_torchao_aten_lib ( libname ):
789812 import glob
790813 import os
791814
792815 libs = glob .glob (
793816 os .path .abspath (
794817 os .path .join (
795818 os .environ .get ("CMAKE_INSTALL_PREFIX" , "" ),
796- "lib/libtorchao_ops_aten .*" ,
819+ f "lib/{ libname } .*" ,
797820 )
798821 )
799822 )
0 commit comments