Class: OnnxRuntime::OrtValue

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, ref = nil) ⇒ OrtValue

Returns a new instance of OrtValue.



3
4
5
6
7
# File 'lib/onnxruntime/ort_value.rb', line 3

def initialize(ptr, ref = nil)
  @ptr = ptr.read_pointer
  @ref = ref # keep reference to data
  ObjectSpace.define_finalizer(@ptr, self.class.finalize(@ptr.to_i))
end

Class Method Details

.from_array(input, element_type:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/onnxruntime/ort_value.rb', line 16

def self.from_array(input, element_type:)
  type_enum = FFI::TensorElementDataType[element_type]
  Utils.unsupported_type("element", element_type) unless type_enum

  input = input.to_a unless input.is_a?(Array) || Utils.numo_array?(input)

  shape = Utils.input_shape(input)
  input_node_dims = ::FFI::MemoryPointer.new(:int64, shape.size)
  input_node_dims.write_array_of_int64(shape)

  ptr = ::FFI::MemoryPointer.new(:pointer)
  if element_type == :string
    # keep reference to _str_ptrs until FillStringTensor call
    input_tensor_values, _str_ptrs = create_input_strings(input)
    Utils.check_status FFI.api[:CreateTensorAsOrtValue].call(Utils.allocator.read_pointer, input_node_dims, shape.size, type_enum, ptr)
    Utils.check_status FFI.api[:FillStringTensor].call(ptr.read_pointer, input_tensor_values, input_tensor_values.size / input_tensor_values.type_size)
  else
    input_tensor_values = create_input_data(input, element_type)
    Utils.check_status FFI.api[:CreateTensorWithDataAsOrtValue].call(allocator_info.read_pointer, input_tensor_values, input_tensor_values.size, input_node_dims, shape.size, type_enum, ptr)
  end

  new(ptr, input_tensor_values)
end

.from_numo(numo_obj) ⇒ Object



9
10
11
12
13
14
# File 'lib/onnxruntime/ort_value.rb', line 9

def self.from_numo(numo_obj)
  element_type = numo_obj.is_a?(Numo::Bit) ? :bool : Utils.numo_types.invert[numo_obj.class]
  Utils.unsupported_type("Numo", numo_obj.class.name) unless element_type

  from_array(numo_obj, element_type: element_type)
end

.from_shape_and_type(shape, element_type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/onnxruntime/ort_value.rb', line 40

def self.from_shape_and_type(shape, element_type)
  type_enum = FFI::TensorElementDataType[element_type]
  Utils.unsupported_type("element", element_type) unless type_enum

  input_node_dims = ::FFI::MemoryPointer.new(:int64, shape.size)
  input_node_dims.write_array_of_int64(shape)

  ptr = ::FFI::MemoryPointer.new(:pointer)
  Utils.check_status FFI.api[:CreateTensorAsOrtValue].call(Utils.allocator.read_pointer, input_node_dims, shape.size, type_enum, ptr)

  new(ptr)
end

Instance Method Details

#data_ptrObject



119
120
121
122
123
# File 'lib/onnxruntime/ort_value.rb', line 119

def data_ptr
  tensor_data = ::FFI::MemoryPointer.new(:pointer)
  FFI.api[:GetTensorMutableData].call(@ptr, tensor_data)
  tensor_data.read_pointer
end

#data_typeObject



87
88
89
90
91
92
93
# File 'lib/onnxruntime/ort_value.rb', line 87

def data_type
  @data_type ||= begin
    typeinfo = ::FFI::MemoryPointer.new(:pointer)
    Utils.check_status FFI.api[:GetTypeInfo].call(@ptr, typeinfo)
    Utils.node_info(typeinfo)[:type]
  end
end

#device_nameObject



103
104
105
# File 'lib/onnxruntime/ort_value.rb', line 103

def device_name
  "cpu"
end

#element_typeObject



95
96
97
# File 'lib/onnxruntime/ort_value.rb', line 95

def element_type
  FFI::TensorElementDataType[type_and_shape_info[0]]
end

#numoObject



107
108
109
# File 'lib/onnxruntime/ort_value.rb', line 107

def numo
  create_from_onnx_value(@ptr, :numo)
end

#shapeObject



99
100
101
# File 'lib/onnxruntime/ort_value.rb', line 99

def shape
  type_and_shape_info[1]
end

#tensor?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/onnxruntime/ort_value.rb', line 83

def tensor?
  FFI::OnnxType[value_type] == :tensor
end

#to_ptrObject



115
116
117
# File 'lib/onnxruntime/ort_value.rb', line 115

def to_ptr
  @ptr
end

#to_rubyObject



111
112
113
# File 'lib/onnxruntime/ort_value.rb', line 111

def to_ruby
  create_from_onnx_value(@ptr, :ruby)
end