-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b91a372
ci: add test for double iterating into empty queue bug
iyilmaz24 696257b
ci: add docstring to describe new test
iyilmaz24 d9b5327
test: remove boilerplate and adjust parameters
iyilmaz24 a7d3412
test: merge changes from formatting bot
pre-commit-ci[bot] bb4899d
Apply suggestions from code review
Borda 17c3e95
Merge branch 'master' into issue-19427-test
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
tests/tests_pytorch/loops/test_trainer_iterable_dataset_double_iter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), "Checkpoint file 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.