diff --git a/README.ar.md b/README.ar.md index 3eaaac3..0a7b4f8 100644 --- a/README.ar.md +++ b/README.ar.md @@ -1,6 +1,5 @@ -
- # فـيس Faiss + [[English]](README.md) روابط لغة الأسس لمكتبة [FAISS](https://github.com/facebookresearch/faiss) - مكتبة للبحث الفعّال عن التشابه وتجميع المتجهات الكثيفة. @@ -11,29 +10,31 @@ ## التثبيت -```alusus +
+ +``` اشمل "مـحا"؛ مـحا.اشمل_حزمة("Alusus/Faiss@0.1"، "فـيس.أسس")؛ استخدم فـيس؛ ``` -أو بالإنجليزية: +
-
+أو بالإنجليزية: -```alusus +``` import "Apm"; Apm.importPackage("Alusus/Faiss@0.1"); use Faiss; ``` -
- ## البدء السريع ### مثال بالعربية -```alusus +
+ +``` اشمل "مـتم/طـرفية"؛ اشمل "مـتم/مـصفوفة"؛ اشمل "مـحا"؛ @@ -59,11 +60,11 @@ use Faiss; فـهرس.حرر(الفهرس)؛ ``` -### مثال بالإنجليزية +
-
+### مثال بالإنجليزية -```alusus +``` import "Srl/Console"; import "Srl/Array"; import "Apm"; @@ -89,739 +90,1030 @@ index.search(1, xq.buf, 3, distances, labels); // Find 3 nearest neighbors Index.free(index); ``` -
- انظر الأمثلة الكاملة في مجلد `Examples/`. ## التوثيق تلتف هذه المكتبة حول واجهة FAISS البرمجية بلغة C. للحصول على توثيق مفصل حول المفاهيم والخوارزميات وأفضل الممارسات، يرجى الرجوع إلى التوثيق الرسمي لـ FAISS: -- **التوثيق الرئيسي**: https://github.com/facebookresearch/faiss/wiki -- **مرجع واجهة C البرمجية**: https://github.com/facebookresearch/faiss/blob/main/c_api/ -- **دليل البدء**: https://github.com/facebookresearch/faiss/wiki/Getting-started -- **دليل اختيار الفهرس**: https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index +* **التوثيق الرئيسي**: https://github.com/facebookresearch/faiss/wiki +* **مرجع واجهة C البرمجية**: https://github.com/facebookresearch/faiss/blob/main/c_api/ +* **دليل البدء**: https://github.com/facebookresearch/faiss/wiki/Getting-started +* **دليل اختيار الفهرس**: https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index ## مرجع الواجهة البرمجية -### الأصناف الأساسية +### فـهرس / Index -#### فـهرس / Index الصنف الرئيسي للبحث عن التشابه. [توثيق واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) -**الدوال الساكنة:** -- `أنشئ` / `new` - ```alusus - فـهرس.أنشئ(الكائن: سند[سند[فـهرس]]، ب: صـحيح، وصف: مـؤشر_محارف، نوع_القياس: صـحيح): صـحيح - ``` +**الأصناف الأساسية:** -
+#### أنشئ / new - ```alusus - Index.new(obj: ref[ref[Index]]، d: Int، وصف: CharsPtr، metric: Int): Int - ``` +
+ +``` +دالة أنشئ(الكائن: سند[سند[فـهرس]]، ب: صـحيح، وصف: مـؤشر_محارف، نوع_القياس: صـحيح): صـحيح +```
- إنشاء فهرس باستخدام نص المصنع +``` +func new(obj: ref[ref[Index]], d: Int, description: CharsPtr, metric: Int): Int +``` -- `حمل` / `load` - ```alusus - فـهرس.حمل(اسم_الملف: مـؤشر_محارف، خيارات: صـحيح، الكائن: سند[سند[فـهرس]]): صـحيح - ``` +إنشاء فهرس باستخدام نص المصنع. -
+#### حمل / load - ```alusus - Index.load(fname: CharsPtr, flags: Int, obj: ref[ref[Index]]): Int - ``` +
+ +``` +دالة حمل(اسم_الملف: مـؤشر_محارف، خيارات: صـحيح، الكائن: سند[سند[فـهرس]]): صـحيح +```
- تحميل فهرس من ملف +``` +func load(fname: CharsPtr, flags: Int, obj: ref[ref[Index]]): Int +``` + +تحميل فهرس من ملف. + +#### احفظ / save + +
+ +``` +دالة احفظ(الكائن: سند[فـهرس]، اسم_الملف: مـؤشر_محارف): صـحيح +``` + +
+ +``` +func save(obj: ref[Index], fname: CharsPtr): Int +``` + +حفظ الفهرس إلى ملف. -- `احفظ` / `save` - ```alusus - فـهرس.احفظ(الكائن: سند[فـهرس]، اسم_الملف: مـؤشر_محارف): صـحيح - ``` +#### حرر / free -
+
- ```alusus - Index.save(obj: ref[Index], fname: CharsPtr): Int - ``` +``` +دالة حرر(obj: سند[فـهرس]) +```
- حفظ الفهرس إلى ملف +``` +func free(obj: ref[Index]) +``` + +تحرير ذاكرة الفهرس. **الدوال الرئيسية:** -- `درب` / `train` - ```alusus - فـهرس.درب(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]): صـحيح - ``` - -
- ```alusus - Index.train(n: Int[64]، x: ref[array[Float]]): Int - ``` +#### درب / train + +
+ +``` +عملية هذا.درب(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]): صـحيح +```
- تدريب الفهرس على البيانات +``` +handler this.train(n: Int[64], x: ref[array[Float]]): Int +``` + +تدريب الفهرس على البيانات. -- `أضف` / `add` - ```alusus - فـهرس.أضف(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]): صـحيح - ``` - -
+#### أضف / add - ```alusus - Index.add(n: Int[64]، x: ref[array[Float]]): Int - ``` +
+ +``` +عملية هذا.أضف(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]): صـحيح +```
- إضافة متجهات إلى الفهرس +``` +handler this.add(n: Int[64], x: ref[array[Float]]): Int +``` + +إضافة متجهات إلى الفهرس. -- `ابحث` / `search` - ```alusus - فـهرس.ابحث(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]، k: صـحيح[64]، - مسافات: سند[مصفوفة[عـائم]]، labels: سند[مصفوفة[صـحيح[64]]] - ): صـحيح - ``` - -
+#### ابحث / search - ```alusus - Index.search(n: Int[64]، x: ref[array[Float]]، k: Int[64]، - distances: ref[array[Float]]، labels: ref[array[Int[64]]] - ): Int - ``` +
+ +``` +عملية هذا.ابحث(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]، k: صـحيح[64]، + مسافات: سند[مصفوفة[عـائم]]، labels: سند[مصفوفة[صـحيح[64]]] + ): صـحيح +```
- البحث عن k من أقرب الجيران +``` +handler this.search(n: Int[64], x: ref[array[Float]], k: Int[64], distances: ref[array[Float]], labels: ref[array[Int[64]]]): Int +``` -- `بحث_المدى` / `rangeSearch` - ```alusus - فـهرس.بحث_المدى(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]، radius: عـائم، - result: سند[نـتيجة_بحث_مدى]): صـحيح - ``` +البحث عن k من أقرب الجيران. -
+#### بحث_المدى / rangeSearch - ```alusus - Index.rangeSearch(n: Int[64], x: ref[array[Float]], radius: Float, - result: ref[RangeSearchResult]): Int - ``` +
+ +``` +عملية هذا.بحث_المدى(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]، radius: عـائم، result: سند[نـتيجة_بحث_مدى]): صـحيح +```
- البحث بنطاق +``` +handler this.rangeSearch(n: Int[64], x: ref[array[Float]], radius: Float, result: ref[RangeSearchResult]): Int +``` + +البحث بنطاق. -- `أعد_الضبط` / `reset` - ```alusus - فـهرس.أعد_الضبط(): صـحيح - ``` +#### أعد_الضبط / reset -
+
- ```alusus - Index.reset(): Int - ``` +``` +عملية هذا.أعد_الضبط(): صـحيح +```
- إزالة جميع المتجهات من الفهرس +``` +handler this.reset(): Int +``` -- `احذف_المعرفات` / `removeIds` - ```alusus - فـهرس.احذف_المعرفات(sel: سند[مـنتقي_معرف]، nRemoved: سند[كلمة_معمارية]): صـحيح - ``` +إزالة جميع المتجهات من الفهرس. -
+#### احذف_المعرفات / removeIds - ```alusus - Index.removeIds(sel: ref[IdSelector], nRemoved: ref[ArchWord]): Int - ``` +
+ +``` +عملية هذا.احذف_المعرفات(sel: سند[مـنتقي_معرف]، nRemoved: سند[طـبيعي_متكيف]): صـحيح +```
- إزالة متجهات محددة +``` +handler this.removeIds(sel: ref[IdSelector], nRemoved: ref[ArchWord]): Int +``` + +إزالة متجهات محددة. **الخصائص:** -- `البعد` / `d`: `صحيح[64]` - بُعد المتجه -- `العدد_الكلي` / `nTotal`: `صحيح[64]` - العدد الإجمالي للمتجهات المفهرسة -- `مدرب` / `isTrained`: `صحيح` - ما إذا كان الفهرس مدرباً (0 أو 1) -- `نوع_القياس` / `metricType`: `نـوع_قياس` - مقياس المسافة المستخدم -- `إطناب` / `verbose`: `صحيح` - مستوى الإسهاب -**التنظيف:** -- `حرر` / `free` - ```alusus - فـهرس.حرر(obj: سند[فـهرس]) - ``` +#### البعد / d + +
+ +``` +البعد: صحيح[64] +``` + +
+ +``` +d: Int[64] +``` + +بُعد المتجه. + +#### العدد_الكلي / nTotal + +
+ +``` +العدد_الكلي: صحيح[64] +``` + +
+ +``` +nTotal: Int[64] +``` + +العدد الإجمالي للمتجهات المفهرسة. + +#### مدرب / isTrained + +
+ +``` +مدرب: صحيح +``` + +
+ +``` +isTrained: Int +``` + +ما إذا كان الفهرس مدرباً (0 أو 1). -
+#### نوع_القياس / metricType + +
- ```alusus - Index.free(obj: ref[Index]) - ``` +``` +نوع_القياس: نـوع_قياس +```
- تحرير ذاكرة الفهرس +``` +metricType: MetricType +``` + +مقياس المسافة المستخدم. + +#### إطناب / verbose + +
+ +``` +إطناب: صحيح +``` + +
+ +``` +verbose: Int +``` + +مستوى الإسهاب. + +### فـهرس_مسطح / IndexFlat -#### فـهرس_مسطح / IndexFlat فهرس القوة الغاشمة الذي يقوم بالبحث الدقيق. [دليل](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes#flat-indexes) **الإنشاء:** -- `أنشئ` / `new` - ```alusus - فـهرس_مسطح.أنشئ(obj: سند[سند[فـهرس_مسطح]]): صـحيح - فـهرس_مسطح.أنشئ(obj: سند[سند[فـهرس_مسطح]]، d: صـحيح[64]، metric: نـوع_قياس): صـحيح - ``` -
+#### أنشئ / new - ```alusus - IndexFlat.new(obj: ref[ref[IndexFlat]]): Int - IndexFlat.new(obj: ref[ref[IndexFlat]], d: Int[64], metric: MetricType): Int - ``` +
+ +``` +دالة أنشئ(obj: سند[سند[فـهرس_مسطح]]): صـحيح +دالة أنشئ(obj: سند[سند[فـهرس_مسطح]]، d: صـحيح[64]، metric: نـوع_قياس): صـحيح +```
+``` +func new(obj: ref[ref[IndexFlat]]): Int +func new(obj: ref[ref[IndexFlat]], d: Int[64], metric: MetricType): Int +``` + **دوال إضافية:** -- `هات_البيانات` / `getXb` - ```alusus - فـهرس_مسطح.هات_البيانات(outXb: سند[سند[مصفوفة[عـائم]]]، outSize: سند[كلمة_معمارية]) - ``` -
+#### هات_البيانات / getXb + +
- ```alusus - IndexFlat.getXb(outXb: ref[ref[array[Float]]], outSize: ref[ArchWord]) - ``` +``` +عملية هذا.هات_البيانات(outXb: سند[سند[مصفوفة[عـائم]]]، outSize: سند[طـبيعي_متكيف]) +```
- الحصول على المتجهات المخزنة +``` +handler this.getXb(outXb: ref[ref[array[Float]]], outSize: ref[ArchWord]) +``` -- `احسب_مسافة_مجموعة_جزئية` / `computeDistanceSubset` - ```alusus - فـهرس_مسطح.احسب_مسافة_مجموعة_جزئية(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]، k: صـحيح[64]، - outDistances: سند[مصفوفة[عـائم]]، labels: سند[مصفوفة[صـحيح[64]]]): صـحيح - ``` +الحصول على المتجهات المخزنة. -
+#### احسب_مسافة_مجموعة_جزئية / computeDistanceSubset - ```alusus - IndexFlat.computeDistanceSubset(n: Int[64], x: ref[array[Float]], k: Int[64], - outDistances: ref[array[Float]], labels: ref[array[Int[64]]]): Int - ``` +
+ +``` +عملية هذا.احسب_مسافة_مجموعة_جزئية(n: صـحيح[64]، x: سند[مصفوفة[عـائم]]، k: صـحيح[64]، outDistances: سند[مصفوفة[عـائم]]، labels: سند[مصفوفة[صـحيح[64]]]): صـحيح +```
- حساب المسافات إلى مجموعة جزئية +``` +handler this.computeDistanceSubset(n: Int[64], x: ref[array[Float]], k: Int[64], outDistances: ref[array[Float]], labels: ref[array[Int[64]]]): Int +``` + +حساب المسافات إلى مجموعة جزئية. يرث جميع دوال فـهرس / Index. -#### فـهرس_مسطح_آيبي / IndexFlatIp +### فـهرس_مسطح_آيبي / IndexFlatIp + فهرس مسطح متخصص لمقياس الجداء الداخلي. [توثيق](https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances) **الإنشاء:** -- `أنشئ` / `new` - ```alusus - فـهرس_مسطح_آيبي.أنشئ(obj: سند[سند[فـهرس_مسطح_آيبي]]): صـحيح - فـهرس_مسطح_آيبي.أنشئ(obj: سند[سند[فـهرس_مسطح_آيبي]]، d: صـحيح[64]): صـحيح - ``` -
+#### أنشئ / new + +
- ```alusus - IndexFlatIp.new(obj: ref[ref[IndexFlatIp]]): Int - IndexFlatIp.new(obj: ref[ref[IndexFlatIp]], d: Int[64]): Int - ``` +``` +دالة أنشئ(obj: سند[سند[فـهرس_مسطح_آيبي]]): صـحيح +دالة أنشئ(obj: سند[سند[فـهرس_مسطح_آيبي]]، d: صـحيح[64]): صـحيح +```
-#### فـهرس_مسطح_ل2 / IndexFlatL2 +``` +func IndexFlatIp.new(obj: ref[ref[IndexFlatIp]]): Int +func IndexFlatIp.new(obj: ref[ref[IndexFlatIp]], d: Int[64]): Int +``` + +### فـهرس_مسطح_ل2 / IndexFlatL2 + فهرس مسطح متخصص لمسافة L2 (إقليدس). [توثيق](https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances) **الإنشاء:** -- `أنشئ` / `new` - ```alusus - فـهرس_مسطح_ل2.أنشئ(obj: سند[سند[فـهرس_مسطح_ل2]]): صـحيح - فـهرس_مسطح_ل2.أنشئ(obj: سند[سند[فـهرس_مسطح_ل2]]، d: صـحيح[64]): صـحيح - ``` -
+#### أنشئ / new - ```alusus - IndexFlatL2.new(obj: ref[ref[IndexFlatL2]]): Int - IndexFlatL2.new(obj: ref[ref[IndexFlatL2]], d: Int[64]): Int - ``` +
+ +``` +دالة أنشئ(obj: سند[سند[فـهرس_مسطح_ل2]]): صـحيح +دالة أنشئ(obj: سند[سند[فـهرس_مسطح_ل2]]، d: صـحيح[64]): صـحيح +```
-#### فـهرس_ملف_معكوس / IndexIvf +``` +func new(obj: ref[ref[IndexFlatL2]]): Int +func new(obj: ref[ref[IndexFlatL2]], d: Int[64]): Int +``` + +### فـهرس_ملف_معكوس / IndexIvf + فهرس الملفات المعكوسة للبحث التقريبي الأسرع. [دليل](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes#cell-probe-methods-indexivf-indexes) -**خصائص إضافية:** -- `عدد_القوائم` / `nList`: `كلمة_معمارية` - عدد القوائم المعكوسة (العناقيد) -- `عدد_الاستقصاءات` / `nProbe`: `كلمة_معمارية` - عدد العناقيد المراد زيارتها أثناء البحث (قابل للضبط) -- `المكمم` / `quantizer`: `سند[فـهرس]` - فهرس المكمم -- `يمتلك_الحقول` / `ownFields`: `صحيح` - ما إذا كان الفهرس يمتلك حقوله +#### عدد_القوائم / nList + +
+ +``` +عدد_القوائم: طـبيعي_متكيف +``` + +
+ +عدد القوائم المعكوسة (العناقيد). + +#### عدد_الاستقصاءات / nProbe + +
+ +``` +عدد_الاستقصاءات: طـبيعي_متكيف +``` + +
+ +عدد العناقيد المراد زيارتها أثناء البحث (قابل للضبط). + +#### المكمم / quantizer + +
+ +``` +المكمم: سند[فـهرس] +``` + +
+ +فهرس المكمم. + +#### يمتلك_الحقول / ownFields + +
+ +``` +يمتلك_الحقول: صحيح +``` + +
+ +ما إذا كان الفهرس يمتلك حقوله. **دوال إضافية:** -- `ادمج_من` / `mergeFrom` - ```alusus - فـهرس_ملف_معكوس.ادمج_من(other: سند[فـهرس_ملف_معكوس]، addId: صـحيح[64]): صـحيح - ``` -
+#### ادمج_من / mergeFrom - ```alusus - IndexIvf.mergeFrom(other: ref[IndexIvf], addId: Int[64]): Int - ``` +
+ +``` +عملية هذا.ادمج_من(other: سند[فـهرس_ملف_معكوس]، addId: صـحيح[64]): صـحيح +```
- دمج فهرس IVF آخر +``` +handler this.mergeFrom(other: ref[IndexIvf], addId: Int[64]): Int +``` + +دمج فهرس IVF آخر. -- `انسخ_مجموعة_جزئية_إلى` / `copySubsetTo` - ```alusus - فـهرس_ملف_معكوس.انسخ_مجموعة_جزئية_إلى(other: سند[فـهرس_ملف_معكوس]، subsetType: صـحيح، a1: صـحيح[64]، a2: صـحيح[64]): صـحيح - ``` +#### انسخ_مجموعة_جزئية_إلى / copySubsetTo -
+
- ```alusus - IndexIvf.copySubsetTo(other: ref[IndexIvf], subsetType: Int, a1: Int[64], a2: Int[64]): Int - ``` +``` +عملية هذا.انسخ_مجموعة_جزئية_إلى(other: سند[فـهرس_ملف_معكوس]، subsetType: صـحيح، a1: صـحيح[64]، a2: صـحيح[64]): صـحيح +```
- نسخ مجموعة جزئية من المتجهات +``` +handler this.copySubsetTo(other: ref[IndexIvf], subsetType: Int, a1: Int[64], a2: Int[64]): Int +``` + +نسخ مجموعة جزئية من المتجهات. -- `هات_حجم_القائمة` / `getListSize` - ```alusus - فـهرس_ملف_معكوس.هات_حجم_القائمة(listNo: كلمة_معمارية): كلمة_معمارية - ``` +#### هات_حجم_القائمة / getListSize -
+
- ```alusus - IndexIvf.getListSize(listNo: ArchWord): ArchWord - ``` +``` +عملية هذا.هات_حجم_القائمة(listNo: طـبيعي_متكيف): طـبيعي_متكيف +```
- الحصول على حجم القائمة المعكوسة +``` +handler this.getListSize(listNo: ArchWord): ArchWord +``` -- `اصنع_تعيينا_مباشرا` / `makeDirectMap` - ```alusus - فـهرس_ملف_معكوس.اصنع_تعيينا_مباشرا(newMaintainDirectMap: صـحيح): صـحيح - ``` +الحصول على حجم القائمة المعكوسة. -
+#### اصنع_تعيينا_مباشرا / makeDirectMap - ```alusus - IndexIvf.makeDirectMap(newMaintainDirectMap: Int): Int - ``` +
+ +``` +عملية هذا.اصنع_تعيينا_مباشرا(newMaintainDirectMap: صـحيح): صـحيح +```
- إنشاء خريطة مباشرة لإعادة البناء +``` +handler this.makeDirectMap(newMaintainDirectMap: Int): Int +``` + +إنشاء خريطة مباشرة لإعادة البناء. + +#### عامل_عدم_التوازن / imbalanceFactor + +
+ +``` +عملية هذا.عامل_عدم_التوازن: عـائم[64] +``` -- `عامل_عدم_التوازن` / `imbalanceFactor`: `عـائم[64]` - الحصول على عامل عدم توازن العناقيد -- `اطبع_الإحصائيات` / `printStats`: `()` - طباعة إحصائيات الفهرس +
+ +``` +handler this.imbalanceFactor: Float[64] +``` + +الحصول على عامل عدم توازن العناقيد. + +#### اطبع_الإحصائيات / printStats + +
+ +``` +عملية هذا.اطبع_الإحصائيات() +``` + +
+ +``` +handler this.printStats() +``` + +طباعة إحصائيات الفهرس. + +### فـهرس_ثنائي / IndexBinary -#### فـهرس_ثنائي / IndexBinary فهرس للمتجهات الثنائية (هامينغ). [دليل](https://github.com/facebookresearch/faiss/wiki/Binary-indexes) مشابه لـ فـهرس / Index ولكنه يعمل على المتجهات الثنائية (مصفوفات Word[8] بدلاً من Float). -### الأصناف الداعمة +**الأصناف الداعمة** + +### فـضاء_وسيط / ParameterSpace -#### فـضاء_وسيط / ParameterSpace إدارة معاملات الفهرس للبحث الشبكي والضبط. [واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/ParameterSpace_c.h) **الدوال:** -- `أنشئ` / `new` - ```alusus - فـضاء_وسيط.أنشئ(parameterSpace: سند[سند[فـضاء_وسيط]]): صـحيح - ``` -
+#### أنشئ / new - ```alusus - ParameterSpace.new(parameterSpace: ref[ref[ParameterSpace]]): Int - ``` +
+ +``` +دالة أنشئ(parameterSpace: سند[سند[فـضاء_وسيط]]): صـحيح +```
-- `حدد_وسيط_فهرس` / `setIndexParameter` - ```alusus - فـضاء_وسيط.حدد_وسيط_فهرس(index: سند[فـهرس]، paramName: مـؤشر_محارف، val: عـائم[64]): صـحيح - ``` +``` +func new(parameterSpace: ref[ref[ParameterSpace]]): Int +``` -
+#### حدد_وسيط_فهرس / setIndexParameter + +
- ```alusus - ParameterSpace.setIndexParameter(index: ref[Index], paramName: CharsPtr, val: Float[64]): Int - ``` +``` +عملية هذا.حدد_وسيط_فهرس(index: سند[فـهرس]، paramName: مـؤشر_محارف، val: عـائم[64]): صـحيح +```
- تعيين معامل واحد +``` +handler this.setIndexParameter(index: ref[Index], paramName: CharsPtr, val: Float[64]): Int +``` -- `حدد_وسطاء_فهرس` / `setIndexParameters` - ```alusus - فـضاء_وسيط.حدد_وسطاء_فهرس(index: سند[فـهرس]، params: مـؤشر_محارف): صـحيح - ``` +تعيين معامل واحد. -
+#### حدد_وسطاء_فهرس / setIndexParameters - ```alusus - ParameterSpace.setIndexParameters(index: ref[Index], params: CharsPtr): Int - ``` +
+ +``` +عملية هذا.حدد_وسطاء_فهرس(index: سند[فـهرس]، params: مـؤشر_محارف): صـحيح +```
- تعيين معاملات متعددة +``` +handler this.setIndexParameters(index: ref[Index], params: CharsPtr): Int +``` + +تعيين معاملات متعددة. -- `أضف_مدى` / `addRange` - ```alusus - فـضاء_وسيط.أضف_مدى(name: مـؤشر_محارف، outRange: سند[سند[مـدى_وسيط]]): صـحيح - ``` +#### أضف_مدى / addRange -
+
- ```alusus - ParameterSpace.addRange(name: CharsPtr, outRange: ref[ref[ParameterRange]]): Int - ``` +``` +عملية هذا.أضف_مدى(name: مـؤشر_محارف، outRange: سند[سند[مـدى_وسيط]]): صـحيح +```
- إضافة نطاق معامل +``` +handler this.addRange(name: CharsPtr, outRange: ref[ref[ParameterRange]]): Int +``` + +إضافة نطاق معامل. + +### وسـطاء_بحث / SearchParameters -#### وسـطاء_بحث / SearchParameters معاملات البحث في وقت التشغيل. [واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **الدوال:** -- `أنشئ` / `new` - ```alusus - وسـطاء_بحث.أنشئ(obj: سند[سند[وسـطاء_بحث]]، sel: سند[مـنتقي_معرف]): صـحيح - ``` -
+#### أنشئ / new - ```alusus - SearchParameters.new(obj: ref[ref[SearchParameters]], sel: ref[IdSelector]): Int - ``` +
+ +``` +دالة أنشئ(obj: سند[سند[وسـطاء_بحث]]، sel: سند[مـنتقي_معرف]): صـحيح +```
-- `عدد_الاستقصاءات` / `nProbe`: `صحيح` - عدد العناقيد المراد استقصاءها (لفهارس IVF) +``` +func new(obj: ref[ref[SearchParameters]], sel: ref[IdSelector]): Int +``` + +**الخصائص:** + +#### عدد_الاستقصاءات / nProbe + +
+ +``` +عدد_الاستقصاءات: صحيح +``` + +
+ +عدد العناقيد المراد استقصاءها (لفهارس IVF). + +### وسـطاء_بحث_ملف_معكوس / SearchParametersIvf -#### وسـطاء_بحث_ملف_معكوس / SearchParametersIvf معاملات بحث موسعة لفهارس IVF. **الدوال:** -- `أنشئ` / `new` - ```alusus - وسـطاء_بحث_ملف_معكوس.أنشئ(obj: سند[سند[وسـطاء_بحث_ملف_معكوس]]): صـحيح - وسـطاء_بحث_ملف_معكوس.أنشئ(obj: سند[سند[وسـطاء_بحث_ملف_معكوس]]، sel: سند[مـنتقي_معرف]، - nprobe: كلمة_معمارية، maxCodes: كلمة_معمارية): صـحيح - ``` -
+#### أنشئ / new - ```alusus - SearchParametersIvf.new(obj: ref[ref[SearchParametersIvf]]): Int - SearchParametersIvf.new(obj: ref[ref[SearchParametersIvf]], sel: ref[IdSelector], - nprobe: ArchWord, maxCodes: ArchWord): Int - ``` +
+ +``` +دالة أنشئ(obj: سند[سند[وسـطاء_بحث_ملف_معكوس]]): صـحيح +دالة أنشئ(obj: سند[سند[وسـطاء_بحث_ملف_معكوس]]، sel: سند[مـنتقي_معرف]،nprobe: طـبيعي_متكيف، maxCodes: طـبيعي_متكيف): صـحيح +```
+``` +func new(obj: ref[ref[SearchParametersIvf]]): Int +func new(obj: ref[ref[SearchParametersIvf]], sel: ref[IdSelector], nprobe: ArchWord, maxCodes: ArchWord): Int +``` + **الخصائص:** -- `المنتقي` / `sel`: `سند[مـنتقي_معرف]` - منتقي المعرف -- `عدد_الاستقصاءات` / `nProbe`: `كلمة_معمارية` - عدد العناقيد المراد استقصاءها -- `أقصى_شفرات` / `maxCodes`: `كلمة_معمارية` - الحد الأقصى للشفرات المراد فحصها -#### تـجميع / Clustering +#### المنتقي / sel + +
+ +``` +المنتقي: سند[مـنتقي_معرف] +``` + +
+ +منتقي المعرف. + +#### عدد_الاستقصاءات / nProbe + +
+ +``` +عدد_الاستقصاءات: طـبيعي_متكيف +``` + +
+ +عدد العناقيد المراد استقصاءها. + +#### أقصى_شفرات / maxCodes + +
+ +``` +أقصى_شفرات: طـبيعي_متكيف +``` + +
+ +الحد الأقصى للشفرات المراد فحصها. + +### تـجميع / Clustering + تطبيق تجميع K-means. [واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/Clustering_c.h) **الإنشاء:** -- `أنشئ` / `new` - ```alusus - تـجميع.أنشئ(out: سند[سند[تـجميع]]، d: صـحيح، k: صـحيح): صـحيح - تـجميع.أنشئ(out: سند[سند[تـجميع]]، d: صـحيح، k: صـحيح، params: مؤشر[وسـطاء_تجميع]): صـحيح - ``` -
+#### أنشئ / new - ```alusus - Clustering.new(out: ref[ref[Clustering]], d: Int, k: Int): Int - Clustering.new(out: ref[ref[Clustering]], d: Int, k: Int, params: ptr[ClusteringParameters]): Int - ``` +
+ +``` +دالة أنشئ(out: سند[سند[تـجميع]]، d: صـحيح، k: صـحيح): صـحيح +دالة أنشئ(out: سند[سند[تـجميع]]، d: صـحيح، k: صـحيح، params: مؤشر[وسـطاء_تجميع]): صـحيح +```
+``` +func new(out: ref[ref[Clustering]], d: Int, k: Int): Int +func new(out: ref[ref[Clustering]], d: Int, k: Int, params: ptr[ClusteringParameters]): Int +``` + +إنشاء بالبُعد وعدد العناقيد k. الصيغة الثانية تنشئ بمعاملات. + **الدوال:** -- `درب` / `train` - ```alusus - تـجميع.درب(n: صـحيح[64]، x: سند[عـائم]، index: سند[فـهرس]): صـحيح - ``` -
+#### درب / train - ```alusus - Clustering.train(n: Int[64], x: ref[Float], index: ref[Index]): Int - ``` +
+ +``` +عملية هذا.درب(n: صـحيح[64]، x: سند[عـائم]، index: سند[فـهرس]): صـحيح +```
- تشغيل k-means +``` +handler this.train(n: Int[64], x: ref[Float], index: ref[Index]): Int +``` -- `هات_المراكز` / `getCentroids` - ```alusus - تـجميع.هات_المراكز(centroids: سند[سند[مصفوفة[عـائم]]]، size: سند[كلمة_معمارية]) - ``` +تشغيل k-means. -
+#### هات_المراكز / getCentroids - ```alusus - Clustering.getCentroids(centroids: ref[ref[array[Float]]], size: ref[ArchWord]) - ``` +
+ +``` +عملية هذا.هات_المراكز(centroids: سند[سند[مصفوفة[عـائم]]]، size: سند[طـبيعي_متكيف]) +```
- الحصول على مراكز العناقيد +``` +handler this.getCentroids(centroids: ref[ref[array[Float]]], size: ref[ArchWord]) +``` -- `هات_إحصائيات_الدورة` / `getIterationStats` - ```alusus - تـجميع.هات_إحصائيات_الدورة(stats_out: سند[سند[إحـصائيات_دورة_تجميع]]، size: سند[كلمة_معمارية]) - ``` +الحصول على مراكز العناقيد. -
+#### هات_إحصائيات_الدورة / getIterationStats - ```alusus - Clustering.getIterationStats(stats_out: ref[ref[ClusteringIterationStats]], size: ref[ArchWord]) - ``` +
+ +``` +عملية هذا.هات_إحصائيات_الدورة(stats_out: سند[سند[إحـصائيات_دورة_تجميع]]، size: سند[طـبيعي_متكيف]) +```
- الحصول على إحصائيات التكرار +``` +handler this.getIterationStats(stats_out: ref[ref[ClusteringIterationStats]], size: ref[ArchWord]) +``` + +الحصول على إحصائيات التكرار. **الخصائص:** -- `عدد_الدورات` / `niter`: `صحيح` - عدد التكرارات -- `عدد_الإعادات` / `nredo`: `صحيح` - عدد إعادات k-means -- `عدد_المراكز` / `k`: `كلمة_معمارية` - عدد العناقيد -- `البعد` / `d`: `كلمة_معمارية` - بُعد المتجه -#### مـنتقي_معرف / IdSelector +#### عدد_الدورات / niter + +
+ +``` +عدد_الدورات: صحيح +``` + +
+ +عدد التكرارات. + +#### عدد_الإعادات / nredo + +
+ +``` +عدد_الإعادات: صحيح +``` + +
+ +عدد إعادات k-means. + +#### عدد_المراكز / k + +
+ +``` +عدد_المراكز: طـبيعي_متكيف +``` + +
+ +عدد العناقيد. + +#### البعد / d + +
+ +``` +البعد: طـبيعي_متكيف +``` + +
+ +بُعد المتجه. + +### مـنتقي_معرف / IdSelector + اختيار مجموعات فرعية من المتجهات حسب المعرف. [واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **الأنواع:** -- `مـنتقي_معرف_حزمة` / `IdSelectorBatch` - اختيار معرفات محددة من قائمة -- `مـنتقي_معرف_مدى` / `IdSelectorRange` - اختيار المعرفات في نطاق -- `مـنتقي_معرف_بتماب` / `IdSelectorBitmap` - الاختيار باستخدام خريطة بت -- `مـنتقي_معرف_نفي` / `IdSelectorNot` - عكس منتقي -- `مـنتقي_معرف_و` / `IdSelectorAnd` - دمج المنتقيات بـ AND -- `مـنتقي_معرف_أو` / `IdSelectorOr` - دمج المنتقيات بـ OR -- `مـنتقي_معرف_أو_حصري` / `IdSelectorXor` - دمج المنتقيات بـ XOR - -#### نـتيجة_بحث_مدى / RangeSearchResult +* `مـنتقي_معرف_حزمة` / `IdSelectorBatch`: اختيار معرفات محددة من قائمة +* `مـنتقي_معرف_مدى` / `IdSelectorRange`: اختيار المعرفات في نطاق +* `مـنتقي_معرف_بتماب` / `IdSelectorBitmap`: الاختيار باستخدام خريطة بت +* `مـنتقي_معرف_نفي` / `IdSelectorNot`: عكس منتقي +* `مـنتقي_معرف_و` / `IdSelectorAnd`: دمج المنتقيات بـ AND +* `مـنتقي_معرف_أو` / `IdSelectorOr`: دمج المنتقيات بـ OR +* `مـنتقي_معرف_أو_حصري` / `IdSelectorXor`: دمج المنتقيات بـ XOR + +### نـتيجة_بحث_مدى / RangeSearchResult + نتائج استعلامات البحث بنطاق. [واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **الدوال:** -- `أنشئ` / `new` - ```alusus - نـتيجة_بحث_مدى.أنشئ(obj: سند[سند[نـتيجة_بحث_مدى]]، nq: صـحيح[64]): صـحيح - ``` -
+#### أنشئ / new - ```alusus - RangeSearchResult.new(obj: ref[ref[RangeSearchResult]], nq: Int[64]): Int - ``` +
+ +``` +دالة أنشئ(obj: سند[سند[نـتيجة_بحث_مدى]]، nq: صـحيح[64]): صـحيح +```
-- `نفذ_التخصيص` / `doAllocation` - ```alusus - نـتيجة_بحث_مدى.نفذ_التخصيص(): صـحيح - ``` +``` +func new(obj: ref[ref[RangeSearchResult]], nq: Int[64]): Int +``` -
+#### نفذ_التخصيص / doAllocation - ```alusus - RangeSearchResult.doAllocation(): Int - ``` +
+ +``` +عملية هذا.نفذ_التخصيص(): صـحيح +```
- تخصيص صوانات النتائج +``` +handler this.doAllocation(): Int +``` + +تخصيص صوانات النتائج. -- `حجم_الصوان` / `bufferSize` - ```alusus - نـتيجة_بحث_مدى.حجم_الصوان(): كلمة_معمارية - ``` +#### حجم_الصوان / bufferSize -
+
- ```alusus - RangeSearchResult.bufferSize(): ArchWord - ``` +``` +عملية هذا.حجم_الصوان(): طـبيعي_متكيف +```
- الحصول على حجم الصوان +``` +handler this.bufferSize(): ArchWord +``` -- `هات_الحدود` / `getLims` - ```alusus - نـتيجة_بحث_مدى.هات_الحدود(outLims: سند[سند[مصفوفة[كلمة_معمارية]]]) - ``` +الحصول على حجم الصوان. -
+#### هات_الحدود / getLims - ```alusus - RangeSearchResult.getLims(outLims: ref[ref[array[ArchWord]]]) - ``` +
+ +``` +عملية هذا.هات_الحدود(outLims: سند[سند[مصفوفة[طـبيعي_متكيف]]]) +```
- الحصول على مصفوفة حدود النتائج +``` +handler this.getLims(outLims: ref[ref[array[ArchWord]]]) +``` + +الحصول على مصفوفة حدود النتائج. -- `هات_الوسوم` / `getLabels` - ```alusus - نـتيجة_بحث_مدى.هات_الوسوم(outLabels: سند[سند[مصفوفة[صـحيح[64]]]]، outDistances: سند[سند[سند[عـائم]]]) - ``` +#### هات_الوسوم / getLabels -
+
- ```alusus - RangeSearchResult.getLabels(outLabels: ref[ref[array[Int[64]]]], outDistances: ref[ref[ref[Float]]]) - ``` +``` +عملية هذا.هات_الوسوم(outLabels: سند[سند[مصفوفة[صـحيح[64]]]]، outDistances: سند[سند[سند[عـائم]]]) +```
- الحصول على الوسوم والمسافات +``` +handler this.getLabels(outLabels: ref[ref[array[Int[64]]]], outDistances: ref[ref[ref[Float]]]) +``` + +الحصول على الوسوم والمسافات. + +### حـاسب_مسافة / DistanceComputer -#### حـاسب_مسافة / DistanceComputer حساب المسافات إلى المتجهات. [واجهة C البرمجية](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **الدوال:** -- `حدد_الاستعلام` / `setQuery` - ```alusus - حـاسب_مسافة.حدد_الاستعلام(x: سند[مصفوفة[عـائم]]): صـحيح - ``` -
+#### حدد_الاستعلام / setQuery - ```alusus - DistanceComputer.setQuery(x: ref[array[Float]]): Int - ``` +
+ +``` +عملية هذا.حدد_الاستعلام(x: سند[مصفوفة[عـائم]]): صـحيح +```
- تعيين متجه الاستعلام +``` +handler this.setQuery(x: ref[array[Float]]): Int +``` + +تعيين متجه الاستعلام. -- `مسافة_متجه_للاستعلام` / `vectorToQueryDis` - ```alusus - حـاسب_مسافة.مسافة_متجه_للاستعلام(i: صـحيح[64]، qd: سند[مصفوفة[عـائم]]): صـحيح - ``` +#### مسافة_متجه_للاستعلام / vectorToQueryDis -
+
- ```alusus - DistanceComputer.vectorToQueryDis(i: Int[64], qd: ref[array[Float]]): Int - ``` +``` +عملية هذا.مسافة_متجه_للاستعلام(i: صـحيح[64]، qd: سند[مصفوفة[عـائم]]): صـحيح +```
- المسافة إلى الاستعلام +``` +handler this.vectorToQueryDis(i: Int[64], qd: ref[array[Float]]): Int +``` -- `مسافة_متماثلة` / `symmetricDis` - ```alusus - حـاسب_مسافة.مسافة_متماثلة(i: صـحيح[64]، j: صـحيح[64]، vd: سند[مصفوفة[عـائم]]): صـحيح - ``` +المسافة إلى الاستعلام. -
+#### مسافة_متماثلة / symmetricDis - ```alusus - DistanceComputer.symmetricDis(i: Int[64], j: Int[64], vd: ref[array[Float]]): Int - ``` +
+ +``` +عملية هذا.مسافة_متماثلة(i: صـحيح[64]، j: صـحيح[64]، vd: سند[مصفوفة[عـائم]]): صـحيح +```
- المسافة المتماثلة +``` +handler this.symmetricDis(i: Int[64], j: Int[64], vd: ref[array[Float]]): Int +``` + +المسافة المتماثلة. ### الثوابت #### نـوع_قياس / MetricType + مقاييس المسافة. [توثيق](https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances) -- `_نتاج_داخلي_` / `METRIC_INNER_PRODUCT`: `0` - الجداء الداخلي (أقصى تشابه) -- `_ل2_` / `METRIC_L2`: `1` - المسافة الإقليدية (معيار L2) -- `_ل1_` / `METRIC_L1`: `2` - مسافة مانهاتن (معيار L1) -- `_ل8_` / `METRIC_LINF`: `3` - معيار اللانهاية (مسافة تشيبيشيف) -- `_لس_` / `METRIC_LP`: `4` - معيار Lp -- `_كانبيرا_` / `METRIC_CANBERRA`: `20` - مسافة كانبيرا -- `_براي_كرتس_` / `METRIC_BRAY_CURTIS`: `21` - تباين براي-كورتس -- `_جنسن_شانون_` / `METRIC_JENSEN_SHANNON`: `22` - تباعد جينسن-شانون +* `_نتاج_داخلي_` / `METRIC_INNER_PRODUCT` (`0`): الجداء الداخلي (أقصى تشابه) +* `_ل2_` / `METRIC_L2` (`1`): المسافة الإقليدية (معيار L2) +* `_ل1_` / `METRIC_L1` (`2`): مسافة مانهاتن (معيار L1) +* `_ل8_` / `METRIC_LINF` (`3`): معيار اللانهاية (مسافة تشيبيشيف) +* `_لس_` / `METRIC_LP` (`4`): معيار Lp +* `_كانبيرا_` / `METRIC_CANBERRA` (`20`): مسافة كانبيرا +* `_براي_كرتس_` / `METRIC_BRAY_CURTIS` (`21`): تباين براي-كورتس +* `_جنسن_شانون_` / `METRIC_JENSEN_SHANNON` (`22`): تباعد جينسن-شانون #### رمـز_خطأ / ErrorCode + رموز الإرجاع من دوال واجهة C البرمجية. -- `_نجاح_` / `OK`: `0` - نجاح -- `_استثناء_مجهول_` / `UNKNOWN_EXCEPT`: `-1` - استثناء غير معروف -- `_استثناء_فيس_` / `FAISS_EXCEPT`: `-2` - استثناء FAISS -- `_استثناء_قياسي_` / `STD_EXCEPT`: `-4` - استثناء المكتبة القياسية +* `_نجاح_` / `OK` (`0`): نجاح +* `_استثناء_مجهول_` / `UNKNOWN_EXCEPT` (`-1`): استثناء غير معروف +* `_استثناء_فيس_` / `FAISS_EXCEPT` (`-2`): استثناء FAISS +* `_استثناء_قياسي_` / `STD_EXCEPT` (`-4`): استثناء المكتبة القياسية ### الدوال -- `هات_آخر_خطأ` / `getLastError` - ```alusus - فـيس.هات_آخر_خطأ(): مـؤشر_محارف - ``` +#### هات_آخر_خطأ / getLastError -
+
- ```alusus - Faiss.getLastError(): CharsPtr - ``` +``` +دالة هات_آخر_خطأ(): مـؤشر_محارف +```
- الحصول على رسالة الخطأ الأخيرة +``` +func getLastError(): CharsPtr +``` + +الحصول على رسالة الخطأ الأخيرة. -- `تجميع_كيمينز` / `kmeansClustering` - ```alusus - فـيس.تجميع_كيمينز(d: كلمة_معمارية، n: كلمة_معمارية، k: كلمة_معمارية، - x: سند[مصفوفة[عـائم]]، centroids: سند[مصفوفة[عـائم]]، q_error: سند[عـائم]): صـحيح - ``` +#### تجميع_كيمينز / kmeansClustering -
+
- ```alusus - Faiss.kmeansClustering(d: ArchWord, n: ArchWord, k: ArchWord, - x: ref[array[Float]], centroids: ref[array[Float]], q_error: ref[Float]): Int - ``` +``` +دالة تجميع_كيمينز(d: طـبيعي_متكيف، n: طـبيعي_متكيف، k: طـبيعي_متكيف، x: سند[مصفوفة[عـائم]]، centroids: سند[مصفوفة[عـائم]]، q_error: سند[عـائم]): صـحيح +```
- k-means مستقل +``` +func kmeansClustering(d: ArchWord, n: ArchWord, k: ArchWord, x: ref[array[Float]], centroids: ref[array[Float]], q_error: ref[Float]): Int +``` + +k-means مستقل. ## دعم GPU لتفعيل تسريع GPU، قم بتعيين متغير البيئة قبل التشغيل: + ```bash export FAISS_USE_GPU=1 ``` @@ -830,13 +1122,13 @@ export FAISS_USE_GPU=1 ## نصوص مصنع الفهرس -تقبل دالة المصنع `أنشئ` / `new` نصوصاً لإنشاء أنواع فهرس مختلفة: +تقبل دالة المصنع `فـهرس.أنشئ` / `Index.new` نصوصاً لإنشاء أنواع فهرس مختلفة: -- `"Flat"` - بحث دقيق (قوة غاشمة) -- `"IVFn,Flat"` - IVF مع n مركز، ترميز مسطح -- `"IVFn,PQm"` - IVF مع n مركز، PQ مع m مكمّم فرعي -- `"HNSW32"` - عالم صغير قابل للتنقل الهرمي مع 32 جار -- `"IVFn,HNSW32"` - IVF و HNSW مدمجان +* `"Flat"`: بحث دقيق (قوة غاشمة) +* `"IVFn,Flat"`: IVF مع n مركز، ترميز مسطح +* `"IVFn,PQm"`: IVF مع n مركز، PQ مع m مكمّم فرعي +* `"HNSW32"`: عالم صغير قابل للتنقل الهرمي مع 32 جار +* `"IVFn,HNSW32"`: IVF و HNSW مدمجان راجع [توثيق مصنع الفهرس](https://github.com/facebookresearch/faiss/wiki/The-index-factory) لجميع الخيارات والتوليفات المتاحة. @@ -844,17 +1136,17 @@ export FAISS_USE_GPU=1 أمثلة عمل كاملة في مجلد `Examples/`: -- **مثال.أسس** - فهرس مسطح أساسي مع بحث الجداء الداخلي (عربي) -- **مثال٢.أسس** - فهرس IVF مع ضبط المعاملات (عربي) -- **example.alusus** - فهرس مسطح أساسي مع بحث الجداء الداخلي (إنجليزي) -- **example2.alusus** - فهرس IVF مع ضبط المعاملات (إنجليزي) +* **مثال.أسس**: فهرس مسطح أساسي مع بحث الجداء الداخلي (عربي) +* **مثال٢.أسس**: فهرس IVF مع ضبط المعاملات (عربي) +* **example.alusus**: فهرس مسطح أساسي مع بحث الجداء الداخلي (إنجليزي) +* **example2.alusus**: فهرس IVF مع ضبط المعاملات (إنجليزي) ## نصائح الأداء 1. **اختيار الفهرس**: - - استخدم `فـهرس_مسطح` / `IndexFlat` للبحث الدقيق في مجموعات البيانات <1 مليون متجه - - استخدم `فـهرس_ملف_معكوس` / `IndexIVF` للبحث التقريبي في مجموعات البيانات الأكبر - - راجع [دليل اختيار الفهرس](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index) + * استخدم `فـهرس_مسطح` / `IndexFlat` للبحث الدقيق في مجموعات البيانات <1 مليون متجه + * استخدم `فـهرس_ملف_معكوس` / `IndexIVF` للبحث التقريبي في مجموعات البيانات الأكبر + * راجع [دليل اختيار الفهرس](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index) 2. **التدريب**: تتطلب فهارس IVF والفهارس التقريبية الأخرى التدريب قبل إضافة المتجهات @@ -868,14 +1160,11 @@ export FAISS_USE_GPU=1 ## موارد إضافية -- **FAISS على GitHub**: https://github.com/facebookresearch/faiss -- **ويكي FAISS**: https://github.com/facebookresearch/faiss/wiki -- **ورقة بحثية**: [البحث عن التشابه بمليارات المقاييس مع GPUs](https://arxiv.org/abs/1702.08734) -- **لغة الأسس**: https://alusus.org +* **FAISS على GitHub**: https://github.com/facebookresearch/faiss +* **ويكي FAISS**: https://github.com/facebookresearch/faiss/wiki +* **ورقة بحثية**: [البحث عن التشابه بمليارات المقاييس مع GPUs](https://arxiv.org/abs/1702.08734) +* **لغة الأسس**: https://alusus.org ## الترخيص تتبع هذه الروابط ترخيص FAISS (MIT). راجع ملف `LICENSE` للتفاصيل. - -
- diff --git a/README.md b/README.md index ed465aa..4c68661 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Faiss + [[العربية]](README.ar.md) Alusus language bindings for the [FAISS library](https://github.com/facebookresearch/faiss) - A library for efficient similarity search and clustering of dense vectors. @@ -9,7 +10,7 @@ This library provides Alusus bindings to FAISS, enabling high-performance vector ## Installation -```alusus +``` import "Apm"; Apm.importPackage("Alusus/Faiss@0.1"); use Faiss; @@ -17,7 +18,7 @@ use Faiss; ## Quick Start -```alusus +``` import "Srl/Console"; import "Srl/Array"; import "Apm"; @@ -49,198 +50,553 @@ See complete examples in the `Examples/` directory. This library wraps the FAISS C API. For detailed documentation of concepts, algorithms, and best practices, please refer to the official FAISS documentation: -- **Main Documentation**: https://github.com/facebookresearch/faiss/wiki -- **C API Reference**: https://github.com/facebookresearch/faiss/blob/main/c_api/ -- **Getting Started Tutorial**: https://github.com/facebookresearch/faiss/wiki/Getting-started -- **Index Selection Guide**: https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index +* **Main Documentation**: https://github.com/facebookresearch/faiss/wiki +* **C API Reference**: https://github.com/facebookresearch/faiss/blob/main/c_api/ +* **Getting Started Tutorial**: https://github.com/facebookresearch/faiss/wiki/Getting-started +* **Index Selection Guide**: https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index ## API Reference -### Core Classes +### Index -#### Index Main index class for similarity search. [C API docs](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **Static methods:** -- `Index.new(obj: ref[ref[Index]], d: Int, description: CharsPtr, metric: Int): Int` - Create index using factory string -- `Index.load(fname: CharsPtr, flags: Int, obj: ref[ref[Index]]): Int` - Load index from a file -- `Index.save(obj: ref[Index], fname: CharsPtr): Int` - Save index to a file + +#### new + +``` +func new(obj: ref[ref[Index]], d: Int, description: CharsPtr, metric: Int): Int +``` +Create index using factory string. + +#### load + +``` +func load(fname: CharsPtr, flags: Int, obj: ref[ref[Index]]): Int +``` +Load index from a file. + +#### save + +``` +func save(obj: ref[Index], fname: CharsPtr): Int +``` +Save index to a file. + +#### free + +``` +func free(obj: ref[Index]) +``` +Free index memory. **Key methods:** -- `train(n: Int[64], x: ref[array[Float]]): Int` - Train the index on data -- `add(n: Int[64], x: ref[array[Float]]): Int` - Add vectors to index -- `search(n: Int[64], x: ref[array[Float]], k: Int[64], distances: ref[array[Float]], labels: ref[array[Int[64]]]): Int` - Search for k nearest neighbors -- `rangeSearch(n: Int[64], x: ref[array[Float]], radius: Float, result: ref[RangeSearchResult]): Int` - Range search -- `reset(): Int` - Remove all vectors from index -- `removeIds(sel: ref[IdSelector], nRemoved: ref[ArchWord]): Int` - Remove specific vectors + +#### train + +``` +handler this.train(n: Int[64], x: ref[array[Float]]): Int +``` +Train the index on data. + +#### add + +``` +handler this.add(n: Int[64], x: ref[array[Float]]): Int +``` +Add vectors to index. + +#### search + +``` +handler this.search(n: Int[64], x: ref[array[Float]], k: Int[64], distances: ref[array[Float]], labels: ref[array[Int[64]]]): Int +``` +Search for k nearest neighbors. + +#### rangeSearch + +``` +handler this.rangeSearch(n: Int[64], x: ref[array[Float]], radius: Float, result: ref[RangeSearchResult]): Int +``` +Range search. + +#### reset + +``` +handler this.reset(): Int +``` +Remove all vectors from index. + +#### removeIds + +``` +handler this.removeIds(sel: ref[IdSelector], nRemoved: ref[ArchWord]): Int +``` +Remove specific vectors. **Properties:** -- `d: Int[64]` - Vector dimension -- `nTotal: Int[64]` - Total number of indexed vectors -- `isTrained: Int` - Whether index is trained (0 or 1) -- `metricType: MetricType` - Distance metric being used -- `verbose: Int` - Verbosity level -**Cleanup:** -- `Index.free(obj: ref[Index])` - Free index memory +#### d + +``` +d: Int[64]; +``` +Vector dimension. + +#### nTotal + +``` +nTotal: Int[64] +``` +Total number of indexed vectors. + +#### isTrained + +``` +isTrained: Int +``` +Whether index is trained (0 or 1). + +#### metricType + +``` +metricType: MetricType +``` +Distance metric being used. + +#### verbose + +``` +verbose: Int +``` +Verbosity level. + +### IndexFlat -#### IndexFlat Brute-force index performing exact search. [Guide](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes#flat-indexes) **Creation:** -- `IndexFlat.new(obj: ref[ref[IndexFlat]]): Int` -- `IndexFlat.new(obj: ref[ref[IndexFlat]], d: Int[64], metric: MetricType): Int` + +#### new + +``` +func new(obj: ref[ref[IndexFlat]]): Int +func new(obj: ref[ref[IndexFlat]], d: Int[64], metric: MetricType): Int +``` **Additional methods:** -- `getXb(outXb: ref[ref[array[Float]]], outSize: ref[ArchWord])` - Get stored vectors -- `computeDistanceSubset(n: Int[64], x: ref[array[Float]], k: Int[64], outDistances: ref[array[Float]], labels: ref[array[Int[64]]]): Int` - Compute distances to subset + +#### getXb + +``` +handler this.getXb(outXb: ref[ref[array[Float]]], outSize: ref[ArchWord]) +``` +Get stored vectors. + +#### computeDistanceSubset + +``` +handler this.computeDistanceSubset(n: Int[64], x: ref[array[Float]], k: Int[64], outDistances: ref[array[Float]], labels: ref[array[Int[64]]]): Int +``` +Compute distances to subset. Inherits all Index methods. -#### IndexFlatIp +### IndexFlatIp + Flat index specialized for inner product metric. [Docs](https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances) **Creation:** -- `IndexFlatIp.new(obj: ref[ref[IndexFlatIp]]): Int` -- `IndexFlatIp.new(obj: ref[ref[IndexFlatIp]], d: Int[64]): Int` -#### IndexFlatL2 +#### new + +``` +func new(obj: ref[ref[IndexFlatIp]]): Int +func new(obj: ref[ref[IndexFlatIp]], d: Int[64]): Int +``` + +### IndexFlatL2 + Flat index specialized for L2 (Euclidean) distance. [Docs](https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances) +#### new + **Creation:** -- `IndexFlatL2.new(obj: ref[ref[IndexFlatL2]]): Int` -- `IndexFlatL2.new(obj: ref[ref[IndexFlatL2]], d: Int[64]): Int` -#### IndexIvf +``` +func IndexFlatL2.new(obj: ref[ref[IndexFlatL2]]): Int +func IndexFlatL2.new(obj: ref[ref[IndexFlatL2]], d: Int[64]): Int +``` + +### IndexIvf + Inverted file index for faster approximate search. [Guide](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes#cell-probe-methods-indexivf-indexes) **Additional properties:** -- `nList: ArchWord` - Number of inverted lists (clusters) -- `nProbe: ArchWord` - Number of clusters to visit during search (tunable) -- `quantizer: ref[Index]` - Quantizer index -- `ownFields: Int` - Whether index owns its fields + +#### nList + +``` +nList: ArchWord +``` +Number of inverted lists (clusters). + +#### nProbe + +``` +nProbe: ArchWord +``` +Number of clusters to visit during search (tunable). + +#### quantizer + +``` +quantizer: ref[Index] +``` +Quantizer index. + +#### ownFields + +``` +ownFields: Int +``` +Whether index owns its fields. **Additional methods:** -- `mergeFrom(other: ref[IndexIvf], addId: Int[64]): Int` - Merge another IVF index -- `copySubsetTo(other: ref[IndexIvf], subsetType: Int, a1: Int[64], a2: Int[64]): Int` - Copy subset of vectors -- `getListSize(listNo: ArchWord): ArchWord` - Get size of inverted list -- `makeDirectMap(newMaintainDirectMap: Int): Int` - Create direct map for reconstruction -- `imbalanceFactor: Float[64]` - Get cluster imbalance factor -- `printStats()` - Print index statistics - -#### IndexBinary + +#### mergeFrom + +``` +handler this.mergeFrom(other: ref[IndexIvf], addId: Int[64]): Int +``` +Merge another IVF index. + +#### copySubsetTo + +``` +handler this.copySubsetTo(other: ref[IndexIvf], subsetType: Int, a1: Int[64], a2: Int[64]): Int +``` +Copy subset of vectors. + +#### getListSize + +``` +handler this.getListSize(listNo: ArchWord): ArchWord +``` +Get size of inverted list. + +#### makeDirectMap + +``` +handler this.makeDirectMap(newMaintainDirectMap: Int): Int +``` +Create direct map for reconstruction. + +#### imbalanceFactor + +``` +handler this.imbalanceFactor: Float[64] +``` +Get cluster imbalance factor. + +#### printStats + +``` +handler this.printStats() +``` +Print index statistics. + +### IndexBinary + Index for binary (hamming) vectors. [Guide](https://github.com/facebookresearch/faiss/wiki/Binary-indexes) Similar to Index but operates on binary vectors (Word[8] arrays instead of Float arrays). -### Support Classes +**Support Classes** + +### ParameterSpace -#### ParameterSpace Manages index parameters for grid search and tuning. [C API](https://github.com/facebookresearch/faiss/blob/main/c_api/ParameterSpace_c.h) **Methods:** -- `new(parameterSpace: ref[ref[ParameterSpace]]): Int` -- `setIndexParameter(index: ref[Index], paramName: CharsPtr, val: Float[64]): Int` - Set single parameter -- `setIndexParameters(index: ref[Index], params: CharsPtr): Int` - Set multiple parameters -- `addRange(name: CharsPtr, outRange: ref[ref[ParameterRange]]): Int` - Add parameter range -#### SearchParameters +#### new + +``` +func new(parameterSpace: ref[ref[ParameterSpace]]): Int +``` + +#### setIndexParameter + +``` +handler this.setIndexParameter(index: ref[Index], paramName: CharsPtr, val: Float[64]): Int +``` +Set single parameter. + +#### setIndexParameters + +``` +handler this.setIndexParameters(index: ref[Index], params: CharsPtr): Int +``` +Set multiple parameters. + +#### addRange + +``` +handler this.addRange(name: CharsPtr, outRange: ref[ref[ParameterRange]]): Int +``` +Add parameter range. + +### SearchParameters + Runtime search parameters. [C API](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **Methods:** -- `new(obj: ref[ref[SearchParameters]], sel: ref[IdSelector]): Int` -- `nProbe: Int` - Number of clusters to probe (for IVF indexes) -#### SearchParametersIvf +#### new + +``` +func new(obj: ref[ref[SearchParameters]], sel: ref[IdSelector]): Int +``` + +**Properties** + +#### nProbe + +``` +nProbe: Int +``` +Number of clusters to probe (for IVF indexes). + +### SearchParametersIvf + Extended search parameters for IVF indexes. **Methods:** -- `new(obj: ref[ref[SearchParametersIvf]]): Int` -- `new(obj: ref[ref[SearchParametersIvf]], sel: ref[IdSelector], nprobe: ArchWord, maxCodes: ArchWord): Int` + +#### new + +``` +func new(obj: ref[ref[SearchParametersIvf]]): Int +func new(obj: ref[ref[SearchParametersIvf]], sel: ref[IdSelector], nprobe: ArchWord, maxCodes: ArchWord): Int +``` **Properties:** -- `sel: ref[IdSelector]` - ID selector -- `nProbe: ArchWord` - Number of clusters to probe -- `maxCodes: ArchWord` - Maximum codes to scan -#### Clustering +#### sel + +``` +sel: ref[IdSelector] +``` +ID selector. + +#### nProbe + +``` +nProbe: ArchWord +``` +Number of clusters to probe. + +#### maxCodes + +``` +maxCodes: ArchWord +``` +Maximum codes to scan. + +### Clustering + K-means clustering implementation. [C API](https://github.com/facebookresearch/faiss/blob/main/c_api/Clustering_c.h) **Creation:** -- `new(out: ref[ref[Clustering]], d: Int, k: Int): Int` - Create with dimension and k clusters -- `new(out: ref[ref[Clustering]], d: Int, k: Int, params: ptr[ClusteringParameters]): Int` - Create with parameters -**Methods:** -- `train(n: Int[64], x: ref[Float], index: ref[Index]): Int` - Run k-means -- `getCentroids(centroids: ref[ref[array[Float]]], size: ref[ArchWord])` - Get cluster centroids -- `getIterationStats(stats_out: ref[ref[ClusteringIterationStats]], size: ref[ArchWord])` - Get iteration statistics +#### new + +``` +func new(out: ref[ref[Clustering]], d: Int, k: Int): Int + +func new(out: ref[ref[Clustering]], d: Int, k: Int, params: ptr[ClusteringParameters]): Int +``` +First form create with dimension and k clusters. + +Second form create with parameters. + +**Methods** + +#### train + +``` +handler this.train(n: Int[64], x: ref[Float], index: ref[Index]): Int +``` +Run k-means. + +#### getCentroids + +``` +handler this.getCentroids(centroids: ref[ref[array[Float]]], size: ref[ArchWord]) +``` +Get cluster centroids. + +#### getIterationStats + +``` +handler this.getIterationStats(stats_out: ref[ref[ClusteringIterationStats]], size: ref[ArchWord]) +``` +Get iteration statistics. **Properties:** -- `niter: Int` - Number of iterations -- `nredo: Int` - Number of k-means restarts -- `k: ArchWord` - Number of clusters -- `d: ArchWord` - Vector dimension -#### IdSelector +#### niter + +``` +niter: Int +``` +Number of iterations. + +#### nredo + +``` +nredo: Int +``` +Number of k-means restarts. + +#### k + +``` +k: ArchWord +``` +Number of clusters. + +#### d + +``` +d: ArchWord +``` +Vector dimension. + +### IdSelector + Select subsets of vectors by ID. [C API](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **Variants:** -- `IdSelectorBatch` - Select specific IDs from a list -- `IdSelectorRange` - Select IDs in a range -- `IdSelectorBitmap` - Select using a bitmap -- `IdSelectorNot` - Invert a selector -- `IdSelectorAnd` - Combine selectors with AND -- `IdSelectorOr` - Combine selectors with OR -- `IdSelectorXor` - Combine selectors with XOR - -#### RangeSearchResult +* `IdSelectorBatch`: Select specific IDs from a list +* `IdSelectorRange`: Select IDs in a range +* `IdSelectorBitmap`: Select using a bitmap +* `IdSelectorNot`: Invert a selector +* `IdSelectorAnd`: Combine selectors with AND +* `IdSelectorOr`: Combine selectors with OR +* `IdSelectorXor`: Combine selectors with XOR + +### RangeSearchResult + Results from range search queries. [C API](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) **Methods:** -- `new(obj: ref[ref[RangeSearchResult]], nq: Int[64]): Int` -- `doAllocation(): Int` - Allocate result buffers -- `bufferSize(): ArchWord` - Get buffer size -- `getLims(outLims: ref[ref[array[ArchWord]]])` - Get result limits array -- `getLabels(outLabels: ref[ref[array[Int[64]]]], outDistances: ref[ref[ref[Float]]])` - Get labels and distances -#### DistanceComputer +#### new + +``` +func new(obj: ref[ref[RangeSearchResult]], nq: Int[64]): Int +``` + +#### doAllocation + +``` +handler this.doAllocation(): Int +``` +Allocate result buffers. + +#### bufferSize + +``` +handler this.bufferSize(): ArchWord +``` +Get buffer size. + +#### getLims + +``` +handler this.getLims(outLims: ref[ref[array[ArchWord]]]) +``` +Get result limits array. + +#### getLabels + +``` +handler this.getLabels(outLabels: ref[ref[array[Int[64]]]], outDistances: ref[ref[ref[Float]]]) +``` +Get labels and distances. + +### DistanceComputer + Compute distances to vectors. [C API](https://github.com/facebookresearch/faiss/blob/main/c_api/Index_c.h) -**Methods:** -- `setQuery(x: ref[array[Float]]): Int` - Set query vector -- `vectorToQueryDis(i: Int[64], qd: ref[array[Float]]): Int` - Distance to query -- `symmetricDis(i: Int[64], j: Int[64], vd: ref[array[Float]]): Int` - Symmetric distance +#### setQuery + +``` +handler this.setQuery(x: ref[array[Float]]): Int +``` +Set query vector. + +#### vectorToQueryDis + +``` +handler this.vectorToQueryDis(i: Int[64], qd: ref[array[Float]]): Int +``` +Distance to query. + +#### symmetricDis + +``` +handler this.symmetricDis(i: Int[64], j: Int[64], vd: ref[array[Float]]): Int +``` +Symmetric distance. ### Constants #### MetricType + Distance metrics. [Docs](https://github.com/facebookresearch/faiss/wiki/MetricType-and-distances) -- `METRIC_INNER_PRODUCT: 0` - Inner product (maximum similarity) -- `METRIC_L2: 1` - Euclidean distance (L2 norm) -- `METRIC_L1: 2` - Manhattan distance (L1 norm) -- `METRIC_LINF: 3` - Infinity norm (Chebyshev distance) -- `METRIC_LP: 4` - Lp norm -- `METRIC_CANBERRA: 20` - Canberra distance -- `METRIC_BRAY_CURTIS: 21` - Bray-Curtis dissimilarity -- `METRIC_JENSEN_SHANNON: 22` - Jensen-Shannon divergence +* `METRIC_INNER_PRODUCT` (0): Inner product (maximum similarity) +* `METRIC_L2` (1): Euclidean distance (L2 norm) +* `METRIC_L1` (2): Manhattan distance (L1 norm) +* `METRIC_LINF` (3): Infinity norm (Chebyshev distance) +* `METRIC_LP` (4): Lp norm +* `METRIC_CANBERRA` (20): Canberra distance +* `METRIC_BRAY_CURTIS` (21): Bray-Curtis dissimilarity +* `METRIC_JENSEN_SHANNON` (22): Jensen-Shannon divergence #### ErrorCode + Return codes from C API functions. -- `OK: 0` - Success -- `UNKNOWN_EXCEPT: -1` - Unknown exception -- `FAISS_EXCEPT: -2` - FAISS exception -- `STD_EXCEPT: -4` - Standard library exception +* `OK` (0): Success +* `UNKNOWN_EXCEPT` (-1): Unknown exception +* `FAISS_EXCEPT` (-2): FAISS exception +* `STD_EXCEPT` (-4): Standard library exception ### Functions -- `getLastError(): CharsPtr` - Get last error message -- `kmeansClustering(d: ArchWord, n: ArchWord, k: ArchWord, x: ref[array[Float]], centroids: ref[array[Float]], q_error: ref[Float]) Int` - Standalone k-means +#### getLastError + +``` +func getLastError(): CharsPtr +``` +Get last error message. + +#### kmeansClustering + +``` +func kmeansClustering(d: ArchWord, n: ArchWord, k: ArchWord, x: ref[array[Float]], centroids: ref[array[Float]], q_error: ref[Float]) Int +``` +Standalone k-means. ## GPU Support To enable GPU acceleration, set the environment variable before running: + ```bash export FAISS_USE_GPU=1 ``` @@ -251,11 +607,11 @@ The library will automatically load GPU-enabled binaries when available. See [FA The `Index.new` factory method accepts strings to create different index types: -- `"Flat"` - Exact search (brute force) -- `"IVFn,Flat"` - IVF with n centroids, flat encoding -- `"IVFn,PQm"` - IVF with n centroids, PQ with m subquantizers -- `"HNSW32"` - Hierarchical navigable small world with 32 neighbors -- `"IVFn,HNSW32"` - Combined IVF and HNSW +* `"Flat"`: Exact search (brute force) +* `"IVFn,Flat"`: IVF with n centroids, flat encoding +* `"IVFn,PQm"`: IVF with n centroids, PQ with m subquantizers +* `"HNSW32"`: Hierarchical navigable small world with 32 neighbors +* `"IVFn,HNSW32"`: Combined IVF and HNSW See the [index factory documentation](https://github.com/facebookresearch/faiss/wiki/The-index-factory) for all available options and combinations. @@ -263,15 +619,15 @@ See the [index factory documentation](https://github.com/facebookresearch/faiss/ Complete working examples are in the `Examples/` directory: -- **example.alusus** - Basic flat index with inner product search -- **example2.alusus** - IVF index with parameter tuning +* **example.alusus**: Basic flat index with inner product search +* **example2.alusus**: IVF index with parameter tuning ## Performance Tips 1. **Index Selection**: - - Use `IndexFlat` for exact search on datasets <1M vectors - - Use `IndexIVF` for approximate search on larger datasets - - See the [index selection guide](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index) + * Use `IndexFlat` for exact search on datasets <1M vectors + * Use `IndexIVF` for approximate search on larger datasets + * See the [index selection guide](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index) 2. **Training**: IVF and other approximate indexes require training before adding vectors @@ -285,10 +641,10 @@ See [FAISS performance guidelines](https://github.com/facebookresearch/faiss/wik ## Additional Resources -- **FAISS GitHub**: https://github.com/facebookresearch/faiss -- **FAISS Wiki**: https://github.com/facebookresearch/faiss/wiki -- **Research Paper**: [Billion-scale similarity search with GPUs](https://arxiv.org/abs/1702.08734) -- **Alusus Language**: https://alusus.org +* **FAISS GitHub**: https://github.com/facebookresearch/faiss +* **FAISS Wiki**: https://github.com/facebookresearch/faiss/wiki +* **Research Paper**: [Billion-scale similarity search with GPUs](https://arxiv.org/abs/1702.08734) +* **Alusus Language**: https://alusus.org ## License