Skip to content

Commit 1566af4

Browse files
authored
Add files via upload
1 parent f86db9d commit 1566af4

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

examples/test-Qwen-generate.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
os.environ["KERAS_BACKEND"] = "torch"
3+
4+
max_len=300
5+
import keras
6+
keras.config.set_dtype_policy("bfloat16")
7+
from transformers import AutoTokenizer
8+
from bert4keras3.models import build_transformer_model
9+
10+
base_path = 'Qwen1.5-0.5B/'
11+
config_path = base_path+'config.json'
12+
weights_path = base_path+'QWen.weights.h5'
13+
dict_path = base_path+'qwen_tokenizer'
14+
15+
tokenizer = AutoTokenizer.from_pretrained(dict_path)
16+
17+
Qwen = build_transformer_model(
18+
config_path,
19+
keras_weights_path=weights_path,
20+
model='qwen',
21+
with_lm=True,
22+
return_keras_model=False,
23+
)
24+
25+
gemma = Qwen.model
26+
gemma.summary()
27+
import numpy as np
28+
29+
cache_model=Qwen.build_cache_model([max_len],end_token=151643,progress_print=True,search_mode='topp',k=0.7)
30+
31+
test_text = '''楼房大小的血睛鬃毛狮,力大无穷的紫睛金毛猿,毁天灭地的九头蛇皇,携带着毁灭雷电的恐怖雷龙…… '''
32+
tokens2 = tokenizer(test_text)['input_ids']
33+
#print(tokens2)
34+
tokens2=np.expand_dims(tokens2+[0]*(max_len-len(tokens2)),0)
35+
tokens2 = np.concatenate([tokens2,tokens2],0)
36+
tokens2 = cache_model.predict(tokens2)
37+
print(tokenizer.decode(tokens2[0]))
38+
print(tokenizer.decode(tokens2[1]))

examples/test-llama-generate.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import os
2+
os.environ["KERAS_BACKEND"] = "torch"
3+
4+
max_len=300
5+
import keras
6+
keras.config.set_dtype_policy("bfloat16")
7+
from transformers import AutoTokenizer
8+
from bert4keras3.models import build_transformer_model
9+
10+
11+
base_path = 'Yi-6B/'
12+
config_path = base_path+'config.json'
13+
weights_path = base_path+'model.weights.h5'
14+
dict_path = base_path+'Yi_tokenizer'
15+
16+
tokenizer = AutoTokenizer.from_pretrained(dict_path)
17+
18+
Qwen = build_transformer_model(
19+
config_path,
20+
keras_weights_path=weights_path,
21+
model='qwen',
22+
with_lm=True,
23+
return_keras_model=False,
24+
)
25+
26+
gemma = Qwen.model
27+
gemma.summary()
28+
import numpy as np
29+
#yi的end_token是2,llama3是128001,具体自己看hf的config
30+
cache_model=Qwen.build_cache_model([max_len],end_token=2,progress_print=True,search_mode='topp',k=0.7)
31+
32+
test_text = '''楼房大小的血睛鬃毛狮,力大无穷的紫睛金毛猿,毁天灭地的九头蛇皇,携带着毁灭雷电的恐怖雷龙…… '''
33+
tokens2 = tokenizer(test_text)['input_ids']
34+
print(tokens2)
35+
tokens2=np.expand_dims(tokens2+[0]*(max_len-len(tokens2)),0)
36+
tokens2 = np.concatenate([tokens2,tokens2],0)
37+
tokens2 = cache_model.predict(tokens2)
38+
print(tokenizer.decode(tokens2[0]))
39+
print(tokenizer.decode(tokens2[1]))

0 commit comments

Comments
 (0)