# LshProjection — MindSpore Signal+ 使用手册 alpha 文档

# LshProjection

局部敏感哈希（Locality Sensitive Hashing, LSH）投影算子。该算子通过多个哈希组（Hash Groups）对输入特征进行处理，每组生成一个指定位宽（bits_per_hash）的哈希签名（int32）。

内部逻辑基于 FNV1a 哈希算法和加权评分机制，将高维特征映射为低维的离散哈希值。

计算过程：

  1. 对于每个哈希组 $i$，循环执行 $j$ 次（$j < bits_per_hash$）。

  2. 每次根据特定的哈希种子 $seed_{i,j}$ 计算特征与权重的加权评分，并通过评分符号决定一个 Bit 位（0 或 1）。

  3. 将生成的 Bit 位拼接成一个完整的 32 位整型哈希签名。

输入：

  - hash_seed - 哈希种子数组地址（float 类型）。

  - feature - 输入特征向量地址（int32 类型）。

  - weight - 权重向量地址（类型随算子名而定，若为 NULL 则不加权）。

  - hash_group_num - 生成的哈希组数量（即输出结果的个数）。

  - bits_per_hash - 每组哈希签名的有效位数（通常 $le 32$）。

  - feature_num - 特征向量的维度。

  - core_mask(int, 可选) - 核掩码（仅适用于共享存储版本）。

输出：

  - output - 存储生成的哈希签名地址（int32 数组）。

支持平台：

  `FT78NE` `MT7004`

备注

- FT78NE 支持权重类型：int8 (i8), int16 (i16), int32 (i32), fp32 (fp), fp64 (dp)

- MT7004 支持权重类型：int16 (i16), int32 (i32), fp16 (hp), fp32 (fp)

- 特征输入（feature）在所有平台上固定为 int32 类型。

- 输出结果（output）在所有平台上固定为 int32 类型。

共享存储版本:

void i8_lsh_projection_s(const float *hash_seed, const int32_t *feature, const int8_t *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num, int core_mask)

void i16_lsh_projection_s(const float *hash_seed, const int32_t *feature, const int16_t *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num, int core_mask)

void i32_lsh_projection_s(const float *hash_seed, const int32_t *feature, const int32_t *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num, int core_mask)

void hp_lsh_projection_s(const float *hash_seed, const int32_t *feature, const half *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num, int core_mask)

void fp_lsh_projection_s(const float *hash_seed, const int32_t *feature, const float *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num, int core_mask)

void dp_lsh_projection_s(const float *hash_seed, const int32_t *feature, const double *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num, int core_mask)

  C调用示例：

       1// FT78NE 示例：fp32 权重，多核共享存储
       2#include "78NE/utils.h"
       3
       4int main() {
       5    float* hash_seed = (float*)0xA0000000;
       6    int32_t* feature = (int32_t*)0xA1000000;
       7    float* weight = (float*)0xA2000000;
       8    int32_t* output = (int32_t*)0xB0000000;
       9
      10    int hash_group_num = 128;
      11    int bits_per_hash = 16;
      12    int feature_num = 64;
      13    int core_mask = 0xFF;
      14
      15    fp_lsh_projection_s(hash_seed, feature, weight, output,
      16                        hash_group_num, bits_per_hash, feature_num, core_mask);
      17    return 0;
      18}

私有存储版本:

void i8_lsh_projection_p(const float *hash_seed, const int32_t *feature, const int8_t *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num)

void i16_lsh_projection_p(const float *hash_seed, const int32_t *feature, const int16_t *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num)

void i32_lsh_projection_p(const float *hash_seed, const int32_t *feature, const int32_t *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num)

void hp_lsh_projection_p(const float *hash_seed, const int32_t *feature, const half *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num)

void fp_lsh_projection_p(const float *hash_seed, const int32_t *feature, const float *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num)

void dp_lsh_projection_p(const float *hash_seed, const int32_t *feature, const double *weight, int32_t *output, int hash_group_num, int bits_per_hash, int feature_num)

  C调用示例：

       1// MT7004 示例：fp16 (hp) 权重，私有存储单核
       2#include <stdio.h>
       3
       4int main() {
       5    float* hash_seed = (float*)0x10000000;
       6    int32_t* feature = (int32_t*)0x10001000;
       7    half* weight = (half*)0x10002000;
       8    int32_t* output = (int32_t*)0x10003000;
       9
      10    int hash_group_num = 64;
      11    int bits_per_hash = 8;
      12    int feature_num = 32;
      13
      14    hp_lsh_projection_p(hash_seed, feature, weight, output,
      15                        hash_group_num, bits_per_hash, feature_num);
      16    return 0;
      17}