Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Ubuntu 22 LTSでOpenCALM をためす その1:環境構築

OpenCALMはCyberAgentさんが開発した大規模言語モデル(LLM)です。
ChatGPTなどと同様、テキスト生成や理解など自然言語処理タスクに利用できますが、
特に日本語の理解に重点をおいているようです。

実際のところ

前提

環境構築

$ pip install transformers sentencepiece
$ pip install accelerate

コード

まずは素直にサンプル通りのモンを実行してみます。
対象が一番ゴツいモデルなので9Gbyteあります。

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained("cyberagent/open-calm-7b", device_map="auto", torch_dtype=torch.float16)
tokenizer = AutoTokenizer.from_pretrained("cyberagent/open-calm-7b")

inputs = tokenizer("AIによって私達の暮らしは、", return_tensors="pt").to(model.device)
with torch.no_grad():
    tokens = model.generate(
        **inputs,
        max_new_tokens=64,
        do_sample=True,
        temperature=0.7,
        top_p=0.9,
        repetition_penalty=1.05,
        pad_token_id=tokenizer.pad_token_id,
    )
    
output = tokenizer.decode(tokens[0], skip_special_tokens=True)
print(output)

ためす

$ python llmtest.py 
pytorch_model.bin.index.json: 100%|████████| 42.0k/42.0k [00:00<00:00, 17.9MB/s]
pytorch_model-00001-of-00002.bin: 100%|████| 9.93G/9.93G [03:24<00:00, 48.4MB/s]
pytorch_model-00002-of-00002.bin: 100%|████| 3.95G/3.95G [01:33<00:00, 42.2MB/s]
Downloading shards: 100%|████████████████████████| 2/2 [04:59<00:00, 149.54s/it]
Loading checkpoint shards: 100%|██████████████████| 2/2 [00:05<00:00,  2.71s/it]
generation_config.json: 100%|███████████████████| 116/116 [00:00<00:00, 855kB/s]
WARNING:root:Some parameters are on the meta device device because they were offloaded to the cpu.
tokenizer_config.json: 100%|███████████████████| 323/323 [00:00<00:00, 2.86MB/s]
tokenizer.json: 100%|██████████████████████| 3.23M/3.23M [00:00<00:00, 4.70MB/s]
special_tokens_map.json: 100%|██████████████████| 129/129 [00:00<00:00, 954kB/s]
AIによって私達の暮らしは、もっと便利で快適で楽しいものになるはずです。
今後、このIoTやAIの技術が進歩していくことによって「人が行う仕事」が減っていくと言われています。そこで、今まさに人手不足が問題となっている建設業界では、その課題を解決するべく、新しい技術の導入を積極的に進めていくことが求められています。そして、その技術を支えるのがITであり

一回実行すると、あとはローカルに記憶されるようです。
もう一度呼んでみると

$ python llmtest.py 
Loading checkpoint shards: 100%|██████████████████| 2/2 [00:02<00:00,  1.05s/it]
WARNING:root:Some parameters are on the meta device device because they were offloaded to the cpu.
AIによって私達の暮らしは、ますます便利に豊かになっていく。しかしその反面、人工知能の発達による倫理観の希薄化やサイバー犯罪の増加により、「人とは何か」という命題に人類の英知は迫られている。作家・伊藤計劃の小説「ハーモニー」を原作とするアニメーション作品『虐殺器官』を今、再び世に問う。【スタッフ】監督

【エラー対応】TypedStorageが云々とエラーがでる(実行はできる

>>
/home/ubuntu/.local/lib/python3.10/site-packages/torch/_utils.py:831: UserWarning: TypedStorage is deprecated. It will be removed in the future and UntypedStorage will be the only storage class. This should only matter to you if you are using storages directly. To access UntypedStorage directly, use tensor.untyped_storage() instead of tensor.storage()
return self.fget.__get__(instance, owner)()