ngt 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/ngt/ffi.rb +3 -0
- data/lib/ngt/index.rb +32 -6
- data/lib/ngt/utils.rb +4 -1
- data/lib/ngt/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94ae9612ba58d3e5aa3fecdea4ccb93e6ecaa8086c7b5e5e64a626962a3c7dbf
|
4
|
+
data.tar.gz: 6bf8e3fc92f83994e47caf230027199a2da2c0464b5cce0cdaf0f861ce2b05e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8580f88ad7c6fa1527445aeeba274e67bcf76612cf3aa2c100afe53ca62628df59237119d1878b3244517bd8c01a75cfc5015176c2baf9e2d6eb9617876d4fd1
|
7
|
+
data.tar.gz: 92e66568528c8cc76fe38efdb2795106bea0e50ca1263bf659ef52e26b4f4c36e192e5180a304acd186c746f3b02a3cf2488c2e95c786d024174b8411e5f8fba
|
data/CHANGELOG.md
CHANGED
data/lib/ngt/ffi.rb
CHANGED
@@ -36,8 +36,10 @@ module Ngt
|
|
36
36
|
attach_function :ngt_set_property_edge_size_for_creation, %i[pointer int16_t pointer], :bool
|
37
37
|
attach_function :ngt_set_property_edge_size_for_search, %i[pointer int16_t pointer], :bool
|
38
38
|
attach_function :ngt_is_property_object_type_float, %i[int32_t], :bool
|
39
|
+
attach_function :ngt_is_property_object_type_float16, %i[int32_t], :bool
|
39
40
|
attach_function :ngt_get_property_object_type, %i[pointer pointer], :int32_t
|
40
41
|
attach_function :ngt_set_property_object_type_float, %i[pointer pointer], :bool
|
42
|
+
attach_function :ngt_set_property_object_type_float16, %i[pointer pointer], :bool
|
41
43
|
attach_function :ngt_set_property_object_type_integer, %i[pointer pointer], :bool
|
42
44
|
attach_function :ngt_set_property_distance_type_l1, %i[pointer pointer], :bool
|
43
45
|
attach_function :ngt_set_property_distance_type_l2, %i[pointer pointer], :bool
|
@@ -67,6 +69,7 @@ module Ngt
|
|
67
69
|
attach_function :ngt_get_property_distance_type, %i[pointer pointer], :distance_type
|
68
70
|
attach_function :ngt_create_error_object, %i[], :pointer
|
69
71
|
attach_function :ngt_get_error_string, %i[pointer], :string
|
72
|
+
attach_function :ngt_clear_error_string, %i[pointer], :void
|
70
73
|
attach_function :ngt_destroy_error_object, %i[pointer], :void
|
71
74
|
attach_function :ngt_create_optimizer, %i[bool pointer], :pointer
|
72
75
|
attach_function :ngt_optimizer_adjust_search_coefficients, %i[pointer string pointer], :bool
|
data/lib/ngt/index.rb
CHANGED
@@ -34,22 +34,34 @@ module Ngt
|
|
34
34
|
def object_type
|
35
35
|
@object_type ||= begin
|
36
36
|
object_type = ffi(:ngt_get_property_object_type, @property)
|
37
|
-
FFI.ngt_is_property_object_type_float(object_type)
|
37
|
+
if FFI.ngt_is_property_object_type_float(object_type)
|
38
|
+
:float
|
39
|
+
elsif FFI.ngt_is_property_object_type_float16(object_type)
|
40
|
+
:float16
|
41
|
+
else
|
42
|
+
:integer
|
43
|
+
end
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
41
47
|
def insert(object)
|
42
|
-
|
48
|
+
object = object.to_a
|
49
|
+
ffi(:ngt_insert_index, @index, c_object(object), object.size)
|
43
50
|
end
|
44
51
|
|
45
52
|
def batch_insert(objects, num_threads: 8)
|
46
53
|
if narray?(objects)
|
54
|
+
check_dimensions(objects.shape[1])
|
55
|
+
|
47
56
|
objects = objects.cast_to(Numo::SFloat) unless objects.is_a?(Numo::SFloat)
|
48
57
|
count = objects.shape[0]
|
49
58
|
obj = ::FFI::MemoryPointer.new(:char, objects.byte_size)
|
50
59
|
obj.write_bytes(objects.to_binary)
|
51
60
|
else
|
52
61
|
objects = objects.to_a
|
62
|
+
objects.each do |object|
|
63
|
+
check_dimensions(object.size)
|
64
|
+
end
|
53
65
|
count = objects.size
|
54
66
|
flat_objects = objects.flatten
|
55
67
|
obj = ::FFI::MemoryPointer.new(:float, flat_objects.size)
|
@@ -70,11 +82,13 @@ module Ngt
|
|
70
82
|
|
71
83
|
def object(id)
|
72
84
|
if object_type == :float
|
73
|
-
res = ffi(:ngt_get_object_as_float,
|
85
|
+
res = ffi(:ngt_get_object_as_float, object_space, id)
|
74
86
|
res.read_array_of_float(dimensions)
|
75
|
-
|
76
|
-
res = ffi(:ngt_get_object_as_integer,
|
87
|
+
elsif object_type == :integer
|
88
|
+
res = ffi(:ngt_get_object_as_integer, object_space, id)
|
77
89
|
res.read_array_of_uint8(dimensions)
|
90
|
+
else
|
91
|
+
raise Error, "Method not supported for this object type"
|
78
92
|
end
|
79
93
|
end
|
80
94
|
|
@@ -85,7 +99,8 @@ module Ngt
|
|
85
99
|
def search(query, size: 20, epsilon: 0.1, radius: nil)
|
86
100
|
radius ||= -1.0
|
87
101
|
results = ffi(:ngt_create_empty_results)
|
88
|
-
|
102
|
+
query = query.to_a
|
103
|
+
ffi(:ngt_search_index, @index, c_object(query), query.size, size, epsilon, radius, results)
|
89
104
|
result_size = ffi(:ngt_get_result_size, results)
|
90
105
|
ret = []
|
91
106
|
result_size.times do |i|
|
@@ -125,6 +140,8 @@ module Ngt
|
|
125
140
|
case object_type.to_s.downcase
|
126
141
|
when "float"
|
127
142
|
ffi(:ngt_set_property_object_type_float, property, error)
|
143
|
+
when "float16"
|
144
|
+
ffi(:ngt_set_property_object_type_float16, property, error)
|
128
145
|
when "integer"
|
129
146
|
ffi(:ngt_set_property_object_type_integer, property, error)
|
130
147
|
else
|
@@ -196,9 +213,18 @@ module Ngt
|
|
196
213
|
end
|
197
214
|
|
198
215
|
def c_object(object)
|
216
|
+
check_dimensions(object.size)
|
199
217
|
c_object = ::FFI::MemoryPointer.new(:double, object.size)
|
200
218
|
c_object.write_array_of_double(object)
|
201
219
|
c_object
|
202
220
|
end
|
221
|
+
|
222
|
+
def check_dimensions(d)
|
223
|
+
raise ArgumentError, "Bad dimensions" if d != dimensions
|
224
|
+
end
|
225
|
+
|
226
|
+
def object_space
|
227
|
+
@object_space ||= ffi(:ngt_get_object_space, @index)
|
228
|
+
end
|
203
229
|
end
|
204
230
|
end
|
data/lib/ngt/utils.rb
CHANGED
@@ -4,7 +4,10 @@ module Ngt
|
|
4
4
|
def self.ffi(method, *args)
|
5
5
|
res = FFI.send(method, *args)
|
6
6
|
message = FFI.ngt_get_error_string(args.last)
|
7
|
-
|
7
|
+
unless message.empty?
|
8
|
+
FFI.ngt_clear_error_string(args.last)
|
9
|
+
raise Error, message
|
10
|
+
end
|
8
11
|
res
|
9
12
|
end
|
10
13
|
|
data/lib/ngt/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ngt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|