{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "MljifiTVCT0_" }, "source": [ "# 推論用コード\n", "本コードはunslothで学習したqLoRAのアダプタを用いてELYZA-tasks-100-TVの出力を得るためのコードです。 \n", "Hugging Faceにアダプタをアップロードしてあることが前提となります。\n", "このコードはunslothライブラリを用いてモデルを読み込み、推論するためのコードとなります。\n", "このコードで生成されたjsonlファイルは課題の成果として提出可能なフォーマットになっております。\n", "\n", "※本コードはGoogle Colabでの動作を想定しており、Omnicampusでの動作を想定しておりません。\n", "Omnicampus向けのコードは別途用意しております。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "I5B5MOHuBy8b" }, "outputs": [], "source": [ "# 必要なライブラリをインストール\n", "%%capture\n", "!pip install unsloth\n", "!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\"\n", "!pip install -U torch\n", "!pip install -U peft" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "GM7SNRtACg9V", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "fd450612-e728-4855-b43e-a4253da2d0c8" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n", "🦥 Unsloth Zoo will now patch everything to make training faster!\n" ] } ], "source": [ "# 必要なライブラリを読み込み\n", "from unsloth import FastLanguageModel\n", "from peft import PeftModel\n", "import torch\n", "import json\n", "from tqdm import tqdm\n", "import re" ] }, { "cell_type": "code", "source": [ "# In case you are using your Google Drive, execute this block\n", "\n", "# from google.colab import drive\n", "# drive.mount('/gdrive')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rPUq6oNghAq4", "outputId": "96cc93ca-fa61-4044-b673-41d654cc465f" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mounted at /gdrive\n" ] } ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "JmdUATTVCtyr" }, "outputs": [], "source": [ "# ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。\n", "model_id = \"google/gemma-2-9b-it\"\n", "adapter_id = \"ganger/gemma-2-9b-it-fd_lora\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "myNnu-A0fVVD" }, "outputs": [], "source": [ "# Hugging Face Token を指定。\n", "# 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。\n", "# https://huggingface.co/settings/tokens\n", "HF_TOKEN = \"\" #@param {type:\"string\"}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "TB6Hzx-2B5g8", "colab": { "base_uri": "https://localhost:8080/", "height": 348, "referenced_widgets": [ "bedd22af201047ad90cd74b0ade535e2", "979609e0a89744aa93a5e4dbbb3c209f", "46b50071237a484890ea0919f98aa49f", "fc61b3b99481497bbc346659404ed9ad", "b6d82816c02d437fb7c8b5fc2b7429cd", "814fa9b6a0f04d1897c61858f04e81ed", "e8174e5cb9684a269d5437de65691249", "15fdbe49873d498b838e408364e2d707", "5ef66b7ebae54e2b8b6a76fa851c4845", "6b15b6b2cb1e462ab125f9825f796ebd", "8591d5852aab4f60b92c839060eda185", "a57e7ea2bfd94b3985ddcd854362f3b8", "3c392abdfb53419685969258064aeb03", "223a4bbc2f0342e1809c33132ef1283a", "fccb7e4a2caf421d99505bebe85ac39d", "9fde47b867b545dead8aeb60ede02586", "49f2173d2ea24820864ba4d1985d394d", "bb0f806df9c3496e93009f4a96f9c10f", "4b6ed533a5de4564a8609711cf2373c3", "dd774389c1ae4f2f9d32904119aabc40", "ddc5ec60c37545ed94d3ce398becd04b", "99c3f37306e148e281261444328fa264", "85df8ad439ec4c6fbbba948a05ab2c45", "ae8399c78ec2434ea7e49158527df5ff", "ab3569cc780c4d7ba475429fbf671937", "bd31af27748f4210b65ab0d3e2fc3c44", "ef0fc62025dc4781a32e3e6e413269f2", "936b0099b8a14707b9ff7a7a5794b47e", "65004deb3f084496a9ab96f5d09b4277", "fe3410bd99cd42c98bb43db62df5ec26", "4b2a7d2e570b4053965b7d1df7859988", "9dd85e018e6d4d2f97691131a42ca964", "645d3078c0264175934bfe0a3e1f8d4f", "9f41071f1ff542dda8c50cdeb910743b", "9587ba6e0583451faf7ec8e5d65b67ab", "f841ef5463a74cc7bec8eefe49e27145", "30db7b07af784c2f831715ebbee0943d", "52a04e81b1af4349b60510316a2f8570", "090e4ee312fd4812a7b09ab91d072a40", "42e123a555814836bbb3e41691d7c5fa", "69228a3b884d4a308d903617c603b51f", "c877e41d6c8c441783d7633c9ae4762c", "24f51412ec6444adbc78bc7bbaaa541e", "8637bcebe435414e8bdd92b50fb96d19", "e34df316c41b4beebfb6a7a54a69f63e", "df9f6c8ba4d045efafb5446e51fe202b", "cdf770194b484d58a066af45b6d62576", "f403a547633b4290b6f76bd119ecba51", "0b89ddd2f17747bc85f9adbb16dd5c13", "7def6352049b4c42ab6309c967d3463a", "a858a816cd1d4587ae9f4318ffbe9415", "bcee2f7388ef47ccbeac294705790dc8", "c1761a9a3d8e45bf8f3ba6b7bf721a00", "d538ed87726a47cd80476d1984b21dd4", "f153b748aae54598babca5e825ed76ae", "ec78e36961ae4a08b194464ae50839b6", "671db1da7c6f416c82cca43480cabf8d", "9175269bfa9f4e0097bf2e324d16c708", "b10eb16578fa4c9dba7c21582509ab34", "7d3766fb420a45d68c99b3b89e08eabe", "a440055b9fad4714bd54cd652b8f72f8", "7238ad11f7534df4b4699d4927d313d9", "af3b00b34d814a8dba46b9d708f0f3e4", "eab9770d4b5244b8870d64b28c05a13c", "3c683540cfdc4df4a350119c4d23e7f4", "2fbb9a41a9d84aacbb857ce50c35cfb0" ] }, "outputId": "a7c23631-1a60-4106-8024-2800237187b2" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Unsloth: WARNING `trust_remote_code` is True.\n", "Are you certain you want to do remote code execution?\n", "==((====))== Unsloth 2024.12.4: Fast Gemma2 patching. Transformers:4.46.3.\n", " \\\\ /| GPU: Tesla T4. Max memory: 14.748 GB. Platform: Linux.\n", "O^O/ \\_/ \\ Torch: 2.5.1+cu121. CUDA: 7.5. CUDA Toolkit: 12.1. Triton: 3.1.0\n", "\\ / Bfloat16 = FALSE. FA [Xformers = 0.0.28.post3. FA2 = False]\n", " \"-____-\" Free Apache license: http://github.com/unslothai/unsloth\n", "Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "model.safetensors: 0%| | 0.00/6.13G [00:00