fix: dtype alignment for attention mask and add bf16 support - #14
Conversation
DAT/HAT/SwinIR window attention added the float32 attention mask and relative position bias directly onto a half-precision attn tensor, which type-promoted the result back to float32 and then failed at the attn @ v matmul with "expected scalar type Half but found Float" (same under bf16). Cast both to attn.dtype before adding. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a bf16 flag alongside fp16 to the model base class and AutoModel (from_pretrained / from_config). Weights and inputs are cast through a shared half_dtype, with bf16 taking precedence over fp16. bf16 has the same dynamic range as fp32, so it avoids the fp16 overflow that produces NaN/black output on transformer SR models (e.g. DAT, SwinIR) while keeping fp16's memory savings. Also cast model output back to float32 before converting to numpy, since numpy has no bfloat16 dtype. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adds bfloat16 (bf16) support alongside fp16 for inference, aiming to reduce overflow/NaN issues on some SR/transformer models while ensuring outputs can be converted to NumPy safely.
Changes:
- Add
bf16option to model construction APIs and plumb it through to base model setup. - Standardize half-precision casting via
self.half_dtypeand update inference input/output conversions accordingly. - Ensure attention bias/mask tensors match attention dtype to avoid dtype-mismatch errors in half precision.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
cccv/model/base_model.py |
Introduces bf16 flag, half_dtype, and applies half-precision casting to model weights. |
cccv/auto/model.py |
Exposes bf16 in factory functions and forwards it to model constructors. |
cccv/model/sr_base_model.py |
Casts inputs to half_dtype and converts outputs to float32 before NumPy. |
cccv/model/vsr_base_model.py |
Updates video SR inference to use half_dtype and float32 conversion before NumPy. |
cccv/model/vfi/rife_model.py |
Updates VFI inference to use half_dtype and float32 conversion before NumPy. |
cccv/model/vfi/drba_model.py |
Updates VFI inference to use half_dtype and float32 conversion before NumPy. |
cccv/arch/sr/swinir_arch.py |
Casts attention bias/mask to attn.dtype for half-precision compatibility. |
cccv/arch/sr/hat_arch.py |
Casts attention bias/mask to attn.dtype for half-precision compatibility. |
cccv/arch/sr/dat_arch.py |
Casts attention bias/mask to attn.dtype for half-precision compatibility. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Code Review
This pull request introduces support for bf16 (bfloat16) precision across various models and architectures, updating model initialization, inference pipelines, and attention mechanisms. Feedback on the changes suggests adding a check using torch.cuda.is_bf16_supported() during initialization to prevent runtime crashes on older GPUs that do not support bfloat16, falling back to fp16 if necessary.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #14 +/- ##
==========================================
- Coverage 92.76% 91.91% -0.86%
==========================================
Files 40 40
Lines 1216 1298 +82
==========================================
+ Hits 1128 1193 +65
- Misses 88 105 +17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
need a unit test for BF16 @NULL204 |
Cover the bf16 code path end to end (AutoModel.from_config(bf16=True) -> weight/input cast -> inference -> numpy output) with an SSIM + output-size assertion, mirroring test_sr_fp16. Runs on CPU in CI. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
CPU bfloat16 replication padding (used by tile inference) isn't implemented in the torch build CI runs on, so test_sr_bf16 errored there. test_sr_fp16 already guards CPU half-precision with skipif(not torch_2_4); apply the same guard so both half-precision tests run only on the pinned dev torch (GPU), not CI's CPU build. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Justin62628
left a comment
There was a problem hiding this comment.
Inference of bf16 on older GPUs not tested. Add fallbacks of disabling bf16 with a test inference (e.g. a 540p inference before actual run, if this inference round fails, disable bf16 as fallback).
Transformer Arch modification not tested. Add DAT, HAT, SwinIR unit test.
|
please click |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
| tile_pad: int = 8, | ||
| pad_img: Optional[Tuple[int, int]] = None, | ||
| bf16_preflight: bool = True, | ||
| bf16_preflight_size: Tuple[int, int] = (540, 960), |
There was a problem hiding this comment.
I think use 64*64 can fasten fallback
| assert compare_image_size(img1, img2, cfg.scale) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not torch_2_4, reason="Skip test if PyTorch version is not 2.4") |
There was a problem hiding this comment.
This pull request adds support for bfloat16 (bf16) precision throughout the model loading and inference pipeline, providing users with an alternative to fp16 for half-precision inference. It also ensures proper dtype handling in attention modules and data conversions, improving numerical stability and compatibility with various hardware and models.
Precision and dtype support:
bf16(bfloat16) option to model loading interfaces (from_pretrained,from_config, andCCBaseModel), which takes precedence overfp16and is used throughout the codebase for half-precision inference. This includes updating documentation and parameter lists to reflect the new option. [1] [2] [3] [4] [5] [6] [7] [8] [9]torch.bfloat16ortorch.float16), with fallback to fp32 if half-precision is not supported.Attention module stability: