Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f06ccf

Browse files
authored
qwen2_awq_support (OpenNMT#1952)
1 parent fd5aef6 commit 2f06ccf

1 file changed

Lines changed: 54 additions & 9 deletions

File tree

python/ctranslate2/converters/transformers.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,28 @@ def get_model_spec(self, model):
21822182
rotary_scaling_type = None
21832183
rotary_scaling_factor = 1
21842184

2185+
# Check for AWQ quantization config
2186+
quantization_config = getattr(model.config, "quantization_config", None)
2187+
if quantization_config:
2188+
quant_type = None
2189+
if quantization_config.quant_method == "awq":
2190+
quant_type = _SUPPORTED_QUANTIZATION.get(quantization_config.version)
2191+
if quant_type is None:
2192+
raise NotImplementedError(
2193+
"Quantization type '%s' is not yet implemented. "
2194+
"The following Quantization types are currently supported: %s"
2195+
% (
2196+
quantization_config.quant_method,
2197+
", ".join(_SUPPORTED_QUANTIZATION.keys()),
2198+
)
2199+
)
2200+
quant_group_size = quantization_config.group_size
2201+
quant_bits = quantization_config.bits
2202+
else:
2203+
quant_type = common_spec.Quantization.CT2
2204+
quant_group_size = None
2205+
quant_bits = None
2206+
21852207
spec = transformer_spec.TransformerDecoderModelSpec.from_config(
21862208
num_layers,
21872209
num_heads,
@@ -2195,9 +2217,12 @@ def get_model_spec(self, model):
21952217
rotary_scaling_factor=rotary_scaling_factor,
21962218
rotary_base=getattr(model.config, "rope_theta", 10000),
21972219
num_heads_kv=num_heads_kv,
2220+
quant_type=quant_type,
2221+
quant_group_size=quant_group_size,
2222+
quant_bits=quant_bits,
21982223
)
21992224

2200-
self.set_decoder(spec.decoder, model.model)
2225+
self.set_decoder(spec.decoder, model.model, quant_type)
22012226
self.set_linear(spec.decoder.projection, model.lm_head)
22022227
return spec
22032228

@@ -2227,7 +2252,7 @@ def set_config(self, config, model, tokenizer):
22272252
def set_layer_norm(self, spec, layer_norm):
22282253
spec.gamma = layer_norm.weight
22292254

2230-
def set_decoder(self, spec, module):
2255+
def set_decoder(self, spec, module, quant_type=common_spec.Quantization.CT2):
22312256
spec.scale_embeddings = False
22322257
self.set_embeddings(spec.embeddings, module.embed_tokens)
22332258
self.set_layer_norm(spec.layer_norm, module.norm)
@@ -2241,19 +2266,39 @@ def set_decoder(self, spec, module):
22412266
)
22422267

22432268
split_layers = [common_spec.LinearSpec() for _ in range(3)]
2244-
self.set_linear(split_layers[0], layer.self_attn.q_proj)
2245-
self.set_linear(split_layers[1], layer.self_attn.k_proj)
2246-
self.set_linear(split_layers[2], layer.self_attn.v_proj)
2269+
self.set_linear(
2270+
split_layers[0], layer.self_attn.q_proj, quant_type=quant_type
2271+
)
2272+
self.set_linear(
2273+
split_layers[1], layer.self_attn.k_proj, quant_type=quant_type
2274+
)
2275+
self.set_linear(
2276+
split_layers[2], layer.self_attn.v_proj, quant_type=quant_type
2277+
)
2278+
2279+
if quant_type == common_spec.Quantization.CT2:
2280+
utils.fuse_linear(layer_spec.self_attention.linear[0], split_layers)
2281+
else:
2282+
cc_dim = 1 if quant_type == common_spec.Quantization.AWQ_GEMM else 0
2283+
utils.fuse_linear_prequant(
2284+
layer_spec.self_attention.linear[0], split_layers, cc_dim
2285+
)
22472286

2248-
utils.fuse_linear(layer_spec.self_attention.linear[0], split_layers)
22492287
self.set_linear(
22502288
layer_spec.self_attention.linear[1],
22512289
layer.self_attn.o_proj,
2290+
quant_type=quant_type,
22522291
)
22532292

2254-
self.set_linear(layer_spec.ffn.linear_0, layer.mlp.gate_proj)
2255-
self.set_linear(layer_spec.ffn.linear_0_noact, layer.mlp.up_proj)
2256-
self.set_linear(layer_spec.ffn.linear_1, layer.mlp.down_proj)
2293+
self.set_linear(
2294+
layer_spec.ffn.linear_0, layer.mlp.gate_proj, quant_type=quant_type
2295+
)
2296+
self.set_linear(
2297+
layer_spec.ffn.linear_0_noact, layer.mlp.up_proj, quant_type=quant_type
2298+
)
2299+
self.set_linear(
2300+
layer_spec.ffn.linear_1, layer.mlp.down_proj, quant_type=quant_type
2301+
)
22572302

22582303
delattr(layer, "self_attn")
22592304
delattr(layer, "mlp")

0 commit comments

Comments
 (0)