17 #include "caffe2/operators/unique_ops.h" 25 bool UniqueOp<CPUContext>::DoRunWithType() {
26 auto& inputTensor = Input(0);
28 int N = inputTensor.dim32(0);
29 CAFFE_ENFORCE_EQ(inputTensor.dim(), 1,
"Input should be a vector");
31 int* remapping =
nullptr;
32 if (REMAPPING < OutputSize()) {
33 auto* remappingTensor =
34 Output(REMAPPING, inputTensor.sizes(), at::dtype<int>());
35 remapping = remappingTensor->template mutable_data<int>();
38 const T* input = inputTensor.template data<T>();
42 std::iota(order_.begin(), order_.end(), 0);
43 std::sort(order_.begin(), order_.end(), [input](
const int x,
const int y) {
44 return input[x] < input[y];
47 for (
int i = 1; i < N; ++i) {
48 K -= input[order_[i]] == input[order_[i - 1]];
50 auto* uniqueTensor = Output(UNIQUE, {K}, at::dtype<T>());
51 T* unique = uniqueTensor->template mutable_data<T>();
54 for (
int i = 0; i < N; ++i) {
55 if (i == 0 || prev != input[order_[i]]) {
56 prev = unique[K++] = input[order_[i]];
59 remapping[order_[i]] = K - 1;
65 REGISTER_CPU_OPERATOR(Unique, UniqueOp<CPUContext>);
67 OPERATOR_SCHEMA(Unique)
71 Deduplicates input indices vector and optionally produces reverse remapping. 72 There's no guarantees on the ordering of the output indices. 74 .Input(0, "indices",
"1D tensor of int32 or int64 indices.")
75 .Output(0,
"unique_indices",
"1D tensor of deduped entries.")
79 "(optional) mapping from `indices` to `unique_indices`. This has the " 80 "same shape as `indices`. Its elements are the indices into " 81 "`unique_indices` such that `Gather(['unique_indices', 'remapping'])` " 83 .TensorInferenceFunction([](
const OperatorDef& def,
84 const vector<TensorShape>& in) {
85 std::vector<TensorShape> out(1);
86 out[0].set_data_type(in[0].data_type());
87 CAFFE_ENFORCE_EQ(in[0].dims_size(), 1);
88 if (in[0].dims(0) <= 1) {
92 out[0].add_dims(in[0].dims(0));
94 out[0].set_unknown_shape(
true);
96 if (def.output_size() > 1) {
99 out.back().set_data_type(TensorProto::INT32);
104 SHOULD_NOT_DO_GRADIENT(Unique);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...