見出し画像

LLMは作業用PCで動く!?         3種類のモデルで試してみた結果。。。

みなさまこんにちは!
今回は、今話題のLLMについて独自の視点で調査し、動かしてみました!


◎はじめに

LLM(大規模言語モデル)とは?

膨大なテキストデータを学習したAIモデルで、
文章生成や要約、質問応答などの様々なタスクに対応できるものです。
数十億ものパラメータから成る巨大なニューラルネットワークで、膨大な量のテキストデータから学習を行っています。

LLMは機械翻訳や文書要約、対話システムなどの自然言語処理の主要なタスクに向いており、近年では人間並みの性能を発揮できるレベルに到達してきています。

イメージ図

 【代表例】
  
Googleの「PaLM 2」 サービス名: Bard
  OpenAIの「GPT-4」     サービス名: ChatGPT
  Metaの「Llama 2」

 【メリット】
 
 ・翻訳や要約などさまざまなタスクを高い精度で実施
  ・新しい知識やアイデアを生成してくれる
  ・異なる言語間でのコミュニケーションが可能

 【デメリット】
  
GPUTPUなどの高性能な演算装置やリソースが必要
  
・出力内容の真偽や倫理性が不明確
  ・個人情報漏洩や偏見・差別のリスクがある

上記の通り、
LLMは非常に優れた技術ですが、一方でまだまだ問題もあります。


◎作業準備

LLMを動作させるうえで、下記を準備しました。

・PC
・LLM
・Python(PyTorch)
 ※Meta Llama 2 , LINE japanese-large-lm のみ

【PC】

 今回使用する作業用PCのスペックです。

 CPU  第10世代 Intel(R) Core(TM) i5-10210U  
 GPU  内蔵
 メモリ 16GB

【LLM】

 続いて、使用したLLM一覧です。

 Meta Llama 2
 LINE japanese-large-lm
 GPT4ALL 


【Python(PyTorch)】

 最後に、Meta Llama 2 , LINE japanese-large-lmで使用する
 Pythonをダウンロードします。

 公式サイト:


◎作業実施

Meta Llama2

①公式サイトからソースコードをダウンロード
 (ダウンロードの際にユーザー登録が必要で、
 メールに書かれた情報が初期設定時に必要でした。)

公式サイト:


②ソースダウンロード完了後、
NVIDIA の GPU が無いと動作しないため、
CPU のみでも動作するように下記サイトにある修正されたソースを利用

サイト:


【実施結果】

airily@user01 MINGW64 /c/projects/llama-main
$ torchrun --nproc_per_node 1 example_chat_completion.py --ckpt_dir llama-2-7b-chat/ --tokenizer_path tokenizer.model --max_seq_len 512 --max_batch_size 4
NOTE: Redirects are currently not supported in Windows or MacOs.
[W ..\torch\csrc\distributed\c10d\socket.cpp:601] [c10d] The client socket has failed to connect to [user01]:29500 (system error: 10049 - 要求したアドレスのコンテキストが無効です。).
[W ..\torch\csrc\distributed\c10d\socket.cpp:601] [c10d] The client socket has failed to connect to [user01]:29500 (system error: 10049 - 要求したアドレスのコンテキストが無効です。).
[W ..\torch\csrc\distributed\c10d\socket.cpp:601] [c10d] The client socket has failed to connect to [user01]:29500 (system error: 10049 - 要求したアドレスのコンテキストが無効です。).
[W ..\torch\csrc\distributed\c10d\socket.cpp:601] [c10d] The client socket has failed to connect to [user01]:29500 (system error: 10049 - 要求したアドレスのコンテキストが無効です。).
> initializing model parallel with size 1
> initializing ddp with size 1
> initializing pipeline with size 1
Traceback (most recent call last):
  File "C:\projects\llama-main\example_chat_completion.py", line 73, in <module>
    fire.Fire(main)
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\fire\core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\fire\core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
                                ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\fire\core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\projects\llama-main\example_chat_completion.py", line 20, in main
    generator = Llama.build(
                ^^^^^^^^^^^^
  File "C:\projects\llama-main\llama\generation.py", line 114, in build
    model = Transformer(model_args)
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\projects\llama-main\llama\model.py", line 269, in __init__
    self.layers.append(TransformerBlock(layer_id, params))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\projects\llama-main\llama\model.py", line 232, in __init__
    self.feed_forward = FeedForward(
                        ^^^^^^^^^^^^
  File "C:\projects\llama-main\llama\model.py", line 211, in __init__
    self.w1 = ColumnParallelLinear(
              ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\fairscale\nn\model_parallel\layers.py", line 262, in __init__
    self.weight = Parameter(torch.Tensor(self.output_size_per_partition, self.in_features))
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: [enforce fail at ..\c10\core\impl\alloc_cpu.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 90177536 bytes.
ERROR:torch.distributed.elastic.multiprocessing.api:failed (exitcode: 1) local_rank: 0 (pid: 42920) of binary: C:\Program Files\Python311\python.exe
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\airily\AppData\Roaming\Python\Python311\Scripts\torchrun.exe\__main__.py", line 7, in <module>
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\elastic\multiprocessing\errors\__init__.py", line 346, in wrapper
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\run.py", line 794, in main
    run(args)
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\run.py", line 785, in run
    elastic_launch(
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\launcher\api.py", line 134, in __call__
    return launch_agent(self._config, self._entrypoint, list(args))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\launcher\api.py", line 250, in launch_agent
    raise ChildFailedError(
torch.distributed.elastic.multiprocessing.errors.ChildFailedError:
============================================================
example_chat_completion.py FAILED
------------------------------------------------------------
Failures:
  <NO_OTHER_FAILURES>
------------------------------------------------------------
Root Cause (first observed failure):
[0]:
  time      : 2023-08-30_19:35:10
  host      : user01
  rank      : 0 (local_rank: 0)
  exitcode  : 1 (pid: 42920)
  error_file: <N/A>
  traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html
============================================================
airily@user01 MINGW64 /c/projects/llama-main

エラー内容:
RuntimeError: [enforce fail at ..\c10\core\impl\alloc_cpu.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 90177536 bytes.・・・

日本語訳:
RuntimeError: [..\c10\core\impl\alloc_cpu.cpp:72 で強制失敗] データ。 DefaultCPUAllocator: メモリが不足しています: 90177536 バイトを割り当てようとしました。・・・

どうやらLlama 2は、CPUだけで実行しようとするとリソース不足で
起動途中に強制終了してしまうようです。


LINE japanese-large-lm

①公式サイトからソースコードをダウンロード

公式サイト:


②ソースダウンロード完了後、
実行用ソースをコピペで作成(今回は test.py)


③下記コマンドを入力
 torchrun test.py

【実施結果】

airily@user01 MINGW64 /c/projects/line/japanese-large-lm-1.7b
$ torchrun test.py 
NOTE: Redirects are currently not supported in Windows or MacOs.
[W ..\torch\csrc\distributed\c10d\socket.cpp:601] [c10d] The client socket has failed to connect to [user01]:29500 (system error: 10049 - 要求したアドレスのコンテキストが無効です。).
[W ..\torch\csrc\distributed\c10d\socket.cpp:601] [c10d] The client socket has failed to connect to [user01]:29500 (system error: 10049 - 要求したアドレスのコンテキストが無効です。).
Downloading (…)lve/main/config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2.01k/2.01k [00:00<00:00, 2.08MB/s]
C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\huggingface_hub\file_download.py:133: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\Users\airily\.cache\huggingface\hub. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.
To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to see activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development
  warnings.warn(message)
Downloading model.safetensors: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3.51G/3.51G [03:40<00:00, 15.9MB/s]
Downloading (…)okenizer_config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 360/360 [00:00<?, ?B/s]
Downloading spiece.model: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.21M/1.21M [00:00<00:00, 4.86MB/s]
You are using the default legacy behaviour of the <class 'transformers.models.t5.tokenization_t5.T5Tokenizer'>. If you see this, DO NOT PANIC! This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=True`. This should only be set if you understand what it means, and thouroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565
Traceback (most recent call last):
  File "C:\projects\line\japanese-large-lm-1.7b\test.py", line 6, in <module>
    tokenizer = AutoTokenizer.from_pretrained("line-corporation/japanese-large-lm-1.7b", use_fast=False)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\transformers\models\auto\tokenization_auto.py", line 727, in from_pretrained
    return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\transformers\tokenization_utils_base.py", line 1854, in from_pretrained
    return cls._from_pretrained(
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\transformers\tokenization_utils_base.py", line 2017, in _from_pretrained
    tokenizer = cls(*init_inputs, **init_kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\transformers\models\t5\tokenization_t5.py", line 194, in __init__
    self.sp_model = self.get_spm_processor()
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\transformers\models\t5\tokenization_t5.py", line 200, in get_spm_processor
    model_pb2 = import_protobuf()
                ^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\transformers\convert_slow_tokenizer.py", line 40, in import_protobuf
    return sentencepiece_model_pb2
           ^^^^^^^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'sentencepiece_model_pb2' where it is not associated with a value
ERROR:torch.distributed.elastic.multiprocessing.api:failed (exitcode: 1) local_rank: 0 (pid: 4152) of binary: C:\Program Files\Python311\python.exe
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\airily\AppData\Roaming\Python\Python311\Scripts\torchrun.exe\__main__.py", line 7, in <module>
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\elastic\multiprocessing\errors\__init__.py", line 346, in wrapper
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\run.py", line 794, in main
    run(args)
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\run.py", line 785, in run
    elastic_launch(
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\launcher\api.py", line 134, in __call__
    return launch_agent(self._config, self._entrypoint, list(args))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\airily\AppData\Roaming\Python\Python311\site-packages\torch\distributed\launcher\api.py", line 250, in launch_agent
    raise ChildFailedError(
torch.distributed.elastic.multiprocessing.errors.ChildFailedError:
============================================================
test.py FAILED
------------------------------------------------------------
Failures:
  <NO_OTHER_FAILURES>
------------------------------------------------------------
Root Cause (first observed failure):
[0]:
  time      : 2023-08-23_18:49:07
  host      : user01
  rank      : 0 (local_rank: 0)
  exitcode  : 1 (pid: 4152)
  error_file: <N/A>
  traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html
============================================================
airily@user01 MINGW64 /c/projects/line/japanese-large-lm-1.7b
$

エラー内容:
test.py FAILED
------------------------------------------------------------
Failures:
<NO_OTHER_FAILURES>
------------------------------------------------------------

日本語訳:
test.pyが失敗しました
------------------------------------------------------------
失敗:
<NO_OTHER_FAILURES>
------------------------------------------------------------

LINEのLLMも起動を試してみましたが、
Llama 2と同様、途中で強制終了してしまいました。


GPT4ALL

①公式サイトからソースコードをダウンロード

 公式サイト:https://gpt4all.io/index.html


②gpt4all-main.zip を解凍


③下記フォルダに移動
 gpt4all-main\gpt4all-training\chat


④下記ファイルが存在する事を確認
 gpt4all-lora-quantized-linux-x86
 gpt4all-lora-quantized-OSX-intel
 gpt4all-lora-quantized-OSX-m1
 gpt4all-lora-quantized-win64.exe


⑤上記のgpt4all-main\gpt4all-training\chatフォルダに、
下記ファイルをコピー
 gpt4all-lora-quantized.bin


⑥下記ファイルを実行する
 gpt4all-lora-quantized-win64.exe

⑦コンソール上でチャット内容を入力(英語のみ)

【実施結果】

main: seed = 1693468375
llama_model_load: loading model from 'gpt4all-lora-quantized.bin' - please wait ...
llama_model_load: ggml ctx size = 6065.35 MB
llama_model_load: memory_size =  2048.00 MB, n_mem = 65536
llama_model_load: loading model part 1/1 from 'gpt4all-lora-quantized.bin'
llama_model_load: .................................... done
llama_model_load: model size =  4017.27 MB / num tensors = 291

system_info: n_threads = 4 / 8 | AVX = 1 | AVX2 = 1 | AVX512 = 0 | FMA = 0 | NEON = 0 | ARM_FMA = 0 | F16C = 0 | FP16_VA = 0 | WASM_SIMD = 0 | BLAS = 0 | SSE3 = 0 | VSX = 0 |
main: interactive mode on.
sampling parameters: temp = 0.100000, top_k = 40, top_p = 0.950000, repeat_last_n = 64, repeat_penalty = 1.300000


== Running in chat mode. ==
 - Press Ctrl+C to interject at any time.
 - Press Return to return control to LLaMA.
 - If you want to submit another line, end your input in '\'.

> Hello! What should I call yours?
Hi there, my name's John Smith and you can always reach me at [email address] or on Twitter @john_smith123
> What should I call yours?
Hello! What is your preferred method of communication (e.g., email, text message)? Thanks for reaching out to us! 🙂
> Hello! What should I call yours?
Hi there, my name's John Smith and you can always reach me at [email address] or on Twitter @john_smith123. Thanks for reaching out to us! 🙂
> Hi! I am Takatsu. Call me Takatsu.
Hello, my name is John Smith and you can reach me at [email address] or on Twitter @john_smith123. Thanks for reaching out to us! 🙂
> I want to find out about LLM. Please tell me about LLM.
LLM stands for Legal Letter Marks, which is a term used in the printing industry when referring to letters that are printed on paper or other materials using letterpress technology (i.e., metal type). The process of creating these marks involves setting individual pieces of movable type into place and then locking them down with clamps called chases before pressing against a sheet of blankets, which is the surface upon which they will be impressed onto paper or other material during printing.
> I'm sorry. What I want to know is LLM on AI.
LLM stands for Law LLM (Master of Laws) degree, a postgraduate law program that focuses primarily on international and comparative laws. AI refers to Artificial Intelligence which has been widely used in various industries such as healthcare, finance etc., but its application is still limited when it comes to legal sector due to the complexities of regulations governing this field.
> I have heard that recent generation AIs are learning using LLM. In the previous answer, there is little information about LLM, so please add a little more.
LLM stands for Law LLM (Master of Laws) degree which is an advanced postgraduate law program focused on international and comparative laws. Recent generation AIs are using this method to learn because it allows them to analyze legal documents, court decisions or other relevant materials in order to understand the contextual meaning behind these texts. This approach can help improve their ability to comprehend complex language used by humans within a specific field of law and make better judgments based on that understanding.
>






GPT4ALLは、CPUだけで起動しました!!!

内容を確認してみます。

筆者質問:Hello! What should I call yours?
日本語訳:こんにちは! 何と呼べばいいですか?

GPT4ALL:Hi there, my name's John Smith and you can always reach me at [email address] or on Twitter @john_smith123
日本語訳:こんにちは、私の名前はジョン・スミスです。[電子メール アドレス] または Twitter @john_smith123 までいつでもご連絡ください。)

会話としてギリギリ成立しているように見えますが、
email address や Twitter @john_smith123 は実在しないため、誤った情報を提供しているようです。
また日本語でのチャットは不可、英語でのやり取りのみでした。
全体的に内容も不安定で、自然な会話とは呼べない結果となりました。


◎終わりに

【まとめ】

Llama 2 ・ LINE japanese-large-lm  → メモリ不足等で起動不可
GPT4ALL              → 起動可能、精度低

という結果となりました。

今回の検証で、LLMを使用するためには
GPUやTPUなどの高性能な演算装置が必要であること、
LLMの種類や性能についても詳しく調べる必要があること
これらの再確認ができました。

現状では、公開されているサービスを利用する方が、
コストがかからないかもしれません。

【所感】

LLMは、今後も進化し続ける技術であり、様々な応用例が期待されてますが、一方でリソースやコストなどの課題面も多く残ります。
そのためLLMを使う際は、その利点と欠点をよく理解しておくことが重要です。

長くなりましたが、以上です。
この度は閲覧いただきありがとうございました。
この記事が誰かのお役に立てば幸いです。

この記事が気に入ったらサポートをしてみませんか?