onnxruntime 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/LICENSE.txt +1 -0
- data/README.md +43 -0
- data/lib/onnxruntime.rb +1 -0
- data/lib/onnxruntime/datasets.rb +10 -0
- data/lib/onnxruntime/ffi.rb +20 -20
- data/lib/onnxruntime/inference_session.rb +35 -4
- data/lib/onnxruntime/model.rb +4 -4
- data/lib/onnxruntime/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa019e016dea45f795caba9b06e972edc6ae705d1599826fb17adf10af572851
|
4
|
+
data.tar.gz: e0704039d0c8a94bf523dce7f707636493696ee92aa3f741d5b49ea62561e6d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07dbde3621a15379bceb5b0fe7bfbb2bb581cb54a1b78d0f104f432396bac9086ab3c6186798fab52026515c8331074b35a68f6cce6748d3315e10bc375c3a9a
|
7
|
+
data.tar.gz: ccc9ee30c55d0c8ae611b70adeab1b8707bc15bce42281ce05ca6bb9d4d6ff4953c2795a3931f4ff325401a3caea95d62e08243732d125cebda9dfb4587bdbc8
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -50,6 +50,35 @@ Get specific outputs
|
|
50
50
|
model.predict({x: [1, 2, 3]}, output_names: ["label"])
|
51
51
|
```
|
52
52
|
|
53
|
+
## Session Options
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
OnnxRuntime::Model.new(path_or_bytes, {
|
57
|
+
enable_cpu_mem_arena: true,
|
58
|
+
enable_mem_pattern: true,
|
59
|
+
enable_profiling: false,
|
60
|
+
execution_mode: :sequential,
|
61
|
+
graph_optimization_level: nil,
|
62
|
+
inter_op_num_threads: nil,
|
63
|
+
intra_op_num_threads: nil,
|
64
|
+
log_severity_level: 2,
|
65
|
+
log_verbosity_level: 0,
|
66
|
+
logid: nil,
|
67
|
+
optimized_model_filepath: nil
|
68
|
+
})
|
69
|
+
```
|
70
|
+
|
71
|
+
## Run Options
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
model.predict(input_feed, {
|
75
|
+
log_severity_level: 2,
|
76
|
+
log_verbosity_level: 0,
|
77
|
+
logid: nil,
|
78
|
+
terminate: false
|
79
|
+
})
|
80
|
+
```
|
81
|
+
|
53
82
|
## Inference Session API
|
54
83
|
|
55
84
|
You can also use the Inference Session API, which follows the [Python API](https://microsoft.github.io/onnxruntime/python/api_summary.html).
|
@@ -59,6 +88,20 @@ session = OnnxRuntime::InferenceSession.new("model.onnx")
|
|
59
88
|
session.run(nil, {x: [1, 2, 3]})
|
60
89
|
```
|
61
90
|
|
91
|
+
The Python example models are included as well.
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
OnnxRuntime::Datasets.example("sigmoid.onnx")
|
95
|
+
```
|
96
|
+
|
97
|
+
## GPU Support
|
98
|
+
|
99
|
+
To enable GPU support on Linux and Windows, download the appropriate [GPU release](https://github.com/microsoft/onnxruntime/releases) and set:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
OnnxRuntime.ffi_lib = "path/to/lib/libonnxruntime.so" # onnxruntime.dll for Windows
|
103
|
+
```
|
104
|
+
|
62
105
|
## History
|
63
106
|
|
64
107
|
View the [changelog](https://github.com/ankane/onnxruntime/blob/master/CHANGELOG.md)
|
data/lib/onnxruntime.rb
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
module OnnxRuntime
|
2
|
+
module Datasets
|
3
|
+
def self.example(name)
|
4
|
+
unless %w(logreg_iris.onnx mul_1.onnx sigmoid.onnx).include?(name)
|
5
|
+
raise ArgumentError, "Unable to find example '#{name}'"
|
6
|
+
end
|
7
|
+
File.expand_path("../../datasets/#{name}", __dir__)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/onnxruntime/ffi.rb
CHANGED
@@ -3,7 +3,7 @@ module OnnxRuntime
|
|
3
3
|
extend ::FFI::Library
|
4
4
|
|
5
5
|
begin
|
6
|
-
ffi_lib OnnxRuntime.ffi_lib
|
6
|
+
ffi_lib Array(OnnxRuntime.ffi_lib)
|
7
7
|
rescue LoadError => e
|
8
8
|
raise e if ENV["ONNXRUNTIME_DEBUG"]
|
9
9
|
raise LoadError, "Could not find ONNX Runtime"
|
@@ -29,21 +29,21 @@ module OnnxRuntime
|
|
29
29
|
:CreateSessionFromArray, callback(%i[pointer pointer size_t pointer pointer], :pointer),
|
30
30
|
:Run, callback(%i[pointer pointer pointer pointer size_t pointer size_t pointer], :pointer),
|
31
31
|
:CreateSessionOptions, callback(%i[pointer], :pointer),
|
32
|
-
:SetOptimizedModelFilePath, callback(%i[], :pointer),
|
32
|
+
:SetOptimizedModelFilePath, callback(%i[pointer string], :pointer),
|
33
33
|
:CloneSessionOptions, callback(%i[], :pointer),
|
34
34
|
:SetSessionExecutionMode, callback(%i[], :pointer),
|
35
|
-
:EnableProfiling, callback(%i[], :pointer),
|
36
|
-
:DisableProfiling, callback(%i[], :pointer),
|
37
|
-
:EnableMemPattern, callback(%i[], :pointer),
|
38
|
-
:DisableMemPattern, callback(%i[], :pointer),
|
39
|
-
:EnableCpuMemArena, callback(%i[], :pointer),
|
40
|
-
:DisableCpuMemArena, callback(%i[], :pointer),
|
41
|
-
:SetSessionLogId, callback(%i[], :pointer),
|
42
|
-
:SetSessionLogVerbosityLevel, callback(%i[], :pointer),
|
43
|
-
:SetSessionLogSeverityLevel, callback(%i[], :pointer),
|
44
|
-
:SetSessionGraphOptimizationLevel, callback(%i[], :pointer),
|
45
|
-
:SetIntraOpNumThreads, callback(%i[], :pointer),
|
46
|
-
:SetInterOpNumThreads, callback(%i[], :pointer),
|
35
|
+
:EnableProfiling, callback(%i[pointer string], :pointer),
|
36
|
+
:DisableProfiling, callback(%i[pointer], :pointer),
|
37
|
+
:EnableMemPattern, callback(%i[pointer], :pointer),
|
38
|
+
:DisableMemPattern, callback(%i[pointer], :pointer),
|
39
|
+
:EnableCpuMemArena, callback(%i[pointer], :pointer),
|
40
|
+
:DisableCpuMemArena, callback(%i[pointer], :pointer),
|
41
|
+
:SetSessionLogId, callback(%i[pointer string], :pointer),
|
42
|
+
:SetSessionLogVerbosityLevel, callback(%i[pointer int], :pointer),
|
43
|
+
:SetSessionLogSeverityLevel, callback(%i[pointer int], :pointer),
|
44
|
+
:SetSessionGraphOptimizationLevel, callback(%i[pointer int], :pointer),
|
45
|
+
:SetIntraOpNumThreads, callback(%i[pointer int], :pointer),
|
46
|
+
:SetInterOpNumThreads, callback(%i[pointer int], :pointer),
|
47
47
|
:CreateCustomOpDomain, callback(%i[], :pointer),
|
48
48
|
:CustomOpDomain_Add, callback(%i[], :pointer),
|
49
49
|
:AddCustomOpDomain, callback(%i[], :pointer),
|
@@ -57,15 +57,15 @@ module OnnxRuntime
|
|
57
57
|
:SessionGetInputName, callback(%i[pointer size_t pointer pointer], :pointer),
|
58
58
|
:SessionGetOutputName, callback(%i[pointer size_t pointer pointer], :pointer),
|
59
59
|
:SessionGetOverridableInitializerName, callback(%i[], :pointer),
|
60
|
-
:CreateRunOptions, callback(%i[], :pointer),
|
61
|
-
:RunOptionsSetRunLogVerbosityLevel, callback(%i[], :pointer),
|
62
|
-
:RunOptionsSetRunLogSeverityLevel, callback(%i[], :pointer),
|
63
|
-
:RunOptionsSetRunTag, callback(%i[], :pointer),
|
60
|
+
:CreateRunOptions, callback(%i[pointer], :pointer),
|
61
|
+
:RunOptionsSetRunLogVerbosityLevel, callback(%i[pointer int], :pointer),
|
62
|
+
:RunOptionsSetRunLogSeverityLevel, callback(%i[pointer int], :pointer),
|
63
|
+
:RunOptionsSetRunTag, callback(%i[pointer string], :pointer),
|
64
64
|
:RunOptionsGetRunLogVerbosityLevel, callback(%i[], :pointer),
|
65
65
|
:RunOptionsGetRunLogSeverityLevel, callback(%i[], :pointer),
|
66
66
|
:RunOptionsGetRunTag, callback(%i[], :pointer),
|
67
|
-
:RunOptionsSetTerminate, callback(%i[], :pointer),
|
68
|
-
:RunOptionsUnsetTerminate, callback(%i[], :pointer),
|
67
|
+
:RunOptionsSetTerminate, callback(%i[pointer], :pointer),
|
68
|
+
:RunOptionsUnsetTerminate, callback(%i[pointer], :pointer),
|
69
69
|
:CreateTensorAsOrtValue, callback(%i[pointer pointer size_t int pointer], :pointer),
|
70
70
|
:CreateTensorWithDataAsOrtValue, callback(%i[pointer pointer size_t pointer size_t int pointer], :pointer),
|
71
71
|
:IsTensor, callback(%i[], :pointer),
|
@@ -2,10 +2,32 @@ module OnnxRuntime
|
|
2
2
|
class InferenceSession
|
3
3
|
attr_reader :inputs, :outputs
|
4
4
|
|
5
|
-
def initialize(path_or_bytes)
|
5
|
+
def initialize(path_or_bytes, enable_cpu_mem_arena: true, enable_mem_pattern: true, enable_profiling: false, execution_mode: 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)
|
6
6
|
# session options
|
7
7
|
session_options = ::FFI::MemoryPointer.new(:pointer)
|
8
8
|
check_status api[:CreateSessionOptions].call(session_options)
|
9
|
+
check_status api[:EnableCpuMemArena].call(session_options.read_pointer) if enable_cpu_mem_arena
|
10
|
+
check_status api[:EnableMemPattern].call(session_options.read_pointer) if enable_mem_pattern
|
11
|
+
check_status api[:EnableProfiling].call(session_options.read_pointer, "onnxruntime_profile_") if enable_profiling
|
12
|
+
if execution_mode
|
13
|
+
mode =
|
14
|
+
case execution_mode
|
15
|
+
when :sequential
|
16
|
+
0
|
17
|
+
when :parallel
|
18
|
+
1
|
19
|
+
else
|
20
|
+
raise ArgumentError, "Invalid execution mode"
|
21
|
+
end
|
22
|
+
check_status api[:SetSessionExecutionMode].call(session_options.read_pointer, mode)
|
23
|
+
end
|
24
|
+
check_status api[:SetSessionGraphOptimizationLevel].call(session_options.read_pointer, graph_optimization_level) if graph_optimization_level
|
25
|
+
check_status api[:SetInterOpNumThreads].call(session_options.read_pointer, inter_op_num_threads) if inter_op_num_threads
|
26
|
+
check_status api[:SetIntraOpNumThreads].call(session_options.read_pointer, intra_op_num_threads) if intra_op_num_threads
|
27
|
+
check_status api[:SetSessionLogSeverityLevel].call(session_options.read_pointer, log_severity_level) if log_severity_level
|
28
|
+
check_status api[:SetSessionLogVerbosityLevel].call(session_options.read_pointer, log_verbosity_level) if log_verbosity_level
|
29
|
+
check_status api[:SetSessionLogId].call(session_options.read_pointer, logid) if logid
|
30
|
+
check_status api[:SetOptimizedModelFilePath].call(session_options.read_pointer, optimized_model_filepath) if optimized_model_filepath
|
9
31
|
|
10
32
|
# session
|
11
33
|
@session = ::FFI::MemoryPointer.new(:pointer)
|
@@ -53,7 +75,8 @@ module OnnxRuntime
|
|
53
75
|
end
|
54
76
|
end
|
55
77
|
|
56
|
-
|
78
|
+
# TODO support logid
|
79
|
+
def run(output_names, input_feed, log_severity_level: nil, log_verbosity_level: nil, logid: nil, terminate: nil)
|
57
80
|
input_tensor = create_input_tensor(input_feed)
|
58
81
|
|
59
82
|
output_names ||= @outputs.map { |v| v[:name] }
|
@@ -61,8 +84,16 @@ module OnnxRuntime
|
|
61
84
|
output_tensor = ::FFI::MemoryPointer.new(:pointer, outputs.size)
|
62
85
|
input_node_names = create_node_names(input_feed.keys.map(&:to_s))
|
63
86
|
output_node_names = create_node_names(output_names.map(&:to_s))
|
64
|
-
|
65
|
-
|
87
|
+
|
88
|
+
# run options
|
89
|
+
run_options = ::FFI::MemoryPointer.new(:pointer)
|
90
|
+
check_status api[:CreateRunOptions].call(run_options)
|
91
|
+
check_status api[:RunOptionsSetRunLogSeverityLevel].call(run_options.read_pointer, log_severity_level) if log_severity_level
|
92
|
+
check_status api[:RunOptionsSetRunLogVerbosityLevel].call(run_options.read_pointer, log_verbosity_level) if log_verbosity_level
|
93
|
+
check_status api[:RunOptionsSetRunTag].call(run_options.read_pointer, logid) if logid
|
94
|
+
check_status api[:RunOptionsSetTerminate].call(run_options.read_pointer) if terminate
|
95
|
+
|
96
|
+
check_status api[:Run].call(read_pointer, run_options.read_pointer, input_node_names, input_tensor, input_feed.size, output_node_names, output_names.size, output_tensor)
|
66
97
|
|
67
98
|
output_names.size.times.map do |i|
|
68
99
|
create_from_onnx_value(output_tensor[i].read_pointer)
|
data/lib/onnxruntime/model.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module OnnxRuntime
|
2
2
|
class Model
|
3
|
-
def initialize(path_or_bytes)
|
4
|
-
@session = InferenceSession.new(path_or_bytes)
|
3
|
+
def initialize(path_or_bytes, **session_options)
|
4
|
+
@session = InferenceSession.new(path_or_bytes, **session_options)
|
5
5
|
end
|
6
6
|
|
7
|
-
def predict(input_feed, output_names: nil)
|
8
|
-
predictions = @session.run(output_names, input_feed)
|
7
|
+
def predict(input_feed, output_names: nil, **run_options)
|
8
|
+
predictions = @session.run(output_names, input_feed, **run_options)
|
9
9
|
output_names ||= outputs.map { |o| o[:name] }
|
10
10
|
|
11
11
|
result = {}
|
data/lib/onnxruntime/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onnxruntime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- LICENSE.txt
|
77
77
|
- README.md
|
78
78
|
- lib/onnxruntime.rb
|
79
|
+
- lib/onnxruntime/datasets.rb
|
79
80
|
- lib/onnxruntime/ffi.rb
|
80
81
|
- lib/onnxruntime/inference_session.rb
|
81
82
|
- lib/onnxruntime/model.rb
|