| 71 | namespace lsh_projection { |
| 72 | |
| 73 | TfLiteStatus Resize(TfLiteContext* context, TfLiteNode* node) { |
| 74 | auto* params = |
| 75 | reinterpret_cast<TfLiteLSHProjectionParams*>(node->builtin_data); |
| 76 | TF_LITE_ENSURE(context, NumInputs(node) == 2 || NumInputs(node) == 3); |
| 77 | TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); |
| 78 | |
| 79 | const TfLiteTensor* hash = GetInput(context, node, 0); |
| 80 | TF_LITE_ENSURE_EQ(context, NumDimensions(hash), 2); |
| 81 | // Support up to 32 bits. |
| 82 | TF_LITE_ENSURE(context, SizeOfDimension(hash, 1) <= 32); |
| 83 | |
| 84 | const TfLiteTensor* input = GetInput(context, node, 1); |
| 85 | TF_LITE_ENSURE(context, NumDimensions(input) >= 1); |
| 86 | TF_LITE_ENSURE(context, SizeOfDimension(input, 0) >= 1); |
| 87 | |
| 88 | if (NumInputs(node) == 3) { |
| 89 | const TfLiteTensor* weight = GetInput(context, node, 2); |
| 90 | TF_LITE_ENSURE_EQ(context, NumDimensions(weight), 1); |
| 91 | TF_LITE_ENSURE_EQ(context, SizeOfDimension(weight, 0), |
| 92 | SizeOfDimension(input, 0)); |
| 93 | } |
| 94 | |
| 95 | TfLiteTensor* output = GetOutput(context, node, 0); |
| 96 | TfLiteIntArray* outputSize = TfLiteIntArrayCreate(1); |
| 97 | switch (params->type) { |
| 98 | case kTfLiteLshProjectionSparse: |
| 99 | outputSize->data[0] = SizeOfDimension(hash, 0); |
| 100 | break; |
| 101 | case kTfLiteLshProjectionDense: |
| 102 | outputSize->data[0] = SizeOfDimension(hash, 0) * SizeOfDimension(hash, 1); |
| 103 | break; |
| 104 | default: |
| 105 | return kTfLiteError; |
| 106 | } |
| 107 | return context->ResizeTensor(context, output, outputSize); |
| 108 | } |
| 109 | |
| 110 | // Compute sign bit of dot product of hash(seed, input) and weight. |
| 111 | // NOTE: use float as seed, and convert it to double as a temporary solution |
no test coverage detected