{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b65299ef-8f7d-4ed6-8b7c-ad6b010e15d4", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting transformers==4.36.2\n", " Downloading transformers-4.36.2-py3-none-any.whl (8.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.2/8.2 MB\u001b[0m \u001b[31m28.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting datasets==2.16.1\n", " Downloading datasets-2.16.1-py3-none-any.whl (507 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m507.1/507.1 kB\u001b[0m \u001b[31m266.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting accelerate==0.26.1\n", " Downloading accelerate-0.26.1-py3-none-any.whl (270 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m270.9/270.9 kB\u001b[0m \u001b[31m272.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting evaluate==0.4.1\n", " Downloading evaluate-0.4.1-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.1/84.1 kB\u001b[0m \u001b[31m248.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting bitsandbytes==0.42.0\n", " Downloading bitsandbytes-0.42.0-py3-none-any.whl (105.0 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m105.0/105.0 MB\u001b[0m \u001b[31m87.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting trl==0.7.10\n", " Downloading trl-0.7.10-py3-none-any.whl (150 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m150.9/150.9 kB\u001b[0m \u001b[31m186.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting peft==0.7.1\n", " Downloading peft-0.7.1-py3-none-any.whl (168 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m168.3/168.3 kB\u001b[0m \u001b[31m222.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from transformers==4.36.2->-r requirements.txt (line 1)) (3.14.0)\n", "Collecting huggingface-hub<1.0,>=0.19.3\n", " Downloading huggingface_hub-0.24.5-py3-none-any.whl (417 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m417.5/417.5 kB\u001b[0m \u001b[31m317.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: requests in /opt/app-root/lib/python3.9/site-packages (from transformers==4.36.2->-r requirements.txt (line 1)) (2.32.2)\n", "Collecting safetensors>=0.3.1\n", " Downloading safetensors-0.4.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (436 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m436.3/436.3 kB\u001b[0m \u001b[31m127.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting tokenizers<0.19,>=0.14\n", " Downloading tokenizers-0.15.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.6/3.6 MB\u001b[0m \u001b[31m92.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: tqdm>=4.27 in /opt/app-root/lib/python3.9/site-packages (from transformers==4.36.2->-r requirements.txt (line 1)) (4.66.4)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from transformers==4.36.2->-r requirements.txt (line 1)) (24.0)\n", "Collecting regex!=2019.12.17\n", " Downloading regex-2024.7.24-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (775 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m775.9/775.9 kB\u001b[0m \u001b[31m262.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from transformers==4.36.2->-r requirements.txt (line 1)) (6.0.1)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from transformers==4.36.2->-r requirements.txt (line 1)) (1.24.4)\n", "Collecting pyarrow-hotfix\n", " Downloading pyarrow_hotfix-0.6-py3-none-any.whl (7.9 kB)\n", "Collecting fsspec[http]<=2023.10.0,>=2023.1.0\n", " Downloading fsspec-2023.10.0-py3-none-any.whl (166 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m166.4/166.4 kB\u001b[0m \u001b[31m288.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting dill<0.3.8,>=0.3.0\n", " Downloading dill-0.3.7-py3-none-any.whl (115 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m115.3/115.3 kB\u001b[0m \u001b[31m144.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: aiohttp in /opt/app-root/lib/python3.9/site-packages (from datasets==2.16.1->-r requirements.txt (line 2)) (3.9.5)\n", "Requirement already satisfied: pyarrow>=8.0.0 in /opt/app-root/lib/python3.9/site-packages (from datasets==2.16.1->-r requirements.txt (line 2)) (14.0.1)\n", "Requirement already satisfied: pandas in /opt/app-root/lib/python3.9/site-packages (from datasets==2.16.1->-r requirements.txt (line 2)) (1.5.3)\n", "Collecting multiprocess\n", " Downloading multiprocess-0.70.16-py39-none-any.whl (133 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m133.4/133.4 kB\u001b[0m \u001b[31m246.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting xxhash\n", " Downloading xxhash-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (193 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m193.8/193.8 kB\u001b[0m \u001b[31m282.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: psutil in /opt/app-root/lib/python3.9/site-packages (from accelerate==0.26.1->-r requirements.txt (line 3)) (5.9.8)\n", "Collecting torch>=1.10.0\n", " Downloading torch-2.4.0-cp39-cp39-manylinux1_x86_64.whl (797.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m797.2/797.2 MB\u001b[0m \u001b[31m88.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting responses<0.19\n", " Downloading responses-0.18.0-py3-none-any.whl (38 kB)\n", "Requirement already satisfied: scipy in /opt/app-root/lib/python3.9/site-packages (from bitsandbytes==0.42.0->-r requirements.txt (line 5)) (1.12.0)\n", "Collecting tyro>=0.5.11\n", " Downloading tyro-0.8.6-py3-none-any.whl (103 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m103.8/103.8 kB\u001b[0m \u001b[31m236.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: frozenlist>=1.1.1 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets==2.16.1->-r requirements.txt (line 2)) (1.4.1)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets==2.16.1->-r requirements.txt (line 2)) (4.0.3)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets==2.16.1->-r requirements.txt (line 2)) (1.3.1)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets==2.16.1->-r requirements.txt (line 2)) (6.0.5)\n", "Requirement already satisfied: attrs>=17.3.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets==2.16.1->-r requirements.txt (line 2)) (23.2.0)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets==2.16.1->-r requirements.txt (line 2)) (1.9.4)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.19.3->transformers==4.36.2->-r requirements.txt (line 1)) (4.11.0)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers==4.36.2->-r requirements.txt (line 1)) (3.7)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers==4.36.2->-r requirements.txt (line 1)) (3.3.2)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers==4.36.2->-r requirements.txt (line 1)) (2024.2.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers==4.36.2->-r requirements.txt (line 1)) (1.26.18)\n", "Collecting nvidia-nvtx-cu12==12.1.105\n", " Downloading nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m99.1/99.1 kB\u001b[0m \u001b[31m225.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.10.0->accelerate==0.26.1->-r requirements.txt (line 3)) (3.1.4)\n", "Collecting nvidia-nccl-cu12==2.20.5\n", " Downloading nvidia_nccl_cu12-2.20.5-py3-none-manylinux2014_x86_64.whl (176.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m176.2/176.2 MB\u001b[0m \u001b[31m84.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting triton==3.0.0\n", " Downloading triton-3.0.0-1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (209.4 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m209.4/209.4 MB\u001b[0m \u001b[31m87.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cufft-cu12==11.0.2.54\n", " Downloading nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.6/121.6 MB\u001b[0m \u001b[31m84.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cuda-cupti-cu12==12.1.105\n", " Downloading nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.1/14.1 MB\u001b[0m \u001b[31m87.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=1.10.0->accelerate==0.26.1->-r requirements.txt (line 3)) (3.2.1)\n", "Collecting nvidia-cuda-nvrtc-cu12==12.1.105\n", " Downloading nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m23.7/23.7 MB\u001b[0m \u001b[31m88.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting sympy\n", " Downloading sympy-1.13.2-py3-none-any.whl (6.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.2/6.2 MB\u001b[0m \u001b[31m98.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cusparse-cu12==12.1.0.106\n", " Downloading nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m196.0/196.0 MB\u001b[0m \u001b[31m65.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cuda-runtime-cu12==12.1.105\n", " Downloading nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m823.6/823.6 kB\u001b[0m \u001b[31m121.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting nvidia-curand-cu12==10.3.2.106\n", " Downloading nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.5/56.5 MB\u001b[0m \u001b[31m69.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cudnn-cu12==9.1.0.70\n", " Downloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m75.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cublas-cu12==12.1.3.1\n", " Downloading nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m410.6/410.6 MB\u001b[0m \u001b[31m46.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cusolver-cu12==11.4.5.107\n", " Downloading nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m124.2/124.2 MB\u001b[0m \u001b[31m36.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-nvjitlink-cu12\n", " Downloading nvidia_nvjitlink_cu12-12.6.20-py3-none-manylinux2014_x86_64.whl (19.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.7/19.7 MB\u001b[0m \u001b[31m37.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: rich>=11.1.0 in /opt/app-root/lib/python3.9/site-packages (from tyro>=0.5.11->trl==0.7.10->-r requirements.txt (line 6)) (12.6.0)\n", "Collecting docstring-parser>=0.16\n", " Downloading docstring_parser-0.16-py3-none-any.whl (36 kB)\n", "Collecting shtab>=1.5.6\n", " Downloading shtab-1.7.1-py3-none-any.whl (14 kB)\n", "Collecting eval-type-backport>=0.1.3\n", " Downloading eval_type_backport-0.2.0-py3-none-any.whl (5.9 kB)\n", "Collecting multiprocess\n", " Downloading multiprocess-0.70.15-py39-none-any.whl (133 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m133.3/133.3 kB\u001b[0m \u001b[31m222.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pytz>=2020.1 in /opt/app-root/lib/python3.9/site-packages (from pandas->datasets==2.16.1->-r requirements.txt (line 2)) (2024.1)\n", "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/app-root/lib/python3.9/site-packages (from pandas->datasets==2.16.1->-r requirements.txt (line 2)) (2.9.0.post0)\n", "Requirement already satisfied: six>=1.5 in /opt/app-root/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas->datasets==2.16.1->-r requirements.txt (line 2)) (1.16.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /opt/app-root/lib/python3.9/site-packages (from rich>=11.1.0->tyro>=0.5.11->trl==0.7.10->-r requirements.txt (line 6)) (2.18.0)\n", "Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /opt/app-root/lib/python3.9/site-packages (from rich>=11.1.0->tyro>=0.5.11->trl==0.7.10->-r requirements.txt (line 6)) (0.9.1)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=1.10.0->accelerate==0.26.1->-r requirements.txt (line 3)) (2.1.5)\n", "Collecting mpmath<1.4,>=1.1.0\n", " Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m536.2/536.2 kB\u001b[0m \u001b[31m68.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: mpmath, xxhash, triton, sympy, shtab, safetensors, regex, pyarrow-hotfix, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, fsspec, eval-type-backport, docstring-parser, dill, tyro, responses, nvidia-cusparse-cu12, nvidia-cudnn-cu12, multiprocess, huggingface-hub, bitsandbytes, tokenizers, nvidia-cusolver-cu12, transformers, torch, datasets, evaluate, accelerate, trl, peft\n", " Attempting uninstall: fsspec\n", " Found existing installation: fsspec 2024.5.0\n", " Uninstalling fsspec-2024.5.0:\n", " Successfully uninstalled fsspec-2024.5.0\n", " Attempting uninstall: docstring-parser\n", " Found existing installation: docstring_parser 0.8.1\n", " Uninstalling docstring_parser-0.8.1:\n", " Successfully uninstalled docstring_parser-0.8.1\n", " Attempting uninstall: dill\n", " Found existing installation: dill 0.3.8\n", " Uninstalling dill-0.3.8:\n", " Successfully uninstalled dill-0.3.8\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "codeflare-torchx 0.6.0.dev2 requires docstring-parser==0.8.1, but you have docstring-parser 0.16 which is incompatible.\u001b[0m\u001b[31m\n", "\u001b[0mSuccessfully installed accelerate-0.26.1 bitsandbytes-0.42.0 datasets-2.16.1 dill-0.3.7 docstring-parser-0.16 eval-type-backport-0.2.0 evaluate-0.4.1 fsspec-2023.10.0 huggingface-hub-0.24.5 mpmath-1.3.0 multiprocess-0.70.15 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.20.5 nvidia-nvjitlink-cu12-12.6.20 nvidia-nvtx-cu12-12.1.105 peft-0.7.1 pyarrow-hotfix-0.6 regex-2024.7.24 responses-0.18.0 safetensors-0.4.4 shtab-1.7.1 sympy-1.13.2 tokenizers-0.15.2 torch-2.4.0 transformers-4.36.2 triton-3.0.0 trl-0.7.10 tyro-0.8.6 xxhash-3.4.1\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!pip install -r requirements.txt" ] }, { "cell_type": "code", "execution_count": 8, "id": "dfaf1390-b9ec-4b36-976a-01f6efafa15f", "metadata": { "tags": [] }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'trustyai.detoxify'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[8], line 16\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m---> 16\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrustyai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdetoxify\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TMaRCo\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'trustyai.detoxify'" ] } ], "source": [ "from transformers import (\n", " AutoTokenizer,\n", " AutoModelForCausalLM,\n", " DataCollatorForLanguageModeling,\n", " BitsAndBytesConfig,\n", " Trainer,\n", " TrainingArguments,\n", " set_seed\n", " )\n", "from datasets import load_dataset, load_from_disk\n", "from peft import LoraConfig\n", "from trl import SFTTrainer\n", "from trl.trainer import ConstantLengthDataset\n", "import numpy as np\n", "import torch\n", "from trustyai.detoxify import TMaRCo" ] }, { "cell_type": "code", "execution_count": 4, "id": "a31ac0ed-fe1f-4128-9c86-d5c834658b67", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[31mERROR: Could not find a version that satisfies the requirement trustyai.detoxify (from versions: none)\u001b[0m\u001b[31m\n", "\u001b[0m\u001b[31mERROR: No matching distribution found for trustyai.detoxify\u001b[0m\u001b[31m\n", "\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!pip install trustyai.detoxify" ] }, { "cell_type": "code", "execution_count": 6, "id": "8e892a23-4728-4f91-a939-2fb2eaa02f31", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: trustyai in /opt/app-root/lib/python3.9/site-packages (0.6.0)\n", "Requirement already satisfied: pyarrow==14.0.1 in /opt/app-root/lib/python3.9/site-packages (from trustyai) (14.0.1)\n", "Requirement already satisfied: numpy~=1.24.1 in /opt/app-root/lib/python3.9/site-packages (from trustyai) (1.24.4)\n", "Requirement already satisfied: pandas~=1.5.3 in /opt/app-root/lib/python3.9/site-packages (from trustyai) (1.5.3)\n", "Requirement already satisfied: Jpype1==1.4.1 in /opt/app-root/lib/python3.9/site-packages (from trustyai) (1.4.1)\n", "Requirement already satisfied: matplotlib~=3.6.3 in /opt/app-root/lib/python3.9/site-packages (from trustyai) (3.6.3)\n", "Requirement already satisfied: jupyter-bokeh~=3.0.5 in /opt/app-root/lib/python3.9/site-packages (from trustyai) (3.0.7)\n", "Requirement already satisfied: packaging in /opt/app-root/lib/python3.9/site-packages (from Jpype1==1.4.1->trustyai) (24.0)\n", "Requirement already satisfied: ipywidgets==8.* in /opt/app-root/lib/python3.9/site-packages (from jupyter-bokeh~=3.0.5->trustyai) (8.1.2)\n", "Requirement already satisfied: bokeh==3.* in /opt/app-root/lib/python3.9/site-packages (from jupyter-bokeh~=3.0.5->trustyai) (3.4.1)\n", "Requirement already satisfied: pillow>=7.1.0 in /opt/app-root/lib/python3.9/site-packages (from bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (10.3.0)\n", "Requirement already satisfied: PyYAML>=3.10 in /opt/app-root/lib/python3.9/site-packages (from bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (6.0.1)\n", "Requirement already satisfied: tornado>=6.2 in /opt/app-root/lib/python3.9/site-packages (from bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (6.4)\n", "Requirement already satisfied: Jinja2>=2.9 in /opt/app-root/lib/python3.9/site-packages (from bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (3.1.4)\n", "Requirement already satisfied: contourpy>=1.2 in /opt/app-root/lib/python3.9/site-packages (from bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (1.2.1)\n", "Requirement already satisfied: xyzservices>=2021.09.1 in /opt/app-root/lib/python3.9/site-packages (from bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (2024.4.0)\n", "Requirement already satisfied: jupyterlab-widgets~=3.0.10 in /opt/app-root/lib/python3.9/site-packages (from ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (3.0.10)\n", "Requirement already satisfied: comm>=0.1.3 in /opt/app-root/lib/python3.9/site-packages (from ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.2.2)\n", "Requirement already satisfied: traitlets>=4.3.1 in /opt/app-root/lib/python3.9/site-packages (from ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (5.14.3)\n", "Requirement already satisfied: widgetsnbextension~=4.0.10 in /opt/app-root/lib/python3.9/site-packages (from ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (4.0.10)\n", "Requirement already satisfied: ipython>=6.1.0 in /opt/app-root/lib/python3.9/site-packages (from ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (8.18.1)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /opt/app-root/lib/python3.9/site-packages (from matplotlib~=3.6.3->trustyai) (1.4.5)\n", "Requirement already satisfied: fonttools>=4.22.0 in /opt/app-root/lib/python3.9/site-packages (from matplotlib~=3.6.3->trustyai) (4.51.0)\n", "Requirement already satisfied: python-dateutil>=2.7 in /opt/app-root/lib/python3.9/site-packages (from matplotlib~=3.6.3->trustyai) (2.9.0.post0)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /opt/app-root/lib/python3.9/site-packages (from matplotlib~=3.6.3->trustyai) (3.1.2)\n", "Requirement already satisfied: cycler>=0.10 in /opt/app-root/lib/python3.9/site-packages (from matplotlib~=3.6.3->trustyai) (0.12.1)\n", "Requirement already satisfied: pytz>=2020.1 in /opt/app-root/lib/python3.9/site-packages (from pandas~=1.5.3->trustyai) (2024.1)\n", "Requirement already satisfied: six>=1.5 in /opt/app-root/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib~=3.6.3->trustyai) (1.16.0)\n", "Requirement already satisfied: pygments>=2.4.0 in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (2.18.0)\n", "Requirement already satisfied: typing-extensions in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (4.11.0)\n", "Requirement already satisfied: decorator in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (5.1.1)\n", "Requirement already satisfied: matplotlib-inline in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.1.7)\n", "Requirement already satisfied: jedi>=0.16 in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.19.1)\n", "Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (3.0.43)\n", "Requirement already satisfied: stack-data in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.6.3)\n", "Requirement already satisfied: exceptiongroup in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (1.2.1)\n", "Requirement already satisfied: pexpect>4.3 in /opt/app-root/lib/python3.9/site-packages (from ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (4.9.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from Jinja2>=2.9->bokeh==3.*->jupyter-bokeh~=3.0.5->trustyai) (2.1.5)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.3 in /opt/app-root/lib/python3.9/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.8.4)\n", "Requirement already satisfied: ptyprocess>=0.5 in /opt/app-root/lib/python3.9/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.7.0)\n", "Requirement already satisfied: wcwidth in /opt/app-root/lib/python3.9/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.2.13)\n", "Requirement already satisfied: pure-eval in /opt/app-root/lib/python3.9/site-packages (from stack-data->ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (0.2.2)\n", "Requirement already satisfied: asttokens>=2.1.0 in /opt/app-root/lib/python3.9/site-packages (from stack-data->ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (2.4.1)\n", "Requirement already satisfied: executing>=1.2.0 in /opt/app-root/lib/python3.9/site-packages (from stack-data->ipython>=6.1.0->ipywidgets==8.*->jupyter-bokeh~=3.0.5->trustyai) (1.2.0)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install trustyai\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "3a11251f-4637-4ecf-9d29-3db29f266795", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting detoxify\n", " Downloading detoxify-0.5.2-py3-none-any.whl (12 kB)\n", "Collecting sentencepiece>=0.1.94\n", " Downloading sentencepiece-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m4.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: transformers in /opt/app-root/lib/python3.9/site-packages (from detoxify) (4.36.2)\n", "Requirement already satisfied: torch>=1.7.0 in /opt/app-root/lib/python3.9/site-packages (from detoxify) (2.4.0)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.3.1)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.2.1)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (11.4.5.107)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (9.1.0.70)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.14.0)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (2.20.5)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (11.0.2.54)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (1.13.2)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.0.106)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (10.3.2.106)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.0.0)\n", "Requirement already satisfied: fsspec in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (2023.10.0)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.1.4)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (4.11.0)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.7.0->detoxify) (12.6.20)\n", "Requirement already satisfied: safetensors>=0.3.1 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.4.4)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (6.0.1)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.24.5)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (1.24.4)\n", "Requirement already satisfied: requests in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (2.32.2)\n", "Requirement already satisfied: tqdm>=4.27 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (4.66.4)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (24.0)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (2024.7.24)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.15.2)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=1.7.0->detoxify) (2.1.5)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (3.3.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (1.26.18)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (2024.2.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (3.7)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch>=1.7.0->detoxify) (1.3.0)\n", "Installing collected packages: sentencepiece, detoxify\n", "Successfully installed detoxify-0.5.2 sentencepiece-0.2.0\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install detoxify\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "00c33828-12a4-4983-9a83-d0876d5cf890", "metadata": { "tags": [] }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'trustyai.detoxify'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[9], line 16\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m---> 16\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrustyai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdetoxify\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TMaRCo\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'trustyai.detoxify'" ] } ], "source": [ "from transformers import (\n", " AutoTokenizer,\n", " AutoModelForCausalLM,\n", " DataCollatorForLanguageModeling,\n", " BitsAndBytesConfig,\n", " Trainer,\n", " TrainingArguments,\n", " set_seed\n", " )\n", "from datasets import load_dataset, load_from_disk\n", "from peft import LoraConfig\n", "from trl import SFTTrainer\n", "from trl.trainer import ConstantLengthDataset\n", "import numpy as np\n", "import torch\n", "from trustyai.detoxify import TMaRCo" ] }, { "cell_type": "code", "execution_count": 10, "id": "ec775cea-5977-410c-ab48-3c2daca20f2c", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "130c867226c047c29c24e468a4c03305", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading readme: 0%| | 0.00/4.22k [00:00 2\u001b[0m tmarco \u001b[38;5;241m=\u001b[39m \u001b[43mTMaRCo\u001b[49m()\n\u001b[1;32m 3\u001b[0m tmarco\u001b[38;5;241m.\u001b[39mload_models([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrustyai/gminus\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrustyai/gplus\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n", "\u001b[0;31mNameError\u001b[0m: name 'TMaRCo' is not defined" ] } ], "source": [ "# load TMaRCo expert and non-expert models\n", "tmarco = TMaRCo()\n", "tmarco.load_models([\"trustyai/gminus\", \"trustyai/gplus\"])" ] }, { "cell_type": "code", "execution_count": 12, "id": "081cbdd4-b8a6-4cf0-b1b8-2d542d123b34", "metadata": { "tags": [] }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'trustyai.detoxify'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 16\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[0;32m---> 16\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrustyai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdetoxify\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TMaRCo\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'trustyai.detoxify'" ] } ], "source": [ "from transformers import (\n", " AutoTokenizer,\n", " AutoModelForCausalLM,\n", " DataCollatorForLanguageModeling,\n", " BitsAndBytesConfig,\n", " Trainer,\n", " TrainingArguments,\n", " set_seed\n", " )\n", "from datasets import load_dataset, load_from_disk\n", "from peft import LoraConfig\n", "from trl import SFTTrainer\n", "from trl.trainer import ConstantLengthDataset\n", "import numpy as np\n", "import torch\n", "from trustyai.detoxify import TMaRCo" ] }, { "cell_type": "code", "execution_count": 13, "id": "8c69b13b-7151-4e30-bd27-1dfb26c952b3", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: detoxify in /opt/app-root/lib/python3.9/site-packages (0.5.2)\n", "Requirement already satisfied: sentencepiece>=0.1.94 in /opt/app-root/lib/python3.9/site-packages (from detoxify) (0.2.0)\n", "Requirement already satisfied: transformers in /opt/app-root/lib/python3.9/site-packages (from detoxify) (4.36.2)\n", "Requirement already satisfied: torch>=1.7.0 in /opt/app-root/lib/python3.9/site-packages (from detoxify) (2.4.0)\n", "Requirement already satisfied: fsspec in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (2023.10.0)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (2.20.5)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.0.0)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (1.13.2)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.2.1)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (11.0.2.54)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.14.0)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (11.4.5.107)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (4.11.0)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.1.4)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.0.106)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (9.1.0.70)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.7.0->detoxify) (12.6.20)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (1.24.4)\n", "Requirement already satisfied: requests in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (2.32.2)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (24.0)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (2024.7.24)\n", "Requirement already satisfied: safetensors>=0.3.1 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.4.4)\n", "Requirement already satisfied: tqdm>=4.27 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (4.66.4)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.15.2)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.24.5)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (6.0.1)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=1.7.0->detoxify) (2.1.5)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (3.3.2)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (2024.2.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (1.26.18)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (3.7)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch>=1.7.0->detoxify) (1.3.0)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install detoxify\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "2597373d-496a-4681-9edf-0a62da82e6b5", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting trustai\n", " Downloading trustai-0.1.12-py3-none-any.whl (89 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m89.9/89.9 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy in /opt/app-root/lib/python3.9/site-packages (from trustai) (1.24.4)\n", "Requirement already satisfied: scikit-learn in /opt/app-root/lib/python3.9/site-packages (from trustai) (1.4.2)\n", "Requirement already satisfied: matplotlib in /opt/app-root/lib/python3.9/site-packages (from trustai) (3.6.3)\n", "Requirement already satisfied: IPython in /opt/app-root/lib/python3.9/site-packages (from trustai) (8.18.1)\n", "Requirement already satisfied: tqdm in /opt/app-root/lib/python3.9/site-packages (from trustai) (4.66.4)\n", "Requirement already satisfied: pygments>=2.4.0 in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (2.18.0)\n", "Requirement already satisfied: traitlets>=5 in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (5.14.3)\n", "Requirement already satisfied: pexpect>4.3 in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (4.9.0)\n", "Requirement already satisfied: matplotlib-inline in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (0.1.7)\n", "Requirement already satisfied: jedi>=0.16 in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (0.19.1)\n", "Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (3.0.43)\n", "Requirement already satisfied: typing-extensions in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (4.11.0)\n", "Requirement already satisfied: exceptiongroup in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (1.2.1)\n", "Requirement already satisfied: stack-data in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (0.6.3)\n", "Requirement already satisfied: decorator in /opt/app-root/lib/python3.9/site-packages (from IPython->trustai) (5.1.1)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (3.1.2)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (1.4.5)\n", "Requirement already satisfied: python-dateutil>=2.7 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (2.9.0.post0)\n", "Requirement already satisfied: contourpy>=1.0.1 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (1.2.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (4.51.0)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (24.0)\n", "Requirement already satisfied: pillow>=6.2.0 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (10.3.0)\n", "Requirement already satisfied: cycler>=0.10 in /opt/app-root/lib/python3.9/site-packages (from matplotlib->trustai) (0.12.1)\n", "Requirement already satisfied: joblib>=1.2.0 in /opt/app-root/lib/python3.9/site-packages (from scikit-learn->trustai) (1.4.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/app-root/lib/python3.9/site-packages (from scikit-learn->trustai) (3.5.0)\n", "Requirement already satisfied: scipy>=1.6.0 in /opt/app-root/lib/python3.9/site-packages (from scikit-learn->trustai) (1.12.0)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.3 in /opt/app-root/lib/python3.9/site-packages (from jedi>=0.16->IPython->trustai) (0.8.4)\n", "Requirement already satisfied: ptyprocess>=0.5 in /opt/app-root/lib/python3.9/site-packages (from pexpect>4.3->IPython->trustai) (0.7.0)\n", "Requirement already satisfied: wcwidth in /opt/app-root/lib/python3.9/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->IPython->trustai) (0.2.13)\n", "Requirement already satisfied: six>=1.5 in /opt/app-root/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib->trustai) (1.16.0)\n", "Requirement already satisfied: asttokens>=2.1.0 in /opt/app-root/lib/python3.9/site-packages (from stack-data->IPython->trustai) (2.4.1)\n", "Requirement already satisfied: executing>=1.2.0 in /opt/app-root/lib/python3.9/site-packages (from stack-data->IPython->trustai) (1.2.0)\n", "Requirement already satisfied: pure-eval in /opt/app-root/lib/python3.9/site-packages (from stack-data->IPython->trustai) (0.2.2)\n", "Installing collected packages: trustai\n", "Successfully installed trustai-0.1.12\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install trustai" ] }, { "cell_type": "code", "execution_count": 15, "id": "1e881be6-2868-41a4-91d0-9c4152671ce0", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on package trustyai:\n", "\n", "NAME\n", " trustyai - Main TrustyAI Python bindings\n", "\n", "PACKAGE CONTENTS\n", " _default_initializer\n", " explainers (package)\n", " initializer\n", " language (package)\n", " local (package)\n", " metrics (package)\n", " model (package)\n", " utils (package)\n", " version\n", " visualizations (package)\n", "\n", "FUNCTIONS\n", " init()\n", " Deprecated manual initializer for the JVM. This function has been replaced by\n", " automatic initialization when importing the components of the module that require\n", " JVM access, or by manual user initialization via :func:`trustyai`initializer.init`.\n", "\n", "DATA\n", " TRUSTYAI_IS_INITIALIZED = False\n", "\n", "VERSION\n", " 0.6.0\n", "\n", "FILE\n", " /opt/app-root/lib64/python3.9/site-packages/trustyai/__init__.py\n", "\n", "\n" ] } ], "source": [ "import trustyai\n", "help(trustyai)\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "7e1a95c3-ec09-4cc8-9f44-da66f34bb16f", "metadata": { "tags": [] }, "outputs": [], "source": [ "from trustyai.language import detoxify\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "4fb8eb2a-0540-40d9-b981-94b3e01d7d98", "metadata": { "tags": [] }, "outputs": [ { "ename": "ImportError", "evalue": "cannot import name 'detoxify' from 'trustyai.explainers' (/opt/app-root/lib64/python3.9/site-packages/trustyai/explainers/__init__.py)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtrustyai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexplainers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m detoxify\n", "\u001b[0;31mImportError\u001b[0m: cannot import name 'detoxify' from 'trustyai.explainers' (/opt/app-root/lib64/python3.9/site-packages/trustyai/explainers/__init__.py)" ] } ], "source": [ "from trustyai.explainers import detoxify\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "3aaf3742-22dd-4580-b89b-ec17d30d3d2a", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: detoxify in /opt/app-root/lib/python3.9/site-packages (0.5.2)\n", "Requirement already satisfied: sentencepiece>=0.1.94 in /opt/app-root/lib/python3.9/site-packages (from detoxify) (0.2.0)\n", "Requirement already satisfied: torch>=1.7.0 in /opt/app-root/lib/python3.9/site-packages (from detoxify) (2.4.0)\n", "Requirement already satisfied: transformers in /opt/app-root/lib/python3.9/site-packages (from detoxify) (4.36.2)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.0.106)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (9.1.0.70)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.3.1)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (4.11.0)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (11.4.5.107)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (2.20.5)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.14.0)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.2.1)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (1.13.2)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (11.0.2.54)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (10.3.2.106)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.0.0)\n", "Requirement already satisfied: fsspec in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (2023.10.0)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (3.1.4)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.7.0->detoxify) (12.1.105)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.7.0->detoxify) (12.6.20)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.24.5)\n", "Requirement already satisfied: safetensors>=0.3.1 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.4.4)\n", "Requirement already satisfied: tqdm>=4.27 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (4.66.4)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (6.0.1)\n", "Requirement already satisfied: requests in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (2.32.2)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (1.24.4)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (24.0)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (0.15.2)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->detoxify) (2024.7.24)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=1.7.0->detoxify) (2.1.5)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (2024.2.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (1.26.18)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (3.7)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers->detoxify) (3.3.2)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch>=1.7.0->detoxify) (1.3.0)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install detoxify\n" ] }, { "cell_type": "code", "execution_count": 19, "id": "ba40bcb3-1fe7-442e-985c-80a53342c137", "metadata": { "tags": [] }, "outputs": [], "source": [ "from detoxify import Detoxify\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "38781751-9c16-46f3-be5a-77520c025d24", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading: \"https://github.com/unitaryai/detoxify/releases/download/v0.1-alpha/toxic_original-c1212f89.ckpt\" to /opt/app-root/src/.cache/torch/hub/checkpoints/toxic_original-c1212f89.ckpt\n", "100%|██████████| 418M/418M [00:10<00:00, 40.5MB/s] \n", "/opt/app-root/lib64/python3.9/site-packages/transformers/utils/generic.py:309: FutureWarning: `torch.utils._pytree._register_pytree_node` is deprecated. Please use `torch.utils._pytree.register_pytree_node` instead.\n", " _torch_pytree._register_pytree_node(\n", "/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "86e6a3ce7f4740019c944ffffd0cbf5e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "config.json: 0%| | 0.00/570 [00:00=1.17 in /opt/app-root/lib/python3.9/site-packages (from transformers) (1.24.4)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers) (2024.7.24)\n", "Requirement already satisfied: safetensors>=0.3.1 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.4.4)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from transformers) (3.14.0)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.15.2)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.24.5)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from transformers) (24.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from transformers) (6.0.1)\n", "Requirement already satisfied: tqdm>=4.27 in /opt/app-root/lib/python3.9/site-packages (from transformers) (4.66.4)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /opt/app-root/lib/python3.9/site-packages (from torch) (4.11.0)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch) (11.4.5.107)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch) (3.0.0)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch) (2.20.5)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch) (9.1.0.70)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.0.106)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch) (3.1.4)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch) (3.2.1)\n", "Requirement already satisfied: fsspec in /opt/app-root/lib/python3.9/site-packages (from torch) (2023.10.0)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch) (1.13.2)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch) (11.0.2.54)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch) (12.6.20)\n", "Requirement already satisfied: sentencepiece>=0.1.94 in /opt/app-root/lib/python3.9/site-packages (from detoxify) (0.2.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch) (2.1.5)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (3.7)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (1.26.18)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (3.3.2)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (2024.2.2)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch) (1.3.0)\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install transformers torch detoxify\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "273d649d-2610-4072-b3ce-662ec6a83451", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pip in /opt/app-root/lib/python3.9/site-packages (22.2.2)\n", "Collecting pip\n", " Downloading pip-24.2-py3-none-any.whl (1.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.8/1.8 MB\u001b[0m \u001b[31m9.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hInstalling collected packages: pip\n", " Attempting uninstall: pip\n", " Found existing installation: pip 22.2.2\n", " Uninstalling pip-22.2.2:\n", " Successfully uninstalled pip-22.2.2\n", "Successfully installed pip-24.2\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install --upgrade pip" ] }, { "cell_type": "code", "execution_count": 23, "id": "fa48aa69-71a0-4890-9f2d-d11810d1b973", "metadata": { "tags": [] }, "outputs": [], "source": [ "from detoxify import Detoxify\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "915a0a6b-5d2e-41cc-acee-6d78aa6548be", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'toxicity': 0.0007457582, 'severe_toxicity': 0.00011119133, 'obscene': 0.00017416461, 'threat': 0.000114033835, 'insult': 0.00017978664, 'identity_attack': 0.00013735857}\n" ] } ], "source": [ "model = Detoxify('original')\n", "results = model.predict(\"Your text here to analyze for toxicity.\")\n", "print(results)\n" ] }, { "cell_type": "code", "execution_count": 25, "id": "37690b1a-dadb-48ac-8c01-4fbab9337b1f", "metadata": { "tags": [] }, "outputs": [], "source": [ "from detoxify import Detoxify\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "001cf409-66d3-46a6-8212-142a0d3fb8f0", "metadata": { "tags": [] }, "outputs": [], "source": [ "model = Detoxify('original')\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "e67c1be0-1f87-4770-ae14-51cbdf225526", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'toxicity': 0.0006598092, 'severe_toxicity': 0.0001192591, 'obscene': 0.00017243883, 'threat': 0.0001247127, 'insult': 0.00018202125, 'identity_attack': 0.0001411103}\n" ] } ], "source": [ "results = model.predict(\"Your text to analyze for toxicity.\")\n", "print(results)\n", "\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "0cdffd0d-2e38-4422-b092-949c435faa15", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['filename', 'begin', 'end', 'challenging', 'prompt.text', 'prompt.profanity', 'prompt.sexually_explicit', 'prompt.identity_attack', 'prompt.flirtation', 'prompt.threat', 'prompt.insult', 'prompt.severe_toxicity', 'prompt.toxicity', 'continuation.text', 'continuation.severe_toxicity', 'continuation.toxicity', 'continuation.profanity', 'continuation.sexually_explicit', 'continuation.identity_attack', 'continuation.flirtation', 'continuation.threat', 'continuation.insult']\n" ] } ], "source": [ "from datasets import load_dataset\n", "\n", "dataset_name = \"allenai/real-toxicity-prompts\"\n", "raw_dataset = load_dataset(dataset_name, split=\"train\").flatten()\n", "print(raw_dataset.column_names)\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "37d26d71-e393-4fec-bd44-8a2865b6ca43", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading: \"https://github.com/unitaryai/detoxify/releases/download/v0.3-alpha/toxic_debiased-c7548aa0.ckpt\" to /opt/app-root/src/.cache/torch/hub/checkpoints/toxic_debiased-c7548aa0.ckpt\n", "100%|██████████| 476M/476M [00:09<00:00, 51.1MB/s] \n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d4ad2a7870234bc8a4cb5bea3e720b1e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "config.json: 0%| | 0.00/481 [00:00 0.6: # Use your desired threshold\n", " # For simplicity, you might choose to replace the entire text or sensitive parts\n", " sample['text'] = \"This content has been flagged for toxicity and has been rephrased for safety.\"\n", " \n", " return sample\n" ] }, { "cell_type": "code", "execution_count": 32, "id": "4f3e2b99-0e06-429f-a3eb-f64adb35ea83", "metadata": { "tags": [] }, "outputs": [], "source": [ "block_size = 128\n", "\n", "def group_texts(examples):\n", " # Concatenate all texts.\n", " concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()}\n", " total_length = len(concatenated_examples[list(examples.keys())[0]])\n", " \n", " # Drop the small remainder, or add padding if the model supported it.\n", " if total_length >= block_size:\n", " total_length = (total_length // block_size) * block_size\n", " \n", " # Split by chunks of block_size.\n", " result = {\n", " k: [t[i : i + block_size] for i in range(0, total_length, block_size)]\n", " for k, t in concatenated_examples.items()\n", " }\n", " \n", " # Set the labels to be the same as the input_ids.\n", " result[\"labels\"] = result[\"input_ids\"].copy()\n", " \n", " return result\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "3b88d48c-ca4b-472a-bd24-8f509398f5d5", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Split the dataset into training and testing sets with an 80/20 split\n", "dataset = raw_dataset.train_test_split(test_size=0.2, shuffle=True, seed=42)\n", "\n", "# Randomly select 1000 samples from the training data\n", "train_data = dataset[\"train\"].select(indices=range(0, 1000))\n", "\n", "# Randomly select 400 samples from the evaluation data\n", "eval_data = dataset[\"test\"].select(indices=range(0, 400))\n" ] }, { "cell_type": "code", "execution_count": 34, "id": "cf46f64b-f63a-4b08-ae18-af87c568fdc9", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6f3a40a74067470d90f102dd6a84e5b1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "tokenizer_config.json: 0%| | 0.00/685 [00:00= block_size:\n", " total_length = (total_length // block_size) * block_size\n", " result = {\n", " k: [t[i: i + block_size] for i in range(0, total_length, block_size)]\n", " for k, t in concatenated_examples.items()\n", " }\n", " result[\"labels\"] = result[\"input_ids\"].copy()\n", " return result\n", "\n", "dataset = raw_dataset.train_test_split(test_size=0.2, shuffle=True, seed=42)\n", "train_data = dataset[\"train\"].select(indices=range(0, 1000))\n", "eval_data = dataset[\"test\"].select(indices=range(0, 400))\n", "\n", "model_id = \"facebook/opt-350m\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "tokenizer.pad_token = tokenizer.eos_token\n", "tokenizer.padding_side = \"right\"\n", "\n", "train_ds = train_data.map(preprocess_func, remove_columns=train_data.column_names)\n", "eval_ds = eval_data.map(preprocess_func, remove_columns=eval_data.column_names)\n", "\n", "mean_length = np.mean([len(text) for text in train_ds['text']])\n", "train_ds = train_ds.filter(lambda x: len(x['text\n" ] }, { "cell_type": "code", "execution_count": 38, "id": "d0a5a9d7-db99-4010-886d-5e23994a03a6", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f1b496dc39324fcfa7ef0d7942ad0bea", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Filter: 0%| | 0/1000 [00:00 8\u001b[0m tokenized_train_ds \u001b[38;5;241m=\u001b[39m train_ds\u001b[38;5;241m.\u001b[39mmap(\u001b[43mtokenize_func\u001b[49m, batched\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, remove_columns\u001b[38;5;241m=\u001b[39mtrain_ds\u001b[38;5;241m.\u001b[39mcolumn_names)\n\u001b[1;32m 9\u001b[0m tokenized_eval_ds \u001b[38;5;241m=\u001b[39m eval_ds\u001b[38;5;241m.\u001b[39mmap(tokenize_func, batched\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, remove_columns\u001b[38;5;241m=\u001b[39meval_ds\u001b[38;5;241m.\u001b[39mcolumn_names)\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Print the sizes of the tokenized datasets\u001b[39;00m\n", "\u001b[0;31mNameError\u001b[0m: name 'tokenize_func' is not defined" ] } ], "source": [ "# Select samples whose length is less than or equal to the mean length of the training set\n", "mean_length = np.mean([len(text) for text in train_ds['text']])\n", "\n", "# Filter the training dataset based on the mean length\n", "train_ds = train_ds.filter(lambda x: len(x['text']) <= mean_length)\n", "\n", "# Tokenize the filtered training and evaluation datasets\n", "tokenized_train_ds = train_ds.map(tokenize_func, batched=True, remove_columns=train_ds.column_names)\n", "tokenized_eval_ds = eval_ds.map(tokenize_func, batched=True, remove_columns=eval_ds.column_names)\n", "\n", "# Print the sizes of the tokenized datasets\n", "print(f\"Size of training set: {len(tokenized_train_ds)}\\nSize of evaluation set: {len(tokenized_eval_ds)}\")\n", "\n", "# Apply the rephrasing function to the training dataset\n", "rephrased_train_ds = train_ds.map(rephrase_func)\n" ] }, { "cell_type": "code", "execution_count": 39, "id": "bd115649-543d-4fbe-9039-f0023c32a57e", "metadata": { "tags": [] }, "outputs": [], "source": [ "def tokenize_func(examples):\n", " return tokenizer(examples['text'], padding='max_length', truncation=True, max_length=128)\n" ] }, { "cell_type": "code", "execution_count": 40, "id": "c267d0e7-7ad8-4fda-835a-ee0b797791fb", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "4dc5df94dcfd4555ab36f8d17d063990", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Filter: 0%| | 0/557 [00:00 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpeft\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m LoraConfig, SFTTrainer\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtransformers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m TrainingArguments, AutoTokenizer\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# Define PEFT configuration for LoRA\u001b[39;00m\n", "\u001b[0;31mImportError\u001b[0m: cannot import name 'SFTTrainer' from 'peft' (/opt/app-root/lib64/python3.9/site-packages/peft/__init__.py)" ] } ], "source": [ "from peft import LoraConfig, SFTTrainer\n", "from transformers import TrainingArguments, AutoTokenizer\n", "\n", "# Define PEFT configuration for LoRA\n", "peft_config = LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"],\n", ")\n", "\n", "# Training arguments\n", "training_args = TrainingArguments(\n", " output_dir=\"../models/opt-350m_CASUAL_LM\",\n", " evaluation_strategy=\"epoch\",\n", " per_device_train_batch_size=1,\n", " per_device_eval_batch_size=1,\n", " num_train_epochs=5,\n", " learning_rate=1e-04,\n", " max_grad_norm=0.3,\n", " warmup_ratio=0.03,\n", " lr_scheduler_type=\"cosine\"\n", ")\n", "\n", "# Define the model and tokenizer\n", "model_id = \"facebook/opt-350m\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "tokenizer.pad_token = tokenizer.eos_token\n", "\n", "# Prepare the trainer with model initialization arguments\n", "trainer = SFTTrainer(\n", " model=model_id, # Pass model ID directly\n", " tokenizer=tokenizer,\n", " args=training_args,\n", " train_dataset=rephrased_train_ds, # Rephrased training dataset\n", " eval_dataset=tokenized_eval_ds, # Evaluation dataset\n", " dataset_text_field=\"text\",\n", " peft_config=peft_config,\n", " max_seq_length=min(tokenizer.model_max_length, 512)\n", ")\n", "\n", "# Start the training process\n", "trainer.train()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "34e14448-7ba2-4a0d-a662-e41bf92b123b", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "80ed60292a0d480bbcdca6ba424878c2", "version_major": 2, "version_minor": 0 }, "text/plain": [ "pytorch_model.bin: 0%| | 0.00/663M [00:00\n", " \n", " \n", " [ 422/1400 1:30:27 < 3:30:39, 0.08 it/s, Epoch 1.50/5]\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EpochTraining LossValidation Loss
1No log0.952133

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from peft import LoraConfig, get_peft_model\n", "from transformers import TrainingArguments, AutoTokenizer, Trainer, AutoModelForCausalLM\n", "\n", "# Define PEFT configuration for LoRA\n", "peft_config = LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"],\n", ")\n", "\n", "# Define the model and tokenizer\n", "model_id = \"facebook/opt-350m\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "tokenizer.pad_token = tokenizer.eos_token\n", "\n", "# Apply the PEFT configuration to the model\n", "model = get_peft_model(model, peft_config)\n", "\n", "# Training arguments\n", "training_args = TrainingArguments(\n", " output_dir=\"../models/opt-350m_CASUAL_LM\",\n", " evaluation_strategy=\"epoch\",\n", " per_device_train_batch_size=1,\n", " per_device_eval_batch_size=1,\n", " num_train_epochs=5,\n", " learning_rate=1e-04,\n", " max_grad_norm=0.3,\n", " warmup_ratio=0.03,\n", " lr_scheduler_type=\"cosine\"\n", ")\n", "\n", "# Create the Trainer instance\n", "trainer = Trainer(\n", " model=model,\n", " args=training_args,\n", " train_dataset=tokenized_train_ds,\n", " eval_dataset=tokenized_eval_ds,\n", " tokenizer=tokenizer,\n", ")\n", "\n", "# Start the training process\n", "trainer.train()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6927c5cd-f7e1-4ec5-aaab-9137f975f80b", "metadata": { "tags": [] }, "outputs": [], "source": [ "trainer.train()" ] }, { "cell_type": "code", "execution_count": null, "id": "f28f2865-7c90-4743-a377-abce7b275b8a", "metadata": { "tags": [] }, "outputs": [], "source": [ "from datasets import load_dataset\n", "\n", "# Load the dataset and filter for toxic prompts\n", "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "dataset = dataset.filter(lambda x: x[\"label\"] == 1).shuffle(seed=42).select(indices=range(0, 400))\n", "\n", "# Print column names to verify dataset structure\n", "print(dataset.column_names)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0fe0c5f1-f679-4de6-b90c-cc8de5f6086f", "metadata": { "tags": [] }, "outputs": [], "source": [ "from transformers import AutoModelForCausalLM\n", "from peft import PeftModel\n", "\n", "# Model IDs\n", "model_id = \"exyou/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"exyou/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the base model (pre-trained)\n", "model = AutoModelForCausalLM.from_pretrained(model_id, device_map=\"auto\", torch_dtype=torch.bfloat16)\n", "\n", "# Load the PEFT model (detoxified version)\n", "peft_model = PeftModel.from_pretrained(peft_model_id, device_map=\"auto\", torch_dtype=torch.bfloat16)\n", "\n", "# Models to test\n", "models_to_test = {model_id: model, peft_model_id: peft_model}\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c4659b1b-7de7-458b-9255-c535c331f569", "metadata": { "tags": [] }, "outputs": [], "source": [ "from transformers import AutoTokenizer\n", "import torch\n", "\n", "# Set the context length\n", "context_length = 2000\n", "output_texts = {}\n", "\n", "# Load the tokenizer and configure padding\n", "tokenizer = AutoTokenizer.from_pretrained(\"facebook/opt-350m\")\n", "tokenizer.pad_token = tokenizer.eos_token\n", "tokenizer.padding_side = \"left\"\n", "\n", "# Iterate over the models\n", "for model_name in models_to_test.keys():\n", " model = models_to_test[model_name]\n", " model.to(device) # Move model to device if not already done\n", " output_texts[model_name] = []\n", "\n", " for i, example in enumerate(dataset):\n", " torch.manual_seed(42)\n", " input_text = example[\"comment_text\"][:context_length]\n", "\n", " # Tokenize input\n", " inputs = tokenizer(\n", " f\"Prompt: {input_text}\\nContinuation:\",\n", " padding=True,\n", " return_tensors=\"pt\",\n", " ).to(device)\n", "\n", " # Trim the inputs to context_length\n", " inputs.input_ids = inputs.input_ids[:, :context_length]\n", " inputs.attention_mask = inputs.attention_mask[:, :context_length]\n", "\n", " # Generate text\n", " generated_texts = model.generate(\n", " **inputs,\n", " max_new_tokens=30,\n", " do_sample=True,\n", " temperature=0.7,\n", " top_k=50,\n", " top_p=0.95,\n", " repetition_penalty=1.2\n", " )\n", "\n", " # Decode generated texts\n", " generated_texts = tokenizer.batch_decode(\n", " generated_texts.detach().cpu().numpy(),\n", " skip_special_tokens=True\n", " )\n", "\n", " output_texts[model_name].append(generated_texts[0][len(input_text):])\n", "\n", " # Free up memory\n", " del model\n", " torch.cuda.empty_cache()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "da1f389d-1076-43e9-ab22-d37b5a4ce3eb", "metadata": { "tags": [] }, "outputs": [], "source": [ "import evaluate\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Example: Evaluate the generated texts for toxicity\n", "for model_name, texts in output_texts.items():\n", " results = toxicity.compute(predictions=texts)\n", " print(f\"Toxicity results for {model_name}: {results}\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "01e278e5-ac2f-4b25-a973-447bac6a04ed", "metadata": { "tags": [] }, "outputs": [], "source": [ "toxicities = {}\n", "for model_name in list(models_to_test.keys()):\n", " toxicities[model_name] = []\n", " for generated_text in output_texts[model_name]:\n", " score = toxicity.compute(predictions=[generated_text])\n", " toxicities[model_name].append(score['toxicity'])\n", " print(\"##\" * 5 + f\" Model {model_name} \" + \"##\" * 5)\n", " print(f\"Mean toxicity: {np.mean(toxicities[model_name])}\")\n", " print(f\"Std: {np.std(toxicities[model_name])}\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9b14825a-5736-4cf0-8706-f5daccde9a37", "metadata": { "tags": [] }, "outputs": [], "source": [ "import evaluate\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Example: Evaluate the generated texts for toxicity\n", "for model_name, texts in output_texts.items():\n", " print(f\"Evaluating model: {model_name}\")\n", " results = toxicity.compute(predictions=texts)\n", " print(f\"Toxicity results for {model_name}: {results}\")\n", "\n", "toxicities = {}\n", "for model_name in list(models_to_test.keys()):\n", " toxicities[model_name] = []\n", " print(f\"Processing toxicity for model: {model_name}\")\n", " for generated_text in output_texts[model_name]:\n", " score = toxicity.compute(predictions=[generated_text])\n", " toxicities[model_name].append(score['toxicity'][0])\n", " print(\"##\" * 5 + f\"Model {model_name}\" + \"##\" * 5)\n", " print(f\"Mean toxicity: {np.mean(toxicities[model_name])}\")\n", " print(f\"Std: {np.std(toxicities[model_name])}\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f5f16ae6-53ef-4228-9a3e-23297a5c393e", "metadata": { "tags": [] }, "outputs": [], "source": [ "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "dataset = dataset.filter(lambda x: x[\"label\"] == 1).shuffle(seed=42).select(indices=range(0, 400))\n", "print(dataset.column_names)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b681a77d-b4fe-4b02-b493-1ece379bb891", "metadata": {}, "outputs": [], "source": [ "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "print(dataset)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1eb5f960-7206-4a5c-be94-a12a926b4c89", "metadata": { "tags": [] }, "outputs": [], "source": [ "dataset = dataset.filter(lambda x: x[\"label\"] == 1)\n", "print(dataset)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "665bf34e-366a-42b7-b001-9371c3f4d864", "metadata": { "tags": [] }, "outputs": [], "source": [ "from datasets import list_datasets\n", "print(list_datasets())\n" ] }, { "cell_type": "code", "execution_count": null, "id": "11b258de-ebb3-4c57-b9ac-345a92337fca", "metadata": { "tags": [] }, "outputs": [], "source": [ "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "print(dataset)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "03f7cb40-4f0c-4e6e-8274-243b6b6dc830", "metadata": { "tags": [] }, "outputs": [], "source": [ "from datasets import list_datasets\n", "print(list_datasets())\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1e5da129-0b17-411f-a6a7-2c964962282e", "metadata": { "tags": [] }, "outputs": [], "source": [ "trainer.train()" ] }, { "cell_type": "code", "execution_count": 47, "id": "791f3a78-79ab-410f-b216-813b4cac1fe7", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:519: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " return torch.load(checkpoint_file, map_location=map_location)\n", "You're using a GPT2TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.\n" ] }, { "data": { "text/html": [ "\n", "

\n", " \n", " \n", " [560/560 2:10:53, Epoch 2/2]\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
EpochTraining LossValidation Loss
10.7043000.959849
20.6184000.948537

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "TrainOutput(global_step=560, training_loss=1.013289361340659, metrics={'train_runtime': 7863.9221, 'train_samples_per_second': 0.071, 'train_steps_per_second': 0.071, 'total_flos': 134526932090880.0, 'train_loss': 1.013289361340659, 'epoch': 2.0})" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from peft import LoraConfig, get_peft_model\n", "from transformers import TrainingArguments, AutoTokenizer, Trainer, AutoModelForCausalLM\n", "\n", "# Define PEFT configuration for LoRA\n", "peft_config = LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"],\n", ")\n", "\n", "# Define the model and tokenizer\n", "model_id = \"facebook/opt-350m\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "tokenizer.pad_token = tokenizer.eos_token\n", "\n", "# Apply the PEFT configuration to the model\n", "model = get_peft_model(model, peft_config)\n", "\n", "# Training arguments with reduced epochs and added logging\n", "training_args = TrainingArguments(\n", " output_dir=\"../models/opt-350m_CASUAL_LM\",\n", " evaluation_strategy=\"epoch\",\n", " per_device_train_batch_size=1,\n", " per_device_eval_batch_size=1,\n", " num_train_epochs=2, # Start with fewer epochs\n", " learning_rate=1e-04,\n", " max_grad_norm=0.3,\n", " warmup_ratio=0.03,\n", " lr_scheduler_type=\"cosine\",\n", " logging_dir='./logs', # Directory for storing logs\n", " logging_steps=10, # Log every 10 steps\n", " save_strategy=\"epoch\", # Save checkpoints every epoch\n", " save_total_limit=2, # Only keep the last 2 checkpoints\n", " load_best_model_at_end=True, # Load best model at the end\n", ")\n", "\n", "# Create the Trainer instance\n", "trainer = Trainer(\n", " model=model,\n", " args=training_args,\n", " train_dataset=tokenized_train_ds,\n", " eval_dataset=tokenized_eval_ds,\n", " tokenizer=tokenizer,\n", ")\n", "\n", "# Start the training process\n", "trainer.train()\n" ] }, { "cell_type": "code", "execution_count": 48, "id": "9d020728-c18d-40c3-b614-09760cb46135", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['id', 'comment_text', 'label']\n" ] } ], "source": [ "from datasets import load_dataset\n", "\n", "# Load the evaluation dataset\n", "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "\n", "# Filter for toxic prompts\n", "dataset = dataset.filter(lambda x: x[\"label\"] == 1).shuffle(seed=42).select(range(400))\n", "\n", "# Display column names to verify\n", "print(dataset.column_names)\n" ] }, { "cell_type": "code", "execution_count": 49, "id": "b3804776-cd19-453e-ae18-0dee99bee57d", "metadata": { "tags": [] }, "outputs": [ { "ename": "ImportError", "evalue": "cannot import name 'AutoPeftModelForCausalLM' from 'transformers' (/opt/app-root/lib64/python3.9/site-packages/transformers/__init__.py)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[49], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtransformers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AutoModelForCausalLM, AutoPeftModelForCausalLM\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Define your model IDs (replace with your specific paths or IDs if necessary)\u001b[39;00m\n\u001b[1;32m 4\u001b[0m model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mImportError\u001b[0m: cannot import name 'AutoPeftModelForCausalLM' from 'transformers' (/opt/app-root/lib64/python3.9/site-packages/transformers/__init__.py)" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoPeftModelForCausalLM\n", "\n", "# Define your model IDs (replace with your specific paths or IDs if necessary)\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the standard and PEFT (detoxified) models\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = AutoPeftModelForCausalLM.from_pretrained(\n", " peft_model_id,\n", " torch_dtype=torch.bfloat16,\n", ")\n", "\n", "# Store models in a dictionary for easy comparison later\n", "models_to_test = {model_id: model, peft_model_id: peft_model}\n" ] }, { "cell_type": "code", "execution_count": 50, "id": "08a2c17f-d325-446c-9013-79e9360f7615", "metadata": { "tags": [] }, "outputs": [ { "ename": "OSError", "evalue": "../models/opt-350m_CASUAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co/../models/opt-350m_CASUAL_LM/None' for available files.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[50], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Load the standard and PEFT (detoxified) models\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m get_peft_model(model, LoraConfig(\n\u001b[1;32m 11\u001b[0m r\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m64\u001b[39m,\n\u001b[1;32m 12\u001b[0m lora_alpha\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m16\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 16\u001b[0m target_modules\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mq_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mo_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 17\u001b[0m ))\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# Store models in a dictionary for easy comparison later\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/auto_factory.py:526\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 523\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mquantization_config\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 524\u001b[0m _ \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mquantization_config\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 526\u001b[0m config, kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mAutoConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 527\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 528\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_unused_kwargs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 529\u001b[0m \u001b[43m \u001b[49m\u001b[43mtrust_remote_code\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrust_remote_code\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 530\u001b[0m \u001b[43m \u001b[49m\u001b[43mcode_revision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcode_revision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 531\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 532\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhub_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 533\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 534\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 536\u001b[0m \u001b[38;5;66;03m# if torch_dtype=auto was passed here, ensure to pass it on\u001b[39;00m\n\u001b[1;32m 537\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs_orig\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtorch_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/configuration_auto.py:1082\u001b[0m, in \u001b[0;36mAutoConfig.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 1079\u001b[0m trust_remote_code \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrust_remote_code\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 1080\u001b[0m code_revision \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcode_revision\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m-> 1082\u001b[0m config_dict, unused_kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mPretrainedConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1083\u001b[0m has_remote_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoConfig\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 1084\u001b[0m has_local_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01min\u001b[39;00m CONFIG_MAPPING\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:644\u001b[0m, in \u001b[0;36mPretrainedConfig.get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 642\u001b[0m original_kwargs \u001b[38;5;241m=\u001b[39m copy\u001b[38;5;241m.\u001b[39mdeepcopy(kwargs)\n\u001b[1;32m 643\u001b[0m \u001b[38;5;66;03m# Get config dict associated with the base config file\u001b[39;00m\n\u001b[0;32m--> 644\u001b[0m config_dict, kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 645\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict:\n\u001b[1;32m 646\u001b[0m original_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:699\u001b[0m, in \u001b[0;36mPretrainedConfig._get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 695\u001b[0m configuration_file \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_configuration_file\u001b[39m\u001b[38;5;124m\"\u001b[39m, CONFIG_NAME)\n\u001b[1;32m 697\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 698\u001b[0m \u001b[38;5;66;03m# Load from local folder or from cache or download from model Hub and cache\u001b[39;00m\n\u001b[0;32m--> 699\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 700\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 701\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfiguration_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 702\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 703\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 704\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 705\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 706\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 707\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 708\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 709\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 710\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 711\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 712\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 713\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 714\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m:\n\u001b[1;32m 715\u001b[0m \u001b[38;5;66;03m# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to\u001b[39;00m\n\u001b[1;32m 716\u001b[0m \u001b[38;5;66;03m# the original exception.\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:360\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 358\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(resolved_file):\n\u001b[1;32m 359\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _raise_exceptions_for_missing_entries:\n\u001b[0;32m--> 360\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 361\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m does not appear to have a file named \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfull_filename\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. Checkout \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://huggingface.co/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrevision\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m for available files.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 363\u001b[0m )\n\u001b[1;32m 364\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 365\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "\u001b[0;31mOSError\u001b[0m: ../models/opt-350m_CASUAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co/../models/opt-350m_CASUAL_LM/None' for available files." ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import LoraConfig, get_peft_model\n", "\n", "# Define your model IDs\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the standard and PEFT (detoxified) models\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = get_peft_model(model, LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"]\n", "))\n", "\n", "# Store models in a dictionary for easy comparison later\n", "models_to_test = {model_id: model, peft_model_id: peft_model}\n" ] }, { "cell_type": "code", "execution_count": 51, "id": "f5247ef3-7387-41f8-b074-d11cff8fe725", "metadata": { "tags": [] }, "outputs": [ { "ename": "OSError", "evalue": "../models/opt-350m_CASUAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co/../models/opt-350m_CASUAL_LM/None' for available files.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[51], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 2\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 4\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 5\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m get_peft_model(AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id), peft_config)\n\u001b[1;32m 7\u001b[0m models_to_test \u001b[38;5;241m=\u001b[39m {model_id: model, peft_model_id: peft_model}\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/auto_factory.py:526\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 523\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mquantization_config\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 524\u001b[0m _ \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mquantization_config\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 526\u001b[0m config, kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mAutoConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 527\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 528\u001b[0m \u001b[43m \u001b[49m\u001b[43mreturn_unused_kwargs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 529\u001b[0m \u001b[43m \u001b[49m\u001b[43mtrust_remote_code\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrust_remote_code\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 530\u001b[0m \u001b[43m \u001b[49m\u001b[43mcode_revision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcode_revision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 531\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 532\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhub_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 533\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 534\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 536\u001b[0m \u001b[38;5;66;03m# if torch_dtype=auto was passed here, ensure to pass it on\u001b[39;00m\n\u001b[1;32m 537\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs_orig\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtorch_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/configuration_auto.py:1082\u001b[0m, in \u001b[0;36mAutoConfig.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 1079\u001b[0m trust_remote_code \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrust_remote_code\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 1080\u001b[0m code_revision \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcode_revision\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m-> 1082\u001b[0m config_dict, unused_kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mPretrainedConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1083\u001b[0m has_remote_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoConfig\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 1084\u001b[0m has_local_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01min\u001b[39;00m CONFIG_MAPPING\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:644\u001b[0m, in \u001b[0;36mPretrainedConfig.get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 642\u001b[0m original_kwargs \u001b[38;5;241m=\u001b[39m copy\u001b[38;5;241m.\u001b[39mdeepcopy(kwargs)\n\u001b[1;32m 643\u001b[0m \u001b[38;5;66;03m# Get config dict associated with the base config file\u001b[39;00m\n\u001b[0;32m--> 644\u001b[0m config_dict, kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 645\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict:\n\u001b[1;32m 646\u001b[0m original_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:699\u001b[0m, in \u001b[0;36mPretrainedConfig._get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 695\u001b[0m configuration_file \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_configuration_file\u001b[39m\u001b[38;5;124m\"\u001b[39m, CONFIG_NAME)\n\u001b[1;32m 697\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 698\u001b[0m \u001b[38;5;66;03m# Load from local folder or from cache or download from model Hub and cache\u001b[39;00m\n\u001b[0;32m--> 699\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 700\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 701\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfiguration_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 702\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 703\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 704\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 705\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 706\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 707\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 708\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 709\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 710\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 711\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 712\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 713\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 714\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m:\n\u001b[1;32m 715\u001b[0m \u001b[38;5;66;03m# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to\u001b[39;00m\n\u001b[1;32m 716\u001b[0m \u001b[38;5;66;03m# the original exception.\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:360\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 358\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(resolved_file):\n\u001b[1;32m 359\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _raise_exceptions_for_missing_entries:\n\u001b[0;32m--> 360\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 361\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m does not appear to have a file named \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfull_filename\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. Checkout \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://huggingface.co/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrevision\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m for available files.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 363\u001b[0m )\n\u001b[1;32m 364\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 365\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "\u001b[0;31mOSError\u001b[0m: ../models/opt-350m_CASUAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co/../models/opt-350m_CASUAL_LM/None' for available files." ] } ], "source": [ "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = get_peft_model(AutoModelForCausalLM.from_pretrained(model_id), peft_config)\n", "\n", "models_to_test = {model_id: model, peft_model_id: peft_model}\n" ] }, { "cell_type": "code", "execution_count": 52, "id": "61719956-fe3e-48a0-b679-15315ba7f9cc", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'peft_model' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[52], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m model\u001b[38;5;241m.\u001b[39msave_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mpeft_model\u001b[49m\u001b[38;5;241m.\u001b[39msave_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'peft_model' is not defined" ] } ], "source": [ "model.save_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "peft_model.save_pretrained(\"../models/opt-350m_DETOXIFY_CAUSAL_LM\")\n" ] }, { "cell_type": "code", "execution_count": 53, "id": "66ab5833-516b-4fbc-a1af-1df74d19f586", "metadata": { "tags": [] }, "outputs": [], "source": [ "model.save_pretrained(\"../models/opt-350m_CASUAL_LM\")\n" ] }, { "cell_type": "code", "execution_count": 54, "id": "e7673156-26fd-4591-8756-8d4761b27b31", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'peft_model' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[54], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mpeft_model\u001b[49m\u001b[38;5;241m.\u001b[39msave_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'peft_model' is not defined" ] } ], "source": [ "peft_model.save_pretrained(\"../models/opt-350m_DETOXIFY_CAUSAL_LM\")\n" ] }, { "cell_type": "code", "execution_count": 55, "id": "a2e2e0b6-22f1-4ccb-846c-a174ec1efbd7", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Make sure you've correctly initialized the peft_model\n", "peft_model = get_peft_model(model, LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"],\n", "))\n", "\n", "# Save the model after it has been properly initialized\n", "model.save_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "peft_model.save_pretrained(\"../models/opt-350m_DETOXIFY_CAUSAL_LM\")\n" ] }, { "cell_type": "code", "execution_count": 56, "id": "37a32643-974b-4cc3-b389-f487ff79b917", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['id', 'comment_text', 'label']\n" ] } ], "source": [ "from datasets import load_dataset\n", "\n", "# Load the dataset\n", "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "\n", "# Filter for toxic prompts\n", "dataset = dataset.filter(lambda x: x[\"label\"] == 1).shuffle(seed=42).select(indices=range(0, 400))\n", "print(dataset.column_names)\n" ] }, { "cell_type": "code", "execution_count": 57, "id": "4ea733e3-6d8e-4e2c-b54f-0cffffe2896c", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:519: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " return torch.load(checkpoint_file, map_location=map_location)\n", "Loading adapter weights from ../models/opt-350m_DETOXIFY_CAUSAL_LM led to unexpected keys not found in the model: ['base_model.model.model.decoder.layers.0.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.0.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.0.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.0.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.0.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.0.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.1.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.1.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.1.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.1.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.1.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.1.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.10.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.10.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.10.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.10.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.10.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.10.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.11.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.11.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.11.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.11.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.11.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.11.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.12.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.12.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.12.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.12.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.12.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.12.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.13.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.13.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.13.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.13.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.13.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.13.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.14.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.14.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.14.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.14.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.14.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.14.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.15.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.15.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.15.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.15.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.15.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.15.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.16.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.16.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.16.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.16.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.16.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.16.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.17.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.17.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.17.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.17.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.17.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.17.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.18.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.18.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.18.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.18.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.18.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.18.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.19.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.19.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.19.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.19.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.19.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.19.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.2.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.2.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.2.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.2.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.2.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.2.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.20.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.20.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.20.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.20.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.20.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.20.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.21.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.21.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.21.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.21.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.21.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.21.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.22.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.22.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.22.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.22.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.22.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.22.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.23.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.23.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.23.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.23.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.23.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.23.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.3.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.3.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.3.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.3.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.3.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.3.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.4.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.4.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.4.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.4.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.4.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.4.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.5.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.5.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.5.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.5.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.5.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.5.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.6.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.6.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.6.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.6.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.6.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.6.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.7.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.7.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.7.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.7.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.7.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.7.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.8.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.8.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.8.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.8.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.8.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.8.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.9.self_attn.k_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.9.self_attn.k_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.9.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.9.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.decoder.layers.9.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.decoder.layers.9.self_attn.v_proj.lora_B.default.weight']. \n" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import get_peft_model, LoraConfig\n", "\n", "# Define your model IDs (replace with your specific paths or IDs if necessary)\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the standard and PEFT (detoxified) models\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = AutoModelForCausalLM.from_pretrained(peft_model_id)\n", "\n", "# Store models in a dictionary for easy comparison later\n", "models_to_test = {model_id: model, peft_model_id: peft_model}\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4e8de9eb-7378-4dc3-80d4-8d11308d7d21", "metadata": { "tags": [] }, "outputs": [], "source": [ "import logging\n", "import time\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import get_peft_model, LoraConfig\n", "\n", "# Initialize logging\n", "logging.basicConfig(level=logging.INFO)\n", "logger = logging.getLogger(__name__)\n", "\n", "# Measure the time taken to load\n", "start_time = time.time()\n", "\n", "# Define PEFT configuration for LoRA\n", "peft_config = LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"],\n", ")\n", "\n", "# Define model paths\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load standard model\n", "logger.info(\"Loading the standard model...\")\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "logger.info(f\"Standard model loaded in {time.time() - start_time:.2f} seconds.\")\n", "\n", "# Apply PEFT configuration\n", "logger.info(\"Applying PEFT configuration...\")\n", "peft_model = get_peft_model(model, peft_config)\n", "logger.info(\"PEFT configuration applied.\")\n", "\n", "# Save models to ensure they are correctly loaded and configured\n", "logger.info(\"Saving models...\")\n", "model.save_pretrained(model_id)\n", "peft_model.save_pretrained(peft_model_id)\n", "logger.info(f\"Models saved successfully in {time.time() - start_time:.2f} seconds.\")\n", "\n", "logger.info(f\"Total time taken: {time.time() - start_time:.2f} seconds.\")\n", "\n", "# Track completion of each stage\n", "logger.info(\"Loading adapter weights...\")\n", "peft_model = AutoModelForCausalLM.from_pretrained(peft_model_id)\n", "logger.info(\"Adapter weights loaded.\")\n", "\n", "logger.info(f\"Loading process completed in {time.time() - start_time:.2f} seconds.\")\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "35667879-7ba1-4111-b7b1-dcf11944a59f", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/transformers/utils/generic.py:441: FutureWarning: `torch.utils._pytree._register_pytree_node` is deprecated. Please use `torch.utils._pytree.register_pytree_node` instead.\n", " _torch_pytree._register_pytree_node(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/utils/generic.py:309: FutureWarning: `torch.utils._pytree._register_pytree_node` is deprecated. Please use `torch.utils._pytree.register_pytree_node` instead.\n", " _torch_pytree._register_pytree_node(\n", "2024-08-14 16:56:13,524 - INFO - Loading the standard model...\n", "/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:519: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " return torch.load(checkpoint_file, map_location=map_location)\n", "/opt/app-root/lib64/python3.9/site-packages/bitsandbytes/cextension.py:34: UserWarning: The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.\n", " warn(\"The installed version of bitsandbytes was compiled without GPU support. \"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/bitsandbytes/libbitsandbytes_cpu.so: undefined symbol: cadam32bit_grad_fp32\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "2024-08-14 16:56:24,570 - INFO - Standard model loaded in 11.05 seconds.\n", "2024-08-14 16:56:24,571 - INFO - Applying PEFT configuration...\n", "2024-08-14 16:56:24,572 - INFO - Already found a `peft_config` attribute in the model. This will lead to having multiple adapters in the model. Make sure to know what you are doing!\n", "2024-08-14 16:56:25,888 - INFO - PEFT configuration applied.\n", "2024-08-14 16:56:25,889 - INFO - Saving models...\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/integrations/peft.py:391: FutureWarning: The `active_adapter` method is deprecated and will be removed in a future version.\n", " warnings.warn(\n", "2024-08-14 16:56:26,166 - INFO - Models saved successfully in 12.64 seconds.\n", "2024-08-14 16:56:26,168 - INFO - Loading adapter weights...\n", "/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:519: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " return torch.load(checkpoint_file, map_location=map_location)\n", "2024-08-14 16:56:31,883 - INFO - Adapter weights loaded.\n", "2024-08-14 16:56:31,884 - INFO - Loading process completed in 18.36 seconds.\n" ] } ], "source": [ "import logging\n", "import time\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import get_peft_model, LoraConfig\n", "\n", "# Initialize Logging\n", "logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')\n", "logger = logging.getLogger(__name__)\n", "\n", "# Measure the time to load\n", "start_time = time.time()\n", "\n", "# Define PEFT configuration for LoRA\n", "peft_config = LoraConfig(\n", " r=64,\n", " lora_alpha=16,\n", " lora_dropout=0.1,\n", " bias=\"none\",\n", " task_type=\"CAUSAL_LM\",\n", " target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\"],\n", ")\n", "\n", "# Define model paths\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load standard model\n", "logger.info(\"Loading the standard model...\")\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "logger.info(f\"Standard model loaded in {time.time() - start_time:.2f} seconds.\")\n", "\n", "# Apply PEFT configuration\n", "logger.info(\"Applying PEFT configuration...\")\n", "peft_model = get_peft_model(model, peft_config)\n", "logger.info(\"PEFT configuration applied.\")\n", "\n", "# Save models to ensure they are correctly loaded and configured\n", "logger.info(\"Saving models...\")\n", "model.save_pretrained(model_id)\n", "peft_model.save_pretrained(peft_model_id)\n", "logger.info(f\"Models saved successfully in {time.time() - start_time:.2f} seconds.\")\n", "\n", "# Track completion of each stage\n", "logger.info(\"Loading adapter weights...\")\n", "peft_model = AutoModelForCausalLM.from_pretrained(peft_model_id)\n", "logger.info(\"Adapter weights loaded.\")\n", "logger.info(f\"Loading process completed in {time.time() - start_time:.2f} seconds.\")\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "c7320d7a-ac75-41e4-89b3-806d6ad7de0b", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'device' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[13], line 21\u001b[0m\n\u001b[1;32m 15\u001b[0m torch\u001b[38;5;241m.\u001b[39mmanual_seed(\u001b[38;5;241m42\u001b[39m)\n\u001b[1;32m 16\u001b[0m input_text \u001b[38;5;241m=\u001b[39m example[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcomment_text\u001b[39m\u001b[38;5;124m\"\u001b[39m][:context_length]\n\u001b[1;32m 17\u001b[0m inputs \u001b[38;5;241m=\u001b[39m tokenizer(\n\u001b[1;32m 18\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPrompt: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00minput_text\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mContinuation:\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 19\u001b[0m padding\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m 20\u001b[0m return_tensors\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpt\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m---> 21\u001b[0m )\u001b[38;5;241m.\u001b[39mto(\u001b[43mdevice\u001b[49m)\n\u001b[1;32m 22\u001b[0m inputs\u001b[38;5;241m.\u001b[39minput_ids \u001b[38;5;241m=\u001b[39m inputs\u001b[38;5;241m.\u001b[39minput_ids[:context_length]\n\u001b[1;32m 23\u001b[0m inputs\u001b[38;5;241m.\u001b[39mattention_mask \u001b[38;5;241m=\u001b[39m inputs\u001b[38;5;241m.\u001b[39mattention_mask[:context_length]\n", "\u001b[0;31mNameError\u001b[0m: name 'device' is not defined" ] } ], "source": [ "import torch\n", "# index prompts to a length of 2000\n", "context_length = 2000\n", "output_texts = {}\n", "\n", "# load tokenizer and add eos token and padding side to prevent warnings\n", "tokenizer = AutoTokenizer.from_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "tokenizer.pad_token = tokenizer.eos_token\n", "tokenizer.padding_side = \"left\"\n", "\n", "for model_name in models_to_test.keys():\n", " model = models_to_test[model_name]\n", " output_texts[model_name] = []\n", " for i, example in enumerate(dataset):\n", " torch.manual_seed(42)\n", " input_text = example[\"comment_text\"][:context_length]\n", " inputs = tokenizer(\n", " f\"Prompt: {input_text}\\nContinuation:\",\n", " padding=True,\n", " return_tensors=\"pt\",\n", " ).to(device)\n", " inputs.input_ids = inputs.input_ids[:context_length]\n", " inputs.attention_mask = inputs.attention_mask[:context_length]\n", "\n", " # define generation args\n", " generated_texts = model.generate(\n", " **inputs,\n", " max_new_tokens=30,\n", " do_sample=True,\n", " temperature=0.7,\n", " top_k=50,\n", " top_p=0.95,\n", " repetition_penalty=1.2 # prevents repetition\n", " )\n", " generated_texts = tokenizer.batch_decode(\n", " generated_texts.detach().cpu().numpy(),\n", " skip_special_tokens=True\n", " )\n", " output_texts[model_name].append(generated_texts[0][len(input_text):])\n", "\n", " # delete model to free up memory\n", " model = None\n", " torch.cuda.empty_cache()\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "9d51817a-0fe1-4c8c-8153-4c4fbae16b2a", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Load the original model's config\n", "original_model_id = \"facebook/opt-350m\" # or any other model ID you initially used\n", "config = AutoModelForCausalLM.from_pretrained(original_model_id).config\n", "\n", "# Save the config to the directory where you saved the fine-tuned model\n", "config.save_pretrained(\"../models/opt-350m_CASUAL_LM\")\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "bb0dc7c2-2672-4c1a-8875-c9a819d05ec3", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "('../models/opt-350m_CASUAL_LM/tokenizer_config.json',\n", " '../models/opt-350m_CASUAL_LM/special_tokens_map.json',\n", " '../models/opt-350m_CASUAL_LM/vocab.json',\n", " '../models/opt-350m_CASUAL_LM/merges.txt',\n", " '../models/opt-350m_CASUAL_LM/added_tokens.json',\n", " '../models/opt-350m_CASUAL_LM/tokenizer.json')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from transformers import AutoTokenizer\n", "\n", "original_model_id = \"facebook/opt-350m\"\n", "tokenizer = AutoTokenizer.from_pretrained(original_model_id)\n", "tokenizer.save_pretrained(\"../models/opt-350m_CASUAL_LM\")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "17e1a875-ef0c-4d16-83b8-7af1e03ea784", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Define model IDs\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the models\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = AutoModelForCausalLM.from_pretrained(peft_model_id)\n", "\n", "# Store models in a dictionary\n", "models_to_test = {\n", " \"casual_model\": model,\n", " \"detoxify_model\": peft_model,\n", "}\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "60ea21d4-d194-452a-b2b6-a09d08b7df3f", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-08-14 17:15:51,603 - INFO - PyTorch version 2.4.0 available.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['id', 'comment_text', 'label']\n" ] } ], "source": [ "from datasets import load_dataset\n", "\n", "# Load the dataset\n", "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "\n", "# Filter for toxic prompts and shuffle\n", "dataset = dataset.filter(lambda x: x[\"label\"] == 1).shuffle(seed=42).select(indices=range(0, 400))\n", "\n", "print(dataset.column_names) # To verify that the dataset is loaded correctly\n" ] }, { "cell_type": "code", "execution_count": null, "id": "644a3a9c-9497-4ca1-9047-1b44d3549fd2", "metadata": { "tags": [] }, "outputs": [], "source": [ "import torch\n", "\n", "# Set the device to use\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "\n", "# index prompts to a length of 2000\n", "context_length = 2000\n", "output_texts = {}\n", "\n", "# load tokenizer and add eos token and padding side to prevent warnings\n", "tokenizer = AutoTokenizer.from_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "tokenizer.pad_token = tokenizer.eos_token\n", "tokenizer.padding_side = \"left\"\n", "\n", "for model_name in models_to_test.keys():\n", " model = models_to_test[model_name]\n", " model.to(device) # Move model to the appropriate device\n", " output_texts[model_name] = []\n", " for i, example in enumerate(dataset):\n", " torch.manual_seed(42)\n", " input_text = example[\"comment_text\"][:context_length]\n", " inputs = tokenizer(\n", " f\"Prompt: {input_text}\\nContinuation:\",\n", " padding=True,\n", " return_tensors=\"pt\",\n", " ).to(device)\n", " inputs.input_ids = inputs.input_ids[:context_length]\n", " inputs.attention_mask = inputs.attention_mask[:context_length]\n", " \n", " # define generation args\n", " generated_texts = model.generate(\n", " **inputs,\n", " max_new_tokens=30,\n", " do_sample=True,\n", " temperature=0.7,\n", " top_k=50,\n", " top_p=0.95,\n", " repetition_penalty=1.2 # prevents repetition\n", " )\n", " generated_texts = tokenizer.batch_decode(\n", " generated_texts.detach().cpu().numpy(),\n", " skip_special_tokens=True\n", " )\n", " output_texts[model_name].append(generated_texts[0][len(input_text):])\n", " \n", " # delete model to free up memory\n", " model = None\n", " torch.cuda.empty_cache()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ec505f8f-15c3-4d8f-9562-63dab94f95ba", "metadata": { "tags": [] }, "outputs": [], "source": [ "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")" ] }, { "cell_type": "code", "execution_count": null, "id": "279ec799-194b-4c14-a081-dbbad48cb196", "metadata": { "tags": [] }, "outputs": [], "source": [ "toxicities = {}\n", " for model_name in list(models_to_test.keys()):\n", " toxicities[model_name] = []\n", " for generated_text in output_texts[model_name]:\n", " score = toxicity.compute(generated_text)\n", " toxicities[model_name].append(score)\n", " print(\"##\"*5 + f\"Model {model_name}\" + \"##\"*5)\n", " print(f\"Mean toxicity: {np.mean(toxicities[model_name])}\")\n", " print(f\"Std: {np.std(toxicities[model_name])}\")\n", " print(\" \")" ] }, { "cell_type": "code", "execution_count": null, "id": "4b475c96-6f27-4d2f-832e-427f69eb51d7", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Dictionary to store toxicities\n", "toxicities = {}\n", "\n", "# Loop through each model to test\n", "for model_name in list(models_to_test.keys()):\n", " print(f\"Evaluating model: {model_name}\")\n", " toxicities[model_name] = []\n", "\n", " # Loop through generated texts\n", " for i, generated_text in enumerate(output_texts[model_name]):\n", " print(f\"Processing generated text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n", " \n", " # Compute toxicity\n", " score = toxicity.compute(predictions=[generated_text])\n", " print(f\"Computed score: {score}\")\n", " \n", " # Append score to the list\n", " toxicities[model_name].append(score)\n", " \n", " # Output the results for this model\n", " print(\"##\" * 5 + f\" Model {model_name} \" + \"##\" * 5)\n", " print(f\"Mean toxicity: {np.mean([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(f\"Std: {np.std([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "3bae3143-6203-4335-aaac-e854e3bfbf34", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Dictionary to store toxicities\n", "toxicities = {}\n", "\n", "# Loop through each model to test\n", "for model_name in list(models_to_test.keys()):\n", " print(f\"Evaluating model: {model_name}\")\n", " toxicities[model_name] = []\n", "\n", " # Loop through generated texts\n", " for i, generated_text in enumerate(output_texts[model_name]):\n", " print(f\"Processing generated text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n", " \n", " # Compute toxicity\n", " try:\n", " score = toxicity.compute(predictions=[generated_text])\n", " print(f\"Computed score: {score}\")\n", " # Append score to the list\n", " toxicities[model_name].append(score)\n", " except Exception as e:\n", " print(f\"Error while computing toxicity: {e}\")\n", " continue\n", "\n", " # Output the results for this model\n", " print(\"##\" * 5 + f\" Model {model_name} \" + \"##\" * 5)\n", " print(f\"Mean toxicity: {np.mean([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(f\"Std: {np.std([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "dc2e30f4-e365-4898-b9b3-4fb24850939d", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(f\"Processing generated text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "46615f86-31f2-406a-a13d-8c0d54a7c44f", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Dictionary to store toxicities\n", "toxicities = {}\n", "\n", "# Loop through each model to test\n", "for model_name in list(models_to_test.keys()):\n", " print(f\"Evaluating model: {model_name}\")\n", " toxicities[model_name] = []\n", "\n", " # Loop through generated texts\n", " for i, generated_text in enumerate(output_texts[model_name]):\n", " print(f\"Processing generated text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n", " try:\n", " # Compute toxicity\n", " score = toxicity.compute(predictions=[generated_text])\n", " print(f\"Computed score: {score}\")\n", "\n", " # Append score to the list\n", " toxicities[model_name].append(score)\n", " except Exception as e:\n", " print(f\"Error while computing toxicity: {e}\")\n", " continue\n", "\n", " # Output the results for this model\n", " print(\"##########\" + f\"Model {model_name}\" + \"##########\")\n", " print(f\"Mean toxicity: {np.mean([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(f\"Std: {np.std([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fa79f4c1-478d-4450-9ca3-159fb979307b", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Dictionary to store toxicities\n", "toxicities = {}\n", "\n", "# Loop through each model to test\n", "for model_name in list(models_to_test.keys()):\n", " print(f\"Evaluating model: {model_name}\")\n", " toxicities[model_name] = []\n", "\n", " # Check if there is output generated for the model\n", " if model_name not in output_texts or len(output_texts[model_name]) == 0:\n", " print(f\"No generated texts found for model {model_name}. Skipping.\")\n", " continue\n", "\n", " # Loop through generated texts\n", " for i, generated_text in enumerate(output_texts[model_name]):\n", " print(f\"Processing generated text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n", " \n", " # Compute toxicity\n", " try:\n", " print(f\"Generated text: {generated_text}\") # Add this to see the actual text\n", " score = toxicity.compute(predictions=[generated_text])\n", " print(f\"Computed score: {score}\")\n", " \n", " # Append score to the list\n", " toxicities[model_name].append(score)\n", " except Exception as e:\n", " print(f\"Error while computing toxicity: {e}\")\n", " continue\n", "\n", " # Output the results for this model\n", " print(\"##########\" + f\"Model {model_name}\" + \"##########\")\n", " if toxicities[model_name]:\n", " print(f\"Mean toxicity: {np.mean([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(f\"Std: {np.std([s['toxicity'] for s in toxicities[model_name]])}\")\n", " else:\n", " print(f\"No toxicity scores were computed for model {model_name}.\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "20638452-03b0-40cb-80f6-0e95f2b36b6a", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Dictionary to store toxicities\n", "toxicities = {}\n", "\n", "# Loop through each model to test\n", "for model_name in list(models_to_test.keys()):\n", " print(f\"Evaluating model: {model_name}\")\n", " toxicities[model_name] = []\n", "\n", " # Check if there is output generated for the model\n", " if model_name not in output_texts or len(output_texts[model_name]) == 0:\n", " print(f\"No generated texts found for model {model_name}. Skipping.\")\n", " continue\n", "\n", " # Loop through generated texts\n", " for i, generated_text in enumerate(output_texts[model_name]):\n", " print(f\"Processing generated text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n", "\n", " # Compute toxicity\n", " try:\n", " print(f\"Generated text: {generated_text}\") # Add this to see the actual text\n", " score = toxicity.compute(predictions=[generated_text])\n", " print(f\"Computed score: {score}\")\n", " # Append score to the list\n", " toxicities[model_name].append(score)\n", " except Exception as e:\n", " print(f\"Error while computing toxicity: {e}\")\n", " continue\n", "\n", " # Output the results for this model\n", " print(\"##########\" + f\"Model {model_name}\" + \"##########\")\n", " if toxicities[model_name]:\n", " print(f\"Mean toxicity: {np.mean([s['toxicity'] for s in toxicities[model_name]])}\")\n", " print(f\"Std: {np.std([s['toxicity'] for s in toxicities[model_name]])}\")\n", " else:\n", " print(f\"No toxicity scores were computed for model {model_name}.\")\n", " print(\" \")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1baa898b-bb92-4352-a92d-e481705969fb", "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "\n", "# Load the toxicity evaluation module\n", "toxicity = evaluate.load(\"toxicity\", module_type=\"measurement\")\n", "\n", "# Dictionary to store toxicities\n", "toxicities = {}\n", "\n", "# Check if output_texts contains anything\n", "if not output_texts:\n", " print(\"No output texts found. Exiting the evaluation.\")\n", "else:\n", " print(f\"Output texts found for models: {list(output_texts.keys())}\")\n", "\n", "# Loop through each model to test\n", "for model_name in models_to_test.keys():\n", " print(f\"Evaluating model: {model_name}\")\n", " \n", " if model_name not in output_texts:\n", " print(f\"No generated texts found for model {model_name}. Skipping.\")\n", " continue\n", " \n", " print(f\"Number of generated texts for model {model_name}: {len(output_texts[model_name])}\")\n", " \n", " toxicities[model_name] = []\n", " \n", " for i, generated_text in enumerate(output_texts[model_name]):\n", " print(f\"Processing text {i+1}/{len(output_texts[model_name])} for model: {model_name}\")\n", " \n", " try:\n", " score = toxicity.compute(predictions=[generated_text])\n", " print(f\"Score computed: {score}\")\n", " toxicities[model_name].append(score)\n", " except Exception as e:\n", " print(f\"Error in computing toxicity: {e}\")\n", " \n", " if toxicities[model_name]:\n", " print(f\"Results for model {model_name}: Mean toxicity: {np.mean([s['toxicity'] for s in toxicities[model_name]])}, Std: {np.std([s['toxicity'] for s in toxicities[model_name]])}\")\n", " else:\n", " print(f\"No toxicity scores computed for model {model_name}.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ae5c12d1-c765-42d6-9ca3-27dd3b0a17ac", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(dataset.column_names) " ] }, { "cell_type": "code", "execution_count": null, "id": "9c9909d2-9928-46bc-ae63-f5d6836ae157", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(len(dataset))" ] }, { "cell_type": "code", "execution_count": null, "id": "66c8ab41-2213-43d4-a434-6a7719daa265", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(dataset[0])" ] }, { "cell_type": "code", "execution_count": null, "id": "13d1daf4-9be6-4101-bcc6-757f938814f3", "metadata": { "tags": [] }, "outputs": [], "source": [ "output_texts.keys()" ] }, { "cell_type": "code", "execution_count": null, "id": "1f14370e-cc01-444a-b5b2-935e531e01af", "metadata": { "tags": [] }, "outputs": [], "source": [ "sample_text = [\"This is a test sentence.\"]\n", "score = toxicity.compute(predictions=sample_text)\n", "print(score)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2b4ad27b-00ba-4e6b-a5df-f07f3e8bfe69", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(len(dataset))" ] }, { "cell_type": "code", "execution_count": null, "id": "5eb05f79-72ee-46aa-9fb5-cc8d300adc15", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(len(dataset))" ] }, { "cell_type": "code", "execution_count": null, "id": "d621b039-95a6-45ac-9e06-6e192c09917a", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(dataset)" ] }, { "cell_type": "code", "execution_count": null, "id": "87185e2d-c01f-4614-a9e8-c93c7049f87e", "metadata": { "tags": [] }, "outputs": [], "source": [ "print(dataset)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9b486a6b-60d8-4f5e-80a8-5ec203345d47", "metadata": { "tags": [] }, "outputs": [], "source": [ "# Reload the dataset to ensure it's properly loaded\n", "from datasets import load_dataset\n", "\n", "# Load the dataset\n", "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "\n", "# Filter for toxic prompts and shuffle\n", "dataset = dataset.filter(lambda x: x[\"label\"] == 1).shuffle(seed=42).select(indices=range(0, 400))\n", "\n", "# Print the dataset\n", "print(f\"Dataset loaded successfully with {len(dataset)} entries.\")\n", "print(f\"First entry: {dataset[0]}\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "81baea1c-6d16-4394-88cf-abbe58d696bd", "metadata": { "tags": [] }, "outputs": [], "source": [ "from datasets import load_dataset\n", "\n", "# Load the dataset\n", "dataset = load_dataset(\"OxAISH-AL-LLM/wiki_toxic\", split=\"test\")\n", "\n", "# Verify if the dataset has been loaded properly\n", "if dataset:\n", " print(f\"Dataset loaded successfully with {len(dataset)} entries.\")\n", " print(f\"First entry: {dataset[0]}\")\n", "else:\n", " print(\"Dataset failed to load or is empty.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "12ad237a-1547-4250-b3b3-713c26cd5dc0", "metadata": { "tags": [] }, "outputs": [], "source": [ "import datasets\n", "print(datasets.__version__)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "81c75b9a-c494-41ca-9634-4fc097c6ec90", "metadata": { "tags": [] }, "outputs": [], "source": [ "from datasets import load_dataset\n", "dataset = load_dataset(\"ag_news\", split=\"test\")\n", "print(f\"Loaded {len(dataset)} entries from the AG News dataset.\")\n", "print(dataset[0])\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "d837dcf9-ffa1-4406-bb8e-bb48c06ae049", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: datasets in /opt/app-root/lib/python3.9/site-packages (2.16.1)\n", "Requirement already satisfied: transformers in /opt/app-root/lib/python3.9/site-packages (4.36.2)\n", "Requirement already satisfied: evaluate in /opt/app-root/lib/python3.9/site-packages (0.4.1)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from datasets) (3.14.0)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from datasets) (1.24.4)\n", "Requirement already satisfied: pyarrow>=8.0.0 in /opt/app-root/lib/python3.9/site-packages (from datasets) (14.0.1)\n", "Requirement already satisfied: pyarrow-hotfix in /opt/app-root/lib/python3.9/site-packages (from datasets) (0.6)\n", "Requirement already satisfied: dill<0.3.8,>=0.3.0 in /opt/app-root/lib/python3.9/site-packages (from datasets) (0.3.7)\n", "Requirement already satisfied: pandas in /opt/app-root/lib/python3.9/site-packages (from datasets) (1.5.3)\n", "Requirement already satisfied: requests>=2.19.0 in /opt/app-root/lib/python3.9/site-packages (from datasets) (2.32.2)\n", "Requirement already satisfied: tqdm>=4.62.1 in /opt/app-root/lib/python3.9/site-packages (from datasets) (4.66.4)\n", "Requirement already satisfied: xxhash in /opt/app-root/lib/python3.9/site-packages (from datasets) (3.4.1)\n", "Requirement already satisfied: multiprocess in /opt/app-root/lib/python3.9/site-packages (from datasets) (0.70.15)\n", "Requirement already satisfied: fsspec<=2023.10.0,>=2023.1.0 in /opt/app-root/lib/python3.9/site-packages (from fsspec[http]<=2023.10.0,>=2023.1.0->datasets) (2023.10.0)\n", "Requirement already satisfied: aiohttp in /opt/app-root/lib/python3.9/site-packages (from datasets) (3.9.5)\n", "Requirement already satisfied: huggingface-hub>=0.19.4 in /opt/app-root/lib/python3.9/site-packages (from datasets) (0.24.5)\n", "Requirement already satisfied: packaging in /opt/app-root/lib/python3.9/site-packages (from datasets) (24.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from datasets) (6.0.1)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers) (2024.7.24)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.15.2)\n", "Requirement already satisfied: safetensors>=0.3.1 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.4.4)\n", "Requirement already satisfied: responses<0.19 in /opt/app-root/lib/python3.9/site-packages (from evaluate) (0.18.0)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets) (1.3.1)\n", "Requirement already satisfied: attrs>=17.3.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets) (23.2.0)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets) (1.4.1)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets) (6.0.5)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets) (1.9.4)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets) (4.0.3)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub>=0.19.4->datasets) (4.11.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests>=2.19.0->datasets) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests>=2.19.0->datasets) (3.7)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests>=2.19.0->datasets) (1.26.18)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests>=2.19.0->datasets) (2024.2.2)\n", "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/app-root/lib/python3.9/site-packages (from pandas->datasets) (2.9.0.post0)\n", "Requirement already satisfied: pytz>=2020.1 in /opt/app-root/lib/python3.9/site-packages (from pandas->datasets) (2024.1)\n", "Requirement already satisfied: six>=1.5 in /opt/app-root/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas->datasets) (1.16.0)\n" ] } ], "source": [ "!pip install datasets transformers evaluate\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "a9aee817-a07e-4fda-87d0-48007ee25f97", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cce033b68a0f481d8b727e08d28db18f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Downloading readme: 0%| | 0.00/7.81k [00:00 6\u001b[0m dataset \u001b[38;5;241m=\u001b[39m \u001b[43mdataset\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmap\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mexamples\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtokenizer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexamples\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mcomment_text\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtruncation\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpadding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mmax_length\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_length\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m2000\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbatched\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Convert dataset to PyTorch tensors\u001b[39;00m\n\u001b[1;32m 9\u001b[0m dataset\u001b[38;5;241m.\u001b[39mset_format(\u001b[38;5;28mtype\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtorch\u001b[39m\u001b[38;5;124m'\u001b[39m, columns\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minput_ids\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mattention_mask\u001b[39m\u001b[38;5;124m'\u001b[39m])\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/datasets/arrow_dataset.py:592\u001b[0m, in \u001b[0;36mtransmit_tasks..wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 590\u001b[0m \u001b[38;5;28mself\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDataset\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mself\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 591\u001b[0m \u001b[38;5;66;03m# apply actual function\u001b[39;00m\n\u001b[0;32m--> 592\u001b[0m out: Union[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDataset\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDatasetDict\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 593\u001b[0m datasets: List[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDataset\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(out\u001b[38;5;241m.\u001b[39mvalues()) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(out, \u001b[38;5;28mdict\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m [out]\n\u001b[1;32m 594\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m dataset \u001b[38;5;129;01min\u001b[39;00m datasets:\n\u001b[1;32m 595\u001b[0m \u001b[38;5;66;03m# Remove task templates if a column mapping of the template is no longer valid\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/datasets/arrow_dataset.py:557\u001b[0m, in \u001b[0;36mtransmit_format..wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 550\u001b[0m self_format \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 551\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtype\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_format_type,\n\u001b[1;32m 552\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mformat_kwargs\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_format_kwargs,\n\u001b[1;32m 553\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcolumns\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_format_columns,\n\u001b[1;32m 554\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124moutput_all_columns\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_output_all_columns,\n\u001b[1;32m 555\u001b[0m }\n\u001b[1;32m 556\u001b[0m \u001b[38;5;66;03m# apply actual function\u001b[39;00m\n\u001b[0;32m--> 557\u001b[0m out: Union[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDataset\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDatasetDict\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 558\u001b[0m datasets: List[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDataset\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(out\u001b[38;5;241m.\u001b[39mvalues()) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(out, \u001b[38;5;28mdict\u001b[39m) \u001b[38;5;28;01melse\u001b[39;00m [out]\n\u001b[1;32m 559\u001b[0m \u001b[38;5;66;03m# re-apply format to the output\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/datasets/arrow_dataset.py:3093\u001b[0m, in \u001b[0;36mDataset.map\u001b[0;34m(self, function, with_indices, with_rank, input_columns, batched, batch_size, drop_last_batch, remove_columns, keep_in_memory, load_from_cache_file, cache_file_name, writer_batch_size, features, disable_nullable, fn_kwargs, num_proc, suffix_template, new_fingerprint, desc)\u001b[0m\n\u001b[1;32m 3087\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m transformed_dataset \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 3088\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m hf_tqdm(\n\u001b[1;32m 3089\u001b[0m unit\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m examples\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 3090\u001b[0m total\u001b[38;5;241m=\u001b[39mpbar_total,\n\u001b[1;32m 3091\u001b[0m desc\u001b[38;5;241m=\u001b[39mdesc \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mMap\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 3092\u001b[0m ) \u001b[38;5;28;01mas\u001b[39;00m pbar:\n\u001b[0;32m-> 3093\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m rank, done, content \u001b[38;5;129;01min\u001b[39;00m Dataset\u001b[38;5;241m.\u001b[39m_map_single(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mdataset_kwargs):\n\u001b[1;32m 3094\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m done:\n\u001b[1;32m 3095\u001b[0m shards_done \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/datasets/arrow_dataset.py:3470\u001b[0m, in \u001b[0;36mDataset._map_single\u001b[0;34m(shard, function, with_indices, with_rank, input_columns, batched, batch_size, drop_last_batch, remove_columns, keep_in_memory, cache_file_name, writer_batch_size, features, disable_nullable, fn_kwargs, new_fingerprint, rank, offset)\u001b[0m\n\u001b[1;32m 3466\u001b[0m indices \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\n\u001b[1;32m 3467\u001b[0m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m*\u001b[39m(\u001b[38;5;28mslice\u001b[39m(i, i \u001b[38;5;241m+\u001b[39m batch_size)\u001b[38;5;241m.\u001b[39mindices(shard\u001b[38;5;241m.\u001b[39mnum_rows)))\n\u001b[1;32m 3468\u001b[0m ) \u001b[38;5;66;03m# Something simpler?\u001b[39;00m\n\u001b[1;32m 3469\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 3470\u001b[0m batch \u001b[38;5;241m=\u001b[39m \u001b[43mapply_function_on_filtered_inputs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 3471\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3472\u001b[0m \u001b[43m \u001b[49m\u001b[43mindices\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3473\u001b[0m \u001b[43m \u001b[49m\u001b[43mcheck_same_num_examples\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mshard\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlist_indexes\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3474\u001b[0m \u001b[43m \u001b[49m\u001b[43moffset\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moffset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3475\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3476\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m NumExamplesMismatchError:\n\u001b[1;32m 3477\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m DatasetTransformationNotAllowedError(\n\u001b[1;32m 3478\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUsing `.map` in batched mode on a dataset with attached indexes is allowed only if it doesn\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt create or remove existing examples. You can first run `.drop_index() to remove your index and then re-add it.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 3479\u001b[0m ) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/datasets/arrow_dataset.py:3349\u001b[0m, in \u001b[0;36mDataset._map_single..apply_function_on_filtered_inputs\u001b[0;34m(pa_inputs, indices, check_same_num_examples, offset)\u001b[0m\n\u001b[1;32m 3347\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m with_rank:\n\u001b[1;32m 3348\u001b[0m additional_args \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m (rank,)\n\u001b[0;32m-> 3349\u001b[0m processed_inputs \u001b[38;5;241m=\u001b[39m \u001b[43mfunction\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfn_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43madditional_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfn_kwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3350\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(processed_inputs, LazyDict):\n\u001b[1;32m 3351\u001b[0m processed_inputs \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 3352\u001b[0m k: v \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m processed_inputs\u001b[38;5;241m.\u001b[39mdata\u001b[38;5;241m.\u001b[39mitems() \u001b[38;5;28;01mif\u001b[39;00m k \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m processed_inputs\u001b[38;5;241m.\u001b[39mkeys_to_format\n\u001b[1;32m 3353\u001b[0m }\n", "Cell \u001b[0;32mIn[3], line 6\u001b[0m, in \u001b[0;36m\u001b[0;34m(examples)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;66;03m# Load tokenizer and dataset\u001b[39;00m\n\u001b[1;32m 5\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 6\u001b[0m dataset \u001b[38;5;241m=\u001b[39m dataset\u001b[38;5;241m.\u001b[39mmap(\u001b[38;5;28;01mlambda\u001b[39;00m examples: tokenizer(\u001b[43mexamples\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mcomment_text\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m, truncation\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, padding\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmax_length\u001b[39m\u001b[38;5;124m'\u001b[39m, max_length\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2000\u001b[39m), batched\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Convert dataset to PyTorch tensors\u001b[39;00m\n\u001b[1;32m 9\u001b[0m dataset\u001b[38;5;241m.\u001b[39mset_format(\u001b[38;5;28mtype\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtorch\u001b[39m\u001b[38;5;124m'\u001b[39m, columns\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minput_ids\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mattention_mask\u001b[39m\u001b[38;5;124m'\u001b[39m])\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/datasets/formatting/formatting.py:270\u001b[0m, in \u001b[0;36mLazyDict.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 269\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__getitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, key):\n\u001b[0;32m--> 270\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m]\u001b[49m\n\u001b[1;32m 271\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m key \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mkeys_to_format:\n\u001b[1;32m 272\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mformat(key)\n", "\u001b[0;31mKeyError\u001b[0m: 'comment_text'" ] } ], "source": [ "from transformers import AutoTokenizer\n", "import torch\n", "\n", "# Load tokenizer and dataset\n", "tokenizer = AutoTokenizer.from_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "dataset = dataset.map(lambda examples: tokenizer(examples['comment_text'], truncation=True, padding='max_length', max_length=2000), batched=True)\n", "\n", "# Convert dataset to PyTorch tensors\n", "dataset.set_format(type='torch', columns=['input_ids', 'attention_mask'])\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "5fda5ff3-e84f-47b7-a293-9692c9ac2b20", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['text', 'label']\n" ] } ], "source": [ "print(dataset.column_names)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "4bc343c0-60bb-4e8d-888a-9056dc157ecc", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "48c48ee862ae4e69bba4051abf9f0ee4", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map: 0%| | 0/250 [00:00 11\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m model_name \u001b[38;5;129;01min\u001b[39;00m \u001b[43mmodels_to_test\u001b[49m\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[1;32m 12\u001b[0m model \u001b[38;5;241m=\u001b[39m models_to_test[model_name]\n\u001b[1;32m 13\u001b[0m model\u001b[38;5;241m.\u001b[39mto(device) \u001b[38;5;66;03m# Move the model to the appropriate device\u001b[39;00m\n", "\u001b[0;31mNameError\u001b[0m: name 'models_to_test' is not defined" ] } ], "source": [ "# Assuming your dataset has already been tokenized and mapped\n", "import torch\n", "\n", "# Set the device to use\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "\n", "# Dictionary to store generated texts\n", "output_texts = {}\n", "\n", "# Iterate through the models and generate outputs\n", "for model_name in models_to_test.keys():\n", " model = models_to_test[model_name]\n", " model.to(device) # Move the model to the appropriate device\n", " output_texts[model_name] = []\n", " for i, example in enumerate(dataset):\n", " torch.manual_seed(42)\n", " input_text = example[\"text\"][:2000]\n", " inputs = tokenizer(\n", " f\"Prompt: {input_text}\\nContinuation:\",\n", " padding=True,\n", " return_tensors=\"pt\",\n", " ).to(device)\n", " inputs.input_ids = inputs.input_ids[:2000]\n", " inputs.attention_mask = inputs.attention_mask[:2000]\n", " # Define generation args\n", " generated_texts = model.generate(\n", " **inputs,\n", " max_new_tokens=30,\n", " do_sample=True,\n", " temperature=0.7,\n", " top_k=50,\n", " top_p=0.95,\n", " repetition_penalty=1.2 # Prevents repetition\n", " )\n", " generated_texts = tokenizer.batch_decode(\n", " generated_texts.detach().cpu().numpy(),\n", " skip_special_tokens=True\n", " )\n", " output_texts[model_name].append(generated_texts[0][len(input_text):])\n", " # Free up memory\n", " model = None\n", " torch.cuda.empty_cache()\n", "\n", "# Now you can proceed to compute toxicity scores on the generated texts.\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "9c37a81e-1ee3-4036-84a2-4b294dd59a99", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/utils/generic.py:309: FutureWarning: `torch.utils._pytree._register_pytree_node` is deprecated. Please use `torch.utils._pytree.register_pytree_node` instead.\n", " _torch_pytree._register_pytree_node(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/utils/generic.py:309: FutureWarning: `torch.utils._pytree._register_pytree_node` is deprecated. Please use `torch.utils._pytree.register_pytree_node` instead.\n", " _torch_pytree._register_pytree_node(\n", "/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:519: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n", " return torch.load(checkpoint_file, map_location=map_location)\n", "/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/file_download.py:1150: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n", " warnings.warn(\n", "/opt/app-root/lib64/python3.9/site-packages/bitsandbytes/cextension.py:34: UserWarning: The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.\n", " warn(\"The installed version of bitsandbytes was compiled without GPU support. \"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/bitsandbytes/libbitsandbytes_cpu.so: undefined symbol: cadam32bit_grad_fp32\n" ] }, { "ename": "OSError", "evalue": "Incorrect path_or_model_id: '../models/opt-350m_DETOXIFY_CASUAL_LM'. Please provide either the path to a local folder or the repo_id of a model on the Hub.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:389\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 387\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 388\u001b[0m \u001b[38;5;66;03m# Load from URL or cache if already cached\u001b[39;00m\n\u001b[0;32m--> 389\u001b[0m resolved_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 390\u001b[0m \u001b[43m \u001b[49m\u001b[43mpath_or_repo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 391\u001b[0m \u001b[43m \u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 392\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43msubfolder\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m==\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 393\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 394\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 395\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 396\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 397\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 398\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 399\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 400\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 401\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 402\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m GatedRepoError \u001b[38;5;28;01mas\u001b[39;00m e:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '../models/opt-350m_DETOXIFY_CASUAL_LM'. Use `repo_type` argument if needed.", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 9\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Load the standard and PEFT (detoxified) models\u001b[39;00m\n\u001b[1;32m 8\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[0;32m----> 9\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_model_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Store models in a dictionary for easy comparison later\u001b[39;00m\n\u001b[1;32m 12\u001b[0m models_to_test \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 13\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mStandard Model\u001b[39m\u001b[38;5;124m\"\u001b[39m: model,\n\u001b[1;32m 14\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDetoxified Model\u001b[39m\u001b[38;5;124m\"\u001b[39m: peft_model\n\u001b[1;32m 15\u001b[0m }\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/auto_factory.py:488\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 485\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m commit_hash \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 486\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PretrainedConfig):\n\u001b[1;32m 487\u001b[0m \u001b[38;5;66;03m# We make a call to the config file first (which may be absent) to get the commit hash as soon as possible\u001b[39;00m\n\u001b[0;32m--> 488\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 489\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 490\u001b[0m \u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 491\u001b[0m \u001b[43m \u001b[49m\u001b[43m_raise_exceptions_for_missing_entries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 492\u001b[0m \u001b[43m \u001b[49m\u001b[43m_raise_exceptions_for_connection_errors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 493\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhub_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 494\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 495\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 496\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:454\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 452\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThere was a specific connection error when trying to load \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00merr\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 453\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HFValidationError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m--> 454\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 455\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIncorrect path_or_model_id: \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Please provide either the path to a local folder or the repo_id of a model on the Hub.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 456\u001b[0m ) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[1;32m 457\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m resolved_file\n", "\u001b[0;31mOSError\u001b[0m: Incorrect path_or_model_id: '../models/opt-350m_DETOXIFY_CASUAL_LM'. Please provide either the path to a local folder or the repo_id of a model on the Hub." ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "\n", "# Define your model IDs\n", "model_id = \"../models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "\n", "# Load the standard and PEFT (detoxified) models\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = AutoModelForCausalLM.from_pretrained(peft_model_id)\n", "\n", "# Store models in a dictionary for easy comparison later\n", "models_to_test = {\n", " \"Standard Model\": model,\n", " \"Detoxified Model\": peft_model\n", "}\n", "\n", "# Now proceed with the generation step as before\n", "output_texts = {}\n", "\n", "# Iterate through the models and generate outputs\n", "for model_name in models_to_test.keys():\n", " model = models_to_test[model_name]\n", " model.to(device) # Move the model to the appropriate device\n", " output_texts[model_name] = []\n", " for i, example in enumerate(dataset):\n", " torch.manual_seed(42)\n", " input_text = example[\"text\"][:2000]\n", " inputs = tokenizer(\n", " f\"Prompt: {input_text}\\nContinuation:\",\n", " padding=True,\n", " return_tensors=\"pt\",\n", " ).to(device)\n", " inputs.input_ids = inputs.input_ids[:2000]\n", " inputs.attention_mask = inputs.attention_mask[:2000]\n", " # Define generation args\n", " generated_texts = model.generate(\n", " **inputs,\n", " max_new_tokens=30,\n", " do_sample=True,\n", " temperature=0.7,\n", " top_k=50,\n", " top_p=0.95,\n", " repetition_penalty=1.2 # Prevents repetition\n", " )\n", " generated_texts = tokenizer.batch_decode(\n", " generated_texts.detach().cpu().numpy(),\n", " skip_special_tokens=True\n", " )\n", " output_texts[model_name].append(generated_texts[0][len(input_text):])\n", " # Free up memory\n", " model = None\n", " torch.cuda.empty_cache()\n", "\n", "# Now you can proceed to compute toxicity scores on the generated texts.\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "5efd7172-fedb-4334-bcb1-7494d6987f72", "metadata": { "tags": [] }, "outputs": [ { "ename": "ValueError", "evalue": "Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:184\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 183\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 184\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 185\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 186\u001b[0m \u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 187\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 188\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 189\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '../models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json'. Use `repo_type` argument if needed.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[8], line 9\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Load the adapter configuration and apply it to the model\u001b[39;00m\n\u001b[1;32m 8\u001b[0m peft_config_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 9\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbase_model\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# Now, peft_model should be the model with the LoRA weights applied\u001b[39;00m\n\u001b[1;32m 12\u001b[0m models_to_test \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 13\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbase_model\u001b[39m\u001b[38;5;124m\"\u001b[39m: base_model,\n\u001b[1;32m 14\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpeft_model\u001b[39m\u001b[38;5;124m\"\u001b[39m: peft_model\n\u001b[1;32m 15\u001b[0m }\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:324\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, **kwargs)\u001b[0m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;66;03m# load the config\u001b[39;00m\n\u001b[1;32m 322\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m config \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 323\u001b[0m config \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_CONFIG_MAPPING[\n\u001b[0;32m--> 324\u001b[0m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_peft_type\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 325\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 326\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msubfolder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 327\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrevision\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 328\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcache_dir\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 329\u001b[0m \u001b[43m \u001b[49m\u001b[43muse_auth_token\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muse_auth_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 330\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtoken\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 331\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 332\u001b[0m ]\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 333\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PeftConfig):\n\u001b[1;32m 334\u001b[0m config\u001b[38;5;241m.\u001b[39minference_mode \u001b[38;5;241m=\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m is_trainable\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:190\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 184\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 185\u001b[0m model_id,\n\u001b[1;32m 186\u001b[0m CONFIG_NAME,\n\u001b[1;32m 187\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs,\n\u001b[1;32m 188\u001b[0m )\n\u001b[1;32m 189\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 190\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodel_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 192\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 193\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m loaded_attributes[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpeft_type\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json'" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Load the base model\n", "base_model = AutoModelForCausalLM.from_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "\n", "# Load the adapter configuration and apply it to the model\n", "peft_config_path = \"../models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\"\n", "peft_model = PeftModel.from_pretrained(base_model, peft_config_path)\n", "\n", "# Now, peft_model should be the model with the LoRA weights applied\n", "models_to_test = {\n", " \"base_model\": base_model,\n", " \"peft_model\": peft_model\n", "}\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "37b4fd47-258b-4500-9690-7cf4777fd2d8", "metadata": { "tags": [] }, "outputs": [ { "ename": "ValueError", "evalue": "Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:184\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 183\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 184\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 185\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 186\u001b[0m \u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 187\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 188\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 189\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '../models/opt-350m_DETOXIFY_CASUAL_LM'. Use `repo_type` argument if needed.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[10], line 8\u001b[0m\n\u001b[1;32m 5\u001b[0m base_model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Apply the adapter configuration directly if the config file is missing\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbase_model\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m../models/opt-350m_DETOXIFY_CASUAL_LM\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Now, peft_model should be the model with the LoRA weights applied\u001b[39;00m\n\u001b[1;32m 11\u001b[0m models_to_test \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m 12\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbase_model\u001b[39m\u001b[38;5;124m\"\u001b[39m: base_model,\n\u001b[1;32m 13\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpeft_model\u001b[39m\u001b[38;5;124m\"\u001b[39m: peft_model\n\u001b[1;32m 14\u001b[0m }\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:324\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, **kwargs)\u001b[0m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;66;03m# load the config\u001b[39;00m\n\u001b[1;32m 322\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m config \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 323\u001b[0m config \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_CONFIG_MAPPING[\n\u001b[0;32m--> 324\u001b[0m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_peft_type\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 325\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 326\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msubfolder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 327\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrevision\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 328\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcache_dir\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 329\u001b[0m \u001b[43m \u001b[49m\u001b[43muse_auth_token\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muse_auth_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 330\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtoken\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 331\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 332\u001b[0m ]\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 333\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PeftConfig):\n\u001b[1;32m 334\u001b[0m config\u001b[38;5;241m.\u001b[39minference_mode \u001b[38;5;241m=\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m is_trainable\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:190\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 184\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 185\u001b[0m model_id,\n\u001b[1;32m 186\u001b[0m CONFIG_NAME,\n\u001b[1;32m 187\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs,\n\u001b[1;32m 188\u001b[0m )\n\u001b[1;32m 189\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 190\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmodel_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 192\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 193\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m loaded_attributes[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpeft_type\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM'" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel\n", "\n", "# Load the base model\n", "base_model = AutoModelForCausalLM.from_pretrained(\"../models/opt-350m_CASUAL_LM\")\n", "\n", "# Apply the adapter configuration directly if the config file is missing\n", "peft_model = PeftModel.from_pretrained(base_model, \"../models/opt-350m_DETOXIFY_CASUAL_LM\")\n", "\n", "# Now, peft_model should be the model with the LoRA weights applied\n", "models_to_test = {\n", " \"base_model\": base_model,\n", " \"peft_model\": peft_model\n", "}\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "d9646bad-58d8-4f08-97d3-bac8eb203dcc", "metadata": { "tags": [] }, "outputs": [ { "ename": "ValueError", "evalue": "Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:108\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 109\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\n\u001b[1;32m 110\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '../models/opt-350m_DETOXIFY_CASUAL_LM'. Use `repo_type` argument if needed.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[11], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Load the PEFT model with the correct path\u001b[39;00m\n\u001b[1;32m 4\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 6\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_model_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m model \u001b[38;5;241m=\u001b[39m PeftModel\u001b[38;5;241m.\u001b[39mfrom_pretrained(model, config)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:112\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 109\u001b[0m pretrained_model_name_or_path, CONFIG_NAME, subfolder\u001b[38;5;241m=\u001b[39msubfolder, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs\n\u001b[1;32m 110\u001b[0m )\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 112\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpretrained_model_name_or_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 114\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 116\u001b[0m \u001b[38;5;66;03m# TODO: this hack is needed to fix the following issue (on commit 702f937):\u001b[39;00m\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# if someone saves a default config and loads it back with `PeftConfig` class it yields to\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# not loading the correct config class.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[38;5;66;03m# print(peft_config)\u001b[39;00m\n\u001b[1;32m 129\u001b[0m \u001b[38;5;66;03m# >>> PeftConfig(peft_type='ADALORA', auto_mapping=None, base_model_name_or_path=None, revision=None, task_type=None, inference_mode=False)\u001b[39;00m\n", "\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM'" ] } ], "source": [ "from peft import PeftModel, PeftConfig\n", "\n", "# Load the PEFT model with the correct path\n", "peft_model_id = \"../models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "\n", "config = PeftConfig.from_pretrained(peft_model_id)\n", "model = PeftModel.from_pretrained(model, config)\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "44808f33-e71a-412f-a915-8ff90960c5fc", "metadata": { "tags": [] }, "outputs": [ { "ename": "ValueError", "evalue": "Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:108\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 109\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\n\u001b[1;32m 110\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '../models/opt-350m_DETOXIFY_CASUAL_LM'. Use `repo_type` argument if needed.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 5\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtransformers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AutoModelForCausalLM\n\u001b[1;32m 4\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/opt-350m_DETOXIFY_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 5\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_model_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(config\u001b[38;5;241m.\u001b[39mbase_model_name_or_path)\n\u001b[1;32m 7\u001b[0m model \u001b[38;5;241m=\u001b[39m PeftModel\u001b[38;5;241m.\u001b[39mfrom_pretrained(model, peft_model_id)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:112\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 109\u001b[0m pretrained_model_name_or_path, CONFIG_NAME, subfolder\u001b[38;5;241m=\u001b[39msubfolder, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs\n\u001b[1;32m 110\u001b[0m )\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 112\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpretrained_model_name_or_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 114\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 116\u001b[0m \u001b[38;5;66;03m# TODO: this hack is needed to fix the following issue (on commit 702f937):\u001b[39;00m\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# if someone saves a default config and loads it back with `PeftConfig` class it yields to\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# not loading the correct config class.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[38;5;66;03m# print(peft_config)\u001b[39;00m\n\u001b[1;32m 129\u001b[0m \u001b[38;5;66;03m# >>> PeftConfig(peft_type='ADALORA', auto_mapping=None, base_model_name_or_path=None, revision=None, task_type=None, inference_mode=False)\u001b[39;00m\n", "\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at '../models/opt-350m_DETOXIFY_CASUAL_LM'" ] } ], "source": [ "from peft import PeftConfig, PeftModel\n", "from transformers import AutoModelForCausalLM\n", "\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/notebooks/models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "config = PeftConfig.from_pretrained(peft_model_id)\n", "model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)\n", "model = PeftModel.from_pretrained(model, peft_model_id)\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "49127afe-6ae7-4bec-9960-0d86eb4adc5c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/opt/app-root/src/trustyai-detoxify-sft/notebooks\n" ] } ], "source": [ "import os\n", "print(os.getcwd())\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "cc9ebb48-60b6-48ed-9d90-3b2761b22de6", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/opt/app-root/src/trustyai-detoxify-sft/notebooks/models/opt-350m_DETOXIFY_CASUAL_LM\n" ] } ], "source": [ "model_directory = os.path.join(os.getcwd(), 'models/opt-350m_DETOXIFY_CASUAL_LM')\n", "print(model_directory)\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "3f8c7fe5-ffb9-457b-b056-87559fe7ea64", "metadata": { "tags": [] }, "outputs": [ { "ename": "ValueError", "evalue": "Can't find 'adapter_config.json' at '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:108\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 109\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\n\u001b[1;32m 110\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM'. Use `repo_type` argument if needed.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[17], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Load the PEFT config with the correct path\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_model_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 11\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel\u001b[38;5;241m.\u001b[39mfrom_pretrained(model, peft_model_id)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:112\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 109\u001b[0m pretrained_model_name_or_path, CONFIG_NAME, subfolder\u001b[38;5;241m=\u001b[39msubfolder, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs\n\u001b[1;32m 110\u001b[0m )\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 112\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpretrained_model_name_or_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 114\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 116\u001b[0m \u001b[38;5;66;03m# TODO: this hack is needed to fix the following issue (on commit 702f937):\u001b[39;00m\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# if someone saves a default config and loads it back with `PeftConfig` class it yields to\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# not loading the correct config class.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[38;5;66;03m# print(peft_config)\u001b[39;00m\n\u001b[1;32m 129\u001b[0m \u001b[38;5;66;03m# >>> PeftConfig(peft_type='ADALORA', auto_mapping=None, base_model_name_or_path=None, revision=None, task_type=None, inference_mode=False)\u001b[39;00m\n", "\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM'" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full paths to your models\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "\n", "# Load the PEFT config with the correct path\n", "config = PeftConfig.from_pretrained(peft_model_id)\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = PeftModel.from_pretrained(model, peft_model_id)\n", "\n", "# Now the models should load correctly with the full paths provided\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "faf92802-d786-4b2e-bcbc-2b64d8efa97c", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", "To disable this warning, you can either:\n", "\t- Avoid using `tokenizers` before the fork if possible\n", "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: transformers in /opt/app-root/lib/python3.9/site-packages (4.36.2)\n", "Collecting transformers\n", " Downloading transformers-4.44.0-py3-none-any.whl.metadata (43 kB)\n", "Requirement already satisfied: peft in /opt/app-root/lib/python3.9/site-packages (0.7.1)\n", "Collecting peft\n", " Downloading peft-0.12.0-py3-none-any.whl.metadata (13 kB)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from transformers) (3.14.0)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.23.2 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.24.5)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from transformers) (1.24.4)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from transformers) (24.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /opt/app-root/lib/python3.9/site-packages (from transformers) (6.0.1)\n", "Requirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers) (2024.7.24)\n", "Requirement already satisfied: requests in /opt/app-root/lib/python3.9/site-packages (from transformers) (2.32.2)\n", "Requirement already satisfied: safetensors>=0.4.1 in /opt/app-root/lib/python3.9/site-packages (from transformers) (0.4.4)\n", "Collecting tokenizers<0.20,>=0.19 (from transformers)\n", " Downloading tokenizers-0.19.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.7 kB)\n", "Requirement already satisfied: tqdm>=4.27 in /opt/app-root/lib/python3.9/site-packages (from transformers) (4.66.4)\n", "Requirement already satisfied: psutil in /opt/app-root/lib/python3.9/site-packages (from peft) (5.9.8)\n", "Requirement already satisfied: torch>=1.13.0 in /opt/app-root/lib/python3.9/site-packages (from peft) (2.4.0)\n", "Requirement already satisfied: accelerate>=0.21.0 in /opt/app-root/lib/python3.9/site-packages (from peft) (0.26.1)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (2023.10.0)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.23.2->transformers) (4.11.0)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (1.13.2)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (3.2.1)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (3.1.4)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (12.1.105)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (9.1.0.70)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (11.0.2.54)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (11.4.5.107)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (12.1.0.106)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (2.20.5)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (12.1.105)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (3.0.0)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.13.0->peft) (12.6.20)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (3.7)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (1.26.18)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->transformers) (2024.2.2)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=1.13.0->peft) (2.1.5)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch>=1.13.0->peft) (1.3.0)\n", "Downloading transformers-4.44.0-py3-none-any.whl (9.5 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m9.5/9.5 MB\u001b[0m \u001b[31m48.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hDownloading peft-0.12.0-py3-none-any.whl (296 kB)\n", "Downloading tokenizers-0.19.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.6/3.6 MB\u001b[0m \u001b[31m83.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: tokenizers, transformers, peft\n", " Attempting uninstall: tokenizers\n", " Found existing installation: tokenizers 0.15.2\n", " Uninstalling tokenizers-0.15.2:\n", " Successfully uninstalled tokenizers-0.15.2\n", " Attempting uninstall: transformers\n", " Found existing installation: transformers 4.36.2\n", " Uninstalling transformers-4.36.2:\n", " Successfully uninstalled transformers-4.36.2\n", " Attempting uninstall: peft\n", " Found existing installation: peft 0.7.1\n", " Uninstalling peft-0.7.1:\n", " Successfully uninstalled peft-0.7.1\n", "Successfully installed peft-0.12.0 tokenizers-0.19.1 transformers-4.44.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install --upgrade transformers peft\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "95d97cdc-2c7e-4106-878e-855c09618daa", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "Can't find 'adapter_config.json' at '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:108\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 109\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhf_hub_download_kwargs\u001b[49m\n\u001b[1;32m 110\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM'. Use `repo_type` argument if needed.", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[18], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Load the PEFT config with the correct path\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_model_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 11\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel\u001b[38;5;241m.\u001b[39mfrom_pretrained(model, peft_model_id)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:112\u001b[0m, in \u001b[0;36mPeftConfigMixin.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, subfolder, **kwargs)\u001b[0m\n\u001b[1;32m 108\u001b[0m config_file \u001b[38;5;241m=\u001b[39m hf_hub_download(\n\u001b[1;32m 109\u001b[0m pretrained_model_name_or_path, CONFIG_NAME, subfolder\u001b[38;5;241m=\u001b[39msubfolder, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhf_hub_download_kwargs\n\u001b[1;32m 110\u001b[0m )\n\u001b[1;32m 111\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m--> 112\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCan\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt find \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mCONFIG_NAME\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m at \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpretrained_model_name_or_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 114\u001b[0m loaded_attributes \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39mfrom_json_file(config_file)\n\u001b[1;32m 116\u001b[0m \u001b[38;5;66;03m# TODO: this hack is needed to fix the following issue (on commit 702f937):\u001b[39;00m\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# if someone saves a default config and loads it back with `PeftConfig` class it yields to\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# not loading the correct config class.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[38;5;66;03m# print(peft_config)\u001b[39;00m\n\u001b[1;32m 129\u001b[0m \u001b[38;5;66;03m# >>> PeftConfig(peft_type='ADALORA', auto_mapping=None, base_model_name_or_path=None, revision=None, task_type=None, inference_mode=False)\u001b[39;00m\n", "\u001b[0;31mValueError\u001b[0m: Can't find 'adapter_config.json' at '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM'" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full paths to your models\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "\n", "# Load the PEFT config with the correct path\n", "config = PeftConfig.from_pretrained(peft_model_id)\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "peft_model = PeftModel.from_pretrained(model, peft_model_id)\n", "\n", "print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 19, "id": "bde87b27-f1a2-4a6f-8a08-3f3adaff558e", "metadata": { "tags": [] }, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[19], line 7\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# Manually load the adapter configuration\u001b[39;00m\n\u001b[1;32m 6\u001b[0m peft_model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mpeft_model_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m 8\u001b[0m config \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mload(f)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Now load the main model\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/IPython/core/interactiveshell.py:310\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 303\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m 304\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 305\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 306\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 307\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 308\u001b[0m )\n\u001b[0;32m--> 310\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json'" ] } ], "source": [ "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel\n", "\n", "# Manually load the adapter configuration\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\"\n", "with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", "# Now load the main model\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", "# Load the PEFT model using the config\n", "peft_model = PeftModel(model, config)\n", "\n", "print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "2b2b47e8-e122-4b8a-865b-604fa237b5eb", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File not found.\n" ] } ], "source": [ "import os\n", "\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\"\n", "\n", "# Check if the file exists\n", "if os.path.exists(peft_model_id):\n", " print(\"File found!\")\n", "else:\n", " print(\"File not found.\")\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "a06c4e51-eaea-4647-9fbc-117fc8038502", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Directory not found.\n" ] } ], "source": [ "import os\n", "\n", "# Directory to list\n", "directory = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "\n", "# List contents\n", "if os.path.exists(directory):\n", " print(\"Directory found. Listing contents:\")\n", " print(os.listdir(directory))\n", "else:\n", " print(\"Directory not found.\")\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "a4c12f47-bbbb-4a16-8262-70e005fdb04f", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Current working directory: /opt/app-root/src/trustyai-detoxify-sft/notebooks\n", "Full models directory path: /opt/app-root/src/trustyai-detoxify-sft/notebooks/models/opt-350m_DETOXIFY_CASUAL_LM\n", "Directory not found.\n" ] } ], "source": [ "import os\n", "\n", "# Check current working directory\n", "current_directory = os.getcwd()\n", "print(f\"Current working directory: {current_directory}\")\n", "\n", "# Start building the path from the current directory\n", "models_directory = os.path.join(current_directory, \"models\", \"opt-350m_DETOXIFY_CASUAL_LM\")\n", "print(f\"Full models directory path: {models_directory}\")\n", "\n", "# Check if this directory exists\n", "if os.path.exists(models_directory):\n", " print(\"Directory found. Listing contents:\")\n", " print(os.listdir(models_directory))\n", "else:\n", " print(\"Directory not found.\")\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "5a2ca274-a9c6-4ed0-82d0-160edac41324", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parent directory path: /opt/app-root/src/trustyai-detoxify-sft\n", "Parent directory found. Listing contents:\n", "['models', '.gitignore', '.ipynb_checkpoints', 'manifests', 'README.md', 'notebooks', 'slides', 'instructions.md', '.git']\n" ] } ], "source": [ "import os\n", "\n", "# Go one directory up from the current working directory\n", "parent_directory = os.path.dirname(current_directory)\n", "print(f\"Parent directory path: {parent_directory}\")\n", "\n", "# List contents of the parent directory\n", "if os.path.exists(parent_directory):\n", " print(\"Parent directory found. Listing contents:\")\n", " print(os.listdir(parent_directory))\n", "else:\n", " print(\"Parent directory not found.\")\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "6e58fca8-94c7-4526-996a-9e3cc9ab41e0", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Directory 'models' found at: /opt/app-root/src/trustyai-detoxify-sft/models\n" ] } ], "source": [ "import os\n", "\n", "def find_directory(directory_name, search_path):\n", " for root, dirs, files in os.walk(search_path):\n", " if directory_name in dirs:\n", " print(f\"Directory '{directory_name}' found at: {os.path.join(root, directory_name)}\")\n", " return os.path.join(root, directory_name)\n", " print(f\"Directory '{directory_name}' not found.\")\n", " return None\n", "\n", "# Start searching from the current working directory upwards\n", "found_directory = find_directory(\"models\", parent_directory)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4d37fb8e-a5f6-44b0-8707-e6bd712c7700", "metadata": { "tags": [] }, "outputs": [], "source": [ "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel\n", "\n", "# Manually load the adapter configuration\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists (sanity check)\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", " \n", " # Load the PEFT model using the config\n", " peft_model = PeftModel(model, config)\n", " \n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "8e38fa43-6534-46a3-b1b6-6aaaffddb854", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Listing contents of models directory:\n", "['opt-350m_DETOXIFY_CAUSAL_LM', 'opt-350m_CASUAL_LM']\n" ] } ], "source": [ "import os\n", "\n", "models_directory = \"/opt/app-root/src/trustyai-detoxify-sft/models\"\n", "print(\"Listing contents of models directory:\")\n", "print(os.listdir(models_directory))\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "5c030f81-0280-4521-84cd-d2ce343f349a", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] } ], "source": [ "import os\n", "\n", "models_directory = \"/opt/app-root/src/trustyai-detoxify-sft/models\"\n", "\n", "def find_file(filename, search_path):\n", " for root, dirs, files in os.walk(search_path):\n", " if filename in files:\n", " print(f\"File found: {os.path.join(root, filename)}\")\n", " return os.path.join(root, filename)\n", " print(f\"File '{filename}' not found.\")\n", " return None\n", "\n", "# Search for the adapter_config.json file\n", "file_path = find_file(\"adapter_config.json\", models_directory)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7001631c-cc48-4b9d-b17e-54c34aea5971", "metadata": { "tags": [] }, "outputs": [], "source": [ "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel\n", "\n", "# Manually load the adapter configuration\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists (sanity check)\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", " \n", " # Load the PEFT model using the config\n", " peft_model = PeftModel(model, config)\n", " \n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c698163b-4362-48c1-8b2c-30a19101e3a7", "metadata": { "tags": [] }, "outputs": [], "source": [ "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel\n", "\n", "# Manually load the adapter configuration\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists (sanity check)\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", " \n", " # Load the PEFT model using the config\n", " peft_model = PeftModel(model, config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "5fb2e124-6e0f-437d-8c99-1571dec49a21", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'model_id' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[43mmodel_id\u001b[49m)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Prepare an input prompt\u001b[39;00m\n\u001b[1;32m 4\u001b[0m input_text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe impact of air quality on health is significant because\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mNameError\u001b[0m: name 'model_id' is not defined" ] } ], "source": [ "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "\n", "# Prepare an input prompt\n", "input_text = \"The impact of air quality on health is significant because\"\n", "\n", "# Tokenize and generate text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(f\"Generated text: {generated_text}\")\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "0c1019a1-d3b0-428b-9975-497e55fa6330", "metadata": { "tags": [] }, "outputs": [], "source": [ "from transformers import AutoTokenizer\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "23a932dd-290d-441a-aa05-b3a6bc98bccf", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'model_id' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[43mmodel_id\u001b[49m)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Prepare an input prompt\u001b[39;00m\n\u001b[1;32m 4\u001b[0m input_text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe impact of air quality on health is significant because\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mNameError\u001b[0m: name 'model_id' is not defined" ] } ], "source": [ "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "\n", "# Prepare an input prompt\n", "input_text = \"The impact of air quality on health is significant because\"\n", "\n", "# Tokenize and generate text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(f\"Generated text: {generated_text}\")\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "751599db-f6a2-4f97-8109-b8520a0363c4", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'model_id' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtransformers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m AutoTokenizer\n\u001b[0;32m----> 3\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[43mmodel_id\u001b[49m)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# Prepare an input prompt\u001b[39;00m\n\u001b[1;32m 6\u001b[0m input_text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe impact of air quality on health is significant because\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mNameError\u001b[0m: name 'model_id' is not defined" ] } ], "source": [ "from transformers import AutoTokenizer\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "\n", "# Prepare an input prompt\n", "input_text = \"The impact of air quality on health is significant because\"\n", "\n", "# Tokenize and generate text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(f\"Generated text: {generated_text}\")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "ab99b42b-df62-4740-99ae-b07163e24ec3", "metadata": { "tags": [] }, "outputs": [ { "ename": "OSError", "evalue": "/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co//opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/tree/None' for available files.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Load the model and tokenizer\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m \u001b[43mAutoTokenizer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 12\u001b[0m \u001b[38;5;66;03m# Assuming you have already loaded the PEFT model:\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/tokenization_auto.py:854\u001b[0m, in \u001b[0;36mAutoTokenizer.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *inputs, **kwargs)\u001b[0m\n\u001b[1;32m 852\u001b[0m config \u001b[38;5;241m=\u001b[39m AutoConfig\u001b[38;5;241m.\u001b[39mfor_model(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mconfig_dict)\n\u001b[1;32m 853\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 854\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mAutoConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 855\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrust_remote_code\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrust_remote_code\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 856\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 857\u001b[0m config_tokenizer_class \u001b[38;5;241m=\u001b[39m config\u001b[38;5;241m.\u001b[39mtokenizer_class\n\u001b[1;32m 858\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoTokenizer\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config\u001b[38;5;241m.\u001b[39mauto_map:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/configuration_auto.py:976\u001b[0m, in \u001b[0;36mAutoConfig.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 973\u001b[0m trust_remote_code \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrust_remote_code\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 974\u001b[0m code_revision \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcode_revision\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 976\u001b[0m config_dict, unused_kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mPretrainedConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 977\u001b[0m has_remote_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoConfig\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 978\u001b[0m has_local_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01min\u001b[39;00m CONFIG_MAPPING\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:632\u001b[0m, in \u001b[0;36mPretrainedConfig.get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 630\u001b[0m original_kwargs \u001b[38;5;241m=\u001b[39m copy\u001b[38;5;241m.\u001b[39mdeepcopy(kwargs)\n\u001b[1;32m 631\u001b[0m \u001b[38;5;66;03m# Get config dict associated with the base config file\u001b[39;00m\n\u001b[0;32m--> 632\u001b[0m config_dict, kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 633\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict:\n\u001b[1;32m 634\u001b[0m original_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:689\u001b[0m, in \u001b[0;36mPretrainedConfig._get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 685\u001b[0m configuration_file \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_configuration_file\u001b[39m\u001b[38;5;124m\"\u001b[39m, CONFIG_NAME) \u001b[38;5;28;01mif\u001b[39;00m gguf_file \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m gguf_file\n\u001b[1;32m 687\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 688\u001b[0m \u001b[38;5;66;03m# Load from local folder or from cache or download from model Hub and cache\u001b[39;00m\n\u001b[0;32m--> 689\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 690\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 691\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfiguration_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 692\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 693\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 694\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 695\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 696\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 697\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 698\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 699\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 700\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 701\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 702\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 703\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 704\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m:\n\u001b[1;32m 705\u001b[0m \u001b[38;5;66;03m# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to\u001b[39;00m\n\u001b[1;32m 706\u001b[0m \u001b[38;5;66;03m# the original exception.\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:373\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 371\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(resolved_file):\n\u001b[1;32m 372\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _raise_exceptions_for_missing_entries:\n\u001b[0;32m--> 373\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 374\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m does not appear to have a file named \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfull_filename\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. Checkout \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 375\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://huggingface.co/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/tree/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrevision\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m for available files.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 376\u001b[0m )\n\u001b[1;32m 377\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "\u001b[0;31mOSError\u001b[0m: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co//opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/tree/None' for available files." ] } ], "source": [ "from transformers import AutoTokenizer\n", "from transformers import AutoModelForCausalLM\n", "from peft import PeftModel\n", "\n", "# Make sure these paths are correct\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the model and tokenizer\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", "# Assuming you have already loaded the PEFT model:\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "peft_model = PeftModel.from_pretrained(model, peft_model_id)\n", "\n", "# Prepare an input prompt\n", "input_text = \"The impact of air quality on health is significant because\"\n", "\n", "# Tokenize and generate text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(f\"Generated text: {generated_text}\")\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "ad5a1f9b-926c-4e90-ac46-5c27aeb6032c", "metadata": { "tags": [] }, "outputs": [ { "ename": "OSError", "evalue": "/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co//opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/tree/None' for available files.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[8], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m model_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;66;03m# Load the model and tokenizer\u001b[39;00m\n\u001b[0;32m----> 9\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m \u001b[43mAutoTokenizer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 12\u001b[0m \u001b[38;5;66;03m# Assuming you have already loaded the PEFT model:\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/tokenization_auto.py:854\u001b[0m, in \u001b[0;36mAutoTokenizer.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *inputs, **kwargs)\u001b[0m\n\u001b[1;32m 852\u001b[0m config \u001b[38;5;241m=\u001b[39m AutoConfig\u001b[38;5;241m.\u001b[39mfor_model(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mconfig_dict)\n\u001b[1;32m 853\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 854\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mAutoConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 855\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrust_remote_code\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrust_remote_code\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 856\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 857\u001b[0m config_tokenizer_class \u001b[38;5;241m=\u001b[39m config\u001b[38;5;241m.\u001b[39mtokenizer_class\n\u001b[1;32m 858\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoTokenizer\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config\u001b[38;5;241m.\u001b[39mauto_map:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/configuration_auto.py:976\u001b[0m, in \u001b[0;36mAutoConfig.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 973\u001b[0m trust_remote_code \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrust_remote_code\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 974\u001b[0m code_revision \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcode_revision\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 976\u001b[0m config_dict, unused_kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mPretrainedConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 977\u001b[0m has_remote_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoConfig\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 978\u001b[0m has_local_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01min\u001b[39;00m CONFIG_MAPPING\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:632\u001b[0m, in \u001b[0;36mPretrainedConfig.get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 630\u001b[0m original_kwargs \u001b[38;5;241m=\u001b[39m copy\u001b[38;5;241m.\u001b[39mdeepcopy(kwargs)\n\u001b[1;32m 631\u001b[0m \u001b[38;5;66;03m# Get config dict associated with the base config file\u001b[39;00m\n\u001b[0;32m--> 632\u001b[0m config_dict, kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 633\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict:\n\u001b[1;32m 634\u001b[0m original_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:689\u001b[0m, in \u001b[0;36mPretrainedConfig._get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 685\u001b[0m configuration_file \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_configuration_file\u001b[39m\u001b[38;5;124m\"\u001b[39m, CONFIG_NAME) \u001b[38;5;28;01mif\u001b[39;00m gguf_file \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m gguf_file\n\u001b[1;32m 687\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 688\u001b[0m \u001b[38;5;66;03m# Load from local folder or from cache or download from model Hub and cache\u001b[39;00m\n\u001b[0;32m--> 689\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 690\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 691\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfiguration_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 692\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 693\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 694\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 695\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 696\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 697\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 698\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 699\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 700\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 701\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 702\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 703\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 704\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m:\n\u001b[1;32m 705\u001b[0m \u001b[38;5;66;03m# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to\u001b[39;00m\n\u001b[1;32m 706\u001b[0m \u001b[38;5;66;03m# the original exception.\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:373\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 371\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(resolved_file):\n\u001b[1;32m 372\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _raise_exceptions_for_missing_entries:\n\u001b[0;32m--> 373\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 374\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m does not appear to have a file named \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfull_filename\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. Checkout \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 375\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://huggingface.co/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/tree/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrevision\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m for available files.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 376\u001b[0m )\n\u001b[1;32m 377\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "\u001b[0;31mOSError\u001b[0m: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co//opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/tree/None' for available files." ] } ], "source": [ "from transformers import AutoTokenizer\n", "from transformers import AutoModelForCausalLM\n", "from peft import PeftModel\n", "\n", "# Make sure these paths are correct\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "\n", "# Load the model and tokenizer\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", "# Assuming you have already loaded the PEFT model:\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\"\n", "peft_model = PeftModel.from_pretrained(model, peft_model_id)\n", "\n", "# Prepare an input prompt\n", "input_text = \"The impact of air quality on health is significant because\"\n", "\n", "# Tokenize and generate text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(f\"Generated text: {generated_text}\")\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "a830accc-bf91-4b79-af47-6353c7e55bd5", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Peft model path: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\n", "Main model path: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\n", "File not found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\n" ] } ], "source": [ "import os\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Define the model paths\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Print paths to verify\n", "print(f\"Peft model path: {peft_model_id}\")\n", "print(f\"Main model path: {model_id}\")\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " # Load the config\n", " config = PeftConfig.from_pretrained(peft_model_id)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", " \n", " # Load the PEFT model using the config\n", " peft_model = PeftModel.from_pretrained(model, config)\n", " \n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "964b19b1-366a-4815-a051-27befc8641b9", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File not found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\n" ] } ], "source": [ "import os\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CASUAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the config\n", " peft_model = PeftModel.from_pretrained(model, config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "c6292e4c-8e18-465a-ac6f-9ebc5a9ace64", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "NameError", "evalue": "name 'json' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 17\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;66;03m# Load the PEFT config manually\u001b[39;00m\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(peft_model_id, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[0;32m---> 17\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mjson\u001b[49m\u001b[38;5;241m.\u001b[39mload(f)\n\u001b[1;32m 19\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[1;32m 20\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n", "\u001b[0;31mNameError\u001b[0m: name 'json' is not defined" ] } ], "source": [ "import os\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the config\n", " peft_model = PeftModel.from_pretrained(model, config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "5fecf52c-f386-4104-816b-f00315712c3d", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "expected str, bytes or os.PathLike object, not dict", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[14], line 24\u001b[0m\n\u001b[1;32m 21\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 23\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the config\u001b[39;00m\n\u001b[0;32m---> 24\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:453\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, **kwargs)\u001b[0m\n\u001b[1;32m 450\u001b[0m \u001b[38;5;66;03m# load the config\u001b[39;00m\n\u001b[1;32m 451\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m config \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 452\u001b[0m config \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_CONFIG_MAPPING[\n\u001b[0;32m--> 453\u001b[0m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_peft_type\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 454\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 455\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msubfolder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 456\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrevision\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 457\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcache_dir\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 458\u001b[0m \u001b[43m \u001b[49m\u001b[43muse_auth_token\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muse_auth_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 459\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtoken\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 460\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 461\u001b[0m ]\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 462\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PeftConfig):\n\u001b[1;32m 463\u001b[0m config\u001b[38;5;241m.\u001b[39minference_mode \u001b[38;5;241m=\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m is_trainable\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:201\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 197\u001b[0m subfolder \u001b[38;5;241m=\u001b[39m hf_hub_download_kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msubfolder\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 199\u001b[0m path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(model_id, subfolder) \u001b[38;5;28;01mif\u001b[39;00m subfolder \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m model_id\n\u001b[0;32m--> 201\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpath\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m)\u001b[49m):\n\u001b[1;32m 202\u001b[0m config_file \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(path, CONFIG_NAME)\n\u001b[1;32m 203\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", "File \u001b[0;32m/usr/lib64/python3.9/posixpath.py:76\u001b[0m, in \u001b[0;36mjoin\u001b[0;34m(a, *p)\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mjoin\u001b[39m(a, \u001b[38;5;241m*\u001b[39mp):\n\u001b[1;32m 72\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Join two or more pathname components, inserting '/' as needed.\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;124;03m If any component is an absolute path, all previous path components\u001b[39;00m\n\u001b[1;32m 74\u001b[0m \u001b[38;5;124;03m will be discarded. An empty last part will result in a path that\u001b[39;00m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;124;03m ends with a separator.\"\"\"\u001b[39;00m\n\u001b[0;32m---> 76\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfspath\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 77\u001b[0m sep \u001b[38;5;241m=\u001b[39m _get_sep(a)\n\u001b[1;32m 78\u001b[0m path \u001b[38;5;241m=\u001b[39m a\n", "\u001b[0;31mTypeError\u001b[0m: expected str, bytes or os.PathLike object, not dict" ] } ], "source": [ "import os\n", "import json # Add this import statement\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the config\n", " peft_model = PeftModel.from_pretrained(model, config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "be5a6d36-f898-44f1-a547-68b720f8c554", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "__init__() got an unexpected keyword argument 'alpha_pattern'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[16], line 24\u001b[0m\n\u001b[1;32m 21\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 23\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 24\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel\u001b[38;5;241m.\u001b[39mfrom_pretrained(model, config\u001b[38;5;241m=\u001b[39m\u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'alpha_pattern'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=PeftConfig(**config))\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "8e6899db-e033-4d6d-b05f-55ddfa26f901", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[18], line 28\u001b[0m\n\u001b[1;32m 25\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfiltered_config\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=PeftConfig(**filtered_config))\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "1caf3306-c84b-45f7-bcaa-3c086127c9ca", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[20], line 28\u001b[0m\n\u001b[1;32m 25\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfiltered_config\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model_id, config=PeftConfig(**filtered_config))\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "6c345b5a-ae86-4913-89ff-e00bc96a8272", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[21], line 28\u001b[0m\n\u001b[1;32m 25\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfiltered_config\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model_id, config=PeftConfig(**filtered_config))\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "8e881bd2-246d-4329-bb7a-f610892573b5", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "expected str, bytes or os.PathLike object, not PeftConfig", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[22], line 28\u001b[0m\n\u001b[1;32m 25\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfiltered_config\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:453\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, **kwargs)\u001b[0m\n\u001b[1;32m 450\u001b[0m \u001b[38;5;66;03m# load the config\u001b[39;00m\n\u001b[1;32m 451\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m config \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 452\u001b[0m config \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_CONFIG_MAPPING[\n\u001b[0;32m--> 453\u001b[0m \u001b[43mPeftConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_peft_type\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 454\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 455\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msubfolder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 456\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrevision\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 457\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcache_dir\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 458\u001b[0m \u001b[43m \u001b[49m\u001b[43muse_auth_token\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43muse_auth_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 459\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtoken\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 460\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 461\u001b[0m ]\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 462\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PeftConfig):\n\u001b[1;32m 463\u001b[0m config\u001b[38;5;241m.\u001b[39minference_mode \u001b[38;5;241m=\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m is_trainable\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/config.py:201\u001b[0m, in \u001b[0;36mPeftConfigMixin._get_peft_type\u001b[0;34m(cls, model_id, **hf_hub_download_kwargs)\u001b[0m\n\u001b[1;32m 197\u001b[0m subfolder \u001b[38;5;241m=\u001b[39m hf_hub_download_kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msubfolder\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 199\u001b[0m path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(model_id, subfolder) \u001b[38;5;28;01mif\u001b[39;00m subfolder \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m model_id\n\u001b[0;32m--> 201\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpath\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m)\u001b[49m):\n\u001b[1;32m 202\u001b[0m config_file \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(path, CONFIG_NAME)\n\u001b[1;32m 203\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", "File \u001b[0;32m/usr/lib64/python3.9/posixpath.py:76\u001b[0m, in \u001b[0;36mjoin\u001b[0;34m(a, *p)\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mjoin\u001b[39m(a, \u001b[38;5;241m*\u001b[39mp):\n\u001b[1;32m 72\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Join two or more pathname components, inserting '/' as needed.\u001b[39;00m\n\u001b[1;32m 73\u001b[0m \u001b[38;5;124;03m If any component is an absolute path, all previous path components\u001b[39;00m\n\u001b[1;32m 74\u001b[0m \u001b[38;5;124;03m will be discarded. An empty last part will result in a path that\u001b[39;00m\n\u001b[1;32m 75\u001b[0m \u001b[38;5;124;03m ends with a separator.\"\"\"\u001b[39;00m\n\u001b[0;32m---> 76\u001b[0m a \u001b[38;5;241m=\u001b[39m \u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfspath\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 77\u001b[0m sep \u001b[38;5;241m=\u001b[39m _get_sep(a)\n\u001b[1;32m 78\u001b[0m path \u001b[38;5;241m=\u001b[39m a\n", "\u001b[0;31mTypeError\u001b[0m: expected str, bytes or os.PathLike object, not PeftConfig" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model_id, PeftConfig(**filtered_config))\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 23, "id": "359b9555-6321-45d2-b7b4-1c022ee8f455", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[23], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Initialize the PeftConfig with filtered config\n", " peft_config = PeftConfig(**filtered_config)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 24, "id": "d1a22f6a-12cb-4225-bacd-1c2e2489a6b2", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "AttributeError", "evalue": "'PeftConfig' object has no attribute 'target_modules'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[24], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:541\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, **kwargs)\u001b[0m\n\u001b[1;32m 539\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m(model, config, adapter_name, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype)\n\u001b[1;32m 540\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 541\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mMODEL_TYPE_TO_PEFT_MODEL_MAPPING\u001b[49m\u001b[43m[\u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtask_type\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 542\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mautocast_adapter_dtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mautocast_adapter_dtype\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 545\u001b[0m model\u001b[38;5;241m.\u001b[39mload_adapter(\n\u001b[1;32m 546\u001b[0m model_id, adapter_name, is_trainable\u001b[38;5;241m=\u001b[39mis_trainable, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 547\u001b[0m )\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:1542\u001b[0m, in \u001b[0;36mPeftModelForCausalLM.__init__\u001b[0;34m(self, model, peft_config, adapter_name, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 1540\u001b[0m \u001b[38;5;28mself\u001b[39m, model: torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mModule, peft_config: PeftConfig, adapter_name: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdefault\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 1541\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1542\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1543\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model_prepare_inputs_for_generation \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model\u001b[38;5;241m.\u001b[39mprepare_inputs_for_generation\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:155\u001b[0m, in \u001b[0;36mPeftModel.__init__\u001b[0;34m(self, model, peft_config, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_MODEL_MAPPING[peft_config\u001b[38;5;241m.\u001b[39mpeft_type]\n\u001b[0;32m--> 155\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mset_additional_trainable_modules(peft_config, adapter_name)\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_cast_adapter_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:139\u001b[0m, in \u001b[0;36mLoraModel.__init__\u001b[0;34m(self, model, config, adapter_name)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, model, config, adapter_name) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 139\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:175\u001b[0m, in \u001b[0;36mBaseTuner.__init__\u001b[0;34m(self, model, peft_config, adapter_name)\u001b[0m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pre_injection_hook(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config[adapter_name], adapter_name)\n\u001b[1;32m 174\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m peft_config \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA \u001b[38;5;129;01mor\u001b[39;00m peft_config[adapter_name] \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA:\n\u001b[0;32m--> 175\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minject_adapter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;66;03m# Copy the peft_config in the injected model.\u001b[39;00m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mpeft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:394\u001b[0m, in \u001b[0;36mBaseTuner.inject_adapter\u001b[0;34m(self, model, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(model_config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_dict\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 392\u001b[0m model_config \u001b[38;5;241m=\u001b[39m model_config\u001b[38;5;241m.\u001b[39mto_dict()\n\u001b[0;32m--> 394\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_prepare_adapter_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 396\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_model(peft_config, model)\n\u001b[1;32m 397\u001b[0m is_target_modules_in_base_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:457\u001b[0m, in \u001b[0;36mLoraModel._prepare_adapter_config\u001b[0;34m(peft_config, model_config)\u001b[0m\n\u001b[1;32m 455\u001b[0m \u001b[38;5;129m@staticmethod\u001b[39m\n\u001b[1;32m 456\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_prepare_adapter_config\u001b[39m(peft_config, model_config):\n\u001b[0;32m--> 457\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mpeft_config\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtarget_modules\u001b[49m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 458\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model_config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING:\n\u001b[1;32m 459\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease specify `target_modules` in `peft_config`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'PeftConfig' object has no attribute 'target_modules'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Initialize the PeftConfig with filtered config\n", " peft_config = PeftConfig(**filtered_config)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config, model_id=model_id)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 25, "id": "c3c5e574-66d7-40fb-af56-22b76e05cd88", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "AttributeError", "evalue": "'PeftConfig' object has no attribute 'target_modules'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[25], line 35\u001b[0m\n\u001b[1;32m 32\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 34\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 35\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:541\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, **kwargs)\u001b[0m\n\u001b[1;32m 539\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m(model, config, adapter_name, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype)\n\u001b[1;32m 540\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 541\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mMODEL_TYPE_TO_PEFT_MODEL_MAPPING\u001b[49m\u001b[43m[\u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtask_type\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 542\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mautocast_adapter_dtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mautocast_adapter_dtype\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 545\u001b[0m model\u001b[38;5;241m.\u001b[39mload_adapter(\n\u001b[1;32m 546\u001b[0m model_id, adapter_name, is_trainable\u001b[38;5;241m=\u001b[39mis_trainable, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 547\u001b[0m )\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:1542\u001b[0m, in \u001b[0;36mPeftModelForCausalLM.__init__\u001b[0;34m(self, model, peft_config, adapter_name, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 1540\u001b[0m \u001b[38;5;28mself\u001b[39m, model: torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mModule, peft_config: PeftConfig, adapter_name: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdefault\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 1541\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1542\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1543\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model_prepare_inputs_for_generation \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model\u001b[38;5;241m.\u001b[39mprepare_inputs_for_generation\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:155\u001b[0m, in \u001b[0;36mPeftModel.__init__\u001b[0;34m(self, model, peft_config, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_MODEL_MAPPING[peft_config\u001b[38;5;241m.\u001b[39mpeft_type]\n\u001b[0;32m--> 155\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mset_additional_trainable_modules(peft_config, adapter_name)\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_cast_adapter_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:139\u001b[0m, in \u001b[0;36mLoraModel.__init__\u001b[0;34m(self, model, config, adapter_name)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, model, config, adapter_name) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 139\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:175\u001b[0m, in \u001b[0;36mBaseTuner.__init__\u001b[0;34m(self, model, peft_config, adapter_name)\u001b[0m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pre_injection_hook(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config[adapter_name], adapter_name)\n\u001b[1;32m 174\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m peft_config \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA \u001b[38;5;129;01mor\u001b[39;00m peft_config[adapter_name] \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA:\n\u001b[0;32m--> 175\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minject_adapter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;66;03m# Copy the peft_config in the injected model.\u001b[39;00m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mpeft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:394\u001b[0m, in \u001b[0;36mBaseTuner.inject_adapter\u001b[0;34m(self, model, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(model_config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_dict\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 392\u001b[0m model_config \u001b[38;5;241m=\u001b[39m model_config\u001b[38;5;241m.\u001b[39mto_dict()\n\u001b[0;32m--> 394\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_prepare_adapter_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 396\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_model(peft_config, model)\n\u001b[1;32m 397\u001b[0m is_target_modules_in_base_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:457\u001b[0m, in \u001b[0;36mLoraModel._prepare_adapter_config\u001b[0;34m(peft_config, model_config)\u001b[0m\n\u001b[1;32m 455\u001b[0m \u001b[38;5;129m@staticmethod\u001b[39m\n\u001b[1;32m 456\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_prepare_adapter_config\u001b[39m(peft_config, model_config):\n\u001b[0;32m--> 457\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mpeft_config\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtarget_modules\u001b[49m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 458\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model_config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING:\n\u001b[1;32m 459\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease specify `target_modules` in `peft_config`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'PeftConfig' object has no attribute 'target_modules'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure the `target_modules` key exists in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\"] # Example modules, you may need to customize this\n", "\n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Initialize the PeftConfig with filtered config\n", " peft_config = PeftConfig(**filtered_config)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config, model_id=model_id)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 26, "id": "10542f7b-4bde-4fce-988c-94ddcea4bb32", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "AttributeError", "evalue": "'PeftConfig' object has no attribute 'target_modules'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[26], line 36\u001b[0m\n\u001b[1;32m 33\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 35\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 36\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 38\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:541\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, **kwargs)\u001b[0m\n\u001b[1;32m 539\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m(model, config, adapter_name, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype)\n\u001b[1;32m 540\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 541\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mMODEL_TYPE_TO_PEFT_MODEL_MAPPING\u001b[49m\u001b[43m[\u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtask_type\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 542\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mautocast_adapter_dtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mautocast_adapter_dtype\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 545\u001b[0m model\u001b[38;5;241m.\u001b[39mload_adapter(\n\u001b[1;32m 546\u001b[0m model_id, adapter_name, is_trainable\u001b[38;5;241m=\u001b[39mis_trainable, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 547\u001b[0m )\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:1542\u001b[0m, in \u001b[0;36mPeftModelForCausalLM.__init__\u001b[0;34m(self, model, peft_config, adapter_name, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 1540\u001b[0m \u001b[38;5;28mself\u001b[39m, model: torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mModule, peft_config: PeftConfig, adapter_name: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdefault\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 1541\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1542\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1543\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model_prepare_inputs_for_generation \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model\u001b[38;5;241m.\u001b[39mprepare_inputs_for_generation\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:155\u001b[0m, in \u001b[0;36mPeftModel.__init__\u001b[0;34m(self, model, peft_config, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_MODEL_MAPPING[peft_config\u001b[38;5;241m.\u001b[39mpeft_type]\n\u001b[0;32m--> 155\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mset_additional_trainable_modules(peft_config, adapter_name)\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_cast_adapter_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:139\u001b[0m, in \u001b[0;36mLoraModel.__init__\u001b[0;34m(self, model, config, adapter_name)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, model, config, adapter_name) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 139\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:175\u001b[0m, in \u001b[0;36mBaseTuner.__init__\u001b[0;34m(self, model, peft_config, adapter_name)\u001b[0m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pre_injection_hook(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config[adapter_name], adapter_name)\n\u001b[1;32m 174\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m peft_config \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA \u001b[38;5;129;01mor\u001b[39;00m peft_config[adapter_name] \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA:\n\u001b[0;32m--> 175\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minject_adapter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;66;03m# Copy the peft_config in the injected model.\u001b[39;00m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mpeft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:394\u001b[0m, in \u001b[0;36mBaseTuner.inject_adapter\u001b[0;34m(self, model, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(model_config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_dict\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 392\u001b[0m model_config \u001b[38;5;241m=\u001b[39m model_config\u001b[38;5;241m.\u001b[39mto_dict()\n\u001b[0;32m--> 394\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_prepare_adapter_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 396\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_model(peft_config, model)\n\u001b[1;32m 397\u001b[0m is_target_modules_in_base_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:457\u001b[0m, in \u001b[0;36mLoraModel._prepare_adapter_config\u001b[0;34m(peft_config, model_config)\u001b[0m\n\u001b[1;32m 455\u001b[0m \u001b[38;5;129m@staticmethod\u001b[39m\n\u001b[1;32m 456\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_prepare_adapter_config\u001b[39m(peft_config, model_config):\n\u001b[0;32m--> 457\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mpeft_config\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtarget_modules\u001b[49m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 458\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model_config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING:\n\u001b[1;32m 459\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease specify `target_modules` in `peft_config`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'PeftConfig' object has no attribute 'target_modules'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure the `target_modules` key exists in the config\n", " if 'target_modules' not in config:\n", " # Example target_modules, should be tailored to your model\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"]\n", " \n", " # Remove any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", " \n", " # Initialize the PeftConfig with filtered config\n", " peft_config = PeftConfig(**filtered_config)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config, model_id=model_id)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 27, "id": "e6e35a4b-574b-43b5-ad95-34511b5e1641", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[31mERROR: Invalid requirement: 'peft==[desired_version]': Expected end or semicolon (after name and no valid version specifier)\n", " peft==[desired_version]\n", " ^\u001b[0m\u001b[31m\n", "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install --upgrade peft==[desired_version]\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "0b35ca4e-f60e-4071-b89e-856dbbee01b7", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "__init__() got an unexpected keyword argument 'alpha_pattern'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[28], line 26\u001b[0m\n\u001b[1;32m 23\u001b[0m config[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtarget_modules\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mq_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mo_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 25\u001b[0m \u001b[38;5;66;03m# Initialize the PeftConfig with the config dictionary\u001b[39;00m\n\u001b[0;32m---> 26\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 28\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[1;32m 29\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n", "\u001b[0;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'alpha_pattern'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure the `target_modules` key exists in the config\n", " if 'target_modules' not in config:\n", " # Example target_modules, should be tailored to your model\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"]\n", " \n", " # Initialize the PeftConfig with the config dictionary\n", " peft_config = PeftConfig(**config)\n", " \n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the config and model ID\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config, model_id=model_id)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "474edf8f-5386-4b13-9ad4-3209bdce711e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "NameError", "evalue": "name 'Peft' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[29], line 29\u001b[0m\n\u001b[1;32m 26\u001b[0m filtered_config[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtarget_modules\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mq_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mo_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 28\u001b[0m \u001b[38;5;66;03m# Initialize the PeftConfig with the filtered config dictionary\u001b[39;00m\n\u001b[0;32m---> 29\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[43mPeft\u001b[49m\n", "\u001b[0;31mNameError\u001b[0m: name 'Peft' is not defined" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Add target_modules if not present\n", " if 'target_modules' not in filtered_config:\n", " filtered_config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"]\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = Peft\n" ] }, { "cell_type": "code", "execution_count": 30, "id": "176efc78-552d-4f30-bbda-168107293ccd", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "__init__() got an unexpected keyword argument 'target_modules'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[30], line 29\u001b[0m\n\u001b[1;32m 26\u001b[0m filtered_config[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtarget_modules\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mq_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mo_proj\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 28\u001b[0m \u001b[38;5;66;03m# Initialize the PeftConfig with the filtered config dictionary\u001b[39;00m\n\u001b[0;32m---> 29\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[43mPeftConfig\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfiltered_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 31\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[1;32m 32\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n", "\u001b[0;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'target_modules'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Add target_modules if not present\n", " if 'target_modules' not in filtered_config:\n", " filtered_config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"]\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 31, "id": "35a718d3-b79b-4ce0-8341-80eb422d9507", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[31], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys and remove 'target_modules'\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 32, "id": "d6585be9-750e-4f2c-9311-b8a93e17133c", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[32], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys and remove 'target_modules'\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model_id=model_id, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 33, "id": "5d5a3d28-adab-4497-b3c4-92954f93b5b4", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[33], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys and remove 'target_modules'\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 34, "id": "e17af5f0-9004-40c6-9be3-be0eb628dc0a", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[34], line 28\u001b[0m\n\u001b[1;32m 25\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m PeftConfig(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mfiltered_config)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary directly\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the PEFT model using the filtered config dictionary directly\n", " peft_model = PeftModel.from_pretrained(model_id=model_id, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 35, "id": "e15438bd-9e44-4090-a128-7778e3290c54", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[35], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the filtered config dictionary and the main model\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the filtered config dictionary and the main model\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 36, "id": "ce24439d-c09e-46bc-ba76-019941ce347e", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[36], line 28\u001b[0m\n\u001b[1;32m 25\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m PeftConfig(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mfiltered_config)\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m# Load the PEFT model directly using the model_id and filtered config\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 30\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the PEFT model directly using the model_id and filtered config\n", " peft_model = PeftModel.from_pretrained(model_id=model_id, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 37, "id": "0a36bf4d-31ea-488d-b7ef-2947ee86e251", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[37], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the base model and config\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the base model and config\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 38, "id": "5bdd323e-3597-455d-bc6f-e5761b1b6903", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "AttributeError", "evalue": "'PeftConfig' object has no attribute 'target_modules'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[38], line 31\u001b[0m\n\u001b[1;32m 28\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 30\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the model_id and config\u001b[39;00m\n\u001b[0;32m---> 31\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:541\u001b[0m, in \u001b[0;36mPeftModel.from_pretrained\u001b[0;34m(cls, model, model_id, adapter_name, is_trainable, config, autocast_adapter_dtype, ephemeral_gpu_offload, **kwargs)\u001b[0m\n\u001b[1;32m 539\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mcls\u001b[39m(model, config, adapter_name, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype)\n\u001b[1;32m 540\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 541\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mMODEL_TYPE_TO_PEFT_MODEL_MAPPING\u001b[49m\u001b[43m[\u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtask_type\u001b[49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 542\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mautocast_adapter_dtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mautocast_adapter_dtype\u001b[49m\n\u001b[1;32m 543\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 545\u001b[0m model\u001b[38;5;241m.\u001b[39mload_adapter(\n\u001b[1;32m 546\u001b[0m model_id, adapter_name, is_trainable\u001b[38;5;241m=\u001b[39mis_trainable, autocast_adapter_dtype\u001b[38;5;241m=\u001b[39mautocast_adapter_dtype, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 547\u001b[0m )\n\u001b[1;32m 549\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m model\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:1542\u001b[0m, in \u001b[0;36mPeftModelForCausalLM.__init__\u001b[0;34m(self, model, peft_config, adapter_name, **kwargs)\u001b[0m\n\u001b[1;32m 1539\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m 1540\u001b[0m \u001b[38;5;28mself\u001b[39m, model: torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mModule, peft_config: PeftConfig, adapter_name: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdefault\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[1;32m 1541\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1542\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1543\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model_prepare_inputs_for_generation \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model\u001b[38;5;241m.\u001b[39mprepare_inputs_for_generation\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:155\u001b[0m, in \u001b[0;36mPeftModel.__init__\u001b[0;34m(self, model, peft_config, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_MODEL_MAPPING[peft_config\u001b[38;5;241m.\u001b[39mpeft_type]\n\u001b[0;32m--> 155\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mset_additional_trainable_modules(peft_config, adapter_name)\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_cast_adapter_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:139\u001b[0m, in \u001b[0;36mLoraModel.__init__\u001b[0;34m(self, model, config, adapter_name)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, model, config, adapter_name) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 139\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:175\u001b[0m, in \u001b[0;36mBaseTuner.__init__\u001b[0;34m(self, model, peft_config, adapter_name)\u001b[0m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pre_injection_hook(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config[adapter_name], adapter_name)\n\u001b[1;32m 174\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m peft_config \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA \u001b[38;5;129;01mor\u001b[39;00m peft_config[adapter_name] \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA:\n\u001b[0;32m--> 175\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minject_adapter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;66;03m# Copy the peft_config in the injected model.\u001b[39;00m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mpeft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:394\u001b[0m, in \u001b[0;36mBaseTuner.inject_adapter\u001b[0;34m(self, model, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(model_config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_dict\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 392\u001b[0m model_config \u001b[38;5;241m=\u001b[39m model_config\u001b[38;5;241m.\u001b[39mto_dict()\n\u001b[0;32m--> 394\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_prepare_adapter_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 396\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_model(peft_config, model)\n\u001b[1;32m 397\u001b[0m is_target_modules_in_base_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:457\u001b[0m, in \u001b[0;36mLoraModel._prepare_adapter_config\u001b[0;34m(peft_config, model_config)\u001b[0m\n\u001b[1;32m 455\u001b[0m \u001b[38;5;129m@staticmethod\u001b[39m\n\u001b[1;32m 456\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_prepare_adapter_config\u001b[39m(peft_config, model_config):\n\u001b[0;32m--> 457\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mpeft_config\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtarget_modules\u001b[49m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 458\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model_config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING:\n\u001b[1;32m 459\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease specify `target_modules` in `peft_config`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'PeftConfig' object has no attribute 'target_modules'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the model_id and config\n", " peft_model = PeftModel.from_pretrained(model_id=model_id, model=model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 39, "id": "7ad47e29-98c9-4865-b642-10a1d1b6fa85", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "TypeError", "evalue": "from_pretrained() missing 1 required positional argument: 'model_id'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[39], line 34\u001b[0m\n\u001b[1;32m 31\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 33\u001b[0m \u001b[38;5;66;03m# Load the PEFT model using the model_id and config\u001b[39;00m\n\u001b[0;32m---> 34\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mTypeError\u001b[0m: from_pretrained() missing 1 required positional argument: 'model_id'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure target_modules is present in the config\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Load the PEFT model using the model_id and config\n", " peft_model = PeftModel.from_pretrained(model, config=peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 40, "id": "f6708098-1893-44c3-8c90-5add156b776e", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "AttributeError", "evalue": "'PeftConfig' object has no attribute 'target_modules'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[40], line 34\u001b[0m\n\u001b[1;32m 31\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 33\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[0;32m---> 34\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m \u001b[43mPeftModel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModels loaded successfully.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/peft_model.py:155\u001b[0m, in \u001b[0;36mPeftModel.__init__\u001b[0;34m(self, model, peft_config, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m PEFT_TYPE_TO_MODEL_MAPPING[peft_config\u001b[38;5;241m.\u001b[39mpeft_type]\n\u001b[0;32m--> 155\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mset_additional_trainable_modules(peft_config, adapter_name)\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_model, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_cast_adapter_dtype\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:139\u001b[0m, in \u001b[0;36mLoraModel.__init__\u001b[0;34m(self, model, config, adapter_name)\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, model, config, adapter_name) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 139\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:175\u001b[0m, in \u001b[0;36mBaseTuner.__init__\u001b[0;34m(self, model, peft_config, adapter_name)\u001b[0m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_pre_injection_hook(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config[adapter_name], adapter_name)\n\u001b[1;32m 174\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m peft_config \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA \u001b[38;5;129;01mor\u001b[39;00m peft_config[adapter_name] \u001b[38;5;241m!=\u001b[39m PeftType\u001b[38;5;241m.\u001b[39mXLORA:\n\u001b[0;32m--> 175\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minject_adapter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43madapter_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 177\u001b[0m \u001b[38;5;66;03m# Copy the peft_config in the injected model.\u001b[39;00m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodel\u001b[38;5;241m.\u001b[39mpeft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpeft_config\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/tuners_utils.py:394\u001b[0m, in \u001b[0;36mBaseTuner.inject_adapter\u001b[0;34m(self, model, adapter_name, autocast_adapter_dtype)\u001b[0m\n\u001b[1;32m 391\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(model_config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_dict\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[1;32m 392\u001b[0m model_config \u001b[38;5;241m=\u001b[39m model_config\u001b[38;5;241m.\u001b[39mto_dict()\n\u001b[0;32m--> 394\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_prepare_adapter_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpeft_config\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel_config\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 396\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_model(peft_config, model)\n\u001b[1;32m 397\u001b[0m is_target_modules_in_base_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/peft/tuners/lora/model.py:457\u001b[0m, in \u001b[0;36mLoraModel._prepare_adapter_config\u001b[0;34m(peft_config, model_config)\u001b[0m\n\u001b[1;32m 455\u001b[0m \u001b[38;5;129m@staticmethod\u001b[39m\n\u001b[1;32m 456\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_prepare_adapter_config\u001b[39m(peft_config, model_config):\n\u001b[0;32m--> 457\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mpeft_config\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtarget_modules\u001b[49m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 458\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m model_config[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING:\n\u001b[1;32m 459\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease specify `target_modules` in `peft_config`\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'PeftConfig' object has no attribute 'target_modules'" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig, PeftType\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure target_modules is present in the config\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Filter out any unexpected keys\n", " allowed_keys = set(PeftConfig.__annotations__.keys())\n", " filtered_config = {k: v for k, v in config.items() if k in allowed_keys}\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = PeftConfig(**filtered_config)\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 41, "id": "4ac8f18f-e2d8-46c9-ac9e-83f21208dc1b", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n", "Models loaded successfully.\n" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 42, "id": "95205d4a-ecad-4d2d-81c0-4cbaed5e910f", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: The impact of air quality on health is significant because it affects the health of people in different ways.\n", "\n", "The impact of air quality on health is significant because it affects the health of people in different ways.\n", "\n", "The impact of air quality\n" ] } ], "source": [ "import torch\n", "\n", "# Tokenize the input text\n", "input_text = \"The impact of air quality on health is significant because\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(\"Generated Text: \", generated_text)\n" ] }, { "cell_type": "code", "execution_count": 43, "id": "b7e57105-0872-428b-9b3d-a5f450a8db70", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: You're so stupid and worthless.\n", "I'm not worthless, I'm just a little bit of a dick.\n", "You're a dick.\n", "I'm a dick.\n", "I'm a dick.\n", "I'm a dick.\n", "I\n" ] } ], "source": [ "input_text = \"You're so stupid and worthless.\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(\"Generated Text:\", generated_text)\n" ] }, { "cell_type": "code", "execution_count": 44, "id": "500ac557-82f0-4fe5-8ecd-21c7b78803ae", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "800aab23ab464d7d90dff7114f010301", "version_major": 2, "version_minor": 0 }, "text/plain": [ "config.json: 0%| | 0.00/811 [00:00 3\u001b[0m outputs \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_length\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m50\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4\u001b[0m generated_text \u001b[38;5;241m=\u001b[39m tokenizer\u001b[38;5;241m.\u001b[39mdecode(outputs[\u001b[38;5;241m0\u001b[39m], skip_special_tokens\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGenerated Text:\u001b[39m\u001b[38;5;124m\"\u001b[39m, generated_text)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/torch/utils/_contextlib.py:116\u001b[0m, in \u001b[0;36mcontext_decorator..decorate_context\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 113\u001b[0m \u001b[38;5;129m@functools\u001b[39m\u001b[38;5;241m.\u001b[39mwraps(func)\n\u001b[1;32m 114\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdecorate_context\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 115\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m ctx_factory():\n\u001b[0;32m--> 116\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/generation/utils.py:1686\u001b[0m, in \u001b[0;36mGenerationMixin.generate\u001b[0;34m(self, inputs, generation_config, logits_processor, stopping_criteria, prefix_allowed_tokens_fn, synced_gpus, assistant_model, streamer, negative_prompt_ids, negative_prompt_attention_mask, **kwargs)\u001b[0m\n\u001b[1;32m 1602\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1603\u001b[0m \n\u001b[1;32m 1604\u001b[0m \u001b[38;5;124;03mGenerates sequences of token ids for models with a language modeling head.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1683\u001b[0m \u001b[38;5;124;03m - [`~generation.GenerateBeamEncoderDecoderOutput`]\u001b[39;00m\n\u001b[1;32m 1684\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1685\u001b[0m \u001b[38;5;66;03m# 1. Handle `generation_config` and kwargs that might update it, and validate the `.generate()` call\u001b[39;00m\n\u001b[0;32m-> 1686\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_validate_model_class\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1687\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtokenizer\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;66;03m# Pull this out first, we only use it for stopping criteria\u001b[39;00m\n\u001b[1;32m 1688\u001b[0m generation_config, model_kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare_generation_config(generation_config, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/generation/utils.py:1167\u001b[0m, in \u001b[0;36mGenerationMixin._validate_model_class\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1165\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m generate_compatible_classes:\n\u001b[1;32m 1166\u001b[0m exception_message \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m Please use one of the following classes instead: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mgenerate_compatible_classes\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m-> 1167\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(exception_message)\n", "\u001b[0;31mTypeError\u001b[0m: The current model class (BertForSequenceClassification) is not compatible with `.generate()`, as it doesn't have a language model head. Please use one of the following classes instead: {'BertLMHeadModel'}" ] } ], "source": [ "input_text = \"You're so stupid and worthless.\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "outputs = model.generate(**inputs, max_length=50)\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(\"Generated Text:\", generated_text)\n" ] }, { "cell_type": "code", "execution_count": 46, "id": "86b0bff0-d020-48b6-af10-e4cadb528c92", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predictions: tensor([[7.9478e-01, 3.3654e-04, 2.0638e-02, 1.3050e-05, 1.8411e-01, 1.2542e-04]],\n", " grad_fn=)\n" ] } ], "source": [ "import torch\n", "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n", "\n", "# Load the model and tokenizer\n", "model_name = \"unitary/toxic-bert\"\n", "model = AutoModelForSequenceClassification.from_pretrained(model_name)\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "\n", "# Tokenize the input text\n", "input_text = \"You're so stupid and worthless.\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Get the model predictions\n", "outputs = model(**inputs)\n", "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n", "\n", "# Print the predictions\n", "print(\"Predictions:\", predictions)\n" ] }, { "cell_type": "code", "execution_count": 47, "id": "69ab5444-8a0d-4ab4-9625-c6b275b3ec04", "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2e2ef084ffe6406c8b43f8e1878ac69c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "config.json: 0%| | 0.00/1.21k [00:00. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thoroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Rephrased Text: to be non-toxic: You're so stupid and worthless.\n" ] } ], "source": [ "# Load a text generation model for rephrasing (like T5 or GPT-2)\n", "from transformers import T5ForConditionalGeneration, T5Tokenizer\n", "\n", "rephrase_model_name = \"t5-small\"\n", "rephrase_model = T5ForConditionalGeneration.from_pretrained(rephrase_model_name)\n", "rephrase_tokenizer = T5Tokenizer.from_pretrained(rephrase_model_name)\n", "\n", "# Prepare the input for rephrasing\n", "detoxify_prompt = f\"Rephrase the following to be non-toxic: {input_text}\"\n", "rephrase_inputs = rephrase_tokenizer(detoxify_prompt, return_tensors=\"pt\", max_length=512, truncation=True).to(model.device)\n", "\n", "# Generate the rephrased text\n", "rephrase_outputs = rephrase_model.generate(**rephrase_inputs, max_length=50)\n", "rephrased_text = rephrase_tokenizer.decode(rephrase_outputs[0], skip_special_tokens=True)\n", "\n", "print(\"Rephrased Text:\", rephrased_text)\n" ] }, { "cell_type": "code", "execution_count": 48, "id": "c681972c-6087-4d71-b3ae-d9206ae85327", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predictions: tensor([[7.9478e-01, 3.3654e-04, 2.0638e-02, 1.3050e-05, 1.8411e-01, 1.2542e-04]],\n", " grad_fn=)\n", "Text is not toxic, no further action needed.\n" ] } ], "source": [ "# Step 1: Classify the Text\n", "input_text = \"You're so stupid and worthless.\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "outputs = model(**inputs)\n", "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n", "print(\"Predictions:\", predictions)\n", "\n", "# Step 2: Check for Toxicity\n", "if predictions[0, 1] > 0.5: # Assuming index 1 is the toxic class\n", " print(\"Text is toxic, proceeding to detoxify.\")\n", " \n", " # Step 3: Generate Detoxified Text\n", " detoxify_prompt = f\"Rephrase the following to be non-toxic: {input_text}\"\n", " rephrase_inputs = rephrase_tokenizer(detoxify_prompt, return_tensors=\"pt\", truncation=True).to(model.device)\n", " rephrase_outputs = rephrase_model.generate(**rephrase_inputs, max_length=50)\n", " rephrased_text = rephrase_tokenizer.decode(rephrase_outputs[0], skip_special_tokens=True)\n", " \n", " print(\"Detoxified Text:\", rephrased_text)\n", "else:\n", " print(\"Text is not toxic, no further action needed.\")\n" ] }, { "cell_type": "code", "execution_count": 49, "id": "52ca0146-cda6-4902-ac94-4a06fc13573d", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predictions: tensor([[9.0871e-01, 2.0563e-04, 6.4823e-03, 3.6782e-05, 8.4451e-02, 1.1745e-04]],\n", " grad_fn=)\n", "Text is not toxic, no further action needed.\n" ] } ], "source": [ "# Step 1: Classify the Text\n", "input_text = \"Shut up, you're worthless\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "outputs = model(**inputs)\n", "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n", "print(\"Predictions:\", predictions)\n", "\n", "# Step 2: Check for Toxicity\n", "if predictions[0, 1] > 0.5: # Assuming index 1 is the toxic class\n", " print(\"Text is toxic, proceeding to detoxify.\")\n", " \n", " # Step 3: Generate Detoxified Text\n", " detoxify_prompt = f\"Rephrase the following to be non-toxic: {input_text}\"\n", " rephrase_inputs = rephrase_tokenizer(detoxify_prompt, return_tensors=\"pt\", truncation=True).to(model.device)\n", " rephrase_outputs = rephrase_model.generate(**rephrase_inputs, max_length=50)\n", " rephrased_text = rephrase_tokenizer.decode(rephrase_outputs[0], skip_special_tokens=True)\n", " \n", " print(\"Detoxified Text:\", rephrased_text)\n", "else:\n", " print(\"Text is not toxic, no further action needed.\")\n" ] }, { "cell_type": "code", "execution_count": 55, "id": "c6d5602b-4910-4f55-9c9a-ac5eda9150f2", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predictions: tensor([[9.0871e-01, 2.0563e-04, 6.4823e-03, 3.6782e-05, 8.4451e-02, 1.1745e-04]],\n", " grad_fn=)\n", "Text is not toxic, no further action needed.\n" ] } ], "source": [ "import torch\n", "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n", "\n", "# Load the model and tokenizer\n", "model_name = \"unitary/toxic-bert\"\n", "model = AutoModelForSequenceClassification.from_pretrained(model_name)\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "\n", "# Step 1: Classify the Text\n", "input_text = \"Shut up, you're worthless\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "outputs = model(**inputs)\n", "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n", "print(\"Predictions:\", predictions)\n", "\n", "# Step 2: Check for Toxicity with a Lower Threshold\n", "if predictions[0, 1] > 0.2: # Lowered threshold from 0.3 to 0.2\n", " print(\"Text is toxic, proceeding to detoxify.\")\n", " # Step 3: Generate Detoxified Text\n", " detoxify_prompt = f\"Rephrase the following to be non-toxic: {input_text}\"\n", " rephrase_inputs = rephrase_tokenizer(detoxify_prompt, return_tensors=\"pt\", truncation=True).to(model.device)\n", " rephrase_outputs = rephrase_model.generate(**rephrase_inputs, max_length=50)\n", " rephrased_text = rephrase_tokenizer.decode(rephrase_outputs[0], skip_special_tokens=True)\n", "\n", " print(\"Detoxified Text:\", rephrased_text)\n", "else:\n", " print(\"Text is not toxic, no further action needed.\")\n" ] }, { "cell_type": "code", "execution_count": 56, "id": "63fd4940-2249-46da-b035-7ea0b5f24dba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Text is not toxic, no further action needed.\n" ] } ], "source": [ "# Step 2: Check for Toxicity with a Lower Threshold\n", "if predictions[0, 1] > 0.2: # Lowered threshold from 0.3 to 0.2\n", " print(\"Text is toxic, proceeding to detoxify.\")\n", " # Step 3: Generate Detoxified Text\n", " detoxify_prompt = f\"Rephrase the following to be non-toxic: {input_text}\"\n", " rephrase_inputs = rephrase_tokenizer(detoxify_prompt, return_tensors=\"pt\", truncation=True).to(model.device)\n", " rephrase_outputs = rephrase_model.generate(**rephrase_inputs, max_length=50)\n", " rephrased_text = rephrase_tokenizer.decode(rephrase_outputs[0], skip_special_tokens=True)\n", "\n", " print(\"Detoxified Text:\", rephrased_text)\n", "else:\n", " print(\"Text is not toxic, no further action needed.\")\n" ] }, { "cell_type": "code", "execution_count": 57, "id": "e1445e99-3eab-4f17-a4a0-5d67093cb074", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predictions: tensor([[9.0871e-01, 2.0563e-04, 6.4823e-03, 3.6782e-05, 8.4451e-02, 1.1745e-04]],\n", " grad_fn=)\n", "Text is not toxic, no further action needed.\n" ] } ], "source": [ "import torch\n", "from transformers import AutoModelForSequenceClassification, AutoTokenizer\n", "\n", "# Load the model and tokenizer\n", "model_name = \"unitary/toxic-bert\"\n", "model = AutoModelForSequenceClassification.from_pretrained(model_name)\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "\n", "# Step 1: Classify the Text\n", "input_text = \"Shut up, you're worthless\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "outputs = model(**inputs)\n", "predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)\n", "print(\"Predictions:\", predictions)\n", "\n", "# Step 2: Check for Toxicity with a Lower Threshold\n", "if predictions[0, 1] > 0.2: # Lowered threshold from 0.3 to 0.2\n", " print(\"Text is toxic, proceeding to detoxify.\")\n", " # Step 3: Generate Detoxified Text\n", " detoxify_prompt = f\"Rephrase the following to be non-toxic: {input_text}\"\n", " rephrase_inputs = rephrase_tokenizer(detoxify_prompt, return_tensors=\"pt\", truncation=True).to(model.device)\n", " rephrase_outputs = rephrase_model.generate(**rephrase_inputs, max_length=50)\n", " rephrased_text = rephrase_tokenizer.decode(rephrase_outputs[0], skip_special_tokens=True)\n", "\n", " print(\"Detoxified Text:\", rephrased_text)\n", "else:\n", " print(\"Text is not toxic, no further action needed.\")\n" ] }, { "cell_type": "code", "execution_count": 58, "id": "395cae43-3ec7-4664-bce5-dc169612da08", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Toxicity Result: [{'label': 'toxic', 'score': 0.9848880767822266}]\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the pipeline with the 'jigsaw' model for toxicity detection\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Test the model on a piece of text\n", "input_text = \"Shut up, you're worthless\"\n", "result = toxicity_pipeline(input_text)\n", "\n", "print(\"Toxicity Result:\", result)\n" ] }, { "cell_type": "code", "execution_count": 59, "id": "aab2bed8-01d6-4467-85b9-6d16fc94e6b5", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Toxicity Result: [{'label': 'toxic', 'score': 0.9848880767822266}]\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/transformers/generation/utils.py:1258: UserWarning: Using the model-agnostic default `max_length` (=20) to control the generation length. We recommend setting `max_new_tokens` to control the maximum length of the generation.\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Detoxified Text: tify: Shut up, you're worthlessless.\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load the detoxification model (T5 or similar)\n", "detox_model_name = \"t5-small\"\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detoxified_text = detox_pipeline(f\"detoxify: {input_text}\")[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": 60, "id": "316acd61-8668-4e74-b77a-dd84d45f796b", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Toxicity Result: [{'label': 'toxic', 'score': 0.9848880767822266}]\n", "Detoxified Text: to be polite: Shut up, you're worthlessless.\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load the detoxification model (T5 or similar)\n", "detox_model_name = \"t5-small\"\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detox_prompt = f\"Rephrase the following text to be polite: {input_text}\"\n", " detoxified_text = detox_pipeline(detox_prompt, max_length=50)[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": 61, "id": "c6ce0b99-6d47-4e7b-a410-b3f2fac1d02d", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Toxicity Result: [{'label': 'toxic', 'score': 0.9848880767822266}]\n", "Detoxified Text: Transform this sentence to be non-offensive: Shut up, you're worthlessless.\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load the detoxification model (T5 or similar)\n", "detox_model_name = \"t5-small\"\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detox_prompt = f\"Transform this sentence to be non-offensive: {input_text}\"\n", " detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": 62, "id": "db277319-6156-444e-b1a1-628ee011cd15", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Toxicity Result: [{'label': 'toxic', 'score': 0.9848880767822266}]\n", "Detoxified Text: Transform this text to be completely non-offensive and polite: Shut up, you're worthlessless: Shut up, you're worthlessless: Shut up, you're worthlessless.\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load the detoxification model (T5 or similar)\n", "detox_model_name = \"t5-small\"\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detox_prompt = f\"Transform this text to be completely non-offensive and polite: {input_text}\"\n", " detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6acf1db4-01b7-4cc5-851c-eb96df13ec67", "metadata": { "tags": [] }, "outputs": [], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load a more robust detoxification model (try t5-large or similar)\n", "detox_model_name = \"t5-large\" # or use another suitable model\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detox_prompt = f\"Convert this aggressive and offensive sentence into a friendly and respectful one: {input_text}\"\n", " detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "495b621c-fe7c-4a66-9574-4f433e9286d8", "metadata": { "tags": [] }, "outputs": [], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load a more robust detoxification model (try t5-large or similar)\n", "detox_model_name = \"t5-large\" # or use another suitable model\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detox_prompt = f\"Convert this aggressive and offensive sentence into a friendly and respectful one: {input_text}\"\n", " detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2b8ec149-0b08-458d-bf54-10c2ffe6a47f", "metadata": { "tags": [] }, "outputs": [], "source": [ "from transformers import pipeline\n", "\n", "# Load the detoxification model\n", "detox_model_name = \"t5-large\" # Ensure this model is available and loaded correctly\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Test with a simple input\n", "test_input = \"Make this sentence more positive: I hate this.\"\n", "detox_output = detox_pipeline(test_input, max_length=100)[0]['generated_text']\n", "print(\"Detox Test Output:\", detox_output)\n", "\n", "# Apply to your case\n", "input_text = \"Shut up, you're worthless\"\n", "detox_prompt = f\"Make the following sentence polite: {input_text}\"\n", "detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "2f0208e8-a7a2-4d5f-9e34-9248ae0a6d4f", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Toxicity Result: [{'label': 'toxic', 'score': 0.9848880767822266}]\n", "Detoxified Text: :... You're worthless. You're worthless. You're worthless. You're worthless. You're worthless. Shut up, you're worthless. Shut up, you're worthless. Shut up, you're worthless. Shut up, you're worthless. Convert this.::::....\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the toxicity detection model\n", "toxicity_pipeline = pipeline(\"text-classification\", model=\"unitary/toxic-bert\")\n", "\n", "# Load a more robust detoxification model (try t5-large or similar)\n", "detox_model_name = \"t5-large\" # or use another suitable model\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Step 1: Detect Toxicity\n", "result = toxicity_pipeline(input_text)\n", "toxicity_score = result[0]['score']\n", "toxicity_label = result[0]['label']\n", "\n", "print(f\"Toxicity Result: {result}\")\n", "\n", "# Step 2: Detoxify if toxic\n", "if toxicity_label == \"toxic\" and toxicity_score > 0.5: # Adjust threshold as needed\n", " detox_prompt = f\"Convert this aggressive and offensive sentence into a friendly and respectful one: {input_text}\"\n", " detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", " print(\"Detoxified Text:\", detoxified_text)\n", "else:\n", " print(\"Text is not toxic, no detoxification needed.\")\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "c7915da1-dcf9-461b-aad5-76ce8cf08dcd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Detox Test Output: Make this sentence more positive: I hate this.\n", "Detoxified Text: :.. Shut up, you're worthless. Make the following sentence polite:. Shut up, you're worthless. You're worthless. Make the following sentence polite: Shut up, you're worthless. You're worthless. Make the following sentence polite: Shut up, you're worthless.\n" ] } ], "source": [ "from transformers import pipeline\n", "\n", "# Load the detoxification model\n", "detox_model_name = \"t5-large\" # Ensure this model is available and loaded correctly\n", "detox_pipeline = pipeline(\"text2text-generation\", model=detox_model_name)\n", "\n", "# Test with a simple input\n", "test_input = \"Make this sentence more positive: I hate this.\"\n", "detox_output = detox_pipeline(test_input, max_length=100)[0]['generated_text']\n", "print(\"Detox Test Output:\", detox_output)\n", "\n", "# Apply to your case\n", "input_text = \"Shut up, you're worthless\"\n", "detox_prompt = f\"Make the following sentence polite: {input_text}\"\n", "detoxified_text = detox_pipeline(detox_prompt, max_length=100)[0]['generated_text']\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "9e156cff-a357-49bb-8595-1e947ca66592", "metadata": { "tags": [] }, "outputs": [ { "ename": "OSError", "evalue": "/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co//opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/tree/None' for available files.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 7\u001b[0m\n\u001b[1;32m 4\u001b[0m model_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m# Replace ... with the exact folder name\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;66;03m# Load the tokenizer and model from the fine-tuned model directory\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m \u001b[43mAutoTokenizer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_name\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 8\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# Create a text generation pipeline using your fine-tuned model\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/tokenization_auto.py:854\u001b[0m, in \u001b[0;36mAutoTokenizer.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *inputs, **kwargs)\u001b[0m\n\u001b[1;32m 852\u001b[0m config \u001b[38;5;241m=\u001b[39m AutoConfig\u001b[38;5;241m.\u001b[39mfor_model(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mconfig_dict)\n\u001b[1;32m 853\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 854\u001b[0m config \u001b[38;5;241m=\u001b[39m \u001b[43mAutoConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 855\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrust_remote_code\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtrust_remote_code\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 856\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 857\u001b[0m config_tokenizer_class \u001b[38;5;241m=\u001b[39m config\u001b[38;5;241m.\u001b[39mtokenizer_class\n\u001b[1;32m 858\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(config, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoTokenizer\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config\u001b[38;5;241m.\u001b[39mauto_map:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/configuration_auto.py:976\u001b[0m, in \u001b[0;36mAutoConfig.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 973\u001b[0m trust_remote_code \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrust_remote_code\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 974\u001b[0m code_revision \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcode_revision\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 976\u001b[0m config_dict, unused_kwargs \u001b[38;5;241m=\u001b[39m \u001b[43mPretrainedConfig\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 977\u001b[0m has_remote_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAutoConfig\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto_map\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 978\u001b[0m has_local_code \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict \u001b[38;5;129;01mand\u001b[39;00m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel_type\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;129;01min\u001b[39;00m CONFIG_MAPPING\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:632\u001b[0m, in \u001b[0;36mPretrainedConfig.get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 630\u001b[0m original_kwargs \u001b[38;5;241m=\u001b[39m copy\u001b[38;5;241m.\u001b[39mdeepcopy(kwargs)\n\u001b[1;32m 631\u001b[0m \u001b[38;5;66;03m# Get config dict associated with the base config file\u001b[39;00m\n\u001b[0;32m--> 632\u001b[0m config_dict, kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_config_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 633\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m config_dict:\n\u001b[1;32m 634\u001b[0m original_kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m config_dict[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_commit_hash\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/configuration_utils.py:689\u001b[0m, in \u001b[0;36mPretrainedConfig._get_config_dict\u001b[0;34m(cls, pretrained_model_name_or_path, **kwargs)\u001b[0m\n\u001b[1;32m 685\u001b[0m configuration_file \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_configuration_file\u001b[39m\u001b[38;5;124m\"\u001b[39m, CONFIG_NAME) \u001b[38;5;28;01mif\u001b[39;00m gguf_file \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m gguf_file\n\u001b[1;32m 687\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 688\u001b[0m \u001b[38;5;66;03m# Load from local folder or from cache or download from model Hub and cache\u001b[39;00m\n\u001b[0;32m--> 689\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 690\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 691\u001b[0m \u001b[43m \u001b[49m\u001b[43mconfiguration_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 692\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 693\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 694\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 695\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 696\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 697\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 698\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 699\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 700\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 701\u001b[0m \u001b[43m \u001b[49m\u001b[43m_commit_hash\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_hash\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 702\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 703\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 704\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m:\n\u001b[1;32m 705\u001b[0m \u001b[38;5;66;03m# Raise any environment error raise by `cached_file`. It will have a helpful error message adapted to\u001b[39;00m\n\u001b[1;32m 706\u001b[0m \u001b[38;5;66;03m# the original exception.\u001b[39;00m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:373\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 371\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39misfile(resolved_file):\n\u001b[1;32m 372\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _raise_exceptions_for_missing_entries:\n\u001b[0;32m--> 373\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 374\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m does not appear to have a file named \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfull_filename\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m. Checkout \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 375\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://huggingface.co/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/tree/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrevision\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m for available files.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 376\u001b[0m )\n\u001b[1;32m 377\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", "\u001b[0;31mOSError\u001b[0m: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM does not appear to have a file named config.json. Checkout 'https://huggingface.co//opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/tree/None' for available files." ] } ], "source": [ "from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n", "\n", "# Specify the path to your fine-tuned model\n", "model_name = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM\" # Replace ... with the exact folder name\n", "\n", "# Load the tokenizer and model from the fine-tuned model directory\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "model = AutoModelForCausalLM.from_pretrained(model_name)\n", "\n", "# Create a text generation pipeline using your fine-tuned model\n", "detox_pipeline = pipeline(\"text-generation\", model=model, tokenizer=tokenizer)\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Generate detoxified text\n", "detox_prompt = f\"Detoxify this text: {input_text}\"\n", "detoxified_text = detox_pipeline(detox_prompt, max_length=100, num_return_sequences=1)[0]['generated_text']\n", "\n", "print(\"Detoxified Text:\", detoxified_text)\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "a132fcdb-79f6-456e-a1de-5ee9a607a96a", "metadata": { "tags": [] }, "outputs": [ { "ename": "NameError", "evalue": "name 'tokenizer' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[6], line 5\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# Tokenize the input text\u001b[39;00m\n\u001b[1;32m 4\u001b[0m input_text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe impact of air quality on health is significant because\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 5\u001b[0m inputs \u001b[38;5;241m=\u001b[39m \u001b[43mtokenizer\u001b[49m(input_text, return_tensors\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpt\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mto(model\u001b[38;5;241m.\u001b[39mdevice)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Generate text using the model\u001b[39;00m\n\u001b[1;32m 8\u001b[0m outputs \u001b[38;5;241m=\u001b[39m peft_model\u001b[38;5;241m.\u001b[39mgenerate(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39minputs, max_length\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m50\u001b[39m)\n", "\u001b[0;31mNameError\u001b[0m: name 'tokenizer' is not defined" ] } ], "source": [ "import torch\n", "\n", "# Tokenize the input text\n", "input_text = \"The impact of air quality on health is significant because\"\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model\n", "outputs = peft_model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(\"Generated Text: \", generated_text)\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "ae9d5e8f-878f-41f1-a00f-c053b930bbab", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 36\u001b[0m\n\u001b[1;32m 25\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m LoraConfig(\n\u001b[1;32m 26\u001b[0m peft_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLORA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct type if different\u001b[39;00m\n\u001b[1;32m 27\u001b[0m task_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct task type if different\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 32\u001b[0m bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 33\u001b[0m )\n\u001b[1;32m 35\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[0;32m---> 36\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 38\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[1;32m 39\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel(model, peft_config)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/auto_factory.py:564\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 562\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(config) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[1;32m 563\u001b[0m model_class \u001b[38;5;241m=\u001b[39m _get_model_class(config, \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping)\n\u001b[0;32m--> 564\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmodel_class\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 565\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mmodel_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhub_kwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 566\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 567\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 568\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnrecognized configuration class \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for this kind of AutoModel: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 569\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModel type should be one of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(c\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mfor\u001b[39;00m\u001b[38;5;250m \u001b[39mc\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01min\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys())\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 570\u001b[0m )\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:3941\u001b[0m, in \u001b[0;36mPreTrainedModel.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 3931\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dtype_orig \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 3932\u001b[0m torch\u001b[38;5;241m.\u001b[39mset_default_dtype(dtype_orig)\n\u001b[1;32m 3934\u001b[0m (\n\u001b[1;32m 3935\u001b[0m model,\n\u001b[1;32m 3936\u001b[0m missing_keys,\n\u001b[1;32m 3937\u001b[0m unexpected_keys,\n\u001b[1;32m 3938\u001b[0m mismatched_keys,\n\u001b[1;32m 3939\u001b[0m offload_index,\n\u001b[1;32m 3940\u001b[0m error_msgs,\n\u001b[0;32m-> 3941\u001b[0m ) \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_load_pretrained_model\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 3942\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3943\u001b[0m \u001b[43m \u001b[49m\u001b[43mstate_dict\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3944\u001b[0m \u001b[43m \u001b[49m\u001b[43mloaded_state_dict_keys\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# XXX: rename?\u001b[39;49;00m\n\u001b[1;32m 3945\u001b[0m \u001b[43m \u001b[49m\u001b[43mresolved_archive_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3946\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3947\u001b[0m \u001b[43m \u001b[49m\u001b[43mignore_mismatched_sizes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mignore_mismatched_sizes\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3948\u001b[0m \u001b[43m \u001b[49m\u001b[43msharded_metadata\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msharded_metadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3949\u001b[0m \u001b[43m \u001b[49m\u001b[43m_fast_init\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_fast_init\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3950\u001b[0m \u001b[43m \u001b[49m\u001b[43mlow_cpu_mem_usage\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlow_cpu_mem_usage\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3951\u001b[0m \u001b[43m \u001b[49m\u001b[43mdevice_map\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdevice_map\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3952\u001b[0m \u001b[43m \u001b[49m\u001b[43moffload_folder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moffload_folder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3953\u001b[0m \u001b[43m \u001b[49m\u001b[43moffload_state_dict\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moffload_state_dict\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3954\u001b[0m \u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtorch_dtype\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3955\u001b[0m \u001b[43m \u001b[49m\u001b[43mhf_quantizer\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mhf_quantizer\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3956\u001b[0m \u001b[43m \u001b[49m\u001b[43mkeep_in_fp32_modules\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkeep_in_fp32_modules\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3957\u001b[0m \u001b[43m \u001b[49m\u001b[43mgguf_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgguf_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3958\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3960\u001b[0m \u001b[38;5;66;03m# make sure token embedding weights are still tied if needed\u001b[39;00m\n\u001b[1;32m 3961\u001b[0m model\u001b[38;5;241m.\u001b[39mtie_weights()\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:4360\u001b[0m, in \u001b[0;36mPreTrainedModel._load_pretrained_model\u001b[0;34m(cls, model, state_dict, loaded_keys, resolved_archive_file, pretrained_model_name_or_path, ignore_mismatched_sizes, sharded_metadata, _fast_init, low_cpu_mem_usage, device_map, offload_folder, offload_state_dict, dtype, hf_quantizer, keep_in_fp32_modules, gguf_path)\u001b[0m\n\u001b[1;32m 4355\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 4356\u001b[0m \u001b[38;5;66;03m# Sharded checkpoint or whole but low_cpu_mem_usage==True\u001b[39;00m\n\u001b[1;32m 4357\u001b[0m assign_to_params_buffers \u001b[38;5;241m=\u001b[39m check_support_param_buffer_assignment(\n\u001b[1;32m 4358\u001b[0m model_to_load, state_dict, start_prefix\n\u001b[1;32m 4359\u001b[0m )\n\u001b[0;32m-> 4360\u001b[0m error_msgs \u001b[38;5;241m=\u001b[39m \u001b[43m_load_state_dict_into_model\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 4361\u001b[0m \u001b[43m \u001b[49m\u001b[43mmodel_to_load\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate_dict\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstart_prefix\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43massign_to_params_buffers\u001b[49m\n\u001b[1;32m 4362\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4364\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 4365\u001b[0m \u001b[38;5;66;03m# This should always be a list but, just to be sure.\u001b[39;00m\n\u001b[1;32m 4366\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(resolved_archive_file, \u001b[38;5;28mlist\u001b[39m):\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:748\u001b[0m, in \u001b[0;36m_load_state_dict_into_model\u001b[0;34m(model_to_load, state_dict, start_prefix, assign_to_params_buffers)\u001b[0m\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m child \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 746\u001b[0m load(child, state_dict, prefix \u001b[38;5;241m+\u001b[39m name \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m, assign_to_params_buffers)\n\u001b[0;32m--> 748\u001b[0m \u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_to_load\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate_dict\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprefix\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstart_prefix\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43massign_to_params_buffers\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43massign_to_params_buffers\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 749\u001b[0m \u001b[38;5;66;03m# Delete `state_dict` so it could be collected by GC earlier. Note that `state_dict` is a copy of the argument, so\u001b[39;00m\n\u001b[1;32m 750\u001b[0m \u001b[38;5;66;03m# it's safe to delete it.\u001b[39;00m\n\u001b[1;32m 751\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m state_dict\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:746\u001b[0m, in \u001b[0;36m_load_state_dict_into_model..load\u001b[0;34m(module, state_dict, prefix, assign_to_params_buffers)\u001b[0m\n\u001b[1;32m 744\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m name, child \u001b[38;5;129;01min\u001b[39;00m module\u001b[38;5;241m.\u001b[39m_modules\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m child \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 746\u001b[0m \u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mchild\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate_dict\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprefix\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43massign_to_params_buffers\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:746\u001b[0m, in \u001b[0;36m_load_state_dict_into_model..load\u001b[0;34m(module, state_dict, prefix, assign_to_params_buffers)\u001b[0m\n\u001b[1;32m 744\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m name, child \u001b[38;5;129;01min\u001b[39;00m module\u001b[38;5;241m.\u001b[39m_modules\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m child \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 746\u001b[0m \u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mchild\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate_dict\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprefix\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43massign_to_params_buffers\u001b[49m\u001b[43m)\u001b[49m\n", " \u001b[0;31m[... skipping similar frames: _load_state_dict_into_model..load at line 746 (2 times)]\u001b[0m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:746\u001b[0m, in \u001b[0;36m_load_state_dict_into_model..load\u001b[0;34m(module, state_dict, prefix, assign_to_params_buffers)\u001b[0m\n\u001b[1;32m 744\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m name, child \u001b[38;5;129;01min\u001b[39;00m module\u001b[38;5;241m.\u001b[39m_modules\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m child \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 746\u001b[0m \u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mchild\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstate_dict\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprefix\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43massign_to_params_buffers\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/modeling_utils.py:742\u001b[0m, in \u001b[0;36m_load_state_dict_into_model..load\u001b[0;34m(module, state_dict, prefix, assign_to_params_buffers)\u001b[0m\n\u001b[1;32m 740\u001b[0m module\u001b[38;5;241m.\u001b[39m_load_from_state_dict(\u001b[38;5;241m*\u001b[39margs)\n\u001b[1;32m 741\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 742\u001b[0m \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_load_from_state_dict\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 744\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m name, child \u001b[38;5;129;01min\u001b[39;00m module\u001b[38;5;241m.\u001b[39m_modules\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m 745\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m child \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/torch/nn/modules/module.py:2096\u001b[0m, in \u001b[0;36mModule._load_from_state_dict\u001b[0;34m(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs)\u001b[0m\n\u001b[1;32m 2094\u001b[0m \u001b[38;5;28msetattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, name, input_param)\n\u001b[1;32m 2095\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 2096\u001b[0m \u001b[43mparam\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcopy_\u001b[49m\u001b[43m(\u001b[49m\u001b[43minput_param\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2097\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m ex:\n\u001b[1;32m 2098\u001b[0m action \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mswapping\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m use_swap_tensors \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcopying\u001b[39m\u001b[38;5;124m\"\u001b[39m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8be09e99-406c-4421-b147-4a755f5c412c", "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", " \n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", " \n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a099b1cc-f95b-4b7c-8b83-66828c8f6b36", "metadata": {}, "outputs": [], "source": [ "import os\n", "import json\n", "from transformers import AutoTokenizer, pipeline\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "import torch\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", "\n", "# Load the PEFT config manually\n", "with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", "# Ensure target_modules is present in the config\n", "if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", "# Initialize the PeftConfig with the filtered config dictionary\n", "peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", ")\n", "\n", "# Load the main model\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "# Apply the PEFT model with the configuration\n", "peft_model = PeftModel(model, peft_config)\n", "\n", "# Load the tokenizer\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "\n", "# Create a pipeline for text generation using your fine-tuned model\n", "detox_pipeline = pipeline(\"text-generation\", model=peft_model, tokenizer=tokenizer, device=torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"))\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Generate detoxified text\n", "detox_prompt = f\"Transform this text to be completely non-offensive and polite: {input_text}\"\n", "detoxified_text = detox_pipeline(detox_prompt, max_length=100, num_return_sequences=1)[0]['generated_text']\n", "\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "9c6c5dcd-c5e0-4d9c-94dc-c5a9d6eabf6d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "NameError", "evalue": "name 'AutoModelForCausalLM' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[6], line 37\u001b[0m\n\u001b[1;32m 26\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m LoraConfig(\n\u001b[1;32m 27\u001b[0m peft_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLORA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct type if different\u001b[39;00m\n\u001b[1;32m 28\u001b[0m task_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct task type if different\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 33\u001b[0m bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 34\u001b[0m )\n\u001b[1;32m 36\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[0;32m---> 37\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_id)\n\u001b[1;32m 38\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[1;32m 39\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel(model, peft_config)\n", "\u001b[0;31mNameError\u001b[0m: name 'AutoModelForCausalLM' is not defined" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoTokenizer, pipeline\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "import torch\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", "\n", "# Load the PEFT config manually\n", "with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", "# Ensure target_modules is present in the config\n", "if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", "# Initialize the PeftConfig with the filtered config dictionary\n", "peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", ")\n", "\n", "# Load the main model\n", "model = AutoModelForCausalLM.from_pretrained(model_id)\n", "# Apply the PEFT model with the configuration\n", "peft_model = PeftModel(model, peft_config)\n", "\n", "# Load the tokenizer\n", "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", "\n", "# Create a pipeline for text generation using your fine-tuned model\n", "detox_pipeline = pipeline(\"text-generation\", model=peft_model, tokenizer=tokenizer, device=torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\"))\n", "\n", "# Input text\n", "input_text = \"Shut up, you're worthless\"\n", "\n", "# Generate detoxified text\n", "detox_prompt = f\"Transform this text to be completely non-offensive and polite: {input_text}\"\n", "detoxified_text = detox_pipeline(detox_prompt, max_length=100, num_return_sequences=1)[0]['generated_text']\n", "\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "f149fac5-d8ec-4c2f-81c9-84370e501c66", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting peft\n", " Downloading peft-0.12.0-py3-none-any.whl (296 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m296.4/296.4 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: psutil in /opt/app-root/lib/python3.9/site-packages (from peft) (5.9.8)\n", "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from peft) (24.0)\n", "Requirement already satisfied: huggingface-hub>=0.17.0 in /opt/app-root/lib/python3.9/site-packages (from peft) (0.24.6)\n", "Requirement already satisfied: safetensors in /opt/app-root/lib/python3.9/site-packages (from peft) (0.4.4)\n", "Requirement already satisfied: tqdm in /opt/app-root/lib/python3.9/site-packages (from peft) (4.66.4)\n", "Requirement already satisfied: numpy>=1.17 in /opt/app-root/lib/python3.9/site-packages (from peft) (1.24.4)\n", "Collecting torch>=1.13.0\n", " Downloading torch-2.4.0-cp39-cp39-manylinux1_x86_64.whl (797.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m797.2/797.2 MB\u001b[0m \u001b[31m113.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pyyaml in /opt/app-root/lib/python3.9/site-packages (from peft) (6.0.1)\n", "Collecting accelerate>=0.21.0\n", " Downloading accelerate-0.33.0-py3-none-any.whl (315 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m315.1/315.1 kB\u001b[0m \u001b[31m288.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: transformers in /opt/app-root/lib/python3.9/site-packages (from peft) (4.44.1)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub>=0.17.0->peft) (4.11.0)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub>=0.17.0->peft) (2024.5.0)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub>=0.17.0->peft) (3.14.0)\n", "Requirement already satisfied: requests in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub>=0.17.0->peft) (2.32.2)\n", "Collecting nvidia-cublas-cu12==12.1.3.1\n", " Downloading nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m410.6/410.6 MB\u001b[0m \u001b[31m115.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (3.1.4)\n", "Collecting nvidia-cuda-cupti-cu12==12.1.105\n", " Downloading nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.1/14.1 MB\u001b[0m \u001b[31m117.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting sympy\n", " Downloading sympy-1.13.2-py3-none-any.whl (6.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.2/6.2 MB\u001b[0m \u001b[31m120.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cudnn-cu12==9.1.0.70\n", " Downloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m119.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cusolver-cu12==11.4.5.107\n", " Downloading nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m124.2/124.2 MB\u001b[0m \u001b[31m118.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-curand-cu12==10.3.2.106\n", " Downloading nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.5/56.5 MB\u001b[0m \u001b[31m70.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cusparse-cu12==12.1.0.106\n", " Downloading nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m196.0/196.0 MB\u001b[0m \u001b[31m47.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cuda-nvrtc-cu12==12.1.105\n", " Downloading nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m23.7/23.7 MB\u001b[0m \u001b[31m94.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=1.13.0->peft) (3.2.1)\n", "Collecting nvidia-cuda-runtime-cu12==12.1.105\n", " Downloading nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m823.6/823.6 kB\u001b[0m \u001b[31m191.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting nvidia-nccl-cu12==2.20.5\n", " Downloading nvidia_nccl_cu12-2.20.5-py3-none-manylinux2014_x86_64.whl (176.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m176.2/176.2 MB\u001b[0m \u001b[31m113.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-nvtx-cu12==12.1.105\n", " Downloading nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m99.1/99.1 kB\u001b[0m \u001b[31m236.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting triton==3.0.0\n", " Downloading triton-3.0.0-1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (209.4 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m209.4/209.4 MB\u001b[0m \u001b[31m115.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-cufft-cu12==11.0.2.54\n", " Downloading nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.6/121.6 MB\u001b[0m \u001b[31m103.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting nvidia-nvjitlink-cu12\n", " Downloading nvidia_nvjitlink_cu12-12.6.20-py3-none-manylinux2014_x86_64.whl (19.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.7/19.7 MB\u001b[0m \u001b[31m101.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: regex!=2019.12.17 in /opt/app-root/lib/python3.9/site-packages (from transformers->peft) (2024.7.24)\n", "Requirement already satisfied: tokenizers<0.20,>=0.19 in /opt/app-root/lib/python3.9/site-packages (from transformers->peft) (0.19.1)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=1.13.0->peft) (2.1.5)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests->huggingface-hub>=0.17.0->peft) (1.26.18)\n", "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests->huggingface-hub>=0.17.0->peft) (3.7)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests->huggingface-hub>=0.17.0->peft) (3.3.2)\n", "Requirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests->huggingface-hub>=0.17.0->peft) (2024.2.2)\n", "Collecting mpmath<1.4,>=1.1.0\n", " Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m536.2/536.2 kB\u001b[0m \u001b[31m271.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: mpmath, triton, sympy, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, nvidia-cusparse-cu12, nvidia-cudnn-cu12, nvidia-cusolver-cu12, torch, accelerate, peft\n", "Successfully installed accelerate-0.33.0 mpmath-1.3.0 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.20.5 nvidia-nvjitlink-cu12-12.6.20 nvidia-nvtx-cu12-12.1.105 peft-0.12.0 sympy-1.13.2 torch-2.4.0 triton-3.0.0\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.2\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!pip install peft\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "0d68edca-5bb8-44eb-9949-1b4722d6ff7a", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pip in /opt/app-root/lib/python3.9/site-packages (22.2.2)\n", "Collecting pip\n", " Downloading pip-24.2-py3-none-any.whl (1.8 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.8/1.8 MB\u001b[0m \u001b[31m9.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hInstalling collected packages: pip\n", " Attempting uninstall: pip\n", " Found existing installation: pip 22.2.2\n", " Uninstalling pip-22.2.2:\n", " Successfully uninstalled pip-22.2.2\n", "Successfully installed pip-24.2\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install --upgrade pip" ] }, { "cell_type": "code", "execution_count": 7, "id": "a51ea0ec-dda5-4cfd-9118-06a405517424", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "ImportError", "evalue": "\nAutoModelForCausalLM requires the PyTorch library but it was not found in your environment. Checkout the instructions on the\ninstallation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.\nPlease note that you may need to restart your runtime after installation.\n", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 37\u001b[0m\n\u001b[1;32m 26\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m LoraConfig(\n\u001b[1;32m 27\u001b[0m peft_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLORA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct type if different\u001b[39;00m\n\u001b[1;32m 28\u001b[0m task_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct task type if different\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 33\u001b[0m bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 34\u001b[0m )\n\u001b[1;32m 36\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[0;32m---> 37\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m(model_id)\n\u001b[1;32m 38\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[1;32m 39\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel(model, peft_config)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/import_utils.py:1543\u001b[0m, in \u001b[0;36mDummyObject.__getattribute__\u001b[0;34m(cls, key)\u001b[0m\n\u001b[1;32m 1541\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m key\u001b[38;5;241m.\u001b[39mstartswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_from_config\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 1542\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__getattribute__\u001b[39m(key)\n\u001b[0;32m-> 1543\u001b[0m \u001b[43mrequires_backends\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_backends\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/import_utils.py:1531\u001b[0m, in \u001b[0;36mrequires_backends\u001b[0;34m(obj, backends)\u001b[0m\n\u001b[1;32m 1529\u001b[0m failed \u001b[38;5;241m=\u001b[39m [msg\u001b[38;5;241m.\u001b[39mformat(name) \u001b[38;5;28;01mfor\u001b[39;00m available, msg \u001b[38;5;129;01min\u001b[39;00m checks \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m available()]\n\u001b[1;32m 1530\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m failed:\n\u001b[0;32m-> 1531\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(failed))\n", "\u001b[0;31mImportError\u001b[0m: \nAutoModelForCausalLM requires the PyTorch library but it was not found in your environment. Checkout the instructions on the\ninstallation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.\nPlease note that you may need to restart your runtime after installation.\n" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "import torch\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", "\n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "9178cf13-e927-4339-8729-c9433fe03eef", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: torch in /opt/app-root/lib/python3.9/site-packages (2.4.0)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from torch) (3.14.0)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /opt/app-root/lib/python3.9/site-packages (from torch) (4.11.0)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch) (1.13.2)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch) (3.2.1)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch) (3.1.4)\n", "Requirement already satisfied: fsspec in /opt/app-root/lib/python3.9/site-packages (from torch) (2024.5.0)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch) (9.1.0.70)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch) (11.0.2.54)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch) (11.4.5.107)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.0.106)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch) (2.20.5)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch) (3.0.0)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch) (12.6.20)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch) (2.1.5)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch) (1.3.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install torch\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "126ede85-c325-40c6-833d-83b3d02ac89d", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: torch in /opt/app-root/lib/python3.9/site-packages (2.4.0)\n", "Collecting torchvision\n", " Downloading torchvision-0.19.0-cp39-cp39-manylinux1_x86_64.whl.metadata (6.0 kB)\n", "Collecting torchaudio\n", " Downloading torchaudio-2.4.0-cp39-cp39-manylinux1_x86_64.whl.metadata (6.4 kB)\n", "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from torch) (3.14.0)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in /opt/app-root/lib/python3.9/site-packages (from torch) (4.11.0)\n", "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch) (1.13.2)\n", "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch) (3.2.1)\n", "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch) (3.1.4)\n", "Requirement already satisfied: fsspec in /opt/app-root/lib/python3.9/site-packages (from torch) (2024.5.0)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in /opt/app-root/lib/python3.9/site-packages (from torch) (9.1.0.70)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /opt/app-root/lib/python3.9/site-packages (from torch) (11.0.2.54)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /opt/app-root/lib/python3.9/site-packages (from torch) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /opt/app-root/lib/python3.9/site-packages (from torch) (11.4.5.107)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.0.106)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /opt/app-root/lib/python3.9/site-packages (from torch) (2.20.5)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /opt/app-root/lib/python3.9/site-packages (from torch) (12.1.105)\n", "Requirement already satisfied: triton==3.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch) (3.0.0)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /opt/app-root/lib/python3.9/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch) (12.6.20)\n", "Requirement already satisfied: numpy in /opt/app-root/lib/python3.9/site-packages (from torchvision) (1.24.4)\n", "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /opt/app-root/lib/python3.9/site-packages (from torchvision) (10.3.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch) (2.1.5)\n", "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch) (1.3.0)\n", "Downloading torchvision-0.19.0-cp39-cp39-manylinux1_x86_64.whl (7.0 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.0/7.0 MB\u001b[0m \u001b[31m43.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hDownloading torchaudio-2.4.0-cp39-cp39-manylinux1_x86_64.whl (3.4 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m89.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: torchvision, torchaudio\n", "Successfully installed torchaudio-2.4.0 torchvision-0.19.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install torch torchvision torchaudio\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "46afad98-9444-4097-ad20-d1fedf0543d0", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.4.0+cu121\n" ] } ], "source": [ "import torch\n", "print(torch.__version__)\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "fc843753-58fb-4827-a082-aca5b0786efd", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "ImportError", "evalue": "\nAutoModelForCausalLM requires the PyTorch library but it was not found in your environment. Checkout the instructions on the\ninstallation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.\nPlease note that you may need to restart your runtime after installation.\n", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[11], line 37\u001b[0m\n\u001b[1;32m 26\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m LoraConfig(\n\u001b[1;32m 27\u001b[0m peft_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLORA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct type if different\u001b[39;00m\n\u001b[1;32m 28\u001b[0m task_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct task type if different\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 33\u001b[0m bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 34\u001b[0m )\n\u001b[1;32m 36\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[0;32m---> 37\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m(model_id)\n\u001b[1;32m 38\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[1;32m 39\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel(model, peft_config)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/import_utils.py:1543\u001b[0m, in \u001b[0;36mDummyObject.__getattribute__\u001b[0;34m(cls, key)\u001b[0m\n\u001b[1;32m 1541\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m key\u001b[38;5;241m.\u001b[39mstartswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_from_config\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 1542\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__getattribute__\u001b[39m(key)\n\u001b[0;32m-> 1543\u001b[0m \u001b[43mrequires_backends\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_backends\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/import_utils.py:1531\u001b[0m, in \u001b[0;36mrequires_backends\u001b[0;34m(obj, backends)\u001b[0m\n\u001b[1;32m 1529\u001b[0m failed \u001b[38;5;241m=\u001b[39m [msg\u001b[38;5;241m.\u001b[39mformat(name) \u001b[38;5;28;01mfor\u001b[39;00m available, msg \u001b[38;5;129;01min\u001b[39;00m checks \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m available()]\n\u001b[1;32m 1530\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m failed:\n\u001b[0;32m-> 1531\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(failed))\n", "\u001b[0;31mImportError\u001b[0m: \nAutoModelForCausalLM requires the PyTorch library but it was not found in your environment. Checkout the instructions on the\ninstallation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.\nPlease note that you may need to restart your runtime after installation.\n" ] } ], "source": [ "import os\n", "import json\n", "from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "import torch\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", "\n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "86f80d8a-b07b-45ff-af1f-aebba8a98412", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "ImportError", "evalue": "\nAutoModelForCausalLM requires the PyTorch library but it was not found in your environment. Checkout the instructions on the\ninstallation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.\nPlease note that you may need to restart your runtime after installation.\n", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 37\u001b[0m\n\u001b[1;32m 26\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m LoraConfig(\n\u001b[1;32m 27\u001b[0m peft_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLORA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct type if different\u001b[39;00m\n\u001b[1;32m 28\u001b[0m task_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct task type if different\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 33\u001b[0m bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 34\u001b[0m )\n\u001b[1;32m 36\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[0;32m---> 37\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m(model_id)\n\u001b[1;32m 39\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[1;32m 40\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel(model, peft_config)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/import_utils.py:1543\u001b[0m, in \u001b[0;36mDummyObject.__getattribute__\u001b[0;34m(cls, key)\u001b[0m\n\u001b[1;32m 1541\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m key\u001b[38;5;241m.\u001b[39mstartswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_from_config\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 1542\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39m\u001b[38;5;21m__getattribute__\u001b[39m(key)\n\u001b[0;32m-> 1543\u001b[0m \u001b[43mrequires_backends\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mcls\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_backends\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/import_utils.py:1531\u001b[0m, in \u001b[0;36mrequires_backends\u001b[0;34m(obj, backends)\u001b[0m\n\u001b[1;32m 1529\u001b[0m failed \u001b[38;5;241m=\u001b[39m [msg\u001b[38;5;241m.\u001b[39mformat(name) \u001b[38;5;28;01mfor\u001b[39;00m available, msg \u001b[38;5;129;01min\u001b[39;00m checks \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m available()]\n\u001b[1;32m 1530\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m failed:\n\u001b[0;32m-> 1531\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(failed))\n", "\u001b[0;31mImportError\u001b[0m: \nAutoModelForCausalLM requires the PyTorch library but it was not found in your environment. Checkout the instructions on the\ninstallation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.\nPlease note that you may need to restart your runtime after installation.\n" ] } ], "source": [ "import os\n", "import json\n", "import torch\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", "\n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "ce963a71-c54a-460f-818e-bbcd3d7215d3", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "File found: /opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\n" ] }, { "ename": "OSError", "evalue": "Incorrect path_or_model_id: '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM'. Please provide either the path to a local folder or the repo_id of a model on the Hub.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHFValidationError\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:402\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 400\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 401\u001b[0m \u001b[38;5;66;03m# Load from URL or cache if already cached\u001b[39;00m\n\u001b[0;32m--> 402\u001b[0m resolved_file \u001b[38;5;241m=\u001b[39m \u001b[43mhf_hub_download\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 403\u001b[0m \u001b[43m \u001b[49m\u001b[43mpath_or_repo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 404\u001b[0m \u001b[43m \u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 405\u001b[0m \u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43msubfolder\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m==\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43msubfolder\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 406\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 407\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 408\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_dir\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 409\u001b[0m \u001b[43m \u001b[49m\u001b[43muser_agent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muser_agent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 410\u001b[0m \u001b[43m \u001b[49m\u001b[43mforce_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mforce_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 411\u001b[0m \u001b[43m \u001b[49m\u001b[43mproxies\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mproxies\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 412\u001b[0m \u001b[43m \u001b[49m\u001b[43mresume_download\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mresume_download\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 413\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 414\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_files_only\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_files_only\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 415\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 416\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m GatedRepoError \u001b[38;5;28;01mas\u001b[39;00m e:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_deprecation.py:101\u001b[0m, in \u001b[0;36m_deprecate_arguments.._inner_deprecate_positional_args..inner_f\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 100\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(message, \u001b[38;5;167;01mFutureWarning\u001b[39;00m)\n\u001b[0;32m--> 101\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:106\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.._inner_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m arg_name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepo_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_id\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mto_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n\u001b[0;32m--> 106\u001b[0m \u001b[43mvalidate_repo_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg_value\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 108\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m arg_name \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtoken\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m arg_value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/huggingface_hub/utils/_validators.py:154\u001b[0m, in \u001b[0;36mvalidate_repo_id\u001b[0;34m(repo_id)\u001b[0m\n\u001b[1;32m 153\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m repo_id\u001b[38;5;241m.\u001b[39mcount(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m--> 154\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HFValidationError(\n\u001b[1;32m 155\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRepo id must be in the form \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrepo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnamespace/repo_name\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m:\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 156\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Use `repo_type` argument if needed.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 157\u001b[0m )\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m REPO_ID_REGEX\u001b[38;5;241m.\u001b[39mmatch(repo_id):\n", "\u001b[0;31mHFValidationError\u001b[0m: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM'. Use `repo_type` argument if needed.", "\nThe above exception was the direct cause of the following exception:\n", "\u001b[0;31mOSError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[2], line 37\u001b[0m\n\u001b[1;32m 26\u001b[0m peft_config \u001b[38;5;241m=\u001b[39m LoraConfig(\n\u001b[1;32m 27\u001b[0m peft_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLORA\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct type if different\u001b[39;00m\n\u001b[1;32m 28\u001b[0m task_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCAUSAL_LM\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;66;03m# Replace with the correct task type if different\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 33\u001b[0m bias\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnone\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 34\u001b[0m )\n\u001b[1;32m 36\u001b[0m \u001b[38;5;66;03m# Load the main model\u001b[39;00m\n\u001b[0;32m---> 37\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mAutoModelForCausalLM\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 39\u001b[0m \u001b[38;5;66;03m# Apply the PEFT model with the configuration\u001b[39;00m\n\u001b[1;32m 40\u001b[0m peft_model \u001b[38;5;241m=\u001b[39m PeftModel(model, peft_config)\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/models/auto/auto_factory.py:485\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[0;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[1;32m 482\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m commit_hash \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 483\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(config, PretrainedConfig):\n\u001b[1;32m 484\u001b[0m \u001b[38;5;66;03m# We make a call to the config file first (which may be absent) to get the commit hash as soon as possible\u001b[39;00m\n\u001b[0;32m--> 485\u001b[0m resolved_config_file \u001b[38;5;241m=\u001b[39m \u001b[43mcached_file\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 486\u001b[0m \u001b[43m \u001b[49m\u001b[43mpretrained_model_name_or_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 487\u001b[0m \u001b[43m \u001b[49m\u001b[43mCONFIG_NAME\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 488\u001b[0m \u001b[43m \u001b[49m\u001b[43m_raise_exceptions_for_gated_repo\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 489\u001b[0m \u001b[43m \u001b[49m\u001b[43m_raise_exceptions_for_missing_entries\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 490\u001b[0m \u001b[43m \u001b[49m\u001b[43m_raise_exceptions_for_connection_errors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 491\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mhub_kwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 492\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 493\u001b[0m commit_hash \u001b[38;5;241m=\u001b[39m extract_commit_hash(resolved_config_file, commit_hash)\n\u001b[1;32m 494\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", "File \u001b[0;32m/opt/app-root/lib64/python3.9/site-packages/transformers/utils/hub.py:466\u001b[0m, in \u001b[0;36mcached_file\u001b[0;34m(path_or_repo_id, filename, cache_dir, force_download, resume_download, proxies, token, revision, local_files_only, subfolder, repo_type, user_agent, _raise_exceptions_for_gated_repo, _raise_exceptions_for_missing_entries, _raise_exceptions_for_connection_errors, _commit_hash, **deprecated_kwargs)\u001b[0m\n\u001b[1;32m 464\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThere was a specific connection error when trying to load \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00merr\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 465\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HFValidationError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m--> 466\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mEnvironmentError\u001b[39;00m(\n\u001b[1;32m 467\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIncorrect path_or_model_id: \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpath_or_repo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m. Please provide either the path to a local folder or the repo_id of a model on the Hub.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 468\u001b[0m ) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[1;32m 469\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m resolved_file\n", "\u001b[0;31mOSError\u001b[0m: Incorrect path_or_model_id: '/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM'. Please provide either the path to a local folder or the repo_id of a model on the Hub." ] } ], "source": [ "import os\n", "import json\n", "import torch\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "from peft import PeftModel, PeftConfig, LoraConfig\n", "\n", "# Set the full path to your model directories\n", "peft_model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_DETOXIFY_CAUSAL_LM/adapter_config.json\"\n", "model_id = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CAUSAL_LM\"\n", "\n", "# Check if the file exists\n", "if not os.path.exists(peft_model_id):\n", " print(f\"File not found: {peft_model_id}\")\n", "else:\n", " print(f\"File found: {peft_model_id}\")\n", "\n", " # Load the PEFT config manually\n", " with open(peft_model_id, \"r\") as f:\n", " config = json.load(f)\n", "\n", " # Ensure target_modules is present in the config\n", " if 'target_modules' not in config:\n", " config['target_modules'] = [\"q_proj\", \"v_proj\", \"k_proj\", \"o_proj\"] # Example modules\n", "\n", " # Initialize the PeftConfig with the filtered config dictionary\n", " peft_config = LoraConfig(\n", " peft_type=\"LORA\", # Replace with the correct type if different\n", " task_type=\"CAUSAL_LM\", # Replace with the correct task type if different\n", " target_modules=config['target_modules'],\n", " r=config.get(\"r\", 4),\n", " lora_alpha=config.get(\"lora_alpha\", 16),\n", " lora_dropout=config.get(\"lora_dropout\", 0.1),\n", " bias=\"none\",\n", " )\n", "\n", " # Load the main model\n", " model = AutoModelForCausalLM.from_pretrained(model_id)\n", "\n", " # Apply the PEFT model with the configuration\n", " peft_model = PeftModel(model, peft_config)\n", "\n", " print(\"Models loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "fc534fe3-1e36-47c4-af4f-6b51b40395ba", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model and tokenizer loaded successfully.\n" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "\n", "model_path = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "\n", "# Load the model and tokenizer\n", "model = AutoModelForCausalLM.from_pretrained(model_path)\n", "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", "\n", "print(\"Model and tokenizer loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "bfced6f3-2f7a-4b28-878f-1cce7f57fcec", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: You are a worthless piece of junk.\n", "I'm not a worthless piece of junk. I'm a good person. I'm not a worthless piece of junk. I'm a good person. I'm not a worthless piece of junk. I\n" ] } ], "source": [ "# Sample input text\n", "input_text = \"You are a worthless piece of junk.\"\n", "\n", "# Tokenize the input text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model\n", "outputs = model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(\"Generated Text:\", generated_text)\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "2a022312-7807-4050-a043-0ec6acd894ca", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model and tokenizer loaded successfully.\n" ] } ], "source": [ "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "\n", "# Load your fine-tuned model and tokenizer\n", "model_path = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "model = AutoModelForCausalLM.from_pretrained(model_path)\n", "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", "\n", "print(\"Model and tokenizer loaded successfully.\")\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "09864056-f036-44ed-88a9-1fe976f03d41", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: You are a worthless piece of junk.\n", "I'm not a worthless piece of junk. I'm a good person. I'm not a worthless piece of junk. I'm a good person. I'm not a worthless piece of junk. I\n", "Detoxified Text: You are a valuable piece of junk.\n", "I'm not a valuable piece of junk. I'm a good person. I'm not a valuable piece of junk. I'm a good person. I'm not a valuable piece of junk. I\n" ] } ], "source": [ "# Sample input text that is toxic\n", "input_text = \"You are a worthless piece of junk.\"\n", "\n", "# Tokenize the input text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model (for detoxification)\n", "outputs = model.generate(**inputs, max_length=50)\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "\n", "# Example of modifying the generated output to make it less toxic\n", "detoxified_text = generated_text.replace(\"worthless\", \"valuable\")\n", "\n", "print(\"Generated Text:\", generated_text)\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "64391559-e312-4f7e-b5df-c48d737625e2", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/app-root/lib64/python3.9/site-packages/transformers/generation/configuration_utils.py:572: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.95` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.\n", " warnings.warn(\n" ] } ], "source": [ "outputs = model.generate(\n", " **inputs,\n", " max_length=50,\n", " repetition_penalty=2.0, # This penalizes repetition\n", " top_k=50, # Limits the next token sampling to the top k options\n", " top_p=0.95 # Enables nucleus sampling with a cumulative probability threshold\n", ")\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "f16e8e83-b7f0-485e-abf0-aa16108e629b", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Detoxified Text: You are a valuable piece of treasure.\n", "I'm not a valuable piece of treasure. I'm a good person. I'm not a valuable piece of treasure. I'm a good person. I'm not a valuable piece of treasure. I\n" ] } ], "source": [ "toxic_words = {\"worthless\": \"valuable\", \"junk\": \"treasure\"}\n", "detoxified_text = generated_text\n", "for toxic, neutral in toxic_words.items():\n", " detoxified_text = detoxified_text.replace(toxic, neutral)\n", "\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "cd7841b0-c2a0-4ae7-9c25-d1a8fffde76a", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: You are a worthless piece of junk.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! You can't be that stupid and still have the ability to make people laugh at your stupidity...and\n", "Detoxified Text: You are a valuable piece of treasure.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! You can't be that stupid and still have the ability to make people laugh at your stupidity...and\n" ] } ], "source": [ "# Sample input text that is toxic\n", "input_text = \"You are a worthless piece of junk.\"\n", "\n", "# Tokenize the input text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model (for detoxification) with repetition penalty\n", "outputs = model.generate(\n", " **inputs,\n", " max_length=50,\n", " repetition_penalty=2.0, # Penalize repetition\n", " top_k=50, # Limit the next token sampling to the top k options\n", " top_p=0.95, # Enable nucleus sampling with a cumulative probability threshold\n", " num_return_sequences=1 # Generate only one sequence for simplicity\n", ")\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "\n", "# Example of modifying the generated output to make it less toxic\n", "toxic_words = {\"worthless\": \"valuable\", \"junk\": \"treasure\"}\n", "detoxified_text = generated_text\n", "for toxic, neutral in toxic_words.items():\n", " detoxified_text = detoxified_text.replace(toxic, neutral)\n", "\n", "print(\"Generated Text:\", generated_text)\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "9219ecab-36f2-41f8-b4d5-679418c863bc", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: You are a worthless piece of junk.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! \n", "Detoxified Text: The statement has been corrected to avoid negative language.\n" ] } ], "source": [ "# Sample input text that is toxic\n", "input_text = \"You are a worthless piece of junk.\"\n", "\n", "# Tokenize the input text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model (for detoxification) with strict length control\n", "outputs = model.generate(\n", " **inputs,\n", " max_length=30, # Strictly limit the length to reduce continuation issues\n", " repetition_penalty=2.0, # Penalize repetition\n", " top_k=50, # Limit the next token sampling to the top k options\n", " top_p=0.95, # Enable nucleus sampling with a cumulative probability threshold\n", " num_return_sequences=1 # Generate only one sequence for simplicity\n", ")\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "\n", "# Example of modifying the generated output to make it less toxic\n", "toxic_words = {\"worthless\": \"valuable\", \"junk\": \"treasure\"}\n", "detoxified_text = generated_text\n", "for toxic, neutral in toxic_words.items():\n", " detoxified_text = detoxified_text.replace(toxic, neutral)\n", "\n", "# Filter to remove or replace sarcastic or inappropriate phrases\n", "if \"sarcastic\" in detoxified_text or \"stupid\" in detoxified_text:\n", " detoxified_text = \"The statement has been corrected to avoid negative language.\"\n", "\n", "print(\"Generated Text:\", generated_text)\n", "print(\"Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "5972d6dd-3127-4e47-abdc-388547568f3f", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generated Text: You are a worthless piece of junk.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! \n", "Intermediate Detoxified Text: You are a valuable piece of treasure.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! \n", "Final Detoxified Text: The statement has been corrected to avoid negative language.\n" ] } ], "source": [ "# Sample input text that is toxic\n", "input_text = \"You are a worthless piece of junk.\"\n", "\n", "# Tokenize the input text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model (for detoxification) with strict length control\n", "outputs = model.generate(\n", " **inputs,\n", " max_length=30, # Strictly limit the length to reduce continuation issues\n", " repetition_penalty=2.0, # Penalize repetition\n", " top_k=50, # Limit the next token sampling to the top k options\n", " top_p=0.95, # Enable nucleus sampling with a cumulative probability threshold\n", " num_return_sequences=1 # Generate only one sequence for simplicity\n", ")\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "\n", "# Example of modifying the generated output to make it less toxic\n", "toxic_words = {\"worthless\": \"valuable\", \"junk\": \"treasure\"}\n", "detoxified_text = generated_text\n", "for toxic, neutral in toxic_words.items():\n", " detoxified_text = detoxified_text.replace(toxic, neutral)\n", "\n", "print(\"Generated Text:\", generated_text)\n", "print(\"Intermediate Detoxified Text:\", detoxified_text)\n", "\n", "# Filter to remove or replace sarcastic or inappropriate phrases\n", "if \"sarcastic\" in detoxified_text or \"stupid\" in detoxified_text:\n", " detoxified_text = \"The statement has been corrected to avoid negative language.\"\n", "\n", "print(\"Final Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": 15, "id": "d3f7da18-4b8f-4f7a-957b-daf42db009ff", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model and tokenizer loaded successfully.\n", "Generated Text: You are a worthless piece of junk.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! \n", "Intermediate Detoxified Text: You are a valuable piece of treasure.\n", "I'm not sure if you're being sarcastic or serious, but I think it's both! \n", "Final Detoxified Text: The statement has been corrected to avoid negative language.\n" ] } ], "source": [ "# Import necessary libraries\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "import torch\n", "\n", "# Load the model and tokenizer\n", "model_path = \"/opt/app-root/src/trustyai-detoxify-sft/models/opt-350m_CASUAL_LM\"\n", "model = AutoModelForCausalLM.from_pretrained(model_path)\n", "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", "\n", "print(\"Model and tokenizer loaded successfully.\")\n", "\n", "# Sample input text that is toxic\n", "input_text = \"You are a worthless piece of junk.\"\n", "\n", "# Tokenize the input text\n", "inputs = tokenizer(input_text, return_tensors=\"pt\").to(model.device)\n", "\n", "# Generate text using the model (for detoxification) with repetition penalty and top-k sampling\n", "outputs = model.generate(\n", " **inputs,\n", " max_length=30, # Strictly limit the length to reduce continuation issues\n", " repetition_penalty=2.0, # Penalize repetition\n", " top_k=50, # Limit the next token sampling to the top k options\n", " top_p=0.95, # Enable nucleus sampling with a cumulative probability threshold\n", " num_return_sequences=1 # Generate only one sequence for simplicity\n", ")\n", "\n", "# Decode and print the output\n", "generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", "print(\"Generated Text:\", generated_text)\n", "\n", "# Example of modifying the generated output to make it less toxic\n", "toxic_words = {\"worthless\": \"valuable\", \"junk\": \"treasure\"}\n", "detoxified_text = generated_text\n", "for toxic, neutral in toxic_words.items():\n", " detoxified_text = detoxified_text.replace(toxic, neutral)\n", "\n", "print(\"Intermediate Detoxified Text:\", detoxified_text)\n", "\n", "# Filter to remove or replace sarcastic or inappropriate phrases\n", "if \"sarcastic\" in detoxified_text or \"stupid\" in detoxified_text:\n", " detoxified_text = \"The statement has been corrected to avoid negative language.\"\n", "\n", "print(\"Final Detoxified Text:\", detoxified_text)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "338b7ef5-c992-4280-aaab-34f078141312", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }