ComputeDistance

API definition

APP_ERROR ComputeDistance(int n, const float16_t *queries, float *distances, unsigned int tableLen = 0, const float *table = nullptr);

Function

Queries the distances between n feature vectors and all feature vectors in the database. For example, if a valid mapping table (tableLen > 0 and table is a non-null pointer) is transferred, the distance after mapping is output.

Input

int n: number of feature vectors to be queried.

const float16_t *queries: feature vectors to be queried. The length is n × dim (vector dimension).

unsigned int tableLen: length of the mapping table. The default value is 0, indicating that no mapping is performed. Currently, the mapping table length can be set to 10000.

const float *table: pointer to the mapping table, pointing to the storage space of valid mapping values with the tableLen length. Currently, the supported redundancy length is 48, that is, the length of the space to which table points is 10048 × sizeof(float) bytes.

Output

float *distances: external memory, which stores the distance between the query vector and the base vector. The total length is n × nTotalPad (ntotalPad = (ntotal + 15)/16 × 16, that is, ntotal is padded with 16).

Return value

APP_ERROR: return status. For details, see Return Code Reference.

Restrictions

  • n: The value range is (0, capacity].
  • distances: The space length to be provided is n × ntotalPad. ntotalPad = (ntotal + 15)/16 × 16. That is, the result is 16-padded by ntotal. The valid query comparison distance is stored in the space of the first ntotal. The padded value does not provide any significance.
  • If both tableLen and table meet the requirements during the parameter pass, the API maps the calculated distance.

    First, the distance value is normalized to a floating point number f1 in the range of [0, 1], and then f1 is multiplied by tableLen and rounded up to obtain an integer index between [0, tableLen]. Then, the integer index is used as an offset to obtain the corresponding score from the memory space pointed to by table. The mapping is completed, and score is saved to distance.

    The index mapping formula is as follows: ((CosDistance + 1)/2) × tableLen

  • queries and distances must be non-null pointers and their lengths must meet the requirements. Otherwise, an out-of-bounds read/write error may occur, causing program breakdown.

API definition

APP_ERROR ComputeDistance(int n, const float *queries, float *distances, unsigned int tableLen = 0, const float *table = nullptr);

Function

Queries the distances between n feature vectors and all feature vectors in the database. For example, if a valid mapping table (tableLen > 0 and table is a non-null pointer) is transferred, the distance after mapping is output.

Input

int n: number of feature vectors to be queried.

const float *queries: feature vectors to be queried. The length is n × dim (vector dimension).

unsigned int tableLen: length of the mapping table. The default value is 0, indicating that no mapping is performed. Currently, the mapping table length can be set to 10000.

const float *table: pointer to the mapping table, pointing to the storage space of valid mapping values with the tableLen length. Currently, the supported redundancy length is 48, that is, the length of the space to which table points is 10048 × sizeof(float) bytes.

Output

float *distances: external memory, which stores the distance between the query vector and the base vector. The total length is n × nTotalPad (ntotalPad = (ntotal + 15)/16 × 16, that is, ntotal is padded with 16).

Return value

APP_ERROR: return status. For details, see Return Code Reference.

Restrictions

  • n: The value range is (0, capacity].
  • distances: The space length to be provided is n × ntotalPad. ntotalPad = (ntotal + 15)/16 × 16. That is, the result is 16-padded by ntotal. The valid query comparison distance is stored in the space of the first ntotal. The padded value does not provide any significance.
  • If both tableLen and table meet the requirements during the parameter pass, the API maps the calculated distance.

    First, the distance value is normalized to a floating point number f1 in the range of [0, 1], and then f1 is multiplied by tableLen and rounded up to obtain an integer index between [0, tableLen]. Then, the integer index is used as an offset to obtain the corresponding score from the memory space pointed to by table. The mapping is completed, and score is saved to distance.

    The index mapping formula is as follows: ((CosDistance + 1)/2) × tableLen

  • queries and distances must be non-null pointers and their lengths must meet the requirements. Otherwise, an out-of-bounds read/write error may occur, causing program breakdown.