Skip to content

Commit

Permalink
Merge pull request #34 from GunnarFarneback/highlevel_release
Browse files Browse the repository at this point in the history
High level release function
  • Loading branch information
jw3126 authored Oct 19, 2023
2 parents 4688a1a + 9260fe2 commit 5ed108b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ONNXRunTime"
uuid = "e034b28e-924e-41b2-b98f-d2bbeb830c6a"
authors = ["Jan Weidner <jw3126@gmail.com> and contributors"]
version = "0.4.0"
version = "0.4.1"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ julia> import CUDA, cuDNN
julia> ORT.load_inference(path, execution_provider=:cuda)
```

Memory allocated by a model is eventually automatically released after
it goes out of scope, when the model object is deleted by the garbage
collector. It can also be immediately released with `release(model)`.

The low level API mirrors the offical [C-API](https://github.com/microsoft/onnxruntime/blob/v1.8.1/include/onnxruntime/core/session/onnxruntime_c_api.h#L347). The above example looks like this:
```julia
using ONNXRunTime.CAPI
Expand Down
25 changes: 24 additions & 1 deletion src/highlevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ end

using .CAPI
using .CAPI: juliatype, EXECUTION_PROVIDERS
export InferenceSession, load_inference
export InferenceSession, load_inference, release

"""
$TYPEDEF
Expand Down Expand Up @@ -146,6 +146,7 @@ function (o::InferenceSession)(
inputs,
output_names=nothing
)
isalive(o) || error("Session has been released and can no longer be called.")
if output_names === nothing
output_names = @__MODULE__().output_names(o)
end
Expand Down Expand Up @@ -179,3 +180,25 @@ function (o::InferenceSession)(
output_tensors = Run(o.api, o.session, run_options, inp_names, input_tensors, output_names)
make_output(o, inputs, output_names, output_tensors)
end

"""
release(o::InferenceSession)::Nothing
Release memory allocated to an [`InferenceSession`](@ref). This also
happens automatically when the object has gone out of scope and the
garbage collector deletes it.
However, there is no guarantee when that happens, so it can be useful
to manually release the memory. This is especially true when the model
has allocated GPU memory, which does not put pressure on the garbage
collector to run promptly.
Using the inference session after releasing is an error.
"""
function release(o::InferenceSession)
CAPI.release(o.api, o.session)
CAPI.release(o.api, o.meminfo)
CAPI.release(o.api, o.allocator)
end

isalive(o::InferenceSession) = all(CAPI.isalive, (o.session, o.meminfo, o.allocator))
1 change: 1 addition & 0 deletions test/test_cuda_extension.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ end
using ONNXRunTime
load_inference("$(onnx_path)", execution_provider = :cpu)
"""
@test success(run(`julia +1.9 --project=$(env) -e "$(test_script)"`))
# CUDA not loaded. Well, cuDNN pulls in CUDA so this passes anyway.
test_script = """
using ONNXRunTime
Expand Down
9 changes: 9 additions & 0 deletions test/test_highlevel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ using ONNXRunTime: juliatype
@test out.x_plus_1 x .+ 1
@test out.y_plus_2 y .+ 2
end
@testset "Release session" begin
path = ORT.testdatapath("increment2x3.onnx")
model = ORT.load_inference(path, execution_provider=:cpu)
input = randn(Float32, 2, 3)
y = model((;input))
release(model)
@test_throws ErrorException y = model((;input))
@test_throws "Session has been released and can no longer be called." y = model((;input))
end
end


Expand Down

2 comments on commit 5ed108b

@jw3126
Copy link
Owner Author

@jw3126 jw3126 commented on 5ed108b Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/93711

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.1 -m "<description of version>" 5ed108bac71213b530934b5a781bb37341c3d8fd
git push origin v0.4.1

Please sign in to comment.