Class: OnnxRuntime::InferenceSession

Inherits:
Object
  • Object
show all
Defined in:
lib/onnxruntime/inference_session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_or_bytes, enable_cpu_mem_arena: true, enable_mem_pattern: true, enable_profiling: false, execution_mode: nil, free_dimension_overrides_by_denotation: nil, free_dimension_overrides_by_name: nil, graph_optimization_level: nil, inter_op_num_threads: nil, intra_op_num_threads: nil, log_severity_level: nil, log_verbosity_level: nil, logid: nil, optimized_model_filepath: nil, profile_file_prefix: nil, session_config_entries: nil, providers: []) ⇒ InferenceSession

Returns a new instance of InferenceSession.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/onnxruntime/inference_session.rb', line 5

def initialize(path_or_bytes, enable_cpu_mem_arena: true, enable_mem_pattern: true, enable_profiling: false, execution_mode: nil, free_dimension_overrides_by_denotation: nil, free_dimension_overrides_by_name: nil, graph_optimization_level: nil, inter_op_num_threads: nil, intra_op_num_threads: nil, log_severity_level: nil, log_verbosity_level: nil, logid: nil, optimized_model_filepath: nil, profile_file_prefix: nil, session_config_entries: nil, providers: [])
  # create environment first to prevent uncaught exception with CoreMLExecutionProvider
  env

  # session options
  session_options = Pointer.new(api[:ReleaseSessionOptions])
  check_status api[:CreateSessionOptions].call(session_options.ref)

  if enable_cpu_mem_arena
    check_status api[:EnableCpuMemArena].call(session_options)
  else
    check_status api[:DisableCpuMemArena].call(session_options)
  end
  if enable_mem_pattern
    check_status api[:EnableMemPattern].call(session_options)
  else
    check_status api[:DisableMemPattern].call(session_options)
  end
  if enable_profiling
    check_status api[:EnableProfiling].call(session_options, ort_string(profile_file_prefix || "onnxruntime_profile_"))
  else
    check_status api[:DisableProfiling].call(session_options)
  end
  if execution_mode
    execution_modes = {sequential: 0, parallel: 1}
    mode = execution_modes[execution_mode]
    raise ArgumentError, "Invalid execution mode" unless mode
    check_status api[:SetSessionExecutionMode].call(session_options, mode)
  end
  if free_dimension_overrides_by_denotation
    free_dimension_overrides_by_denotation.each do |k, v|
      check_status api[:AddFreeDimensionOverride].call(session_options, k.to_s, v)
    end
  end
  if free_dimension_overrides_by_name
    free_dimension_overrides_by_name.each do |k, v|
      check_status api[:AddFreeDimensionOverrideByName].call(session_options, k.to_s, v)
    end
  end
  if graph_optimization_level
    optimization_levels = {none: 0, basic: 1, extended: 2, all: 99}
    level = optimization_levels[graph_optimization_level]
    raise ArgumentError, "Invalid graph optimization level" unless level
    check_status api[:SetSessionGraphOptimizationLevel].call(session_options, level)
  end
  check_status api[:SetInterOpNumThreads].call(session_options, inter_op_num_threads) if inter_op_num_threads
  check_status api[:SetIntraOpNumThreads].call(session_options, intra_op_num_threads) if intra_op_num_threads
  check_status api[:SetSessionLogSeverityLevel].call(session_options, log_severity_level) if log_severity_level
  check_status api[:SetSessionLogVerbosityLevel].call(session_options, log_verbosity_level) if log_verbosity_level
  check_status api[:SetSessionLogId].call(session_options, logid) if logid
  check_status api[:SetOptimizedModelFilePath].call(session_options, ort_string(optimized_model_filepath)) if optimized_model_filepath
  if session_config_entries
    session_config_entries.each do |k, v|
      check_status api[:AddSessionConfigEntry].call(session_options, k.to_s, v.to_s)
    end
  end
  providers.each do |provider|
    unless self.providers.include?(provider)
      warn "Provider not available: #{provider}"
      next
    end

    case provider
    when "CUDAExecutionProvider"
      cuda_options = Pointer.new(api[:ReleaseCUDAProviderOptions])
      check_status api[:CreateCUDAProviderOptions].call(cuda_options.ref)
      check_status api[:SessionOptionsAppendExecutionProvider_CUDA_V2].call(session_options, cuda_options)
    when "CoreMLExecutionProvider"
      unless FFI.respond_to?(:OrtSessionOptionsAppendExecutionProvider_CoreML)
        raise ArgumentError, "Provider not available: #{provider}"
      end

      coreml_flags = 0
      check_status FFI.OrtSessionOptionsAppendExecutionProvider_CoreML(session_options, coreml_flags)
    when "CPUExecutionProvider"
      break
    else
      raise ArgumentError, "Provider not supported: #{provider}"
    end
  end

  @session = load_session(path_or_bytes, session_options)
  @allocator = Utils.allocator
  @inputs = load_inputs
  @outputs = load_outputs
end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



3
4
5
# File 'lib/onnxruntime/inference_session.rb', line 3

def inputs
  @inputs
end

#outputsObject (readonly)

Returns the value of attribute outputs.



3
4
5
# File 'lib/onnxruntime/inference_session.rb', line 3

def outputs
  @outputs
end

Class Method Details

.apiObject



293
294
295
# File 'lib/onnxruntime/inference_session.rb', line 293

def self.api
  FFI.api
end

Instance Method Details

#end_profilingObject

return value has double underscore like Python



179
180
181
182
183
# File 'lib/onnxruntime/inference_session.rb', line 179

def end_profiling
  out = Pointer.new(method(:allocator_free))
  check_status api[:SessionEndProfiling].call(@session, @allocator, out.ref)
  out.read_string
end

#modelmetaObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/onnxruntime/inference_session.rb', line 130

def modelmeta
   = Pointer.new(api[:ReleaseModelMetadata])
  check_status api[:SessionGetModelMetadata].call(@session, .ref)

  keys = Pointer.new(method(:allocator_free))
  num_keys = ::FFI::MemoryPointer.new(:int64_t)
  check_status api[:ModelMetadataGetCustomMetadataMapKeys].call(, @allocator, keys.ref, num_keys)
  key_ptrs =
    keys.read_array_of_pointer(num_keys.read(:int64_t)).map do |ptr|
      ::FFI::AutoPointer.new(ptr, method(:allocator_free))
    end

   =
    key_ptrs.to_h do |key|
      value = Pointer.new(method(:allocator_free))
      check_status api[:ModelMetadataLookupCustomMetadataMap].call(, @allocator, key, value.ref)
      [key.read_string, value.read_string]
    end

  description = Pointer.new(method(:allocator_free))
  check_status api[:ModelMetadataGetDescription].call(, @allocator, description.ref)

  domain = Pointer.new(method(:allocator_free))
  check_status api[:ModelMetadataGetDomain].call(, @allocator, domain.ref)

  graph_name = Pointer.new(method(:allocator_free))
  check_status api[:ModelMetadataGetGraphName].call(, @allocator, graph_name.ref)

  graph_description = Pointer.new(method(:allocator_free))
  check_status api[:ModelMetadataGetGraphDescription].call(, @allocator, graph_description.ref)

  producer_name = Pointer.new(method(:allocator_free))
  check_status api[:ModelMetadataGetProducerName].call(, @allocator, producer_name.ref)

  version = ::FFI::MemoryPointer.new(:int64_t)
  check_status api[:ModelMetadataGetVersion].call(, version)

  {
    custom_metadata_map: ,
    description: description.read_string,
    domain: domain.read_string,
    graph_name: graph_name.read_string,
    graph_description: graph_description.read_string,
    producer_name: producer_name.read_string,
    version: version.read(:int64_t)
  }
end

#providersObject

no way to set providers with C API yet so we can return all available providers



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/onnxruntime/inference_session.rb', line 187

def providers
  out_ptr = Pointer.new
  length_ptr = ::FFI::MemoryPointer.new(:int)
  check_status api[:GetAvailableProviders].call(out_ptr.ref, length_ptr)
  length = length_ptr.read_int
  begin
    out_ptr.read_array_of_pointer(length).map(&:read_string)
  ensure
    api[:ReleaseAvailableProviders].call(out_ptr, length)
  end
end

#run(output_names, input_feed, log_severity_level: nil, log_verbosity_level: nil, logid: nil, terminate: nil, output_type: :ruby) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/onnxruntime/inference_session.rb', line 92

def run(output_names, input_feed, log_severity_level: nil, log_verbosity_level: nil, logid: nil, terminate: nil, output_type: :ruby)
  if ![:ruby, :numo, :ort_value].include?(output_type)
    raise ArgumentError, "Invalid output type: #{output_type}"
  end

  ort_values = input_feed.keys.zip(create_input_tensor(input_feed)).to_h

  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 : (output_type == :ort_value ? v : v.to_ruby) }
end

#run_with_ort_values(output_names, input_feed, log_severity_level: nil, log_verbosity_level: nil, logid: nil, terminate: nil) ⇒ Object

TODO support logid



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/onnxruntime/inference_session.rb', line 105

def run_with_ort_values(output_names, input_feed, log_severity_level: nil, log_verbosity_level: nil, logid: nil, terminate: nil)
  input_tensor = ::FFI::MemoryPointer.new(:pointer, input_feed.size)
  input_tensor.write_array_of_pointer(input_feed.values)

  output_names ||= @outputs.map { |v| v[:name] }

  output_tensor = ::FFI::MemoryPointer.new(:pointer, outputs.size)
  refs = []
  input_node_names = create_node_names(input_feed.keys.map(&:to_s), refs)
  output_node_names = create_node_names(output_names.map(&:to_s), refs)

  # run options
  run_options = Pointer.new(api[:ReleaseRunOptions])
  check_status api[:CreateRunOptions].call(run_options.ref)

  check_status api[:RunOptionsSetRunLogSeverityLevel].call(run_options, log_severity_level) if log_severity_level
  check_status api[:RunOptionsSetRunLogVerbosityLevel].call(run_options, log_verbosity_level) if log_verbosity_level
  check_status api[:RunOptionsSetRunTag].call(run_options, logid) if logid
  check_status api[:RunOptionsSetTerminate].call(run_options) if terminate

  check_status api[:Run].call(@session, run_options, input_node_names, input_tensor, input_feed.size, output_node_names, output_names.size, output_tensor)

  output_tensor.read_array_of_pointer(output_names.size).map { |v| OrtValue.new(v) }
end