barbaroo/Sprotin_parallel
Viewer • Updated • 126k • 23
How to use barbaroo/llama3.1_translate_8B with PEFT:
from peft import PeftModel
from transformers import AutoModelForCausalLM
base_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3.1-8B")
model = PeftModel.from_pretrained(base_model, "barbaroo/llama3.1_translate_8B")Model Description
This adapter is intended to perform English→Faroese translation, leveraging a parameter-efficient fine-tuning (PEFT) approach.
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load the trained model and tokenizer from the checkpoint
checkpoint_dir = "barbaroo/llama3.1_translate_8B" # The directory where your trained model and tokenizer are saved
model = AutoModelForCausalLM.from_pretrained(checkpoint_dir, device_map="auto", load_in_8bit = True)
tokenizer = AutoTokenizer.from_pretrained(checkpoint_dir)
MAX_SEQ_LENGTH = 512
sentences = ["What's your name?"]
# Define the prompt template (same as in training)
alpaca_prompt = """
### Instruction:
{}
### Input:
{}
### Response:
{}"""
# Inference loop
for sentence in sentences:
inputs = tokenizer(
[
alpaca_prompt.format(
"Translate this sentence from English to Faroese:", # Instruction
sentence, # The input sentence to translate
"", # Leave blank for generation
)
],
return_tensors="pt",
padding=True,
truncation=True, # Make sure the input is not too long
max_length=MAX_SEQ_LENGTH # Enforce the max length if necessary
).to("cuda")
# Generate the translation
outputs = model.generate(
**inputs,
max_new_tokens=512, # Limit the number of new tokens generated
eos_token_id=tokenizer.eos_token_id, # Ensure EOS token is used
pad_token_id=tokenizer.pad_token_id, # Ensure padding token is used
temperature=0.1, # Sampling temperature for diversity
top_p=1.0, # Sampling top-p for generation
use_cache=True # Use cache for efficiency
)
# Decode the generated tokens into text
output_string = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
print(f"Input: {sentence}")
print(f"Generated Translation: {output_string}")
We used the Sprotin parallel corpus for English–Faroese translation: barbaroo/Sprotin_parallel.
meta-llama/Llama-3.1-8B.Human evaluation was also performed (see paper)
[COMING SOON]
Base model
meta-llama/Llama-3.1-8B