Skip to content

Solve error: ambiguous overload for ‘operator!=’ (operand types are ‘__half’ and ‘double’)#23948

Closed
jiapei100 wants to merge 2 commits intoopencv:4.xfrom
LongerVision:4.x
Closed

Solve error: ambiguous overload for ‘operator!=’ (operand types are ‘__half’ and ‘double’)#23948
jiapei100 wants to merge 2 commits intoopencv:4.xfrom
LongerVision:4.x

Conversation

@jiapei100
Copy link
Copy Markdown

@jiapei100 jiapei100 commented Jul 7, 2023

**Remove the error: error: ambiguous overload for ‘operator!=’ (operand types are ‘__half’ and ‘double’) **

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • 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

@asmorkalov asmorkalov changed the title Remove the error: error: ambiguous overload for ‘operator!=’ (operand… Solve error: ambiguous overload for ‘operator!=’ (operand types are ‘__half’ and ‘double’) Jul 11, 2023
@asmorkalov asmorkalov requested a review from mshabunin July 11, 2023 07:54
@asmorkalov asmorkalov added this to the 4.9.0 milestone Jul 11, 2023
@asmorkalov
Copy link
Copy Markdown
Contributor

@mshabunin could you take a look?

@mshabunin
Copy link
Copy Markdown
Contributor

@jiapei100
Copy link
Copy Markdown
Author

@mshabunin
Could be the reasons:

  • Ubuntu 20.04 vs. Ubuntu 22.04
  • Cuda 11.7 vs. Cuda 12.2

?????

BTW, are you building everything on your own OpenCV CI ?? Everything automatically??
That is cool...

* or we don't have to scale
*/
if (weight != (T)(1.0))
if (weight != static_cast(T)(1.0))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong static_cast syntax, please refer to cppreference for more information about how it works.

static_cast<T>(1.0)

@mshabunin
Copy link
Copy Markdown
Contributor

mshabunin commented Jul 12, 2023

Perhaps original issue is no caused by missing type case, but by ambiguous constant type. Try to change to 1.f and 0.f.

@opencv-alalek opencv-alalek added the category: gpu/cuda (contrib) OpenCV 4.0+: moved to opencv_contrib label Jul 13, 2023
@cudawarped
Copy link
Copy Markdown
Contributor

Perhaps original issue is no caused by missing type case, but by ambiguous constant type. Try to change to 1.f and 0.f.

The static cast fixes the issue with CUDA 12.2 but due to the ambiguous constant type you mentioned causes an issue with earlier versions of CUDA.

The combination of both, i.e.

if (weight != static_cast<T>(1.0f))
if(nms_iou_threshold > static_cast<T>(0.0f))

or double

if (weight != static_cast<T>(1.0))
if(nms_iou_threshold > static_cast<T>(0.0))

works on Windows with Visual Studio 2022 for CUDA 12.1 (old math api) and CUDA 12.2.

@asmorkalov asmorkalov mentioned this pull request Jul 13, 2023
4 tasks
@Zacrain
Copy link
Copy Markdown

Zacrain commented Jul 14, 2023

Perhaps original issue is no caused by missing type case, but by ambiguous constant type. Try to change to 1.f and 0.f.

The static cast fixes the issue with CUDA 12.2 but due to the ambiguous constant type you mentioned causes an issue with earlier versions of CUDA.

The combination of both, i.e.

if (weight != static_cast<T>(1.0f))
if(nms_iou_threshold > static_cast<T>(0.0f))

or double

if (weight != static_cast<T>(1.0))
if(nms_iou_threshold > static_cast<T>(0.0))

works on Windows with Visual Studio 2022 for CUDA 12.1 (old math api) and CUDA 12.2.

If I am not mistaken, ISO C++ is clear about default primitive types of constant literals. 1 defaults to int and can be implicitly type-casted when assigning to, e.g,, double, 1.0 defaults to double, 1.0f to float and so on. So there should not be an "ambiguous constant type".
Therefore it shouldn't be necessary to specifically indicate the use of double or float when it is static_cast-ed to the target template type anyway. Maybe there are conversion functions missing for the "primitive" data types added by CUDA?
At this point I wonder whether this might be a problem of nvcc, although I couldn't find any info about that. Maybe that's intentional for some reason?

@cudawarped
Copy link
Copy Markdown
Contributor

If I am not mistaken, ISO C++ is clear about default primitive types of constant literals. 1 defaults to int and can be implicitly type-casted when assigning to, e.g,, double, 1.0 defaults to double, 1.0f to float and so on. So there should not be an "ambiguous constant type".
Therefore it shouldn't be necessary to specifically indicate the use of double or float when it is static_cast-ed to the target template type anyway. Maybe there are conversion functions missing for the "primitive" data types added by CUDA?
At this point I wonder whether this might be a problem of nvcc, although I couldn't find any info about that. Maybe that's intentional for some reason?

I think the use of the word ambiguous has itself become ambiguous!

I may be wrong but my interpretation of the build error

Ubuntu2004-x64-CUDA / BuildAndTest output

@jiapei100 , our CI CUDA builds have failed: https://github.com/opencv/opencv/actions/runs/5489317969/jobs/10008696594?pr=23948

/home/ci/opencv/modules/dnn/src/layers/../cuda4dnn/primitives/region.hpp:124:37: error: call of overloaded '__half(int)' is ambiguous
124 | if (nms_iou_threshold > (T)(0)) {
| ^~~~~~
In file included from /usr/local/cuda/include/cuda_fp16.h:3790,
from /usr/local/cuda/include/cublas_api.h:75,
from /usr/local/cuda/include/cublas_v2.h:65,
from /home/ci/opencv/modules/dnn/src/layers/../cuda4dnn/csl/cublas.hpp:14,
from /home/ci/opencv/modules/dnn/src/layers/../op_cuda.hpp:11,
from /home/ci/opencv/modules/dnn/src/layers/region_layer.cpp:44:
/usr/local/cuda/include/cuda_fp16.hpp:202:25: note: candidate: '__half::__half(double)'
202 | CUDA_HOSTDEVICE __half(const double f) { __x = __double2half(f).__x; }
| ^~~~~~
/usr/local/cuda/include/cuda_fp16.hpp:201:25: note: candidate: '__half::__half(float)'
201 | CUDA_HOSTDEVICE __half(const float f) { __x = __float2half(f).__x; }
| ^~~~~~
/usr/local/cuda/include/cuda_fp16.hpp:179:26: note: candidate: 'constexpr __half::__half(const __half&)'
179 | struct CUDA_ALIGN(2) __half {
| ^~~~~~

is that there is no conversion from int to half in CUDA 12.1 so it ambiguous as to whether it should cast the constant 0 to a float or a double as theses are the only two overloads available.

@cudawarped
Copy link
Copy Markdown
Contributor

@mshabunin
Could be the reasons:

Ubuntu 20.04 vs. Ubuntu 22.04
Cuda 11.7 vs. Cuda 12.2

@jiapei100 The CI uses an older version of CUDA, can you apply the changes suggested by @mshabunin in addition to your changes, e.g.

if (weight != static_cast<T>(1.0f))
if (nms_iou_threshold > static_cast<T>(0.0f))

to this PR to see if it compiles successfully so we can update OpenCV to be compatilble with CUDA 12.2 please.

@ibrewster
Copy link
Copy Markdown

For what it's worth, when I tried manually applying the suggested changes to my copy of the source on my Ubuntu 22.04 box with Cuda 12.2, it did resolve the errors being generated by those lines, however I ran into other issues later in compilation with different files (looks like the python bindings). Different issue, so not including the details unless requested...

@cudawarped
Copy link
Copy Markdown
Contributor

@jiapei100 Kindly asking you to fix this PR so that OpenCV can support the latest version of CUDA.

@cudawarped
Copy link
Copy Markdown
Contributor

@VadimLevin, @mshabunin is it possilble for the maintainers to apply the fixes discussed here (also shown here on my forked branch) to see if this fixes the issue and passes all the relavent CI tests.

CUDA 12.2 was released over a month ago with update 1 released yesterday and @jiapei100 seems to have gone awol. It seems a shame to let this fix go stale when 🤞 it should be such a simple fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants