Spaces:
Running
Running
Download lpu/benchmarks/models.py from iparaco/Virtual-LPU: direct link, hf CLI and curl.
- Browser
- Download file 5.2 kB
-
https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/benchmarks/models.py
- Command line
-
hf download hf://spaces/iparaco/Virtual-LPU/lpu/benchmarks/models.py
-
curl -L -o models.py https://huggingface.co/spaces/iparaco/Virtual-LPU/resolve/main/lpu/benchmarks/models.py
5.2 kB
| """Benchmark SR models (real PyTorch implementations, seeded deterministically). | |
| - SRResNet (quick tier, ~447K params): the fast A/B/C comparison model. | |
| - EDSR-baseline (extended tier, 16 residual blocks, no BN, ~1.41M params): | |
| the realistic SR training workload. | |
| Both are standard architectures implemented for THIS benchmark so A/B/C run | |
| identical model code - the only variable is the optimization environment. | |
| Scale support is explicit: SRResNet x2/x4 (PixelShuffle chains), EDSR-baseline | |
| x2 (the standard configuration); anything else raises instead of silently | |
| producing wrong upscale factors. | |
| """ | |
| from __future__ import annotations | |
| from ..compat import require_torch | |
| def _conv_block(nn, in_c, out_c, k=3): | |
| return nn.Sequential( | |
| nn.Conv2d(in_c, out_c, k, padding=k // 2), | |
| nn.PReLU(), | |
| ) | |
| class SRResNetQuick: | |
| """Compact SRResNet-style generator (4 residual blocks, upscale x2).""" | |
| name = "srresnet_quick" | |
| def build(n_res: int = 4, feats: int = 64, scale: int = 2): | |
| torch = require_torch() | |
| nn = torch.nn | |
| if scale not in (2, 4): | |
| raise ValueError(f"SRResNetQuick supports scale 2 or 4, got {scale}") | |
| def res_block(): | |
| return nn.Sequential( | |
| nn.Conv2d(feats, feats, 3, padding=1), | |
| nn.PReLU(), | |
| nn.Conv2d(feats, feats, 3, padding=1), | |
| ) | |
| class ResChain(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.blocks = nn.ModuleList([res_block() for _ in range(n_res)]) | |
| def forward(self, x): | |
| for b in self.blocks: | |
| x = x + b(x) | |
| return x | |
| class Net(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.head = nn.Conv2d(3, feats, 3, padding=1) | |
| self.body = ResChain() | |
| up = [] | |
| for _ in range(max(1, scale // 2)): | |
| up += [nn.Conv2d(feats, feats * 4, 3, padding=1), | |
| nn.PixelShuffle(2), nn.PReLU()] | |
| self.up = nn.Sequential(*up) | |
| self.tail = nn.Conv2d(feats, 3, 3, padding=1) | |
| def forward(self, x): | |
| x = self.head(x) | |
| x = self.body(x) | |
| x = self.up(x) | |
| return self.tail(x) | |
| return Net() | |
| class EDSRBaseline: | |
| """EDSR-baseline: 16 residual blocks, 64 features, no BatchNorm, x2. | |
| Matches the canonical EDSR-baseline configuration used across SR research | |
| (Lim et al., CVPRW 2017) closely enough for a realistic training load. | |
| """ | |
| name = "edsr_baseline" | |
| def build(n_res: int = 16, feats: int = 64, scale: int = 2): | |
| torch = require_torch() | |
| nn = torch.nn | |
| def res(): | |
| return nn.Sequential( | |
| nn.Conv2d(feats, feats, 3, padding=1), | |
| nn.ReLU(inplace=True), | |
| nn.Conv2d(feats, feats, 3, padding=1), | |
| ) | |
| class MeanShift(nn.Module): | |
| # keep the classic EDSR mean-shift at 0 to stay simple+reversible | |
| def forward(self, x): | |
| return x | |
| class Net(nn.Module): | |
| def __init__(self): | |
| super().__init__() | |
| self.shift = MeanShift() | |
| self.head = nn.Conv2d(3, feats, 3, padding=1) | |
| self.body = nn.Sequential(*[res() for _ in range(n_res)]) | |
| self.body_conv = nn.Conv2d(feats, feats, 3, padding=1) | |
| up = [nn.Conv2d(feats, feats * 4, 3, padding=1), | |
| nn.PixelShuffle(2), nn.Conv2d(feats, feats, 3, padding=1)] \ | |
| if scale == 2 else [] | |
| self.up = nn.Sequential(*up) | |
| self.tail = nn.Conv2d(feats, 3, 3, padding=1) | |
| def forward(self, x): | |
| x = self.shift(x) | |
| x = self.head(x) | |
| x = x + self.body_conv(self.body(x)) | |
| x = self.up(x) | |
| return self.tail(x) | |
| return Net() | |
| def build_model(tier: str): | |
| if tier == "quick": | |
| return SRResNetQuick.build() | |
| if tier == "extended": | |
| return EDSRBaseline.build() | |
| raise ValueError(f"unknown benchmark tier {tier!r} (quick | extended)") | |
| def model_facts(model) -> dict: | |
| torch = require_torch() | |
| params = list(model.parameters()) | |
| n_params = sum(p.numel() for p in params) | |
| mb = sum(p.numel() * p.element_size() for p in params) / (1024 * 1024) | |
| dtype = str(params[0].dtype) if params else "unknown" | |
| return {"params": n_params, "param_mb": round(mb, 2), | |
| "dtype": dtype, "device": str(params[0].device) if params else "?"} | |
| BENCHMARK_TIERS = { | |
| "quick": { | |
| "model": "srresnet_quick", | |
| "patch_lr": 48, | |
| "scale": 2, | |
| "default_batch": 16, | |
| "description": "SRResNet 4RB, LR patch 48x48, x2 upscale", | |
| }, | |
| "extended": { | |
| "model": "edsr_baseline", | |
| "patch_lr": 64, | |
| "scale": 2, | |
| "default_batch": 8, | |
| "description": "EDSR-baseline 16RB, LR patch 64x64, x2 upscale", | |
| }, | |
| } | |