|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "id": "wGfI8meEHXfM" |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "[](https://colab.research.google.com/github/openai/openai-cookbook/blob/main/articles/gpt-oss/run-colab.ipynb)" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": { |
| 15 | + "id": "gj6KvThm8Jjn" |
| 16 | + }, |
| 17 | + "source": [ |
| 18 | + "# Run OpenAI gpt-oss 20B in a FREE Google Colab\n", |
| 19 | + "\n", |
| 20 | + "OpenAI released `gpt-oss` [120B](https://hf.co/openai/gpt-oss-120b) and [20B](https://hf.co/openai/gpt-oss-20b). Both models are Apache 2.0 licensed.\n", |
| 21 | + "\n", |
| 22 | + "Specifically, `gpt-oss-20b` was made for lower latency and local or specialized use cases (21B parameters with 3.6B active parameters).\n", |
| 23 | + "\n", |
| 24 | + "Since the models were trained in native MXFP4 quantization it makes it easy to run the 20B even in resource constrained environments like Google Colab.\n", |
| 25 | + "\n", |
| 26 | + "Authored by: [Pedro](https://huggingface.co/pcuenq) and [VB](https://huggingface.co/reach-vb)" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": { |
| 32 | + "id": "Kv2foJJa9Xkc" |
| 33 | + }, |
| 34 | + "source": [ |
| 35 | + "## Setup environment" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": { |
| 41 | + "id": "zMRXDOpY1Q3Q" |
| 42 | + }, |
| 43 | + "source": [ |
| 44 | + "Since support for mxfp4 in transformers is bleeding edge, we need a recent version of PyTorch and CUDA, in order to be able to install the `mxfp4` triton kernels.\n", |
| 45 | + "\n", |
| 46 | + "We also need to install transformers from source, and we uninstall `torchvision` and `torchaudio` to remove dependency conflicts." |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "metadata": { |
| 53 | + "id": "4gUEKrLEvJmf" |
| 54 | + }, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "!pip install -q --upgrade torch" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "metadata": { |
| 64 | + "id": "3N00UT7gtpkp" |
| 65 | + }, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "!pip install -q transformers triton==3.4 kernels" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "metadata": { |
| 75 | + "id": "7GW0knW2w3ND" |
| 76 | + }, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "!pip uninstall -q torchvision torchaudio -y" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "metadata": { |
| 85 | + "id": "pxU0WKwtH19m" |
| 86 | + }, |
| 87 | + "source": [ |
| 88 | + "Please, restart your Colab runtime session after installing the packages above." |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": { |
| 94 | + "id": "D3xCxY159frD" |
| 95 | + }, |
| 96 | + "source": [ |
| 97 | + "## Load the model from Hugging Face in Google Colab\n", |
| 98 | + "\n", |
| 99 | + "We load the model from here: [openai/gpt-oss-20b](https://hf.co/openai/gpt-oss-20b)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "metadata": { |
| 106 | + "id": "k2HFwdkXu2R1" |
| 107 | + }, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "from transformers import AutoModelForCausalLM, AutoTokenizer\n", |
| 111 | + "\n", |
| 112 | + "model_id = \"openai/gpt-oss-20b\"\n", |
| 113 | + "\n", |
| 114 | + "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", |
| 115 | + "model = AutoModelForCausalLM.from_pretrained(\n", |
| 116 | + " model_id,\n", |
| 117 | + " torch_dtype=\"auto\",\n", |
| 118 | + " device_map=\"cuda\",\n", |
| 119 | + ")" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "metadata": { |
| 125 | + "id": "Jbeq6kN79ql0" |
| 126 | + }, |
| 127 | + "source": [ |
| 128 | + "## Setup messages/ chat\n", |
| 129 | + "\n", |
| 130 | + "You can provide an optional system prompt or directly the input." |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": null, |
| 136 | + "metadata": { |
| 137 | + "id": "P5dJV3xsu_89" |
| 138 | + }, |
| 139 | + "outputs": [], |
| 140 | + "source": [ |
| 141 | + "messages = [\n", |
| 142 | + " {\"role\": \"system\", \"content\": \"Always respond in riddles\"},\n", |
| 143 | + " {\"role\": \"user\", \"content\": \"What is the weather like in Madrid?\"},\n", |
| 144 | + "]\n", |
| 145 | + "\n", |
| 146 | + "inputs = tokenizer.apply_chat_template(\n", |
| 147 | + " messages,\n", |
| 148 | + " add_generation_prompt=True,\n", |
| 149 | + " return_tensors=\"pt\",\n", |
| 150 | + " return_dict=True,\n", |
| 151 | + ").to(model.device)\n", |
| 152 | + "\n", |
| 153 | + "generated = model.generate(**inputs, max_new_tokens=500)\n", |
| 154 | + "print(tokenizer.decode(generated[0][inputs[\"input_ids\"].shape[-1]:]))" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "markdown", |
| 159 | + "metadata": { |
| 160 | + "id": "ksxo7bjR_-th" |
| 161 | + }, |
| 162 | + "source": [ |
| 163 | + "## Specify Reasoning Effort" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "metadata": { |
| 169 | + "id": "fcv6QdcQKLr0" |
| 170 | + }, |
| 171 | + "source": [ |
| 172 | + "Simply pass it as an additional argument to `apply_chat_template()`. Supported values are `\"low\"`, `\"medium\"` (default), or `\"high\"`." |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": { |
| 179 | + "id": "CmnkAle608Hl" |
| 180 | + }, |
| 181 | + "outputs": [], |
| 182 | + "source": [ |
| 183 | + "messages = [\n", |
| 184 | + " {\"role\": \"system\", \"content\": \"Always respond in riddles\"},\n", |
| 185 | + " {\"role\": \"user\", \"content\": \"Explain why the meaning of life is 42\"},\n", |
| 186 | + "]\n", |
| 187 | + "\n", |
| 188 | + "inputs = tokenizer.apply_chat_template(\n", |
| 189 | + " messages,\n", |
| 190 | + " add_generation_prompt=True,\n", |
| 191 | + " return_tensors=\"pt\",\n", |
| 192 | + " return_dict=True,\n", |
| 193 | + " reasoning_effort=\"high\",\n", |
| 194 | + ").to(model.device)\n", |
| 195 | + "\n", |
| 196 | + "generated = model.generate(**inputs, max_new_tokens=500)\n", |
| 197 | + "print(tokenizer.decode(generated[0][inputs[\"input_ids\"].shape[-1]:]))" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "markdown", |
| 202 | + "metadata": { |
| 203 | + "id": "Tf2-ocGqEC_r" |
| 204 | + }, |
| 205 | + "source": [ |
| 206 | + "## Try out other prompts and ideas!\n", |
| 207 | + "\n", |
| 208 | + "Check out our blogpost for other ideas: [https://hf.co/blog/welcome-openai-gpt-oss](https://hf.co/blog/welcome-openai-gpt-oss)" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": null, |
| 214 | + "metadata": { |
| 215 | + "id": "2QrnTpcCKd_n" |
| 216 | + }, |
| 217 | + "outputs": [], |
| 218 | + "source": [] |
| 219 | + } |
| 220 | + ], |
| 221 | + "metadata": { |
| 222 | + "accelerator": "GPU", |
| 223 | + "colab": { |
| 224 | + "gpuType": "T4", |
| 225 | + "provenance": [] |
| 226 | + }, |
| 227 | + "kernelspec": { |
| 228 | + "display_name": "Python 3 (ipykernel)", |
| 229 | + "language": "python", |
| 230 | + "name": "python3" |
| 231 | + }, |
| 232 | + "language_info": { |
| 233 | + "codemirror_mode": { |
| 234 | + "name": "ipython", |
| 235 | + "version": 3 |
| 236 | + }, |
| 237 | + "file_extension": ".py", |
| 238 | + "mimetype": "text/x-python", |
| 239 | + "name": "python", |
| 240 | + "nbconvert_exporter": "python", |
| 241 | + "pygments_lexer": "ipython3", |
| 242 | + "version": "3.12.7" |
| 243 | + } |
| 244 | + }, |
| 245 | + "nbformat": 4, |
| 246 | + "nbformat_minor": 0 |
| 247 | +} |
0 commit comments