Module: OnnxRuntime::Utils
- Defined in:
- lib/onnxruntime/utils.rb
Class Attribute Summary collapse
-
.mutex ⇒ Object
Returns the value of attribute mutex.
Class Method Summary collapse
- .allocator ⇒ Object
- .api ⇒ Object
- .check_status(status) ⇒ Object
- .input_shape(input) ⇒ Object
- .node_info(typeinfo) ⇒ Object
- .numo_array?(obj) ⇒ Boolean
- .numo_types ⇒ Object
- .tensor_type_and_shape(tensor_info) ⇒ Object
- .unsupported_type(name, type) ⇒ Object
Class Attribute Details
.mutex ⇒ Object
Returns the value of attribute mutex.
4 5 6 |
# File 'lib/onnxruntime/utils.rb', line 4 def mutex @mutex end |
Class Method Details
.allocator ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/onnxruntime/utils.rb', line 131 def self.allocator @allocator ||= begin # do not free default allocator allocator = Pointer.new check_status api[:GetAllocatorWithDefaultOptions].call(allocator.ref) allocator end 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? = api[:GetErrorMessage].call(status).read_string api[:ReleaseStatus].call(status) raise Error, end end |
.input_shape(input) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/onnxruntime/utils.rb', line 117 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
44 45 46 47 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 |
# File 'lib/onnxruntime/utils.rb', line 44 def self.node_info(typeinfo) onnx_type = ::FFI::MemoryPointer.new(:int) check_status api[:GetOnnxTypeFromTypeInfo].call(typeinfo, onnx_type) type = FFI::OnnxType[onnx_type.read_int] case type when :tensor # don't free tensor_info tensor_info = Pointer.new check_status api[:CastTypeInfoToTensorInfo].call(typeinfo, tensor_info.ref) type, shape = Utils.tensor_type_and_shape(tensor_info) { type: "tensor(#{FFI::TensorElementDataType[type]})", shape: shape } when :sequence # don't free sequence_info sequence_type_info = Pointer.new check_status api[:CastTypeInfoToSequenceTypeInfo].call(typeinfo, sequence_type_info.ref) nested_type_info = Pointer.new(api[:ReleaseTypeInfo]) check_status api[:GetSequenceElementType].call(sequence_type_info, nested_type_info.ref) v = node_info(nested_type_info)[:type] { type: "seq(#{v})", shape: [] } when :map # don't free map_type_info map_type_info = Pointer.new check_status api[:CastTypeInfoToMapTypeInfo].call(typeinfo, map_type_info.ref) # key key_type = ::FFI::MemoryPointer.new(:int) check_status api[:GetMapKeyType].call(map_type_info, key_type) k = FFI::TensorElementDataType[key_type.read_int] # value value_type_info = Pointer.new(api[:ReleaseTypeInfo]) check_status api[:GetMapValueType].call(map_type_info, value_type_info.ref) v = node_info(value_type_info)[:type] { type: "map(#{k},#{v})", shape: [] } else Utils.unsupported_type("ONNX", type) end end |
.numo_array?(obj) ⇒ Boolean
97 98 99 |
# File 'lib/onnxruntime/utils.rb', line 97 def self.numo_array?(obj) defined?(Numo::NArray) && obj.is_a?(Numo::NArray) end |
.numo_types ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/onnxruntime/utils.rb', line 101 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 |
.tensor_type_and_shape(tensor_info) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/onnxruntime/utils.rb', line 24 def self.tensor_type_and_shape(tensor_info) type = ::FFI::MemoryPointer.new(:int) check_status api[:GetTensorElementType].call(tensor_info, type) num_dims_ptr = ::FFI::MemoryPointer.new(:size_t) check_status api[:GetDimensionsCount].call(tensor_info, 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, 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, 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
20 21 22 |
# File 'lib/onnxruntime/utils.rb', line 20 def self.unsupported_type(name, type) raise Error, "Unsupported #{name} type: #{type}" end |