Skip to content
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

Fix usage of fs.listdir in CheckpointConnector #15413

Merged
merged 22 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -19,6 +19,7 @@
from typing import Any, Dict, Optional

import torch
from fsspec.core import url_to_fs
from torch import Tensor
from torchmetrics import Metric

Expand Down Expand Up @@ -574,12 +575,12 @@ def __max_ckpt_version_in_folder(dir_path: _PATH, name_key: str = "ckpt_") -> Op
"""

# check directory existence
fs = get_filesystem(dir_path)
fs, uri = url_to_fs(dir_path)
awaelchli marked this conversation as resolved.
Show resolved Hide resolved
if not fs.exists(dir_path):
return None

# check corresponding file existence
files = [os.path.basename(f["name"]) for f in fs.listdir(dir_path)]
files = [os.path.basename(f["name"]) for f in fs.listdir(uri)]
files = [x for x in files if name_key in x]
if len(files) == 0:
return None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import os
from unittest import mock

import fsspec.registry
import pytest
import torch
from fsspec.implementations.arrow import ArrowFSWrapper

from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
Expand Down Expand Up @@ -102,6 +104,38 @@ def test_hpc_max_ckpt_version(tmpdir):
)


def test_ckpt_for_fsspec(tmpdir):
"""Test that the CheckpointConnector is able to write to fsspec file systems."""

class MockFileSystem(ArrowFSWrapper):
"""A wrapper on pyarrow's mock filesystem for testing."""

protocol = "mock"

def __init__(self, **kwargs):
from pyarrow.fs import FileSystem

fs = FileSystem.from_uri("mock://") # Get mock file system
super().__init__(fs=fs, **kwargs)

fsspec.registry.register_implementation("mock", MockFileSystem)

model = BoringModel()
trainer = Trainer(default_root_dir="mock://" + tmpdir, max_steps=1)
trainer.fit(model)
trainer.save_checkpoint(tmpdir / "hpc_ckpt.ckpt")
trainer.save_checkpoint(tmpdir / "hpc_ckpt_0.ckpt")
trainer.save_checkpoint(tmpdir / "hpc_ckpt_3.ckpt")
trainer.save_checkpoint(tmpdir / "hpc_ckpt_33.ckpt")

assert trainer._checkpoint_connector._hpc_resume_path == str(tmpdir / "hpc_ckpt_33.ckpt")
assert trainer._checkpoint_connector._CheckpointConnector__max_ckpt_version_in_folder(tmpdir) == 33
assert (
trainer._checkpoint_connector._CheckpointConnector__max_ckpt_version_in_folder(tmpdir / "not" / "existing")
is None
)


def test_loops_restore(tmpdir):
"""Test that required loop state_dict is loaded correctly by checkpoint connector."""
model = BoringModel()
Expand Down