モデル概要
モデル特徴
モデル能力
使用事例
🚀 Qwen3-30B-A3B-AWQ
このモデルは、Qwen3-30B-A3B モデルを AWQ 形式に変換したものです。元のモデルは Hugging Face と Modelscope で公開されています。このモデルを使用することで、大規模言語モデルの機能を活用し、様々な自然言語処理タスクを実行できます。
🚀 クイックスタート
Modelscope AWQ モデルの使用例
import torch
from modelscope import AutoModelForCausalLM, AutoTokenizer
model_name = "swift/Qwen3-30B-A3B-AWQ"
## トークナイザーとモデルの読み込み
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto"
)
## モデル入力の準備
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
## テキスト生成の実行
generated_ids = model.generate(
**model_inputs,
max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
## 思考内容の解析
try:
# rindex finding 151668 (</think>)
index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
index = 0
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
print("thinking content:", thinking_content)
print("content:", content)
元のモデルの使用例
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-30B-A3B"
# トークナイザーとモデルの読み込み
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
# モデル入力の準備
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# テキスト生成の実行
generated_ids = model.generate(
**model_inputs,
max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
# 思考内容の解析
try:
# rindex finding 151668 (</think>)
index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
index = 0
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
print("thinking content:", thinking_content)
print("content:", content)
デプロイメント方法
- SGLangを使用する場合:
python -m sglang.launch_server --model-path Qwen/Qwen3-30B-A3B --reasoning-parser qwen3
- vLLMを使用する場合:
vllm serve Qwen/Qwen3-30B-A3B --enable-reasoning --reasoning-parser deepseek_r1
ローカルでの使用
Ollama、LMStudio、MLX-LM、llama.cpp、KTransformersなどのアプリケーションでもQwen3を使用できます。
✨ 主な機能
Qwen3の特徴
Qwen3は、Qwenシリーズの最新世代の大規模言語モデルで、以下のような特徴を持っています。
- 思考モードと非思考モードの切り替え:単一のモデル内で、思考モード(複雑な論理推論、数学、コーディングなど)と非思考モード(効率的な汎用対話)をシームレスに切り替えることができます。
- 推論能力の向上:数学、コード生成、常識的な論理推論などのタスクで、以前のQwQ(思考モード)やQwen2.5 instructモデル(非思考モード)を上回る性能を発揮します。
- 人間嗜好の整合性:創作的な文章作成、ロールプレイ、多ターン対話、命令の実行などで優れた性能を示し、より自然で魅力的な対話体験を提供します。
- エージェント機能:思考モードと非思考モードの両方で外部ツールとの精密な統合が可能で、複雑なエージェントベースのタスクでオープンソースモデルの中でもトップクラスの性能を達成します。
- 多言語サポート:100以上の言語と方言をサポートし、多言語の命令実行と翻訳能力が強力です。
Qwen3-30B-A3Bの特徴
プロパティ | 詳細 |
---|---|
モデルタイプ | 因果言語モデル |
訓練段階 | 事前学習と事後学習 |
パラメータ数 | 合計30.5B、活性化されたパラメータ数3.3B |
パラメータ数(非埋め込み) | 29.9B |
レイヤー数 | 48 |
アテンションヘッド数(GQA) | Q: 32、KV: 4 |
エキスパート数 | 128 |
活性化されたエキスパート数 | 8 |
コンテキスト長 | ネイティブで32,768トークン、YaRNを使用すると131,072トークン |
詳細な情報(ベンチマーク評価、ハードウェア要件、推論性能など)については、ブログ、GitHub、ドキュメントを参照してください。
💻 使用例
思考モードと非思考モードの切り替え
enable_thinking=True
デフォルトでは、Qwen3は思考能力が有効になっています。このモードでは、モデルは推論能力を使って生成される応答の品質を向上させます。
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True # True is the default value for enable_thinking
)
このモードでは、モデルは <think>...</think>
ブロックで囲まれた思考内容を生成し、その後に最終的な応答を生成します。
⚠️ 重要な注意
思考モードでは、
Temperature=0.6
、TopP=0.95
、TopK=20
、MinP=0
(generation_config.json
のデフォルト設定)を使用してください。貪欲なデコーディングは使用しないでください。これは性能の低下や無限の繰り返しを引き起こす可能性があります。詳細なガイダンスについては、ベストプラクティス セクションを参照してください。
enable_thinking=False
このモードでは、モデルの思考機能を完全に無効にすることができ、以前のQwen2.5-Instructモデルと同じ機能を持ちます。このモードは、思考機能を無効にすることが効率向上に重要なシナリオで特に有用です。
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False # Setting enable_thinking=False disables thinking mode
)
このモードでは、モデルは思考内容を生成せず、<think>...</think>
ブロックも含まれません。
⚠️ 重要な注意
非思考モードでは、
Temperature=0.7
、TopP=0.8
、TopK=20
、MinP=0
を使用することをおすすめします。詳細なガイダンスについては、ベストプラクティス セクションを参照してください。
ユーザー入力による思考モードと非思考モードの切り替え
enable_thinking=True
の場合、ユーザーは /think
と /no_think
をユーザープロンプトまたはシステムメッセージに追加することで、モデルの思考モードをターンごとに動的に制御することができます。モデルは多ターン会話で最新の命令に従います。
以下は多ターン会話の例です。
from transformers import AutoModelForCausalLM, AutoTokenizer
class QwenChatbot:
def __init__(self, model_name="Qwen/Qwen3-30B-A3B"):
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.model = AutoModelForCausalLM.from_pretrained(model_name)
self.history = []
def generate_response(self, user_input):
messages = self.history + [{"role": "user", "content": user_input}]
text = self.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
inputs = self.tokenizer(text, return_tensors="pt")
response_ids = self.model.generate(**inputs, max_new_tokens=32768)[0][len(inputs.input_ids[0]):].tolist()
response = self.tokenizer.decode(response_ids, skip_special_tokens=True)
# Update history
self.history.append({"role": "user", "content": user_input})
self.history.append({"role": "assistant", "content": response})
return response
# Example Usage
if __name__ == "__main__":
chatbot = QwenChatbot()
# First input (without /think or /no_think tags, thinking mode is enabled by default)
user_input_1 = "How many r's in strawberries?"
print(f"User: {user_input_1}")
response_1 = chatbot.generate_response(user_input_1)
print(f"Bot: {response_1}")
print("----------------------")
# Second input with /no_think
user_input_2 = "Then, how many r's in blueberries? /no_think"
print(f"User: {user_input_2}")
response_2 = chatbot.generate_response(user_input_2)
print(f"Bot: {response_2}")
print("----------------------")
# Third input with /think
user_input_3 = "Really? /think"
print(f"User: {user_input_3}")
response_3 = chatbot.generate_response(user_input_3)
print(f"Bot: {response_3}")
⚠️ 重要な注意
API互換性のため、
enable_thinking=True
の場合、ユーザーが/think
または/no_think
を使用しても、モデルは常に<think>...</think>
で囲まれたブロックを出力します。ただし、思考が無効になっている場合、このブロック内の内容は空になることがあります。enable_thinking=False
の場合、このソフトスイッチは無効です。ユーザーが入力した/think
または/no_think
タグに関係なく、モデルは思考内容を生成せず、<think>...</think>
ブロックも含まれません。
エージェント機能の使用
Qwen3はツール呼び出し機能に優れています。Qwen3のエージェント機能を最大限に活用するために、Qwen-Agent の使用をおすすめします。Qwen-Agentは内部でツール呼び出しテンプレートとツール呼び出しパーサーをカプセル化しているため、コーディングの複雑さを大幅に軽減します。
from qwen_agent.agents import Assistant
# Define LLM
llm_cfg = {
'model': 'Qwen3-30B-A3B',
# Use the endpoint provided by Alibaba Model Studio:
# 'model_type': 'qwen_dashscope',
# 'api_key': os.getenv('DASHSCOPE_API_KEY'),
# Use a custom endpoint compatible with OpenAI API:
'model_server': 'http://localhost:8000/v1', # api_base
'api_key': 'EMPTY',
# Other parameters:
# 'generate_cfg': {
# # Add: When the response content is `<think>this is the thought</think>this is the answer;
# # Do not add: When the response has been separated by reasoning_content and content.
# 'thought_in_content': True,
# },
}
# Define Tools
tools = [
{'mcpServers': { # You can specify the MCP configuration file
'time': {
'command': 'uvx',
'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
},
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"]
}
}
},
'code_interpreter', # Built-in tools
]
# Define Agent
bot = Assistant(llm=llm_cfg, function_list=tools)
# Streaming generation
messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
for responses in bot.run(messages=messages):
pass
print(responses)
🔧 技術詳細
長文処理
Qwen3はネイティブで最大32,768トークンのコンテキスト長をサポートしています。入力と出力の合計長がこの制限を大幅に超える会話の場合、RoPEスケーリング技術を使用して長文を効果的に処理することをおすすめします。YaRN 方法を使用して、最大131,072トークンのコンテキスト長でモデルの性能を検証しています。
YaRNはいくつかの推論フレームワークでサポートされています。例えば、ローカルでの使用には transformers
と llama.cpp
、デプロイメントには vllm
と sglang
がサポートしています。一般的に、サポートされているフレームワークでYaRNを有効にするには、以下の2つの方法があります。
-
モデルファイルの修正:
config.json
ファイルにrope_scaling
フィールドを追加します。{ ..., "rope_scaling": { "rope_type": "yarn", "factor": 4.0, "original_max_position_embeddings": 32768 } }
llama.cpp
の場合は、修正後にGGUFファイルを再生成する必要があります。 -
コマンドライン引数の指定:
vllm
の場合:vllm serve ... --rope-scaling '{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}' --max-model-len 131072
sglang
の場合:python -m sglang.launch_server ... --json-model-override-args '{"rope_scaling":{"rope_type":"yarn","factor":4.0,"original_max_position_embeddings":32768}}'
llama.cpp
のllama-server
の場合:llama-server ... --rope-scaling yarn --rope-scale 4 --yarn-orig-ctx 32768
⚠️ 重要な注意
以下の警告が表示された場合は、
transformers>=4.51.0
にアップグレードしてください。Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'original_max_position_embeddings'}
⚠️ 重要な注意
すべての著名なオープンソースフレームワークは静的なYaRNを実装しています。これは、入力長に関係なくスケーリング係数が一定であることを意味し、短いテキストでの性能に影響を与える可能性があります。 長いコンテキストを処理する必要がある場合のみ、
rope_scaling
設定を追加することをおすすめします。 また、必要に応じてfactor
を変更することもおすすめします。例えば、アプリケーションの典型的なコンテキスト長が65,536トークンの場合、factor
を2.0に設定するとよいでしょう。
⚠️ 重要な注意
config.json
のデフォルトのmax_position_embeddings
は40,960に設定されています。これには、出力用に32,768トークンと、典型的なプロンプト用に8,192トークンが予約されており、短いテキスト処理を含むほとんどのシナリオに十分です。平均コンテキスト長が32,768トークンを超えない場合、このシナリオでYaRNを有効にすることはおすすめしません。これはモデルの性能を低下させる可能性があります。
💡 使用アドバイス
アリババモデルスタジオが提供するエンドポイントはデフォルトで動的なYaRNをサポートしており、追加の設定は必要ありません。
📚 詳細ドキュメント
ベストプラクティス
最適な性能を得るために、以下の設定をおすすめします。
- サンプリングパラメータ:
- 思考モード (
enable_thinking=True
) では、Temperature=0.6
、TopP=0.95
、TopK=20
、MinP=0
を使用してください。貪欲なデコーディングは使用しないでください。これは性能の低下や無限の繰り返しを引き起こす可能性があります。 - 非思考モード (
enable_thinking=False
) では、Temperature=0.7
、TopP=0.8
、TopK=20
、MinP=0
を使用することをおすすめします。 - サポートされているフレームワークでは、
presence_penalty
パラメータを0から2の間で調整して、無限の繰り返しを減らすことができます。ただし、高い値を使用すると、言語の混合やモデル性能のわずかな低下が発生することがあります。
- 思考モード (
- 十分な出力長: ほとんどのクエリでは、32,768トークンの出力長を使用することをおすすめします。数学やプログラミングコンペティションなどの非常に複雑な問題のベンチマークでは、最大出力長を38,912トークンに設定することをおすすめします。これにより、モデルが詳細で包括的な応答を生成するための十分なスペースが確保され、全体的な性能が向上します。
- 出力形式の標準化: ベンチマーク時には、プロンプトを使用してモデルの出力を標準化することをおすすめします。
- 数学問題: プロンプトに "Please reason step by step, and put your final answer within \boxed{}." を含めてください。
- 選択肢付きの質問: 応答を標準化するために、以下のJSON構造をプロンプトに追加してください。"Please show your choice in the
answer
field with only the choice letter, e.g.,"answer": "C"
."
- 履歴に思考内容を含めない: 多ターン会話では、過去のモデル出力には最終的な出力部分のみを含め、思考内容は含める必要はありません。これは提供されているJinja2のチャットテンプレートで実装されています。ただし、Jinja2のチャットテンプレートを直接使用しないフレームワークでは、開発者がこのベストプラクティスを遵守するようにする必要があります。
引用
このモデルを使用した研究や開発に役立った場合は、以下のように引用してください。
@misc{qwen3,
title = {Qwen3},
url = {https://qwenlm.github.io/blog/qwen3/},
author = {Qwen Team},
month = {April},
year = {2025}
}
📄 ライセンス
このモデルはApache 2.0ライセンスの下で公開されています。



