Skip to content

Commit

Permalink
Improved memory management for OrtValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 11, 2024
1 parent 6580511 commit 5957d70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
11 changes: 0 additions & 11 deletions lib/onnxruntime/inference_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,6 @@ def run(output_names, input_feed, log_severity_level: nil, log_verbosity_level:
outputs = run_with_ort_values(output_names, ort_values, log_severity_level: log_severity_level, log_verbosity_level: log_verbosity_level, logid: logid, terminate: terminate)

outputs.map { |v| output_type == :numo ? v.numo : v.to_a }
ensure
if ort_values
ort_values.each do |_, v|
release :Value, v.instance_variable_get(:@ortvalue)
end
end
if outputs
outputs.each do |v|
release :Value, v.instance_variable_get(:@ortvalue)
end
end
end

# TODO support logid
Expand Down
10 changes: 8 additions & 2 deletions lib/onnxruntime/ort_value.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module OnnxRuntime
class OrtValue
def initialize(ortvalue, binary_data = nil)
@ortvalue = ortvalue
@ortvalue = ortvalue.read_pointer
@binary_data = binary_data
ObjectSpace.define_finalizer(@ortvalue, self.class.finalize(@ortvalue.to_i))
end

def self.ortvalue_from_numo(numo_obj)
Expand Down Expand Up @@ -59,7 +60,7 @@ def to_a
private

def out_ptr
@ortvalue.read_pointer
@ortvalue
end

def value_type
Expand Down Expand Up @@ -200,6 +201,11 @@ def reshape(arr, dims)
arr.to_a
end

def self.finalize(addr)
# must use proc instead of stabby lambda
proc { FFI.api[:ReleaseValue].call(::FFI::Pointer.new(:pointer, addr)) }
end

def self.allocator_info
@allocator_info ||= begin
allocator_info = ::FFI::MemoryPointer.new(:pointer)
Expand Down

0 comments on commit 5957d70

Please sign in to comment.