This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Exp backoff for downloads. #9671
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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
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
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
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 |
---|---|---|
|
@@ -75,6 +75,7 @@ | |
from . import image as img | ||
|
||
from . import test_utils | ||
from . import utils | ||
|
||
from . import rnn | ||
|
||
|
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
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 |
---|---|---|
|
@@ -18,23 +18,14 @@ | |
# coding: utf-8 | ||
# pylint: disable= | ||
"""Parallelization utility optimizer.""" | ||
__all__ = ['split_data', 'split_and_load', 'clip_global_norm', | ||
'check_sha1', 'download'] | ||
__all__ = ['split_data', 'split_and_load', 'clip_global_norm'] | ||
|
||
import os | ||
import hashlib | ||
import warnings | ||
try: | ||
import requests | ||
except ImportError: | ||
class requests_failed_to_import(object): | ||
pass | ||
requests = requests_failed_to_import | ||
|
||
import numpy as np | ||
|
||
from .. import ndarray | ||
|
||
|
||
def split_data(data, num_slice, batch_axis=0, even_split=True): | ||
"""Splits an NDArray into `num_slice` slices along `batch_axis`. | ||
Usually used for data parallelism where each slices is sent | ||
|
@@ -61,13 +52,13 @@ def split_data(data, num_slice, batch_axis=0, even_split=True): | |
size = data.shape[batch_axis] | ||
if size < num_slice: | ||
raise ValueError( | ||
"Too many slices for data with shape %s. Arguments are " \ | ||
"num_slice=%d and batch_axis=%d."%(str(data.shape), num_slice, batch_axis)) | ||
"Too many slices for data with shape %s. Arguments are " | ||
"num_slice=%d and batch_axis=%d." % (str(data.shape), num_slice, batch_axis)) | ||
if even_split and size % num_slice != 0: | ||
raise ValueError( | ||
"data with shape %s cannot be evenly split into %d slices along axis %d. " \ | ||
"Use a batch size that's multiple of %d or set even_split=False to allow " \ | ||
"uneven partitioning of data."%( | ||
"data with shape %s cannot be evenly split into %d slices along axis %d. " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Backslashes |
||
"Use a batch size that's multiple of %d or set even_split=False to allow " | ||
"uneven partitioning of data." % ( | ||
str(data.shape), num_slice, batch_axis, num_slice)) | ||
|
||
step = size // num_slice | ||
|
@@ -131,90 +122,13 @@ def clip_global_norm(arrays, max_norm): | |
return total_norm | ||
|
||
|
||
def _indent(s_, numSpaces): | ||
def _indent(s_, num_spaces): | ||
"""Indent string | ||
""" | ||
s = s_.split('\n') | ||
if len(s) == 1: | ||
return s_ | ||
first = s.pop(0) | ||
s = [first] + [(numSpaces * ' ') + line for line in s] | ||
s = [first] + [(num_spaces * ' ') + line for line in s] | ||
s = '\n'.join(s) | ||
return s | ||
|
||
|
||
def check_sha1(filename, sha1_hash): | ||
"""Check whether the sha1 hash of the file content matches the expected hash. | ||
|
||
Parameters | ||
---------- | ||
filename : str | ||
Path to the file. | ||
sha1_hash : str | ||
Expected sha1 hash in hexadecimal digits. | ||
|
||
Returns | ||
------- | ||
bool | ||
Whether the file content matches the expected hash. | ||
""" | ||
sha1 = hashlib.sha1() | ||
with open(filename, 'rb') as f: | ||
while True: | ||
data = f.read(1048576) | ||
if not data: | ||
break | ||
sha1.update(data) | ||
|
||
return sha1.hexdigest() == sha1_hash | ||
|
||
|
||
def download(url, path=None, overwrite=False, sha1_hash=None): | ||
"""Download an given URL | ||
|
||
Parameters | ||
---------- | ||
url : str | ||
URL to download | ||
path : str, optional | ||
Destination path to store downloaded file. By default stores to the | ||
current directory with same name as in url. | ||
overwrite : bool, optional | ||
Whether to overwrite destination file if already exists. | ||
sha1_hash : str, optional | ||
Expected sha1 hash in hexadecimal digits. Will ignore existing file when hash is specified | ||
but doesn't match. | ||
|
||
Returns | ||
------- | ||
str | ||
The file path of the downloaded file. | ||
""" | ||
if path is None: | ||
fname = url.split('/')[-1] | ||
elif os.path.isdir(path): | ||
fname = os.path.join(path, url.split('/')[-1]) | ||
else: | ||
fname = path | ||
|
||
if overwrite or not os.path.exists(fname) or (sha1_hash and not check_sha1(fname, sha1_hash)): | ||
dirname = os.path.dirname(os.path.abspath(os.path.expanduser(fname))) | ||
if not os.path.exists(dirname): | ||
os.makedirs(dirname) | ||
|
||
print('Downloading %s from %s...'%(fname, url)) | ||
r = requests.get(url, stream=True) | ||
if r.status_code != 200: | ||
raise RuntimeError("Failed downloading url %s"%url) | ||
with open(fname, 'wb') as f: | ||
for chunk in r.iter_content(chunk_size=1024): | ||
if chunk: # filter out keep-alive new chunks | ||
f.write(chunk) | ||
|
||
if sha1_hash and not check_sha1(fname, sha1_hash): | ||
raise UserWarning('File {} is downloaded but the content hash does not match. ' \ | ||
'The repo may be outdated or download may be incomplete. ' \ | ||
'If the "repo_url" is overridden, consider switching to ' \ | ||
'the default repo.'.format(fname)) | ||
|
||
return fname |
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
Oops, something went wrong.
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.
Isn't the backslash required?
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.
Isn't triple quotes useful in this case? """blahs dah"""
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.
I think triplequotes would be the equiv of
but I'll try a few options.