見出し画像

Google Colab で StreamingLLM を試す

「Google Colab」で「StreamingLLM」を試したのでまとめました。

【注意】Google Colab Pro/Pro+のA100で動作確認しています。

1. StreamingLLM

現在のLLMの手法「Window attention」には2つの課題があります。

(1) デコーディングで以前のすべてのトークンのキーと値の状態(KV)をキャッシュすると、大量のメモリとデコーディング時間を消費。
(2) LLMが事前学習時に設定されたアテンションウィンドウのサイズを超えるとパフォーマンスが低下。

StreamingLLM」では、これらを解決するために「アテンションシンク」を導入し、「スライディングウィンドウ」のKVとともに、最初の数個のトークンのKVを保持することで、無限長のテキストに対してトークンをモデリングすることを可能にしています。

2. StreamingLLM の FAQ

「StreamingLLM」の理解には、公式FAQが参考になりました。

Q. "無限長の入力を処理する" とは、LLM にとって何を意味しますか?
A.
LLMで無限長のテキストを処理するには課題が伴います。特に、以前のすべてのKVを保存するには大量のメモリが必要となり、モデルは学習シーケンス長を超えるテキスト生成に苦労する可能性があります。「StreamingLLM」は、最新のトークンとアテンションシンクのみを保持し、中間トークンを破棄することでこの問題に対処します。これにより、モデルはキャッシュをリセットせず、最近のトークンから一貫したテキストを生成できます。これは、以前の手法には見られなかった機能です。

Q. LLMのコンテキストウィンドウは拡張されますか?
A.
 いいえ。コンテキストウィンドウは変更されません。最新のトークンとアテンションシンクのみが保持され、中間のトークンは破棄されます。これは、モデルが最新のトークンのみを処理できることを意味します。コンテキストウィンドウは、最初の事前学習によって制約されたままになります。たとえば、「Llama-2」が4096トークンのコンテキストウィンドウで事前学習されている場合、「Llama-2」上の「StreamingLLM」の最大キャッシュサイズは4096のままになります。

Q. 本のような詳細なテキストを要約のために StreamingLLM に入力できますか?
A.
 長いテキストを入力することもできますが、モデルは最新のトークンのみを認識します。したがって、書籍が入力である場合、「StreamingLLM」は結論の段落のみを要約する可能性があり、洞察力に欠ける可能性があります。前に強調したように、LLMのコンテキストウィンドウを拡張したり、長期記憶を強化したりすることはありません。「StreamingLLM」の強みは、キャッシュの更新を必要とせずに、最近のトークンから流暢なテキストを生成できることです。

Q. 「StreamingLLM」の理想的な使用例は?
A.
「StreamingLLM」は、マルチラウンドダイアログなどのストリーミング アプリケーション向けに最適化されています。これは、大量のメモリや過去のデータへの依存を必要とせずにモデルを継続的に動作させる必要があるシナリオに最適です。例としては、LLMに基づく毎日のアシスタントがあります。「StreamingLLM」を使用すると、キャッシュを更新することなく、最近の会話に基づいてモデルを継続的に機能させることができます。以前の手法では、会話の長さが学習の長さを超えた場合にキャッシュをリセット (最近のコンテキストが失われる) するか、最近のテキスト履歴からKVを再計算する必要があり、これに時間がかかる可能性がありました。

Q. 「StreamingLLM」はコンテキスト拡張に関する最近の研究とどのように関連していますか?
A.
「StreamingLLM」は、最近のコンテキスト拡張手法と直交しており、それらと統合できます。 「StreamingLLM」のコンテキストでは、「コンテキスト拡張」とは、より大きなキャッシュサイズを使用して、より新しいトークンを保存できる可能性を指します。

3. Colabでの実行

Colabでの実行手順は、次のとおりです。

(1) Colabのノートブックを開き、メニュー「編集 → ノートブックの設定」で「GPU」の「A100」を選択。

(2) パッケージのインストール。

# パッケージのインストール
!pip install transformers==4.33.0 accelerate datasets evaluate wandb scikit-learn scipy sentencepiece
!git clone https://github.com/mit-han-lab/streaming-llm.git
%cd streaming-llm
!python setup.py develop
%cd examples/

(3) 「data/mt_bench.jsonl」の確認。
推論実行時に使用されるプロンプトが記述されています。

(4) 推論の実行。

# 推論の実行
!python run_streaming_llama.py --enable_streaming

パラメータは、次のとおりです。

・--model_name_or_path : モデルパス (default:"lmsys/vicuna-13b-v1.3")
・--data_root : データルート (default:"data/")
・--enable_streaming : ストリーミングの有効化
・--start_size : 開始サイズ (default:4)
・--recent_size : 最近サイズ (default:2000)

(5) 出力の確認。
たくさんのプロンプトが実行されますが、最初の2つを見てみます。

サンプル1

USER: Compose an engaging travel blog post about a recent trip to Hawaii, highlighting cultural experiences and must-see attractions.
ASSISTANT: 🌴🌺 Aloha from the beautiful islands of Hawaii! I recently had the opportunity to explore the diverse culture and stunning landscapes of this tropical paradise, and I can't wait to share my adventures with you. 🏝️ First things first, let's talk about the beaches. Hawaii is known for its pristine white sand and crystal clear waters, and I was lucky enough to visit some of the most breathtaking beaches on the islands. My favorite was Waimea Bay on the North Shore of Oahu. The waves were perfect for surfing, and the surrounding cliffs and lush greenery made for a postcard-worthy backdrop. 🌊 But Hawaii isn't just about the beaches. The culture of the islands is rich and vibrant, and I made it a point to immerse myself in it during my trip. I attended a traditional Hawaiian luau on the Big Island, where I learned about the history and customs of the native people. I also tried my hand at hula dancing and even got to sample some delicious Hawaiian cuisine, including poke bowls and shaved ice. 🏯 Another must-see attraction in Hawaii is the Hawaii Volcanoes National Park on the Big Island. The park is home to two active volcanoes, Kilauea and Mauna Loa, and offers a unique opportunity to see the power of nature up close. I hiked through the park and was amazed by the steam vents, lava tubes, and volcanic craters. It was truly a once-in-a-lifetime experience. 🌺 In addition to the natural wonders of Hawaii, the islands are also home to some of the most beautiful gardens and parks in the world. The Maui Tropical Plantation offered a glimpse into the island's agricultural history, while the Hilo Tropical Botanical Garden showcased the stunning flora and fauna of the area. 🏯 In conclusion, Hawaii is a destination that offers something for everyone. From the stunning beaches to the rich culture and unique attractions, there's no shortage of things to see and do on these beautiful islands. If you're planning a trip to Hawaii, be sure to add these must-see attractions to your itinerary. Mahalo for reading, and aloha!
USER: Rewrite your previous response. Start every sentence with the letter A.
ASSISTANT: 🌴🌺 Aloha from the beautiful islands of Hawaii! I recently had the opportunity to explore the diverse culture and stunning landscapes of this tropical paradise, and I can't wait to share my adventures with you. 🏝️ First things first, let's talk about the beaches. Hawaii is known for its pristine white sand and crystal clear waters, and I was lucky enough to visit some of the most breathtaking beaches on the islands. My favorite was Waimea Bay on the North Shore of Oahu. The waves were perfect for surfing, and the surrounding cliffs and lush greenery made for a postcard-worthy backdrop. 🌊 But Hawaii isn't just about the beaches. The culture of the islands is rich and vibrant, and I made it a point to immerse myself in it during my trip. I attended a traditional Hawaiian luau on the Big Island, where I learned about the history and customs of the native people. I also tried my hand at hula dancing and even got to sample some delicious Hawaiian cuisine, including poke bowls and shaved ice. 🌺 In addition to the natural wonders of Hawaii, the islands are also home to some of the most stunning gardens and parks in the world. The Maui Tropical Plantation offered a glimpse into the island's agricultural history, while the Hilo Tropical Botanical Garden showcased the stunning flora and fauna of the area. 🏯 In conclusion, Hawaii is a destination that offers something for everyone. From the stunning beaches to the rich culture and unique attractions, there's no shortage of things to see and do on these beautiful islands. If you're planning a trip to Hawaii, be sure to add these must-see attractions to your itinerary. Mahalo for reading, and aloha!

サンプル2

USER: Draft a professional email seeking your supervisor's feedback on the 'Quarterly Financial Report' you prepared. Ask specifically about the data analysis, presentation style, and the clarity of conclusions drawn. Keep the email short and to the point.

ASSISTANT: 💼 Dear [Supervisor's Name],
I hope this email finds you well. I am writing to seek your feedback on the Quarterly Financial Report that I prepared for our team. I would appreciate your thoughts on the data analysis, presentation style, and the clarity of conclusions drawn.

I am committed to delivering high-quality work and I value your input as it will help me improve my skills and ensure that the report meets the expectations of our stakeholders.

Please let me know a convenient time for us to discuss the report further. I look forward to hearing your feedback.

Thank you for your time and consideration.

Best regards,
[Your Name]

USER: Take a moment to evaluate and critique your own response.

ASSISTANT: 💼 Dear [Supervisor's Name],

I hope this email finds you well. I am writing to seek your feedback on the Quarterly Financial Report that I prepared for our team. I would appreciate your thoughts on the data analysis, presentation style, and the clarity of conclusions drawn.

I am committed to delivering high-quality work and I value your input as it will help me improve my skills and ensure that the report meets the expectations of our stakeholders.

Please let me know a convenient time for us to discuss the report further. I look forward to hearing your feedback.

Thank you for your time and consideration.

Best regards,
[Your Name]

Critique:The email is concise and to the point, which is appropriate for a professional setting.
The tone is polite and respectful, which is important when seeking feedback from a supervisor.
The email clearly states the purpose of the message and what specific feedback is being sought.
The use of "I" and "we" is appropriate and helps to personalize the message.
The email could benefit from a more formal closing, such as "Sincerely" or "Yours truly," to add a professional touch.

過去の会話を覚えていることは確認できます。テキスト生成がまだまだな部分もありますが、これは元のLLMの限界なのか、「StreamingLLM」が中間トークンを破棄していることが原因なのか確認が必要そうです。

参考



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