{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -Uqq transformers datasets evaluate bitsandbytes peft accelerate scipy einops trl","metadata":{"execution":{"iopub.status.busy":"2024-08-12T14:49:38.260628Z","iopub.execute_input":"2024-08-12T14:49:38.261217Z","iopub.status.idle":"2024-08-12T14:50:21.011903Z","shell.execute_reply.started":"2024-08-12T14:49:38.261184Z","shell.execute_reply":"2024-08-12T14:50:21.010797Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"\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.\ncuml 24.6.1 requires cupy-cuda11x>=12.0.0, which is not installed.\nkfp 2.5.0 requires google-cloud-storage<3,>=2.2.1, but you have google-cloud-storage 1.44.0 which is incompatible.\nlibpysal 4.9.2 requires packaging>=22, but you have packaging 21.3 which is incompatible.\nlibpysal 4.9.2 requires shapely>=2.0.1, but you have shapely 1.8.5.post1 which is incompatible.\nmomepy 0.7.2 requires shapely>=2, but you have shapely 1.8.5.post1 which is incompatible.\npointpats 2.5.0 requires shapely>=2, but you have shapely 1.8.5.post1 which is incompatible.\nspaghetti 1.7.6 requires shapely>=2.0.1, but you have shapely 1.8.5.post1 which is incompatible.\nspopt 0.6.1 requires shapely>=2.0.1, but you have shapely 1.8.5.post1 which is incompatible.\nydata-profiling 4.6.4 requires numpy<1.26,>=1.16.0, but you have numpy 1.26.4 which is incompatible.\nydata-profiling 4.6.4 requires scipy<1.12,>=1.4.1, but you have scipy 1.14.0 which is incompatible.\u001b[0m\u001b[31m\n\u001b[0m","output_type":"stream"}]},{"cell_type":"code","source":"import torch\nimport wandb\nimport evaluate\nimport datasets\nimport peft\nfrom peft import AutoPeftModelForCausalLM\nfrom transformers import AutoTokenizer, pipeline\nfrom huggingface_hub import login\nfrom kaggle_secrets import UserSecretsClient\nfrom transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\nfrom trl import setup_chat_format","metadata":{"execution":{"iopub.status.busy":"2024-08-12T14:50:25.811088Z","iopub.execute_input":"2024-08-12T14:50:25.812019Z","iopub.status.idle":"2024-08-12T14:50:45.078565Z","shell.execute_reply.started":"2024-08-12T14:50:25.811981Z","shell.execute_reply":"2024-08-12T14:50:45.077727Z"},"trusted":true},"execution_count":2,"outputs":[{"name":"stderr","text":"2024-08-12 14:50:34.357616: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n2024-08-12 14:50:34.357778: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n2024-08-12 14:50:34.484438: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n","output_type":"stream"}]},{"cell_type":"code","source":"user_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"mlops_huggingface_token\")\nsecret_value_1 = user_secrets.get_secret(\"wandb_key\")\n\nwandb.login(key = secret_value_1)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T14:50:45.080592Z","iopub.execute_input":"2024-08-12T14:50:45.081183Z","iopub.status.idle":"2024-08-12T14:50:47.425904Z","shell.execute_reply.started":"2024-08-12T14:50:45.081141Z","shell.execute_reply":"2024-08-12T14:50:47.424945Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stderr","text":"\u001b[34m\u001b[1mwandb\u001b[0m: W&B API key is configured. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m If you're specifying your api key in code, ensure this code is not shared publicly.\n\u001b[34m\u001b[1mwandb\u001b[0m: \u001b[33mWARNING\u001b[0m Consider setting the WANDB_API_KEY environment variable, or running `wandb login` from the command line.\n\u001b[34m\u001b[1mwandb\u001b[0m: Appending key for api.wandb.ai to your netrc file: /root/.netrc\n","output_type":"stream"},{"execution_count":3,"output_type":"execute_result","data":{"text/plain":"True"},"metadata":{}}]},{"cell_type":"code","source":"login(token = secret_value_0)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T14:50:47.427066Z","iopub.execute_input":"2024-08-12T14:50:47.427802Z","iopub.status.idle":"2024-08-12T14:50:47.556901Z","shell.execute_reply.started":"2024-08-12T14:50:47.427771Z","shell.execute_reply":"2024-08-12T14:50:47.555544Z"},"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.\nToken is valid (permission: fineGrained).\nYour token has been saved to /root/.cache/huggingface/token\nLogin successful\n","output_type":"stream"}]},{"cell_type":"markdown","source":"#### Download model","metadata":{}},{"cell_type":"code","source":"base_model = 'microsoft/phi-2'\npeft_model = 'bisoye/phi-2-for-mental-health-2'","metadata":{"execution":{"iopub.status.busy":"2024-08-12T14:50:47.559103Z","iopub.execute_input":"2024-08-12T14:50:47.559939Z","iopub.status.idle":"2024-08-12T14:50:47.645370Z","shell.execute_reply.started":"2024-08-12T14:50:47.559900Z","shell.execute_reply":"2024-08-12T14:50:47.644355Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"# Load base model\nmodel = AutoModelForCausalLM.from_pretrained(\n base_model,\n device_map=\"auto\",\n torch_dtype=torch.bfloat16,\n quantization_config=bnb_config\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T14:13:24.186945Z","iopub.execute_input":"2024-08-12T14:13:24.187935Z","iopub.status.idle":"2024-08-12T14:16:42.905124Z","shell.execute_reply.started":"2024-08-12T14:13:24.187886Z","shell.execute_reply":"2024-08-12T14:16:42.904179Z"},"trusted":true},"execution_count":9,"outputs":[{"output_type":"display_data","data":{"text/plain":"config.json: 0%| | 0.00/735 [00:00