From beb51c26ed71721ea039cab130df39ecb9e6c42e Mon Sep 17 00:00:00 2001 From: xupan Date: Tue, 22 Nov 2022 20:36:46 +0800 Subject: [PATCH] support int input for Unique op --- .../cpu/kernel/nnacl/fp32/unique_fp32.c | 25 +++++++++++++++++++ .../cpu/kernel/nnacl/fp32/unique_fp32.h | 1 + .../src/litert/kernel/cpu/fp32/unique_fp32.cc | 3 +++ 3 files changed, 29 insertions(+) diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.c b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.c index 110086e8d60..34962d00a98 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.c +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.c @@ -40,3 +40,28 @@ void Unique(const float *input, int input_len, float *output0, int *output0_len, } } } + +int FindInt(const int *array, int len, int target) { + if (array == NULL) { + return -1; + } + for (int i = 0; i < len; ++i) { + if (array[i] == target) { + return i; + } + } + return -1; +} + +void UniqueInt(const int *input, int input_len, int *output0, int *output0_len, int *output1) { + *output0_len = 0; + for (int i = 0; i < input_len; i++) { + int idx = FindInt(output0, *output0_len, input[i]); + if (idx != -1) { + *output1++ = idx; + } else { + output0[(*output0_len)++] = input[i]; + *output1++ = *output0_len - 1; + } + } +} diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.h b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.h index 2051048c74c..de54af1c389 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.h +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/fp32/unique_fp32.h @@ -28,6 +28,7 @@ typedef struct UniqueParameter { extern "C" { #endif void Unique(const float *input, int input_len, float *output0, int *output0_len, int *output1); +void UniqueInt(const int *input, int input_len, int *output0, int *output0_len, int *output1); #ifdef __cplusplus } #endif diff --git a/mindspore/lite/src/litert/kernel/cpu/fp32/unique_fp32.cc b/mindspore/lite/src/litert/kernel/cpu/fp32/unique_fp32.cc index c34830ba74d..1b9b97ece0e 100644 --- a/mindspore/lite/src/litert/kernel/cpu/fp32/unique_fp32.cc +++ b/mindspore/lite/src/litert/kernel/cpu/fp32/unique_fp32.cc @@ -53,6 +53,9 @@ int UniqueCPUKernel::Run() { UniqueFp16(static_cast(input), in_tensors_[0]->ElementsNum(), static_cast(output0), &output0_len, output1); #endif + } else if (in_tensors_[0]->data_type() == kNumberTypeInt32) { + UniqueInt(static_cast(input), in_tensors_[0]->ElementsNum(), static_cast(output0), &output0_len, + output1); } else { Unique(static_cast(input), in_tensors_[0]->ElementsNum(), static_cast(output0), &output0_len, output1);