Upload 7 files
Browse files- added_tokens.json +4 -0
- conv.py +134 -0
- generation_config.json +8 -0
- special_tokens_map.json +34 -0
- tokenizer.json +0 -0
- tokenizer_config.json +55 -0
- vocab.json +0 -0
added_tokens.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"<|im_end|>": 100278,
|
3 |
+
"<|im_start|>": 100277
|
4 |
+
}
|
conv.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from safetensors import safe_open
|
4 |
+
from safetensors.torch import save_file
|
5 |
+
import torch
|
6 |
+
|
7 |
+
def load_index_file(index_path):
|
8 |
+
with open(index_path, 'r') as f:
|
9 |
+
return json.load(f)
|
10 |
+
|
11 |
+
def save_index_file(index_path, index_data):
|
12 |
+
with open(index_path, 'w') as f:
|
13 |
+
json.dump(index_data, f, indent=2)
|
14 |
+
|
15 |
+
def load_tensor(file_path, tensor_name):
|
16 |
+
with safe_open(file_path, framework="pt") as f:
|
17 |
+
return f.get_tensor(tensor_name)
|
18 |
+
|
19 |
+
def save_tensor(file_path, tensor_data):
|
20 |
+
save_file(tensor_data, file_path)
|
21 |
+
|
22 |
+
def get_tensor_location(tensor_index, tensor_name):
|
23 |
+
if tensor_name in tensor_index['weight_map']:
|
24 |
+
return tensor_index['weight_map'][tensor_name], tensor_name
|
25 |
+
return None, None
|
26 |
+
|
27 |
+
def concatenate_experts(input_path, fine_tuned_index, block_number, tensor_base_name, tensor_fine_name, temp_folder):
|
28 |
+
experts_tensors = []
|
29 |
+
for expert_number in range(16):
|
30 |
+
tensor_name = tensor_fine_name.replace('%b', str(block_number)).replace('%e', str(expert_number))
|
31 |
+
shard_file, tensor_location = get_tensor_location(fine_tuned_index, tensor_name)
|
32 |
+
if shard_file is None:
|
33 |
+
raise ValueError(f"Tensor {tensor_name} not found in the index.")
|
34 |
+
tensor_data = load_tensor(os.path.join(input_path, shard_file), tensor_location)
|
35 |
+
if tensor_fine_name.endswith(".w2.weight"):
|
36 |
+
tensor_data = tensor_data.transpose(0, 1) # Reverse the dimensions
|
37 |
+
experts_tensors.append(tensor_data)
|
38 |
+
|
39 |
+
concatenated_tensor = torch.cat(experts_tensors, dim=0) # Concatenate along the correct dimension
|
40 |
+
temp_file_path = os.path.join(temp_folder, f"temp_concatenated_{block_number}_{tensor_base_name}.pt")
|
41 |
+
torch.save(concatenated_tensor, temp_file_path)
|
42 |
+
return temp_file_path
|
43 |
+
|
44 |
+
def save_sharded_tensors(output_path, tensors, shard_size=5 * 1024 * 1024 * 1024): # 5GB limit
|
45 |
+
shard_data = {}
|
46 |
+
shard_count = 1 # Start counting from 1
|
47 |
+
current_shard_size = 0
|
48 |
+
shard_index = []
|
49 |
+
tensor_to_shard_map = {}
|
50 |
+
|
51 |
+
for tensor_name, tensor_file_path in tensors.items():
|
52 |
+
tensor_data = torch.load(tensor_file_path)
|
53 |
+
tensor_size = tensor_data.numel() * tensor_data.element_size() # Calculate tensor size
|
54 |
+
if current_shard_size + tensor_size > shard_size:
|
55 |
+
shard_filename = f"model-{shard_count:05d}-of-00061.safetensors"
|
56 |
+
save_tensor(os.path.join(output_path, shard_filename), shard_data)
|
57 |
+
shard_index.append({
|
58 |
+
'filename': shard_filename,
|
59 |
+
'tensors': {name: {'shape': tensor.shape, 'dtype': str(tensor.dtype), 'offset': 0} for name, tensor in shard_data.items()}
|
60 |
+
})
|
61 |
+
shard_data = {}
|
62 |
+
shard_count += 1
|
63 |
+
current_shard_size = 0
|
64 |
+
|
65 |
+
shard_data[tensor_name] = tensor_data
|
66 |
+
tensor_to_shard_map[tensor_name] = f"model-{shard_count:05d}-of-00061.safetensors"
|
67 |
+
current_shard_size += tensor_size
|
68 |
+
|
69 |
+
if shard_data:
|
70 |
+
shard_filename = f"model-{shard_count:05d}-of-00061.safetensors"
|
71 |
+
save_tensor(os.path.join(output_path, shard_filename), shard_data)
|
72 |
+
shard_index.append({
|
73 |
+
'filename': shard_filename,
|
74 |
+
'tensors': {name: {'shape': tensor.shape, 'dtype': str(tensor.dtype), 'offset': 0} for name, tensor in shard_data.items()}
|
75 |
+
})
|
76 |
+
# Ensure the last shard is correctly mapped
|
77 |
+
for tensor_name in shard_data.keys():
|
78 |
+
tensor_to_shard_map[tensor_name] = shard_filename
|
79 |
+
|
80 |
+
return shard_index, tensor_to_shard_map
|
81 |
+
|
82 |
+
def transform_model(input_path, fine_tuned_index_path, output_path, output_index_path, temp_folder):
|
83 |
+
fine_tuned_index = load_index_file(fine_tuned_index_path)
|
84 |
+
|
85 |
+
transformed_tensors = {}
|
86 |
+
|
87 |
+
for block_number in range(40):
|
88 |
+
for tensor_base_name, tensor_fine_name in [
|
89 |
+
("transformer.blocks.%b.ffn.experts.mlp.v1", "transformer.blocks.%b.ffn.experts.mlp_experts.%e.v1.weight"),
|
90 |
+
("transformer.blocks.%b.ffn.experts.mlp.w1", "transformer.blocks.%b.ffn.experts.mlp_experts.%e.w1.weight"),
|
91 |
+
("transformer.blocks.%b.ffn.experts.mlp.w2", "transformer.blocks.%b.ffn.experts.mlp_experts.%e.w2.weight"),
|
92 |
+
]:
|
93 |
+
tensor_name = tensor_base_name.replace('%b', str(block_number))
|
94 |
+
concatenated_tensor_path = concatenate_experts(input_path, fine_tuned_index, block_number, tensor_base_name, tensor_fine_name, temp_folder)
|
95 |
+
transformed_tensors[tensor_name] = concatenated_tensor_path
|
96 |
+
|
97 |
+
# For other tensors, directly copy from fine-tuned model
|
98 |
+
for tensor_name in [
|
99 |
+
"lm_head.weight",
|
100 |
+
"transformer.blocks.%b.ffn.router.layer.weight",
|
101 |
+
"transformer.blocks.%b.norm_attn_norm.attn.Wqkv.weight",
|
102 |
+
"transformer.blocks.%b.norm_attn_norm.attn.out_proj.weight",
|
103 |
+
"transformer.blocks.%b.norm_attn_norm.norm_1.weight",
|
104 |
+
"transformer.blocks.%b.norm_attn_norm.norm_2.weight",
|
105 |
+
"transformer.norm_f.weight",
|
106 |
+
"transformer.wte.weight"
|
107 |
+
]:
|
108 |
+
tensor_name = tensor_name.replace('%b', str(block_number))
|
109 |
+
shard_file, tensor_location = get_tensor_location(fine_tuned_index, tensor_name)
|
110 |
+
if shard_file is None:
|
111 |
+
raise ValueError(f"Tensor {tensor_name} not found in the index.")
|
112 |
+
tensor_data = load_tensor(os.path.join(input_path, shard_file), tensor_location)
|
113 |
+
temp_file_path = os.path.join(temp_folder, f"temp_{tensor_name}.pt")
|
114 |
+
torch.save(tensor_data, temp_file_path)
|
115 |
+
transformed_tensors[tensor_name] = temp_file_path
|
116 |
+
|
117 |
+
# Save transformed tensors into shards
|
118 |
+
shard_index, tensor_to_shard_map = save_sharded_tensors(output_path, transformed_tensors)
|
119 |
+
|
120 |
+
# Update and save the index file
|
121 |
+
fine_tuned_index['weight_map'] = tensor_to_shard_map
|
122 |
+
save_index_file(output_index_path, fine_tuned_index)
|
123 |
+
|
124 |
+
# Paths
|
125 |
+
input_path = '/path/cognitivecomputations_dolphin-2.9.1-dbrx/'
|
126 |
+
fine_tuned_index_path = '/path/cognitivecomputations_dolphin-2.9.1-dbrx/model.safetensors.index.json'
|
127 |
+
output_index_path = '/path/dolphin-2.9.1-dbrx-llamacppfix/model.safetensors.index.json'
|
128 |
+
output_path = '/path/output/' # make sure there is 250GB free in there
|
129 |
+
temp_folder = '/path/temp/' # make sure there is 250GB free in there
|
130 |
+
|
131 |
+
# Ensure the temp_folder exists
|
132 |
+
os.makedirs(temp_folder, exist_ok=True)
|
133 |
+
|
134 |
+
transform_model(input_path, fine_tuned_index_path, output_path, output_index_path, temp_folder)
|
generation_config.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"eos_token_id": [
|
4 |
+
100257,
|
5 |
+
100279
|
6 |
+
],
|
7 |
+
"transformers_version": "4.40.0.dev0"
|
8 |
+
}
|
special_tokens_map.json
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"additional_special_tokens": [
|
3 |
+
"<|im_start|>",
|
4 |
+
"<|im_end|>"
|
5 |
+
],
|
6 |
+
"bos_token": {
|
7 |
+
"content": "<|endoftext|>",
|
8 |
+
"lstrip": false,
|
9 |
+
"normalized": false,
|
10 |
+
"rstrip": false,
|
11 |
+
"single_word": false
|
12 |
+
},
|
13 |
+
"eos_token": {
|
14 |
+
"content": "<|im_end|>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": false,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false
|
19 |
+
},
|
20 |
+
"pad_token": {
|
21 |
+
"content": "<|pad|>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": false,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false
|
26 |
+
},
|
27 |
+
"unk_token": {
|
28 |
+
"content": "<|endoftext|>",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false
|
33 |
+
}
|
34 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": false,
|
3 |
+
"add_eos_token": false,
|
4 |
+
"add_prefix_space": false,
|
5 |
+
"added_tokens_decoder": {
|
6 |
+
"100257": {
|
7 |
+
"content": "<|endoftext|>",
|
8 |
+
"lstrip": false,
|
9 |
+
"normalized": false,
|
10 |
+
"rstrip": false,
|
11 |
+
"single_word": false,
|
12 |
+
"special": true
|
13 |
+
},
|
14 |
+
"100277": {
|
15 |
+
"content": "<|im_start|>",
|
16 |
+
"lstrip": false,
|
17 |
+
"normalized": false,
|
18 |
+
"rstrip": false,
|
19 |
+
"single_word": false,
|
20 |
+
"special": true
|
21 |
+
},
|
22 |
+
"100278": {
|
23 |
+
"content": "<|im_end|>",
|
24 |
+
"lstrip": false,
|
25 |
+
"normalized": false,
|
26 |
+
"rstrip": false,
|
27 |
+
"single_word": false,
|
28 |
+
"special": true
|
29 |
+
},
|
30 |
+
"100279": {
|
31 |
+
"content": "<|pad|>",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": false,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false,
|
36 |
+
"special": true
|
37 |
+
}
|
38 |
+
},
|
39 |
+
"additional_special_tokens": [
|
40 |
+
"<|im_start|>",
|
41 |
+
"<|im_end|>"
|
42 |
+
],
|
43 |
+
"bos_token": "<|endoftext|>",
|
44 |
+
"chat_template": "{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
|
45 |
+
"clean_up_tokenization_spaces": true,
|
46 |
+
"encoding_name": null,
|
47 |
+
"eos_token": "<|im_end|>",
|
48 |
+
"errors": "replace",
|
49 |
+
"model_max_length": 1000000000000000019884624838656,
|
50 |
+
"model_name": "gpt-4",
|
51 |
+
"pad_token": "<|pad|>",
|
52 |
+
"tokenizer_class": "GPT2Tokenizer",
|
53 |
+
"unk_token": "<|endoftext|>",
|
54 |
+
"use_default_system_prompt": false
|
55 |
+
}
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|