Add --precision fp16 for GPUs without fast bf16 (T4, V100)

#5
by JorisViaud - opened

hecate.py offers --precision fp32 (default) and bf16. GPUs before Ampere, such as the free-Colab T4, have no fast bf16 path: on a T4, bf16 is slower than fp32.

Summary

  • predict() accepts precision='fp16' and runs torch.autocast('cuda', dtype=torch.float16).
  • fp16 and bf16 are both refused on CPU with "bf16 and fp16 require CUDA; otherwise use fp32".
  • --precision gains fp16 and a help string; default remains fp32.
  • One sentence added in README.md.
  • Note on discussion #3 (--resample): #3 adds --resample on the line right after --precision, so whichever merges second will have a one-line argparse context conflict (verified cleanly against refs/pr/3 @ f4386b19).

Parity Measurements (fp16 vs fp32)

1. Both checkpoints (2.4 um and 9.6 um), 2D --output & 3D --output-3d, forward & --reverse (RTX 4060 Laptop GPU, torch 2.10.0+cu128, --batch-size 1, [64, 256, 256] structural crops from public PHerc0846A 2.403 um CT & PHerc0306B 8.640 um control)

Run Seconds (fp16 / fp32) 2D max |Ξ”| (uint8) 2D mean |Ξ”| 2D r 3D max |Ξ”| (uint8) 3D mean |Ξ”| 3D r
2.4 um forward 5.22 / 5.34 1 0.004013 0.999647 1 0.000589 0.999866
2.4 um --reverse 6.22 / 5.83 1 0.003311 0.999683 1 0.000539 0.999755
9.6 um forward 9.18 / 12.24 1 0.000732 0.999135 1 0.000125 0.999959
9.6 um --reverse 9.46 / 11.90 1 0.000931 0.999545 1 0.000126 0.999953

Peak allocated+reserved GPU delta dropped 1940 -> 1590 MiB (2.4 um) and 972 -> 944 MiB (9.6 um) at batch 1. All 8 comparisons differ by at most 1 uint8 level across both 2D PNG and 3D Zarr outputs.

2. Colab T4 timing (torch 2.14.1+cu130, hecate_9.6um.pth, 2D --output, --batch-size 32, 27 x 999 x 999 public PHerc. 0841 crop, fresh process per run)

precision seconds vs fp32 output (uint8 levels)
fp16 41.2 max 1, mean 0.047, r = 0.99999
fp32 (default) 123.6 β€”
bf16 228.6 max 11, mean 0.321, r = 0.999893

On CPU, --precision fp16 raises ValueError('bf16 and fp16 require CUDA; otherwise use fp32'), and default fp32 behavior is unchanged. MIT license preserved, checkpoints untouched.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment