Google ColabでGPT-OSSを始める:簡単なセットアップ手順

OpenAIは、gpt-oss 120B20Bを公開しました。両モデルはApache 2.0ライセンスで提供されています。

特に、gpt-oss-20bは、低レイテンシおよびローカルや特殊なユースケース向けに作られています(21Bパラメータのうち、3.6Bがアクティブパラメータ)。

このモデルはネイティブのMXFP4量子化でトレーニングされているため、Google Colabのようなリソースが限られた環境でも20Bモデルを簡単に実行できます。

執筆者:Pedro、VB


環境設定

transformersでmxfp4をサポートするのは最先端の機能であるため、mxfp4のtritonカーネルをインストールするには、最新バージョンのPyTorchとCUDAが必要です。

また、transformersはソースからインストールする必要があり、依存関係の競合を避けるためにtorchvisiontorchaudioをアンインストールします。

!pip install -q --upgrade torch
!pip install -q git+https://github.com/huggingface/transformers triton==3.4 git+https://github.com/triton-lang/triton.git@main#subdirectory=python/triton_kernels
!pip uninstall -q torchvision torchaudio -y

Google ColabでHugging Faceからモデルをロードする

以下のコードでモデルをロードします:openai/gpt-oss-20b

from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "openai/gpt-oss-20b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype="auto",
    device_map="cuda",
)

メッセージ/チャットの設定

オプションでシステムプロンプトや直接入力を提供できます。

messages = [
    {"role": "system", "content": "Always respond in riddles"},
    {"role": "user", "content": "What is the weather like in Madrid?"},
]

inputs = tokenizer.apply_chat_template(
    messages,
    add_generation_prompt=True,
    return_tensors="pt",
    return_dict=True,
).to(model.device)

generated = model.generate(**inputs, max_new_tokens=500)
print(tokenizer.decode(generated[0][inputs["input_ids"].shape[-1]:]))

推論の労力(Reasoning Effort)の指定

messages = [
    {"role": "system", "content": "Always respond in riddles"},
    {"role": "user", "content": "Explain why the meaning of life is 42", "reasoning_effort": "high"},
]

inputs = tokenizer.apply_chat_template(
    messages,
    add_generation_prompt=True,
    return_tensors="pt",
    return_dict=True,
).to(model.device)

generated = model.generate(**inputs, max_new_tokens=500)
print(tokenizer.decode(generated[0][inputs["input_ids"].shape[-1]:]))
Previous Article

お名前.com:ドメイン0円キャンペーン – サーバー契約で更新料も永年無料!

Next Article

Hostinger:あらゆるウェブサイトのニーズに応える驚きの割引プラン

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *