|
|
@ -24,17 +24,63 @@ class UniqueOp : public framework::OperatorWithKernel {
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
void InferShape(framework::InferShapeContext* ctx) const override {
|
|
|
|
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "unique");
|
|
|
|
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "unique");
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "unique");
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "unique");
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Index"), "Output", "Index", "unique");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto in_dims = ctx->GetInputDim("X");
|
|
|
|
auto in_dims = ctx->GetInputDim("X");
|
|
|
|
PADDLE_ENFORCE_EQ(
|
|
|
|
if (!ctx->Attrs().Get<bool>("is_sorted")) {
|
|
|
|
in_dims.size(), 1,
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Index"), "Output", "Index", "unique");
|
|
|
|
platform::errors::InvalidArgument("The Input(X) should be 1-D Tensor, "
|
|
|
|
PADDLE_ENFORCE_EQ(in_dims.size(), 1,
|
|
|
|
"But now the dims of Input(X) is %d.",
|
|
|
|
platform::errors::InvalidArgument(
|
|
|
|
in_dims.size()));
|
|
|
|
"The Input(X) should be 1-D Tensor, "
|
|
|
|
|
|
|
|
"But now the dims of Input(X) is %d.",
|
|
|
|
|
|
|
|
in_dims.size()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Out", {-1});
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Index", in_dims);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool return_index = ctx->Attrs().Get<bool>("return_index");
|
|
|
|
|
|
|
|
bool return_inverse = ctx->Attrs().Get<bool>("return_inverse");
|
|
|
|
|
|
|
|
bool return_counts = ctx->Attrs().Get<bool>("return_counts");
|
|
|
|
|
|
|
|
auto axis_vec = ctx->Attrs().Get<std::vector<int>>("axis");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (return_index) {
|
|
|
|
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Indices"), "Output", "Indices", "unique");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (return_inverse) {
|
|
|
|
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Index"), "Output", "Index", "unique");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (return_counts) {
|
|
|
|
|
|
|
|
OP_INOUT_CHECK(ctx->HasOutput("Counts"), "Output", "Counts", "unique");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Out", {-1});
|
|
|
|
if (axis_vec.empty()) {
|
|
|
|
ctx->SetOutputDim("Index", in_dims);
|
|
|
|
ctx->SetOutputDim("Out", {-1});
|
|
|
|
|
|
|
|
if (return_inverse) {
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Index", {framework::product(in_dims)});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
int axis = axis_vec[0];
|
|
|
|
|
|
|
|
if (axis < 0) {
|
|
|
|
|
|
|
|
axis += in_dims.size();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PADDLE_ENFORCE_LT(
|
|
|
|
|
|
|
|
axis, in_dims.size(),
|
|
|
|
|
|
|
|
platform::errors::InvalidArgument("The axis(%d) should be less than "
|
|
|
|
|
|
|
|
"the dimension size(%d) of x.",
|
|
|
|
|
|
|
|
axis, in_dims.size()));
|
|
|
|
|
|
|
|
auto out_dims = in_dims;
|
|
|
|
|
|
|
|
out_dims[axis] = -1;
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Out", out_dims);
|
|
|
|
|
|
|
|
if (return_inverse) {
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Index", {in_dims[axis]});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (return_index) {
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Indices", {-1});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (return_counts) {
|
|
|
|
|
|
|
|
ctx->SetOutputDim("Counts", {-1});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
protected:
|
|
|
@ -49,14 +95,47 @@ class UniqueOp : public framework::OperatorWithKernel {
|
|
|
|
class UniqueOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
class UniqueOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
void Make() override {
|
|
|
|
void Make() override {
|
|
|
|
AddInput("X", "Input tensor. It should be a 1-D tensor.");
|
|
|
|
AddInput("X",
|
|
|
|
|
|
|
|
"Input tensor. It should be a 1-D tensor when Attr(is_sorted)"
|
|
|
|
|
|
|
|
" is fasle or a N-D tensor when Attr(is_sorted) is true.");
|
|
|
|
AddAttr<int>("dtype", "data type for output index");
|
|
|
|
AddAttr<int>("dtype", "data type for output index");
|
|
|
|
AddOutput("Out", "A unique subsequence for input tensor.");
|
|
|
|
AddOutput("Out", "A unique subsequence for input tensor.");
|
|
|
|
AddOutput("Index",
|
|
|
|
AddOutput("Index",
|
|
|
|
"An index tensor pointing to unique subsequence, which has "
|
|
|
|
"Equivalent to inverse in numpy.unique, "
|
|
|
|
"identical shape with input tensor and int64 dtype.");
|
|
|
|
"the indices for where elements in the original input ended up "
|
|
|
|
|
|
|
|
"in the returned unique tensor.");
|
|
|
|
|
|
|
|
AddOutput(
|
|
|
|
|
|
|
|
"Indices",
|
|
|
|
|
|
|
|
"The indices of the input tensor that result in the unique tensor.")
|
|
|
|
|
|
|
|
.AsDispensable();
|
|
|
|
|
|
|
|
AddOutput("Counts", "The counts for each unique element.").AsDispensable();
|
|
|
|
|
|
|
|
AddAttr<bool>("return_index",
|
|
|
|
|
|
|
|
"If True, also return the indices of the input"
|
|
|
|
|
|
|
|
" tensor that result in the unique Tensor.")
|
|
|
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
|
|
|
AddAttr<bool>(
|
|
|
|
|
|
|
|
"return_inverse",
|
|
|
|
|
|
|
|
"If True, also return the indices for where elements"
|
|
|
|
|
|
|
|
" in the original input ended up in the returned unique tensor.")
|
|
|
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
|
|
|
AddAttr<bool>("return_counts",
|
|
|
|
|
|
|
|
"If True, also return the counts for each unique element.")
|
|
|
|
|
|
|
|
.SetDefault(false);
|
|
|
|
|
|
|
|
AddAttr<std::vector<int>>(
|
|
|
|
|
|
|
|
"axis",
|
|
|
|
|
|
|
|
"The axis to apply unique. If None, the input will be flattened.")
|
|
|
|
|
|
|
|
.SetDefault({});
|
|
|
|
|
|
|
|
AddAttr<bool>("is_sorted",
|
|
|
|
|
|
|
|
"If True, the unique elements of X are in ascending order."
|
|
|
|
|
|
|
|
"Otherwise, the unique elements are not sorted.")
|
|
|
|
|
|
|
|
.SetDefault(false);
|
|
|
|
AddComment(R"DOC(
|
|
|
|
AddComment(R"DOC(
|
|
|
|
Return a unique subsequence for 1-D input tensor, and an index tensor pointing to this unique subsequence
|
|
|
|
1. Return a unique subsequence for 1-D input tensor, and an index tensor
|
|
|
|
|
|
|
|
pointing to this unique subsequence when Attr(is_sorted) is false. This
|
|
|
|
|
|
|
|
means paddle.unique is called.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2. Returns the unique elements of X in ascending order when Attr(is_sorted)
|
|
|
|
|
|
|
|
is true. This means fluid.layers.unique is called.
|
|
|
|
)DOC");
|
|
|
|
)DOC");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -65,6 +144,8 @@ class UniqueOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
|
|
|
|
|
|
|
namespace ops = paddle::operators;
|
|
|
|
namespace ops = paddle::operators;
|
|
|
|
REGISTER_OP_WITHOUT_GRADIENT(unique, ops::UniqueOp, ops::UniqueOpMaker);
|
|
|
|
REGISTER_OP_WITHOUT_GRADIENT(unique, ops::UniqueOp, ops::UniqueOpMaker);
|
|
|
|
REGISTER_OP_CPU_KERNEL(unique, ops::UniqueKernel<float>,
|
|
|
|
REGISTER_OP_CPU_KERNEL(
|
|
|
|
ops::UniqueKernel<double>, ops::UniqueKernel<int32_t>,
|
|
|
|
unique, ops::UniqueKernel<paddle::platform::CPUDeviceContext, float>,
|
|
|
|
ops::UniqueKernel<int64_t>);
|
|
|
|
ops::UniqueKernel<paddle::platform::CPUDeviceContext, double>,
|
|
|
|
|
|
|
|
ops::UniqueKernel<paddle::platform::CPUDeviceContext, int32_t>,
|
|
|
|
|
|
|
|
ops::UniqueKernel<paddle::platform::CPUDeviceContext, int64_t>);
|
|
|
|