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
# File 'lib/onnxruntime/ort_value.rb', line 3

def initialize(ptr, ref = nil)
  @ptr = ::FFI::AutoPointer.new(ptr, FFI.api[:ReleaseValue])
  @ref = ref # keep reference to data
end

Class Method Details

.allocator_infoObject



254
255
256
257
258
259
260
# File 'lib/onnxruntime/ort_value.rb', line 254

def self.allocator_info
  @allocator_info ||= begin
    allocator_info = Pointer.new(FFI.api[:ReleaseMemoryInfo])
    Utils.check_status FFI.api[:CreateCpuMemoryInfo].call(1, 0, allocator_info.ref)
    allocator_info
  end
end

.from_array(input, element_type:) ⇒ Object



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

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 = Pointer.new
  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, input_node_dims, shape.size, type_enum, ptr.ref)
    Utils.check_status FFI.api[:FillStringTensor].call(ptr, 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, input_tensor_values, input_tensor_values.size, input_node_dims, shape.size, type_enum, ptr.ref)
  end

  new(ptr.to_ptr, input_tensor_values)
end

.from_numo(numo_obj) ⇒ Object



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

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



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

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 = Pointer.new
  Utils.check_status FFI.api[:CreateTensorAsOrtValue].call(Utils.allocator, input_node_dims, shape.size, type_enum, ptr.ref)

  new(ptr.to_ptr)
end

Instance Method Details

#data_ptrObject



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

def data_ptr
  tensor_data = Pointer.new
  FFI.api[:GetTensorMutableData].call(@ptr, tensor_data.ref)
  tensor_data.to_ptr
end

#data_typeObject



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

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

#device_nameObject



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

def device_name
  "cpu"
end

#element_typeObject



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

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

#numoObject



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

def numo
  create_from_onnx_value(@ptr, :numo)
end

#shapeObject



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

def shape
  type_and_shape_info[1]
end

#tensor?Boolean

Returns:

  • (Boolean)


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

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

#to_ptrObject



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

def to_ptr
  @ptr
end

#to_rubyObject



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

def to_ruby
  create_from_onnx_value(@ptr, :ruby)
end