{"cells":[{"cell_type":"markdown","metadata":{"id":"Y7Wdw0O6o2xa"},"source":["# 最終課題コンペ用 Fine-tuning テンプレート(unsloth)\n","\n","最終課題コンペにて Fine-tuning を行ないたい方に向けの Fine-tuning コードです。\n","こちらは L4 を利用できない受講生の方向けにUnslothを用いたものとなっております。\n","Google Colab の無料版で利用可能な T4 でも動作可能になっています。\n","環境設定の難易度が高いので、慎重に取り組んでいただければと思います。\n","\n","\n","### terminalでのconda環境構築(Omnicampusの環境などの場合)\n","事前にterminalで環境構築の必要があります。Google Colabでは不要です。 \n","```\n","# conda環境の構築\n","wget \"https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh\" \n","\n","# このコマンドではいくつか質問があるので答えて下さい。おそらくインストール先のデフォルトは/root/miniforge3かと思います \n","bash Miniforge3-$(uname)-$(uname -m).sh \n","\n","# 以下、インストール先が/root/miniforge3であることを前提とします \n","export PATH=/root/miniforge3/bin:$PATH \n","conda init \n","\n","# ここで一度、terminalを立ち上げ直す必要があります。 \n","# 以下のリンク先に従い環境を作ります。 \n","# https://docs.unsloth.ai/get-started/installation/conda-install \n","conda create --name unsloth_env python=3.10 pytorch-cuda=12.1 pytorch cudatoolkit xformers -c pytorch -c nvidia -c xformers -y \n","conda activate unsloth_env \n","pip install \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\" \n","pip install --no-deps \"trl<0.9.0\" peft accelerate bitsandbytes \n","\n","# jupyter notebook用のセットアップ。 \n","conda install -c conda-forge ipykernel \n","python -m ipykernel install --user --name=unsloth_env --display-name \"Python (unsloth_env)\" \n","```"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":2447,"status":"ok","timestamp":1734275884602,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"cfLPgVFUxua2","outputId":"583990d6-be2e-4f27-e8a9-eac00603e67f"},"outputs":[{"output_type":"stream","name":"stdout","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"]}],"source":["from google.colab import drive\n","drive.mount('/content/drive')"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":2834,"status":"ok","timestamp":1734275887435,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"ixz8RP8ci7be","outputId":"d57a0356-12ff-44f0-ccfc-bbda85a64ffd"},"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: datasets in /usr/local/lib/python3.10/dist-packages (3.2.0)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from datasets) (3.16.1)\n","Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from datasets) (1.26.4)\n","Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (17.0.0)\n","Requirement already satisfied: dill<0.3.9,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (0.3.8)\n","Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets) (2.2.2)\n","Requirement already satisfied: requests>=2.32.2 in /usr/local/lib/python3.10/dist-packages (from datasets) (2.32.3)\n","Requirement already satisfied: tqdm>=4.66.3 in /usr/local/lib/python3.10/dist-packages (from datasets) (4.66.6)\n","Requirement already satisfied: xxhash in /usr/local/lib/python3.10/dist-packages (from datasets) (3.5.0)\n","Requirement already satisfied: multiprocess<0.70.17 in /usr/local/lib/python3.10/dist-packages (from datasets) (0.70.16)\n","Requirement already satisfied: fsspec<=2024.9.0,>=2023.1.0 in /usr/local/lib/python3.10/dist-packages (from fsspec[http]<=2024.9.0,>=2023.1.0->datasets) (2024.9.0)\n","Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets) (3.11.10)\n","Requirement already satisfied: huggingface-hub>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (0.26.5)\n","Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from datasets) (24.2)\n","Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from datasets) (6.0.2)\n","Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (2.4.4)\n","Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.3.1)\n","Requirement already satisfied: async-timeout<6.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (4.0.3)\n","Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (24.2.0)\n","Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.5.0)\n","Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (6.1.0)\n","Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (0.2.1)\n","Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.18.3)\n","Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.23.0->datasets) (4.12.2)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (3.4.0)\n","Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (3.10)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (2.2.3)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (2024.8.30)\n","Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2.8.2)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)\n","Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.17.0)\n"]}],"source":["!pip install datasets"]},{"cell_type":"code","source":["# import os\n","# import json\n","# import pandas as pd\n","\n","# # ディレクトリパス\n","# data_dir = \"/content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/サンプルコード/Distribution20241221_all\"\n","\n","# # ディレクトリ内のすべてのJSONファイルを取得\n","# json_files = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if f.endswith(\".json\")]\n","\n","# # JSONファイルを読み込み、縦に統合\n","# all_data = []\n","# for json_file in json_files:\n","# try:\n","# with open(json_file, 'r', encoding='utf-8') as f:\n","# data = json.load(f) # JSONをロード\n","# if isinstance(data, list): # JSONがリスト形式の場合\n","# all_data.extend(data) # リストを結合\n","# else:\n","# all_data.append(data) # オブジェクトの場合そのまま追加\n","# except json.JSONDecodeError as e:\n","# print(f\"Error decoding JSON in file {json_file}: {e}\")\n","# except Exception as e:\n","# print(f\"Error reading file {json_file}: {e}\")\n","\n","# # 統合されたデータをDataFrameに変換\n","# if all_data: # データが存在する場合のみ実行\n","# merged_data = pd.DataFrame(all_data)\n","# # 結果を保存(必要に応じて)\n","# output_path = os.path.join(data_dir, \"merged_data.json\")\n","# merged_data.to_json(output_path, orient=\"records\", force_ascii=False, indent=4)\n","# print(f\"統合されたデータは {output_path} に保存されました。\")\n","# else:\n","# print(\"統合するデータが見つかりませんでした。\")\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"xm2oh2vC4090","executionInfo":{"status":"ok","timestamp":1734275887847,"user_tz":-540,"elapsed":415,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"}},"outputId":"a780a925-3eda-4e05-8314-901fcf5a8628"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Error decoding JSON in file /content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/サンプルコード/Distribution20241221_all/ichikara-instruction-003-002-1.json: Invalid \\escape: line 3765 column 324 (char 672927)\n","Error decoding JSON in file /content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/サンプルコード/Distribution20241221_all/ichikara-instruction-003-003-1.json: Invalid \\escape: line 5917 column 443 (char 1022307)\n","統合されたデータは /content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/サンプルコード/Distribution20241221_all/merged_data.json に保存されました。\n"]}]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":11200,"status":"ok","timestamp":1734275899044,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"NeKrhsB3Q_3H","colab":{"base_uri":"https://localhost:8080/"},"outputId":"48a4b66a-e9dc-4806-888a-403eafd223a3"},"outputs":[{"output_type":"stream","name":"stdout","text":["Found existing installation: unsloth 2024.12.4\n","Uninstalling unsloth-2024.12.4:\n"," Successfully uninstalled unsloth-2024.12.4\n","Collecting unsloth@ git+https://github.com/unslothai/unsloth.git (from unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git)\n"," Cloning https://github.com/unslothai/unsloth.git to /tmp/pip-install-l7sfds2s/unsloth_c59ff28c769e422fa9a79df247958f09\n"," Running command git clone --filter=blob:none --quiet https://github.com/unslothai/unsloth.git /tmp/pip-install-l7sfds2s/unsloth_c59ff28c769e422fa9a79df247958f09\n"," Resolved https://github.com/unslothai/unsloth.git to commit 85f1fa096afde5efe2fb8521d8ceec8d13a00715\n"," Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n"," Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n"," Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n","Requirement already satisfied: unsloth_zoo>=2024.11.8 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2024.12.1)\n","Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (24.2)\n","Requirement already satisfied: tyro in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.9.2)\n","Requirement already satisfied: transformers>=4.46.1 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (4.46.3)\n","Requirement already satisfied: datasets>=2.16.0 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.2.0)\n","Requirement already satisfied: sentencepiece>=0.2.0 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.2.0)\n","Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (4.66.6)\n","Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (5.9.5)\n","Requirement already satisfied: wheel>=0.42.0 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.45.1)\n","Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.26.4)\n","Requirement already satisfied: protobuf<4.0.0 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.20.3)\n","Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.26.5)\n","Requirement already satisfied: hf_transfer in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.1.8)\n","Requirement already satisfied: bitsandbytes>=0.43.3 in /usr/local/lib/python3.10/dist-packages (from unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.45.0)\n","Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.5.1+cu121)\n","Requirement already satisfied: typing_extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (4.12.2)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.16.1)\n","Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (17.0.0)\n","Requirement already satisfied: dill<0.3.9,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.3.8)\n","Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.2.2)\n","Requirement already satisfied: requests>=2.32.2 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.32.3)\n","Requirement already satisfied: xxhash in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.5.0)\n","Requirement already satisfied: multiprocess<0.70.17 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.70.16)\n","Requirement already satisfied: fsspec<=2024.9.0,>=2023.1.0 in /usr/local/lib/python3.10/dist-packages (from fsspec[http]<=2024.9.0,>=2023.1.0->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2024.9.0)\n","Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.11.10)\n","Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (6.0.2)\n","Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers>=4.46.1->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2024.9.11)\n","Requirement already satisfied: tokenizers<0.21,>=0.20 in /usr/local/lib/python3.10/dist-packages (from transformers>=4.46.1->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.20.3)\n","Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers>=4.46.1->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.4.5)\n","Requirement already satisfied: triton in /usr/local/lib/python3.10/dist-packages (from unsloth_zoo>=2024.11.8->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.1.0)\n","Requirement already satisfied: accelerate>=0.34.1 in /usr/local/lib/python3.10/dist-packages (from unsloth_zoo>=2024.11.8->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.1.1)\n","Requirement already satisfied: trl!=0.9.0,!=0.9.1,!=0.9.2,!=0.9.3,>=0.7.9 in /usr/local/lib/python3.10/dist-packages (from unsloth_zoo>=2024.11.8->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.12.2)\n","Requirement already satisfied: peft!=0.11.0,>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from unsloth_zoo>=2024.11.8->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.13.2)\n","Requirement already satisfied: cut_cross_entropy in /usr/local/lib/python3.10/dist-packages (from unsloth_zoo>=2024.11.8->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (24.12.2)\n","Requirement already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (from unsloth_zoo>=2024.11.8->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (11.0.0)\n","Requirement already satisfied: docstring-parser>=0.16 in /usr/local/lib/python3.10/dist-packages (from tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.16)\n","Requirement already satisfied: rich>=11.1.0 in /usr/local/lib/python3.10/dist-packages (from tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (13.9.4)\n","Requirement already satisfied: shtab>=1.5.6 in /usr/local/lib/python3.10/dist-packages (from tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.7.1)\n","Requirement already satisfied: typeguard>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (4.4.1)\n","Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.4.4)\n","Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.3.1)\n","Requirement already satisfied: async-timeout<6.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (4.0.3)\n","Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (24.2.0)\n","Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.5.0)\n","Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (6.1.0)\n","Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.2.1)\n","Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.18.3)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.4.0)\n","Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.10)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.2.3)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2024.8.30)\n","Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=11.1.0->tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.0.0)\n","Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=11.1.0->tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.18.0)\n","Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.4.2)\n","Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.1.4)\n","Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch->bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.13.1)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch->bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.3.0)\n","Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2.8.2)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2024.2)\n","Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (2024.2)\n","Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=11.1.0->tyro->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (0.1.2)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->datasets>=2.16.0->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (1.17.0)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->bitsandbytes>=0.43.3->unsloth@ git+https://github.com/unslothai/unsloth.git->unsloth[colab-new]@ git+https://github.com/unslothai/unsloth.git) (3.0.2)\n","Building wheels for collected packages: unsloth\n"," Building wheel for unsloth (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n"," Created wheel for unsloth: filename=unsloth-2024.12.4-py3-none-any.whl size=173746 sha256=c7779439cf6598a9c98a91b7bafa3a0be666bdd2dd8b69ba9a6ac1bd71e8a9d4\n"," Stored in directory: /tmp/pip-ephem-wheel-cache-_jca5i07/wheels/ed/d4/e9/76fb290ee3df0a5fc21ce5c2c788e29e9607a2353d8342fd0d\n","Successfully built unsloth\n","Installing collected packages: unsloth\n","Successfully installed unsloth-2024.12.4\n"]}],"source":["# Google Colab の場合は上記の環境構築手順を行なわず、単にこのセルから実行していってください。\n","!pip uninstall unsloth -y\n","!pip install --upgrade --no-cache-dir \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\""]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":6057,"status":"ok","timestamp":1734275905100,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"knPVRpxkXSNW","colab":{"base_uri":"https://localhost:8080/"},"outputId":"a4d449ce-8298-477a-84ab-5cddeb75fd15"},"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (2.5.1+cu121)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch) (3.16.1)\n","Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch) (4.12.2)\n","Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch) (3.4.2)\n","Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch) (3.1.4)\n","Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch) (2024.9.0)\n","Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch) (1.13.1)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch) (1.3.0)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch) (3.0.2)\n","Requirement already satisfied: xformers in /usr/local/lib/python3.10/dist-packages (0.0.28.post3)\n","Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from xformers) (1.26.4)\n","Requirement already satisfied: torch==2.5.1 in /usr/local/lib/python3.10/dist-packages (from xformers) (2.5.1+cu121)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch==2.5.1->xformers) (3.16.1)\n","Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch==2.5.1->xformers) (4.12.2)\n","Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch==2.5.1->xformers) (3.4.2)\n","Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch==2.5.1->xformers) (3.1.4)\n","Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch==2.5.1->xformers) (2024.9.0)\n","Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch==2.5.1->xformers) (1.13.1)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch==2.5.1->xformers) (1.3.0)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch==2.5.1->xformers) (3.0.2)\n"]}],"source":["# Google Colab のデフォルトで入っているパッケージをアップグレード(Moriyasu さんありがとうございます)\n","!pip install --upgrade torch\n","!pip install --upgrade xformers"]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":2929,"status":"ok","timestamp":1734275908028,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"kAKC2CnIkEgO","colab":{"base_uri":"https://localhost:8080/"},"outputId":"807c679e-c5dd-454b-a46b-46ac7a1fb0db"},"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: ipywidgets in /usr/local/lib/python3.10/dist-packages (8.1.5)\n","Requirement already satisfied: comm>=0.1.3 in /usr/local/lib/python3.10/dist-packages (from ipywidgets) (0.2.2)\n","Requirement already satisfied: ipython>=6.1.0 in /usr/local/lib/python3.10/dist-packages (from ipywidgets) (7.34.0)\n","Requirement already satisfied: traitlets>=4.3.1 in /usr/local/lib/python3.10/dist-packages (from ipywidgets) (5.7.1)\n","Requirement already satisfied: widgetsnbextension~=4.0.12 in /usr/local/lib/python3.10/dist-packages (from ipywidgets) (4.0.13)\n","Requirement already satisfied: jupyterlab-widgets~=3.0.12 in /usr/local/lib/python3.10/dist-packages (from ipywidgets) (3.0.13)\n","Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (75.1.0)\n","Requirement already satisfied: jedi>=0.16 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (0.19.2)\n","Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (4.4.2)\n","Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (0.7.5)\n","Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (3.0.48)\n","Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (2.18.0)\n","Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (0.2.0)\n","Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (0.1.7)\n","Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython>=6.1.0->ipywidgets) (4.9.0)\n","Requirement already satisfied: parso<0.9.0,>=0.8.4 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.4)\n","Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0)\n","Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=6.1.0->ipywidgets) (0.2.13)\n"]}],"source":["# notebookでインタラクティブな表示を可能とする(ただし、うまく動かない場合あり)\n","# Google Colabでは実行不要\n","!pip install ipywidgets --upgrade"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6-t3HhxN8joX"},"outputs":[],"source":["# Install Flash Attention 2 for softcapping support\n","import torch\n","#if torch.cuda.get_device_capability()[0] >= 8:\n"," #!pip install --no-deps packaging ninja einops \"flash-attn>=2.6.3\""]},{"cell_type":"markdown","metadata":{"id":"25o4Cq9id4YC"},"source":["## モデルのロード\n","以下のコードでモデルを読み込みます。 \n","受講生の方からご指摘頂いたのですが、unslothでgemma2を読み込むと、自動でunslothが作成した非公式モデルがダウンロードされるようです。 \n","対処方法がわからない受講生はLLM-jp-3のみをご利用ください! "]},{"cell_type":"code","execution_count":null,"metadata":{"id":"WAaS0RKXgG72"},"outputs":[],"source":["# Hugging Face Token を指定\n","# 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。\n","# Write権限を付与してください。\n","# https://huggingface.co/settings/tokens\n","HF_TOKEN = \"input_yourpassword\" #@param {type:\"string\"}\n","\n","# あるいは Google Colab シークレットを使う場合、左のサイドバーより🔑マークをクリック\n","# HF_TOKEN という名前で Value に Hugging Face Token を入れてください。\n","# ノートブックからのアクセスのトグルをオンにし、下記の二行のコードのコメントアウトを外してください。\n","\n","# from google.colab import userdata\n","# HF_TOKEN=userdata.get('HF_TOKEN')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_fo4ylzJ0Ft9"},"outputs":[],"source":["from google.colab import output\n","output.enable_custom_widget_manager()"]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":140171,"status":"ok","timestamp":1734276050012,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"LbmtYWUH8p_J","colab":{"base_uri":"https://localhost:8080/","height":243,"referenced_widgets":["6835b97849554a26850917c7fd5ee7c0","c4a6e3ac8efe4f4b83268803744f02f1","d09e8feeebf94bb1bc36f18bc1bffe06","33a98b120a1d4f5495f7352a92b85c65","ed713975227a4ed49cfd5d39f49b93b7","f5b2137b874146e7bc02105a57818b6e","f9b50cdf659547329596a0f7d7060f69","1b88cf54738a48b582921f79b38f375a","1950c83da8fa40629dda25aed593bf43","3f98740e7ea24e1ea5302d0d37ac2778","7de23801c473415ba0eec75fff065969"]},"outputId":"2464455b-55ee-436a-ac64-58486032fd91"},"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","Unsloth: WARNING `trust_remote_code` is True.\n","Are you certain you want to do remote code execution?\n","==((====))== Unsloth 2024.12.4: Fast Llama patching. Transformers:4.46.3.\n"," \\\\ /| GPU: NVIDIA L4. Max memory: 22.168 GB. Platform: Linux.\n","O^O/ \\_/ \\ Torch: 2.5.1+cu121. CUDA: 8.9. CUDA Toolkit: 12.1. Triton: 3.1.0\n","\\ / Bfloat16 = TRUE. 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":["Loading checkpoint shards: 0%| | 0/6 [00:00\n"]}],"source":["# データを確認\n","print(dataset[\"train\"][\"formatted_text\"][3])"]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":2852,"status":"ok","timestamp":1734276265702,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"GHSyrGcEQ_3I","colab":{"base_uri":"https://localhost:8080/","height":17,"referenced_widgets":["9e2c0203ea3141ed9f2fc9c7dc448ee1","01c1270916b44c20b13c671b6b4a15e0","4706c9cd9f2d41989f87d20e22851530","42947fb0c8fb4962b53692c17208c3ea","7a69af4df4554bb6a1f3ee7f26042618","7f39f252a22d48d492aee310782a5529","f599606e39f24112b7ae757c27966dee","d4000de9ba4c467ca4dcb926ae8514d0","19fad2440b2b4ff9a67c7a2bd871b155","8e5030a391224a21bf5b4305a20a59c5","fb1361bc24a2451aa3c352844125775e"]},"outputId":"17e30814-3754-4d6d-ff03-e4da62b24eaf"},"outputs":[{"output_type":"display_data","data":{"text/plain":["Map: 0%| | 0/5806 [00:00"],"text/html":["\n","
\n"," \n"," \n"," [725/725 1:14:32, Epoch 0/1]\n","
\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
StepTraining Loss
102.163000
201.991100
301.849500
401.790100
501.691200
601.853300
701.883600
801.859800
901.722000
1001.648500
1101.974900
1201.849200
1301.769100
1401.664900
1501.541700
1601.965900
1701.801900
1801.820800
1901.746900
2001.547600
2101.896400
2201.731700
2301.743000
2401.618800
2501.566300
2601.906500
2701.763500
2801.710100
2901.722800
3001.495200
3101.860900
3201.686700
3301.732300
3401.561300
3501.371700
3601.809600
3701.742700
3801.658000
3901.521600
4001.258800
4101.833100
4201.653200
4301.611900
4401.501300
4501.260800
4601.927100
4701.713000
4801.639300
4901.494300
5001.298300
5101.716600
5201.655600
5301.532200
5401.493700
5501.181300
5601.767600
5701.577500
5801.515700
5901.453400
6001.336300
6101.773400
6201.642600
6301.448900
6401.374000
6501.115100
6601.749800
6701.627600
6801.514000
6901.365500
7001.079500
7101.662800
7201.400700

"]},"metadata":{}}],"source":["#@title 学習実行\n","trainer_stats = trainer.train()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"z_P7-b0_Q_3J"},"outputs":[],"source":["# ELYZA-tasks-100-TVの読み込み。事前にファイルをアップロードしてください\n","# データセットの読み込み。\n","# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。\n","import json\n","datasets = []\n","with open(\"/content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/elyza-tasks-100-TV_0.jsonl\", \"r\") as f:\n"," item = \"\"\n"," for line in f:\n"," line = line.strip()\n"," item += line\n"," if item.endswith(\"}\"):\n"," datasets.append(json.loads(item))\n"," item = \"\""]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":656833,"status":"ok","timestamp":1734281415883,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"ZsbwPAOJQ_3J","colab":{"base_uri":"https://localhost:8080/"},"outputId":"1888df37-047d-46a4-90e7-e887df723122"},"outputs":[{"output_type":"stream","name":"stderr","text":["100%|██████████| 100/100 [10:56<00:00, 6.57s/it]\n"]}],"source":["# 学習したモデルを用いてタスクを実行\n","from tqdm import tqdm\n","\n","# 推論するためにモデルのモードを変更\n","FastLanguageModel.for_inference(model)\n","\n","results = []\n","for dt in tqdm(datasets):\n"," input = dt[\"input\"]\n","\n"," prompt = f\"\"\"### 指示\\n{input}\\n### 回答\\n\"\"\"\n","\n"," inputs = tokenizer([prompt], return_tensors = \"pt\").to(model.device)\n","\n"," outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)\n"," prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\\n### 回答')[-1]\n","\n"," results.append({\"task_id\": dt[\"task_id\"], \"input\": input, \"output\": prediction})"]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":14,"status":"ok","timestamp":1734281415883,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"AUZb3AG8_aTK","colab":{"base_uri":"https://localhost:8080/"},"outputId":"38b6ed74-2238-41a3-d205-f2aac7d668fe"},"outputs":[{"output_type":"stream","name":"stdout","text":["/content\n"]}],"source":["import os\n","print(os.getcwd())\n"]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":14,"status":"ok","timestamp":1734281415884,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"3rFK3Vo9_3pa","colab":{"base_uri":"https://localhost:8080/"},"outputId":"c3628819-db48-4e34-97ec-fc82af1acf74"},"outputs":[{"output_type":"stream","name":"stdout","text":["現在のカレントディレクトリ: /content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/サンプルコード\n"]}],"source":["import os\n","\n","# 移動したいディレクトリのパスを指定\n","new_directory = \"/content/drive/MyDrive/2024メタバース工学部LLM/03.教材(講義前後にUP予定)/05.最終課題/サンプルコード\"\n","\n","# カレントディレクトリを変更\n","os.chdir(new_directory)\n","\n","# 確認: カレントディレクトリを表示\n","print(\"現在のカレントディレクトリ:\", os.getcwd())"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pd8VbKeIQ_3J"},"outputs":[],"source":["# jsonlで保存\n","with open(f\"{new_model_id}_output.jsonl\", 'w', encoding='utf-8') as f:\n"," for result in results:\n"," json.dump(result, f, ensure_ascii=False)\n"," f.write('\\n')"]},{"cell_type":"markdown","metadata":{"id":"SZrFExsmCdXq"},"source":["モデルとトークナイザーをHugging Faceにアップロードします。 \n","本コードではLoRAのアダブタのみを保存します。 \n","このアダプタを用いた推論方法はModel_Inference_Template_unsloth_20241127.ipynbをご参照ください。 \n"," \n","一旦privateでアップロードしてください。 \n","https://docs.unsloth.ai/basics/saving-and-using-models"]},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":15252,"status":"ok","timestamp":1734281431124,"user":{"displayName":"菊池満帆","userId":"10441799421875206511"},"user_tz":-540},"id":"GsLyYfZlF2qE","colab":{"base_uri":"https://localhost:8080/","height":86,"referenced_widgets":["d8673be4d10d4846a6e91be87364b83f","f401e60436144eab852362d090d05acd","f2f1cc4aa9b64e9190f60401ba7a2bcf","a6ea23e5a0ae484eb2b542b6eb4f5f60","ad5f7937ee5244d2b26f73d26997dbc8","c23411519d2048ac807a400ddf0e4b26","5fb2a7403eae415e97201e7f39c4a913","2b148c6af6a242da8808079dc64fec61","f00d9f56370e4172b9ab68e9785d0ab6","2cd1eef9e3a341e4aa7df850e309a612","77a8189354ac4c7a871bd3dd172706c0","f0d4bad1c7ba48a890f6bb9daeebf2bc","2142f9910e8f4fa9a838c69588609361","24492813d0a84f8b89e89239ba5c9037","5b3d91b8499c45ad9b9cc644e1c28e32","f8d4aa4659c5440dbcead8ba21d66265","6b917b2174034acab6f97551cab4fb51","db8d2a58f62843d8ac6583b6b6e1628f","a234e9d9850446e7b2f406a59a4313ce","17862f160633428ab5f7fca35812c01a","5624ab4dbe064a4880ea7a58b47b1574","10e5812322a6405cbea9a839f56cbbe8"]},"outputId":"1cd43188-36f4-47aa-bc8f-97e295345c74"},"outputs":[{"output_type":"stream","name":"stdout","text":["Unsloth: Saving LoRA adapters. Please wait...\n"]},{"output_type":"display_data","data":{"text/plain":[" 0%| | 0/1 [00:00