Views
No views yet
1
2import torch
3from peft import PeftModel, PeftConfig
4from transformers import AutoModelForCausalLM, AutoTokenizer
5
6model_name = "cyberagent/open-calm-large"
7lora_weights = "Mizuiro-sakura/open-calm-large-finetuned-databricks-dolly"
8
9# モデルの準備
10model = AutoModelForCausalLM.from_pretrained(
11 model_name
12)
13
14# トークンナイザーの準備
15tokenizer = AutoTokenizer.from_pretrained(model_name)
16
17# LoRAモデルの準備
18model = PeftModel.from_pretrained(
19 model,
20 lora_weights,
21 adapter_name=lora_weights
22)
23
24# 評価モード
25model.eval()
26
27# プロンプトテンプレートの準備
28def generate_prompt(data_point):
29 if data_point["input"]:
30 return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
31
32### Instruction:
33{data_point["instruction"]}
34
35### Input:
36{data_point["input"]}
37
38### Response:"""
39 else:
40 return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
41
42### Instruction:
43{data_point["instruction"]}
44
45### Response:"""
46
47# テキスト生成関数の定義
48def generate(instruction,input=None,maxTokens=256):
49 # 推論
50 prompt = generate_prompt({'instruction':instruction,'input':input})
51 input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids
52 outputs = model.generate(
53 input_ids=input_ids,
54 max_new_tokens=maxTokens,
55 do_sample=True,
56 temperature=0.7,
57 top_p=0.75,
58 top_k=40,
59 no_repeat_ngram_size=2,
60 )
61 outputs = outputs[0].tolist()
62
63 # EOSトークンにヒットしたらデコード完了
64 if tokenizer.eos_token_id in outputs:
65 eos_index = outputs.index(tokenizer.eos_token_id)
66 else:
67 eos_index = len(outputs)
68 decoded = tokenizer.decode(outputs[:eos_index])
69
70 # レスポンス内容のみ抽出
71 sentinel = "### Response:"
72 sentinelLoc = decoded.find(sentinel)
73 if sentinelLoc >= 0:
74 print(decoded[sentinelLoc+len(sentinel):])
75 else:
76 print('Warning: Expected prompt template to be emitted. Ignoring output.')
77
78generate("自然言語処理とは?")| Model | Params | Layers | Dim | Heads | Dev ppl |
|---|---|---|---|---|---|
| cyberagent/open-calm-small | 160M | 12 | 768 | 12 | 19.7 |
| cyberagent/open-calm-medium | 400M | 24 | 1024 | 16 | 13.8 |
| cyberagent/open-calm-large | 830M | 24 | 1536 | 16 | 11.3 |
| cyberagent/open-calm-1b | 1.4B | 24 | 2048 | 16 | 10.3 |
| cyberagent/open-calm-3b | 2.7B | 32 | 2560 | 32 | 9.7 |
| cyberagent/open-calm-7b | 6.8B | 32 | 4096 | 32 | 8.2 |
1@software{gpt-neox-library,
2 title = {{GPT-NeoX: Large Scale Autoregressive Language Modeling in PyTorch}},
3 author = {Andonian, Alex and Anthony, Quentin and Biderman, Stella and Black, Sid and Gali, Preetham and Gao, Leo and Hallahan, Eric and Levy-Kramer, Josh and Leahy, Connor and Nestler, Lucas and Parker, Kip and Pieler, Michael and Purohit, Shivanshu and Songz, Tri and Phil, Wang and Weinbach, Samuel},
4 url = {https://www.github.com/eleutherai/gpt-neox},
5 doi = {10.5281/zenodo.5879544},
6 month = {8},
7 year = {2021},
8 version = {0.0.1},
9}