🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(models): convert EfficientViT's attention output back to the input dtype (#5793)
Shiven Solankie765373f61fa
diff --git a/README.md b/README.mdindex f1c1b17cf..0b9cdb398 100644--- a/README.md+++ b/README.md@@ -112,7 +112,7 @@ covers CUDA or MPS half precision.| `kornia.losses` | ⚠️ | ⚠️ | 3 / 4 | Dice averaging overflows to inf/NaN in float16; mutual information range check; bfloat16 Dice weighting and total variation || `kornia.feature` | ✅ | ✅ | 0 / 0 | Matching uses a manual `cdist` fallback for half dtypes; LightGlue's float16 tests are skipped, so that path is unmeasured || `kornia.metrics` | ✅ | ⚠️ | 0 / 1 | bfloat16: `ssim3d` accuracy |-| `kornia.models` | ⚠️ | ⚠️ | 9 / 1 | EfficientViT raises dtype mismatches (float16); bfloat16 RT-DETR RepVGG fusion accuracy |+| `kornia.models` | ✅ | ⚠️ | 0 / 1 | bfloat16 RT-DETR RepVGG fusion accuracy || `contrib`, `core`, `io`, `onnx`, `sensors`, `tracking`, `utils` | ✅ | ⚠️ | 0 / 3 | bfloat16: histogram matching, `_torch_svd_cast`, camera-model projection |✅ No known CPU failures ⚠️ Runs, with known failures (mostly accuracy; notes name the ops that raise)diff --git a/changelog.d/5793.fixed.md b/changelog.d/5793.fixed.mdnew file mode 100644index 000000000..96d65b993--- /dev/null+++ b/changelog.d/5793.fixed.md@@ -0,0 +1,4 @@+`EfficientViT` now runs with float16 weights on CPU. `LiteMLA.relu_linear_att` lifts a float16 input to float32 for its+matrix products and returned the float32 result to a float16 projection layer, which raised+`RuntimeError: expected scalar type Float but found Half`. The result is now converted back to the input dtype.+float32 and bfloat16 results are unchanged.diff --git a/docs/source/get-started/precision.rst b/docs/source/get-started/precision.rstindex d15a348ef..b65b1dc32 100644--- a/docs/source/get-started/precision.rst+++ b/docs/source/get-started/precision.rst@@ -145,10 +145,10 @@ precision, so the table says nothing about those backends.- 0 / 1- bfloat16: ``ssim3d`` accuracy.* - ``kornia.models``+ - ✅ Yes- ⚠️ Partial- - ⚠️ Partial- - 9 / 1- - float16: EfficientViT raises dtype-mismatch errors. bfloat16: RT-DETR RepVGG deployment fusion misses+ - 0 / 1+ - bfloat16: RT-DETR RepVGG deployment fusion missesaccuracy.* - ``contrib``, ``core``, ``io``, ``onnx``, ``sensors``, ``tracking``, ``utils``- ✅ Yesdiff --git a/kornia/models/efficient_vit/nn/ops.py b/kornia/models/efficient_vit/nn/ops.pyindex 6122e4244..371e5f6d4 100644--- a/kornia/models/efficient_vit/nn/ops.py+++ b/kornia/models/efficient_vit/nn/ops.py@@ -472,6 +472,8 @@ class LiteMLA(nn.Module):def relu_linear_att(self, qkv: torch.Tensor) -> torch.Tensor:B, _, H, W = list(qkv.size())+ dtype = qkv.dtype+if qkv.dtype == torch.float16:qkv = qkv.float()@@ -492,7 +494,7 @@ class LiteMLA(nn.Module):out = out[..., :-1] / (out[..., -1:] + self.eps)out = torch.transpose(out, -1, -2)- return torch.reshape(out, (B, -1, H, W))+ return torch.reshape(out, (B, -1, H, W)).to(dtype)def forward(self, x: torch.Tensor) -> torch.Tensor:"""Apply lightweight multi-scale linear attention.diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txtindex 3f946b518..ca81f06b5 100644--- a/testing/half_precision_xfails/cpu_float16.txt+++ b/testing/half_precision_xfails/cpu_float16.txt@@ -312,12 +312,3 @@ call builtins.AssertionError tests/integration/test_conversions.py::TestQuaternicall builtins.AssertionError tests/losses/test_dice.py::TestDiceLoss::test_averaging_macro[cpu-float16]call builtins.AssertionError tests/losses/test_dice.py::TestDiceLoss::test_averaging_micro[cpu-float16]call builtins.AssertionError tests/losses/test_mutual_information.py::TestMutualInformationLoss::test_value_ranges[cpu-float16]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-128-4-b0]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-128-4-b1]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-128-4-b2]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-64-2-b0]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-64-2-b1]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-64-2-b2]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-96-3-b0]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-96-3-b1]-call builtins.RuntimeError tests/models/test_efficient_vit.py::TestEfficientViT::test_smoke[cpu-float16-96-3-b2]