|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import time |
| 17 | +import requests |
| 18 | +import shutil |
| 19 | +from fleetx.utils import logger |
| 20 | +from tqdm import tqdm |
| 21 | +import paddle |
| 22 | + |
| 23 | +DOWNLOAD_RETRY_LIMIT = 3 |
| 24 | + |
| 25 | + |
| 26 | +def is_url(path): |
| 27 | + """ |
| 28 | + Whether path is URL. |
| 29 | + Args: |
| 30 | + path (string): URL string or not. |
| 31 | + """ |
| 32 | + return path.startswith('http://') or path.startswith('https://') |
| 33 | + |
| 34 | + |
| 35 | +def _map_path(url, root_dir): |
| 36 | + # parse path after download under root_dir |
| 37 | + fname = os.path.split(url)[-1] |
| 38 | + fpath = fname |
| 39 | + return os.path.join(root_dir, fpath) |
| 40 | + |
| 41 | + |
| 42 | +def cached_path(url_or_path, cache_dir=None): |
| 43 | + if cache_dir is None: |
| 44 | + cache_dir = '~/.cache/fleetx/' |
| 45 | + |
| 46 | + cache_dir = os.path.expanduser(cache_dir) |
| 47 | + |
| 48 | + if not os.path.exists(cache_dir): |
| 49 | + os.makedirs(cache_dir) |
| 50 | + |
| 51 | + if is_url(url_or_path): |
| 52 | + path = _map_path(url_or_path, cache_dir) |
| 53 | + url = url_or_path |
| 54 | + else: |
| 55 | + path = url_or_path |
| 56 | + url = None |
| 57 | + |
| 58 | + if os.path.exists(path): |
| 59 | + logger.info( |
| 60 | + f"Found {os.path.split(path)[-1]} in cache_dir: {cache_dir}.") |
| 61 | + return path |
| 62 | + |
| 63 | + download(url, path) |
| 64 | + return path |
| 65 | + |
| 66 | + |
| 67 | +def _download(url, fullname): |
| 68 | + """ |
| 69 | + Download from url, save to path. |
| 70 | + url (str): download url |
| 71 | + path (str): download to given path |
| 72 | + """ |
| 73 | + retry_cnt = 0 |
| 74 | + |
| 75 | + while not os.path.exists(fullname): |
| 76 | + if retry_cnt < DOWNLOAD_RETRY_LIMIT: |
| 77 | + retry_cnt += 1 |
| 78 | + else: |
| 79 | + raise RuntimeError("Download from {} failed. " |
| 80 | + "Retry limit reached".format(url)) |
| 81 | + |
| 82 | + logger.info("Downloading {}".format(url)) |
| 83 | + |
| 84 | + try: |
| 85 | + req = requests.get(url, stream=True) |
| 86 | + except Exception as e: # requests.exceptions.ConnectionError |
| 87 | + logger.info("Downloading {} failed {} times with exception {}". |
| 88 | + format(url, retry_cnt + 1, str(e))) |
| 89 | + time.sleep(1) |
| 90 | + continue |
| 91 | + |
| 92 | + if req.status_code != 200: |
| 93 | + raise RuntimeError("Downloading from {} failed with code " |
| 94 | + "{}!".format(url, req.status_code)) |
| 95 | + |
| 96 | + # For protecting download interupted, download to |
| 97 | + # tmp_fullname firstly, move tmp_fullname to fullname |
| 98 | + # after download finished |
| 99 | + tmp_fullname = fullname + "_tmp" |
| 100 | + total_size = req.headers.get('content-length') |
| 101 | + with open(tmp_fullname, 'wb') as f: |
| 102 | + if total_size: |
| 103 | + with tqdm(total=(int(total_size) + 1023) // 1024) as pbar: |
| 104 | + for chunk in req.iter_content(chunk_size=1024): |
| 105 | + f.write(chunk) |
| 106 | + pbar.update(1) |
| 107 | + else: |
| 108 | + for chunk in req.iter_content(chunk_size=1024): |
| 109 | + if chunk: |
| 110 | + f.write(chunk) |
| 111 | + shutil.move(tmp_fullname, fullname) |
| 112 | + |
| 113 | + return fullname |
| 114 | + |
| 115 | + |
| 116 | +def download(url, path): |
| 117 | + local_rank = 0 |
| 118 | + world_size = 1 |
| 119 | + if paddle.fluid.core.is_compiled_with_dist(): |
| 120 | + local_rank = paddle.distributed.ParallelEnv().dev_id |
| 121 | + world_size = paddle.distributed.get_world_size() |
| 122 | + if world_size > 1 and local_rank != 0: |
| 123 | + while not os.path.exists(path): |
| 124 | + time.sleep(1) |
| 125 | + else: |
| 126 | + _download(url, path) |
0 commit comments