Add falcon gguf (#33437)
* feat(gguf): add falcon q2 k * fix(gguf): remove useless renaming * feat(gguf): seperate falcon 7b and 40b * feat(gguf): apply fixup * fix(test): error rebase * feat(gguf): add fp16 weight comparison for falcon * feat(gguf): test weight of all layers * test(gguf): add falcon 40b under skip decorator * feat(gguf): quick example for extracting model size
This commit is contained in:
@@ -81,6 +81,7 @@ For now the supported model architectures are the architectures that have been v
|
|||||||
- Qwen2Moe
|
- Qwen2Moe
|
||||||
- Phi3
|
- Phi3
|
||||||
- Bloom
|
- Bloom
|
||||||
|
- Falcon
|
||||||
|
|
||||||
## Example usage
|
## Example usage
|
||||||
|
|
||||||
|
|||||||
@@ -345,7 +345,6 @@ class GPT2Converter(Converter):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
add_prefix_space = False
|
|
||||||
add_prefix_space = getattr(self.original_tokenizer, "add_prefix_space", False)
|
add_prefix_space = getattr(self.original_tokenizer, "add_prefix_space", False)
|
||||||
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=add_prefix_space)
|
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=add_prefix_space)
|
||||||
tokenizer.decoder = decoders.ByteLevel()
|
tokenizer.decoder = decoders.ByteLevel()
|
||||||
|
|||||||
@@ -120,6 +120,29 @@ GGUF_TENSOR_MAPPING = {
|
|||||||
"output.weight": "lm_head.weight",
|
"output.weight": "lm_head.weight",
|
||||||
"output_norm": "transformer.ln_f",
|
"output_norm": "transformer.ln_f",
|
||||||
},
|
},
|
||||||
|
"falcon7b": {
|
||||||
|
"token_embd": "word_embeddings",
|
||||||
|
"blk": "h",
|
||||||
|
"ffn_up": "mlp.dense_h_to_4h",
|
||||||
|
"ffn_down": "mlp.dense_4h_to_h",
|
||||||
|
"attn_norm": "input_layernorm",
|
||||||
|
"attn_qkv": "self_attention.query_key_value",
|
||||||
|
"attn_output": "self_attention.dense",
|
||||||
|
".output.": ".lm_head.",
|
||||||
|
"output_norm": "ln_f",
|
||||||
|
},
|
||||||
|
"falcon40b": {
|
||||||
|
"token_embd": "word_embeddings",
|
||||||
|
"blk": "h",
|
||||||
|
"ffn_up": "mlp.dense_h_to_4h",
|
||||||
|
"ffn_down": "mlp.dense_4h_to_h",
|
||||||
|
".attn_norm.": ".ln_mlp.",
|
||||||
|
"attn_norm_2": "ln_attn",
|
||||||
|
"attn_qkv": "self_attention.query_key_value",
|
||||||
|
"attn_output": "self_attention.dense",
|
||||||
|
".output.": ".lm_head.",
|
||||||
|
"output_norm": "ln_f",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -178,6 +201,18 @@ GGUF_CONFIG_MAPPING = {
|
|||||||
"attention.layer_norm_rms_epsilon": "rms_norm_eps",
|
"attention.layer_norm_rms_epsilon": "rms_norm_eps",
|
||||||
"vocab_size": "vocab_size",
|
"vocab_size": "vocab_size",
|
||||||
},
|
},
|
||||||
|
"falcon": {
|
||||||
|
"context_length": "max_position_embeddings",
|
||||||
|
"block_count": "num_hidden_layers",
|
||||||
|
"feed_forward_length": "intermediate_size",
|
||||||
|
"embedding_length": "hidden_size",
|
||||||
|
"rope.dimension_count": None,
|
||||||
|
"rope.freq_base": "rope_theta",
|
||||||
|
"attention.head_count": "num_attention_heads",
|
||||||
|
"attention.head_count_kv": "num_key_value_heads",
|
||||||
|
"attention.layer_norm_rms_epsilon": "rms_norm_eps",
|
||||||
|
"vocab_size": "vocab_size",
|
||||||
|
},
|
||||||
"tokenizer": {
|
"tokenizer": {
|
||||||
"ggml.bos_token_id": "bos_token_id",
|
"ggml.bos_token_id": "bos_token_id",
|
||||||
"ggml.eos_token_id": "eos_token_id",
|
"ggml.eos_token_id": "eos_token_id",
|
||||||
@@ -530,6 +565,7 @@ GGUF_TO_FAST_CONVERTERS = {
|
|||||||
"qwen2_moe": GGUFQwen2Converter,
|
"qwen2_moe": GGUFQwen2Converter,
|
||||||
"phi3": GGUFPhi3Converter,
|
"phi3": GGUFPhi3Converter,
|
||||||
"bloom": GGUFBloomConverter,
|
"bloom": GGUFBloomConverter,
|
||||||
|
"falcon": GGUFBloomConverter,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import re
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -99,8 +100,20 @@ def load_gguf_checkpoint(gguf_checkpoint_path, return_tensors=False):
|
|||||||
if "qwen2moe" in architecture:
|
if "qwen2moe" in architecture:
|
||||||
updated_architecture = "qwen2_moe"
|
updated_architecture = "qwen2_moe"
|
||||||
|
|
||||||
if architecture not in GGUF_SUPPORTED_ARCHITECTURES:
|
model_size = ""
|
||||||
raise ValueError(f"Architecture {architecture} not supported")
|
# extract the number of params from file name as architectures can differ ;
|
||||||
|
# eg. for falcon : `...falcon-7b-...`
|
||||||
|
if "falcon" in architecture:
|
||||||
|
gguf_file_name = gguf_checkpoint_path.split("/")[-1].lower()
|
||||||
|
m = re.search(r"-\d+b-", gguf_file_name) # regex to catch `-7b-`
|
||||||
|
if m is None:
|
||||||
|
raise ValueError(
|
||||||
|
f"From file name, cannot determine the number of parameters for {architecture} architecture"
|
||||||
|
)
|
||||||
|
model_size = m.group().strip("-") # only keeps `7b`
|
||||||
|
|
||||||
|
if architecture + model_size not in GGUF_SUPPORTED_ARCHITECTURES:
|
||||||
|
raise ValueError(f"Architecture {architecture + model_size} not supported")
|
||||||
|
|
||||||
# List all key-value pairs in a columnized format
|
# List all key-value pairs in a columnized format
|
||||||
for gguf_key, field in reader.fields.items():
|
for gguf_key, field in reader.fields.items():
|
||||||
@@ -146,17 +159,9 @@ def load_gguf_checkpoint(gguf_checkpoint_path, return_tensors=False):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if return_tensors:
|
if return_tensors:
|
||||||
tensor_key_mapping = GGUF_TO_TRANSFORMERS_MAPPING["tensors"][architecture]
|
tensor_key_mapping = GGUF_TO_TRANSFORMERS_MAPPING["tensors"][architecture + model_size]
|
||||||
|
|
||||||
for tensor in tqdm(reader.tensors, desc="Converting and de-quantizing GGUF tensors..."):
|
for tensor in tqdm(reader.tensors, desc="Converting and de-quantizing GGUF tensors..."):
|
||||||
renamed_tensor_name = tensor.name
|
|
||||||
|
|
||||||
for tensor_name_mapping in GGUF_TO_TRANSFORMERS_MAPPING["tensors"]:
|
|
||||||
if tensor_name_mapping in renamed_tensor_name:
|
|
||||||
renamed_tensor_name = renamed_tensor_name.replace(
|
|
||||||
tensor_name_mapping, GGUF_TO_TRANSFORMERS_MAPPING["tensors"][tensor_name_mapping]
|
|
||||||
)
|
|
||||||
|
|
||||||
name = tensor.name
|
name = tensor.name
|
||||||
|
|
||||||
weights = dequantize(tensor.data, tensor.tensor_type)
|
weights = dequantize(tensor.data, tensor.tensor_type)
|
||||||
|
|||||||
@@ -44,6 +44,9 @@ class GgufIntegrationTests(unittest.TestCase):
|
|||||||
phi3_model_id = "microsoft/Phi-3-mini-4k-instruct-gguf"
|
phi3_model_id = "microsoft/Phi-3-mini-4k-instruct-gguf"
|
||||||
bloom_model_id = "afrideva/bloom-560m-GGUF"
|
bloom_model_id = "afrideva/bloom-560m-GGUF"
|
||||||
original_bloom_model_id = "bigscience/bloom-560m"
|
original_bloom_model_id = "bigscience/bloom-560m"
|
||||||
|
falcon7b_model_id = "xaviviro/falcon-7b-quantized-gguf"
|
||||||
|
falcon40b_model_id = "maddes8cht/tiiuae-falcon-40b-gguf"
|
||||||
|
original_flacon7b_model_id = "tiiuae/falcon-7b"
|
||||||
|
|
||||||
# standard quants
|
# standard quants
|
||||||
q4_0_gguf_model_id = "tinyllama-1.1b-chat-v1.0.Q4_0.gguf"
|
q4_0_gguf_model_id = "tinyllama-1.1b-chat-v1.0.Q4_0.gguf"
|
||||||
@@ -74,6 +77,9 @@ class GgufIntegrationTests(unittest.TestCase):
|
|||||||
fp16_bloom_model_id = "bloom-560m.fp16.gguf"
|
fp16_bloom_model_id = "bloom-560m.fp16.gguf"
|
||||||
q8_bloom_model_id = "bloom-560m.q8_0.gguf"
|
q8_bloom_model_id = "bloom-560m.q8_0.gguf"
|
||||||
f16_tinyllama_model_id = "TinyLlama-1.1B-Chat-v1.0.FP16.gguf"
|
f16_tinyllama_model_id = "TinyLlama-1.1B-Chat-v1.0.FP16.gguf"
|
||||||
|
q2_k_falcon7b_model_id = "falcon-7b-q2_k.gguf"
|
||||||
|
fp16_falcon7b_model_id = "falcon-7b-fp16.gguf"
|
||||||
|
q2_k_falcon40b_model_id = "tiiuae-falcon-40b-Q2_K.gguf"
|
||||||
|
|
||||||
example_text = "Hello"
|
example_text = "Hello"
|
||||||
|
|
||||||
@@ -445,6 +451,58 @@ class GgufIntegrationTests(unittest.TestCase):
|
|||||||
self.assertTrue(quantized_param.shape == original_param.shape)
|
self.assertTrue(quantized_param.shape == original_param.shape)
|
||||||
torch.testing.assert_close(quantized_param, original_param)
|
torch.testing.assert_close(quantized_param, original_param)
|
||||||
|
|
||||||
|
@unittest.skip(reason="Heavy memory")
|
||||||
|
def test_falcon40b_q2_k(self):
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(self.falcon40b_model_id, gguf_file=self.q2_k_falcon40b_model_id)
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
self.falcon40b_model_id,
|
||||||
|
gguf_file=self.q2_k_falcon40b_model_id,
|
||||||
|
device_map="auto",
|
||||||
|
torch_dtype=torch.float16,
|
||||||
|
)
|
||||||
|
|
||||||
|
text = tokenizer(self.example_text, return_tensors="pt").to(torch_device)
|
||||||
|
out = model.generate(**text, max_new_tokens=10)
|
||||||
|
|
||||||
|
EXPECTED_TEXT = "Hello All,\nI am new to this forum."
|
||||||
|
self.assertEqual(tokenizer.decode(out[0], skip_special_tokens=True), EXPECTED_TEXT)
|
||||||
|
|
||||||
|
def test_falcon7b_q2_k(self):
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(self.falcon7b_model_id, gguf_file=self.q2_k_falcon7b_model_id)
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
self.falcon7b_model_id,
|
||||||
|
gguf_file=self.q2_k_falcon7b_model_id,
|
||||||
|
device_map="auto",
|
||||||
|
torch_dtype=torch.float16,
|
||||||
|
)
|
||||||
|
|
||||||
|
text = tokenizer(self.example_text, return_tensors="pt").to(torch_device)
|
||||||
|
out = model.generate(**text, max_new_tokens=10)
|
||||||
|
|
||||||
|
EXPECTED_TEXT = "Hello All,\nI am new to this forum."
|
||||||
|
self.assertEqual(tokenizer.decode(out[0], skip_special_tokens=True), EXPECTED_TEXT)
|
||||||
|
|
||||||
|
def test_falcon7b_weights_conversion_fp16(self):
|
||||||
|
quantized_model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
self.falcon7b_model_id,
|
||||||
|
gguf_file=self.fp16_falcon7b_model_id,
|
||||||
|
device_map="auto",
|
||||||
|
torch_dtype=torch.float16,
|
||||||
|
)
|
||||||
|
original_model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
self.original_flacon7b_model_id,
|
||||||
|
device_map="auto",
|
||||||
|
torch_dtype=torch.float16,
|
||||||
|
)
|
||||||
|
|
||||||
|
quantized_state_dict = quantized_model.state_dict()
|
||||||
|
original_state_dict = original_model.state_dict()
|
||||||
|
|
||||||
|
for layer_name, original_params in original_state_dict.items():
|
||||||
|
if layer_name in quantized_state_dict:
|
||||||
|
self.assertTrue(original_params.shape == quantized_state_dict[layer_name].shape)
|
||||||
|
torch.testing.assert_close(original_params, quantized_state_dict[layer_name])
|
||||||
|
|
||||||
def test_tokenization_xnli(self):
|
def test_tokenization_xnli(self):
|
||||||
import tqdm
|
import tqdm
|
||||||
from datasets import load_dataset
|
from datasets import load_dataset
|
||||||
|
|||||||
Reference in New Issue
Block a user