ymwangg commented on pull request #7441:
URL: https://github.com/apache/tvm/pull/7441#issuecomment-780229483
@masahi Thanks for your comment.
Here's the algorithm that I came up with based on your suggestions.
```python
# topi
def unique(data, data_sorted, data_argsorted):
output = [0] * len(data)
count = [0] * len(data)
first_occurrence = [len(data)] * len(data)
inverse_indices = [0] * len(data)
num_unique = 0
# ir_builder
for i in range(len(data)):
if i == 0 or data_sorted[i] != data_sorted[i-1]:
num_unique += 1
output[num_unique-1] = data_sorted[i]
first_occurrence[num_unique-1] =
min(first_occurrence[num_unique-1], data_argsorted[i])
count[num_unique-1] += 1
inverse_indices[data_argsorted[i]] = num_unique - 1
return output, count, first_occurrence, inverse_indices, num_unique
# tf front end
def tf_unique(data):
output, count, first_occurrence, inverse_indices, num_unique =
unique(data, np.sort(data), np.argsort(data))
sorted_occurence_indices = np.argsort(first_occurrence) # relay.argsort
new_output = [output[sorted_occurence_indices[i]] for i in
range(num_unique)] # relay.take
index_converter = np.argsort(sorted_occurence_indices) # relay.argsort
new_inverse_indices = [index_converter[i] for i in inverse_indices] #
relay.take
return new_output, new_inverse_indices
```
It defines a topi function that is similar to `np.unique` but requires the
sorted data and the argsort of the data. In the frontend, it needs to do
argsort twice if we want to keep the unique elements in the order of their
first occurrence.
Does this look good to you?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]