UCDP-AEC

Models and data for the UCDP Abstractive Event analysis Corpus paper
git clone https://esimon.eu/repos/UCDP-AEC.git
Log | Files | Refs | README | LICENSE

Finetune_llms.ipynb (8751B)


      1 {
      2  "cells": [
      3   {
      4    "cell_type": "code",
      5    "execution_count": null,
      6    "id": "63cca615-498a-492f-834b-715d09a2c26b",
      7    "metadata": {},
      8    "outputs": [],
      9    "source": [
     10     "from unsloth import FastLanguageModel\n",
     11     "import torch\n",
     12     "max_seq_length = 2048 \n",
     13     "dtype =  torch.float16 \n",
     14     "load_in_4bit = False\n",
     15     "r_val = 128\n",
     16     "model_name =\"unsloth/Llama-3.2-3B-Instruct\"\n",
     17     "model_save = \"Llama-3.2-3B-Instruct\"\n",
     18     "prompt = 2"
     19    ]
     20   },
     21   {
     22    "cell_type": "code",
     23    "execution_count": null,
     24    "id": "c62b5526-6278-4e48-9c11-dc3fff15d716",
     25    "metadata": {},
     26    "outputs": [],
     27    "source": [
     28     "model, tokenizer = FastLanguageModel.from_pretrained(\n",
     29     "    model_name = model_name,\n",
     30     "    max_seq_length = max_seq_length,\n",
     31     "    dtype = dtype,\n",
     32     "    load_in_4bit = False,\n",
     33     "    # token = \"hf_...\", # use one if using gated models like meta-llama/Llama-2-7b-hf\n",
     34     ")"
     35    ]
     36   },
     37   {
     38    "cell_type": "code",
     39    "execution_count": null,
     40    "id": "e07b7f91-4c90-4ac3-9391-653b579519a9",
     41    "metadata": {},
     42    "outputs": [],
     43    "source": [
     44     "#model = FastLanguageModel.for_training(model)\n",
     45     "model = FastLanguageModel.get_peft_model(\n",
     46     "    model,\n",
     47     "    r = r_val, \n",
     48     "    target_modules = [\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n",
     49     "                      \"gate_proj\", \"up_proj\", \"down_proj\",],\n",
     50     "    lora_alpha = r_val,\n",
     51     "    lora_dropout = 0, \n",
     52     "    bias = \"none\",\n",
     53     "    use_gradient_checkpointing = \"unsloth\",\n",
     54     "    random_state = 3407,\n",
     55     "    use_rslora = False,\n",
     56     "    loftq_config = None,\n",
     57     ")\n"
     58    ]
     59   },
     60   {
     61    "cell_type": "code",
     62    "execution_count": null,
     63    "id": "5bd42ff4-2562-4451-9304-60314cd07b8f",
     64    "metadata": {},
     65    "outputs": [],
     66    "source": [
     67     "#alpaca_prompt = \"\"\"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n",
     68     "alpaca_prompt = \"\"\"\n",
     69     "### Instruction:\n",
     70     "{}\n",
     71     "\n",
     72     "### Input:\n",
     73     "{}\n",
     74     "\n",
     75     "### Response:\n",
     76     "{}\"\"\"\n",
     77     "\n",
     78     "EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN\n",
     79     "def formatting_prompts_func(examples):\n",
     80     "    instructions = examples[\"instruction\"]\n",
     81     "    inputs       = examples[\"input\"]\n",
     82     "    outputs      = examples[\"output\"]\n",
     83     "    texts = []\n",
     84     "    for instruction, input, output in zip(instructions, inputs, outputs):\n",
     85     "        # Must add EOS_TOKEN, otherwise your generation will go on forever!\n",
     86     "        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN\n",
     87     "        texts.append(text)\n",
     88     "    return { \"text\" : texts, }\n",
     89     "pass"
     90    ]
     91   },
     92   {
     93    "cell_type": "code",
     94    "execution_count": null,
     95    "id": "1df8193a-d871-4c37-ae2a-fbf4a313508e",
     96    "metadata": {},
     97    "outputs": [],
     98    "source": [
     99     "from datasets import load_dataset\n",
    100     "#dataset = load_dataset(\"yahma/alpaca-cleaned\", split = \"train\")\n",
    101     "dataset = load_dataset(\"json\", data_files=\"/cluster/work/users/anonymous/new_jsons_prompt/train_data_json_prompt\"+str(prompt)+\".json\")\n",
    102     "dataset = dataset[\"train\"].map(formatting_prompts_func, batched = True,)\n"
    103    ]
    104   },
    105   {
    106    "cell_type": "code",
    107    "execution_count": null,
    108    "id": "19fc700c-5c02-4eaf-b131-59250fbdc80f",
    109    "metadata": {},
    110    "outputs": [],
    111    "source": [
    112     "from trl import SFTTrainer\n",
    113     "from transformers import TrainingArguments\n",
    114     "from unsloth import is_bfloat16_supported\n",
    115     "\n",
    116     "trainer = SFTTrainer(\n",
    117     "    model = model,\n",
    118     "    tokenizer = tokenizer,\n",
    119     "    train_dataset = dataset,\n",
    120     "    dataset_text_field = \"text\",\n",
    121     "    max_seq_length = max_seq_length,\n",
    122     "    dataset_num_proc = 2,\n",
    123     "    packing = False, # Can make training 5x faster for short sequences.\n",
    124     "    args = TrainingArguments(\n",
    125     "        per_device_train_batch_size = 4,\n",
    126     "        gradient_accumulation_steps = 8,\n",
    127     "        warmup_steps = 10,\n",
    128     "        # num_train_epochs = 1, # Set this for 1 full training run.\n",
    129     "        max_steps = 900,\n",
    130     "        learning_rate = 2e-4,\n",
    131     "        fp16 = True, #not is_bfloat16_supported(),\n",
    132     "        bf16 = False, #is_bfloat16_supported(),\n",
    133     "        logging_steps = 1,\n",
    134     "        optim = \"adamw_8bit\",\n",
    135     "        weight_decay = 0.01,\n",
    136     "        lr_scheduler_type = \"linear\",\n",
    137     "        seed = 3407,\n",
    138     "        output_dir = \"/cluster/work/users/anonymous/finetuneEE_US4B\",\n",
    139     "        report_to = \"none\", # Use this for WandB etc\n",
    140     "    ),\n",
    141     ")"
    142    ]
    143   },
    144   {
    145    "cell_type": "code",
    146    "execution_count": null,
    147    "id": "55f736a4-a397-40e3-8222-18e25b5f4b7b",
    148    "metadata": {},
    149    "outputs": [],
    150    "source": [
    151     "gpu_stats = torch.cuda.get_device_properties(0)\n",
    152     "start_gpu_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)\n",
    153     "max_memory = round(gpu_stats.total_memory / 1024 / 1024 / 1024, 3)\n",
    154     "print(f\"GPU = {gpu_stats.name}. Max memory = {max_memory} GB.\")\n",
    155     "print(f\"{start_gpu_memory} GB of memory reserved.\")\n",
    156     "\n",
    157     "\n",
    158     "trainer_stats = trainer.train()\n"
    159    ]
    160   },
    161   {
    162    "cell_type": "code",
    163    "execution_count": null,
    164    "id": "a0e4e452-1541-47a9-a9e5-98f10de79e3c",
    165    "metadata": {},
    166    "outputs": [],
    167    "source": [
    168     "FastLanguageModel.for_inference(model) # Enable native 2x faster inference\n",
    169     "inputs = tokenizer(\n",
    170     "[\n",
    171     "    alpaca_prompt.format(\n",
    172     "        \"Extract the information in JSONL format for the following source_text:\", # instruction\n",
    173     "        \"DEN - Separate bomb attacks by Al-Qaeda suspects on Saturday killed a senior army officer and three soldiers across Yemen\\'s restive south and southeast, military officials said. A bomb planted by Al-Qaeda on a road linking the towns of Seiyun and Shibam (in the southeastern province of Hadramawt) exploded when an army vehicle passed, killing three soldiers and wounding six others, a military official told AFP.The official added that two suspected Al-Qaeda members were arrested at a checkpoint at the entrance to Seiyun just hours before the attack. Hadramawt has been the scene of frequent attacks on the army.On August 17, six Al-Qaeda suspects and three soldiers were killed in clashes in the restive province where the army has boosted its deployment. In the main southern city of Aden, meanwhile, a senior Yemeni army officer was killed on Saturday when a bomb planted in his vehicle exploded, another military official said, also blaming Al-Qaeda. A bomb exploded in the car of General Ahmed Mohammed Saleh al-Omari, logistics and supplies officer of the third military region... wounding him and his son in Aden\\'s Al-Mansura district, the source said.Both were hospitalised but Omari later died of his wounds, the official and a medical source said. Yemeni authorities blame Al-Qaeda in the Arabian Peninsula, which has been branded by Washington as the extremist network\\'s deadliest franchise, for most attacks on members of the security forces.In late April, the army launched a ground offensive against AQAP in Shabwa and nearby Abyan provinces, both in the south.The group is active across several parts of Yemen, having exploited the collapse of central authority during a 2011 uprising that ousted veteran president Ali Abdullah Saleh.\", # input\n",
    174     "        \"\", # output - leave this blank for generation!\n",
    175     "    )\n",
    176     "], return_tensors = \"pt\").to(\"cuda\")\n",
    177     "\n",
    178     "outputs = model.generate(**inputs, max_new_tokens = 200, use_cache = True)\n",
    179     "outtext = tokenizer.batch_decode(outputs)\n",
    180     "\n",
    181     "print(outtext)\n",
    182     "\n",
    183     "model.save_pretrained(\"NProStrt_\"+str(prompt)+\"_\"+model_save+str(r_val)) # Local saving\n",
    184     "tokenizer.save_pretrained(\"NProStrt_\"+str(prompt)+\"_\"+model_save+str(r_val))\n",
    185     "model.save_pretrained_merged(\"NProStrt_\"+str(prompt)+\"_\"+\"Merged_\"+model_save+str(r_val), tokenizer, save_method = \"merged_16bit\",)"
    186    ]
    187   }
    188  ],
    189  "metadata": {
    190   "kernelspec": {
    191    "display_name": "Python 3 (ipykernel)",
    192    "language": "python",
    193    "name": "python3"
    194   },
    195   "language_info": {
    196    "codemirror_mode": {
    197     "name": "ipython",
    198     "version": 3
    199    },
    200    "file_extension": ".py",
    201    "mimetype": "text/x-python",
    202    "name": "python",
    203    "nbconvert_exporter": "python",
    204    "pygments_lexer": "ipython3",
    205    "version": "3.12.9"
    206   }
    207  },
    208  "nbformat": 4,
    209  "nbformat_minor": 5
    210 }