{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# only for WSL\n", "import os \n", "os.environ[\"PATH\"] = f\"{os.environ['PATH']}:/usr/local/cuda-12.0/bin\"\n", "# os.environ['LD_LIBRARY_PATH'] = \"/usr/lib/wsl/lib:/usr/local/cuda/lib64\"\n", "os.environ['LD_LIBRARY_PATH'] = \"/usr/local/cuda-12.0/lib64\"\n", "os.environ[\"BNB_CUDA_VERSION\"]=\"120\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/cuda_setup/main.py:106: UserWarning: \n", "\n", "================================================================================\n", "WARNING: Manual override via BNB_CUDA_VERSION env variable detected!\n", "BNB_CUDA_VERSION=XXX can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.\n", "If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=\n", "If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH\n", "For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: dict:\n", " len_ids = [len(feature[\"input_ids\"]) for feature in features]\n", " longest = max(len_ids)\n", " input_ids = []\n", " labels_list = []\n", " for ids_l, feature in sorted(zip(len_ids, features), key=lambda x: -x[0]):\n", " ids = feature[\"input_ids\"]\n", " seq_len = feature[\"seq_len\"]\n", " labels = (\n", " [tokenizer.pad_token_id] * (seq_len - 1) + ids[(seq_len - 1) :] + [tokenizer.pad_token_id] * (longest - ids_l)\n", " )\n", " ids = ids + [tokenizer.pad_token_id] * (longest - ids_l)\n", " _ids = torch.LongTensor(ids)\n", " labels_list.append(torch.LongTensor(labels))\n", " input_ids.append(_ids)\n", " input_ids = torch.stack(input_ids)\n", " labels = torch.stack(labels_list)\n", " return {\n", " \"input_ids\": input_ids,\n", " \"labels\": labels,\n", " }\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from torch.utils.tensorboard import SummaryWriter\n", "from transformers.integrations import TensorBoardCallback" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "You are adding a to the callbacks of this Trainer, but there is already one. The currentlist of callbacks is\n", ":DefaultFlowCallback\n", "TensorBoardCallback\n", "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n" ] }, { "data": { "text/html": [ "\n", "
\n", " \n", " \n", " [3838/3838 17:14:17, Epoch 1/2]\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
StepTraining LossValidation Loss
5004.3378000.005839
10000.0056000.004147
15000.0045000.003341
20000.0038000.003795
25000.0032000.002809
30000.0029000.002647
35000.0029000.002470

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n", "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/autograd/_functions.py:322: UserWarning: MatMul8bitLt: inputs will be cast from torch.float32 to float16 during quantization\n", " warnings.warn(f\"MatMul8bitLt: inputs will be cast from {A.dtype} to float16 during quantization\")\n" ] } ], "source": [ "# Train\n", "writer = SummaryWriter()\n", "trainer = ModifiedTrainer(\n", " model=model, \n", " args=training_args, # Trainer args\n", " train_dataset=dataset[\"train\"], # Training set\n", " eval_dataset=dataset[\"test\"], # Testing set\n", " data_collator=data_collator, # Data Collator\n", " callbacks=[TensorBoardCallback(writer)],\n", ")\n", "trainer.train()\n", "writer.close()\n", "# save model\n", "model.save_pretrained(training_args.output_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## GPU Trining MEM: 46.5%\n", "## GPU Evaling MEM: 96.9%" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Push to hub" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/oliverwang15/miniconda3/lib/python3.10/site-packages/bitsandbytes-0.41.1-py3.10.egg/bitsandbytes/cuda_setup/main.py:106: UserWarning: \n", "\n", "================================================================================\n", "WARNING: Manual override via BNB_CUDA_VERSION env variable detected!\n", "BNB_CUDA_VERSION=XXX can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.\n", "If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=\n", "If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH\n", "For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: