python: wrap long lines in the bfloat16 / float8 binding code and tests

Signed-off-by: Abhishek Gola <abhishek.gola@bigvisionllc.com>
This commit is contained in:
Abhishek Gola
2026-10-30 16:04:56 +05:30
parent 26bd26e400
commit 782daa0ab9
6 changed files with 14 additions and 8 deletions

View File

@@ -137,7 +137,8 @@ static PyMethodDef special_methods[] = {
{"redirectError", CV_PY_FN_WITH_KW(pycvRedirectError), "redirectError(onError) -> None"},
{"setUseExactNumpyDtypes", (PyCFunction)(pycvSetUseExactNumpyDtypes), METH_O,
"setUseExactNumpyDtypes(flag) -> None\n. Keep output bfloat16 / float8 arrays at their exact dtype "
"(registered by the ml_dtypes package, which must be imported) instead of widening them to float32. Off by default. Input arrays are accepted regardless of this flag. "
"(registered by the ml_dtypes package, which must be imported) instead of widening them "
"to float32. Off by default. Input arrays are accepted regardless of this flag. "
"The flag is process-wide: set it in the application, libraries should not change it."},
{"useExactNumpyDtypes", (PyCFunction)(pycvUseExactNumpyDtypes), METH_NOARGS,
"useExactNumpyDtypes() -> retval"},

View File

@@ -372,7 +372,8 @@ PyObject* pyopencv_from(const cv::Mat& m)
}
if( useExactNumpyDtypes() )
CV_LOG_ONCE_WARNING(NULL, "Python bindings: setUseExactNumpyDtypes(True) is set, "
"but NumPy does not know bfloat16 / float8 and they are returned as float32. "
"but NumPy does not know bfloat16 / float8 and they are "
"returned as float32. "
"Import ml_dtypes to keep the exact dtype.");
// NumPy alone cannot name these; widen to float32 (lossless).
cv::Mat m32f;

View File

@@ -53,10 +53,12 @@ UMatData* NumpyAllocator::allocate(int dims0, const int* sizes, int type, void*
CV_Error_(Error::StsError, ("The numpy array of depth=%d, ndims=%d can not be created", depth, dims));
return allocate(eo, dims0, sizes, type, step);
}
// Mat::create() falls back to the default allocator on this error; pyopencv_from() then widens to float32.
// Mat::create() falls back to the default allocator on this error;
// pyopencv_from() then widens the result to float32.
if( isNonConventionalDepth(depth) )
CV_Error(Error::StsNotImplemented, useExactNumpyDtypes()
? "bfloat16 / float8 arrays cannot be allocated through the NumPy allocator: import ml_dtypes first"
? "bfloat16 / float8 arrays cannot be allocated through the NumPy allocator: "
"import ml_dtypes first"
: "bfloat16 / float8 arrays cannot be allocated through the NumPy allocator: "
"call cv.setUseExactNumpyDtypes(True) first");

View File

@@ -53,7 +53,8 @@ const NonConventionalDtype kNonConventionalDtypes[] = {
{ CV_8F_E4M3FN, "float8_e4m3fn", 1 },
{ CV_8F_E4M3FNUZ, "float8_e4m3fnuz", 1 },
};
const int kNumNonConventionalDtypes = sizeof(kNonConventionalDtypes) / sizeof(kNonConventionalDtypes[0]);
const int kNumNonConventionalDtypes =
sizeof(kNonConventionalDtypes) / sizeof(kNonConventionalDtypes[0]);
bool g_useExactNumpyDtypes = false;