Bf16 and tesla V100 32GB

Sourabh_Yadav: <p>are they compatible and what precautions should I take if I use it to fine-tune a bf16 model.</p>

ptrblck: <p>No, <code>bfloat16</code> is supported starting from Ampere GPUs.</p>

Sourabh_Yadav: <p>sorry to bother but can it be reason why i am getting error<br>
None type object is non iterable. because rest of code looks fine to me. I am using qwen2.5-3B-Instruct.</p>
<p>import os<br>
import pandas as pd<br>
from torch.utils.data import Dataset, DataLoader<br>
from PIL import Image<br>
from transformers import AutoProcessor, AutoTokenizer, AdamW , AutoModelForCausalLM<br>
import torch<br>
from tqdm import tqdm</p>
<p>class ImageTextDataset(Dataset):<br>
def <strong>init</strong>(self, csv_file, images_folder, processor, max_length=128):<br>
self.data = pd.read_csv(csv_file)<br>
self.images_folder = images_folder<br>
self.processor = processor<br>
self.max_length = max_length<br>
self.image_size = (224, 224)</p>
<pre><code>def __len__(self):
    return len(self.data)
        
def __getitem__(self, idx):
    image_id = self.data.iloc[idx]['id'].strip()
    question = self.data.iloc[idx]['user']
    answer = self.data.iloc[idx]['assistant']
    
    image_path = os.path.join(self.images_folder, f"{image_id}.png")
    image = Image.open(image_path).convert("RGB").resize(self.image_size)
    
    # Process inputs
    inputs = self.processor(
        text=question,
        images=image,
        return_tensors="pt",
        padding='max_length',
        max_length=self.max_length,
        truncation=True
    )
    
    # Process labels
    label_output = self.processor(
        text=answer,
        return_tensors="pt",
        padding='max_length',
        max_length=self.max_length,
        truncation=True
    )
    labels = label_output.input_ids if label_output is not None else torch.zeros((1, self.max_length), dtype=torch.long)
    
    return {
        "input_ids": inputs.input_ids.squeeze(),
        "pixel_values": inputs.pixel_values.squeeze(),
        "labels": labels.squeeze()
    }
</code></pre>
<h1><a name="p-464724-check-if-the-csv-file-and-images-folder-exist-1" class="anchor" href="#p-464724-check-if-the-csv-file-and-images-folder-exist-1"></a>Check if the CSV file and images folder exist</h1>
<p>if not os.path.exists(“mapping.csv”):<br>
raise FileNotFoundError(“mapping.csv not found”)<br>
if not os.path.exists(“images_folder”):<br>
raise FileNotFoundError(“images_folder not found”)</p>
<p>dataset = ImageTextDataset(csv_file=“mapping.csv”, images_folder=“images_folder”, processor=processor, max_length=128)</p>
<h1><a name="p-464724-verify-dataset-is-not-empty-2" class="anchor" href="#p-464724-verify-dataset-is-not-empty-2"></a>Verify dataset is not empty</h1>
<p>if len(dataset) == 0:<br>
raise ValueError(“Dataset is empty”)<br>
def collate_fn(batch):<br>
if not batch:<br>
raise ValueError(“Batch is empty”)<br>
input_ids = torch.stack([item[‘input_ids’] for item in batch if ‘input_ids’ in item])<br>
pixel_values = torch.stack([item[‘pixel_values’] for item in batch if ‘pixel_values’ in item])<br>
labels = torch.stack([item[‘labels’] for item in batch if ‘labels’ in item])</p>
<pre><code># Ensure all tensors have the same batch dimension
if len(input_ids) != len(pixel_values) or len(input_ids) != len(labels):
    raise ValueError("Inconsistent batch sizes")
    
return {
    "input_ids": input_ids,
    "pixel_values": pixel_values,
    "labels": labels
}
</code></pre>
<p>dataloader = DataLoader(<br>
dataset,<br>
batch_size=2,<br>
shuffle=True,<br>
collate_fn=collate_fn,<br>
num_workers=0  # Set to 0 to make debugging easier<br>
)</p>
<h1><a name="p-464724-verify-dataloader-is-not-empty-3" class="anchor" href="#p-464724-verify-dataloader-is-not-empty-3"></a>Verify dataloader is not empty</h1>
<p>try:<br>
first_batch = next(iter(dataloader))<br>
except StopIteration:<br>
raise ValueError(“DataLoader is empty”)</p>
<h1><a name="p-464724-set-model-to-training-mode-4" class="anchor" href="#p-464724-set-model-to-training-mode-4"></a>Set model to training mode</h1>
<p>model.train()<br>
optimizer = torch.optim.AdamW(model.parameters(), lr=5e-5)<br>
epochs = 1</p>
<h1><a name="p-464724-training-loop-5" class="anchor" href="#p-464724-training-loop-5"></a>Training loop</h1>
<p>total_steps = len(dataloader)<br>
for epoch in range(epochs):<br>
running_loss = 0.0<br>
progress_bar = tqdm(enumerate(dataloader), total=total_steps, desc=f"Epoch {epoch + 1}/{epochs}")</p>
<pre><code>for step, batch in progress_bar:
    # Zero the gradients
    optimizer.zero_grad()
    
    # Move data to device
    input_ids = batch["input_ids"].to(device)
    pixel_values = batch["pixel_values"].to(device)
    labels = batch["labels"].to(device)
    
    # Forward pass
    outputs = model(input_ids=input_ids, pixel_values=pixel_values, labels=labels)
    loss = outputs.loss
    
    # Backward pass
    loss.backward()
    
    # Update weights
    optimizer.step()
    
    # Update progress bar
    running_loss += loss.item()
    avg_loss = running_loss / (step + 1)
    progress_bar.set_postfix({"avg_loss": f"{avg_loss:.4f}", "loss": f"{loss.item():.4f}"})
</code></pre>