Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.apm/
Binary file added Bin/Cpu/libfaiss.so
Binary file not shown.
Binary file added Bin/Cpu/libfaiss_c.so
Binary file not shown.
Binary file added Bin/Gpu/libfaiss.so
Binary file not shown.
Binary file added Bin/Gpu/libfaiss_c.so
Binary file not shown.
37 changes: 37 additions & 0 deletions BufferList.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@merge module Faiss {
class BufferList {
@expname[faiss_BufferList_new]
func new(
obj: ref[ref[BufferList]],
bufferSize: ArchWord
): Int;

@expname[faiss_BufferList_free]
func free(
bl: ref[BufferList]
);

@expname[faiss_BufferList_append_buffer]
handler this.appendBuffer(): Int;

@expname[faiss_BufferList_add]
handler this.add(
id: Int[64],
dis: Float
): Int;

@expname[faiss_BufferList_copy_range]
handler this.copyRange(
ofs: ArchWord,
n: ArchWord,
destIds: ref[array[Int[64]]],
destDis: ref[array[Float]]
): Int;

@expname[faiss_BufferList_buffer_size]
handler this.bufferSize: ArchWord;

@expname[faiss_BufferList_wp]
handler this.wp: ArchWord;
}
}
121 changes: 121 additions & 0 deletions Clustering.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
@merge module Faiss {
class Clustering {
@expname[faiss_Clustering_new]
func new(
out: ref[ref[Clustering]],
d: Int,
k: Int
): Int;

@expname[faiss_Clustering_new_with_params]
func new(
out: ref[ref[Clustering]],
d: Int,
k: Int,
params: ptr[ClusteringParameters]
): Int;

@expname[faiss_Clustering_free]
func free(
clustering: ref[Clustering]
);

@expname[faiss_Clustering_niter]
handler this.nIter: Int;

@expname[faiss_Clustering_nredo]
handler this.nRedo: Int;

@expname[faiss_Clustering_verbose]
handler this.verbose: Int;

@expname[faiss_Clustering_spherical]
handler this.spherical: Int;

@expname[faiss_Clustering_int_centroids]
handler this.intCentroids: Int;

@expname[faiss_Clustering_update_index]
handler this.updateIndex: Int;

@expname[faiss_Clustering_frozen_centroids]
handler this.frozenCentroids: Int;

@expname[faiss_Clustering_min_points_per_centroid]
handler this.minPointsPerCentroid: Int;

@expname[faiss_Clustering_max_points_per_centroid]
handler this.maxPointsPerCentroid: Int;

@expname[faiss_Clustering_seed]
handler this.seed: Int;

@expname[faiss_Clustering_decode_block_size]
handler this.decodeBlockSize: ArchWord;

@expname[faiss_Clustering_d]
handler this.d: ArchWord;

@expname[faiss_Clustering_k]
handler this.k: ArchWord;

@expname[faiss_Clustering_centroids]
handler this.getCentroids(
centroids: ref[ref[array[Float]]],
size: ref[ArchWord]
);

@expname[faiss_Clustering_iteration_stats]
handler this.getIterationStats(
stats_out: ref[ref[ClusteringIterationStats]],
size: ref[ArchWord]
);

@expname[faiss_Clustering_train]
handler this.train(
n: Int[64],
x: ref[Float],
index: ref[Index]
): Int;
}

class ClusteringParameters {
def niter: Int; ///< clustering iterations
def nredo: Int; ///< redo clustering this many times and keep best

def verbose: Int; ///< (bool)
def spherical: Int; ///< (bool) do we want normalized centroids?
def intCentroids: Int; ///< (bool) round centroids coordinates to integer
def updateIndex: Int; ///< (bool) update index after each iteration?
def frozenCentroids: Int; ///< (bool) use the centroids provided as input and do
///< not change them during iterations

def minPointsPerCentroid: Int; ///< otherwise you get a warning
def maxPointsPerCentroid: Int; ///< to limit size of dataset

def seed: Int; ///< seed for the random number generator
def decodeBlockSize: ArchWord; ///< how many vectors at a time to decode

@expname[faiss_ClusteringParameters_init]
func init(
params: ref[ref[ClusteringParameters]]
);
}

class ClusteringIterationStats {
@expname[faiss_ClusteringIterationStats_obj]
handler this.obj: Float;

@expname[faiss_ClusteringIterationStats_time]
handler this.time: Float[64];

@expname[faiss_ClusteringIterationStats_time_search]
handler this.timeSearch: Float[64];

@expname[faiss_ClusteringIterationStats_imbalance_factor]
handler this.imbalanceFactor: Float[64];

@expname[faiss_ClusteringIterationStats_nsplit]
handler this.nSplit: Int;
}
}
24 changes: 24 additions & 0 deletions DistanceComputer.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@merge module Faiss {
class DistanceComputer {
@expname[faiss_DistanceComputer_set_query]
handler this.setQuery(
x: ref[array[Float]]
): Int;

@expname[faiss_DistanceComputer_vector_to_query_dis]
handler this.vectorToQueryDis(
i: Int[64],
qd: ref[array[Float]]
): Int;

@expname[faiss_DistanceComputer_symmetric_dis]
handler this.symmetricDis(
i: Int[64],
j: Int[64],
vd: ref[array[Float]]
): Int;

@expname[faiss_DistanceComputer_free]
func free(dc: ref[DistanceComputer]);
}
}
52 changes: 52 additions & 0 deletions Examples/example.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import "Srl/Console";
import "Srl/Array";
import "Apm";
Apm.importFile("Alusus/Faiss");
use Srl;
use Faiss;

// Input data
def D: Int = 4;
def xb: Array[Float]({
1.0, 2.0, 3.0, 4.0,
2.0, 3.0, 4.0, 5.0,
10.0, 11.0, 12.0, 13.0,
11.0, 12.0, 13.0, 14.0,
5.0, 6.0, 7.0, 8.0
});
def xq: Array[Float]({
1.5, 2.5, 3.5, 4.5,
10.5, 11.5, 12.5, 13.5
});

// Prepare the index
def index: ref[Index];
if Index.new(index, D, "Flat", MetricType.INNER_PRODUCT) == 0 {
Console.print("Index.new: OK \n");
} else {
Console.print("Index.new: ERROR \n");
}
index.add(xb.getLength() / D, xb.buf);

// Perform the search
def labels: array[Int[64], 6];
def distances: array[Float, 6];
index.search(xq.getLength() / D, xq.buf, 3, distances, labels);

// Print the resutls
def i: Int;
def j: Int;
for i = 0, i < xq.getLength() / D, i++ {
Console.print("Query %i:\n", i);
for j = 0, j < 3, j++ {
Console.print(" Neighbor ");
Console.print(j);
Console.print(", idx= ");
Console.print(labels(i * 3 + j));
Console.print(", dist= ");
Console.print(distances(i * 3 + j));
Console.print("\n");
}
}

Index.free(index);
59 changes: 59 additions & 0 deletions Examples/example2.alusus
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import "Srl/Console";
import "Srl/Array";
import "Apm";
Apm.importFile("Alusus/Faiss");
use Faiss;
use Srl;

def D: int = 8;
def NB: Int[64] = 80;
def NQ: Int[64] = 2;
def K: Int[64] = 3;

// Prepare the data
def xb: Array[Float];
def i: Int[64] = 0;
for i = 0, i < NB * D, i++ {
xb.add((i % D) + 1);
}
def xq: Array[Float];
for i = 0, i < NQ * D, i++ {
xq.add((i % D) + 1.5);
}

// Prepare the index
def index: ref[IndexIvf];
if Index.new(index, D, "IVF1,Flat", MetricType.L2) == 0 {
Console.print("Index.new: OK\n");
} else {
Console.print("Index.new: ERROR\n");
}
index.train(NB, xb.buf);
index.add(NB, xb.buf);

def ps: ref[ParameterSpace];
ParameterSpace.new(ps);
ps.setIndexParameter(index, "nprobe", 2.0);
ParameterSpace.free(ps);

// Perform the search
def labels: array[Int[64], 6];
def distances: array[float, 6];
index.search(NQ, xq.buf, 3, distances, labels);

// Print the results
def j: int = 0;
for i = 0, i < NQ, i++ {
Console.print("Query %i:\n", i);
for j = 0, j < 3, j++ {
Console.print(" Neighbor ");
Console.print(j);
Console.print(", idx= ");
Console.print(labels(i * 3 + j));
Console.print(", dist= ");
Console.print(distances(i * 3 + j));
Console.print("\n");
}
}

Index.free(index);
52 changes: 52 additions & 0 deletions Examples/مثال.أسس
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
اشمل "مـتم/طـرفية"؛
اشمل "مـتم/مـصفوفة"؛
اشمل "مـحا"؛
مـحا.اشمل_ملف("Alusus/Faiss"، "فـيس.أسس")؛
استخدم مـتم؛
استخدم فـيس؛

// بيانات الإدخال
عرف _ب_: صحيح = 4؛
عرف سب: مـصفوفة[عـائم]({
1.0، 2.0، 3.0، 4.0،
2.0، 3.0، 4.0، 5.0،
10.0، 11.0، 12.0، 13.0،
11.0، 12.0، 13.0، 14.0،
5.0، 6.0، 7.0، 8.0
})؛
عرف سس: مـصفوفة[عـائم]({
1.5، 2.5، 3.5، 4.5،
10.5، 11.5، 12.5، 13.5
})؛

// تحضير الفهرس
عرف الفهرس: سند[فـهرس]؛
إذا فـهرس.أنشئ(الفهرس، _ب_، "Flat"، نـوع_قياس._نتاج_داخلي_) == 0 {
طـرفية.اطبع("فـهرس.أنشئ: نجح\ج")؛
} وإلا {
طـرفية.اطبع("فـهرس.أنشئ: خطأ\ج")؛
}
الفهرس.أضف(سب.هات_الطول() / _ب_، سب.صوان)؛

// تنفيذ البحث
عرف الوسوم: مصفوفة[صحيح[64]، 6]؛
عرف المسافات: مصفوفة[عـائم، 6]؛
الفهرس.ابحث(سس.هات_الطول() / _ب_، سس.صوان، 3، المسافات، الوسوم)؛

// طباعة النتائج
عرف ت: صحيح؛
عرف ج: صحيح؛
لكل ت = 0، ت < سس.هات_الطول() / _ب_، ت++ {
طـرفية.اطبع("الاستعلام %i:\n"، ت)؛
لكل ج = 0، ج < 3، ج++ {
طـرفية.اطبع(" الجار ")؛
طـرفية.اطبع(ج)؛
طـرفية.اطبع("، رقم= ")؛
طـرفية.اطبع(الوسوم(ت * 3 + ج))؛
طـرفية.اطبع("، مسافة= ")؛
طـرفية.اطبع(المسافات(ت * 3 + ج))؛
طـرفية.اطبع("\n")؛
}
}

فـهرس.حرر(الفهرس)؛
Loading