python: rename setAllowNonConventionalDataTypes() / getAllowNonConventionalDataTypes() to setUseExactNumpyDtypes() / useExactNumpyDtypes()

Signed-off-by: Abhishek Gola <abhishek.gola@bigvisionllc.com>
This commit is contained in:
Abhishek Gola
2026-10-30 16:00:13 +05:30
parent 8751eb491e
commit 26bd26e400
9 changed files with 47 additions and 47 deletions

View File

@@ -118,29 +118,29 @@ static PyObject* pycvRegisterMatType(PyObject *self, PyObject *value)
Py_RETURN_NONE;
}
static PyObject* pycvSetAllowNonConventionalDataTypes(PyObject*, PyObject* value)
static PyObject* pycvSetUseExactNumpyDtypes(PyObject*, PyObject* value)
{
int allow = PyObject_IsTrue(value);
if (allow < 0)
int flag = PyObject_IsTrue(value);
if (flag < 0)
return NULL;
setAllowNonConventionalDataTypes(allow != 0);
setUseExactNumpyDtypes(flag != 0);
Py_RETURN_NONE;
}
static PyObject* pycvGetAllowNonConventionalDataTypes(PyObject*, PyObject*)
static PyObject* pycvUseExactNumpyDtypes(PyObject*, PyObject*)
{
return PyBool_FromLong(getAllowNonConventionalDataTypes());
return PyBool_FromLong(useExactNumpyDtypes());
}
static PyMethodDef special_methods[] = {
{"_registerMatType", (PyCFunction)(pycvRegisterMatType), METH_O, "_registerMatType(cv.Mat) -> None (Internal)"},
{"redirectError", CV_PY_FN_WITH_KW(pycvRedirectError), "redirectError(onError) -> None"},
{"setAllowNonConventionalDataTypes", (PyCFunction)(pycvSetAllowNonConventionalDataTypes), METH_O,
"setAllowNonConventionalDataTypes(allow) -> None\n. Keep output bfloat16 / float8 arrays at their exact dtype "
{"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. "
"The flag is process-wide: set it in the application, libraries should not change it."},
{"getAllowNonConventionalDataTypes", (PyCFunction)(pycvGetAllowNonConventionalDataTypes), METH_NOARGS,
"getAllowNonConventionalDataTypes() -> retval"},
{"useExactNumpyDtypes", (PyCFunction)(pycvUseExactNumpyDtypes), METH_NOARGS,
"useExactNumpyDtypes() -> retval"},
#ifdef HAVE_OPENCV_HIGHGUI
{"createTrackbar", (PyCFunction)pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"},
{"createButton", CV_PY_FN_WITH_KW(pycvCreateButton), "createButton(buttonName, onChange [, userData, buttonType, initialButtonState]) -> None"},

View File

@@ -370,8 +370,8 @@ PyObject* pyopencv_from(const cv::Mat& m)
Py_INCREF(o);
return o;
}
if( getAllowNonConventionalDataTypes() )
CV_LOG_ONCE_WARNING(NULL, "Python bindings: non-conventional data types are allowed, "
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. "
"Import ml_dtypes to keep the exact dtype.");
// NumPy alone cannot name these; widen to float32 (lossless).

View File

@@ -55,10 +55,10 @@ UMatData* NumpyAllocator::allocate(int dims0, const int* sizes, int type, void*
}
// Mat::create() falls back to the default allocator on this error; pyopencv_from() then widens to float32.
if( isNonConventionalDepth(depth) )
CV_Error(Error::StsNotImplemented, getAllowNonConventionalDataTypes()
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: "
"call cv.setAllowNonConventionalDataTypes(True) first");
"call cv.setUseExactNumpyDtypes(True) first");
int typenum = cvDepthToNumpyType(depth);
PyObject* o = PyArray_SimpleNew(dims, _sizes.data(), typenum);

View File

@@ -55,7 +55,7 @@ const NonConventionalDtype kNonConventionalDtypes[] = {
};
const int kNumNonConventionalDtypes = sizeof(kNonConventionalDtypes) / sizeof(kNonConventionalDtypes[0]);
bool g_allowNonConventionalDataTypes = false;
bool g_useExactNumpyDtypes = false;
// Process-lifetime cache, accessed under the GIL. Misses are not cached, so a later
// `import ml_dtypes` still takes effect.
@@ -93,14 +93,14 @@ int nonConventionalIndex(int depth)
} // namespace
void setAllowNonConventionalDataTypes(bool allow)
void setUseExactNumpyDtypes(bool flag)
{
g_allowNonConventionalDataTypes = allow;
g_useExactNumpyDtypes = flag;
}
bool getAllowNonConventionalDataTypes()
bool useExactNumpyDtypes()
{
return g_allowNonConventionalDataTypes;
return g_useExactNumpyDtypes;
}
bool isNonConventionalDepth(int depth)
@@ -111,7 +111,7 @@ bool isNonConventionalDepth(int depth)
PyArray_Descr* nonConventionalDescrForCvDepth(int depth)
{
const int idx = nonConventionalIndex(depth);
if (!g_allowNonConventionalDataTypes || idx < 0)
if (!g_useExactNumpyDtypes || idx < 0)
return nullptr;
return nonConventionalDescr(idx);
}

View File

@@ -17,11 +17,11 @@ PyObject* failmsgp(const char *fmt, ...);
int cvDepthToNumpyType(int depth);
int numpyTypeToCvDepth(int typenum);
// Output bfloat16 / float8 Mats keep their exact dtype only when allowed (off by default).
void setAllowNonConventionalDataTypes(bool allow);
bool getAllowNonConventionalDataTypes();
// Output bfloat16 / float8 Mats keep their exact dtype only when enabled (off by default).
void setUseExactNumpyDtypes(bool flag);
bool useExactNumpyDtypes();
bool isNonConventionalDepth(int depth);
// Output descriptor: nullptr when disallowed, unregistered, or without an OpenCV counterpart.
// Output descriptor: nullptr when disabled, unregistered, or without an OpenCV counterpart.
PyArray_Descr* nonConventionalDescrForCvDepth(int depth);
// Input depth: -1 when unregistered or without an OpenCV counterpart; ignores the output flag.
int nonConventionalTypeToCvDepth(int typenum);

View File

@@ -58,10 +58,10 @@ def apply_manual_api_refinement(root: NamespaceNode) -> None:
)
])
root.add_function("setAllowNonConventionalDataTypes", [
FunctionNode.Arg("allow", PrimitiveTypeNode.bool_())
root.add_function("setUseExactNumpyDtypes", [
FunctionNode.Arg("flag", PrimitiveTypeNode.bool_())
])
root.add_function("getAllowNonConventionalDataTypes",
root.add_function("useExactNumpyDtypes",
return_type=FunctionNode.RetType(PrimitiveTypeNode.bool_()))

View File

@@ -18,24 +18,24 @@ class non_conventional_dtypes_test(NewOpenCVTests):
def setUp(self):
super(non_conventional_dtypes_test, self).setUp()
self.addCleanup(cv.setAllowNonConventionalDataTypes, cv.getAllowNonConventionalDataTypes())
cv.setAllowNonConventionalDataTypes(False)
self.addCleanup(cv.setUseExactNumpyDtypes, cv.useExactNumpyDtypes())
cv.setUseExactNumpyDtypes(False)
def test_flag_roundtrip(self):
cv.setAllowNonConventionalDataTypes(True)
self.assertTrue(cv.getAllowNonConventionalDataTypes())
cv.setAllowNonConventionalDataTypes(False)
self.assertFalse(cv.getAllowNonConventionalDataTypes())
cv.setUseExactNumpyDtypes(True)
self.assertTrue(cv.useExactNumpyDtypes())
cv.setUseExactNumpyDtypes(False)
self.assertFalse(cv.useExactNumpyDtypes())
def test_disallowed_widens_to_float32(self):
def test_disabled_widens_to_float32(self):
for depth in NON_CONVENTIONAL_DEPTHS:
for m in cv.utils.generateVectorOfMat(1, 3, 4, depth):
self.assertEqual(m.dtype, np.float32)
def test_allowed_without_ml_dtypes_widens_to_float32(self):
def test_enabled_without_ml_dtypes_widens_to_float32(self):
if ml_dtypes is not None:
self.skipTest("ml_dtypes is installed")
cv.setAllowNonConventionalDataTypes(True)
cv.setUseExactNumpyDtypes(True)
for depth in NON_CONVENTIONAL_DEPTHS:
for m in cv.utils.generateVectorOfMat(1, 3, 4, depth):
self.assertEqual(m.dtype, np.float32)
@@ -54,8 +54,8 @@ class ml_dtypes_test(NewOpenCVTests):
super(ml_dtypes_test, self).setUp()
if ml_dtypes is None:
self.skipTest("ml_dtypes is not installed")
self.addCleanup(cv.setAllowNonConventionalDataTypes, cv.getAllowNonConventionalDataTypes())
cv.setAllowNonConventionalDataTypes(True)
self.addCleanup(cv.setUseExactNumpyDtypes, cv.useExactNumpyDtypes())
cv.setUseExactNumpyDtypes(True)
# (numpy dtype, OpenCV depth, typeToString prefix, relative half-ULP)
self.cases = [
(ml_dtypes.bfloat16, cv.CV_16BF, "CV_16BF", 2.0 ** -8),
@@ -77,7 +77,7 @@ class ml_dtypes_test(NewOpenCVTests):
self.assertIn("type(-1)=%sC1" % name, cv.utils.dumpInputArray(np.array(1.5, dtype=dt)))
def test_inputs_accepted_with_outputs_disabled(self):
cv.setAllowNonConventionalDataTypes(False)
cv.setUseExactNumpyDtypes(False)
for dt, _, name, _ in self.cases:
a = np.arange(6, dtype=np.float32).reshape(2, 3).astype(dt)
self.assertIn("type(-1)=%sC1" % name, cv.utils.dumpInputArray(a))
@@ -91,8 +91,8 @@ class ml_dtypes_test(NewOpenCVTests):
def test_short_array_scalar_operands(self):
# A float32 src keeps the result float32, so the operand is checked with the flag off too.
for allow in (False, True):
cv.setAllowNonConventionalDataTypes(allow)
for exact in (False, True):
cv.setUseExactNumpyDtypes(exact)
for dt, _, _, _ in self.cases:
for channels in range(1, 5):
src = np.zeros((2, 3, channels), dtype=np.float32)