-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add unique_consecutive_op #34334
Merged
Merged
add unique_consecutive_op #34334
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
452ebab
add unique_consecutive_op
firestonelib da812ea
add unique_consecutive_op
firestonelib ac9a082
add unique_consecutive_op
firestonelib 88956dd
add unique_consecutive_op
firestonelib 5b6593f
add unique_consecutive_op
firestonelib 14bfb51
add unique_consecutive_op
firestonelib 54e7b8d
add unique_consecutive_op
firestonelib 4205bb4
add unique_consecutive_op
firestonelib d5735eb
remove unity build
firestonelib 2f63c04
add unique_consecutive op
firestonelib d61fdc2
add unique_consecutive op
firestonelib d0cee60
add enable static
firestonelib 31985e3
add noqa
firestonelib 51bd235
add space line
firestonelib acc0356
add default case.
firestonelib 27844f8
add comma
firestonelib c8650ba
add space line
firestonelib c19894e
modify unique_consecutive unittest
firestonelib b3f2c98
optimize ut coverage
firestonelib 2b9cfb0
rebase develop
firestonelib 0a8d6e4
improve coverage
firestonelib caebd0c
update en docs
firestonelib 31ac3b8
update en docs
firestonelib dfd6e09
update en docs
firestonelib a7bbfac
update en docs
firestonelib 0e187d8
update en docs
firestonelib f8dff43
update en doc
firestonelib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/fluid/operators/unique_consecutive_op.h" | ||
#include "paddle/fluid/framework/op_version_registry.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class UniqueConsecutiveOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
void InferShape(framework::InferShapeContext* ctx) const override { | ||
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "unique_consecutive"); | ||
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", | ||
"unique_consecutive"); | ||
|
||
auto in_dims = ctx->GetInputDim("X"); | ||
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_inverse) { | ||
OP_INOUT_CHECK(ctx->HasOutput("Index"), "Output", "Index", | ||
"unique_consecutive"); | ||
} | ||
if (return_counts) { | ||
OP_INOUT_CHECK(ctx->HasOutput("Counts"), "Output", "Counts", | ||
"unique_consecutive"); | ||
} | ||
|
||
if (axis_vec.empty()) { | ||
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_counts) { | ||
ctx->SetOutputDim("Counts", {-1}); | ||
} | ||
} | ||
|
||
protected: | ||
framework::OpKernelType GetExpectedKernelType( | ||
const framework::ExecutionContext& ctx) const override { | ||
return framework::OpKernelType( | ||
OperatorWithKernel::IndicateVarDataType(ctx, "X"), ctx.GetPlace()); | ||
} | ||
}; | ||
|
||
class UniqueConsecutiveOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
void Make() override { | ||
AddInput("X", "The input tensor of unique_consecutive op."); | ||
AddAttr<int>("dtype", | ||
"(int, default 5(FP32)) " | ||
"data type for output index") | ||
.SetDefault(framework::proto::VarType::FP32); | ||
|
||
AddOutput("Out", "A unique consecutive subsequence for input tensor."); | ||
AddOutput("Index", | ||
"The indices for where elements in the original input ended up " | ||
"in the returned unique tensor.") | ||
.AsDispensable(); | ||
AddOutput("Counts", "The counts for each unique element.").AsDispensable(); | ||
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({}); | ||
AddComment(R"DOC( | ||
This function is different from paddle.unique() in the sense that this | ||
function only eliminates consecutive duplicate values. | ||
)DOC"); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP_WITHOUT_GRADIENT(unique_consecutive, ops::UniqueConsecutiveOp, | ||
ops::UniqueConsecutiveOpMaker); | ||
REGISTER_OP_CPU_KERNEL( | ||
unique_consecutive, | ||
ops::UniqueConsecutiveKernel<paddle::platform::CPUDeviceContext, float>, | ||
ops::UniqueConsecutiveKernel<paddle::platform::CPUDeviceContext, double>, | ||
ops::UniqueConsecutiveKernel<paddle::platform::CPUDeviceContext, int32_t>, | ||
ops::UniqueConsecutiveKernel<paddle::platform::CPUDeviceContext, int64_t>); | ||
REGISTER_OP_VERSION(unique_consecutive) | ||
.AddCheckpoint( | ||
R"ROC( | ||
Upgrade unique_consecutive, add 2 outputs [Indices, Counts] and 3 attribute | ||
[return_inverse, return_counts, axis]. | ||
)ROC", | ||
paddle::framework::compatible::OpVersionDesc() | ||
.NewOutput("Counts", "The counts for each unique element.") | ||
.NewAttr("return_inverse", | ||
"If True, also return the indices for where elements" | ||
" in the original input ended up in the returned unique " | ||
"tensor.", | ||
false) | ||
.NewAttr("return_counts", | ||
"If True, also return the counts for each unique element.", | ||
false) | ||
.NewAttr("axis", | ||
"The axis to apply unique. If None, the input will be " | ||
"flattened.", | ||
std::vector<int>{})); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2019->2021