Module: OnnxRuntime::Utils

Defined in:
lib/onnxruntime/utils.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.mutexObject

Returns the value of attribute mutex.



4
5
6
# File 'lib/onnxruntime/utils.rb', line 4

def mutex
  @mutex
end

Class Method Details

.allocatorObject



134
135
136
137
138
139
140
# File 'lib/onnxruntime/utils.rb', line 134

def self.allocator
  @allocator ||= begin
    allocator = ::FFI::MemoryPointer.new(:pointer)
    check_status api[:GetAllocatorWithDefaultOptions].call(allocator)
    allocator
  end
end

.apiObject



16
17
18
# File 'lib/onnxruntime/utils.rb', line 16

def self.api
  FFI.api
end

.check_status(status) ⇒ Object



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

def self.check_status(status)
  unless status.null?
    message = api[:GetErrorMessage].call(status).read_string
    api[:ReleaseStatus].call(status)
    raise Error, message
  end
end

.input_shape(input) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/onnxruntime/utils.rb', line 120

def self.input_shape(input)
  if numo_array?(input)
    input.shape
  else
    shape = []
    s = input
    while s.is_a?(Array)
      shape << s.size
      s = s.first
    end
    shape
  end
end

.node_info(typeinfo) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/onnxruntime/utils.rb', line 48

def self.node_info(typeinfo)
  onnx_type = ::FFI::MemoryPointer.new(:int)
  check_status api[:GetOnnxTypeFromTypeInfo].call(typeinfo.read_pointer, onnx_type)

  type = FFI::OnnxType[onnx_type.read_int]
  case type
  when :tensor
    tensor_info = ::FFI::MemoryPointer.new(:pointer)
    # don't free tensor_info
    check_status api[:CastTypeInfoToTensorInfo].call(typeinfo.read_pointer, tensor_info)

    type, shape = Utils.tensor_type_and_shape(tensor_info)
    {
      type: "tensor(#{FFI::TensorElementDataType[type]})",
      shape: shape
    }
  when :sequence
    sequence_type_info = ::FFI::MemoryPointer.new(:pointer)
    check_status api[:CastTypeInfoToSequenceTypeInfo].call(typeinfo.read_pointer, sequence_type_info)
    nested_type_info = ::FFI::MemoryPointer.new(:pointer)
    check_status api[:GetSequenceElementType].call(sequence_type_info.read_pointer, nested_type_info)
    v = node_info(nested_type_info)[:type]

    {
      type: "seq(#{v})",
      shape: []
    }
  when :map
    map_type_info = ::FFI::MemoryPointer.new(:pointer)
    check_status api[:CastTypeInfoToMapTypeInfo].call(typeinfo.read_pointer, map_type_info)

    # key
    key_type = ::FFI::MemoryPointer.new(:int)
    check_status api[:GetMapKeyType].call(map_type_info.read_pointer, key_type)
    k = FFI::TensorElementDataType[key_type.read_int]

    # value
    value_type_info = ::FFI::MemoryPointer.new(:pointer)
    check_status api[:GetMapValueType].call(map_type_info.read_pointer, value_type_info)
    v = node_info(value_type_info)[:type]

    {
      type: "map(#{k},#{v})",
      shape: []
    }
  else
    Utils.unsupported_type("ONNX", type)
  end
ensure
  release :TypeInfo, typeinfo
end

.numo_array?(obj) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/onnxruntime/utils.rb', line 100

def self.numo_array?(obj)
  defined?(Numo::NArray) && obj.is_a?(Numo::NArray)
end

.numo_typesObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/onnxruntime/utils.rb', line 104

def self.numo_types
  @numo_types ||= {
    float: Numo::SFloat,
    uint8: Numo::UInt8,
    int8: Numo::Int8,
    uint16: Numo::UInt16,
    int16: Numo::Int16,
    int32: Numo::Int32,
    int64: Numo::Int64,
    bool: Numo::UInt8,
    double: Numo::DFloat,
    uint32: Numo::UInt32,
    uint64: Numo::UInt64
  }
end

.release(type, pointer) ⇒ Object



20
21
22
# File 'lib/onnxruntime/utils.rb', line 20

def self.release(type, pointer)
  FFI.api[:"Release#{type}"].call(pointer.read_pointer) if pointer && !pointer.null?
end

.tensor_type_and_shape(tensor_info) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/onnxruntime/utils.rb', line 28

def self.tensor_type_and_shape(tensor_info)
  type = ::FFI::MemoryPointer.new(:int)
  check_status api[:GetTensorElementType].call(tensor_info.read_pointer, type)

  num_dims_ptr = ::FFI::MemoryPointer.new(:size_t)
  check_status api[:GetDimensionsCount].call(tensor_info.read_pointer, num_dims_ptr)
  num_dims = num_dims_ptr.read(:size_t)

  node_dims = ::FFI::MemoryPointer.new(:int64, num_dims)
  check_status api[:GetDimensions].call(tensor_info.read_pointer, node_dims, num_dims)
  dims = node_dims.read_array_of_int64(num_dims)

  symbolic_dims = ::FFI::MemoryPointer.new(:pointer, num_dims)
  check_status api[:GetSymbolicDimensions].call(tensor_info.read_pointer, symbolic_dims, num_dims)
  named_dims = num_dims.times.map { |i| symbolic_dims[i].read_pointer.read_string }
  dims = named_dims.zip(dims).map { |n, d| n.empty? ? d : n }

  [type.read_int, dims]
end

.unsupported_type(name, type) ⇒ Object

Raises:



24
25
26
# File 'lib/onnxruntime/utils.rb', line 24

def self.unsupported_type(name, type)
  raise Error, "Unsupported #{name} type: #{type}"
end