|
| 1 | +# PyTorch from Custom Package Sources |
| 2 | + |
| 3 | +This example demonstrates how to configure uv to install PyTorch from cluster-specific package sources, such as internal mirrors, pre-built wheels on shared filesystems, or custom builds optimized for specific hardware. |
| 4 | + |
| 5 | +## Common HPC Use Cases |
| 6 | + |
| 7 | +- **Cluster-optimized builds**: System admins provide PyTorch wheels optimized for cluster hardware |
| 8 | +- **Internal mirrors**: Packages hosted on internal servers for air-gapped or bandwidth-restricted clusters |
| 9 | +- **Shared filesystem wheels**: Pre-built wheels on `/gpfs` or `/scratch` to avoid repeated downloads |
| 10 | +- **Custom PyTorch builds**: Modified PyTorch with cluster-specific patches or optimizations |
| 11 | + |
| 12 | +## Full Example |
| 13 | + |
| 14 | +```python title="pytorch_custom_index.py" |
| 15 | +# /// script |
| 16 | +# requires-python = ">=3.11,<3.13" |
| 17 | +# dependencies = [ |
| 18 | +# "torch==2.5.1", |
| 19 | +# "torchvision==0.20.1", |
| 20 | +# ] |
| 21 | +# |
| 22 | +# [tool.uv] |
| 23 | +# exclude-newer = "2025-12-19T00:00:00Z" |
| 24 | +# python-preference = "managed" |
| 25 | +# |
| 26 | +# [[tool.uv.index]] (1) |
| 27 | +# name = "pytorch-cpu" |
| 28 | +# url = "https://download.pytorch.org/whl/cpu" |
| 29 | +# |
| 30 | +# [tool.uv.sources] (2) |
| 31 | +# torch = { index = "pytorch-cpu" } |
| 32 | +# torchvision = { index = "pytorch-cpu" } |
| 33 | +# |
| 34 | +# [tool.hog.my_endpoint] |
| 35 | +# endpoint = "your-endpoint-uuid" |
| 36 | +# /// |
| 37 | + |
| 38 | +import groundhog_hpc as hog |
| 39 | + |
| 40 | + |
| 41 | +@hog.function(endpoint="my_endpoint") |
| 42 | +def check_pytorch() -> dict[str, str]: |
| 43 | + """Check PyTorch installation details.""" |
| 44 | + import torch |
| 45 | + |
| 46 | + return { |
| 47 | + "version": torch.__version__, |
| 48 | + "cuda_available": str(torch.cuda.is_available()), |
| 49 | + "device": str(torch.device("cuda" if torch.cuda.is_available() else "cpu")), |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +@hog.function(endpoint="my_endpoint") |
| 54 | +def matrix_multiply(size: int = 1000) -> dict[str, float]: |
| 55 | + """Simple PyTorch matrix multiplication benchmark.""" |
| 56 | + import time |
| 57 | + |
| 58 | + import torch |
| 59 | + |
| 60 | + device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 61 | + |
| 62 | + start = time.time() |
| 63 | + a = torch.randn(size, size, device=device) |
| 64 | + b = torch.randn(size, size, device=device) |
| 65 | + c = torch.mm(a, b) |
| 66 | + elapsed = time.time() - start |
| 67 | + |
| 68 | + return { |
| 69 | + "size": size, |
| 70 | + "device": str(device), |
| 71 | + "time_seconds": elapsed, |
| 72 | + "mean": float(c.mean()), |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +@hog.harness() |
| 77 | +def main(): |
| 78 | + """Run PyTorch functions remotely.""" |
| 79 | + info = check_pytorch.remote() |
| 80 | + print(f"PyTorch {info['version']} on {info['device']}") |
| 81 | + |
| 82 | + result = matrix_multiply.remote(500) |
| 83 | + print(f"{result['size']}x{result['size']} matmul: {result['time_seconds']:.3f}s") |
| 84 | +``` |
| 85 | + |
| 86 | +1. Define a named index pointing to your package source. In this example, PyTorch's public index for CPU wheels. Replace with your cluster's internal index URL. |
| 87 | + |
| 88 | +2. Specify which packages should use which source. This tells uv to fetch `torch` and `torchvision` from the custom index instead of PyPI. |
| 89 | + |
| 90 | +## Configuration Options |
| 91 | + |
| 92 | +### Custom Package Index |
| 93 | + |
| 94 | +For internal PyPI mirrors or cluster-specific package servers: |
| 95 | + |
| 96 | +```toml |
| 97 | +[[tool.uv.index]] |
| 98 | +name = "cluster-pypi" |
| 99 | +url = "https://pypi.internal.mylab.edu/simple" |
| 100 | + |
| 101 | +[tool.uv.sources] |
| 102 | +torch = { index = "cluster-pypi" } |
| 103 | +``` |
| 104 | + |
| 105 | +### Local Filesystem Path |
| 106 | + |
| 107 | +For pre-built wheels on shared storage: |
| 108 | + |
| 109 | +```toml |
| 110 | +[tool.uv.sources] |
| 111 | +torch = { path = "/gpfs/shared/wheels/torch-2.5.1+cu121-cp311-linux_x86_64.whl" } |
| 112 | +``` |
| 113 | + |
| 114 | +Or for a local package directory: |
| 115 | + |
| 116 | +```toml |
| 117 | +[tool.uv.sources] |
| 118 | +torch = { path = "/gpfs/shared/pytorch-build", editable = true } |
| 119 | +``` |
| 120 | + |
| 121 | +### Direct URL |
| 122 | + |
| 123 | +For wheels hosted on a web server: |
| 124 | + |
| 125 | +```toml |
| 126 | +[tool.uv.sources] |
| 127 | +torch = { url = "https://internal.server.edu/wheels/torch-2.5.1-custom-py3-none-any.whl" } |
| 128 | +``` |
| 129 | + |
| 130 | +### Git Repository |
| 131 | + |
| 132 | +For custom builds from Git: |
| 133 | + |
| 134 | +```toml |
| 135 | +[tool.uv.sources] |
| 136 | +torch = { git = "https://github.com/myorg/pytorch", tag = "v2.5.1-custom" } |
| 137 | +``` |
| 138 | + |
| 139 | +## Per-Endpoint Configuration |
| 140 | + |
| 141 | +Different endpoints may need different PyTorch builds. Use environment variables to override per endpoint: |
| 142 | + |
| 143 | +```toml |
| 144 | +[tool.hog.cluster_a] |
| 145 | +endpoint = "cluster-a-uuid" |
| 146 | +worker_init = """ |
| 147 | +# Cluster A has PyTorch wheels on shared storage |
| 148 | +export UV_FIND_LINKS=/gpfs/cluster-a/wheels |
| 149 | +""" |
| 150 | + |
| 151 | +[tool.hog.cluster_b] |
| 152 | +endpoint = "cluster-b-uuid" |
| 153 | +worker_init = """ |
| 154 | +# Cluster B uses an internal PyPI mirror |
| 155 | +export UV_INDEX_URL=https://pypi.cluster-b.edu/simple |
| 156 | +""" |
| 157 | +``` |
| 158 | + |
| 159 | +See also: [Environment Variables](../api/environment_variables.md#uv-environment-variables) |
| 160 | + |
| 161 | +## Running the Example |
| 162 | + |
| 163 | +```bash |
| 164 | +hog run pytorch_custom_index.py |
| 165 | +``` |
| 166 | + |
| 167 | +Output: |
| 168 | + |
| 169 | +``` |
| 170 | +PyTorch 2.5.1 on cuda |
| 171 | +500x500 matmul: 0.015s |
| 172 | +``` |
| 173 | + |
| 174 | +## Next Steps |
| 175 | + |
| 176 | +- **[PEP 723 Concepts](../concepts/pep723.md#configuring-uv-via-tooluv)** - Complete uv configuration reference |
| 177 | +- **[Environment Variables](../api/environment_variables.md#uv-environment-variables)** - Override uv settings per endpoint |
| 178 | +- **[uv Dependencies](https://docs.astral.sh/uv/concepts/projects/dependencies/)** - Full uv dependency configuration docs |
0 commit comments