npc0 commited on
Commit
70d3bb7
·
verified ·
1 Parent(s): 4a74cac

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +110 -3
README.md CHANGED
@@ -1,3 +1,110 @@
1
- ---
2
- license: llama3.1
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: meta-llama/Meta-Llama-3.1-70B-Instruct
3
+ language:
4
+ - en
5
+ - de
6
+ - fr
7
+ - it
8
+ - pt
9
+ - hi
10
+ - es
11
+ - th
12
+ library_name: transformers
13
+ license: llama3.1
14
+ pipeline_tag: text-generation
15
+ tags:
16
+ - facebook
17
+ - meta
18
+ - pytorch
19
+ - llama
20
+ - llama-3
21
+ ---
22
+
23
+ ## Model Information
24
+ The Llama 3.1 text only 41B model is pruned from Llama 3.1 instruction finetuned text only 70B
25
+ using [FLAP method](arxiv.org/abs/2312.11983).
26
+
27
+ Hyper parameters used for pruning:
28
+ ```
29
+ metrics: WIFV
30
+ structure: AL-AM
31
+ pruning_ratio: 0.5
32
+ ```
33
+
34
+ ## Limitation
35
+ This `llama3.1-41B-raw` model shows unstable output.
36
+ A finetune on instruction dataset is recommended.
37
+
38
+ The model is not supported by any library at the moment
39
+ due to its unconsistent shape between layers after pruning.
40
+
41
+ ## Usage
42
+ The model is not supported by any library at the moment,
43
+ following is a workaround.
44
+ ```python
45
+ from functools import reduce
46
+ def get_module_by_name(module, access_string):
47
+ names = access_string.split(sep='.')
48
+ return reduce(getattr, names, module)
49
+
50
+ import json
51
+ from safetensors import safe_open
52
+ from transformers import LlamaForCausalLM
53
+ class MyLlamaForCausalLM(LlamaForCausalLM):
54
+ def __init__(self, config):
55
+ super().__init__(config)
56
+ with open(os.path.join(
57
+ config._name_or_path,
58
+ "model.safetensors.index.json")) as f:
59
+ weight_map = json.load(f)
60
+ weight_map = weight_map["weight_map"]
61
+ for name, path in weight_map.items():
62
+ module_name = name.replace('.weight', '')
63
+ if '.bias' in module_name:
64
+ continue
65
+ layer = get_module_by_name(self, module_name)
66
+ with safe_open(
67
+ os.path.join(
68
+ config._name_or_path,
69
+ path), framework="pt") as f:
70
+ tensor = f.get_tensor(name)
71
+ if 'mlp.' in name or 'attn.' in name:
72
+ if tensor.shape != (layer.out_features, layer.in_features):
73
+ layer = layer.__init__(
74
+ tensor.shape[1],
75
+ tensor.shape[0],
76
+ bias=layer.bias,
77
+ dtype=layer.weight.dtype,
78
+ device=layer.weight.device)
79
+ for name, path in weight_map.items():
80
+ if 'attn.' in name:
81
+ module = get_module_by_name(
82
+ self,
83
+ '.'.join(name.split('.')[:-2]))
84
+ module.num_heads = module.q_proj.out_features // module.head_dim
85
+ module.num_key_value_heads = module.num_heads
86
+ module.num_key_value_groups = module.num_heads // module.num_key_value_heads
87
+
88
+
89
+ model = MyLlamaForCausalLM.from_pretrained(
90
+ "npc0/llama3.1-41B-raw",
91
+ torch_dtype=torch.float16,
92
+ device_map="auto"
93
+ )
94
+ tokenizer = AutoTokenizer.from_pretrained(
95
+ "FLAP/llm_weights/flap_p0.5_WIFV_ALAM_llama_70b")
96
+ model = model.eval()
97
+
98
+ messages = [
99
+ {"role": "system", "content": "You are a helpful AI assistant."},
100
+ {"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
101
+ {"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
102
+ {"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
103
+ ]
104
+
105
+ model_inputs = tokenizer.apply_chat_template(messages,
106
+ return_tensors="pt").to(model.device)
107
+ generated_ids = model.generate(model_inputs, max_new_tokens=128)
108
+ decoded = tokenizer.batch_decode(generated_ids)
109
+ print(decoded[0])
110
+ ```