Module: NDAV::OrtValue::FromNDAV

Defined in:
lib/ndav/ort_value.rb

Instance Method Summary collapse

Instance Method Details

#from_ffi_memory_pointer(input, shape:, element_type:) ⇒ Object



75
76
77
# File 'lib/ndav/ort_value.rb', line 75

def from_ffi_memory_pointer(input, shape:, element_type:)
  from_pointer(input, shape:, element_type:, ref: input)
end

#from_ffi_pointer(input, shape:, element_type:, ref:) ⇒ Object



79
80
81
# File 'lib/ndav/ort_value.rb', line 79

def from_ffi_pointer(input, shape:, element_type:, ref:)
  from_pointer(input, shape:, element_type:, ref:)
end

#from_fiddle_memory_view(input) ⇒ Object



66
67
68
# File 'lib/ndav/ort_value.rb', line 66

def from_fiddle_memory_view(input)
  from_ndav(::NDAV.new(input))
end

#from_fiddle_pointer(input, shape:, element_type:, ref:) ⇒ Object



70
71
72
73
# File 'lib/ndav/ort_value.rb', line 70

def from_fiddle_pointer(input, shape:, element_type:, ref:)
  format = TYPE_TO_FORMAT[element_type]
  from_ndav(::NDAV.new(input, shape:, format:, lifetime: ref))
end

#from_memory_view(input) ⇒ Object



62
63
64
# File 'lib/ndav/ort_value.rb', line 62

def from_memory_view(input)
  from_ndav(::NDAV.new(input))
end

#from_ndav(input) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ndav/ort_value.rb', line 83

def from_ndav(input)
  element_type = FORMAT_TO_TYPE[input.format]
  ::OnnxRuntime::Utils.unsupported_type("NDAV", input.format) unless element_type

  type_enum = ::OnnxRuntime::FFI::TensorElementDataType[element_type]
  ::OnnxRuntime::Utils.unsupported_type("element", element_type) unless type_enum

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

  ptr = ::OnnxRuntime::Pointer.new
  ::OnnxRuntime::Utils.check_status ::OnnxRuntime::FFI.api[:CreateTensorWithDataAsOrtValue].call(allocator_info, input.to_s, input.byte_size, input_node_dims, shape.size, type_enum, ptr.ref)

  new(ptr.to_ptr, input)
end

#from_pointer(input, shape:, element_type:, ref:) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/ndav/ort_value.rb', line 100

def from_pointer(input, shape:, element_type:, ref:)
  item_size = TYPE_SIZES[element_type]
  byte_size = shape.reduce(item_size, :*)
  if input.kind_of?(::Fiddle::MemoryView) && input.size != byte_size
    raise ArgumentError, "size and shape doesn't match"
  end

  from_fiddle_pointer(Fiddle::Pointer.new(input.address, byte_size), shape:, element_type:, ref:)
end