【これがゲームチェンジャーか!】松尾研のTanuki-8BとTanuki-8x8Bを試す

2024-09-02 shi3z

東大松尾研がついに真打とも言えるべきオープンな日本語LLMを発表した。その名もTanuki

松尾研、フルスクラッチ開発のLLMを公開 得意なのは“共感”と“思いやり” 東京大学の松尾・岩澤研究室はフルスクラッチで開発した大規模言語モデル(LLM)の「Tanuki-8×8B」を公開した。 www.itmedia.co.jp

なぜタヌキなのか?
その謎は謎のままだが、とにかく日本語性能がGemini1.5Proに次ぎ、少し前のGPT-4よりも高い上に商用利用可能という太っ腹仕様なので使わない手はない。むしろこれさえあればもう誰もGPTに課金しなくて済む、そんな未来が来るのかもしれない。

しかし、Tanukiは特殊な何かをしてるらしくMLXに簡単にコンバートできずvllmで動かすときもちょっと魔改造したvllmが必要になるという。

最近ローカルづいてる吾輩としてはできればMLXで動かしたいのだがMLXがまだTanukiに対応してない(し、そもそも何をすればTanuki対応にできるのかよくわからない)ので、とりあえず「非推奨」とされてはいるものの、Macでもギリギリ動きそうなGGUF版を動かしてみた。

from llama_cpp import Llama
filename = "Tanuki-8B-dpo-v1.0-IQ4_NL.gguf"
model_name = "team-hatakeyama-phase2/Tanuki-8B-dpo-v1.0-GGUF"
llm = Llama.from_pretrained(repo_id=model_name, 
               filename=filename, n_gpu_layers=99)

これで読み込める。ちなみに8x8Bにしようとしたら、モデルだけで34GBくらいになって、軽く吾輩のMacBookPro M2のメインメモリ容量を超えてしまうので諦めた。

サンプルコードそのままを実行するとしばらく時間が経ってこんな文字列が表示された。

>>> prompt = """<s>以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。
... 
... ### 指示:
... AIによって私たちの暮らしはどのように変わりますか?
... 
... ### 応答:
... """
>>> 
>>> output = llm.create_completion(prompt, max_tokens=1024, temperature=0.5, echo=True)

llama_print_timings:        load time =    4946.28 ms
llama_print_timings:      sample time =      23.61 ms /   453 runs   (    0.05 ms per token, 19185.16 tokens per second)
llama_print_timings: prompt eval time =    4946.12 ms /    59 tokens (   83.83 ms per token,    11.93 tokens per second)
llama_print_timings:        eval time =   61475.78 ms /   452 runs   (  136.01 ms per token,     7.35 tokens per second)
llama_print_timings:       total time =   66943.07 ms /   511 tokens
>>> 
>>> print(output['choices'][0]['text'])
<s>以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。

### 指示:
AIによって私たちの暮らしはどのように変わりますか?

### 応答:
 AIの進化は私たちの生活に多岐にわたる影響をもたらし、その変化は多岐にわたります。まず、日常生活においては、スマートホーム技術が進化し、家庭内の家電やセキュリティシステムがAIによって自動化されます。これにより、エネルギー効率の向上やセキュリティの強化が実現し、生活の質が向上します。例えば、音声認識や自動制御システムにより、照明や温度調整、さらには防犯カメラの監視が簡単になります。

次に、仕事環境においても大きな変化が期待されます。AIはルーチン作業の自動化やデータ分析の効率化を通じて、従業員がより創造的で戦略的な業務に集中できるよう支援します。これにより、生産性が向上し、新しいビジネスモデルやサービスの創出が加速するでしょう。また、リモートワークやフリーランスの働き方がさらに普及し、地理的制約を超えた働き方が一般的になるかもしれません。

教育分野でもAIは大きな変革をもたらします。個別学習支援システムやAIチューターが、各生徒の学習進度や理解度に応じたカスタマイズされた教育を提供することで、学習効率が飛躍的に向上します。これにより、より多くの人々が質の高い教育にアクセスできるようになります。

医療分野でもAIは重要な役割を果たします。診断の精度向上や早期発見、パーソナライズドメディスン(個別化医療)の実現により、病気の予防や治療がより効果的かつ迅速に行われるようになります。また、遠隔医療の普及により、地方や過疎地でも高度な医療サービスが受けられるようになります。

さらに、交通や交通管理においてもAIは革新をもたらします。自動運転車やスマートシティ技術により、交通渋滞の緩和や事故の減少が期待され、移動がより安全で効率的になります。公共交通機関もAIによって最適化され、利用者の利便性が向上します。

エンターテインメントや娯楽の分野でも、
>>> 
>>> 

非推奨ではあるらしいが結構すごい。
ちょっと使いやすいようにこんな関数を作ってみた


def q(prompt,max_tokens=4096):
	prompt = f"""<s>以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。

	### 指示:
	{prompt}

	### 応答:
	"""
	output = llm.create_completion(prompt, max_tokens=max_tokens, temperature=0.5, echo=True)
	return output['choices'][0]['text'] 

これで結構なんでも聞くことができる。
日本語翻訳をさせてみた

>>> q("""以下の英文を日本語に訳しなさい
... 
... The term "washoku" embodies the essence of Japanese food, which is not just about the ingredients and recipes but also about the philosophy and etiquette surrounding it. It reflects a way of life that emphasizes balance, harmony, and respect for nature, which is evident in the meticulous preparation, presentation, and consumption of food. The concept of washoku has been recognized by UNESCO as an intangible cultural heritage, underscoring its global significance and the need to preserve and promote it.
... 
... Understanding the importance of Japanese culinary culture requires delving into its historical roots. The development of Japanese cuisine is a testament to the country's geographical diversity and its people's ingenuity in harnessing natural resources. From the ancient times of the Jomon and Yayoi periods, where foraging and early agricultural practices laid the foundation, to the sophisticated tea ceremonies of the Heian period and the culinary innovations of the Edo period, Japanese food has evolved through centuries of cultural exchange and indigenous creativity.
... """)
Llama.generate: 30 prefix-match hit, remaining 335 prompt tokens to eval

llama_print_timings:        load time =    4946.28 ms
llama_print_timings:      sample time =       6.75 ms /   147 runs   (    0.05 ms per token, 21774.55 tokens per second)
llama_print_timings: prompt eval time =     902.43 ms /   335 tokens (    2.69 ms per token,   371.22 tokens per second)
llama_print_timings:        eval time =   23509.85 ms /   146 runs   (  161.03 ms per token,     6.21 tokens per second)
llama_print_timings:       total time =   24482.43 ms /   481 tokens
'<s>以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。\n\n\t### 指示:\n\t以下の英文を日本語に訳しなさい\n\nThe term "washoku" embodies the essence of Japanese food, which is not just about the ingredients and recipes but also about the philosophy and etiquette surrounding it. It reflects a way of life that emphasizes balance, harmony, and respect for nature, which is evident in the meticulous preparation, presentation, and consumption of food. The concept of washoku has been recognized by UNESCO as an intangible cultural heritage, underscoring its global significance and the need to preserve and promote it.\n\nUnderstanding the importance of Japanese culinary culture requires delving into its historical roots. The development of Japanese cuisine is a testament to the country\'s geographical diversity and its people\'s ingenuity in harnessing natural resources. From the ancient times of the Jomon and Yayoi periods, where foraging and early agricultural practices laid the foundation, to the sophisticated tea ceremonies of the Heian period and the culinary innovations of the Edo period, Japanese food has evolved through centuries of cultural exchange and indigenous creativity.\n\n\n\t### 応答:\n\t 和食という用語は、日本料理の本質を体現しており、単なる食材やレシピにとどまらず、その背後にある哲学と礼儀にも深く根ざしています。これは、食事に対するバランス、調和、そして自然への敬意を重視する生活スタイルを反映しています。料理の準備、プレゼンテーション、そして消費に至るまで、細部にわたる配慮が感じられます。\n\n和食はユネスコによって無形文化遺産として認められ、その重要性は国際的にも認識されています。この認定は、和食が持つ文化的価値と、それを保護し広める必要性を強調しています。\n'
>>> 

出力された日本語訳

和食という用語は、日本料理の本質を体現しており、単なる食材やレシピにとどまらず、その背後にある哲学と礼儀にも深く根ざしています。これは、食事に対するバランス、調和、そして自然への敬意を重視する生活スタイルを反映しています。料理の準備、プレゼンテーション、そして消費に至るまで、細部にわたる配慮が感じられます。\n\n和食はユネスコによって無形文化遺産として認められ、その重要性は国際的にも認識されています。この認定は、和食が持つ文化的価値と、それを保護し広める必要性を強調しています。

Tanuki-8Bによる出力

参考までにGPT-4による訳も載せておく

「和食」という言葉は、単に食材やレシピにとどまらず、それを取り巻く哲学や作法をも含む日本料理の本質を表しています。それは、食材の準備や盛り付け、そして食べ方に至るまで、自然への敬意、調和、そしてバランスを重んじる生き方を反映しています。この和食の概念はユネスコによって無形文化遺産として認められており、その世界的な重要性と保存・促進の必要性が強調されています。

日本の食文化の重要性を理解するためには、その歴史的な根源を探る必要があります。日本料理の発展は、国の地理的多様性と、自然資源を活用する国民の創意工夫を証明しています。縄文時代や弥生時代の狩猟採集や初期の農業の実践が基盤を築き、平安時代の洗練された茶道や江戸時代の料理革新に至るまで、日本の食文化は何世紀にもわたる文化交流と独自の創造性によって進化してきました。

GPT-4による訳

原文

The term "washoku" embodies the essence of Japanese food, which is not just about the ingredients and recipes but also about the philosophy and etiquette surrounding it. It reflects a way of life that emphasizes balance, harmony, and respect for nature, which is evident in the meticulous preparation, presentation, and consumption of food. The concept of washoku has been recognized by UNESCO as an intangible cultural heritage, underscoring its global significance and the need to preserve and promote it. Understanding the importance of Japanese culinary culture requires delving into its historical roots. The development of Japanese cuisine is a testament to the country's geographical diversity and its people's ingenuity in harnessing natural resources. From the ancient times of the Jomon and Yayoi periods, where foraging and early agricultural practices laid the foundation, to the sophisticated tea ceremonies of the Heian period and the culinary innovations of the Edo period, Japanese food has evolved through centuries of cultural exchange and indigenous creativity.

8BモデルのGGUF版という、だいぶ苦しい条件でも、まあまあちゃんと動くようだ。もちろんChatGPTのほうが訳は忠実のようだが。

8x8B版も試してみたいが、流石にMacのローカルで動かないのでGPUを搭載したマシンで試してみる。

まずはAWQ版
ドスパラ謹製A6000x2のマシンでうごいた。

「和食」という言葉は、日本の食文化の本質を体現しています。これは単なる食材やレシピにとどまらず、哲学や礼儀作法にも深く根ざしています。食事を通じてバランス、調和、そして自然への敬意を重視するその姿勢は、食材の丁寧な準備や見た目の美しさ、そして食べる際の礼儀に表れています。この「和食」の概念はユネスコの無形文化遺産として認められており、その重要性と価値が世界的に認識されています。

日本の食文化を理解するためには、その歴史的な背景を探ることが不可欠です。日本の食文化の発展は、地理的な多様性と人々の創意工夫によって支えられてきました。縄文時代や弥生時代の狩猟採集や初期の農業から始まり、平安時代の茶会や江戸時代の料理革新に至るまで、長い歴史の中でさまざまな文化交流と独自の創造性が融合してきました。

例えば、縄文時代の狩猟採集生活では、自然の恵みを最大限に活用する知恵が育まれました。その後、稲作が導入されることで農業が発展し、食材の多様化が進みました。平安時代には茶道が誕生し、食事そのものが芸術の一部となりました。江戸時代には、都市の発展とともに外食文化が栄え、寿司や天ぷらといった現代でも親しまれる料理が生まれました。

このように、「和食」は単なる食事の枠を超え、日本の文化や歴史、価値観を反映した総合的な概念です。そのため、その保存と普及は、単に食文化の保護にとどまらず、日本文化全体の理解と継承にもつながります。

AWQ版のTanuku8x8Bによる翻訳

これだけで完璧と言える。むしろChatGPTよりも丁寧ではないだろうか。
ついでにHuggingFace版も試した。

「和食」という言葉は、日本の食文化の本質を体現しています。それは単に食材やレシピにとどまらず、哲学や礼儀作法にも深く根ざしています。バランス、調和、そして自然への敬意を重んじる姿勢が、料理の準備
プレゼンテーション、そして消費の各段階に反映されています。この概念はユネスコによって無形文化遺産として認められており、その重要性と世界的な意義が認識されています。

日本の食文化を理解するためには、その歴史的な背景を掘り下げる必要があります。日本の食文化の発展は、地理的多様性と自然資源を巧みに利用する人々の創意工夫の結果です。縄文時代や弥生時代の狩猟採集や初
の農業から始まり、平安時代の茶道や江戸時代の料理革新に至るまで、日本の食文化は何世紀にもわたる文化的交流と独自の創造性によって形作られてきました。

Tanuki8x8B HuggingFace版による翻訳

さすが完璧

おまけ:Tanuku8x8Bの動かし方

vllmやAWQやGPTQは特殊な改造版を入れないと動かず、しかもそれがどのCUDAバージョンなのかどのPythonバージョンなのかどこにも明記されておらずオフィシャル(https://zenn.dev/matsuolab/articles/a3b42ddbf5d121)で示されている情報だけではなかなうまくいかずハマった。

再現性を確認できたのは以下の方法

$ conda create -n tanuki python=3.10
$ export PATH=/usr/local/cuda-12.1/bin:$PATH
$ pip install transformers accelerate bitsandbytes
$ pip install flash_attn
$ python hf.py

hf.pyの中身は以下

from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer

model_name = "weblab-GENIAC/Tanuki-8x8B-dpo-v1.0"
# model_name = "team-hatakeyama-phase2/Tanuki-8x8B-dpo-v1.0-AWQ"

model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_name)
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)

messages = [
    {"role": "system", "content": "以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。"},
    {"role": "user", "content": '''以下の英文を日本語に訳しなさい\nThe term "washoku" embodies the essence of Japanese food, which is not just about the ingredients and recipes but also about the philosophy and etiquette surrounding it. It reflects a way of life that emphasizes balance, harmony, and respect for nature, which is evident in the meticulous preparation, presentation, and consumption of food. The concept of washoku has been recognized by UNESCO as an intangible cultural heritage, underscoring its global significance and the need to preserve and promote it.
Understanding the importance of Japanese culinary culture requires delving into its historical roots. The development of Japanese cuisine is a testament to the country's geographical diversity and its people's ingenuity in harnessing natural resources. From the ancient times of the Jomon and Yayoi periods, where foraging and early agricultural practices laid the foundation, to the sophisticated tea ceremonies of the Heian period and the culinary innovations of the Edo period, Japanese food has evolved through centuries of cultural exchange and indigenous creativity.'''}
]

input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
output_ids = model.generate(input_ids, max_new_tokens=1024, temperature=0.5, streamer=streamer)

pipパッケージのバージョンなどは以下

$ pip list|grep tor
torch                    2.4.0
$ pip list|grep trans
transformers             4.44.2
$ pip list|grep flash
flash-attn               2.6.3
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Mon_Apr__3_17:16:06_PDT_2023
Cuda compilation tools, release 12.1, V12.1.105
Build cuda_12.1.r12.1/compiler.32688072_0


鄭重声明:本文の著作権は原作者に帰属します。記事の転載は情報の伝達のみを目的としており、投資の助言を構成するものではありません。もし侵害行為があれば、すぐにご連絡ください。修正または削除いたします。ありがとうございます。
      SiteMap   サイト概要