Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【PaddlePaddle Hackathon 2】18、为 Paddle 新增 paddle.heaviside 和 paddle.Te…
…nsor.heaviside API (#41872) * Create elementwise_heaviside_op.cc * add ElementwiseHeavisideFunctor * Create test_elementwise_heaviside_op.py * 增加heaviside的python接口 * add heaviside in white list * 增加heaviside的签名 * 增加heaviside的核函数 * 增加heaviside梯度的核函数 * 增加heaviside梯度的注册 * 调整代码格式 * Update elementwise_sig.cc * add heaviside in __all__ * Update heaviside docs * Update math.py * Update math.py * Update math.py
- Loading branch information
1 parent
8164414
commit 4892d59
Showing
17 changed files
with
468 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
paddle/fluid/operators/elementwise/elementwise_heaviside_op.cc
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,70 @@ | ||
// Copyright (c) 2022 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 <string> | ||
#include "paddle/fluid/operators/elementwise/elementwise_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class ElementwiseHeavisideOpMaker : public ElementwiseOpMaker { | ||
protected: | ||
std::string GetName() const override { return "Heaviside"; } | ||
std::string GetEquation() const override { return "Out = Heaviside(X, Y)"; } | ||
|
||
void AddInputX() override { | ||
AddInput("X", | ||
"(Tensor), The input tensor of Heaviside step function. " | ||
"Its dtype can be int32, int64, float32 and float64"); | ||
} | ||
|
||
void AddInputY() override { | ||
AddInput("Y", | ||
"(Tensor), The tensor determining a Heaviside step function, " | ||
"which is the value when X = 0. Its dtype should be same as X."); | ||
} | ||
|
||
std::string GetOpFuntionality() const override { | ||
return "Computes the Heaviside step function determined by Y " | ||
"for each element in X."; | ||
} | ||
}; | ||
|
||
template <typename T> | ||
class ElementwiseHeavisideGradOpMaker : public framework::SingleGradOpMaker<T> { | ||
public: | ||
using framework::SingleGradOpMaker<T>::SingleGradOpMaker; | ||
|
||
protected: | ||
void Apply(GradOpPtr<T> op) const override { | ||
op->SetType("elementwise_heaviside_grad"); | ||
op->SetInput("X", this->Input("X")); | ||
op->SetInput("Y", this->Input("Y")); | ||
op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); | ||
op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); | ||
op->SetOutput(framework::GradVarName("Y"), this->InputGrad("Y")); | ||
op->SetAttrMap(this->Attrs()); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OPERATOR( | ||
elementwise_heaviside, ops::ElementwiseOp, ops::ElementwiseHeavisideOpMaker, | ||
ops::ElementwiseHeavisideGradOpMaker<paddle::framework::OpDesc>, | ||
ops::ElementwiseHeavisideGradOpMaker<paddle::imperative::OpBase>); | ||
|
||
REGISTER_OPERATOR(elementwise_heaviside_grad, ops::ElementwiseOpGrad); |
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.