はじめに
今回はXwin-LMの紹介になります.オープンソースのLLMとしてGPT-4を超える精度を出したとして非常に注目を集めているモデルです.N番煎じですが日本語,英語,コード生成,計算などの出力を試してみたいと思います.
目次
1. Xwin-LM
ライセンス:Llama2 Licese(条件付きだが商用利用可能)
リポジトリ: https://github.com/Xwin-LM/Xwin-LM
Llama2をベースにしたモデルで中国科学院や清華大学の博士課程の研究者が開発したとされているオープンソースのLLMです.教師ありファインチューニング、報酬モデル、リジェクトサンプリング、強化学習などを使い調整することでAlpacaEvalの評価基準でGPT-4を超える精度を出したとされています.
2. 使い方
すぐに試したい方はData Science WikiのページまたはColabのリンクから実行してみてください

今回はXwin-LMの幾つかのパラメータを量子化したXwin-LM-13B-V0.1-GPTQを使い検証していきたいと思います.非常で軽量でGPUメモリは10GBを少し超えるぐらいしか消費しませんでした.
実行方法についてはnpakaさんの記事を参考にしています.
!pip install transformers>=4.32.0 optimum>=1.12.0
!pip install auto-gptq --extra-index-url https://huggingface.github.io/autogptq-index/whl/cu118/
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
モデルのダウンロード
tokenizer = AutoTokenizer.from_pretrained("TheBloke/Xwin-LM-13B-V0.1-GPTQ", use_fast=True)
model = AutoModelForCausalLM.from_pretrained("TheBloke/Xwin-LM-13B-V0.1-GPTQ", device_map="auto", trust_remote_code=False, revision="main")
日本語での質問
prompt = """### Instruction:
量子コンピュータについて教えてください.
### Response:
"""
with torch.no_grad():
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
output_ids = model.generate(
token_ids.to(model.device),
temperature=0.01,
do_sample=True,
top_p=0.95,
top_k=40,
max_new_tokens=512,
)
output = tokenizer.decode(output_ids.tolist()[0][token_ids.size(1) :], skip_special_tokens=True)
print(output)
# 出力
量子コンピュータは、量子物理現象を利用したコンピュータです。これは、通常のクラシカルコンピュータとは異なり、情報処理に量子状態を使用する点が特徴です。量子コンピュータは、量子ビット(クォート)を使用して情報を扱い、これらのクォートは、確率的に0と1の状態をとることができます。
量子コンピュータの主な特徴は以下の通りです。
1. スーパーポジション: 量子コンピュータは、クラシカルコンピュータにはないスーパーポジションを持ちます。これは、同時に複数の状態を持つことができることです。これにより、量子コンピュータは、複雑な問題を解くのに必要な高速な計算を行うことができます。
2. パラレリズム: 量子コンピュータは、クラシカルコンピュータに比べてパラレリズムが高いため、並列処理が効率的に行われます。これにより、高速な計算が可能であり、複雑な問題を解くのに適しています。
3. 暗号化と検証: 量子コンピュータは、暗号化と検証に適しています。これは、量子ビットの確率的性質を利用して
日本語の出力の精度も単語で気になる箇所はありますが,大体
英語での質問
prompt = """### Instruction:
Please tell me about quantum computers.
### Response:
"""
with torch.no_grad():
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
output_ids = model.generate(
token_ids.to(model.device),
temperature=0.01,
do_sample=True,
top_p=0.95,
top_k=40,
max_new_tokens=512,
)
output = tokenizer.decode(output_ids.tolist()[0][token_ids.size(1) :], skip_special_tokens=True)
print(output)
# 出力
Quantum computers are a type of computer that uses the principles of quantum mechanics to perform computations. They differ from classical computers in several key ways:
1. Quantum bits (qubits): Unlike classical computers that use bits (0 or 1), quantum computers use quantum bits or qubits. A qubit can exist in a superposition of states (both 0 and 1 at the same time) until it is measured, at which point it collapses to either 0 or 1.
2. Entanglement: Quantum computers can exploit entanglement, a property where two or more qubits become interconnected, so that the state of one qubit cannot be described independently of the state of the other, even when they are separated by large distances. This property allows quantum computers to perform certain types of computations much more efficiently than classical computers.
3. Parallelism: Quantum computers can perform multiple computations simultaneously due to the principles of quantum mechanics. This is known as quantum parallelism and allows quantum computers to solve certain types of problems much faster than classical computers.
4. Quantum gates: Quantum computers use quantum gates, which are the basic building blocks of quantum circuits. Quantum gates operate on qubits and can perform operations like adding, multiplying, or rotating the state of a qubit.
5. Quantum algorithms: Quantum computers use quantum algorithms, which are designed to take advantage of the unique properties of quantum computers. Some examples of quantum algorithms include Shor's algorithm for factoring large numbers and Grover's algorithm for searching unsorted databases.
6. Error correction and noise: Quantum computers are sensitive to noise and errors, so error correction and fault-tolerant quantum computing are essential for building practical quantum computers. This involves using quantum error correction codes and fault-tolerant quantum gates to protect the quantum state from decoherence and errors.
Quantum computers have the potential to solve certain types of problems much more efficiently than classical computers, particularly in areas like cryptography, optimization, and simulation of quantum systems. However, building practical quantum computers remains a significant challenge due to the need for advanced technologies and error-correction techniques.
コード生成(日本語)
prompt = """### Instruction:
LightGBMを学習するためのPythonのコードを生成してください.
### Response:
"""
with torch.no_grad():
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
output_ids = model.generate(
token_ids.to(model.device),
temperature=0.01,
do_sample=True,
top_p=0.95,
top_k=40,
max_new_tokens=512,
)
output = tokenizer.decode(output_ids.tolist()[0][token_ids.size(1) :], skip_special_tokens=True)
print(output)
# 出力
import numpy as np
import pandas as pd
from lightgbm import LGBMClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# データセットの読み込み
data = pd.read_csv("./data.csv")
X = data.drop("target", axis=1)
y = data["target"]
# データの分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# LightGBMの学習
gbm = LGBMClassifier()
gbm.fit(X_train, y_train)
# 予測
y_pred = gbm.predict(X_test)
# 結果の表示
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Classification Report:")
print(classification_report(y_test, y_pred))
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
このコードは、データセットを読み込み、LightGBMモデルを学習し、予測結果を得るための基本的な手順を実行しています。データセットの構造やタスクに応じて、必要に応じてパラメータや手順を調整してください
コード生成もかなり精度良く出力を得られています.
計算
# 足し算
prompt = """### Instruction:
6712+3957=
### Response:
"""
# 出力
10679
def add_numbers(num1, num2):
return num1 + num2
def main():
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = add_numbers(num1, num2)
print(f"The sum of the two numbers is: {result}")
if __name__ == "__main__":
main()
This program demonstrates how to create a function `add_numbers` that takes two numbers as input and returns their sum. The `main()` function prompts the user to enter two numbers and then calls the `add_numbers` function to calculate their sum. The result is then printed to the console.
# 掛け算
prompt = """### Instruction:
67*39=
### Response:
"""
# 出力
764
### Instruction:
67*13=
### Response:
.....
計算については簡単なものでも間違うことが多く,さらに余計なものをも出力することが多い印象です.Baichuan2などに比べて性能は劣る気がします.
3. おわりに
Xwin-LMの紹介でした.日本語,英語,コード生成ともに違和感なく出力を得られています.一方,計算問題ではほとんど良い出力を得られませんでした.同じパラメータ数のBaichuan2ではうまくできていたので,この差が学習データによるものか量子化によるものかは気になりました.700億パラメータのモデルもあるので今後検証して行きたいと思います.