Buckets:
| """Detect where the code runs (this machine or a Colab kernel) and set up Hub settings. | |
| Call setup() before importing transformers or huggingface_hub, because they read the | |
| environment at import time. | |
| """ | |
| import importlib.util | |
| import os | |
| from pathlib import Path | |
| import yaml | |
| ROOT = Path(__file__).resolve().parent.parent | |
| RUNTIMES = ("local", "colab") | |
| def detect(): | |
| if os.environ.get("COLAB_RELEASE_TAG"): | |
| return "colab" | |
| try: | |
| if importlib.util.find_spec("google.colab") is not None: | |
| return "colab" | |
| except ModuleNotFoundError: | |
| pass | |
| return "local" | |
| def load_config(path=ROOT / "config.yaml"): | |
| return yaml.safe_load(Path(path).read_text(encoding="utf-8")) | |
| def setup(runtime="auto"): | |
| if runtime == "auto": | |
| runtime = detect() | |
| if runtime not in RUNTIMES: | |
| raise ValueError(f"unknown runtime {runtime!r}, expected one of {RUNTIMES}") | |
| if runtime != detect(): | |
| raise RuntimeError(f"runtime set to {runtime!r} but this kernel runs on {detect()!r}") | |
| config = load_config() | |
| settings = config["runtimes"][runtime] | |
| hf_home = Path(os.path.expanduser(settings["hf_home"])) | |
| os.environ["HF_HOME"] = str(hf_home) | |
| os.environ["HF_HUB_CACHE"] = str(hf_home / "hub") | |
| os.environ["HF_HUB_OFFLINE"] = "1" if settings["offline"] else "0" | |
| os.environ.pop("HF_TOKEN", None) | |
| os.environ["HF_HUB_DISABLE_IMPLICIT_TOKEN"] = "1" # no token from env, file or the Colab vault | |
| return {"runtime": runtime, "config": config, "dtype": settings["dtype"], "hf_home": hf_home} | |
| def gpu_summary(): | |
| """Name and compute capability of GPU 0, or None when the runtime has no CUDA GPU.""" | |
| import torch | |
| if not torch.cuda.is_available(): | |
| return None | |
| cap = ".".join(map(str, torch.cuda.get_device_capability(0))) | |
| return f"{torch.cuda.get_device_name(0)} (compute capability {cap})" | |
| def require_gpu(): | |
| if gpu_summary() is None: | |
| raise RuntimeError("no CUDA GPU in this runtime. On Colab, connect to a GPU runtime such as T4") | |
Xet Storage Details
- Size:
- 2.05 kB
- Xet hash:
- a5f50d7ae6977fbc7d65e8db38fdd3d7a4495f3a2ccf2bc7e904e28fb3ac9bb4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.