-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Add AWS S3 i/o #2175
Closed
Closed
Add AWS S3 i/o #2175
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,12 +1,106 @@ | ||
from typing import Tuple | ||
from urllib.parse import urlparse | ||
import os.path as osp | ||
import os | ||
import torch | ||
from torch.hub import _get_torch_home | ||
|
||
from pathlib import Path | ||
from urllib.parse import urlparse | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
torch_cache_home = _get_torch_home() | ||
default_cache_path = osp.join(torch_cache_home, "pl_checkpoints") | ||
|
||
|
||
def try_import_boto3(): | ||
try: | ||
import boto3 | ||
except ImportError: | ||
raise ImportError(f'Could not import `boto3`. Please `pip install boto3` and try again.') | ||
|
||
|
||
def load(path_or_url: str, map_location=None): | ||
parsed = urlparse(path_or_url) | ||
if parsed.scheme == '' or Path(path_or_url).is_file(): | ||
# no scheme or local file | ||
return torch.load(path_or_url, map_location=map_location) | ||
elif parsed.scheme == 's3': | ||
# AWS S3 file | ||
filepath = download_checkpoint_from_s3(path_or_url) | ||
return torch.load(filepath, map_location=map_location) | ||
# URL | ||
return torch.hub.load_state_dict_from_url(path_or_url, map_location=map_location) | ||
|
||
|
||
def is_s3_path(path: str): | ||
"""Checks if path is a valid S3 path""" | ||
return path.startswith("s3://") | ||
|
||
|
||
def parse_s3_path(s3_path: str) -> Tuple[str, str]: | ||
""" | ||
Returns bucket and key from an S3 path. | ||
Example: "s3://my-bucket/folder/checkpoint.ckpt" -> ("my-bucket", "folder/checkpoint.ckpt") | ||
""" | ||
s3_path = urlparse(s3_path, allow_fragments=True) | ||
assert s3_path.scheme == 's3', f'{s3_path} is not a valid AWS S3 path. Needs to start with `s3://`' | ||
bucket, key = s3_path.netloc, s3_path.path | ||
if key.startswith('/'): | ||
key = key[1:] | ||
return bucket, key | ||
|
||
|
||
def save_checkpoint_to_s3(bucket_name, key): | ||
""" | ||
Saves a single checkpoint to an S3 path. | ||
Args: | ||
bucket_name: The name of the bucket we want to save to | ||
key: The rest of the s3 path. | ||
Returns: | ||
None | ||
""" | ||
try_import_boto3() | ||
bucket = boto3.resource("s3").Bucket(bucket_name) | ||
bucket.upload_file(Filename=key, Key=key) | ||
|
||
|
||
def download_checkpoint_from_s3(path_or_url: str, overwrite=False) -> str: | ||
""" | ||
Downloads file from S3 and saves it in default cache path under original S3 key. | ||
Returns filepath where object has been downloaded. | ||
""" | ||
try_import_boto3() | ||
|
||
# Eg "s3://bucket-name/folder/checkpoint.ckpt" --> ("bucket-name", "folder/checkpoint.ckpt") | ||
bucket_name, key = parse_s3_path(path_or_url) | ||
|
||
# ("folder", "checkpoint.ckpt") | ||
directory, filename = osp.split(key) | ||
|
||
# Make directory: '/Users/johnDoe/.cache/torch/pl_checkpoints/folder' | ||
directory_to_make = osp.join(default_cache_path, directory) | ||
os.makedirs(directory_to_make, exist_ok=True) | ||
|
||
# File we will download to: '/Users/johnDoe/.cache/torch/pl_checkpoints/folder/checkpoint.ckpt' | ||
filepath = osp.join(directory_to_make, filename) | ||
|
||
def _download(): | ||
s3 = boto3.resource("s3") | ||
bucket = s3.Bucket(bucket_name) | ||
bucket.download_file(Key=key, Filename=filepath) | ||
|
||
if not osp.exists(filepath): | ||
_download() | ||
else: | ||
if overwrite: | ||
_download() | ||
return filepath | ||
|
||
|
||
def remove_checkpoint_from_s3(bucket, key): | ||
"""Simple remove object from S3""" | ||
try_import_boto3() | ||
s3 = boto3.resource("s3") | ||
obj = s3.Object(bucket, key) | ||
obj.delete() |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -9,3 +9,4 @@ check-manifest | |
twine==1.13.0 | ||
black==19.10b0 | ||
pre-commit>=1.0 | ||
moto |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here I would keep it synced so I would drop
remove_non_top_k_s3_files