benchang1110 commited on
Commit
3432a0c
·
verified ·
1 Parent(s): e858caa

upload host_weight folder

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ host_weight/Whead_fp.csv filter=lfs diff=lfs merge=lfs -text
37
+ host_weight/emb.csv filter=lfs diff=lfs merge=lfs -text
host_weight/Makefile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ PHONY:
2
+
3
+ # path to original model weights
4
+ MODEL_DIR = ../../quant/hf_weight
5
+
6
+ # file names
7
+ EMB = emb
8
+ WHEAD = Whead_fp
9
+
10
+ clean:
11
+ rm -f *.csv
12
+ rm -f *.bin
13
+
14
+ gen_csv:
15
+ rm -f *.csv
16
+ python gen_csv.py --model_dir $(MODEL_DIR) --emb $(EMB) --whead $(WHEAD)
17
+
18
+ # wait for several seconds
19
+ gen_bin:
20
+ rm -f *.bin
21
+ python gen_bin.py --emb $(EMB) --whead $(WHEAD)
host_weight/README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 產生 embed 和 lm_head
2
+
3
+ ### embed
4
+ 流程: .safetensors -> emb.csv -> emb.bin -> emb_elem.bin & emb_scale.bin
5
+
6
+ 最後一個步驟要利用 host_code 當中的 mxq_emb 來產生 emb_elem.bin 和 emb_scale.bin
7
+ 但目前 demo 所使用的 cpp 檔為 llm_acc_host.cpp 暫時用不到 emb_elem.bin 和 emb_scale.bin
8
+
9
+ ### lm_head
10
+ 流程: .safetensors -> Whead_fp.csv -> Whead_fp.bin
host_weight/Whead_fp.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:89ad3853f57938a4634a960a2f0b0b4f1887699aefc338d0317fd0b8333d457f
3
+ size 524288000
host_weight/Whead_fp.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a2fa11f326f0c477af049ab46bacba72470e40215066ff42ab7a96f6e8f2a26
3
+ size 3342321439
host_weight/emb.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b4f79a4374a649dfd30b70325c80bf824d12ca9b826a278706d4392a73e7f597
3
+ size 524288000
host_weight/emb.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f94761a5565853c25ebda677de48fdc52a69a0acc82922a3c90979dd6f2264ed
3
+ size 3342488234
host_weight/gen_bin.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import struct
3
+ import argparse
4
+
5
+ parser = argparse.ArgumentParser(description="Generate binary files from CSV embeddings and weights.")
6
+ parser.add_argument("--emb", type=str, default="emb", help="Path to the embedding CSV file.")
7
+ parser.add_argument("--whead", type=str, default="Whead_fp", help="Path to the Whead CSV file.")
8
+ args = parser.parse_args()
9
+
10
+ if __name__ == '__main__':
11
+ emb_path = args.emb + ".csv"
12
+ head_path = args.whead + ".csv"
13
+
14
+ emb = np.loadtxt(emb_path, delimiter=',')
15
+ Whead = np.loadtxt(head_path, delimiter=',')
16
+
17
+ print(emb.shape)
18
+ print(Whead.shape)
19
+
20
+ emb = emb.flatten()
21
+ Whead = Whead.flatten()
22
+
23
+ with open(args.emb + ".bin", 'wb') as f:
24
+ for data in emb:
25
+ binary_data = struct.pack('f', data)
26
+ f.write(binary_data)
27
+
28
+ with open(args.whead + ".bin", 'wb') as f:
29
+ for data in Whead:
30
+ binary_data = struct.pack('f', data)
31
+ f.write(binary_data)
host_weight/gen_csv.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM
2
+ import argparse
3
+ import numpy as np
4
+ import os
5
+
6
+ def write_csv_chunked(path, array, chunk_rows=1024):
7
+ with open(path, "w") as f:
8
+ n_rows = array.shape[0]
9
+ for start in range(0, n_rows, chunk_rows):
10
+ end = min(start + chunk_rows, n_rows)
11
+ np.savetxt(f, array[start:end], delimiter=",")
12
+
13
+ if __name__ == "__main__":
14
+ parser = argparse.ArgumentParser(description="Generate CSV file with model information.")
15
+ parser.add_argument("--model_dir", type=str, default="../../quant/hf_weight", help="Directory to save the model files.")
16
+ parser.add_argument("--emb", type=str, default="emb", help="Path to save the embedding CSV file.")
17
+ parser.add_argument("--whead", type=str, default="Whead_fp", help="Path to save the Whead CSV file.")
18
+ args = parser.parse_args()
19
+ model = AutoModelForCausalLM.from_pretrained(args.model_dir)
20
+
21
+ emb = model.model.embed_tokens.weight.detach().cpu().numpy() # (V, H)
22
+ whead = model.lm_head.weight.detach().cpu().numpy()
23
+
24
+ emb_csv = args.emb + ".csv"
25
+ write_csv_chunked(emb_csv, emb)
26
+ print(f"Saved embeddings to {emb_csv}.")
27
+ whead_csv = args.whead + ".csv"
28
+ write_csv_chunked(whead_csv, whead)
29
+ print(f"Saved Whead to {whead_csv}.")
30
+ print("Conversion to binary completed.")