Skip to content

ci: add test for double iterating into empty queue bug #20705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import multiprocessing as mp
import os
from collections.abc import Iterator
from queue import Queue

import numpy as np
from torch.utils.data import DataLoader, IterableDataset

from lightning import Trainer
from lightning.pytorch.demos.boring_classes import BoringModel


class QueueDataset(IterableDataset):
def __init__(self, queue: Queue) -> None:
super().__init__()
self.queue = queue

def __iter__(self) -> Iterator:
for _ in range(5):
tensor, _ = self.queue.get(timeout=5)
yield tensor


def create_queue():
q = mp.Queue()
arr = np.random.random([1, 32]).astype(np.float32)
for ind in range(10):
q.put((arr, ind))
return q


def train_model(queue, maxEpochs, ckptPath):
dataloader = DataLoader(QueueDataset(queue), num_workers=1, batch_size=None, persistent_workers=True)
trainer = Trainer(max_epochs=maxEpochs, enable_progress_bar=False, devices=1)
if os.path.exists(ckptPath):
trainer.fit(BoringModel(), dataloader, ckpt_path=ckptPath)
else:
trainer.fit(BoringModel(), dataloader)
trainer.save_checkpoint(ckptPath)
return trainer


def test_training():
"""Test that reproduces issue in calling iter twice on a queue-based IterableDataset leads to Queue Empty errors
when resuming from a checkpoint."""
queue = create_queue()

ckpt_path = "model.ckpt"
trainer = train_model(queue, 1, ckpt_path)
assert trainer is not None

assert os.path.exists(ckpt_path), f"Checkpoint file '{ckpt_path}' wasn't created"

ckpt_size = os.path.getsize(ckpt_path)
assert ckpt_size > 0, f"Checkpoint file is empty (size: {ckpt_size} bytes)"

trainer = train_model(queue, 2, ckpt_path)
assert trainer is not None
Loading