!45888 support int input for Unique op

Merge pull request !45888 from xupan/unique_int
This commit is contained in:
i-robot 2022-11-24 07:29:38 +00:00 committed by Gitee
commit ca872c6bd2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 29 additions and 0 deletions

View File

@ -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;
}
}
}

View File

@ -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

View File

@ -53,6 +53,9 @@ int UniqueCPUKernel::Run() {
UniqueFp16(static_cast<float16_t *>(input), in_tensors_[0]->ElementsNum(), static_cast<float16_t *>(output0),
&output0_len, output1);
#endif
} else if (in_tensors_[0]->data_type() == kNumberTypeInt32) {
UniqueInt(static_cast<int *>(input), in_tensors_[0]->ElementsNum(), static_cast<int *>(output0), &output0_len,
output1);
} else {
Unique(static_cast<float *>(input), in_tensors_[0]->ElementsNum(), static_cast<float *>(output0), &output0_len,
output1);