MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / quantize

Function quantize

accessory/util/quant.py:95–163  ·  view source on GitHub ↗
(
        model : MetaModel,
        quant_conf : BitsAndBytesConfig,
)

Source from the content-addressed store, hash-verified

93 return output
94
95def quantize(
96 model : MetaModel,
97 quant_conf : BitsAndBytesConfig,
98):
99 module_list = [_ for _ in model.named_modules() if isinstance(_[1],
100 (LoraColumnParallelLinear, LoraRowParallelLinear,
101 ColumnParallelLinear, RowParallelLinear, torch.nn.Linear))]
102 quant_blocklist = model.get_quant_blocklist()
103
104 for name, module in tqdm(module_list, desc="Qunatization Process"):
105 if "lora" in name or name in quant_blocklist:
106 continue
107 if isinstance(module, (
108 LoraColumnParallelLinear,
109 LoraRowParallelLinear,
110 ColumnParallelLinear,
111 RowParallelLinear,
112 torch.nn.Linear
113 )):
114 # 1. Initialize quantization operator
115 if quant_conf.load_in_4bit:
116 quanted_layer = bnb.nn.Linear4bit(
117 module.in_features,
118 module.out_features,
119 bias=None,
120 compute_dtype=quant_conf.bnb_4bit_compute_dtype,
121 compress_statistics=True,
122 device=None)
123 if quant_conf.bnb_4bit_compute_dtype != None:
124 quanted_layer.compute_type_is_set = True
125
126 quanted_layer.weight = bnb.nn.Params4bit(
127 module.weight.data.clone(),
128 requires_grad=False,
129 quant_type=quant_conf.bnb_4bit_quant_type,
130 )
131
132 elif quant_conf.load_in_8bit:
133 quanted_layer= bnb.nn.Linear8bitLt(
134 module.in_features,
135 module.out_features,
136 bias=None,
137 has_fp16_weights=quant_conf.llm_int8_has_fp16_weight,
138 threshold=quant_conf.llm_int8_threshold,
139 )
140 quanted_layer.weight = bnb.nn.Int8Params(
141 module.weight.data.clone(),
142 requires_grad=False,
143 #has_fp16_weights=quant_conf.llm_int8_has_fp16_weight,
144 )
145 else:
146 raise NotImplementedError(f'Please determine the proper quantization type.')
147
148 # 2. Convert FP layer to quantized layer
149 module.quanted_layer = quanted_layer
150
151 if isinstance(module, LoraColumnParallelLinear):
152 forward_func = forward_LoraColumnParallelLinear

Callers 15

loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
loadFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
from_pretrainedMethod · 0.90

Calls 1

get_quant_blocklistMethod · 0.45

Tested by

no test coverage detected