这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions examples/conditional_generation/multitask_prompt_tuning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
},
"outputs": [],
"source": [
"import torch\n",
"from datasets import load_dataset\n",
"from transformers import set_seed, AutoModelForSeq2SeqLM, AutoTokenizer\n",
"from peft import get_peft_model, MultitaskPromptTuningConfig, TaskType, MultitaskPromptTuningInit\n",
"\n",
"set_seed(42)\n",
"\n",
"device = torch.accelerator.current_accelerator().type if hasattr(torch, \"accelerator\") else \"cuda\"\n",
"model_name = \"google/flan-t5-base\"\n",
"\n",
"peft_config = MultitaskPromptTuningConfig(\n",
Expand All @@ -31,18 +32,18 @@
"model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
"model = get_peft_model(model, peft_config)\n",
"\n",
"model = model.cuda()\n",
"model = model.to(device)\n",
"\n",
"\n",
"def send_to_device(batch):\n",
" for i in batch:\n",
" batch[i] = batch[i].cuda()\n",
" batch[i] = batch[i].to(device)\n",
" return batch"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "eb112bc1-ffaf-49fa-a216-0d601ec304ee",
"metadata": {
"tags": []
Expand Down Expand Up @@ -86,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "e5a16ec4-8fef-4ba9-95b6-a661eb51e50c",
"metadata": {
"tags": []
Expand Down Expand Up @@ -159,7 +160,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "cceecc94-f43a-4f62-8d45-926f2f02f36d",
"metadata": {
"tags": []
Expand Down Expand Up @@ -293,7 +294,7 @@
" num_tasks=1,\n",
" task_type=TaskType.SEQ_2_SEQ_LM,\n",
" prompt_tuning_init=MultitaskPromptTuningInit.EXACT_SOURCE_TASK,\n",
" prompt_tuning_init_state_dict_path=\"checkpoints_source/50000/adapter_model.bin\",\n",
" prompt_tuning_init_state_dict_path=\"checkpoints_source/50000/adapter_model.safetensors\",\n",
" num_virtual_tokens=50,\n",
" num_transformer_submodules=1,\n",
")\n",
Expand All @@ -302,7 +303,7 @@
"model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
"model = get_peft_model(model, peft_config)\n",
"\n",
"model = model.cuda()"
"model = model.to(device)"
]
},
{
Expand Down Expand Up @@ -360,8 +361,9 @@
"source": [
"# load last checkpoint for now\n",
"from peft import set_peft_model_state_dict\n",
"from safetensors.torch import load_file\n",
"\n",
"sd_6000 = torch.load(\"checkpoints_target/6000/adapter_model.bin\")\n",
"sd_6000 = load_file(\"checkpoints_target/6000/adapter_model.safetensors\")\n",
"set_peft_model_state_dict(model, sd_6000)\n",
"\n",
"# evaluate val\n",
Expand All @@ -382,6 +384,22 @@
"f1 = {f1}\"\"\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d18325c-9607-4cb5-a5b0-5b44dfee2a75",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "43988e92-af42-45cb-8bca-f19c193ad04f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -400,7 +418,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.13"
}
},
"nbformat": 4,
Expand Down
33 changes: 17 additions & 16 deletions examples/conditional_generation/peft_adalora_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

os.environ["TOKENIZERS_PARALLELISM"] = "false"

device = "cuda"
device = torch.accelerator.current_accelerator().type if hasattr(torch, "accelerator") else "cuda"
model_name_or_path = "facebook/bart-base"
tokenizer_name_or_path = "facebook/bart-base"

Expand All @@ -24,6 +24,20 @@
batch_size = 8


# loading dataset
dataset = load_dataset("financial_phrasebank", "sentences_allagree")
dataset = dataset["train"].train_test_split(test_size=0.1)
dataset["validation"] = dataset["test"]
del dataset["test"]

classes = dataset["train"].features["label"].names
dataset = dataset.map(
lambda x: {"text_label": [classes[label] for label in x["label"]]},
batched=True,
num_proc=1,
)


# creating model
peft_config = AdaLoraConfig(
init_r=12,
Expand All @@ -37,27 +51,14 @@
lora_dropout=0.1,
task_type=TaskType.SEQ_2_SEQ_LM,
inference_mode=False,
total_step=len(dataset["train"]) * num_epochs,
)

model = AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)
model = get_peft_model(model, peft_config)
model.print_trainable_parameters()


# loading dataset
dataset = load_dataset("financial_phrasebank", "sentences_allagree")
dataset = dataset["train"].train_test_split(test_size=0.1)
dataset["validation"] = dataset["test"]
del dataset["test"]

classes = dataset["train"].features["label"].names
dataset = dataset.map(
lambda x: {"text_label": [classes[label] for label in x["label"]]},
batched=True,
num_proc=1,
)


# data preprocessing
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)

Expand Down Expand Up @@ -159,7 +160,7 @@ def preprocess_function(examples):
model.save_pretrained(peft_model_id)


ckpt = f"{peft_model_id}/adapter_model.bin"
ckpt = f"{peft_model_id}/adapter_model.safetensors"
# get_ipython().system('du -h $ckpt')


Expand Down
Loading
Loading