-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Build] SIGSEGV calling into native library from Java on MacOS on M2 Mac #19512
Comments
Have you got multiple versions of ONNX Runtime on the classpath? It sounds like it's loaded in an incompatible version of the native library that conflicts with the class files that are loaded. For the earlier versions which didn't load the model, is that due to an unsupported opset error? Those are quite old and segment anything is quite a new model. |
Running the following through jshell on an M1 MBP works fine for me, can you check that it works in your environment?
Also it looks like that segment anything implementation is using multidimensional Java arrays for passing things back and forth, and that's going to be very slow. I recommend taking a look at the stable diffusion example which shows how to work with images using |
Very good call. I only have an explicit dependency on the onnxruntime 1.17.0 in my POM, but it turns out there was an extra secondary dependency lurking for onnxruntime-gpu 1.10.0. I traced it to my explicit dependency on Apache OpenNLP-DL version 2.1.0, which had a stray dependency on both the onnxruntime and the onnxruntime-gpu. I updated OpenNLP to version 2.3.2 and now they have just a dependency on the onnxruntime. Thank you for the suggestion to go check that! Also, I'll pass along to the developer working that SAM interoperability library. He is working actively to get the rough code complete and this will be a good tip. Many of us are very excited about the ONNX runtime for Java and hungry for more such tips and guidance. Thank you. |
The stable diffusion example is current as of ORT v1.14.0, I plan to update it to v1.17.0 when I get the chance which will involve pinning the various buffers as inputs & outputs to minimize Java side allocations. At some point after that I'll do a write up which walks through the design choices and considerations for efficient use in Java. |
I know this is now drifting quickly off topic from my initial ONNX issue, but I just borrowed and adapted your code for BxCxHxW conversion per your suggestion. It seems like many models would benefit greatly from a solid, performant BxCxHxW to BufferedImage converter (and the inverse). HAWP and Segment Anything take BxCxHxW as inputs and Segment Anything and Stable Diffusion have BxCxHxW as outputs. I'm not sure if they all have pixel float values in the range [-1.0f,1.0f], though.
|
I agree it might be useful to have some utility routines for this kind of thing, particularly as channels first vs channels last is a consistent problem with people porting their Python image processing code to Java, but I want to minimize the number of entry points in this code which operate on Java arrays. A Java 4d array of size |
Absolutely! A single, performant implementation would be a huge benefit to the community so that everyone isn't rolling their own. Having the Java ONNX runtime is so critical to Java, but good utilities to better connect to the Java ecosystem, like to BufferedImage is also crucial. Most of us using ONNX are going to have our expertise elsewhere. |
You could also consider adding the layout conversion to the model using the python tools in onnxruntime-extensions instead of implementing in Java. There are a lot of examples (we need to simplify a little as they tend to be model driven), but basically you can add pre-processing steps to the model that handle NCHW to NHWC, normalization, centered crop/letterbox, etc. This script handles a few models: https://github.com/microsoft/onnxruntime-extensions/blob/main/onnxruntime_extensions/tools/add_pre_post_processing_to_model.py But the 'steps' can be assembled as needed. e.g. something like this can do basic pre-processing of uint8 RGB input. steps = [
ImageBytesToFloat(),
ChannelsLastToChannelsFirst(),
Normalize([(0.5, 0.5)], layout="CHW"), # normalize to range -1..1
Unsqueeze([0]) # add batch dim
] Post-processing can also be added (e.g. FloatToImageBytes to convert from float back to 0..255) As they're added to the model as ONNX operators the ORT C++ implementation does the work, which should be very performant. Some basic docs for each Step in https://github.com/microsoft/onnxruntime-extensions/blob/main/onnxruntime_extensions/tools/pre_post_processing/docs/. |
Sure, adding the transform to the model will help, but something that gets the data out of a Java |
This issue has been automatically marked as stale due to inactivity and will be closed in 30 days if no further activity occurs. If further support is needed, please provide an update and/or more details. |
Describe the issue
A call to Map<String, NodeInfo> inputsInfo = model.getInputInfo(); shortly after loading an ONNX model from Java on an M2 Mac causes an immediate SIGSEGV. Calls to model.getInputNames() and model.getOutputNames() succeed and give correct results, so the native library did link and the ONNX model did load.
The SIGSEGV:
The Java code:
The Java debugger shows that the Java code crashes in the process of trying to throw a NoSuchMethodError because it can not find an expected Tensor constructor in the native code.
Tested on an M2 Mac using ONNX runtime versions from 1.10.0 through 1.17.0. Versions 14 to 17 SIGSEGV’d and versions 10 to 13 refused to load the ONNX at all. We are testing the SegmentAnything (polygonal shape) and HAWP (line segment) detection models from here:
Urgency
No
Target platform
Java 21 ONNX on ARM-based Mac M2
Build script
Error / output
Visual Studio Version
Apache Netbeans 19
GCC / Compiler Version
Java 21
The text was updated successfully, but these errors were encountered: