-
Notifications
You must be signed in to change notification settings - Fork 15k
[mlir:python] Prevent crash in DenseElementsAttr. #163564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir:python] Prevent crash in DenseElementsAttr. #163564
Conversation
This PR fixes a crash in the `bf_getbuffer` implementation of `PyDenseElementsAttribute` that occurred when an element type was not supported, such as `bf16`. I believe that supportion `bf16` is not possible with that protocol but that's out of the scope of this PR. Previsouly, the code raised an `std::exception` out of `bf_getbuffer` that nanobind does not catch (see also pybind/pybind11#3336). The PR makes the function catch all `std::exception`s and manually raises a Python exception instead. Signed-off-by: Ingo Müller <ingomueller@google.com>
|
@llvm/pr-subscribers-mlir Author: Ingo Müller (ingomueller-net) ChangesThis PR fixes a crash in the Full diff: https://github.com/llvm/llvm-project/pull/163564.diff 1 Files Affected:
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 045c0fbf4630f..c0a945e3f4f3b 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -1306,6 +1306,10 @@ PyType_Slot PyDenseElementsAttribute::slots[] = {
e.restore();
nb::chain_error(PyExc_BufferError, "Error converting attribute to buffer");
return -1;
+ } catch (std::exception &e) {
+ nb::chain_error(PyExc_BufferError,
+ "Error converting attribute to buffer: %s", e.what());
+ return -1;
}
view->obj = obj;
view->ndim = 1;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but I'm curious - this doesn't work even when constructing the original denseattr using ml_dtypes?
|
TBH, I have not tried but I am pretty confident that it doesn't matter how the attribute has been constructed. The It could be that the |
|
I have just stumbled upon DLPack. That's probably a good way to replace the |
nanobind's dlpack isn't "real" - they don't actually use the dlpack header and have a reimplementation. So it'll solve your bf16 problem but won't work for the other dtypes that dlpack should support (I know this because I had your same exact problem last 2 weeks ago but for f8). |
|
Right, but I think that that has a few workable solutions. For example, we can include |
Tried that, didn't work. Nanobind doesn't handle it correctly (I didn't investigate deeply).
This is the thing to do but I can't do it (I can't do it - I don't have permission to contribute to nanobind).
🤷♂️ |
This PR fixes a crash in the
bf_getbufferimplementation ofPyDenseElementsAttributethat occurred when an element type was not supported, such asbf16. I believe that supportionbf16is not possible with that protocol but that's out of the scope of this PR. Previsouly, the code raised anstd::exceptionout ofbf_getbufferthat nanobind does not catch (see also pybind/pybind11#3336). The PR makes the function catch allstd::exceptions and manually raises a Python exception instead.