メむンコンテンツぞスキップ
芋出し画像

【これがゲヌムチェンゞャヌか!】束尟研のTanuki-8BずTanuki-8x8Bを詊す

    東倧束尟研が぀いに真打ずも蚀えるべきオヌプンな日本語LLMを発衚した。その名もTanuki

    なぜタヌキなのか?
    その謎は謎のたただが、ずにかく日本語性胜が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
    $ conda activate tanuki
    $ 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-8B-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


     

    shi3z

     
     
    さすらいのプログラマヌ 知胜ず生呜の研究è€