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



50
51
52
# File 'lib/ndav/ort_value.rb', line 50

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



54
55
56
# File 'lib/ndav/ort_value.rb', line 54

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

#from_fiddle_memory_view(input) ⇒ Object



41
42
43
# File 'lib/ndav/ort_value.rb', line 41

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

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



45
46
47
48
# File 'lib/ndav/ort_value.rb', line 45

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



37
38
39
# File 'lib/ndav/ort_value.rb', line 37

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

#from_ndav(input) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ndav/ort_value.rb', line 58

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



75
76
77
78
79
80
81
82
83
# File 'lib/ndav/ort_value.rb', line 75

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