[code-reflection] RFR: Model lifetimes of onnx session-related objects more explicitly

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Feb 28 22:24:03 UTC 2025


On Fri, 28 Feb 2025 21:05:49 GMT, Paul Sandoz <psandoz at openjdk.org> wrote:

> I think you are on to something here. I am wondering if we can refame it like this:
> 
> ```
> try (OnnxRuntime or = OnnxRuntime.of(MethodHandles.lookup(), Area.ofConfined()) {
>   or.execute(() -> cnn(imageTensor))
> }
> ```
> 
> ?
> 
> I realize there can be only one runtime instance of a native ONNX runtime but i don't think we should be beholden to that constraint regarding the scoping of executing one or more models and with tensor memory under same lookup and arena (and even execution mode). Maybe there is a better name than `OnnxRuntime` to avoid any confusion.

I think you are correct in that the high-level API we want doesn't really need to expose all this ONNX "cruft" (e.g. it seems neither high-level nor low-level enough). From the perspective of the Java client, there should be some kind of ONNX "session" (which might, or might not be related to the ONNX session struct in the C API) -- which has a user-defined lifetime. The only interesting method in this session object is "run" and when you "close", the lifetime ends, and all the resources are garbage collected.

Maybe we can even do something like:


try (OnnxSession onnx = OnnxSession.of()) {
  or.execute(lookup, () -> cnn(imageTensor))
}


And hide the confined arena creation inside the API alltogether. (I'm not sure where `lookup` goes).

-------------

PR Comment: https://git.openjdk.org/babylon/pull/332#issuecomment-2691645157


More information about the babylon-dev mailing list