mirror of
https://github.com/opencv/opencv.git
synced 2026-11-16 14:27:47 -06:00
core: fall back to CPU for CV_16BF in OpenCL arithmetic - #30383 Fixes #30382 ### Problem `arithm_op()` calls the OpenCL path directly and relies on it returning `false` when it can't run. `ocl_arithm_op()` and `ocl_binary_op()` don't check the depth and pass it to `ocl::typeToStr()`, which asserts for `CV_16BF`. So `add`, `subtract`, `multiply`, `divide`, `absdiff`, `min`, `max`, `addWeighted` and add-with-scalar on a `CV_16BF` `UMat` throw, while the same calls on a `Mat` work. ### Fix In `modules/core/src/arithm.cpp`, add `noOclType()` for the depths that have no OpenCL type (`CV_16BF`, `CV_Bool`, `CV_8F_E4M3FN`, `CV_8F_E4M3FNUZ`) and return `false` for them: - `ocl_arithm_op()`: if any of the source, destination or working depths has no OpenCL type. - `ocl_binary_op()`: if the source depth has no OpenCL type and the op is not bitwise (bitwise ops use `memopTypeToStr()`, not `typeToStr()`). The CPU path then handles them, as it already does for `Mat`. Same approach as #30252 for `threshold`. ### Tests New `OCL_Arithm.bf16_falls_back_to_cpu` runs the 9 ops on `CV_16BF` `Mat` and `UMat` inputs and checks the results are identical. It fails on current 5.x with the `typeToStr` assertion and passes with this patch. All `OCL_Arithm*`, `Core_Arithm*`, the CPU add/sub/mul/div/min/max/absdiff/addWeighted tests and the `UMat` tests pass locally (7933 tests). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake