MCPcopy
hub / github.com/tinygrad/tinygrad / fetch

Function fetch

tinygrad/helpers.py:450–475  ·  view source on GitHub ↗
(url:str, name:pathlib.Path|str|None=None, subdir:str|None=None, gunzip:bool=False, allow_caching=not getenv("DISABLE_HTTP_CACHE"),
          headers:dict[str, str]={}, sha256:str|None=None)

Source from the content-addressed store, hash-verified

448 return pathlib.Path(cache_dir) / "downloads"
449
450def fetch(url:str, name:pathlib.Path|str|None=None, subdir:str|None=None, gunzip:bool=False, allow_caching=not getenv("DISABLE_HTTP_CACHE"),
451 headers:dict[str, str]={}, sha256:str|None=None) -> pathlib.Path:
452 import urllib.request
453 if url.startswith(("/", ".")): return pathlib.Path(url)
454 if name is not None and (isinstance(name, pathlib.Path) or '/' in name): fp = pathlib.Path(name)
455 else:
456 hh = "_"+hashlib.md5(("\n".join(f"{k.strip()}:{v.strip()}" for k,v in sorted(headers.items()))).encode("utf-8")).hexdigest() if headers else ""
457 fp = _ensure_downloads_dir() / (subdir or "") / ((name or hashlib.md5(url.encode('utf-8')).hexdigest()) + hh + (".gunzip" if gunzip else ""))
458 if not fp.is_file() or not allow_caching or (sha256 and hashlib.sha256(fp.read_bytes()).hexdigest() != sha256):
459 (_dir := fp.parent).mkdir(parents=True, exist_ok=True)
460 with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "tinygrad 0.13.0", **headers}), timeout=10) as r:
461 assert r.status in {200, 206}, r.status
462 length = int(r.headers.get('content-length', 0)) if not gunzip else None
463 readfile = gzip.GzipFile(fileobj=r) if gunzip else r
464 progress_bar:tqdm = tqdm(total=length, unit='B', unit_scale=True, desc=f"{url}")
465 h = hashlib.sha256() if sha256 else None
466 with tempfile.NamedTemporaryFile(dir=_dir, delete=False) as f:
467 while chunk := readfile.read(16384):
468 if h: h.update(chunk)
469 progress_bar.update(f.write(chunk))
470 f.close()
471 if h and (actual_sha256:=h.hexdigest()) != sha256: raise RuntimeError(f"fetch sha mismatch, expected {sha256} but got {actual_sha256}")
472 pathlib.Path(f.name).rename(fp)
473 progress_bar.update(close=True)
474 if length and (file_size:=os.stat(fp).st_size) < length: raise RuntimeError(f"fetch size incomplete, {file_size} < {length}")
475 return fp
476
477def fetch_fw(path:str, name:str, sha256:str) -> bytes:
478 if sys.version_info >= (3,14) and (p:=pathlib.Path(f"/lib/firmware/{path}/{name}.zst")).is_file():

Callers 15

from_urlMethod · 0.90
mainFunction · 0.90
parse_xmlFunction · 0.90
extract_pdf_textFunction · 0.90
loadFunction · 0.90
ensure_appMethod · 0.90
decode.pyFile · 0.90
fetch_mnistFunction · 0.90
fetch_cifarFunction · 0.90
get_val_filesFunction · 0.90

Calls 10

getenvFunction · 0.85
_ensure_downloads_dirFunction · 0.85
tqdmClass · 0.85
read_bytesMethod · 0.80
encodeMethod · 0.45
getMethod · 0.45
readMethod · 0.45
updateMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45

Tested by 15

llama_tokMethod · 0.72
test_fetch_smallMethod · 0.72
test_fetch_imgMethod · 0.72
test_fetch_subdirMethod · 0.72
test_fetch_user_agentMethod · 0.72
test_fetch_shaMethod · 0.72
test_llama1b_fullMethod · 0.72
compare_weights_bothFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…