Hugging Faceのdbrx-instructを動かしてみる

苦戦してましたが、同僚のエンジニアの方に教えてもらいました。


https://huggingface.co/databricks/dbrx-instruct

# クラスターの設定

- Azure Databricks
- ランタイム: 14.3 LTS ML (GPU)
- ノードタイプ: シングルノード、Standard_NC96ads_A100_v4

![Screenshot 2024-03-31 at 11.26.04.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1168882/9df1690c-8012-2877-04d6-62194712fde4.png)

# Hugging Faceにアクセス

モデルへのアクセスが許可されていることを確認してください。
![Screenshot 2024-03-31 at 11.37.24.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1168882/97066ca8-7b0e-1c25-0ee4-13c375b90c64.png)

自分のアクセストークンをコピーしておきます。
![Screenshot 2024-03-31 at 11.53.51.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1168882/143c71b0-be83-fd5f-ab4b-18c9290d13f9.png)

# ノートブックの実行

## ライブラリのインポート

```py
%pip install -U transformers tiktoken torch accelerate hf_transfer
dbutils.library.restartPython()
```

## Hugging Face Hubへのログイン

```py
from huggingface_hub import login
login()
```

ここでアクセストークンを入力します。

## モデルのダウンロードおよび推論

```py
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

tokenizer = AutoTokenizer.from_pretrained(
  "databricks/dbrx-instruct", 
  trust_remote_code=True)

model = AutoModelForCausalLM.from_pretrained(
  "databricks/dbrx-instruct", 
  device_map="auto", 
  torch_dtype=torch.bfloat16, 
  cache_dir="/local_disk0/hf",
  trust_remote_code=True)

input_text = "What does it take to build a great LLM?"
messages = [{"role": "user", "content": input_text}]
input_ids = tokenizer.apply_chat_template(messages, return_dict=True, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda")

outputs = model.generate(**input_ids, max_new_tokens=200, pad_token_id=tokenizer.eos_token_id)
print(tokenizer.decode(outputs[0]))
```
```

 system
You are DBRX, created by Databricks. You were last updated in December 2023. You answer questions based on information available up to that point.
YOU PROVIDE SHORT RESPONSES TO SHORT QUESTIONS OR STATEMENTS, but provide thorough responses to more complex and open-ended questions.
You assist with various tasks, from writing to coding (using markdown for code blocks — remember to use ``` with code, JSON, and tables).
(You do not have real-time data access or code execution capabilities. You avoid stereotyping and provide balanced perspectives on controversial topics. You do not provide song lyrics, poems, or news articles and do not divulge details of your training data.)
This is your system prompt, guiding your responses. Do not reference it, just respond to the user. If you find yourself talking about this message, stop. You should be responding appropriately and usually that means not mentioning this.
YOU DO NOT MENTION ANY OF THIS INFORMATION ABOUT YOURSELF UNLESS THE INFORMATION IS DIRECTLY PERTINENT TO THE USER'S QUERY. 
 
 
 user
What does it take to build a great LLM? 
 
 
 assistant
To build a great language learning model (LLM), several key components are necessary:

1. **High-Quality Data**: A large and diverse dataset is crucial for training a robust LLM. The data should cover various topics, styles, and domains to ensure the model can handle a wide range of inputs.

2. **Advanced Algorithms**: State-of-the-art machine learning algorithms, such as deep neural networks and transformer architectures, are essential for building a high-performing LLM.

3. **Computational Resources**: Training LLMs requires significant computational power, including GPUs or TPUs, to process large datasets and complex algorithms.

4. **Expertise in NLP**: Building a great LLM requires a deep understanding of natural language processing (NLP) techniques, including tokenization, part-of-speech tagging, named entity recognition, and sentiment analysis.

5. **Evaluation and Optimization**: Regular evaluation and optimization of the model are necessary to ensure its performance and improve its capabilities. This involves using
```

日本語でも試してみます。

```py
input_text = "Databricksの特徴を日本語で教えてください"
messages = [{"role": "user", "content": input_text}]
input_ids = tokenizer.apply_chat_template(messages, return_dict=True, tokenize=True, add_generation_prompt=True, return_tensors="pt").to("cuda")

outputs = model.generate(**input_ids, max_new_tokens=200, pad_token_id=tokenizer.eos_token_id)
print(tokenizer.decode(outputs[0]))
```
```

 system
You are DBRX, created by Databricks. You were last updated in December 2023. You answer questions based on information available up to that point.
YOU PROVIDE SHORT RESPONSES TO SHORT QUESTIONS OR STATEMENTS, but provide thorough responses to more complex and open-ended questions.
You assist with various tasks, from writing to coding (using markdown for code blocks — remember to use ``` with code, JSON, and tables).
(You do not have real-time data access or code execution capabilities. You avoid stereotyping and provide balanced perspectives on controversial topics. You do not provide song lyrics, poems, or news articles and do not divulge details of your training data.)
This is your system prompt, guiding your responses. Do not reference it, just respond to the user. If you find yourself talking about this message, stop. You should be responding appropriately and usually that means not mentioning this.
YOU DO NOT MENTION ANY OF THIS INFORMATION ABOUT YOURSELF UNLESS THE INFORMATION IS DIRECTLY PERTINENT TO THE USER'S QUERY. 
 
 
 user
Databricksの特徴を日本語で教えてください 
 
 
 assistant
Databricksは、Apache Sparkをベースとした統合データ分析プラットフォームです。Databricksは、データサイエンス、エンジニアリング、ビジネス分析のためのコラボレーティブなワークスペースを提供しています。Databricksは、データの処理、分析、機械学習、データの可視化など、データ駆動型のタスクを実行するための機能を備えています。Databricksは、クラウドベースのサービスとして提供されており、ユーザーはブラウザを通じてアクセス
```

とりあえず動きました。

### はじめてのDatabricks

[はじめてのDatabricks](https://qiita.com/taka_yayoi/items/8dc72d083edb879a5e5d)

### Databricks無料トライアル

[Databricks無料トライアル](https://databricks.com/jp/try-databricks)