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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2405ad6cda55897be52cbb6afd3854d2cf2ed5ed01abc09bf78568557af446ff
4
- data.tar.gz: 1e04e516b4726f510bf489108c1f684e398f8d63718c9717106ac9659773d3f0
3
+ metadata.gz: fa019e016dea45f795caba9b06e972edc6ae705d1599826fb17adf10af572851
4
+ data.tar.gz: e0704039d0c8a94bf523dce7f707636493696ee92aa3f741d5b49ea62561e6d5
5
5
  SHA512:
6
- metadata.gz: a2a38af03a82eecc60e28b6fb7b012c28188af69af9a7521cb4b1e4c2519c99c6d328045f0f20fe509787942d304a15e92e43ad94483c55f895cf4bbaf21b53e
7
- data.tar.gz: c782475317186f24c7e34285584ee7761522cc1f792e092c896abe32a041cf593ef4e7f0736a12688ad3e7539101234123d8948ab8ebe0c7986bfb40f33ff236
6
+ metadata.gz: 07dbde3621a15379bceb5b0fe7bfbb2bb581cb54a1b78d0f104f432396bac9086ab3c6186798fab52026515c8331074b35a68f6cce6748d3315e10bc375c3a9a
7
+ data.tar.gz: ccc9ee30c55d0c8ae611b70adeab1b8707bc15bce42281ce05ca6bb9d4d6ff4953c2795a3931f4ff325401a3caea95d62e08243732d125cebda9dfb4587bdbc8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.2 (2019-12-24)
2
+
3
+ - Added support for session options
4
+ - Added support for run options
5
+ - Added `Datasets` module
6
+
1
7
  ## 0.2.1 (2019-12-19)
2
8
 
3
9
  - Updated ONNX Runtime to 1.1.0
data/LICENSE.txt CHANGED
@@ -1,4 +1,5 @@
1
1
  Copyright (c) 2019 Andrew Kane
2
+ Datasets Copyright (c) Microsoft Corporation
2
3
 
3
4
  MIT License
4
5
 
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
@@ -2,6 +2,7 @@
2
2
  require "ffi"
3
3
 
4
4
  # modules
5
+ require "onnxruntime/datasets"
5
6
  require "onnxruntime/inference_session"
6
7
  require "onnxruntime/model"
7
8
  require "onnxruntime/utils"
@@ -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
@@ -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
- def run(output_names, input_feed)
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
- # TODO support run options
65
- check_status api[:Run].call(read_pointer, nil, input_node_names, input_tensor, input_feed.size, output_node_names, output_names.size, output_tensor)
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)
@@ -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 = {}
@@ -1,3 +1,3 @@
1
1
  module OnnxRuntime
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
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.1
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-20 00:00:00.000000000 Z
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