During Pretraining of Rag on custom dataset getting out of memory

I am trying to fine tune pretrained Rag on my Custom dataset. I am enclosing the code. During Training, I am getting OutOfMemoryError: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 22.19 GiB of which 3.50 MiB is free. Process 15094 has 22.18 GiB memory in use. Of the allocated memory 21.70 GiB is allocated by PyTorch, and 195.58 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings… I tried everything.
I am enclosing my code also any help would be appreciated.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># %%
!pip install transformers datasets torch
# %%
from transformers import RagTokenizer, RagTokenForGeneration
tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq")
# %%
!pip install accelerate -U
# %%
!pip install faiss-cpu
# %%
!pip install accelerate -U
# %%
pip show accelerate
# %%
import os
# Set the environment variable
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
# %%
import pandas as pd
from sklearn.model_selection import train_test_split
from datasets import Dataset, DatasetDict, load_from_disk
from transformers import Trainer, TrainingArguments, RagTokenizer, RagSequenceForGeneration, RagRetriever, DPRContextEncoder, DPRContextEncoderTokenizerFast
import faiss
import torch
import numpy as np
# Load your CSV data
df = pd.read_csv("/teamspace/studios/this_studio/examples/qa_pairs1.csv") # Replace with the path to your CSV file
# Split the data into train and validation datasets
train_df, val_df = train_test_split(df, test_size=0.2)
# Convert DataFrame to Hugging Face Dataset
train_dataset = Dataset.from_pandas(train_df)
val_dataset = Dataset.from_pandas(val_df)
# Combine into a DatasetDict
climate_change_dataset = DatasetDict({
"train": train_dataset,
"validation": val_dataset
})
# Generate embeddings for your passages
ctx_encoder = DPRContextEncoder.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
ctx_tokenizer = DPRContextEncoderTokenizerFast.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
def create_embeddings(sentences):
embeddings = []
for passage in sentences:
inputs = ctx_tokenizer(passage, return_tensors="pt", padding=True, truncation=True, max_length=512)
with torch.no_grad():
embedding = ctx_encoder(**inputs).pooler_output.cpu().numpy()
embeddings.append(embedding)
return np.concatenate(embeddings, axis=0).astype("float32")
# Prepare the passages data
passages = df["sentence"].tolist()
embeddings = create_embeddings(passages)
# Structure your data
data = {
"title": [""] * len(df), # Adding dummy title column
"text": df["sentence"].tolist(),
"embeddings": embeddings.tolist()
}
# Create a dataset using the Dataset class from datasets
passages_dataset = Dataset.from_dict(data)
# Save the dataset to disk
passages_dataset.save_to_disk("/teamspace/studios/this_studio/examples/my_knowledge_dataset")
# Create and save FAISS index
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
faiss.write_index(index, "/teamspace/studios/this_studio/examples/faiss_index")
# Load tokenizers and models
question_encoder_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
generator_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
# Define the tokenizer for question encoder and generator
tokenizer = question_encoder_tokenizer
# Define a function to process the data for training
def preprocess_function(examples):
inputs = [q + " [SEP] " + s for q, s in zip(examples["question"], examples["sentence"])]
model_inputs = tokenizer(inputs, max_length=512, truncation=True, padding="max_length")
labels = tokenizer(examples["answer"], max_length=512, truncation=True, padding="max_length")
model_inputs["labels"] = labels["input_ids"]
return model_inputs
# Process the dataset
tokenized_datasets = climate_change_dataset.map(preprocess_function, batched=True)
# Initialize the Trainer
training_args = TrainingArguments(
output_dir="./rag_finetuned/",
num_train_epochs=3,
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
warmup_steps=500,
weight_decay=0.01,
logging_dir="./logs/",
logging_steps=10,
)
# Load the dataset from disk
dataset_path = "/teamspace/studios/this_studio/examples/my_knowledge_dataset"
dataset = load_from_disk(dataset_path)
# Load the FAISS index
index_path = "/teamspace/studios/this_studio/examples/faiss_index"
index = faiss.read_index(index_path)
retriever = RagRetriever.from_pretrained(
"facebook/rag-token-nq",
index_name="custom",
passages_path=dataset_path,
index_path=index_path,
question_encoder_tokenizer=question_encoder_tokenizer,
generator_tokenizer=generator_tokenizer,
)
model = RagSequenceForGeneration.from_pretrained(
"facebook/rag-token-nq",
retriever=retriever,
question_encoder_tokenizer=question_encoder_tokenizer,
generator_tokenizer=generator_tokenizer,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
)
# Start fine-tuning
trainer.train()
# %%
torch.cuda.empty_cache()
</code>
<code># %% !pip install transformers datasets torch # %% from transformers import RagTokenizer, RagTokenForGeneration tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq") model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq") # %% !pip install accelerate -U # %% !pip install faiss-cpu # %% !pip install accelerate -U # %% pip show accelerate # %% import os # Set the environment variable os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True" # %% import pandas as pd from sklearn.model_selection import train_test_split from datasets import Dataset, DatasetDict, load_from_disk from transformers import Trainer, TrainingArguments, RagTokenizer, RagSequenceForGeneration, RagRetriever, DPRContextEncoder, DPRContextEncoderTokenizerFast import faiss import torch import numpy as np # Load your CSV data df = pd.read_csv("/teamspace/studios/this_studio/examples/qa_pairs1.csv") # Replace with the path to your CSV file # Split the data into train and validation datasets train_df, val_df = train_test_split(df, test_size=0.2) # Convert DataFrame to Hugging Face Dataset train_dataset = Dataset.from_pandas(train_df) val_dataset = Dataset.from_pandas(val_df) # Combine into a DatasetDict climate_change_dataset = DatasetDict({ "train": train_dataset, "validation": val_dataset }) # Generate embeddings for your passages ctx_encoder = DPRContextEncoder.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base") ctx_tokenizer = DPRContextEncoderTokenizerFast.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base") def create_embeddings(sentences): embeddings = [] for passage in sentences: inputs = ctx_tokenizer(passage, return_tensors="pt", padding=True, truncation=True, max_length=512) with torch.no_grad(): embedding = ctx_encoder(**inputs).pooler_output.cpu().numpy() embeddings.append(embedding) return np.concatenate(embeddings, axis=0).astype("float32") # Prepare the passages data passages = df["sentence"].tolist() embeddings = create_embeddings(passages) # Structure your data data = { "title": [""] * len(df), # Adding dummy title column "text": df["sentence"].tolist(), "embeddings": embeddings.tolist() } # Create a dataset using the Dataset class from datasets passages_dataset = Dataset.from_dict(data) # Save the dataset to disk passages_dataset.save_to_disk("/teamspace/studios/this_studio/examples/my_knowledge_dataset") # Create and save FAISS index index = faiss.IndexFlatL2(embeddings.shape[1]) index.add(embeddings) faiss.write_index(index, "/teamspace/studios/this_studio/examples/faiss_index") # Load tokenizers and models question_encoder_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq") generator_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq") # Define the tokenizer for question encoder and generator tokenizer = question_encoder_tokenizer # Define a function to process the data for training def preprocess_function(examples): inputs = [q + " [SEP] " + s for q, s in zip(examples["question"], examples["sentence"])] model_inputs = tokenizer(inputs, max_length=512, truncation=True, padding="max_length") labels = tokenizer(examples["answer"], max_length=512, truncation=True, padding="max_length") model_inputs["labels"] = labels["input_ids"] return model_inputs # Process the dataset tokenized_datasets = climate_change_dataset.map(preprocess_function, batched=True) # Initialize the Trainer training_args = TrainingArguments( output_dir="./rag_finetuned/", num_train_epochs=3, per_device_train_batch_size=4, per_device_eval_batch_size=4, warmup_steps=500, weight_decay=0.01, logging_dir="./logs/", logging_steps=10, ) # Load the dataset from disk dataset_path = "/teamspace/studios/this_studio/examples/my_knowledge_dataset" dataset = load_from_disk(dataset_path) # Load the FAISS index index_path = "/teamspace/studios/this_studio/examples/faiss_index" index = faiss.read_index(index_path) retriever = RagRetriever.from_pretrained( "facebook/rag-token-nq", index_name="custom", passages_path=dataset_path, index_path=index_path, question_encoder_tokenizer=question_encoder_tokenizer, generator_tokenizer=generator_tokenizer, ) model = RagSequenceForGeneration.from_pretrained( "facebook/rag-token-nq", retriever=retriever, question_encoder_tokenizer=question_encoder_tokenizer, generator_tokenizer=generator_tokenizer, ) trainer = Trainer( model=model, args=training_args, train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], ) # Start fine-tuning trainer.train() # %% torch.cuda.empty_cache() </code>
# %%
!pip install transformers datasets torch

# %%
from transformers import RagTokenizer, RagTokenForGeneration
tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq")

# %%
!pip install accelerate -U

# %%
!pip install faiss-cpu

# %%
!pip install accelerate -U

# %%
pip show accelerate

# %%
import os

# Set the environment variable
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"


# %%
import pandas as pd
from sklearn.model_selection import train_test_split
from datasets import Dataset, DatasetDict, load_from_disk
from transformers import Trainer, TrainingArguments, RagTokenizer, RagSequenceForGeneration, RagRetriever, DPRContextEncoder, DPRContextEncoderTokenizerFast
import faiss
import torch
import numpy as np


# Load your CSV data
df = pd.read_csv("/teamspace/studios/this_studio/examples/qa_pairs1.csv")  # Replace with the path to your CSV file

# Split the data into train and validation datasets
train_df, val_df = train_test_split(df, test_size=0.2)

# Convert DataFrame to Hugging Face Dataset
train_dataset = Dataset.from_pandas(train_df)
val_dataset = Dataset.from_pandas(val_df)

# Combine into a DatasetDict
climate_change_dataset = DatasetDict({
    "train": train_dataset,
    "validation": val_dataset
})

# Generate embeddings for your passages
ctx_encoder = DPRContextEncoder.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
ctx_tokenizer = DPRContextEncoderTokenizerFast.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")

def create_embeddings(sentences):
    embeddings = []
    for passage in sentences:
        inputs = ctx_tokenizer(passage, return_tensors="pt", padding=True, truncation=True, max_length=512)
        with torch.no_grad():
            embedding = ctx_encoder(**inputs).pooler_output.cpu().numpy()
        embeddings.append(embedding)
    return np.concatenate(embeddings, axis=0).astype("float32")

# Prepare the passages data
passages = df["sentence"].tolist()
embeddings = create_embeddings(passages)

# Structure your data
data = {
    "title": [""] * len(df),  # Adding dummy title column
    "text": df["sentence"].tolist(),
    "embeddings": embeddings.tolist()
}

# Create a dataset using the Dataset class from datasets
passages_dataset = Dataset.from_dict(data)

# Save the dataset to disk
passages_dataset.save_to_disk("/teamspace/studios/this_studio/examples/my_knowledge_dataset")

# Create and save FAISS index
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
faiss.write_index(index, "/teamspace/studios/this_studio/examples/faiss_index")

# Load tokenizers and models
question_encoder_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
generator_tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")

# Define the tokenizer for question encoder and generator
tokenizer = question_encoder_tokenizer

# Define a function to process the data for training
def preprocess_function(examples):
    inputs = [q + " [SEP] " + s for q, s in zip(examples["question"], examples["sentence"])]
    model_inputs = tokenizer(inputs, max_length=512, truncation=True, padding="max_length")
    labels = tokenizer(examples["answer"], max_length=512, truncation=True, padding="max_length")
    model_inputs["labels"] = labels["input_ids"]
    return model_inputs

# Process the dataset
tokenized_datasets = climate_change_dataset.map(preprocess_function, batched=True)

# Initialize the Trainer
training_args = TrainingArguments(
    output_dir="./rag_finetuned/",
    num_train_epochs=3,
    per_device_train_batch_size=4,
    per_device_eval_batch_size=4,
    warmup_steps=500,
    weight_decay=0.01,
    logging_dir="./logs/",
    logging_steps=10,
)

# Load the dataset from disk
dataset_path = "/teamspace/studios/this_studio/examples/my_knowledge_dataset"
dataset = load_from_disk(dataset_path)

# Load the FAISS index
index_path = "/teamspace/studios/this_studio/examples/faiss_index"
index = faiss.read_index(index_path)

retriever = RagRetriever.from_pretrained(
    "facebook/rag-token-nq",
    index_name="custom",
    passages_path=dataset_path,
    index_path=index_path,
    question_encoder_tokenizer=question_encoder_tokenizer,
    generator_tokenizer=generator_tokenizer,
)

model = RagSequenceForGeneration.from_pretrained(
    "facebook/rag-token-nq",
    retriever=retriever,
    question_encoder_tokenizer=question_encoder_tokenizer,
    generator_tokenizer=generator_tokenizer,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_datasets["train"],
    eval_dataset=tokenized_datasets["validation"],
    
)

# Start fine-tuning
trainer.train()


# %%
torch.cuda.empty_cache()

I am getting to fine tuene my Rag Model but getting out of Memory inspite of using GPU.

New contributor

surbhi sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật