Точная настройка модели LlaMA 7B с использованием Pytorch Lightning Framework
Need Expert help to solve this issue. LLaMA 7B model for sentiment classification with instructional Finetuning.
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from transformers import LlamaTokenizer, LlamaForCausalLM, AdamW
from pytorch_lightning import LightningModule, Trainer, seed_everything
from datasets import load_dataset
import pandas as pd
import json
seed_everything(42)
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class SentimentDataset(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
item = self.data[idx]
prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. # noqa: E501
### Instruction:
{item["instruction"]}
### Input:
{item["input"]}
### Response:
{item["output"]}"""
return prompt
class SentimentClassifier(LightningModule):
def __init__(self, base_model, learning_rate=2e-5):
super().__init__()
self.base_model = base_model
self.tokenizer = LlamaTokenizer.from_pretrained(base_model)
self.classifier = nn.Linear(self.base_model.config.hidden_size, 3)
self.learning_rate = learning_rate
def forward(self, input_ids, attention_mask):
outputs = self.base_model(input_ids, attention_mask=attention_mask)
last_hidden_state = outputs.last_hidden_state
logits = self.classifier(last_hidden_state[:, 0, :])
return logits
def training_step(self, batch, batch_idx):
input_ids = batch["input_ids"].to(self.device)
attention_mask = batch["attention_mask"].to(self.device)
labels = batch["labels"].to(self.device)
logits = self(input_ids, attention_mask)
loss = nn.CrossEntropyLoss()(logits, labels)
self.log("train_loss", loss)
return loss
def validation_step(self, batch, batch_idx):
input_ids = batch["input_ids"].to(self.device)
attention_mask = batch["attention_mask"].to(self.device)
labels = batch["labels"].to(self.device)
logits = self(input_ids, attention_mask)
loss = nn.CrossEntropyLoss()(logits, labels)
self.log("val_loss", loss)
def configure_optimizers(self):
optimizer = AdamW(self.parameters(), lr=self.learning_rate)
return optimizer
def collate_fn(self, batch):
encoding = self.tokenizer.batch_encode_plus(
batch,
padding="longest",
truncation=True,
return_tensors="pt"
)
return {
"input_ids": encoding["input_ids"].squeeze(),
"attention_mask": encoding["attention_mask"].squeeze(),
"labels": encoding["input_ids"].squeeze()
}
train = load_dataset("json", data_files="alpaca-bitcoin-sentiment-dataset_train.json")
validation = load_dataset("json", data_files="alpaca-bitcoin-sentiment-dataset_test.json")
train_dataset = SentimentDataset(train)
val_dataset = SentimentDataset(validation)
LEARNING_RATE = 2e-5
BATCH_SIZE = 8
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE)
val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE)
from peft import (
LoraConfig,
get_peft_model,
get_peft_model_state_dict,
prepare_model_for_int8_training,
)
BASE_MODEL = "decapoda-research/llama-7b-hf"
model = LlamaForCausalLM.from_pretrained(
BASE_MODEL,
load_in_8bit=True,
torch_dtype=torch.float16,
device_map=0,
)
tokenizer = LlamaTokenizer.from_pretrained(BASE_MODEL)
tokenizer.pad_token_id = (
0 # unk. we want this to be different from the eos token
)
tokenizer.padding_side = "left"
LORA_R = 8
LORA_ALPHA = 16
LORA_DROPOUT= 0.05
LORA_TARGET_MODULES = [
"q_proj",
"v_proj",
]
model = prepare_model_for_int8_training(model)
config = LoraConfig(
r=LORA_R,
lora_alpha=LORA_ALPHA,
target_modules=LORA_TARGET_MODULES,
lora_dropout=LORA_DROPOUT,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, config)
model.print_trainable_parameters()
Вывод: обучаемые параметры: 4194304 || все параметры: 6742609920 || обучаемость%: 0,06220594176090199
model = SentimentClassifier(model, learning_rate=LEARNING_RATE)
HFValidationError: идентификатор репо должен содержать буквенно-цифровые символы, иначе «-», «_», «.», «--» и «..» запрещены, «-» и «.» не может начинать или заканчивать имя, максимальная длина — 96: 'PeftModelForCausalLM((base_model): LoraModel( (модель): LlamaForCausalLM( (модель): LlamaModel( (embed_tokens): Embedding(32000, 4096, padding_idx=31999) (слои): ModuleList((0-31): 32 x LlamaDecoderLayer((self_attn): LlamaAttention((q_proj): Linear8bitLt(in_features=4096, out_features=4096, смещение=False
Я пытался настроить модель LLaMA 7B, используя среду pytorch Lightning. Но не в состоянии это сделать.
1 ответ
Вместо использования Pytorch Lightning вы можете использовать LlamaForSequenceClassification для выполнения задачи классификации.