@@ -72,12 +72,31 @@ 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_linear_fp_act_xbit_weight_aten" )
81+ from torchao .experimental .quant_api import UIntxWeightOnlyLinearQuantizer
82+
83+ with torch .no_grad ():
84+ model = UIntxWeightOnlyLinearQuantizer (
85+ device = "mps" ,
86+ precision = torch .float32 ,
87+ groupsize = group_size ,
88+ bitwidth = bitwidth
89+ ).quantize (model )
90+
91+ if verbose :
92+ print ("quantized model:" , model )
93+ return model
94+ elif qmode .startswith ("torchao:8da" ):
7695 pattern = r"torchao:8da(\d+)w"
7796 matches = re .findall (pattern , qmode )
7897 assert len (matches ) == 1 , f"Expected 1 match for pattern but got { len (matches )} "
7998 bitwidth = int (matches [0 ][0 ])
80- _load_torchao_ops_aten ( )
99+ _load_torchao_aten_lib ( libname = "libtorchao_ops_aten" )
81100 from torchao .experimental .quant_api import Int8DynActIntxWeightLinearQuantizer
82101
83102 with torch .no_grad ():
@@ -729,7 +748,7 @@ def get_quant_embedding_transform(args):
729748 bitwidth , group_size = args .embedding_quantize .split (":" )[1 ].split ("," )
730749 group_size = int (group_size )
731750 bitwidth = int (bitwidth )
732- _load_torchao_ops_aten ( )
751+ _load_torchao_aten_lib ( libname = "libtorchao_ops_aten" )
733752 from torchao .experimental .quant_api import IntxWeightEmbeddingQuantizer
734753
735754 def _torchao_embedding_quantizer (model ):
@@ -785,15 +804,15 @@ def get_quant_weight_transform(args, dtype_override, verbose):
785804 )
786805
787806
788- def _load_torchao_ops_aten ( ):
807+ def _load_torchao_aten_lib ( libname ):
789808 import glob
790809 import os
791810
792811 libs = glob .glob (
793812 os .path .abspath (
794813 os .path .join (
795814 os .environ .get ("CMAKE_INSTALL_PREFIX" , "" ),
796- "lib/libtorchao_ops_aten .*" ,
815+ f "lib/{ libname } .*" ,
797816 )
798817 )
799818 )
0 commit comments