nextgpt-chapter6-report / code /scripts /download_cuda_wheel.py
CollinMa's picture
Publish NExT-GPT course code and actual execution records
15c46ce verified
Raw History Blame Contribute Delete
1.31 kB
"""Fetch the pinned CUDA wheel in parallel and verify the official index digest."""
import html
import re
import urllib.request
from pathlib import Path
from range_download import download
ROOT = Path(__file__).resolve().parents[1]
NAME = "torch-1.13.1%2Bcu117-cp310-cp310-win_amd64.whl"
if __name__ == '__main__':
index = urllib.request.urlopen("https://download.pytorch.org/whl/cu117/torch/", timeout=30).read().decode()
pattern = r'href="([^"#]*torch-1\.13\.1%2Bcu117-cp310-cp310-win_amd64\.whl)#sha256=([a-f0-9]+)"'
match = re.search(pattern, index, re.I)
if not match:
raise RuntimeError("Pinned CUDA wheel not found in the official index")
url, digest = html.unescape(match.group(1)), match.group(2)
with urllib.request.urlopen(urllib.request.Request(url, method="HEAD"), timeout=30) as response:
size = int(response.headers['Content-Length'])
candidates = list((Path.home() / "AppData/Local/Temp").glob("pip-unpack-*/torch-1.13.1*cu117*win_amd64.whl"))
prefix = max(candidates, key=lambda p: p.stat().st_size) if candidates else None
target = ROOT / ".downloads" / NAME.replace("%2B", "+")
print("Trusted SHA256:", digest, "Size:", size, "Resume prefix:", prefix, flush=True)
download(url, target, size, sha256=digest, seed_prefix=prefix)