Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental Numo support #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/menoh.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'menoh/version'
require 'menoh/menoh_native'
require 'json'
require 'numo/narray'

module Menoh
class Menoh
Expand All @@ -20,6 +21,16 @@ def make_model(option)
end

class MenohModel
DTYPE_TO_NUMO_NARRAY_CLASS = {
float: Numo::SFloat,
float32: Numo::SFloat,
float64: Numo::DFloat,
int8: Numo::Int8,
int16: Numo::Int16,
int32: Numo::Int32,
int64: Numo::Int64,
}

def initialize(menoh, option)
if option[:input_layers].nil? || option[:input_layers].empty?
raise "Required ':input_layers'"
Expand Down Expand Up @@ -72,6 +83,30 @@ def run(dataset)
yield results if block_given?
results
end

def run_numo(dataset)
raise 'Invalid dataset' if !dataset.instance_of?(Array) || dataset.empty?
if dataset.length != @option[:input_layers].length
raise "Invalid input num: expected==#{@option[:input_layers].length} actual==#{dataset.length}"
end
dataset.each do |input|
set_data_str(input[:name], input[:data].to_binary)
end

# run
native_run

results = {}
@option[:output_layers].each do |name|
dtype = get_dtype(name)
c = DTYPE_TO_NUMO_NARRAY_CLASS[dtype]
raise InvalidDTypeError.new("unsupported dtype: #{dtype}") if c.nil?
results[name] = c.from_binary(get_data_str(name), get_shape(name))
end

yield results if block_given?
results
end
end

module Util
Expand Down
1 change: 1 addition & 0 deletions menoh.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'pry'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rake-compiler'
spec.add_development_dependency 'numo-narray'
end
31 changes: 31 additions & 0 deletions test/menoh_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'numo/narray'

MNIST_ONNX_FILE = 'example/data/mnist.onnx'.freeze
MNIST_IN_NAME = '139900320569040'.freeze
Expand Down Expand Up @@ -39,6 +40,36 @@ def test_menoh_basic_function
end
end

def test_menoh_basic_function_numo
onnx = Menoh::Menoh.new(MNIST_ONNX_FILE)
assert_instance_of(Menoh::Menoh, onnx)
batch_size = 3
model_opt = {
backend: 'mkldnn',
input_layers: [
{
name: MNIST_IN_NAME,
dims: [batch_size, 1, 28, 28]
}
],
output_layers: [MNIST_OUT_NAME]
}
model = onnx.make_model(model_opt)
assert_instance_of(Menoh::MenohModel, model)
10.times do
imageset = [
{
name: MNIST_IN_NAME,
data: Numo::SFloat.zeros(batch_size, 1, 28, 28)
}
]
inferenced_results = model.run_numo imageset
assert_instance_of(Hash, inferenced_results)
assert_instance_of(Numo::SFloat, inferenced_results[MNIST_OUT_NAME])
assert_equal([batch_size, 10], inferenced_results[MNIST_OUT_NAME].shape)
end
end

def test_menoh_basic_function_with_block
batch_size = 3
model_opt = {
Expand Down